1 // System.Net.Sockets.SocketTest.cs
4 // Brad Fitzpatrick (brad@danga.com)
5 // Gonzalo Paniagua Javier (gonzalo@novell.com)
7 // (C) Copyright 2003 Brad Fitzpatrick
8 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
12 using System.Diagnostics;
14 using System.Collections;
15 using System.Threading;
16 using System.Reflection;
17 using System.Text.RegularExpressions;
18 using System.Threading.Tasks;
20 using System.Net.Sockets;
21 using NUnit.Framework;
24 using System.Collections.Generic;
26 using MonoTests.Helpers;
28 namespace MonoTests.System.Net.Sockets
31 public class SocketTest
33 // note: also used in SocketCas tests
34 public const string BogusAddress = "192.168.244.244";
35 public const int BogusPort = 23483;
38 #if FEATURE_NO_BSD_SOCKETS
39 [ExpectedException (typeof (PlatformNotSupportedException))]
41 public void ConnectIPAddressAny ()
43 IPEndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
45 /* UDP sockets use Any to disconnect
47 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
52 } catch (SocketException ex) {
53 Assert.AreEqual (10049, ex.ErrorCode, "#2");
58 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
63 } catch (SocketException ex) {
64 Assert.AreEqual (10049, ex.ErrorCode, "#4");
69 [Ignore ("Bug #75158")] // Looks like MS fails after the .ctor, when you try to use the socket
70 public void IncompatibleAddress ()
72 IPEndPoint epIPv6 = new IPEndPoint (IPAddress.IPv6Any,
73 NetworkHelpers.FindFreePort ());
76 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)) {
81 } catch (SocketException ex) {
82 // address incompatible with protocol
83 int expectedError = 10047;
84 Assert.AreEqual (expectedError, ex.ErrorCode,
90 [Category ("InetAccess")]
91 #if FEATURE_NO_BSD_SOCKETS
92 [ExpectedException (typeof (PlatformNotSupportedException))]
94 public void BogusEndConnect ()
96 IPAddress ipOne = IPAddress.Parse (BogusAddress);
97 IPEndPoint ipEP = new IPEndPoint (ipOne, BogusPort);
98 Socket sock = new Socket (ipEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
99 IAsyncResult ar = sock.BeginConnect (ipEP, null, null);
102 // should raise an exception because connect was bogus
103 sock.EndConnect (ar);
105 } catch (SocketException ex) {
106 // Actual error code depends on network configuration.
107 var error = (SocketError) ex.ErrorCode;
108 Assert.That (error == SocketError.TimedOut ||
109 error == SocketError.ConnectionRefused ||
110 error == SocketError.NetworkUnreachable ||
111 error == SocketError.HostUnreachable, "#2");
116 [ExpectedException (typeof (ArgumentNullException))]
117 public void SelectEmpty ()
119 ArrayList list = new ArrayList ();
120 Socket.Select (list, list, list, 1000);
123 private bool BlockingConnect (bool block, int port)
125 IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, port);
126 Socket server = new Socket(AddressFamily.InterNetwork,
130 server.Blocking=block;
134 Socket conn = new Socket (AddressFamily.InterNetwork,
139 Socket client = null;
140 var sw = Stopwatch.StartNew ();
141 while (sw.ElapsedMilliseconds < 100)
144 client = server.Accept();
147 catch (SocketException ex) {
148 if (ex.SocketErrorCode == SocketError.WouldBlock)
153 Assert.IsNotNull (client, "Couldn't accept a client connection within 100ms.");
154 bool client_block = client.Blocking;
160 return(client_block);
164 #if FEATURE_NO_BSD_SOCKETS
165 [ExpectedException (typeof (PlatformNotSupportedException))]
167 public void AcceptBlockingStatus()
170 var port = NetworkHelpers.FindFreePort ();
172 block = BlockingConnect(true, port);
173 Assert.AreEqual (block, true, "BlockingStatus01");
175 block = BlockingConnect(false, port);
176 Assert.AreEqual (block, false, "BlockingStatus02");
179 static bool CFAConnected = false;
180 static ManualResetEvent CFACalledBack;
182 private static void CFACallback (IAsyncResult asyncResult)
184 Socket sock = (Socket)asyncResult.AsyncState;
185 CFAConnected = sock.Connected;
187 if (sock.Connected) {
188 sock.EndConnect (asyncResult);
191 CFACalledBack.Set ();
194 [Test] // Connect (IPEndPoint)
195 public void Connect1_RemoteEP_Null ()
197 Socket s = new Socket (AddressFamily.InterNetwork,
198 SocketType.Stream, ProtocolType.Tcp);
200 s.Connect ((IPEndPoint) null);
202 } catch (ArgumentNullException ex) {
203 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
204 Assert.IsNull (ex.InnerException, "#3");
205 Assert.IsNotNull (ex.Message, "#4");
206 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
211 #if FEATURE_NO_BSD_SOCKETS
212 [ExpectedException (typeof (PlatformNotSupportedException))]
214 public void ConnectFailAsync ()
216 Socket sock = new Socket (AddressFamily.InterNetwork,
219 sock.Blocking = false;
220 CFACalledBack = new ManualResetEvent (false);
221 CFACalledBack.Reset ();
223 /* Need a port that is not being used for
226 sock.BeginConnect (new IPEndPoint (IPAddress.Loopback,
227 NetworkHelpers.FindFreePort ()),
228 new AsyncCallback (CFACallback),
230 CFACalledBack.WaitOne ();
232 Assert.AreEqual (CFAConnected, false, "ConnectFail");
236 #if FEATURE_NO_BSD_SOCKETS
237 [ExpectedException (typeof (PlatformNotSupportedException))]
239 public void SetSocketOptionBoolean ()
241 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
242 Socket sock = new Socket (ep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
244 sock.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
250 #if FEATURE_NO_BSD_SOCKETS
251 [ExpectedException (typeof (PlatformNotSupportedException))]
253 public void TestSelect1 ()
255 Socket srv = CreateServer (NetworkHelpers.FindFreePort ());
256 ClientSocket clnt = new ClientSocket (srv.LocalEndPoint);
257 Thread th = new Thread (new ThreadStart (clnt.ConnectSleepClose));
263 ArrayList list = new ArrayList ();
264 ArrayList empty = new ArrayList ();
266 Socket.Select (list, empty, empty, 100);
267 Assert.AreEqual (0, empty.Count, "#01");
268 Assert.AreEqual (1, list.Count, "#02");
269 Socket.Select (empty, list, empty, 100);
270 Assert.AreEqual (0, empty.Count, "#03");
271 Assert.AreEqual (1, list.Count, "#04");
272 Socket.Select (list, empty, empty, -1);
273 Assert.AreEqual (0, empty.Count, "#05");
274 Assert.AreEqual (1, list.Count, "#06");
275 // Need to read the 10 bytes from the client to avoid a RST
276 byte [] bytes = new byte [10];
285 static Socket CreateServer (int port)
287 Socket sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
288 sock.Bind (new IPEndPoint (IPAddress.Loopback, port));
297 public ClientSocket (EndPoint ep)
300 sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
303 public void ConnectSleepClose ()
312 byte [] b = new byte [10];
317 byte[] buf = new byte[100];
320 [ExpectedException (typeof (ObjectDisposedException))]
321 public void Disposed2 ()
323 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
330 [ExpectedException (typeof (ObjectDisposedException))]
331 public void Disposed6 ()
333 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
340 [ExpectedException (typeof (ObjectDisposedException))]
341 public void Disposed7 ()
343 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
350 [ExpectedException (typeof (ObjectDisposedException))]
351 public void Disposed15 ()
353 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
360 [ExpectedException (typeof (ObjectDisposedException))]
361 public void Disposed16 ()
363 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
370 [ExpectedException (typeof (ObjectDisposedException))]
371 public void Disposed17 ()
373 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
380 [ExpectedException (typeof (ObjectDisposedException))]
381 public void Disposed18 ()
383 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
386 s.Send (buf, 0, 10, 0);
390 #if FEATURE_NO_BSD_SOCKETS
391 [ExpectedException (typeof (PlatformNotSupportedException))]
393 [ExpectedException (typeof (ObjectDisposedException))]
395 public void Disposed19 ()
397 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
398 EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
401 s.SendTo (buf, 0, ep);
405 #if FEATURE_NO_BSD_SOCKETS
406 [ExpectedException (typeof (PlatformNotSupportedException))]
408 [ExpectedException (typeof (ObjectDisposedException))]
410 public void Disposed20 ()
412 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
413 EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
416 s.SendTo (buf, 10, 0, ep);
420 #if FEATURE_NO_BSD_SOCKETS
421 [ExpectedException (typeof (PlatformNotSupportedException))]
423 [ExpectedException (typeof (ObjectDisposedException))]
425 public void Disposed21 ()
427 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
428 EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
431 s.SendTo (buf, 0, 10, 0, ep);
435 #if FEATURE_NO_BSD_SOCKETS
436 [ExpectedException (typeof (PlatformNotSupportedException))]
438 [ExpectedException (typeof (ObjectDisposedException))]
440 public void Disposed22 ()
442 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
443 EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
450 [ExpectedException (typeof (ObjectDisposedException))]
451 public void Disposed23 ()
453 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
460 #if FEATURE_NO_BSD_SOCKETS
461 [ExpectedException (typeof (PlatformNotSupportedException))]
463 public void GetHashCodeTest ()
465 Socket server = new Socket (AddressFamily.InterNetwork,
466 SocketType.Stream, ProtocolType.Tcp);
467 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
468 NetworkHelpers.FindFreePort ());
472 Socket client = new Socket (AddressFamily.InterNetwork,
473 SocketType.Stream, ProtocolType.Tcp);
474 int hashcodeA = client.GetHashCode ();
476 int hashcodeB = client.GetHashCode ();
477 Assert.AreEqual (hashcodeA, hashcodeB, "#1");
479 int hashcodeC = client.GetHashCode ();
480 Assert.AreEqual (hashcodeB, hashcodeC, "#2");
484 static ManualResetEvent SocketError_event = new ManualResetEvent (false);
486 private static void SocketError_callback (IAsyncResult ar)
488 Socket sock = (Socket)ar.AsyncState;
491 sock.EndConnect (ar);
494 SocketError_event.Set ();
498 [Category ("RequiresBSDSockets")] // This verifies particular error codes, which we don't care about when nothing's working anyway.
499 public void SocketErrorTest ()
501 Socket sock = new Socket (AddressFamily.InterNetwork,
504 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
507 SocketError_event.Reset ();
509 sock.Blocking = false;
510 sock.BeginConnect (ep, new AsyncCallback(SocketError_callback),
513 if (SocketError_event.WaitOne (2000, false) == false) {
514 Assert.Fail ("SocketError wait timed out");
517 Assert.AreEqual (false, sock.Connected, "SocketError #1");
521 error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
522 Assert.AreEqual (10061, error, "SocketError #2");
524 error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
525 Assert.AreEqual (10061, error, "SocketError #3");
532 public void SocketInformationCtor ()
537 public void DontFragmentDefaultTcp ()
539 Socket sock = new Socket (AddressFamily.InterNetwork,
543 Assert.AreEqual (false, sock.DontFragment, "DontFragmentDefaultTcp");
549 [Category ("NotWorking")] // DontFragment doesn't work
550 public void DontFragmentChangeTcp ()
552 Socket sock = new Socket (AddressFamily.InterNetwork,
556 sock.DontFragment = true;
558 Assert.AreEqual (true, sock.DontFragment, "DontFragmentChangeTcp");
564 public void DontFragmentDefaultUdp ()
566 Socket sock = new Socket (AddressFamily.InterNetwork,
570 Assert.AreEqual (false, sock.DontFragment, "DontFragmentDefaultUdp");
576 [Category ("NotWorking")] // DontFragment doesn't work
577 public void DontFragmentChangeUdp ()
579 Socket sock = new Socket (AddressFamily.InterNetwork,
583 sock.DontFragment = true;
585 Assert.AreEqual (true, sock.DontFragment, "DontFragmentChangeUdp");
591 [ExpectedException (typeof(ObjectDisposedException))]
592 public void DontFragmentClosed ()
594 Socket sock = new Socket (AddressFamily.InterNetwork,
600 bool val = sock.DontFragment;
604 [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
605 public void DontFragment ()
607 Socket sock = new Socket (AddressFamily.NetBios,
608 SocketType.Seqpacket,
609 ProtocolType.Unspecified);
612 sock.DontFragment = true;
613 Assert.Fail ("DontFragment #1");
614 } catch (NotSupportedException) {
621 public void EnableBroadcastDefaultTcp ()
623 Socket sock = new Socket (AddressFamily.InterNetwork,
628 bool value = sock.EnableBroadcast;
629 Assert.Fail ("EnableBroadcastDefaultTcp #1");
630 } catch (SocketException ex) {
631 Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastDefaultTcp #2");
638 public void EnableBroadcastDefaultUdp ()
640 Socket sock = new Socket (AddressFamily.InterNetwork,
644 Assert.AreEqual (false, sock.EnableBroadcast, "EnableBroadcastDefaultUdp");
650 public void EnableBroadcastChangeTcp ()
652 Socket sock = new Socket (AddressFamily.InterNetwork,
657 sock.EnableBroadcast = true;
658 Assert.Fail ("EnableBroadcastChangeTcp #1");
659 } catch (SocketException ex) {
660 Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastChangeTcp #2");
667 public void EnableBroadcastChangeUdp ()
669 Socket sock = new Socket (AddressFamily.InterNetwork,
673 sock.EnableBroadcast = true;
675 Assert.AreEqual (true, sock.EnableBroadcast, "EnableBroadcastChangeUdp");
681 [ExpectedException (typeof(ObjectDisposedException))]
682 public void EnableBroadcastClosed ()
684 Socket sock = new Socket (AddressFamily.InterNetwork,
690 bool val = sock.EnableBroadcast;
693 /* Can't test the default for ExclusiveAddressUse as
694 * it's different on different versions and service
698 [Category ("NotWorking")] // Not supported on Linux
699 public void ExclusiveAddressUseUnbound ()
701 Socket sock = new Socket (AddressFamily.InterNetwork,
705 sock.ExclusiveAddressUse = true;
707 Assert.AreEqual (true, sock.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
713 [ExpectedException (typeof(InvalidOperationException))]
714 [Category ("NotWorking")] // Not supported on Linux
715 public void ExclusiveAddressUseBound ()
717 Socket sock = new Socket (AddressFamily.InterNetwork,
721 sock.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
722 sock.ExclusiveAddressUse = true;
727 [ExpectedException (typeof(ObjectDisposedException))]
728 public void ExclusiveAddressUseClosed ()
730 Socket sock = new Socket (AddressFamily.InterNetwork,
736 bool val = sock.ExclusiveAddressUse;
740 #if FEATURE_NO_BSD_SOCKETS
741 [ExpectedException (typeof (PlatformNotSupportedException))]
743 public void IsBoundTcp ()
745 Socket sock = new Socket (AddressFamily.InterNetwork,
748 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
751 Assert.AreEqual (false, sock.IsBound, "IsBoundTcp #1");
754 Assert.AreEqual (true, sock.IsBound, "IsBoundTcp #2");
758 Socket sock2 = new Socket (AddressFamily.InterNetwork,
762 Assert.AreEqual (false, sock2.IsBound, "IsBoundTcp #3");
765 Assert.AreEqual (true, sock2.IsBound, "IsBoundTcp #4");
768 Assert.AreEqual (true, sock2.IsBound, "IsBoundTcp #5");
771 Assert.AreEqual (true, sock.IsBound, "IsBoundTcp #6");
775 #if FEATURE_NO_BSD_SOCKETS
776 [ExpectedException (typeof (PlatformNotSupportedException))]
778 public void IsBoundUdp ()
780 Socket sock = new Socket (AddressFamily.InterNetwork,
783 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
786 Assert.AreEqual (false, sock.IsBound, "IsBoundUdp #1");
789 Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #2");
792 Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #3");
795 sock = new Socket (AddressFamily.InterNetwork,
799 Assert.AreEqual (false, sock.IsBound, "IsBoundUdp #4");
802 Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #5");
805 Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #6");
809 /* Should not throw an exception */
810 public void IsBoundClosed ()
812 Socket sock = new Socket (AddressFamily.InterNetwork,
818 bool val = sock.IsBound;
821 /* Nothing much to test for LingerState */
824 public void MulticastLoopbackDefaultTcp ()
826 Socket sock = new Socket (AddressFamily.InterNetwork,
831 bool value = sock.MulticastLoopback;
832 Assert.Fail ("MulticastLoopbackDefaultTcp #1");
833 } catch (SocketException ex) {
834 Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackDefaultTcp #2");
841 public void MulticastLoopbackChangeTcp ()
843 Socket sock = new Socket (AddressFamily.InterNetwork,
848 sock.MulticastLoopback = false;
849 Assert.Fail ("MulticastLoopbackChangeTcp #1");
850 } catch (SocketException ex) {
851 Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackChangeTcp #2");
858 public void MulticastLoopbackDefaultUdp ()
860 Socket sock = new Socket (AddressFamily.InterNetwork,
864 Assert.AreEqual (true, sock.MulticastLoopback, "MulticastLoopbackDefaultUdp");
870 public void MulticastLoopbackChangeUdp ()
872 Socket sock = new Socket (AddressFamily.InterNetwork,
876 sock.MulticastLoopback = false;
878 Assert.AreEqual (false, sock.MulticastLoopback, "MulticastLoopbackChangeUdp");
884 [ExpectedException (typeof(ObjectDisposedException))]
885 public void MulticastLoopbackClosed ()
887 Socket sock = new Socket (AddressFamily.InterNetwork,
893 bool val = sock.MulticastLoopback;
896 /* OSSupportsIPv6 depends on the environment */
899 [Category("NotWorking")] // We have different defaults for perf reasons
900 public void ReceiveBufferSizeDefault ()
902 Socket sock = new Socket (AddressFamily.InterNetwork,
906 Assert.AreEqual (8192, sock.ReceiveBufferSize, "ReceiveBufferSizeDefault");
912 [Category("NotWorking")] // We have different defaults for perf reasons
913 public void ReceiveBufferSizeDefaultUdp ()
915 Socket sock = new Socket (AddressFamily.InterNetwork,
919 Assert.AreEqual (8192, sock.ReceiveBufferSize, "ReceiveBufferSizeDefaultUdp");
925 public void ReceiveBufferSizeChange ()
927 Socket sock = new Socket (AddressFamily.InterNetwork,
931 sock.ReceiveBufferSize = 16384;
933 Assert.AreEqual (16384, sock.ReceiveBufferSize, "ReceiveBufferSizeChange");
939 [Category("NotWorking")] // We cannot totally remove buffers (minimum is set to 256
940 public void BuffersCheck_None ()
942 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
943 int original = s.ReceiveBufferSize;
944 s.ReceiveBufferSize = 0;
945 Assert.AreEqual (0, s.ReceiveBufferSize, "ReceiveBufferSize " + original.ToString ());
947 original = s.SendBufferSize;
948 s.SendBufferSize = 0;
949 Assert.AreEqual (0, s.SendBufferSize, "SendBufferSize " + original.ToString ());
954 [ExpectedException (typeof(ObjectDisposedException))]
955 public void ReceiveBufferSizeClosed ()
957 Socket sock = new Socket (AddressFamily.InterNetwork,
963 int val = sock.ReceiveBufferSize;
967 [Category("NotWorking")] // We have different defaults for perf reasons
968 public void SendBufferSizeDefault ()
970 Socket sock = new Socket (AddressFamily.InterNetwork,
974 Assert.AreEqual (8192, sock.SendBufferSize, "SendBufferSizeDefault");
980 [Category("NotWorking")] // We have different defaults for perf reasons
981 public void SendBufferSizeDefaultUdp ()
983 Socket sock = new Socket (AddressFamily.InterNetwork,
987 Assert.AreEqual (8192, sock.SendBufferSize, "SendBufferSizeDefaultUdp");
993 public void SendBufferSizeChange ()
995 Socket sock = new Socket (AddressFamily.InterNetwork,
999 sock.SendBufferSize = 16384;
1001 Assert.AreEqual (16384, sock.SendBufferSize, "SendBufferSizeChange");
1007 [ExpectedException (typeof(ObjectDisposedException))]
1008 public void SendBufferSizeClosed ()
1010 Socket sock = new Socket (AddressFamily.InterNetwork,
1016 int val = sock.SendBufferSize;
1019 /* No test for TTL default as it's platform dependent */
1021 public void TtlChange ()
1023 Socket sock = new Socket (AddressFamily.InterNetwork,
1029 Assert.AreEqual (255, sock.Ttl, "TtlChange");
1035 public void TtlChangeOverflow ()
1037 Socket sock = new Socket (AddressFamily.InterNetwork,
1043 Assert.Fail ("TtlChangeOverflow #1");
1044 } catch (ArgumentOutOfRangeException ex) {
1045 Assert.AreEqual ("value", ex.ParamName,
1046 "TtlChangeOverflow #2");
1052 /* Apparently you can set TTL=0 on the ms runtime!!
1055 Assert.Fail ("TtlChangeOverflow #4");
1056 } catch (SocketException ex) {
1057 Assert.AreEqual (10022, ex.ErrorCode,
1058 "TtlChangeOverflow #5");
1065 [ExpectedException (typeof(ObjectDisposedException))]
1066 public void TtlClosed ()
1068 Socket sock = new Socket (AddressFamily.InterNetwork,
1078 public void UseOnlyOverlappedIODefault ()
1080 Socket sock = new Socket (AddressFamily.InterNetwork,
1084 Assert.AreEqual (false, sock.UseOnlyOverlappedIO, "UseOnlyOverlappedIODefault");
1090 // We need this because the Linux kernel in certain configurations
1091 // will end up rounding up the values passed on to the kernel
1092 // for socket send/receive timeouts.
1094 int Approximate (int target, int value)
1098 if (value > target-10 && value < target+10)
1104 public void UseOnlyOverlappedIOChange ()
1106 Socket sock = new Socket (AddressFamily.InterNetwork,
1110 sock.UseOnlyOverlappedIO = true;
1112 Assert.AreEqual (true, sock.UseOnlyOverlappedIO, "UseOnlyOverlappedIOChange");
1118 /* Should not throw an exception */
1119 public void UseOnlyOverlappedIOClosed ()
1121 Socket sock = new Socket (AddressFamily.InterNetwork,
1127 bool val = sock.UseOnlyOverlappedIO;
1131 public void SendTimeoutDefault ()
1133 Socket sock = new Socket (AddressFamily.InterNetwork,
1137 Assert.AreEqual (0, sock.SendTimeout, "SendTimeoutDefault");
1143 public void SendTimeoutChange ()
1145 Socket sock = new Socket (AddressFamily.InterNetwork,
1149 /* Should be rounded up to 500, according to
1150 * the MSDN docs, but the MS runtime doesn't
1152 sock.SendTimeout = 50;
1153 Assert.AreEqual (50, Approximate (50, sock.SendTimeout), "SendTimeoutChange #1");
1155 sock.SendTimeout = 2000;
1156 Assert.AreEqual (2000, Approximate (2000, sock.SendTimeout), "SendTimeoutChange #2");
1158 sock.SendTimeout = 0;
1159 Assert.AreEqual (0, Approximate (0, sock.SendTimeout), "SendTimeoutChange #3");
1161 /* Should be the same as setting 0 */
1162 sock.SendTimeout = -1;
1163 Assert.AreEqual (0, sock.SendTimeout, "SendTimeoutChange #4");
1165 sock.SendTimeout = 65536;
1166 Assert.AreEqual (65536, Approximate (65536, sock.SendTimeout), "SendTimeoutChange #5");
1169 sock.SendTimeout = -2;
1170 Assert.Fail ("SendTimeoutChange #8");
1171 } catch (ArgumentOutOfRangeException) {
1178 [ExpectedException (typeof(ObjectDisposedException))]
1179 public void SendTimeoutClosed ()
1181 Socket sock = new Socket (AddressFamily.InterNetwork,
1187 int val = sock.SendTimeout;
1191 public void ReceiveTimeoutDefault ()
1193 Socket sock = new Socket (AddressFamily.InterNetwork,
1197 Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutDefault");
1203 public void ReceiveTimeoutChange ()
1205 Socket sock = new Socket (AddressFamily.InterNetwork,
1209 sock.ReceiveTimeout = 50;
1210 Assert.AreEqual (50, Approximate (50, sock.ReceiveTimeout), "ReceiveTimeoutChange #1");
1212 sock.ReceiveTimeout = 2000;
1213 Assert.AreEqual (2000, Approximate (2000, sock.ReceiveTimeout), "ReceiveTimeoutChange #2");
1215 sock.ReceiveTimeout = 0;
1216 Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutChange #3");
1218 /* Should be the same as setting 0 */
1219 sock.ReceiveTimeout = -1;
1220 Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutChange #4");
1222 sock.ReceiveTimeout = 65536;
1223 Assert.AreEqual (65536, Approximate (65536, sock.ReceiveTimeout), "ReceiveTimeoutChange #5");
1226 sock.ReceiveTimeout = -2;
1227 Assert.Fail ("ReceiveTimeoutChange #8");
1228 } catch (ArgumentOutOfRangeException) {
1235 [ExpectedException (typeof(ObjectDisposedException))]
1236 public void ReceiveTimeoutClosed ()
1238 Socket sock = new Socket (AddressFamily.InterNetwork,
1244 int val = sock.ReceiveTimeout;
1248 public void NoDelayDefaultTcp ()
1250 Socket sock = new Socket (AddressFamily.InterNetwork,
1254 Assert.AreEqual (false, sock.NoDelay, "NoDelayDefaultTcp");
1260 public void NoDelayChangeTcp ()
1262 Socket sock = new Socket (AddressFamily.InterNetwork,
1266 sock.NoDelay = true;
1268 Assert.AreEqual (true, sock.NoDelay, "NoDelayChangeTcp");
1274 public void NoDelayDefaultUdp ()
1276 Socket sock = new Socket (AddressFamily.InterNetwork,
1281 bool val = sock.NoDelay;
1282 Assert.Fail ("NoDelayDefaultUdp #1");
1283 } catch (SocketException ex) {
1284 Assert.AreEqual (10042, ex.ErrorCode,
1285 "NoDelayDefaultUdp #2");
1292 public void NoDelayChangeUdp ()
1294 Socket sock = new Socket (AddressFamily.InterNetwork,
1299 sock.NoDelay = true;
1300 Assert.Fail ("NoDelayChangeUdp #1");
1301 } catch (SocketException ex) {
1302 Assert.AreEqual (10042, ex.ErrorCode,
1303 "NoDelayChangeUdp #2");
1310 [ExpectedException (typeof(ObjectDisposedException))]
1311 public void NoDelayClosed ()
1313 Socket sock = new Socket (AddressFamily.InterNetwork,
1319 bool val = sock.NoDelay;
1322 static bool BAAccepted = false;
1323 static Socket BASocket = null;
1324 static ManualResetEvent BACalledBack = new ManualResetEvent (false);
1326 private static void BACallback (IAsyncResult asyncResult)
1328 Socket sock = (Socket)asyncResult.AsyncState;
1330 BASocket = sock.EndAccept (asyncResult);
1333 BACalledBack.Set ();
1337 [ExpectedException (typeof(InvalidOperationException))]
1338 public void BeginAcceptNotBound ()
1340 Socket sock = new Socket (AddressFamily.InterNetwork,
1344 sock.BeginAccept (BACallback, sock);
1350 #if FEATURE_NO_BSD_SOCKETS
1351 [ExpectedException (typeof (PlatformNotSupportedException))]
1353 [ExpectedException (typeof(InvalidOperationException))]
1355 public void BeginAcceptNotListening ()
1357 Socket sock = new Socket (AddressFamily.InterNetwork,
1361 sock.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
1363 sock.BeginAccept (BACallback, sock);
1369 #if FEATURE_NO_BSD_SOCKETS
1370 [ExpectedException (typeof (PlatformNotSupportedException))]
1372 public void BeginAccept ()
1374 Socket sock = new Socket (AddressFamily.InterNetwork,
1377 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1378 NetworkHelpers.FindFreePort ());
1383 BACalledBack.Reset ();
1385 sock.BeginAccept (BACallback, sock);
1387 Socket conn = new Socket (AddressFamily.InterNetwork,
1393 if (BACalledBack.WaitOne (2000, false) == false) {
1394 Assert.Fail ("BeginAccept wait timed out");
1397 Assert.AreEqual (true, BAAccepted, "BeginAccept #1");
1398 Assert.AreEqual (true, BASocket.Connected, "BeginAccept #2");
1399 Assert.AreEqual (false, sock.Connected, "BeginAccept #3");
1400 Assert.AreEqual (true, conn.Connected, "BeginAccept #4");
1408 [ExpectedException (typeof(ObjectDisposedException))]
1409 public void BeginAcceptClosed ()
1411 Socket sock = new Socket (AddressFamily.InterNetwork,
1417 sock.BeginAccept (BACallback, sock);
1420 static bool BADAccepted = false;
1421 static Socket BADSocket = null;
1422 static byte[] BADBytes;
1423 static int BADByteCount;
1424 static ManualResetEvent BADCalledBack = new ManualResetEvent (false);
1426 private static void BADCallback (IAsyncResult asyncResult)
1428 Socket sock = (Socket)asyncResult.AsyncState;
1430 BADSocket = sock.EndAccept (out BADBytes,
1435 BADCalledBack.Set ();
1439 #if FEATURE_NO_BSD_SOCKETS
1440 [ExpectedException (typeof (PlatformNotSupportedException))]
1442 public void BeginAcceptData ()
1444 Socket sock = new Socket (AddressFamily.InterNetwork,
1447 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1448 NetworkHelpers.FindFreePort ());
1453 BADCalledBack.Reset ();
1455 sock.BeginAccept (256, BADCallback, sock);
1457 Socket conn = new Socket (AddressFamily.InterNetwork,
1460 byte[] send_bytes = new byte[] {10, 11, 12, 13};
1463 conn.Send (send_bytes);
1465 if (BADCalledBack.WaitOne (2000, false) == false) {
1466 Assert.Fail ("BeginAcceptData wait timed out");
1469 Assert.AreEqual (true, BADAccepted, "BeginAcceptData #1");
1470 Assert.AreEqual (true, BADSocket.Connected, "BeginAcceptData #2");
1471 Assert.AreEqual (false, sock.Connected, "BeginAcceptData #3");
1472 Assert.AreEqual (true, conn.Connected, "BeginAcceptData #4");
1473 Assert.AreEqual (send_bytes.Length, BADByteCount, "BeginAcceptData #5");
1475 /* The MS runtime gives the returned data in a
1476 * much bigger array. TODO: investigate
1477 * whether it the size correlates to the first
1478 * parameter in BeginAccept()
1480 Assert.IsFalse (BADBytes.Length == send_bytes.Length,
1481 "BeginAcceptData #6");
1483 for(int i = 0; i < send_bytes.Length; i++) {
1484 Assert.AreEqual (send_bytes[i], BADBytes[i], "BeginAcceptData #" + (i+7).ToString ());
1493 [ExpectedException (typeof(ObjectDisposedException))]
1494 public void BeginAcceptDataClosed ()
1496 Socket sock = new Socket (AddressFamily.InterNetwork,
1502 sock.BeginAccept (256, BADCallback, sock);
1506 #if FEATURE_NO_BSD_SOCKETS
1507 [ExpectedException (typeof (PlatformNotSupportedException))]
1509 public void BeginAcceptSocketUdp ()
1511 Socket sock = new Socket (AddressFamily.InterNetwork,
1514 Socket acc = new Socket (AddressFamily.InterNetwork,
1518 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1519 NetworkHelpers.FindFreePort ());
1525 sock.BeginAccept (acc, 256, BADCallback, sock);
1526 Assert.Fail ("BeginAcceptSocketUdp #1");
1527 } catch (SocketException ex) {
1528 Assert.AreEqual (10022, ex.ErrorCode, "BeginAcceptSocketUdp #2");
1536 #if FEATURE_NO_BSD_SOCKETS
1537 [ExpectedException (typeof (PlatformNotSupportedException))]
1539 public void BeginAcceptSocketBound ()
1541 Socket sock = new Socket (AddressFamily.InterNetwork,
1544 Socket acc = new Socket (AddressFamily.InterNetwork,
1548 IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
1549 NetworkHelpers.FindFreePort ());
1551 IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
1552 NetworkHelpers.FindFreePort ());
1560 sock.BeginAccept (acc, 256, BADCallback, sock);
1561 Assert.Fail ("BeginAcceptSocketBound #1");
1562 } catch (InvalidOperationException) {
1570 #if FEATURE_NO_BSD_SOCKETS
1571 [ExpectedException (typeof (PlatformNotSupportedException))]
1573 public void BeginAcceptSocket ()
1575 Socket sock = new Socket (AddressFamily.InterNetwork,
1578 Socket acc = new Socket (AddressFamily.InterNetwork,
1582 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1583 NetworkHelpers.FindFreePort ());
1588 BADCalledBack.Reset ();
1590 sock.BeginAccept (acc, 256, BADCallback, sock);
1592 Socket conn = new Socket (AddressFamily.InterNetwork,
1595 byte[] send_bytes = new byte[] {10, 11, 12, 13};
1598 conn.Send (send_bytes);
1600 if (BADCalledBack.WaitOne (2000, false) == false) {
1601 Assert.Fail ("BeginAcceptSocket wait timed out");
1604 Assert.AreEqual (true, BADAccepted, "BeginAcceptSocket #1");
1605 Assert.AreEqual (true, BADSocket.Connected, "BeginAcceptSocket #2");
1606 Assert.AreEqual (false, sock.Connected, "BeginAcceptSocket #3");
1607 Assert.AreEqual (true, conn.Connected, "BeginAcceptSocket #4");
1608 Assert.AreEqual (send_bytes.Length, BADByteCount, "BeginAcceptSocket #5");
1609 Assert.AreEqual (AddressFamily.InterNetwork, acc.AddressFamily, "BeginAcceptSocket #6");
1610 Assert.AreEqual (SocketType.Stream, acc.SocketType, "BeginAcceptSocket #7");
1611 Assert.AreEqual (ProtocolType.Tcp, acc.ProtocolType, "BeginAcceptSocket #8");
1612 Assert.AreEqual (conn.LocalEndPoint, acc.RemoteEndPoint, "BeginAcceptSocket #9");
1614 /* The MS runtime gives the returned data in a
1615 * much bigger array. TODO: investigate
1616 * whether it the size correlates to the first
1617 * parameter in BeginAccept()
1619 Assert.IsFalse (BADBytes.Length == send_bytes.Length,
1620 "BeginAcceptSocket #10");
1622 for(int i = 0; i < send_bytes.Length; i++) {
1623 Assert.AreEqual (send_bytes[i], BADBytes[i], "BeginAcceptSocket #" + (i+11).ToString ());
1633 public void BeginAcceptSocketClosed ()
1635 Socket sock = new Socket (AddressFamily.InterNetwork,
1638 Socket acc = new Socket (AddressFamily.InterNetwork,
1645 sock.BeginAccept (acc, 256, BADCallback, null);
1646 Assert.Fail ("BeginAcceptSocketClosed #1");
1647 } catch (ObjectDisposedException) {
1654 #if FEATURE_NO_BSD_SOCKETS
1655 [ExpectedException (typeof (PlatformNotSupportedException))]
1657 public void BeginAcceptSocketAccClosed ()
1659 Socket sock = new Socket (AddressFamily.InterNetwork,
1662 Socket acc = new Socket (AddressFamily.InterNetwork,
1665 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1666 NetworkHelpers.FindFreePort ());
1673 BADCalledBack.Reset ();
1676 sock.BeginAccept (acc, 256, BADCallback, null);
1677 Assert.Fail ("BeginAcceptSocketAccClosed #1");
1678 } catch (ObjectDisposedException) {
1684 static bool BCConnected = false;
1685 static ManualResetEvent BCCalledBack = new ManualResetEvent (false);
1687 private static void BCCallback (IAsyncResult asyncResult)
1689 Socket sock = (Socket)asyncResult.AsyncState;
1692 sock.EndConnect (asyncResult);
1693 } catch (Exception e) {
1694 Console.WriteLine ("BCCallback exception:");
1695 Console.WriteLine (e);
1702 BCCalledBack.Set ();
1706 #if FEATURE_NO_BSD_SOCKETS
1707 [ExpectedException (typeof (PlatformNotSupportedException))]
1709 public void BeginConnectAddressPort ()
1711 Socket sock = new Socket (AddressFamily.InterNetwork,
1714 Socket listen = new Socket (AddressFamily.InterNetwork,
1717 IPAddress ip = IPAddress.Loopback;
1718 IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
1723 BCCalledBack.Reset ();
1725 BCConnected = false;
1727 sock.BeginConnect (ip, ep.Port, BCCallback, sock);
1729 if (BCCalledBack.WaitOne (2000, false) == false) {
1730 Assert.Fail ("BeginConnectAddressPort wait timed out");
1733 Assert.AreEqual (true, BCConnected, "BeginConnectAddressPort #1");
1740 public void BeginConnectAddressPortNull ()
1742 Socket sock = new Socket (AddressFamily.InterNetwork,
1745 IPAddress ip = null;
1748 sock.BeginConnect (ip, 1244, BCCallback,
1750 Assert.Fail ("BeginConnectAddressPortNull #1");
1751 } catch (ArgumentNullException) {
1758 #if FEATURE_NO_BSD_SOCKETS
1759 [ExpectedException (typeof (PlatformNotSupportedException))]
1761 public void BeginConnectAddressPortListen ()
1763 Socket sock = new Socket (AddressFamily.InterNetwork,
1766 IPAddress ip = IPAddress.Loopback;
1767 IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
1773 sock.BeginConnect (ip, ep.Port, BCCallback, sock);
1774 Assert.Fail ("BeginConnectAddressPortListen #1");
1775 } catch (InvalidOperationException) {
1782 [ExpectedException (typeof(ObjectDisposedException))]
1783 public void BeginConnectAddressPortClosed ()
1785 Socket sock = new Socket (AddressFamily.InterNetwork,
1788 IPAddress ip = IPAddress.Loopback;
1792 sock.BeginConnect (ip, 1244, BCCallback, sock);
1796 [Category ("NotOnMac")]
1798 * This is not a Mono bug.
1800 * By default, only 127.0.0.1 is enabled and you must explicitly
1801 * enable additional addresses using 'sudo ifconfig lo0 alias 127.0.0.1'.
1803 * I tested this on Mac OS 10.7.4; a 'ping 127.0.0.2' does not work
1804 * until I add that alias.
1807 public void BeginConnectMultiple ()
1809 Socket sock = new Socket (AddressFamily.InterNetwork,
1812 Socket listen = new Socket (AddressFamily.InterNetwork,
1815 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1816 NetworkHelpers.FindFreePort ());
1817 IPAddress[] ips = new IPAddress[4];
1819 ips[0] = IPAddress.Parse ("127.0.0.4");
1820 ips[1] = IPAddress.Parse ("127.0.0.3");
1821 ips[2] = IPAddress.Parse ("127.0.0.2");
1822 ips[3] = IPAddress.Parse ("127.0.0.1");
1827 BCCalledBack.Reset ();
1829 BCConnected = false;
1831 sock.BeginConnect (ips, ep.Port, BCCallback, sock);
1833 /* Longer wait here, because the ms runtime
1834 * takes a lot longer to not connect
1837 if (BCCalledBack.WaitOne (30000, false) == false) {
1838 Assert.Fail ("BeginConnectMultiple wait failed");
1842 Assert.IsTrue (BCCalledBack.WaitOne (30000), "#0");
1844 Assert.AreEqual (true, BCConnected, "BeginConnectMultiple #1");
1845 Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "BeginConnectMultiple #2");
1846 IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
1848 Assert.AreEqual (IPAddress.Loopback, remep.Address, "BeginConnectMultiple #2");
1855 #if FEATURE_NO_BSD_SOCKETS
1856 [ExpectedException (typeof (PlatformNotSupportedException))]
1858 public void BeginConnectMultiple2 ()
1860 Socket sock = new Socket (AddressFamily.InterNetwork,
1863 Socket listen = new Socket (AddressFamily.InterNetwork,
1867 // Need at least two addresses.
1868 var ips = Dns.GetHostAddresses (string.Empty);
1870 Assert.Ignore ("This test needs at least two IP addresses.");
1872 var allIps = new IPAddress [ips.Length + 1];
1873 allIps [0] = IPAddress.Loopback;
1874 ips.CopyTo (allIps, 1);
1877 * Only bind to the loopback interface, so all the non-loopback
1878 * IP addresses will fail. BeginConnect()/EndConnect() should
1879 * succeed it it can connect to at least one of the requested
1882 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
1887 BCCalledBack.Reset ();
1889 BCConnected = false;
1891 sock.BeginConnect (allIps, ep.Port, BCCallback, sock);
1893 /* Longer wait here, because the ms runtime
1894 * takes a lot longer to not connect
1896 if (BCCalledBack.WaitOne (10000, false) == false) {
1897 Assert.Fail ("BeginConnectMultiple2 wait failed");
1900 Assert.AreEqual (true, BCConnected, "BeginConnectMultiple2 #1");
1901 Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "BeginConnectMultiple2 #2");
1902 IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
1904 Assert.AreEqual (IPAddress.Loopback, remep.Address, "BeginConnectMultiple2 #2");
1912 public void BeginConnectMultipleNull ()
1914 Socket sock = new Socket (AddressFamily.InterNetwork,
1917 IPAddress[] ips = null;
1920 sock.BeginConnect (ips, 1246, BCCallback,
1922 Assert.Fail ("BeginConnectMultipleNull #1");
1923 } catch (ArgumentNullException) {
1930 #if FEATURE_NO_BSD_SOCKETS
1931 [ExpectedException (typeof (PlatformNotSupportedException))]
1933 public void BeginConnectMultipleListen ()
1935 Socket sock = new Socket (AddressFamily.InterNetwork,
1938 IPAddress[] ips = new IPAddress[4];
1939 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1940 NetworkHelpers.FindFreePort ());
1942 ips[0] = IPAddress.Parse ("127.0.0.4");
1943 ips[1] = IPAddress.Parse ("127.0.0.3");
1944 ips[2] = IPAddress.Parse ("127.0.0.2");
1945 ips[3] = IPAddress.Parse ("127.0.0.1");
1951 sock.BeginConnect (ips, ep.Port, BCCallback,
1953 Assert.Fail ("BeginConnectMultipleListen #1");
1954 } catch (InvalidOperationException) {
1961 [ExpectedException (typeof(ObjectDisposedException))]
1962 public void BeginConnectMultipleClosed ()
1964 Socket sock = new Socket (AddressFamily.InterNetwork,
1967 IPAddress[] ips = new IPAddress[4];
1969 ips[0] = IPAddress.Parse ("127.0.0.4");
1970 ips[1] = IPAddress.Parse ("127.0.0.3");
1971 ips[2] = IPAddress.Parse ("127.0.0.2");
1972 ips[3] = IPAddress.Parse ("127.0.0.1");
1976 sock.BeginConnect (ips, 1247, BCCallback, sock);
1980 public void BeginConnectHostPortNull ()
1982 Socket sock = new Socket (AddressFamily.InterNetwork,
1987 sock.BeginConnect ((string)null, 0,
1989 Assert.Fail ("BeginConnectHostPort #1");
1990 } catch (ArgumentNullException) {
1997 #if FEATURE_NO_BSD_SOCKETS
1998 [ExpectedException (typeof (PlatformNotSupportedException))]
2000 public void BeginConnectHostPortListen ()
2002 Socket sock = new Socket (AddressFamily.InterNetwork,
2005 IPAddress ip = IPAddress.Loopback;
2006 IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
2012 sock.BeginConnect ("localhost", ep.Port,
2014 Assert.Fail ("BeginConnectHostPortListen #1");
2015 } catch (InvalidOperationException) {
2022 [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
2023 public void BeginConnectHostPortNotIP ()
2025 Socket sock = new Socket (AddressFamily.NetBios,
2026 SocketType.Seqpacket,
2027 ProtocolType.Unspecified);
2030 sock.BeginConnect ("localhost", 0, BCCallback,
2032 Assert.Fail ("BeginConnectHostPortNotIP #1");
2033 } catch (NotSupportedException) {
2040 [ExpectedException (typeof(ObjectDisposedException))]
2041 public void BeginConnectHostPortClosed ()
2043 Socket sock = new Socket (AddressFamily.InterNetwork,
2049 sock.BeginConnect ("localhost", 0, BCCallback, sock);
2052 static bool BDDisconnected = false;
2053 static ManualResetEvent BDCalledBack = new ManualResetEvent (false);
2055 private static void BDCallback (IAsyncResult asyncResult)
2057 Socket sock = (Socket)asyncResult.AsyncState;
2059 sock.EndDisconnect (asyncResult);
2060 BDDisconnected = true;
2062 BDCalledBack.Set ();
2066 [Category ("NotDotNet")] // "Needs XP or later"
2067 #if FEATURE_NO_BSD_SOCKETS
2068 [ExpectedException (typeof (PlatformNotSupportedException))]
2070 public void BeginDisconnect ()
2072 Socket sock = new Socket (AddressFamily.InterNetwork,
2075 Socket listen = new Socket (AddressFamily.InterNetwork,
2078 IPAddress ip = IPAddress.Loopback;
2079 IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
2084 sock.Connect (ip, ep.Port);
2086 Assert.AreEqual (true, sock.Connected, "BeginDisconnect #1");
2088 sock.Shutdown (SocketShutdown.Both);
2090 BDCalledBack.Reset ();
2091 BDDisconnected = false;
2093 sock.BeginDisconnect (false, BDCallback, sock);
2095 if (BDCalledBack.WaitOne (2000, false) == false) {
2096 Assert.Fail ("BeginDisconnect wait timed out");
2099 Assert.AreEqual (true, BDDisconnected, "BeginDisconnect #2");
2100 Assert.AreEqual (false, sock.Connected, "BeginDisconnect #3");
2107 public void BeginReceiveSocketError ()
2112 public void BeginReceiveGeneric ()
2117 public void BeginReceiveGenericSocketError ()
2121 private static void BSCallback (IAsyncResult asyncResult)
2123 Socket sock = (Socket)asyncResult.AsyncState;
2125 sock.EndSend (asyncResult);
2129 public void BeginSendNotConnected ()
2131 Socket sock = new Socket (AddressFamily.InterNetwork,
2135 byte[] send_bytes = new byte[] {10, 11, 12, 13};
2138 sock.BeginSend (send_bytes, 0,
2140 SocketFlags.None, BSCallback,
2142 Assert.Fail ("BeginSendNotConnected #1");
2143 } catch (SocketException ex) {
2144 Assert.AreEqual (10057, ex.ErrorCode, "BeginSendNotConnected #2");
2151 public void BeginSendSocketError ()
2156 public void BeginSendGeneric ()
2161 public void BeginSendGenericSocketError ()
2166 #if FEATURE_NO_BSD_SOCKETS
2167 [ExpectedException (typeof (PlatformNotSupportedException))]
2169 public void BindTwice ()
2171 Socket sock = new Socket (AddressFamily.InterNetwork,
2174 IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
2175 NetworkHelpers.FindFreePort ());
2176 IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
2177 NetworkHelpers.FindFreePort ());
2183 Assert.Fail ("BindTwice #1");
2184 } catch (SocketException ex) {
2185 Assert.AreEqual (10022, ex.ErrorCode, "BindTwice #2");
2192 #if FEATURE_NO_BSD_SOCKETS
2193 [ExpectedException (typeof (PlatformNotSupportedException))]
2195 public void Close ()
2197 Socket sock = new Socket (AddressFamily.InterNetwork,
2200 Socket listen = new Socket (AddressFamily.InterNetwork,
2203 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2204 NetworkHelpers.FindFreePort ());
2211 Assert.AreEqual (true, sock.Connected, "Close #1");
2215 Thread.Sleep (3000);
2217 Assert.AreEqual (false, sock.Connected, "Close #2");
2223 #if FEATURE_NO_BSD_SOCKETS
2224 [ExpectedException (typeof (PlatformNotSupportedException))]
2226 public void ConnectAddressPort ()
2228 Socket sock = new Socket (AddressFamily.InterNetwork,
2231 Socket listen = new Socket (AddressFamily.InterNetwork,
2234 IPAddress ip = IPAddress.Loopback;
2235 IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
2240 sock.Connect (ip, ep.Port);
2242 Assert.AreEqual (true, sock.Connected, "ConnectAddressPort #1");
2249 public void ConnectAddressPortNull ()
2251 Socket sock = new Socket (AddressFamily.InterNetwork,
2254 IPAddress ip = null;
2257 sock.Connect (ip, 1249);
2258 Assert.Fail ("ConnectAddressPortNull #1");
2259 } catch (ArgumentNullException) {
2266 #if FEATURE_NO_BSD_SOCKETS
2267 [ExpectedException (typeof (PlatformNotSupportedException))]
2269 public void ConnectAddressPortListen ()
2271 Socket sock = new Socket (AddressFamily.InterNetwork,
2274 IPAddress ip = IPAddress.Loopback;
2275 IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
2281 sock.Connect (ip, ep.Port);
2282 Assert.Fail ("ConnectAddressPortListen #1");
2283 } catch (InvalidOperationException) {
2290 [ExpectedException (typeof(ObjectDisposedException))]
2291 public void ConnectAddressPortClosed ()
2293 Socket sock = new Socket (AddressFamily.InterNetwork,
2296 IPAddress ip = IPAddress.Loopback;
2300 sock.Connect (ip, 1250);
2304 [Category ("NotOnMac")] // MacOSX trashes the fd after the failed connect attempt to 127.0.0.4
2306 * This is not a Mono bug.
2308 * By default, only 127.0.0.1 is enabled and you must explicitly
2309 * enable additional addresses using 'sudo ifconfig lo0 alias 127.0.0.1'.
2311 * I tested this on Mac OS 10.7.4; a 'ping 127.0.0.2' does not work
2312 * until I add that alias.
2314 * However, after doing so, Mac OS treats these as separate addresses, ie. attempting
2315 * to connect to 127.0.0.4 yields a connection refused.
2317 * When using Connect(), the .NET runtime also throws an exception if connecting to
2318 * any of the IP addresses fails. This is different with BeginConnect()/EndConnect()
2319 * which succeeds when it can connect to at least one of the addresses.
2322 public void ConnectMultiple ()
2324 Socket sock = new Socket (AddressFamily.InterNetwork,
2327 Socket listen = new Socket (AddressFamily.InterNetwork,
2330 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2331 NetworkHelpers.FindFreePort ());
2332 IPAddress[] ips = new IPAddress[4];
2334 ips[0] = IPAddress.Parse ("127.0.0.4");
2335 ips[1] = IPAddress.Parse ("127.0.0.3");
2336 ips[2] = IPAddress.Parse ("127.0.0.2");
2337 ips[3] = IPAddress.Parse ("127.0.0.1");
2342 sock.Connect (ips, ep.Port);
2344 Assert.AreEqual (true, sock.Connected, "ConnectMultiple #1");
2345 Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "ConnectMultiple #2");
2346 IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
2348 Assert.AreEqual (IPAddress.Loopback, remep.Address, "ConnectMultiple #2");
2355 #if FEATURE_NO_BSD_SOCKETS
2356 [ExpectedException (typeof (PlatformNotSupportedException))]
2358 public void ConnectMultiple2 ()
2360 Socket sock = new Socket (AddressFamily.InterNetwork,
2363 Socket listen = new Socket (AddressFamily.InterNetwork,
2367 // Need at least two addresses.
2368 var ips = Dns.GetHostAddresses (string.Empty);
2370 Assert.Ignore ("This test needs at least two IP addresses.");
2372 var allIps = new IPAddress [ips.Length + 1];
2373 allIps [0] = IPAddress.Loopback;
2374 ips.CopyTo (allIps, 1);
2377 * Bind to IPAddress.Any; Connect() will fail unless it can
2378 * connect to all the addresses in allIps.
2380 IPEndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
2385 sock.Connect (allIps, ep.Port);
2387 Assert.AreEqual (true, sock.Connected, "ConnectMultiple2 #1");
2388 Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "ConnectMultiple2 #2");
2389 IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
2391 Assert.AreEqual (IPAddress.Loopback, remep.Address, "ConnectMultiple2 #3");
2398 public void ConnectMultipleNull ()
2400 Socket sock = new Socket (AddressFamily.InterNetwork,
2403 IPAddress[] ips = null;
2406 sock.Connect (ips, 1251);
2407 Assert.Fail ("ConnectMultipleNull #1");
2408 } catch (ArgumentNullException) {
2415 #if FEATURE_NO_BSD_SOCKETS
2416 [ExpectedException (typeof (PlatformNotSupportedException))]
2418 public void ConnectMultipleListen ()
2420 Socket sock = new Socket (AddressFamily.InterNetwork,
2423 IPAddress[] ips = new IPAddress[4];
2424 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2425 NetworkHelpers.FindFreePort ());
2427 ips[0] = IPAddress.Parse ("127.0.0.4");
2428 ips[1] = IPAddress.Parse ("127.0.0.3");
2429 ips[2] = IPAddress.Parse ("127.0.0.2");
2430 ips[3] = IPAddress.Parse ("127.0.0.1");
2436 sock.Connect (ips, ep.Port);
2437 Assert.Fail ("ConnectMultipleListen #1");
2438 } catch (InvalidOperationException) {
2445 [ExpectedException (typeof(ObjectDisposedException))]
2446 public void ConnectMultipleClosed ()
2448 Socket sock = new Socket (AddressFamily.InterNetwork,
2451 IPAddress[] ips = new IPAddress[4];
2453 ips[0] = IPAddress.Parse ("127.0.0.4");
2454 ips[1] = IPAddress.Parse ("127.0.0.3");
2455 ips[2] = IPAddress.Parse ("127.0.0.2");
2456 ips[3] = IPAddress.Parse ("127.0.0.1");
2460 sock.Connect (ips, 1252);
2464 public void ConnectHostPortNull ()
2466 Socket sock = new Socket (AddressFamily.InterNetwork,
2471 sock.Connect ((string)null, 0);
2472 Assert.Fail ("ConnectHostPort #1");
2473 } catch (ArgumentNullException) {
2480 #if FEATURE_NO_BSD_SOCKETS
2481 [ExpectedException (typeof (PlatformNotSupportedException))]
2483 public void ConnectHostPortListen ()
2485 Socket sock = new Socket (AddressFamily.InterNetwork,
2488 IPAddress ip = IPAddress.Loopback;
2489 IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
2495 sock.Connect ("localhost", ep.Port);
2496 Assert.Fail ("ConnectHostPortListen #1");
2497 } catch (InvalidOperationException) {
2504 [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
2505 public void ConnectHostPortNotIP ()
2507 Socket sock = new Socket (AddressFamily.NetBios,
2508 SocketType.Seqpacket,
2509 ProtocolType.Unspecified);
2512 sock.Connect ("localhost", 0);
2513 Assert.Fail ("ConnectHostPortNotIP #1");
2514 } catch (NotSupportedException) {
2521 #if FEATURE_NO_BSD_SOCKETS
2522 [ExpectedException (typeof (PlatformNotSupportedException))]
2524 [ExpectedException (typeof(ObjectDisposedException))]
2526 public void ConnectHostPortClosed ()
2528 Socket sock = new Socket (AddressFamily.InterNetwork,
2534 sock.Connect ("localhost", 0);
2538 [Category ("NotDotNet")] // "Needs XP or later"
2539 #if FEATURE_NO_BSD_SOCKETS
2540 [ExpectedException (typeof (PlatformNotSupportedException))]
2542 public void Disconnect ()
2544 Socket sock = new Socket (AddressFamily.InterNetwork,
2547 Socket listen = new Socket (AddressFamily.InterNetwork,
2550 IPAddress ip = IPAddress.Loopback;
2551 IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
2556 sock.Connect (ip, ep.Port);
2558 Assert.AreEqual (true, sock.Connected, "Disconnect #1");
2560 sock.Shutdown (SocketShutdown.Both);
2562 sock.Disconnect (false);
2564 Assert.AreEqual (false, sock.Connected, "BeginDisconnect #3");
2571 public void DuplicateAndClose ()
2576 public void IOControl ()
2581 public void TestDefaultsDualMode ()
2583 using (var socket = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp)){
2584 Assert.IsTrue (socket.DualMode, "In Mono, DualMode must be true when constructing InterNetworkV6 sockets");
2587 using (var socket = new Socket (SocketType.Stream, ProtocolType.Tcp)){
2588 Assert.AreEqual (AddressFamily.InterNetworkV6, socket.AddressFamily, "When creating sockets of type stream/tcp, the address family should be InterNetworkV6");
2589 Assert.IsTrue (socket.DualMode, "In Mono, DualMode must be true when constructing InterNetworkV6 sockets");
2591 socket.DualMode = false;
2593 Assert.IsFalse (socket.DualMode, "Setting of DualSocket should turn DualSockets off");
2599 #if FEATURE_NO_BSD_SOCKETS
2600 [ExpectedException (typeof (PlatformNotSupportedException))]
2602 public void ReceiveGeneric ()
2606 IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, NetworkHelpers.FindFreePort ());
2608 Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2609 listensock.Bind (endpoint);
2610 listensock.Listen(1);
2612 Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2613 sendsock.Connect(endpoint);
2615 Socket clientsock = listensock.Accept();
2617 byte[] sendbuf = new byte[256];
2619 for(i = 0; i < 256; i++) {
2620 sendbuf[i] = (byte)i;
2622 for (i = 4; i < 6; i++) {
2623 Assert.AreEqual (sendbuf[i], (byte)i,
2624 "#1/" + i.ToString());
2628 sendsock.Send (sendbuf, 0, 256, SocketFlags.None,
2632 byte[] recvbuf = new byte[256];
2633 List<ArraySegment<byte>> recvbuflist = new List<ArraySegment<byte>>(2);
2634 recvbuflist.Add(new ArraySegment<byte>(recvbuf, 4, 2));
2635 recvbuflist.Add(new ArraySegment<byte>(recvbuf, 20, 230));
2637 clientsock.Receive (recvbuflist);
2639 /* recvbuf should now hold the first 2 bytes
2640 * of sendbuf from pos 4, and the next 230
2641 * bytes of sendbuf from pos 20
2644 for (i = 0; i < 2; i++) {
2645 Assert.AreEqual (sendbuf[i], recvbuf[i + 4],
2646 "#2/" + i.ToString());
2648 for (i = 2; i < 232; i++) {
2649 Assert.AreEqual (sendbuf[i], recvbuf[i + 18],
2650 "#2/" + i.ToString());
2654 clientsock.Close ();
2655 listensock.Close ();
2659 #if FEATURE_NO_BSD_SOCKETS
2660 [ExpectedException (typeof (PlatformNotSupportedException))]
2662 public void SendGeneric ()
2666 IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, NetworkHelpers.FindFreePort ());
2668 Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2669 listensock.Bind (endpoint);
2670 listensock.Listen(1);
2672 Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2673 sendsock.Connect(endpoint);
2675 Socket clientsock = listensock.Accept();
2677 byte[] sendbuf = new byte[256];
2678 List<ArraySegment<byte>> sendbuflist = new List<ArraySegment<byte>>(2);
2680 sendbuflist.Add(new ArraySegment<byte>(sendbuf, 4, 2));
2681 sendbuflist.Add(new ArraySegment<byte>(sendbuf, 20, 230));
2683 for(i = 0; i < 256; i++) {
2684 sendbuf[i] = (byte)i;
2686 for (i = 4; i < 6; i++) {
2687 Assert.AreEqual (sendbuf[i], (byte)i,
2688 "#1/" + i.ToString());
2692 sendsock.Send (sendbuflist, SocketFlags.None, out err);
2695 byte[] recvbuf = new byte[256];
2697 clientsock.Receive (recvbuf);
2699 /* The first 2 bytes of recvbuf should now
2700 * hold 2 bytes of sendbuf from pos 4, and the
2701 * next 230 bytes of recvbuf should be sendbuf
2705 for (i = 0; i < 2; i++) {
2706 Assert.AreEqual (recvbuf[i], sendbuf[i + 4],
2707 "#2/" + i.ToString());
2709 for (i = 2; i < 232; i++) {
2710 Assert.AreEqual (recvbuf[i], sendbuf[i + 18],
2711 "#2/" + i.ToString());
2715 clientsock.Close ();
2716 listensock.Close ();
2720 #if FEATURE_NO_BSD_SOCKETS
2721 [ExpectedException (typeof (AggregateException))] // Something catches the PlatformNotSupportedException and re-throws an AggregateException
2723 public void ConcurrentExceedSocketLimit ()
2725 var tasks = new Task[4];
2726 for (int i = 0; i < 4; i++) {
2727 tasks[i] = Task.Factory.StartNew (() => SendGenericExceedBuffer ());
2729 Task.WaitAll (tasks);
2733 #if FEATURE_NO_BSD_SOCKETS
2734 [ExpectedException (typeof (PlatformNotSupportedException))]
2736 public void SendGenericExceedBuffer ()
2738 // Create a buffer larger than the default max.
2739 const int BUFFER_SIZE = 65 * 1024;
2742 IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, NetworkHelpers.FindFreePort ());
2744 Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2745 listensock.Bind (endpoint);
2746 listensock.Listen (1);
2748 Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2749 sendsock.Connect (endpoint);
2751 Socket clientsock = listensock.Accept ();
2753 byte[] sendbuf = new byte[BUFFER_SIZE];
2755 for (i = 0; i < BUFFER_SIZE; i++) {
2756 sendbuf[i] = (byte)i;
2759 Task sendTask = Task.Factory.StartNew(() => {
2760 int sent = sendsock.Send (sendbuf);
2762 Assert.AreEqual (BUFFER_SIZE, sent, "#1");
2765 byte[] recvbuf = new byte[BUFFER_SIZE];
2767 Task recvTask = Task.Factory.StartNew(() => {
2768 int totalReceived = 0;
2769 byte[] buffer = new byte[256];
2770 while (totalReceived < sendbuf.Length) {
2771 int recvd = clientsock.Receive (buffer, 0, buffer.Length, SocketFlags.None);
2772 Array.Copy (buffer, 0, recvbuf, totalReceived, recvd);
2773 totalReceived += recvd;
2776 Assert.AreEqual (BUFFER_SIZE, totalReceived, "#2");
2779 Assert.IsTrue (Task.WaitAll (new []{sendTask, recvTask}, 20 * 1000), "#2a");
2781 for (i = 0; i < BUFFER_SIZE; i++) {
2782 Assert.AreEqual (recvbuf[i], sendbuf[i],
2783 "#3/" + i.ToString());
2787 clientsock.Close ();
2788 listensock.Close ();
2792 public void ListenNotBound ()
2794 Socket sock = new Socket (AddressFamily.InterNetwork,
2800 Assert.Fail ("ListenNotBound #1");
2801 } catch (SocketException ex) {
2802 Assert.AreEqual (10022, ex.ErrorCode, "ListenNotBound #2");
2808 static Socket CWRSocket;
2809 static bool CWRReceiving = true;
2810 static ManualResetEvent CWRReady = new ManualResetEvent (false);
2812 private static void CWRReceiveThread ()
2814 byte[] buf = new byte[256];
2817 CWRSocket.Receive (buf);
2818 } catch (SocketException) {
2819 CWRReceiving = false;
2826 #if FEATURE_NO_BSD_SOCKETS
2827 [ExpectedException (typeof (PlatformNotSupportedException))]
2829 public void CloseWhileReceiving ()
2831 CWRSocket = new Socket (AddressFamily.InterNetwork,
2834 CWRSocket.Bind (new IPEndPoint (IPAddress.Loopback,
2835 NetworkHelpers.FindFreePort ()));
2837 Thread recv_thread = new Thread (new ThreadStart (CWRReceiveThread));
2839 recv_thread.Start ();
2840 Thread.Sleep (250); /* Wait for the thread to be already receiving */
2843 if (CWRReady.WaitOne (1000, false) == false) {
2844 Assert.Fail ("CloseWhileReceiving wait timed out");
2847 Assert.IsFalse (CWRReceiving);
2850 static bool RRCLastRead = false;
2851 static ManualResetEvent RRCReady = new ManualResetEvent (false);
2853 private static void RRCClientThread (int port)
2855 byte[] bytes = new byte[8];
2858 Socket sock = new Socket (AddressFamily.InterNetwork,
2861 sock.Connect (new IPEndPoint (IPAddress.Loopback,
2864 NetworkStream stream = new NetworkStream (sock);
2866 readbyte = stream.ReadByte ();
2867 Assert.AreEqual (0, readbyte, "ReceiveRemoteClosed #1");
2869 stream.Read (bytes, 0, 0);
2871 readbyte = stream.ReadByte ();
2872 Assert.AreEqual (0, readbyte, "ReceiveRemoteClosed #2");
2874 stream.Read (bytes, 0, 0);
2876 readbyte = stream.ReadByte ();
2877 Assert.AreEqual (-1, readbyte, "ReceiveRemoteClosed #3");
2885 [Test] // Receive (Byte [])
2886 public void Receive1_Buffer_Null ()
2888 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2892 s.Receive ((byte []) null);
2894 } catch (ArgumentNullException ex) {
2895 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2896 Assert.IsNull (ex.InnerException, "#3");
2897 Assert.IsNotNull (ex.Message, "#4");
2898 Assert.AreEqual ("buffer", ex.ParamName, "#5");
2904 [Test] // Receive (Byte [])
2905 public void Receive1_Socket_Closed ()
2907 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2912 s.Receive ((byte []) null);
2914 } catch (ObjectDisposedException ex) {
2915 // Cannot access a disposed object
2916 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2917 Assert.IsNull (ex.InnerException, "#3");
2918 Assert.IsNotNull (ex.Message, "#4");
2919 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2923 [Test] // Receive (Byte [], SocketFlags)
2924 public void Receive2_Buffer_Null ()
2926 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2930 s.Receive ((byte []) null, (SocketFlags) 666);
2932 } catch (ArgumentNullException ex) {
2933 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2934 Assert.IsNull (ex.InnerException, "#3");
2935 Assert.IsNotNull (ex.Message, "#4");
2936 Assert.AreEqual ("buffer", ex.ParamName, "#5");
2942 [Test] // Receive (Byte [], SocketFlags)
2943 public void Receive2_Socket_Closed ()
2945 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2950 s.Receive ((byte []) null, (SocketFlags) 666);
2952 } catch (ObjectDisposedException ex) {
2953 // Cannot access a disposed object
2954 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2955 Assert.IsNull (ex.InnerException, "#3");
2956 Assert.IsNotNull (ex.Message, "#4");
2957 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2961 [Test] // Receive (Byte [], Int32, SocketFlags)
2962 public void Receive3_Buffer_Null ()
2964 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2968 s.Receive ((byte []) null, 0, (SocketFlags) 666);
2970 } catch (ArgumentNullException ex) {
2971 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2972 Assert.IsNull (ex.InnerException, "#3");
2973 Assert.IsNotNull (ex.Message, "#4");
2974 Assert.AreEqual ("buffer", ex.ParamName, "#5");
2980 [Test] // Receive (Byte [], Int32, SocketFlags)
2981 public void Receive3_Socket_Closed ()
2983 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2988 s.Receive ((byte []) null, 0, (SocketFlags) 666);
2990 } catch (ObjectDisposedException ex) {
2991 // Cannot access a disposed object
2992 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2993 Assert.IsNull (ex.InnerException, "#3");
2994 Assert.IsNotNull (ex.Message, "#4");
2995 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2999 [Test] // Receive (Byte [], Int32, Int32, SocketFlags)
3000 public void Receive4_Buffer_Null ()
3002 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3006 s.Receive ((byte []) null, 0, 0, (SocketFlags) 666);
3008 } catch (ArgumentNullException ex) {
3009 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3010 Assert.IsNull (ex.InnerException, "#3");
3011 Assert.IsNotNull (ex.Message, "#4");
3012 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3018 [Test] // Receive (Byte [], Int32, Int32, SocketFlags)
3019 public void Receive4_Socket_Closed ()
3021 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3026 s.Receive ((byte []) null, 0, 0, (SocketFlags) 666);
3028 } catch (ObjectDisposedException ex) {
3029 // Cannot access a disposed object
3030 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3031 Assert.IsNull (ex.InnerException, "#3");
3032 Assert.IsNotNull (ex.Message, "#4");
3033 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3037 [Test] // Receive (Byte [], Int32, Int32, SocketFlags, out SocketError)
3038 public void Receive5_Buffer_Null ()
3040 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3045 s.Receive ((byte []) null, 0, 0, SocketFlags.None, out error);
3047 } catch (ArgumentNullException ex) {
3048 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3049 Assert.IsNull (ex.InnerException, "#3");
3050 Assert.IsNotNull (ex.Message, "#4");
3051 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3057 [Test] // Receive (Byte [], Int32, Int32, SocketFlags, out SocketError)
3058 public void Receive5_Socket_Closed ()
3060 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3066 s.Receive ((byte []) null, 0, 0, SocketFlags.None, out error);
3068 } catch (ObjectDisposedException ex) {
3069 // Cannot access a disposed object
3070 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3071 Assert.IsNull (ex.InnerException, "#3");
3072 Assert.IsNotNull (ex.Message, "#4");
3073 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3077 [Test] // Receive (IList<ArraySegment<Byte>>)
3078 public void Receive6_Buffers_Null ()
3080 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3084 s.Receive ((IList<ArraySegment<byte>>) null);
3086 } catch (ArgumentNullException ex) {
3087 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3088 Assert.IsNull (ex.InnerException, "#3");
3089 Assert.IsNotNull (ex.Message, "#4");
3090 Assert.AreEqual ("buffers", ex.ParamName, "#5");
3096 [Test] // Receive (IList<ArraySegment<Byte>>)
3097 public void Receive6_Socket_Closed ()
3099 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3104 s.Receive ((IList<ArraySegment<byte>>) null);
3106 } catch (ObjectDisposedException ex) {
3107 // Cannot access a disposed object
3108 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3109 Assert.IsNull (ex.InnerException, "#3");
3110 Assert.IsNotNull (ex.Message, "#4");
3111 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3115 [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags)
3116 public void Receive7_Buffers_Null ()
3118 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3122 s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666);
3124 } catch (ArgumentNullException ex) {
3125 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3126 Assert.IsNull (ex.InnerException, "#3");
3127 Assert.IsNotNull (ex.Message, "#4");
3128 Assert.AreEqual ("buffers", ex.ParamName, "#5");
3134 [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags)
3135 public void Receive7_Socket_Closed ()
3137 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3142 s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666);
3144 } catch (ObjectDisposedException ex) {
3145 // Cannot access a disposed object
3146 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3147 Assert.IsNull (ex.InnerException, "#3");
3148 Assert.IsNotNull (ex.Message, "#4");
3149 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3153 [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags, out SocketError)
3154 public void Receive8_Buffers_Null ()
3156 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3161 s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666,
3164 } catch (ArgumentNullException ex) {
3165 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3166 Assert.IsNull (ex.InnerException, "#3");
3167 Assert.IsNotNull (ex.Message, "#4");
3168 Assert.AreEqual ("buffers", ex.ParamName, "#5");
3174 [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags, out SocketError)
3175 public void Receive8_Socket_Closed ()
3177 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3183 s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666,
3186 } catch (ObjectDisposedException ex) {
3187 // Cannot access a disposed object
3188 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3189 Assert.IsNull (ex.InnerException, "#3");
3190 Assert.IsNotNull (ex.Message, "#4");
3191 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3197 [Test] // ReceiveFrom (Byte [], ref EndPoint)
3198 #if FEATURE_NO_BSD_SOCKETS
3199 [ExpectedException (typeof (PlatformNotSupportedException))]
3201 public void ReceiveFrom1_Buffer_Null ()
3203 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3206 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3208 s.ReceiveFrom ((Byte []) null, ref remoteEP);
3210 } catch (ArgumentNullException ex) {
3211 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3212 Assert.IsNull (ex.InnerException, "#3");
3213 Assert.IsNotNull (ex.Message, "#4");
3214 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3220 [Test] // ReceiveFrom (Byte [], ref EndPoint)
3221 public void ReceiveFrom1_RemoteEP_Null ()
3223 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3226 byte [] buffer = new byte [0];
3227 EndPoint remoteEP = null;
3229 s.ReceiveFrom (buffer, ref remoteEP);
3231 } catch (ArgumentNullException ex) {
3232 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3233 Assert.IsNull (ex.InnerException, "#3");
3234 Assert.IsNotNull (ex.Message, "#4");
3235 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
3241 [Test] // ReceiveFrom (Byte [], ref EndPoint)
3242 #if FEATURE_NO_BSD_SOCKETS
3243 [ExpectedException (typeof (PlatformNotSupportedException))]
3245 public void ReceiveFrom1_Socket_Closed ()
3247 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3251 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3253 s.ReceiveFrom ((Byte []) null, ref remoteEP);
3255 } catch (ObjectDisposedException ex) {
3256 // Cannot access a disposed object
3257 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3258 Assert.IsNull (ex.InnerException, "#3");
3259 Assert.IsNotNull (ex.Message, "#4");
3260 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3264 [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
3265 #if FEATURE_NO_BSD_SOCKETS
3266 [ExpectedException (typeof (PlatformNotSupportedException))]
3268 public void ReceiveFrom2_Buffer_Null ()
3270 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3273 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3275 s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP);
3277 } catch (ArgumentNullException ex) {
3278 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3279 Assert.IsNull (ex.InnerException, "#3");
3280 Assert.IsNotNull (ex.Message, "#4");
3281 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3287 [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
3288 public void ReceiveFrom2_RemoteEP_Null ()
3290 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3293 byte [] buffer = new byte [5];
3294 EndPoint remoteEP = null;
3296 s.ReceiveFrom (buffer, (SocketFlags) 666, ref remoteEP);
3298 } catch (ArgumentNullException ex) {
3299 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3300 Assert.IsNull (ex.InnerException, "#3");
3301 Assert.IsNotNull (ex.Message, "#4");
3302 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
3308 [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
3309 #if FEATURE_NO_BSD_SOCKETS
3310 [ExpectedException (typeof (PlatformNotSupportedException))]
3312 public void ReceiveFrom2_Socket_Closed ()
3314 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3318 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3320 s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP);
3322 } catch (ObjectDisposedException ex) {
3323 // Cannot access a disposed object
3324 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3325 Assert.IsNull (ex.InnerException, "#3");
3326 Assert.IsNotNull (ex.Message, "#4");
3327 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3331 [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
3332 #if FEATURE_NO_BSD_SOCKETS
3333 [ExpectedException (typeof (PlatformNotSupportedException))]
3335 public void ReceiveFrom3_Buffer_Null ()
3337 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3340 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3342 s.ReceiveFrom ((Byte []) null, 0, (SocketFlags) 666,
3345 } catch (ArgumentNullException ex) {
3346 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3347 Assert.IsNull (ex.InnerException, "#3");
3348 Assert.IsNotNull (ex.Message, "#4");
3349 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3355 [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
3356 public void ReceiveFrom3_RemoteEP_Null ()
3358 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3361 byte [] buffer = new byte [5];
3362 EndPoint remoteEP = null;
3364 s.ReceiveFrom (buffer, buffer.Length, (SocketFlags) 666, ref remoteEP);
3366 } catch (ArgumentNullException ex) {
3367 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3368 Assert.IsNull (ex.InnerException, "#3");
3369 Assert.IsNotNull (ex.Message, "#4");
3370 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
3376 [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
3377 #if FEATURE_NO_BSD_SOCKETS
3378 [ExpectedException (typeof (PlatformNotSupportedException))]
3380 public void ReceiveFrom3_Size_OutOfRange ()
3383 byte [] buffer = new byte [5];
3384 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3387 s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3390 s.ReceiveFrom (buffer, -1, (SocketFlags) 666, ref remoteEP);
3391 Assert.Fail ("#A1");
3392 } catch (ArgumentOutOfRangeException ex) {
3393 // Specified argument was out of the range of valid values
3394 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
3395 Assert.IsNull (ex.InnerException, "#A3");
3396 Assert.IsNotNull (ex.Message, "#A4");
3397 Assert.AreEqual ("size", ex.ParamName, "#A5");
3402 // size > buffer length
3403 s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3406 s.ReceiveFrom (buffer, (buffer.Length + 1), (SocketFlags) 666,
3408 Assert.Fail ("#B1");
3409 } catch (ArgumentOutOfRangeException ex) {
3410 // Specified argument was out of the range of valid values
3411 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
3412 Assert.IsNull (ex.InnerException, "#B3");
3413 Assert.IsNotNull (ex.Message, "#B4");
3414 Assert.AreEqual ("size", ex.ParamName, "#B5");
3420 [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
3421 #if FEATURE_NO_BSD_SOCKETS
3422 [ExpectedException (typeof (PlatformNotSupportedException))]
3424 public void ReceiveFrom3_Socket_Closed ()
3426 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3430 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3432 s.ReceiveFrom ((Byte []) null, -1, (SocketFlags) 666,
3435 } catch (ObjectDisposedException ex) {
3436 // Cannot access a disposed object
3437 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3438 Assert.IsNull (ex.InnerException, "#3");
3439 Assert.IsNotNull (ex.Message, "#4");
3440 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3444 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
3445 #if FEATURE_NO_BSD_SOCKETS
3446 [ExpectedException (typeof (PlatformNotSupportedException))]
3448 public void ReceiveFrom4_Buffer_Null ()
3450 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3452 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3455 s.ReceiveFrom ((Byte []) null, -1, -1, (SocketFlags) 666,
3458 } catch (ArgumentNullException ex) {
3459 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3460 Assert.IsNull (ex.InnerException, "#3");
3461 Assert.IsNotNull (ex.Message, "#4");
3462 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3466 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
3467 #if FEATURE_NO_BSD_SOCKETS
3468 [ExpectedException (typeof (PlatformNotSupportedException))]
3470 public void ReceiveFrom4_Offset_OutOfRange ()
3473 byte [] buffer = new byte [5];
3474 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3477 s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3480 s.ReceiveFrom (buffer, -1, 0, (SocketFlags) 666,
3482 Assert.Fail ("#A1");
3483 } catch (ArgumentOutOfRangeException ex) {
3484 // Specified argument was out of the range of valid values
3485 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
3486 Assert.IsNull (ex.InnerException, "#A3");
3487 Assert.IsNotNull (ex.Message, "#A4");
3488 Assert.AreEqual ("offset", ex.ParamName, "#A5");
3493 // offset > buffer length
3494 s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3497 s.ReceiveFrom (buffer, (buffer.Length + 1), 0, (SocketFlags) 666,
3499 Assert.Fail ("#B1");
3500 } catch (ArgumentOutOfRangeException ex) {
3501 // Specified argument was out of the range of valid values
3502 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
3503 Assert.IsNull (ex.InnerException, "#B3");
3504 Assert.IsNotNull (ex.Message, "#B4");
3505 Assert.AreEqual ("offset", ex.ParamName, "#B5");
3511 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, ref IPEndPoint)
3512 public void ReceiveFrom4_RemoteEP_Null ()
3514 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3516 byte [] buffer = new byte [5];
3517 EndPoint remoteEP = null;
3520 s.ReceiveFrom (buffer, 0, buffer.Length, (SocketFlags) 666, ref remoteEP);
3522 } catch (ArgumentNullException ex) {
3523 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3524 Assert.IsNull (ex.InnerException, "#3");
3525 Assert.IsNotNull (ex.Message, "#4");
3526 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
3532 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
3533 #if FEATURE_NO_BSD_SOCKETS
3534 [ExpectedException (typeof (PlatformNotSupportedException))]
3536 public void ReceiveFrom4_Size_OutOfRange ()
3539 byte [] buffer = new byte [5];
3540 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3543 s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3546 s.ReceiveFrom (buffer, 0, -1, (SocketFlags) 666,
3548 Assert.Fail ("#A1");
3549 } catch (ArgumentOutOfRangeException ex) {
3550 // Specified argument was out of the range of valid values
3551 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
3552 Assert.IsNull (ex.InnerException, "#A3");
3553 Assert.IsNotNull (ex.Message, "#A4");
3554 Assert.AreEqual ("size", ex.ParamName, "#A5");
3559 // size > buffer length
3560 s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3563 s.ReceiveFrom (buffer, 0, (buffer.Length + 1), (SocketFlags) 666,
3565 Assert.Fail ("#B1");
3566 } catch (ArgumentOutOfRangeException ex) {
3567 // Specified argument was out of the range of valid values
3568 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
3569 Assert.IsNull (ex.InnerException, "#B3");
3570 Assert.IsNotNull (ex.Message, "#B4");
3571 Assert.AreEqual ("size", ex.ParamName, "#B5");
3576 // offset + size > buffer length
3577 s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3580 s.ReceiveFrom (buffer, 2, 4, (SocketFlags) 666, ref remoteEP);
3581 Assert.Fail ("#C1");
3582 } catch (ArgumentOutOfRangeException ex) {
3583 // Specified argument was out of the range of valid values
3584 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
3585 Assert.IsNull (ex.InnerException, "#C3");
3586 Assert.IsNotNull (ex.Message, "#C4");
3587 Assert.AreEqual ("size", ex.ParamName, "#C5");
3593 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, ref EndPoint)
3594 #if FEATURE_NO_BSD_SOCKETS
3595 [ExpectedException (typeof (PlatformNotSupportedException))]
3597 public void ReceiveFrom4_Socket_Closed ()
3599 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3603 byte [] buffer = new byte [5];
3604 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3606 s.ReceiveFrom (buffer, -1, -1, (SocketFlags) 666,
3609 } catch (ObjectDisposedException ex) {
3610 // Cannot access a disposed object
3611 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3612 Assert.IsNull (ex.InnerException, "#3");
3613 Assert.IsNotNull (ex.Message, "#4");
3614 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3619 #if FEATURE_NO_BSD_SOCKETS
3620 [ExpectedException (typeof (PlatformNotSupportedException))]
3622 public void ReceiveRemoteClosed ()
3624 var port = NetworkHelpers.FindFreePort ();
3625 Socket sock = new Socket (AddressFamily.InterNetwork,
3628 sock.Bind (new IPEndPoint (IPAddress.Loopback, port));
3632 Thread client_thread = new Thread (() => RRCClientThread (port));
3633 client_thread.Start ();
3635 Socket client = sock.Accept ();
3636 NetworkStream stream = new NetworkStream (client);
3637 stream.WriteByte (0x00);
3638 stream.WriteByte (0x00);
3642 RRCReady.WaitOne (1000, false);
3643 Assert.IsTrue (RRCLastRead);
3647 // Test case for bug #471580
3649 #if FEATURE_NO_BSD_SOCKETS
3650 [ExpectedException (typeof (PlatformNotSupportedException))]
3652 public void UdpDoubleBind ()
3654 using (Socket s = new Socket (AddressFamily.InterNetwork,
3655 SocketType.Dgram, ProtocolType.Udp))
3656 using (Socket ss = new Socket (AddressFamily.InterNetwork,
3657 SocketType.Dgram, ProtocolType.Udp)) {
3658 var supportsReuseAddress = true;
3660 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
3661 } catch (SocketException e) {
3662 // Exception is thrown when ReuseAddress is not supported
3663 Assert.AreEqual ((int) SocketError.OperationNotSupported, e.NativeErrorCode,
3664 "Expected SocketError.OperationNotSupported");
3665 supportsReuseAddress = false;
3668 var ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
3671 if (supportsReuseAddress)
3672 ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
3675 ss.Bind (new IPEndPoint (IPAddress.Any, ep.Port));
3676 if (!supportsReuseAddress)
3677 Assert.Fail ("Reusing address is not supported, exception was expected on second bind.");
3678 } catch (SocketException e) {
3683 // Test case for bug #31557
3685 #if FEATURE_NO_BSD_SOCKETS
3686 [ExpectedException (typeof (PlatformNotSupportedException))]
3688 public void TcpDoubleBind ()
3690 using (Socket s = new Socket (AddressFamily.InterNetwork,
3691 SocketType.Stream, ProtocolType.Tcp))
3692 using (Socket ss = new Socket (AddressFamily.InterNetwork,
3693 SocketType.Stream, ProtocolType.Tcp)) {
3694 var supportsReuseAddress = true;
3696 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
3697 } catch (SocketException e) {
3698 // Exception is thrown when ReuseAddress is not supported
3699 Assert.AreEqual ((int) SocketError.OperationNotSupported, e.NativeErrorCode,
3700 "Expected SocketError.OperationNotSupported");
3701 supportsReuseAddress = false;
3704 var ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
3708 if (supportsReuseAddress)
3709 ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
3712 ss.Bind (new IPEndPoint (IPAddress.Any, ep.Port));
3714 if (!supportsReuseAddress)
3715 Assert.Fail ("Reusing address is not supported, exception was expected on second bind.");
3716 } catch (SocketException e) {
3722 [Category ("NotOnMac")]
3723 #if FULL_AOT_DESKTOP
3724 [Ignore ("https://bugzilla.xamarin.com/show_bug.cgi?id=52157")]
3726 public void ConnectedProperty ()
3728 TcpListener listener = new TcpListener (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3731 Socket client = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3732 client.Connect (IPAddress.Loopback, ((IPEndPoint)listener.LocalEndpoint).Port);
3733 Socket server = listener.AcceptSocket ();
3736 server.EndSend(server.BeginSend (new byte[10], 0, 10, SocketFlags.None, null, null));
3739 server.EndReceive (server.BeginReceive (new byte[10], 0, 10, SocketFlags.None, null, null));
3742 Assert.IsTrue (!client.Connected);
3743 Assert.IsTrue (!server.Connected);
3751 [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName)
3752 public void GetSocketOption1_Socket_Closed ()
3754 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3757 s.GetSocketOption (0, 0);
3759 } catch (ObjectDisposedException ex) {
3760 // Cannot access a disposed object
3761 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3762 Assert.IsNull (ex.InnerException, "#3");
3763 Assert.IsNotNull (ex.Message, "#4");
3764 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3768 [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3769 public void GetSocketOption2_OptionValue_Null ()
3771 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3773 s.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
3776 } catch (SocketException ex) {
3777 // The system detected an invalid pointer address in attempting
3778 // to use a pointer argument in a call
3779 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
3780 Assert.AreEqual (10014, ex.ErrorCode, "#3");
3781 Assert.IsNull (ex.InnerException, "#4");
3782 Assert.IsNotNull (ex.Message, "#5");
3783 Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
3784 Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
3788 [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3789 public void GetSocketOption2_Socket_Closed ()
3791 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3794 s.GetSocketOption (0, 0, (byte []) null);
3796 } catch (ObjectDisposedException ex) {
3797 // Cannot access a disposed object
3798 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3799 Assert.IsNull (ex.InnerException, "#3");
3800 Assert.IsNotNull (ex.Message, "#4");
3801 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3805 [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
3806 public void GetSocketOption3_Socket_Closed ()
3808 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3811 s.GetSocketOption (0, 0, 0);
3813 } catch (ObjectDisposedException ex) {
3814 // Cannot access a disposed object
3815 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3816 Assert.IsNull (ex.InnerException, "#3");
3817 Assert.IsNotNull (ex.Message, "#4");
3818 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3822 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3823 public void SetSocketOption1_DontLinger ()
3825 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3826 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
3827 new byte [] { 0x00 });
3828 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
3829 new byte [] { 0x01 });
3833 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3834 public void SetSocketOption1_DontLinger_Null ()
3836 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3838 s.SetSocketOption (SocketOptionLevel.Socket,
3839 SocketOptionName.DontLinger, (byte []) null);
3841 } catch (SocketException ex) {
3842 // The system detected an invalid pointer address in attempting
3843 // to use a pointer argument in a call
3844 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
3845 Assert.AreEqual (10014, ex.ErrorCode, "#3");
3846 Assert.IsNull (ex.InnerException, "#4");
3847 Assert.IsNotNull (ex.Message, "#5");
3848 Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
3849 Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
3854 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3855 public void SetSocketOption1_Linger_Null ()
3857 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3859 s.SetSocketOption (SocketOptionLevel.Socket,
3860 SocketOptionName.DontLinger, (byte []) null);
3862 } catch (SocketException ex) {
3863 // The system detected an invalid pointer address in attempting
3864 // to use a pointer argument in a call
3865 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
3866 Assert.AreEqual (10014, ex.ErrorCode, "#3");
3867 Assert.IsNull (ex.InnerException, "#4");
3868 Assert.IsNotNull (ex.Message, "#5");
3869 Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
3870 Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
3875 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3876 public void SetSocketOption1_Socket_Close ()
3878 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3881 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
3882 new byte [] { 0x00 });
3884 } catch (ObjectDisposedException ex) {
3885 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3886 Assert.IsNull (ex.InnerException, "#3");
3887 Assert.IsNotNull (ex.Message, "#4");
3888 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3892 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
3893 public void SetSocketOption2_DontLinger ()
3895 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3896 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 0);
3897 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 5);
3901 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
3902 [Category ("NotWorking")]
3903 public void SetSocketOption2_Linger ()
3905 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3906 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, 0);
3907 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, 5);
3911 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
3912 public void SetSocketOption2_Socket_Closed ()
3914 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3917 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 0);
3919 } catch (ObjectDisposedException ex) {
3920 // Cannot access a disposed object
3921 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3922 Assert.IsNull (ex.InnerException, "#3");
3923 Assert.IsNotNull (ex.Message, "#4");
3924 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3928 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3929 #if FEATURE_NO_BSD_SOCKETS
3930 [ExpectedException (typeof (PlatformNotSupportedException))]
3932 public void SetSocketOption3_AddMembershipIPv4_IPv6MulticastOption ()
3934 IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
3936 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
3937 s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
3939 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
3940 new IPv6MulticastOption (mcast_addr));
3942 } catch (ArgumentException ex) {
3943 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
3944 Assert.IsNull (ex.InnerException, "#3");
3945 Assert.IsNotNull (ex.Message, "#4");
3946 // The specified value is not a valid 'MulticastOption'
3947 Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
3948 Assert.AreEqual ("optionValue", ex.ParamName, "#6");
3953 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3954 #if FEATURE_NO_BSD_SOCKETS
3955 [ExpectedException (typeof (PlatformNotSupportedException))]
3957 public void SetSocketOption3_AddMembershipIPv4_MulticastOption ()
3959 IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
3961 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
3962 s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
3963 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
3964 new MulticastOption (mcast_addr));
3968 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3969 [Category ("NotWorking")]
3970 public void SetSocketOption3_AddMembershipIPv4_Socket_NotBound ()
3972 IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
3974 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
3976 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
3977 new MulticastOption (mcast_addr));
3979 } catch (SocketException ex) {
3980 // An invalid argument was supplied
3981 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
3982 Assert.AreEqual (10022, ex.ErrorCode, "#3");
3983 Assert.IsNull (ex.InnerException, "#4");
3984 Assert.IsNotNull (ex.Message, "#5");
3985 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
3986 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
3992 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3993 #if FEATURE_NO_BSD_SOCKETS
3994 [ExpectedException (typeof (PlatformNotSupportedException))]
3996 public void SetSocketOption3_AddMembershipIPv6_IPv6MulticastOption ()
3998 if (!Socket.OSSupportsIPv6)
3999 Assert.Ignore ("IPv6 not enabled.");
4001 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
4003 using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
4004 s.Bind (new IPEndPoint (IPAddress.IPv6Any, NetworkHelpers.FindFreePort ()));
4005 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
4006 new IPv6MulticastOption (mcast_addr));
4010 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4011 #if FEATURE_NO_BSD_SOCKETS
4012 [ExpectedException (typeof (PlatformNotSupportedException))]
4014 public void SetSocketOption3_AddMembershipIPv6_MulticastOption ()
4016 if (!Socket.OSSupportsIPv6)
4017 Assert.Ignore ("IPv6 not enabled.");
4019 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
4021 using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
4022 s.Bind (new IPEndPoint (IPAddress.IPv6Any, NetworkHelpers.FindFreePort ()));
4024 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
4025 new MulticastOption (mcast_addr));
4027 } catch (ArgumentException ex) {
4028 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4029 Assert.IsNull (ex.InnerException, "#3");
4030 Assert.IsNotNull (ex.Message, "#4");
4031 // The specified value is not a valid 'IPv6MulticastOption'
4032 Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
4033 Assert.AreEqual ("optionValue", ex.ParamName, "#6");
4038 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4039 [Category ("NotWorking")]
4040 public void SetSocketOption3_AddMembershipIPv6_Socket_NotBound ()
4042 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
4044 Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
4046 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
4047 new IPv6MulticastOption (mcast_addr));
4049 } catch (SocketException ex) {
4050 // An invalid argument was supplied
4051 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
4052 Assert.AreEqual (10022, ex.ErrorCode, "#3");
4053 Assert.IsNull (ex.InnerException, "#4");
4054 Assert.IsNotNull (ex.Message, "#5");
4055 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
4056 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
4062 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4063 public void SetSocketOption3_DontLinger_Boolean ()
4065 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4067 s.SetSocketOption (SocketOptionLevel.Socket,
4068 SocketOptionName.DontLinger, (object) false);
4070 } catch (ArgumentException ex) {
4071 // The specified value is not valid
4072 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4073 Assert.IsNull (ex.InnerException, "#3");
4074 Assert.IsNotNull (ex.Message, "#4");
4075 Assert.AreEqual ("optionValue", ex.ParamName, "#5");
4080 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4081 public void SetSocketOption3_DontLinger_Int32 ()
4083 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4085 s.SetSocketOption (SocketOptionLevel.Socket,
4086 SocketOptionName.DontLinger, (object) 0);
4088 } catch (ArgumentException ex) {
4089 // The specified value is not valid
4090 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4091 Assert.IsNull (ex.InnerException, "#3");
4092 Assert.IsNotNull (ex.Message, "#4");
4093 Assert.AreEqual ("optionValue", ex.ParamName, "#5");
4098 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4099 public void SetSocketOption3_DontLinger_LingerOption ()
4101 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4103 s.SetSocketOption (SocketOptionLevel.Socket,
4104 SocketOptionName.DontLinger, new LingerOption (true, 1000));
4106 } catch (ArgumentException ex) {
4107 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4108 Assert.IsNull (ex.InnerException, "#3");
4109 // The specified value is not valid
4110 Assert.IsNotNull (ex.Message, "#4");
4111 Assert.AreEqual ("optionValue", ex.ParamName, "#5");
4116 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4117 public void SetSocketOption3_Linger_Boolean ()
4119 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4121 s.SetSocketOption (SocketOptionLevel.Socket,
4122 SocketOptionName.Linger, (object) false);
4124 } catch (ArgumentException ex) {
4125 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4126 Assert.IsNull (ex.InnerException, "#3");
4127 // The specified value is not valid
4128 Assert.IsNotNull (ex.Message, "#4");
4129 Assert.AreEqual ("optionValue", ex.ParamName, "#5");
4134 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4135 public void SetSocketOption3_Linger_Int32 ()
4137 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4139 s.SetSocketOption (SocketOptionLevel.Socket,
4140 SocketOptionName.Linger, (object) 0);
4142 } catch (ArgumentException ex) {
4143 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4144 Assert.IsNull (ex.InnerException, "#3");
4145 // The specified value is not valid
4146 Assert.IsNotNull (ex.Message, "#4");
4147 Assert.AreEqual ("optionValue", ex.ParamName, "#5");
4152 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4153 public void SetSocketOption3_Linger_LingerOption ()
4155 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4156 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
4157 new LingerOption (false, 0));
4158 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
4159 new LingerOption (true, 0));
4160 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
4161 new LingerOption (false, 1000));
4162 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
4163 new LingerOption (true, 1000));
4167 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4168 #if FEATURE_NO_BSD_SOCKETS
4169 [ExpectedException (typeof (PlatformNotSupportedException))]
4171 public void SetSocketOption3_DropMembershipIPv4_IPv6MulticastOption ()
4173 IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
4175 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
4176 s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
4177 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
4178 new MulticastOption (mcast_addr));
4180 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
4181 new IPv6MulticastOption (mcast_addr));
4183 } catch (ArgumentException ex) {
4184 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4185 Assert.IsNull (ex.InnerException, "#3");
4186 Assert.IsNotNull (ex.Message, "#4");
4187 // The specified value is not a valid 'MulticastOption'
4188 Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
4189 Assert.AreEqual ("optionValue", ex.ParamName, "#6");
4194 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4195 #if FEATURE_NO_BSD_SOCKETS
4196 [ExpectedException (typeof (PlatformNotSupportedException))]
4198 public void SetSocketOption3_DropMembershipIPv4_MulticastOption ()
4200 IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
4202 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
4203 MulticastOption option = new MulticastOption (mcast_addr);
4205 s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
4206 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
4208 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
4213 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4214 [Category ("NotWorking")]
4215 public void SetSocketOption3_DropMembershipIPv4_Socket_NotBound ()
4217 IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
4219 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
4221 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
4222 new MulticastOption (mcast_addr));
4224 } catch (SocketException ex) {
4225 // An invalid argument was supplied
4226 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
4227 Assert.AreEqual (10022, ex.ErrorCode, "#3");
4228 Assert.IsNull (ex.InnerException, "#4");
4229 Assert.IsNotNull (ex.Message, "#5");
4230 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
4231 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
4237 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4238 #if FEATURE_NO_BSD_SOCKETS
4239 [ExpectedException (typeof (PlatformNotSupportedException))]
4241 public void SetSocketOption3_DropMembershipIPv6_IPv6MulticastOption ()
4243 if (!Socket.OSSupportsIPv6)
4244 Assert.Ignore ("IPv6 not enabled.");
4246 using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
4247 IPv6MulticastOption option = new IPv6MulticastOption (
4248 IPAddress.Parse ("ff02::1"));
4250 s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
4251 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
4253 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
4258 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4259 #if FEATURE_NO_BSD_SOCKETS
4260 [ExpectedException (typeof (PlatformNotSupportedException))]
4262 public void SetSocketOption3_DropMembershipIPv6_MulticastOption ()
4264 if (!Socket.OSSupportsIPv6)
4265 Assert.Ignore ("IPv6 not enabled.");
4267 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
4269 using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
4270 s.Bind (new IPEndPoint (IPAddress.IPv6Any, NetworkHelpers.FindFreePort ()));
4271 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
4272 new IPv6MulticastOption (mcast_addr));
4274 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
4275 new MulticastOption (mcast_addr));
4277 } catch (ArgumentException ex) {
4278 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4279 Assert.IsNull (ex.InnerException, "#3");
4280 Assert.IsNotNull (ex.Message, "#4");
4281 // The specified value is not a valid 'IPv6MulticastOption'
4282 Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
4283 Assert.AreEqual ("optionValue", ex.ParamName, "#6");
4288 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4289 [Category ("NotWorking")]
4290 public void SetSocketOption3_DropMembershipIPv6_Socket_NotBound ()
4292 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
4294 Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
4296 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
4297 new IPv6MulticastOption (mcast_addr));
4299 } catch (SocketException ex) {
4300 // An invalid argument was supplied
4301 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
4302 Assert.AreEqual (10022, ex.ErrorCode, "#3");
4303 Assert.IsNull (ex.InnerException, "#4");
4304 Assert.IsNotNull (ex.Message, "#5");
4305 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
4306 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
4312 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4313 public void SetSocketOption3_OptionValue_Null ()
4315 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4317 s.SetSocketOption (SocketOptionLevel.Socket,
4318 SocketOptionName.Linger, (object) null);
4320 } catch (ArgumentNullException ex) {
4321 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
4322 Assert.IsNull (ex.InnerException, "#3");
4323 Assert.IsNotNull (ex.Message, "#4");
4324 Assert.AreEqual ("optionValue", ex.ParamName, "#5");
4329 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4330 public void SetSocketOption3_Socket_Closed ()
4332 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4335 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
4336 new LingerOption (false, 0));
4338 } catch (ObjectDisposedException ex) {
4339 // Cannot access a disposed object
4340 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
4341 Assert.IsNull (ex.InnerException, "#3");
4342 Assert.IsNotNull (ex.Message, "#4");
4343 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
4348 [Category ("RequiresBSDSockets")] // on watchOS device this happens: System.Net.Sockets.SocketException : The requested address is not valid in this context. This situation is too complex to detect and throw a PlatformNotSupportedException, so just ignore it.
4349 public void SetSocketOption_MulticastInterfaceIndex_Any ()
4351 IPAddress ip = IPAddress.Parse ("239.255.255.250");
4353 using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
4355 s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
4356 s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
4361 [Category ("RequiresBSDSockets")] // on watchOS device this happens: System.Net.Sockets.SocketException : The requested address is not valid in this context. This situation is too complex to detect and throw a PlatformNotSupportedException, so just ignore it.
4362 public void SetSocketOption_MulticastInterfaceIndex_Loopback ()
4364 IPAddress ip = IPAddress.Parse ("239.255.255.250");
4366 using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
4368 s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
4369 s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
4374 public void SetSocketOption_MulticastInterfaceIndex_Invalid ()
4376 IPAddress ip = IPAddress.Parse ("239.255.255.250");
4378 using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
4382 s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
4389 s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
4398 #if FEATURE_NO_BSD_SOCKETS
4399 [ExpectedException (typeof (PlatformNotSupportedException))]
4401 public void Shutdown_NoConnect ()
4403 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4404 s.Bind (new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ()));
4407 s.Shutdown (SocketShutdown.Both);
4409 } catch (SocketException exc) {
4410 Assert.AreEqual (10057, exc.ErrorCode, "#2");
4417 [ExpectedException (typeof (NullReferenceException))]
4418 public void ReceiveAsync_Null ()
4420 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4421 s.ReceiveAsync (null);
4426 [ExpectedException (typeof (NullReferenceException))]
4427 public void ReceiveAsync_Default ()
4429 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4430 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
4431 s.ReceiveAsync (saea);
4437 [ExpectedException (typeof (NullReferenceException))]
4438 public void ReceiveAsync_NullBuffer ()
4440 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4441 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
4442 saea.SetBuffer (null, 0, 0);
4443 s.ReceiveAsync (null);
4448 [ExpectedException (typeof (ObjectDisposedException))]
4449 public void ReceiveAsync_ClosedSocket ()
4451 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4453 s.ReceiveAsync (null);
4457 [ExpectedException (typeof (NullReferenceException))]
4458 public void SendAsync_Null ()
4460 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4466 [ExpectedException (typeof (NullReferenceException))]
4467 public void SendAsync_Default ()
4469 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4470 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
4477 [ExpectedException (typeof (NullReferenceException))]
4478 public void SendAsync_NullBuffer ()
4480 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4481 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
4482 saea.SetBuffer (null, 0, 0);
4488 [ExpectedException (typeof (ObjectDisposedException))]
4489 public void SendAsync_ClosedSocket ()
4491 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4497 #if FEATURE_NO_BSD_SOCKETS
4498 [ExpectedException (typeof (PlatformNotSupportedException))]
4500 public void SendAsyncFile ()
4502 Socket serverSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4504 serverSocket.Bind (new IPEndPoint (IPAddress.Loopback, 0));
4505 serverSocket.Listen (1);
4507 var mReceived = new ManualResetEvent (false);
4509 serverSocket.BeginAccept (AsyncCall => {
4510 byte[] bytes = new byte [1024];
4512 Socket listener = (Socket)AsyncCall.AsyncState;
4513 Socket client = listener.EndAccept (AsyncCall);
4514 client.Receive (bytes, bytes.Length, 0);
4519 Socket clientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4520 clientSocket.Connect (serverSocket.LocalEndPoint);
4521 clientSocket.NoDelay = true;
4523 // Initialize buffer used to create testing file
4524 var buffer = new byte [1024];
4525 for (int i = 0; i < 1024; ++i)
4526 buffer [i] = (byte) (i % 256);
4528 string temp = Path.GetTempFileName ();
4530 // Testing file creation
4531 using (StreamWriter sw = new StreamWriter (temp)) {
4535 var mSent = new ManualResetEvent (false);
4537 // Async Send File to server
4538 clientSocket.BeginSendFile(temp, (ar) => {
4539 Socket client = (Socket) ar.AsyncState;
4540 client.EndSendFile (ar);
4544 Assert.IsTrue (mSent.WaitOne (5000), "#1");
4545 Assert.IsTrue (mReceived.WaitOne (5000), "#2");
4547 if (File.Exists (temp))
4550 clientSocket.Close ();
4551 serverSocket.Close ();
4556 #if FEATURE_NO_BSD_SOCKETS
4557 [ExpectedException (typeof (PlatformNotSupportedException))]
4559 public void ConnectToIPV4EndPointUsingDualModelSocket () {
4561 * IPv6 DualMode sockets are defaults in Mono. Explicitly specify that
4562 * anyways in this test to make it more interoparable with .NET where
4563 * IPv6 and DualMode needs to be specified.
4565 using (var server = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp)) {
4567 var host = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
4569 server.DualMode = true;
4572 * Nothing to Accept the connect - we need a backlog to make sure we don't get
4577 var ep = server.LocalEndPoint as IPEndPoint;
4578 var client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
4579 client.DualMode = true;
4580 client.Connect (ep);
4581 client.Disconnect (false);
4584 client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
4585 client.DualMode = true;
4586 client.Connect (IPAddress.Loopback, ep.Port);
4587 client.Disconnect (false);
4590 client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
4591 client.DualMode = true;
4592 client.Connect (new [] { IPAddress.Loopback }, ep.Port);
4593 client.Disconnect (false);
4599 #if FEATURE_NO_BSD_SOCKETS
4600 [ExpectedException (typeof (PlatformNotSupportedException))]
4602 public void BeginConnectToIPV4EndPointUsingDualModelSocket () {
4604 * IPv6 DualMode sockets are defaults in Mono. Explicitly specify that
4605 * anyways in this test to make it more interoparable with .NET where
4606 * IPv6 and DualMode needs to be specified.
4608 using (var server = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
4610 var host = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
4612 server.DualMode = true;
4616 var ep = server.LocalEndPoint as IPEndPoint;
4618 BCCalledBack.Reset ();
4619 var client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
4620 client.DualMode = true;
4621 var ar1 = client.BeginConnect (ep, BCCallback, client);
4622 Assert.IsTrue (BCCalledBack.WaitOne (10000), "#1");
4623 client.Disconnect (false);
4626 BCCalledBack.Reset ();
4627 client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
4628 client.DualMode = true;
4629 var ar2 = client.BeginConnect (IPAddress.Loopback, ep.Port, BCCallback, client);
4630 Assert.IsTrue (BCCalledBack.WaitOne (10000), "#2");
4631 client.Disconnect (false);
4634 BCCalledBack.Reset ();
4635 client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
4636 client.DualMode = true;
4637 var ar3 = client.BeginConnect (new [] {IPAddress.Loopback}, ep.Port, BCCallback, client);
4638 Assert.IsTrue (BCCalledBack.WaitOne (10000), "#2");
4639 client.Disconnect (false);
4645 #if FEATURE_NO_BSD_SOCKETS
4646 [ExpectedException (typeof (PlatformNotSupportedException))]
4648 public void UdpMulticasTimeToLive ()
4650 /* see https://bugzilla.xamarin.com/show_bug.cgi?id=36941 */
4652 using (Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
4653 IPEndPoint end_point = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
4654 socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
4655 socket.Bind (end_point);
4656 socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 19);
4660 [Test] // Covers 41616
4661 public void ConnectAsyncUnhandledEx ()
4663 var mre = new ManualResetEvent (false);
4665 var endPoint = new IPEndPoint(0,0);
4666 var socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Unspecified);
4668 var socketArgs = new SocketAsyncEventArgs();
4669 socketArgs.RemoteEndPoint = endPoint;
4670 socketArgs.Completed += (sender, e) => mre.Set ();
4672 socket.ConnectAsync (socketArgs);
4674 Assert.IsTrue (mre.WaitOne (1000), "ConnectedAsync timeout");
4677 [Test] // Covers https://bugzilla.xamarin.com/show_bug.cgi?id=52549
4678 public void SocketMismatchProtocol ()
4681 using (Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Tcp));
4683 } catch (SocketException e) {
4685 // Assert.AreEqual(SocketError.ProtocolType, e.SocketErrorCode, "#2");
4689 using (Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Udp));
4691 } catch (SocketException e) {
4693 // Assert.AreEqual(SocketError.ProtocolType, e.SocketErrorCode, "#4");