diff options
author | Gert Driesen <drieseng@users.sourceforge.net> | 2009-06-29 00:39:25 +0400 |
---|---|---|
committer | Gert Driesen <drieseng@users.sourceforge.net> | 2009-06-29 00:39:25 +0400 |
commit | 916aaf1dbb873c1d21d153f43605ed2684e93793 (patch) | |
tree | 28b7acd2ce7a0ea26831a53c416309b1e220b955 /mcs/class | |
parent | 2b3a1eaef41531abeea849d3200c0409c70cc0a7 (diff) |
* SocketTest.cs: Added tests for argument checks, and improved tests
for disposed socket.
* Socket.cs: Modified some argument names to match MS. Moved disposed
check before argument checks in Receive. Use SocketError.InvalidArgument
instead of using magic numbers. Added back comments that was
accidentally removed in my previous commit.
* Socket_2_1.cs: Modified some argument names to match MS.
svn path=/trunk/mcs/; revision=137051
Diffstat (limited to 'mcs/class')
-rw-r--r-- | mcs/class/System/System.Net.Sockets/ChangeLog | 8 | ||||
-rw-r--r-- | mcs/class/System/System.Net.Sockets/Socket.cs | 177 | ||||
-rw-r--r-- | mcs/class/System/System.Net.Sockets/Socket_2_1.cs | 46 | ||||
-rw-r--r-- | mcs/class/System/Test/System.Net.Sockets/ChangeLog | 5 | ||||
-rw-r--r-- | mcs/class/System/Test/System.Net.Sockets/SocketTest.cs | 804 |
5 files changed, 843 insertions, 197 deletions
diff --git a/mcs/class/System/System.Net.Sockets/ChangeLog b/mcs/class/System/System.Net.Sockets/ChangeLog index 94b98e843a2..718eb826055 100644 --- a/mcs/class/System/System.Net.Sockets/ChangeLog +++ b/mcs/class/System/System.Net.Sockets/ChangeLog @@ -1,5 +1,13 @@ 2009-06-28 Gert Driesen <drieseng@users.sourceforge.net> + * Socket.cs: Modified some argument names to match MS. Moved disposed + check before argument checks in Receive. Use SocketError.InvalidArgument + instead of using magic numbers. Added back comments that was + accidentally removed in my previous commit. + * Socket_2_1.cs: Modified some argument names to match MS. + +2009-06-28 Gert Driesen <drieseng@users.sourceforge.net> + * Socket.cs: In GetSocketOption, throw SocketException when (byte []) option value is null. Use SocketError fields instead of using "magic" number. In SetSocketOption (SocketOptionLevel, SocketOptionName, object) diff --git a/mcs/class/System/System.Net.Sockets/Socket.cs b/mcs/class/System/System.Net.Sockets/Socket.cs index 5f1b5128870..2435e1ba914 100644 --- a/mcs/class/System/System.Net.Sockets/Socket.cs +++ b/mcs/class/System/System.Net.Sockets/Socket.cs @@ -2172,32 +2172,32 @@ namespace System.Net.Sockets SocketOptionLevel level, SocketOptionName name, ref byte[] byte_val, out int error); - public void GetSocketOption (SocketOptionLevel level, SocketOptionName name, byte [] opt_value) + public void GetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, byte [] optionValue) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); - if (opt_value == null) + if (optionValue == null) throw new SocketException ((int) SocketError.Fault, "Error trying to dereference an invalid pointer"); int error; - - GetSocketOption_arr_internal(socket, level, name, ref opt_value, + + GetSocketOption_arr_internal (socket, optionLevel, optionName, ref optionValue, out error); if (error != 0) throw new SocketException (error); } - public byte [] GetSocketOption (SocketOptionLevel level, SocketOptionName name, int length) + public byte [] GetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, int length) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); byte[] byte_val=new byte[length]; int error; - - GetSocketOption_arr_internal(socket, level, name, ref byte_val, + + GetSocketOption_arr_internal (socket, optionLevel, optionName, ref byte_val, out error); if (error != 0) throw new SocketException (error); @@ -2302,17 +2302,17 @@ namespace System.Net.Sockets return result; } - public int Receive (byte [] buf) + public int Receive (byte [] buffer) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); - if (buf == null) - throw new ArgumentNullException ("buf"); + if (buffer == null) + throw new ArgumentNullException ("buffer"); SocketError error; - int ret = Receive_nochecks (buf, 0, buf.Length, SocketFlags.None, out error); + int ret = Receive_nochecks (buffer, 0, buffer.Length, SocketFlags.None, out error); if (error != SocketError.Success) throw new SocketException ((int) error); @@ -2320,17 +2320,17 @@ namespace System.Net.Sockets return ret; } - public int Receive (byte [] buf, SocketFlags flags) + public int Receive (byte [] buffer, SocketFlags flags) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); - if (buf == null) - throw new ArgumentNullException ("buf"); + if (buffer == null) + throw new ArgumentNullException ("buffer"); SocketError error; - int ret = Receive_nochecks (buf, 0, buf.Length, flags, out error); + int ret = Receive_nochecks (buffer, 0, buffer.Length, flags, out error); if (error != SocketError.Success) { if (error == SocketError.WouldBlock && blocking) // This might happen when ReceiveTimeout is set @@ -2341,20 +2341,20 @@ namespace System.Net.Sockets return ret; } - public int Receive (byte [] buf, int size, SocketFlags flags) + public int Receive (byte [] buffer, int size, SocketFlags flags) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); - if (buf == null) - throw new ArgumentNullException ("buf"); + if (buffer == null) + throw new ArgumentNullException ("buffer"); - if (size < 0 || size > buf.Length) + if (size < 0 || size > buffer.Length) throw new ArgumentOutOfRangeException ("size"); SocketError error; - int ret = Receive_nochecks (buf, 0, size, flags, out error); + int ret = Receive_nochecks (buffer, 0, size, flags, out error); if (error != SocketError.Success) { if (error == SocketError.WouldBlock && blocking) // This might happen when ReceiveTimeout is set @@ -2365,23 +2365,23 @@ namespace System.Net.Sockets return ret; } - public int Receive (byte [] buf, int offset, int size, SocketFlags flags) + public int Receive (byte [] buffer, int offset, int size, SocketFlags flags) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); - if (buf == null) - throw new ArgumentNullException ("buf"); + if (buffer == null) + throw new ArgumentNullException ("buffer"); - if (offset < 0 || offset > buf.Length) + if (offset < 0 || offset > buffer.Length) throw new ArgumentOutOfRangeException ("offset"); - if (size < 0 || offset + size > buf.Length) + if (size < 0 || offset + size > buffer.Length) throw new ArgumentOutOfRangeException ("size"); SocketError error; - int ret = Receive_nochecks (buf, offset, size, flags, out error); + int ret = Receive_nochecks (buffer, offset, size, flags, out error); if (error != SocketError.Success) { if (error == SocketError.WouldBlock && blocking) // This might happen when ReceiveTimeout is set @@ -2393,21 +2393,21 @@ namespace System.Net.Sockets } #if NET_2_0 - public int Receive (byte [] buf, int offset, int size, SocketFlags flags, out SocketError error) + public int Receive (byte [] buffer, int offset, int size, SocketFlags flags, out SocketError error) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); - if (buf == null) - throw new ArgumentNullException ("buf"); + if (buffer == null) + throw new ArgumentNullException ("buffer"); - if (offset < 0 || offset > buf.Length) + if (offset < 0 || offset > buffer.Length) throw new ArgumentOutOfRangeException ("offset"); - if (size < 0 || offset + size > buf.Length) + if (size < 0 || offset + size > buffer.Length) throw new ArgumentOutOfRangeException ("size"); - return Receive_nochecks (buf, offset, size, flags, out error); + return Receive_nochecks (buffer, offset, size, flags, out error); } [MethodImplAttribute (MethodImplOptions.InternalCall)] @@ -2449,18 +2449,17 @@ namespace System.Net.Sockets SocketFlags socketFlags, out SocketError errorCode) { + if (disposed && closed) + throw new ObjectDisposedException (GetType ().ToString ()); + if (buffers == null || buffers.Count == 0) { throw new ArgumentNullException ("buffers"); } - - if (disposed && closed) { - throw new ObjectDisposedException (GetType ().ToString ()); - } int numsegments = buffers.Count; int nativeError; - int ret; + int ret; /* Only example I can find of sending a byte * array reference directly into an internal @@ -2513,51 +2512,51 @@ namespace System.Net.Sockets return true; } #endif - - public int ReceiveFrom (byte [] buf, ref EndPoint remote_end) + + public int ReceiveFrom (byte [] buffer, ref EndPoint remoteEP) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); - if (buf == null) - throw new ArgumentNullException ("buf"); + if (buffer == null) + throw new ArgumentNullException ("buffer"); - if (remote_end == null) - throw new ArgumentNullException ("remote_end"); + if (remoteEP == null) + throw new ArgumentNullException ("remoteEP"); - return ReceiveFrom_nochecks (buf, 0, buf.Length, SocketFlags.None, ref remote_end); + return ReceiveFrom_nochecks (buffer, 0, buffer.Length, SocketFlags.None, ref remoteEP); } - public int ReceiveFrom (byte [] buf, SocketFlags flags, ref EndPoint remote_end) + public int ReceiveFrom (byte [] buffer, SocketFlags flags, ref EndPoint remoteEP) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); - if (buf == null) - throw new ArgumentNullException ("buf"); + if (buffer == null) + throw new ArgumentNullException ("buffer"); - if (remote_end == null) - throw new ArgumentNullException ("remote_end"); + if (remoteEP == null) + throw new ArgumentNullException ("remoteEP"); - return ReceiveFrom_nochecks (buf, 0, buf.Length, flags, ref remote_end); + return ReceiveFrom_nochecks (buffer, 0, buffer.Length, flags, ref remoteEP); } - public int ReceiveFrom (byte [] buf, int size, SocketFlags flags, - ref EndPoint remote_end) + public int ReceiveFrom (byte [] buffer, int size, SocketFlags flags, + ref EndPoint remoteEP) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); - if (buf == null) - throw new ArgumentNullException ("buf"); + if (buffer == null) + throw new ArgumentNullException ("buffer"); - if (remote_end == null) - throw new ArgumentNullException ("remote_end"); + if (remoteEP == null) + throw new ArgumentNullException ("remoteEP"); - if (size < 0 || size > buf.Length) + if (size < 0 || size > buffer.Length) throw new ArgumentOutOfRangeException ("size"); - return ReceiveFrom_nochecks (buf, 0, size, flags, ref remote_end); + return ReceiveFrom_nochecks (buffer, 0, size, flags, ref remoteEP); } [MethodImplAttribute(MethodImplOptions.InternalCall)] @@ -2569,25 +2568,25 @@ namespace System.Net.Sockets ref SocketAddress sockaddr, out int error); - public int ReceiveFrom (byte [] buf, int offset, int size, SocketFlags flags, - ref EndPoint remote_end) + public int ReceiveFrom (byte [] buffer, int offset, int size, SocketFlags flags, + ref EndPoint remoteEP) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); - if (buf == null) - throw new ArgumentNullException ("buf"); + if (buffer == null) + throw new ArgumentNullException ("buffer"); - if (remote_end == null) - throw new ArgumentNullException ("remote_end"); + if (remoteEP == null) + throw new ArgumentNullException ("remoteEP"); - if (offset < 0 || offset > buf.Length) + if (offset < 0 || offset > buffer.Length) throw new ArgumentOutOfRangeException ("offset"); - if (size < 0 || offset + size > buf.Length) + if (size < 0 || offset + size > buffer.Length) throw new ArgumentOutOfRangeException ("size"); - return ReceiveFrom_nochecks (buf, offset, size, flags, ref remote_end); + return ReceiveFrom_nochecks (buffer, offset, size, flags, ref remoteEP); } internal int ReceiveFrom_nochecks (byte [] buf, int offset, int size, SocketFlags flags, @@ -3028,66 +3027,66 @@ namespace System.Net.Sockets return ret; } - public void SetSocketOption (SocketOptionLevel level, SocketOptionName name, byte[] opt_value) + public void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, byte [] optionValue) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); // I'd throw an ArgumentNullException, but this is what MS does. - if (opt_value == null) + if (optionValue == null) throw new SocketException ((int) SocketError.Fault, "Error trying to dereference an invalid pointer"); int error; - - SetSocketOption_internal(socket, level, name, null, - opt_value, 0, out error); + + SetSocketOption_internal (socket, optionLevel, optionName, null, + optionValue, 0, out error); if (error != 0) { - if (error == 10022) // WSAEINVAL + if (error == (int) SocketError.InvalidArgument) throw new ArgumentException (); throw new SocketException (error); } } - public void SetSocketOption (SocketOptionLevel level, SocketOptionName name, object opt_value) + public void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, object optionValue) { - if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); - if (opt_value == null) + // NOTE: if a null is passed, the byte[] overload is used instead... + if (optionValue == null) throw new ArgumentNullException("optionValue"); int error; - if (level == SocketOptionLevel.Socket && name == SocketOptionName.Linger) { - LingerOption linger = opt_value as LingerOption; + if (optionLevel == SocketOptionLevel.Socket && optionName == SocketOptionName.Linger) { + LingerOption linger = optionValue as LingerOption; if (linger == null) #if NET_2_0 throw new ArgumentException ("A 'LingerOption' value must be specified.", "optionValue"); #else throw new ArgumentException ("optionValue"); #endif - SetSocketOption_internal (socket, level, name, linger, null, 0, out error); - } else if (level == SocketOptionLevel.IP && (name == SocketOptionName.AddMembership || name == SocketOptionName.DropMembership)) { - MulticastOption multicast = opt_value as MulticastOption; + SetSocketOption_internal (socket, optionLevel, optionName, linger, null, 0, out error); + } else if (optionLevel == SocketOptionLevel.IP && (optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership)) { + MulticastOption multicast = optionValue as MulticastOption; if (multicast == null) #if NET_2_0 throw new ArgumentException ("A 'MulticastOption' value must be specified.", "optionValue"); #else throw new ArgumentException ("optionValue"); #endif - SetSocketOption_internal (socket, level, name, multicast, null, 0, out error); - } else if (level == SocketOptionLevel.IPv6 && (name == SocketOptionName.AddMembership || name == SocketOptionName.DropMembership)) { - IPv6MulticastOption multicast = opt_value as IPv6MulticastOption; + SetSocketOption_internal (socket, optionLevel, optionName, multicast, null, 0, out error); + } else if (optionLevel == SocketOptionLevel.IPv6 && (optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership)) { + IPv6MulticastOption multicast = optionValue as IPv6MulticastOption; if (multicast == null) #if NET_2_0 throw new ArgumentException ("A 'IPv6MulticastOption' value must be specified.", "optionValue"); #else throw new ArgumentException ("optionValue"); #endif - SetSocketOption_internal (socket, level, name, multicast, null, 0, out error); + SetSocketOption_internal (socket, optionLevel, optionName, multicast, null, 0, out error); } else { #if NET_2_0 throw new ArgumentException ("Invalid value specified.", "optionValue"); @@ -3097,23 +3096,23 @@ namespace System.Net.Sockets } if (error != 0) { - if (error == 10022) // WSAEINVAL + if (error == (int) SocketError.InvalidArgument) throw new ArgumentException (); throw new SocketException (error); } } #if NET_2_0 - public void SetSocketOption (SocketOptionLevel level, SocketOptionName name, bool optionValue) + public void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, bool optionValue) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); int error; int int_val = (optionValue) ? 1 : 0; - SetSocketOption_internal (socket, level, name, null, null, int_val, out error); + SetSocketOption_internal (socket, optionLevel, optionName, null, null, int_val, out error); if (error != 0) { - if (error == 10022) // WSAEINVAL + if (error == (int) SocketError.InvalidArgument) throw new ArgumentException (); throw new SocketException (error); } diff --git a/mcs/class/System/System.Net.Sockets/Socket_2_1.cs b/mcs/class/System/System.Net.Sockets/Socket_2_1.cs index cf91f60c3a4..619a9d49b3b 100644 --- a/mcs/class/System/System.Net.Sockets/Socket_2_1.cs +++ b/mcs/class/System/System.Net.Sockets/Socket_2_1.cs @@ -426,38 +426,38 @@ namespace System.Net.Sockets { SocketAddress sa, out int error); - public void Connect (EndPoint remote_end) + public void Connect (EndPoint remoteEP) { SocketAddress serial = null; if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); - if (remote_end == null) - throw new ArgumentNullException("remote_end"); + if (remoteEP == null) + throw new ArgumentNullException ("remoteEP"); - if (remote_end is IPEndPoint) { - IPEndPoint ep = (IPEndPoint) remote_end; + IPEndPoint ep = remoteEP as IPEndPoint; + if (ep != null) if (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any)) throw new SocketException ((int) SocketError.AddressNotAvailable); - } + #if NET_2_1 if (protocol_type != ProtocolType.Tcp) throw new SocketException ((int) SocketError.AccessDenied); - DnsEndPoint dep = (remote_end as DnsEndPoint); + DnsEndPoint dep = (remoteEP as DnsEndPoint); if (dep != null) serial = dep.AsIPEndPoint ().Serialize (); else - serial = remote_end.Serialize (); + serial = remoteEP.Serialize (); #elif NET_2_0 /* TODO: check this for the 1.1 profile too */ if (islistening) throw new InvalidOperationException (); - serial = remote_end.Serialize (); + serial = remoteEP.Serialize (); #else - serial = remote_end.Serialize (); + serial = remoteEP.Serialize (); #endif int error = 0; @@ -482,8 +482,8 @@ namespace System.Net.Sockets { #if NET_2_0 isbound = true; #endif - - seed_endpoint = remote_end; + + seed_endpoint = remoteEP; } #if NET_2_0 @@ -615,23 +615,23 @@ namespace System.Net.Sockets { return ret; } - public object GetSocketOption (SocketOptionLevel level, SocketOptionName name) + public object GetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); object obj_val; int error; - - GetSocketOption_obj_internal(socket, level, name, out obj_val, + + GetSocketOption_obj_internal (socket, optionLevel, optionName, out obj_val, out error); if (error != 0) throw new SocketException (error); - - if (name == SocketOptionName.Linger) { + + if (optionName == SocketOptionName.Linger) { return((LingerOption)obj_val); - } else if (name==SocketOptionName.AddMembership || - name==SocketOptionName.DropMembership) { + } else if (optionName == SocketOptionName.AddMembership || + optionName == SocketOptionName.DropMembership) { return((MulticastOption)obj_val); } else if (obj_val is int) { return((int)obj_val); @@ -662,15 +662,15 @@ namespace System.Net.Sockets { byte [] byte_val, int int_val, out int error); - public void SetSocketOption (SocketOptionLevel level, SocketOptionName name, int opt_value) + public void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); int error; - - SetSocketOption_internal(socket, level, name, null, - null, opt_value, out error); + + SetSocketOption_internal (socket, optionLevel, optionName, null, + null, optionValue, out error); if (error != 0) throw new SocketException (error); diff --git a/mcs/class/System/Test/System.Net.Sockets/ChangeLog b/mcs/class/System/Test/System.Net.Sockets/ChangeLog index 77033d2342a..4a73df4b41f 100644 --- a/mcs/class/System/Test/System.Net.Sockets/ChangeLog +++ b/mcs/class/System/Test/System.Net.Sockets/ChangeLog @@ -1,5 +1,10 @@ 2009-06-28 Gert Driesen <drieseng@users.sourceforge.net> + * SocketTest.cs: Added tests for argument checks, and improved tests + for disposed socket. + +2009-06-28 Gert Driesen <drieseng@users.sourceforge.net> + * IPv6MulticastOptionTest.cs: Added .ctor and property tests. * MulticastOptionTest.cs: Added .ctor and property tests. * SocketTest.cs: Fixed compiler warnings. Improved tests for diff --git a/mcs/class/System/Test/System.Net.Sockets/SocketTest.cs b/mcs/class/System/Test/System.Net.Sockets/SocketTest.cs index e45115260cc..58d5c50282f 100644 --- a/mcs/class/System/Test/System.Net.Sockets/SocketTest.cs +++ b/mcs/class/System/Test/System.Net.Sockets/SocketTest.cs @@ -159,6 +159,22 @@ namespace MonoTests.System.Net.Sockets CFACalledBack.Set (); } + [Test] // Connect (IPEndPoint) + public void Connect1_RemoteEP_Null () + { + Socket s = new Socket (AddressFamily.InterNetwork, + SocketType.Stream, ProtocolType.Tcp); + try { + s.Connect ((IPEndPoint) null); + Assert.Fail ("#1"); + } catch (ArgumentNullException ex) { + Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual ("remoteEP", ex.ParamName, "#5"); + } + } + [Test] public void ConnectFailAsync () { @@ -266,17 +282,6 @@ namespace MonoTests.System.Net.Sockets [Test] [ExpectedException (typeof (ObjectDisposedException))] - public void Disposed1 () - { - Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - EndPoint ep = new IPEndPoint (IPAddress.Any, 31337); - s.Close(); - - s.ReceiveFrom (buf, ref ep); - } - - [Test] - [ExpectedException (typeof (ObjectDisposedException))] public void Disposed2 () { Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); @@ -307,79 +312,6 @@ namespace MonoTests.System.Net.Sockets [Test] [ExpectedException (typeof (ObjectDisposedException))] - public void Disposed8 () - { - Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - s.Close(); - - s.Receive (buf); - } - - [Test] - [ExpectedException (typeof (ObjectDisposedException))] - public void Disposed9 () - { - Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - s.Close(); - - s.Receive (buf, 0); - } - - [Test] - [ExpectedException (typeof (ObjectDisposedException))] - public void Disposed10 () - { - Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - s.Close(); - - s.Receive (buf, 10, 0); - } - - [Test] - [ExpectedException (typeof (ObjectDisposedException))] - public void Disposed11 () - { - Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - s.Close(); - - s.Receive (buf, 0, 10, 0); - } - - [Test] - [ExpectedException (typeof (ObjectDisposedException))] - public void Disposed12 () - { - Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - EndPoint ep = new IPEndPoint (IPAddress.Any, 31337); - s.Close(); - - s.ReceiveFrom (buf, 0, ref ep); - } - - [Test] - [ExpectedException (typeof (ObjectDisposedException))] - public void Disposed13 () - { - Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - EndPoint ep = new IPEndPoint (IPAddress.Any, 31337); - s.Close(); - - s.ReceiveFrom (buf, 10, 0, ref ep); - } - - [Test] - [ExpectedException (typeof (ObjectDisposedException))] - public void Disposed14 () - { - Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - EndPoint ep = new IPEndPoint (IPAddress.Any, 31337); - s.Close(); - - s.ReceiveFrom (buf, 0, 10, 0, ref ep); - } - - [Test] - [ExpectedException (typeof (ObjectDisposedException))] public void Disposed15 () { Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); @@ -2643,7 +2575,709 @@ namespace MonoTests.System.Net.Sockets RRCLastRead = true; RRCReady.Set (); } - + + [Test] // Receive (Byte []) + public void Receive1_Buffer_Null () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + + try { + s.Receive ((byte []) null); + Assert.Fail ("#1"); + } catch (ArgumentNullException ex) { + Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual ("buffer", ex.ParamName, "#5"); + } finally { + s.Close (); + } + } + + [Test] // Receive (Byte []) + public void Receive1_Socket_Closed () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + s.Close (); + + try { + s.Receive ((byte []) null); + Assert.Fail ("#1"); + } catch (ObjectDisposedException ex) { + // Cannot access a disposed object + Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5"); + } + } + + [Test] // Receive (Byte [], SocketFlags) + public void Receive2_Buffer_Null () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + + try { + s.Receive ((byte []) null, (SocketFlags) 666); + Assert.Fail ("#1"); + } catch (ArgumentNullException ex) { + Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual ("buffer", ex.ParamName, "#5"); + } finally { + s.Close (); + } + } + + [Test] // Receive (Byte [], SocketFlags) + public void Receive2_Socket_Closed () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + s.Close (); + + try { + s.Receive ((byte []) null, (SocketFlags) 666); + Assert.Fail ("#1"); + } catch (ObjectDisposedException ex) { + // Cannot access a disposed object + Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5"); + } + } + + [Test] // Receive (Byte [], Int32, SocketFlags) + public void Receive3_Buffer_Null () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + + try { + s.Receive ((byte []) null, 0, (SocketFlags) 666); + Assert.Fail ("#1"); + } catch (ArgumentNullException ex) { + Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual ("buffer", ex.ParamName, "#5"); + } finally { + s.Close (); + } + } + + [Test] // Receive (Byte [], Int32, SocketFlags) + public void Receive3_Socket_Closed () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + s.Close (); + + try { + s.Receive ((byte []) null, 0, (SocketFlags) 666); + Assert.Fail ("#1"); + } catch (ObjectDisposedException ex) { + // Cannot access a disposed object + Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5"); + } + } + + [Test] // Receive (Byte [], Int32, Int32, SocketFlags) + public void Receive4_Buffer_Null () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + + try { + s.Receive ((byte []) null, 0, 0, (SocketFlags) 666); + Assert.Fail ("#1"); + } catch (ArgumentNullException ex) { + Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual ("buffer", ex.ParamName, "#5"); + } finally { + s.Close (); + } + } + + [Test] // Receive (Byte [], Int32, Int32, SocketFlags) + public void Receive4_Socket_Closed () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + s.Close (); + + try { + s.Receive ((byte []) null, 0, 0, (SocketFlags) 666); + Assert.Fail ("#1"); + } catch (ObjectDisposedException ex) { + // Cannot access a disposed object + Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5"); + } + } + +#if NET_2_0 + [Test] // Receive (Byte [], Int32, Int32, SocketFlags, out SocketError) + public void Receive5_Buffer_Null () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + + SocketError error; + try { + s.Receive ((byte []) null, 0, 0, SocketFlags.None, out error); + Assert.Fail ("#1"); + } catch (ArgumentNullException ex) { + Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual ("buffer", ex.ParamName, "#5"); + } finally { + s.Close (); + } + } + + [Test] // Receive (Byte [], Int32, Int32, SocketFlags, out SocketError) + public void Receive5_Socket_Closed () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + s.Close (); + + SocketError error; + try { + s.Receive ((byte []) null, 0, 0, SocketFlags.None, out error); + Assert.Fail ("#1"); + } catch (ObjectDisposedException ex) { + // Cannot access a disposed object + Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5"); + } + } + + [Test] // Receive (IList<ArraySegment<Byte>>) + public void Receive6_Buffers_Null () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + + try { + s.Receive ((IList<ArraySegment<byte>>) null); + Assert.Fail ("#1"); + } catch (ArgumentNullException ex) { + Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual ("buffers", ex.ParamName, "#5"); + } finally { + s.Close (); + } + } + + [Test] // Receive (IList<ArraySegment<Byte>>) + public void Receive6_Socket_Closed () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + s.Close (); + + try { + s.Receive ((IList<ArraySegment<byte>>) null); + Assert.Fail ("#1"); + } catch (ObjectDisposedException ex) { + // Cannot access a disposed object + Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5"); + } + } + + [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags) + public void Receive7_Buffers_Null () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + + try { + s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666); + Assert.Fail ("#1"); + } catch (ArgumentNullException ex) { + Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual ("buffers", ex.ParamName, "#5"); + } finally { + s.Close (); + } + } + + [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags) + public void Receive7_Socket_Closed () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + s.Close (); + + try { + s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666); + Assert.Fail ("#1"); + } catch (ObjectDisposedException ex) { + // Cannot access a disposed object + Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5"); + } + } + + [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags, out SocketError) + public void Receive8_Buffers_Null () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + + SocketError error; + try { + s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666, + out error); + Assert.Fail ("#1"); + } catch (ArgumentNullException ex) { + Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual ("buffers", ex.ParamName, "#5"); + } finally { + s.Close (); + } + } + + [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags, out SocketError) + public void Receive8_Socket_Closed () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + s.Close (); + + SocketError error; + try { + s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666, + out error); + Assert.Fail ("#1"); + } catch (ObjectDisposedException ex) { + // Cannot access a disposed object + Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5"); + } finally { + s.Close (); + } + } +#endif + + [Test] // ReceiveFrom (Byte [], ref EndPoint) + public void ReceiveFrom1_Buffer_Null () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + + EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001); + try { + s.ReceiveFrom ((Byte []) null, ref remoteEP); + Assert.Fail ("#1"); + } catch (ArgumentNullException ex) { + Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual ("buffer", ex.ParamName, "#5"); + } finally { + s.Close (); + } + } + + [Test] // ReceiveFrom (Byte [], ref EndPoint) + public void ReceiveFrom1_RemoteEP_Null () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + + byte [] buffer = new byte [0]; + EndPoint remoteEP = null; + try { + s.ReceiveFrom (buffer, ref remoteEP); + Assert.Fail ("#1"); + } catch (ArgumentNullException ex) { + Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual ("remoteEP", ex.ParamName, "#5"); + } finally { + s.Close (); + } + } + + [Test] // ReceiveFrom (Byte [], ref EndPoint) + public void ReceiveFrom1_Socket_Closed () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + s.Close (); + + EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001); + try { + s.ReceiveFrom ((Byte []) null, ref remoteEP); + Assert.Fail ("#1"); + } catch (ObjectDisposedException ex) { + // Cannot access a disposed object + Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5"); + } + } + + [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint) + public void ReceiveFrom2_Buffer_Null () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + + EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001); + try { + s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP); + Assert.Fail ("#1"); + } catch (ArgumentNullException ex) { + Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual ("buffer", ex.ParamName, "#5"); + } finally { + s.Close (); + } + } + + [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint) + public void ReceiveFrom2_RemoteEP_Null () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + + byte [] buffer = new byte [5]; + EndPoint remoteEP = null; + try { + s.ReceiveFrom (buffer, (SocketFlags) 666, ref remoteEP); + Assert.Fail ("#1"); + } catch (ArgumentNullException ex) { + Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual ("remoteEP", ex.ParamName, "#5"); + } finally { + s.Close (); + } + } + + [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint) + public void ReceiveFrom2_Socket_Closed () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + s.Close (); + + EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001); + try { + s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP); + Assert.Fail ("#1"); + } catch (ObjectDisposedException ex) { + // Cannot access a disposed object + Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5"); + } + } + + [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint) + public void ReceiveFrom3_Buffer_Null () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + + EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001); + try { + s.ReceiveFrom ((Byte []) null, -1, (SocketFlags) 666, + ref remoteEP); + Assert.Fail ("#1"); + } catch (ArgumentNullException ex) { + Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual ("buffer", ex.ParamName, "#5"); + } finally { + s.Close (); + } + } + + [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint) + public void ReceiveFrom3_RemoteEP_Null () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + + byte [] buffer = new byte [5]; + EndPoint remoteEP = null; + try { + s.ReceiveFrom (buffer, -1, (SocketFlags) 666, ref remoteEP); + Assert.Fail ("#1"); + } catch (ArgumentNullException ex) { + Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual ("remoteEP", ex.ParamName, "#5"); + } finally { + s.Close (); + } + } + + [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint) + public void ReceiveFrom3_Size_OutOfRange () + { + Socket s; + byte [] buffer = new byte [5]; + EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001); + + // size negative + s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + try { + s.ReceiveFrom (buffer, -1, (SocketFlags) 666, ref remoteEP); + Assert.Fail ("#A1"); + } catch (ArgumentOutOfRangeException ex) { + // Specified argument was out of the range of valid values + Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2"); + Assert.IsNull (ex.InnerException, "#A3"); + Assert.IsNotNull (ex.Message, "#A4"); + Assert.AreEqual ("size", ex.ParamName, "#A5"); + } finally { + s.Close (); + } + + // size > buffer length + s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + try { + s.ReceiveFrom (buffer, (buffer.Length + 1), (SocketFlags) 666, + ref remoteEP); + Assert.Fail ("#B1"); + } catch (ArgumentOutOfRangeException ex) { + // Specified argument was out of the range of valid values + Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2"); + Assert.IsNull (ex.InnerException, "#B3"); + Assert.IsNotNull (ex.Message, "#B4"); + Assert.AreEqual ("size", ex.ParamName, "#B5"); + } finally { + s.Close (); + } + } + + [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint) + public void ReceiveFrom3_Socket_Closed () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + s.Close (); + + EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001); + try { + s.ReceiveFrom ((Byte []) null, -1, (SocketFlags) 666, + ref remoteEP); + Assert.Fail ("#1"); + } catch (ObjectDisposedException ex) { + // Cannot access a disposed object + Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5"); + } + } + + [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint) + public void ReceiveFrom4_Buffer_Null () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001); + + try { + s.ReceiveFrom ((Byte []) null, -1, -1, (SocketFlags) 666, + ref remoteEP); + Assert.Fail ("#1"); + } catch (ArgumentNullException ex) { + Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual ("buffer", ex.ParamName, "#5"); + } + } + + [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint) + public void ReceiveFrom4_Offset_OutOfRange () + { + Socket s; + byte [] buffer = new byte [5]; + EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001); + + // offset negative + s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + try { + s.ReceiveFrom (buffer, -1, 0, (SocketFlags) 666, + ref remoteEP); + Assert.Fail ("#A1"); + } catch (ArgumentOutOfRangeException ex) { + // Specified argument was out of the range of valid values + Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2"); + Assert.IsNull (ex.InnerException, "#A3"); + Assert.IsNotNull (ex.Message, "#A4"); + Assert.AreEqual ("offset", ex.ParamName, "#A5"); + } finally { + s.Close (); + } + + // offset > buffer length + s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + try { + s.ReceiveFrom (buffer, (buffer.Length + 1), 0, (SocketFlags) 666, + ref remoteEP); + Assert.Fail ("#B1"); + } catch (ArgumentOutOfRangeException ex) { + // Specified argument was out of the range of valid values + Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2"); + Assert.IsNull (ex.InnerException, "#B3"); + Assert.IsNotNull (ex.Message, "#B4"); + Assert.AreEqual ("offset", ex.ParamName, "#B5"); + } finally { + s.Close (); + } + } + + [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, ref IPEndPoint) + public void ReceiveFrom4_RemoteEP_Null () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + byte [] buffer = new byte [5]; + EndPoint remoteEP = null; + + try { + s.ReceiveFrom (buffer, -1, -1, (SocketFlags) 666, ref remoteEP); + Assert.Fail ("#1"); + } catch (ArgumentNullException ex) { + Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual ("remoteEP", ex.ParamName, "#5"); + } finally { + s.Close (); + } + } + + [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint) + public void ReceiveFrom4_Size_OutOfRange () + { + Socket s; + byte [] buffer = new byte [5]; + EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001); + + // size negative + s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + try { + s.ReceiveFrom (buffer, 0, -1, (SocketFlags) 666, + ref remoteEP); + Assert.Fail ("#A1"); + } catch (ArgumentOutOfRangeException ex) { + // Specified argument was out of the range of valid values + Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2"); + Assert.IsNull (ex.InnerException, "#A3"); + Assert.IsNotNull (ex.Message, "#A4"); + Assert.AreEqual ("size", ex.ParamName, "#A5"); + } finally { + s.Close (); + } + + // size > buffer length + s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + try { + s.ReceiveFrom (buffer, 0, (buffer.Length + 1), (SocketFlags) 666, + ref remoteEP); + Assert.Fail ("#B1"); + } catch (ArgumentOutOfRangeException ex) { + // Specified argument was out of the range of valid values + Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2"); + Assert.IsNull (ex.InnerException, "#B3"); + Assert.IsNotNull (ex.Message, "#B4"); + Assert.AreEqual ("size", ex.ParamName, "#B5"); + } finally { + s.Close (); + } + + // offset + size > buffer length + s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + try { + s.ReceiveFrom (buffer, 2, 4, (SocketFlags) 666, ref remoteEP); + Assert.Fail ("#C1"); + } catch (ArgumentOutOfRangeException ex) { + // Specified argument was out of the range of valid values + Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2"); + Assert.IsNull (ex.InnerException, "#C3"); + Assert.IsNotNull (ex.Message, "#C4"); + Assert.AreEqual ("size", ex.ParamName, "#C5"); + } finally { + s.Close (); + } + } + + [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, ref EndPoint) + public void ReceiveFrom4_Socket_Closed () + { + Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, + ProtocolType.Tcp); + s.Close (); + + byte [] buffer = new byte [5]; + EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001); + try { + s.ReceiveFrom (buffer, -1, -1, (SocketFlags) 666, + ref remoteEP); + Assert.Fail ("#1"); + } catch (ObjectDisposedException ex) { + // Cannot access a disposed object + Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5"); + } + } + [Test] public void ReceiveRemoteClosed () { |