2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System / Test / System.Net.Sockets / SocketTest.cs
1 // System.Net.Sockets.SocketTest.cs
2 //
3 // Authors:
4 //    Brad Fitzpatrick (brad@danga.com)
5 //    Gonzalo Paniagua Javier (gonzalo@novell.com)
6 //
7 // (C) Copyright 2003 Brad Fitzpatrick
8 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
9 //
10
11 using System;
12 using System.Collections;
13 using System.Threading;
14 using System.Net;
15 using System.Net.Sockets;
16 using NUnit.Framework;
17
18 #if NET_2_0
19 using System.Collections.Generic;
20 #endif
21
22 namespace MonoTests.System.Net.Sockets
23 {
24         [TestFixture]
25         public class SocketTest
26         {
27                 // note: also used in SocketCas tests
28                 public const string BogusAddress = "192.168.244.244";
29                 public const int BogusPort = 23483;
30
31                 [Test]
32                 public void ConnectIPAddressAny ()
33                 {
34                         IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
35
36                         try {
37                                 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
38                                         s.Connect (ep);
39                                         s.Close ();
40                                 }
41                                 Assert.Fail ("#1");
42                         } catch (SocketException ex) {
43                                 Assert.AreEqual (10049, ex.ErrorCode, "#2");
44                         }
45
46                         try {
47                                 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
48                                         s.Connect (ep);
49                                         s.Close ();
50                                 }
51                                 Assert.Fail ("#3");
52                         } catch (SocketException ex) {
53                                 Assert.AreEqual (10049, ex.ErrorCode, "#4");
54                         }
55                 }
56
57                 [Test]
58                 [Ignore ("Bug #75158")] // Looks like MS fails after the .ctor, when you try to use the socket
59                 public void IncompatibleAddress ()
60                 {
61                         IPEndPoint epIPv6 = new IPEndPoint (IPAddress.IPv6Any,
62                                                                 0);
63
64                         try {
65                                 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)) {
66                                         s.Connect (epIPv6);
67                                         s.Close ();
68                                 }
69                                 Assert.Fail ("#1");
70                         } catch (SocketException ex) {
71 #if !NET_2_0
72                                 // invalid argument
73                                 int expectedError = 10022;
74 #else
75                                 // address incompatible with protocol
76                                 int expectedError = 10047;
77 #endif
78                                 Assert.AreEqual (expectedError, ex.ErrorCode,
79                                                 "#2");
80                         }
81                 }
82
83                 [Test]
84                 [Category ("InetAccess")]
85                 public void EndConnect ()
86                 {
87                         IPAddress ipOne = IPAddress.Parse (BogusAddress);
88                         IPEndPoint ipEP = new IPEndPoint (ipOne, BogusPort);
89                         Socket sock = new Socket (ipEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
90                         IAsyncResult ar = sock.BeginConnect (ipEP, null, null);
91
92                         try {
93                                 // should raise an exception because connect was bogus
94                                 sock.EndConnect (ar);
95                                 Assert.Fail ("#1");
96                         } catch (SocketException ex) {
97                                 Assert.AreEqual (10060, ex.ErrorCode, "#2");
98                         }
99                 }
100
101                 [Test]
102                 [ExpectedException (typeof (ArgumentNullException))]
103                 public void SelectEmpty ()
104                 {
105                         ArrayList list = new ArrayList ();
106                         Socket.Select (list, list, list, 1000);
107                 }
108                 
109                 private bool BlockingConnect (bool block)
110                 {
111                         IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 1234);
112                         Socket server = new Socket(AddressFamily.InterNetwork,
113                                                    SocketType.Stream,
114                                                    ProtocolType.Tcp);
115                         server.Bind(ep);
116                         server.Blocking=block;
117
118                         server.Listen(0);
119
120                         Socket conn = new Socket (AddressFamily.InterNetwork,
121                                                   SocketType.Stream,
122                                                   ProtocolType.Tcp);
123                         conn.Connect (ep);
124
125                         Socket client = server.Accept();
126                         bool client_block = client.Blocking;
127
128                         client.Close();
129                         conn.Close();
130                         server.Close();
131                         
132                         return(client_block);
133                 }
134
135                 [Test]
136                 public void AcceptBlockingStatus()
137                 {
138                         bool block;
139
140                         block = BlockingConnect(true);
141                         Assert.AreEqual (block, true, "BlockingStatus01");
142
143                         block = BlockingConnect(false);
144                         Assert.AreEqual (block, false, "BlockingStatus02");
145                 }
146
147                 static bool CFAConnected = false;
148                 static ManualResetEvent CFACalledBack;
149                 
150                 private static void CFACallback (IAsyncResult asyncResult)
151                 {
152                         Socket sock = (Socket)asyncResult.AsyncState;
153                         CFAConnected = sock.Connected;
154                         
155                         if (sock.Connected) {
156                                 sock.EndConnect (asyncResult);
157                         }
158
159                         CFACalledBack.Set ();
160                 }
161
162                 [Test] // Connect (IPEndPoint)
163                 public void Connect1_RemoteEP_Null ()
164                 {
165                         Socket s = new Socket (AddressFamily.InterNetwork,
166                                 SocketType.Stream, ProtocolType.Tcp);
167                         try {
168                                 s.Connect ((IPEndPoint) null);
169                                 Assert.Fail ("#1");
170                         } catch (ArgumentNullException ex) {
171                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
172                                 Assert.IsNull (ex.InnerException, "#3");
173                                 Assert.IsNotNull (ex.Message, "#4");
174                                 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
175                         }
176                 }
177
178                 [Test]
179                 public void ConnectFailAsync ()
180                 {
181                         Socket sock = new Socket (AddressFamily.InterNetwork,
182                                                   SocketType.Stream,
183                                                   ProtocolType.Tcp);
184                         sock.Blocking = false;
185                         CFACalledBack = new ManualResetEvent (false);
186                         CFACalledBack.Reset ();
187
188                         /* Need a port that is not being used for
189                          * anything...
190                          */
191                         sock.BeginConnect (new IPEndPoint (IPAddress.Loopback,
192                                                            114),
193                                            new AsyncCallback (CFACallback),
194                                            sock);
195                         CFACalledBack.WaitOne ();
196
197                         Assert.AreEqual (CFAConnected, false, "ConnectFail");
198                 }
199                 
200 #if !TARGET_JVM
201                 [Test]
202 #if !NET_2_0
203                 [ExpectedException (typeof (ArgumentException))]
204 #endif
205                 public void SetSocketOptionBoolean ()
206                 {
207                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1);
208                         Socket sock = new Socket (ep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
209                         try {
210                                 sock.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
211                         } finally {
212                                 sock.Close ();
213                         }
214                 }
215 #endif
216                 [Test]
217                 public void TestSelect1 ()
218                 {
219                         Socket srv = CreateServer ();
220                         ClientSocket clnt = new ClientSocket (srv.LocalEndPoint);
221                         Thread th = new Thread (new ThreadStart (clnt.ConnectSleepClose));
222                         Socket acc = null;
223                         try {
224                                 th.Start ();
225                                 acc = srv.Accept ();
226                                 clnt.Write ();
227                                 ArrayList list = new ArrayList ();
228                                 ArrayList empty = new ArrayList ();
229                                 list.Add (acc);
230                                 Socket.Select (list, empty, empty, 100);
231                                 Assert.AreEqual (0, empty.Count, "#01");
232                                 Assert.AreEqual (1, list.Count, "#02");
233                                 Socket.Select (empty, list, empty, 100);
234                                 Assert.AreEqual (0, empty.Count, "#03");
235                                 Assert.AreEqual (1, list.Count, "#04");
236                                 Socket.Select (list, empty, empty, -1);
237                                 Assert.AreEqual (0, empty.Count, "#05");
238                                 Assert.AreEqual (1, list.Count, "#06");
239                                 // Need to read the 10 bytes from the client to avoid a RST
240                                 byte [] bytes = new byte [10];
241                                 acc.Receive (bytes);
242                         } finally {
243                                 if (acc != null)
244                                         acc.Close ();
245                                 srv.Close ();
246                         }
247                 }
248
249                 static Socket CreateServer ()
250                 {
251                         Socket sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
252                         sock.Bind (new IPEndPoint (IPAddress.Loopback, 0));
253                         sock.Listen (1);
254                         return sock;
255                 }
256
257                 class ClientSocket {
258                         Socket sock;
259                         EndPoint ep;
260
261                         public ClientSocket (EndPoint ep)
262                         {
263                                 this.ep = ep;
264                                 sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
265                         }
266
267                         public void ConnectSleepClose ()
268                         {
269                                 sock.Connect (ep);
270                                 Thread.Sleep (2000);
271                                 sock.Close ();
272                         }
273
274                         public void Write ()
275                         {
276                                 byte [] b = new byte [10];
277                                 sock.Send (b);
278                         }
279                 }
280
281                 byte[] buf = new byte[100];
282
283                 [Test]
284                 [ExpectedException (typeof (ObjectDisposedException))]
285                 public void Disposed2 ()
286                 {
287                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
288                         s.Close();
289
290                         s.Blocking = true;
291                 }
292
293                 [Test]
294                 [ExpectedException (typeof (ObjectDisposedException))]
295                 public void Disposed6 ()
296                 {
297                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
298                         s.Close();
299
300                         s.Listen (5);
301                 }
302
303                 [Test]
304                 [ExpectedException (typeof (ObjectDisposedException))]
305                 public void Disposed7 ()
306                 {
307                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
308                         s.Close();
309
310                         s.Poll (100, 0);
311                 }
312
313                 [Test]
314                 [ExpectedException (typeof (ObjectDisposedException))]
315                 public void Disposed15 ()
316                 {
317                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
318                         s.Close();
319
320                         s.Send (buf);
321                 }
322
323                 [Test]
324                 [ExpectedException (typeof (ObjectDisposedException))]
325                 public void Disposed16 ()
326                 {
327                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
328                         s.Close();
329
330                         s.Send (buf, 0);
331                 }
332
333                 [Test]
334                 [ExpectedException (typeof (ObjectDisposedException))]
335                 public void Disposed17 ()
336                 {
337                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
338                         s.Close();
339
340                         s.Send (buf, 10, 0);
341                 }
342
343                 [Test]
344                 [ExpectedException (typeof (ObjectDisposedException))]
345                 public void Disposed18 ()
346                 {
347                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
348                         s.Close();
349
350                         s.Send (buf, 0, 10, 0);
351                 }
352
353                 [Test]
354                 [ExpectedException (typeof (ObjectDisposedException))]
355                 public void Disposed19 ()
356                 {
357                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
358                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
359                         s.Close();
360
361                         s.SendTo (buf, 0, ep);
362                 }
363
364                 [Test]
365                 [ExpectedException (typeof (ObjectDisposedException))]
366                 public void Disposed20 ()
367                 {
368                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
369                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
370                         s.Close();
371
372                         s.SendTo (buf, 10, 0, ep);
373                 }
374
375                 [Test]
376                 [ExpectedException (typeof (ObjectDisposedException))]
377                 public void Disposed21 ()
378                 {
379                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
380                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
381                         s.Close();
382
383                         s.SendTo (buf, 0, 10, 0, ep);
384                 }
385
386                 [Test]
387                 [ExpectedException (typeof (ObjectDisposedException))]
388                 public void Disposed22 ()
389                 {
390                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
391                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
392                         s.Close();
393
394                         s.SendTo (buf, ep);
395                 }
396
397                 [Test]
398                 [ExpectedException (typeof (ObjectDisposedException))]
399                 public void Disposed23 ()
400                 {
401                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
402                         s.Close();
403
404                         s.Shutdown (0);
405                 }
406
407                 [Test]
408                 public void GetHashCodeTest ()
409                 {
410                         Socket server = new Socket (AddressFamily.InterNetwork,
411                                 SocketType.Stream, ProtocolType.Tcp);
412                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
413                                                         9010);
414                         server.Bind (ep);
415                         server.Listen (1);
416
417                         Socket client = new Socket (AddressFamily.InterNetwork, 
418                                 SocketType.Stream, ProtocolType.Tcp);
419                         int hashcodeA = client.GetHashCode ();
420                         client.Connect (ep);
421                         int hashcodeB = client.GetHashCode ();
422                         Assert.AreEqual (hashcodeA, hashcodeB, "#1");
423                         client.Close ();
424                         int hashcodeC = client.GetHashCode ();
425 #if NET_2_0
426                         Assert.AreEqual (hashcodeB, hashcodeC, "#2");
427 #else
428                         Assert.IsFalse (hashcodeB == hashcodeC, "#2");
429 #endif
430                         server.Close ();
431                 }
432
433                 static ManualResetEvent SocketError_event = new ManualResetEvent (false);
434
435                 private static void SocketError_callback (IAsyncResult ar)
436                 {
437                         Socket sock = (Socket)ar.AsyncState;
438                         
439                         if(sock.Connected) {
440                                 sock.EndConnect (ar);
441                         }
442
443                         SocketError_event.Set ();
444                 }
445
446                 [Test]
447                 public void SocketErrorTest ()
448                 {
449                         Socket sock = new Socket (AddressFamily.InterNetwork,
450                                                   SocketType.Stream,
451                                                   ProtocolType.Tcp);
452                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
453                                                         BogusPort);
454                         
455                         SocketError_event.Reset ();
456
457                         sock.Blocking = false;
458                         sock.BeginConnect (ep, new AsyncCallback(SocketError_callback),
459                                 sock);
460
461                         if (SocketError_event.WaitOne (2000, false) == false) {
462                                 Assert.Fail ("SocketError wait timed out");
463                         }
464
465                         Assert.AreEqual (false, sock.Connected, "SocketError #1");
466
467                         int error;
468
469                         error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
470                         Assert.AreEqual (10061, error, "SocketError #2");
471
472                         error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
473                         Assert.AreEqual (10061, error, "SocketError #3");
474
475                         sock.Close ();
476                 }
477                 
478
479 #if NET_2_0
480                 [Test]
481                 public void SocketInformationCtor ()
482                 {
483                 }
484                 
485                 [Test]
486                 public void DontFragmentDefaultTcp ()
487                 {
488                         Socket sock = new Socket (AddressFamily.InterNetwork,
489                                                   SocketType.Stream,
490                                                   ProtocolType.Tcp);
491                         
492                         Assert.AreEqual (false, sock.DontFragment, "DontFragmentDefaultTcp");
493
494                         sock.Close ();
495                 }
496
497                 [Test]
498                 [Category ("NotOnMac")] // DontFragment doesn't work on Mac
499                 public void DontFragmentChangeTcp ()
500                 {
501                         Socket sock = new Socket (AddressFamily.InterNetwork,
502                                                   SocketType.Stream,
503                                                   ProtocolType.Tcp);
504                         
505                         sock.DontFragment = true;
506                         
507                         Assert.AreEqual (true, sock.DontFragment, "DontFragmentChangeTcp");
508
509                         sock.Close ();
510                 }
511                 
512                 [Test]
513                 public void DontFragmentDefaultUdp ()
514                 {
515                         Socket sock = new Socket (AddressFamily.InterNetwork,
516                                                   SocketType.Dgram,
517                                                   ProtocolType.Udp);
518                         
519                         Assert.AreEqual (false, sock.DontFragment, "DontFragmentDefaultUdp");
520
521                         sock.Close ();
522                 }
523
524                 [Test]
525                 [Category ("NotOnMac")] // DontFragment doesn't work on Mac
526                 public void DontFragmentChangeUdp ()
527                 {
528                         Socket sock = new Socket (AddressFamily.InterNetwork,
529                                                   SocketType.Dgram,
530                                                   ProtocolType.Udp);
531                         
532                         sock.DontFragment = true;
533                         
534                         Assert.AreEqual (true, sock.DontFragment, "DontFragmentChangeUdp");
535
536                         sock.Close ();
537                 }
538
539                 [Test]
540                 [ExpectedException (typeof(ObjectDisposedException))]
541                 public void DontFragmentClosed ()
542                 {
543                         Socket sock = new Socket (AddressFamily.InterNetwork,
544                                                   SocketType.Stream,
545                                                   ProtocolType.Tcp);
546                         
547                         sock.Close ();
548                         
549                         bool val = sock.DontFragment;
550                 }
551                 
552                 [Test]
553                 [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
554                 public void DontFragment ()
555                 {
556                         Socket sock = new Socket (AddressFamily.NetBios,
557                                                   SocketType.Seqpacket,
558                                                   ProtocolType.Unspecified);
559                         
560                         try {
561                                 sock.DontFragment = true;
562                                 Assert.Fail ("DontFragment #1");
563                         } catch (NotSupportedException) {
564                         } catch {
565                                 Assert.Fail ("DontFragment #2");
566                         } finally {
567                                 sock.Close ();
568                         }
569                 }
570                 
571                 [Test]
572                 public void EnableBroadcastDefaultTcp ()
573                 {
574                         Socket sock = new Socket (AddressFamily.InterNetwork,
575                                                   SocketType.Stream,
576                                                   ProtocolType.Tcp);
577                         
578                         try {
579                                 bool value = sock.EnableBroadcast;
580                                 Assert.Fail ("EnableBroadcastDefaultTcp #1");
581                         } catch (SocketException ex) {
582                                 Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastDefaultTcp #2");
583                         } catch {
584                                 Assert.Fail ("EnableBroadcastDefaultTcp #2");
585                         } finally {
586                                 sock.Close ();
587                         }
588                 }
589
590                 [Test]
591                 public void EnableBroadcastDefaultUdp ()
592                 {
593                         Socket sock = new Socket (AddressFamily.InterNetwork,
594                                                   SocketType.Dgram,
595                                                   ProtocolType.Udp);
596                         
597                         Assert.AreEqual (false, sock.EnableBroadcast, "EnableBroadcastDefaultUdp");
598
599                         sock.Close ();
600                 }
601                 
602                 [Test]
603                 public void EnableBroadcastChangeTcp ()
604                 {
605                         Socket sock = new Socket (AddressFamily.InterNetwork,
606                                                   SocketType.Stream,
607                                                   ProtocolType.Tcp);
608                         
609                         try {
610                                 sock.EnableBroadcast = true;
611                                 Assert.Fail ("EnableBroadcastChangeTcp #1");
612                         } catch (SocketException ex) {
613                                 Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastChangeTcp #2");
614                         } catch {
615                                 Assert.Fail ("EnableBroadcastChangeTcp #2");
616                         } finally {
617                                 sock.Close ();
618                         }
619                 }
620                 
621                 [Test]
622                 public void EnableBroadcastChangeUdp ()
623                 {
624                         Socket sock = new Socket (AddressFamily.InterNetwork,
625                                                   SocketType.Dgram,
626                                                   ProtocolType.Udp);
627                         
628                         sock.EnableBroadcast = true;
629                         
630                         Assert.AreEqual (true, sock.EnableBroadcast, "EnableBroadcastChangeUdp");
631
632                         sock.Close ();
633                 }
634
635                 [Test]
636                 [ExpectedException (typeof(ObjectDisposedException))]
637                 public void EnableBroadcastClosed ()
638                 {
639                         Socket sock = new Socket (AddressFamily.InterNetwork,
640                                                   SocketType.Dgram,
641                                                   ProtocolType.Udp);
642                         
643                         sock.Close ();
644                         
645                         bool val = sock.EnableBroadcast;
646                 }
647
648                 /* Can't test the default for ExclusiveAddressUse as
649                  * it's different on different versions and service
650                  * packs of windows
651                  */
652                 [Test]
653                 [Category ("NotWorking")] // Not supported on Linux
654                 public void ExclusiveAddressUseUnbound ()
655                 {
656                         Socket sock = new Socket (AddressFamily.InterNetwork,
657                                                   SocketType.Stream,
658                                                   ProtocolType.Tcp);
659                         
660                         sock.ExclusiveAddressUse = true;
661                         
662                         Assert.AreEqual (true, sock.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
663                         
664                         sock.Close ();
665                 }
666
667                 [Test]
668                 [ExpectedException (typeof(InvalidOperationException))]
669                 [Category ("NotWorking")] // Not supported on Linux
670                 public void ExclusiveAddressUseBound ()
671                 {
672                         Socket sock = new Socket (AddressFamily.InterNetwork,
673                                                   SocketType.Stream,
674                                                   ProtocolType.Tcp);
675                         
676                         sock.Bind (new IPEndPoint (IPAddress.Any, 1235));
677                         sock.ExclusiveAddressUse = true;
678                         sock.Close ();
679                 }
680
681                 [Test]
682                 [ExpectedException (typeof(ObjectDisposedException))]
683                 public void ExclusiveAddressUseClosed ()
684                 {
685                         Socket sock = new Socket (AddressFamily.InterNetwork,
686                                                   SocketType.Stream,
687                                                   ProtocolType.Tcp);
688                         
689                         sock.Close ();
690                         
691                         bool val = sock.ExclusiveAddressUse;
692                 }
693                 
694                 [Test]
695                 public void IsBoundTcp ()
696                 {
697                         Socket sock = new Socket (AddressFamily.InterNetwork,
698                                                   SocketType.Stream,
699                                                   ProtocolType.Tcp);
700                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
701                                                         BogusPort);
702                         
703                         Assert.AreEqual (false, sock.IsBound, "IsBoundTcp #1");
704                         
705                         sock.Bind (ep);
706                         Assert.AreEqual (true, sock.IsBound, "IsBoundTcp #2");
707
708                         sock.Listen (1);
709                         
710                         Socket sock2 = new Socket (AddressFamily.InterNetwork,
711                                                    SocketType.Stream,
712                                                    ProtocolType.Tcp);
713                         
714                         Assert.AreEqual (false, sock2.IsBound, "IsBoundTcp #3");
715                         
716                         sock2.Connect (ep);
717                         Assert.AreEqual (true, sock2.IsBound, "IsBoundTcp #4");
718                         
719                         sock2.Close ();
720                         Assert.AreEqual (true, sock2.IsBound, "IsBoundTcp #5");
721
722                         sock.Close ();
723                         Assert.AreEqual (true, sock.IsBound, "IsBoundTcp #6");
724                 }
725
726                 [Test]
727                 public void IsBoundUdp ()
728                 {
729                         Socket sock = new Socket (AddressFamily.InterNetwork,
730                                                   SocketType.Dgram,
731                                                   ProtocolType.Udp);
732                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
733                                                         BogusPort);
734                         
735                         Assert.AreEqual (false, sock.IsBound, "IsBoundUdp #1");
736                         
737                         sock.Bind (ep);
738                         Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #2");
739                         
740                         sock.Close ();
741                         Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #3");
742                         
743
744                         sock = new Socket (AddressFamily.InterNetwork,
745                                            SocketType.Dgram,
746                                            ProtocolType.Udp);
747                         
748                         Assert.AreEqual (false, sock.IsBound, "IsBoundUdp #4");
749                         
750                         sock.Connect (ep);
751                         Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #5");
752                         
753                         sock.Close ();
754                         Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #6");
755                 }
756
757                 [Test]
758                 /* Should not throw an exception */
759                 public void IsBoundClosed ()
760                 {
761                         Socket sock = new Socket (AddressFamily.InterNetwork,
762                                                   SocketType.Stream,
763                                                   ProtocolType.Tcp);
764                         
765                         sock.Close ();
766                         
767                         bool val = sock.IsBound;
768                 }
769                 
770                 /* Nothing much to test for LingerState */
771                 
772                 [Test]
773                 public void MulticastLoopbackDefaultTcp ()
774                 {
775                         Socket sock = new Socket (AddressFamily.InterNetwork,
776                                                   SocketType.Stream,
777                                                   ProtocolType.Tcp);
778                         
779                         try {
780                                 bool value = sock.MulticastLoopback;
781                                 Assert.Fail ("MulticastLoopbackDefaultTcp #1");
782                         } catch (SocketException ex) {
783                                 Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackDefaultTcp #2");
784                         } catch {
785                                 Assert.Fail ("MulticastLoopbackDefaultTcp #2");
786                         } finally {
787                                 sock.Close ();
788                         }
789                 }
790
791                 [Test]
792                 public void MulticastLoopbackChangeTcp ()
793                 {
794                         Socket sock = new Socket (AddressFamily.InterNetwork,
795                                                   SocketType.Stream,
796                                                   ProtocolType.Tcp);
797                         
798                         try {
799                                 sock.MulticastLoopback = false;
800                                 Assert.Fail ("MulticastLoopbackChangeTcp #1");
801                         } catch (SocketException ex) {
802                                 Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackChangeTcp #2");
803                         } catch {
804                                 Assert.Fail ("MulticastLoopbackChangeTcp #2");
805                         } finally {
806                                 sock.Close ();
807                         }
808                 }
809                 
810                 [Test]
811                 public void MulticastLoopbackDefaultUdp ()
812                 {
813                         Socket sock = new Socket (AddressFamily.InterNetwork,
814                                                   SocketType.Dgram,
815                                                   ProtocolType.Udp);
816                         
817                         Assert.AreEqual (true, sock.MulticastLoopback, "MulticastLoopbackDefaultUdp");
818                         
819                         sock.Close ();
820                 }
821                 
822                 [Test]
823                 public void MulticastLoopbackChangeUdp ()
824                 {
825                         Socket sock = new Socket (AddressFamily.InterNetwork,
826                                                   SocketType.Dgram,
827                                                   ProtocolType.Udp);
828                         
829                         sock.MulticastLoopback = false;
830                         
831                         Assert.AreEqual (false, sock.MulticastLoopback, "MulticastLoopbackChangeUdp");
832                         
833                         sock.Close ();
834                 }
835
836                 [Test]
837                 [ExpectedException (typeof(ObjectDisposedException))]
838                 public void MulticastLoopbackClosed ()
839                 {
840                         Socket sock = new Socket (AddressFamily.InterNetwork,
841                                                   SocketType.Stream,
842                                                   ProtocolType.Tcp);
843                         
844                         sock.Close ();
845                         
846                         bool val = sock.MulticastLoopback;
847                 }
848                 
849                 /* OSSupportsIPv6 depends on the environment */
850                 
851                 [Test]
852                 [Category("NotWorking")] // We have different defaults for perf reasons
853                 public void ReceiveBufferSizeDefault ()
854                 {
855                         Socket sock = new Socket (AddressFamily.InterNetwork,
856                                                   SocketType.Stream,
857                                                   ProtocolType.Tcp);
858                         
859                         Assert.AreEqual (8192, sock.ReceiveBufferSize, "ReceiveBufferSizeDefault");
860                         
861                         sock.Close ();
862                 }
863                 
864                 [Test]
865                 [Category("NotWorking")] // We have different defaults for perf reasons
866                 public void ReceiveBufferSizeDefaultUdp ()
867                 {
868                         Socket sock = new Socket (AddressFamily.InterNetwork,
869                                                   SocketType.Dgram,
870                                                   ProtocolType.Udp);
871                         
872                         Assert.AreEqual (8192, sock.ReceiveBufferSize, "ReceiveBufferSizeDefaultUdp");
873                         
874                         sock.Close ();
875                 }
876
877                 [Test]
878                 public void ReceiveBufferSizeChange ()
879                 {
880                         Socket sock = new Socket (AddressFamily.InterNetwork,
881                                                   SocketType.Stream,
882                                                   ProtocolType.Tcp);
883                         
884                         sock.ReceiveBufferSize = 16384;
885                         
886                         Assert.AreEqual (16384, sock.ReceiveBufferSize, "ReceiveBufferSizeChange");
887                         
888                         sock.Close ();
889                 }
890
891                 [Test]
892                 [ExpectedException (typeof(ObjectDisposedException))]
893                 public void ReceiveBufferSizeClosed ()
894                 {
895                         Socket sock = new Socket (AddressFamily.InterNetwork,
896                                                   SocketType.Stream,
897                                                   ProtocolType.Tcp);
898                         
899                         sock.Close ();
900                         
901                         int val = sock.ReceiveBufferSize;
902                 }
903                 
904                 [Test]
905                 [Category("NotWorking")] // We have different defaults for perf reasons
906                 public void SendBufferSizeDefault ()
907                 {
908                         Socket sock = new Socket (AddressFamily.InterNetwork,
909                                                   SocketType.Stream,
910                                                   ProtocolType.Tcp);
911                         
912                         Assert.AreEqual (8192, sock.SendBufferSize, "SendBufferSizeDefault");
913                         
914                         sock.Close ();
915                 }
916                 
917                 [Test]
918                 [Category("NotWorking")] // We have different defaults for perf reasons
919                 public void SendBufferSizeDefaultUdp ()
920                 {
921                         Socket sock = new Socket (AddressFamily.InterNetwork,
922                                                   SocketType.Dgram,
923                                                   ProtocolType.Udp);
924                         
925                         Assert.AreEqual (8192, sock.SendBufferSize, "SendBufferSizeDefaultUdp");
926                         
927                         sock.Close ();
928                 }
929
930                 [Test]
931                 public void SendBufferSizeChange ()
932                 {
933                         Socket sock = new Socket (AddressFamily.InterNetwork,
934                                                   SocketType.Stream,
935                                                   ProtocolType.Tcp);
936                         
937                         sock.SendBufferSize = 16384;
938                         
939                         Assert.AreEqual (16384, sock.SendBufferSize, "SendBufferSizeChange");
940                         
941                         sock.Close ();
942                 }
943
944                 [Test]
945                 [ExpectedException (typeof(ObjectDisposedException))]
946                 public void SendBufferSizeClosed ()
947                 {
948                         Socket sock = new Socket (AddressFamily.InterNetwork,
949                                                   SocketType.Stream,
950                                                   ProtocolType.Tcp);
951                         
952                         sock.Close ();
953                         
954                         int val = sock.SendBufferSize;
955                 }
956                 
957                 /* No test for TTL default as it's platform dependent */
958                 [Test]
959                 public void TtlChange ()
960                 {
961                         Socket sock = new Socket (AddressFamily.InterNetwork,
962                                                   SocketType.Stream,
963                                                   ProtocolType.Tcp);
964                         
965                         sock.Ttl = 255;
966                         
967                         Assert.AreEqual (255, sock.Ttl, "TtlChange");
968                         
969                         sock.Close ();
970                 }
971
972                 [Test]
973                 [Category ("NotOnMac")] // Mac doesn't throw when overflowing the ttl
974                 public void TtlChangeOverflow ()
975                 {
976                         Socket sock = new Socket (AddressFamily.InterNetwork,
977                                                   SocketType.Stream,
978                                                   ProtocolType.Tcp);
979                         
980                         try {
981                                 sock.Ttl = 256;
982                                 Assert.Fail ("TtlChangeOverflow #1");
983                         } catch (SocketException ex) {
984                                 Assert.AreEqual (10022, ex.ErrorCode,
985                                                  "TtlChangeOverflow #2");
986                         } catch {
987                                 Assert.Fail ("TtlChangeoverflow #3");
988                         } finally {
989                                 sock.Close ();
990                         }
991                 }
992                 
993 /* Apparently you can set TTL=0 on the ms runtime!!
994                         try {
995                                 sock.Ttl = 0;
996                                 Assert.Fail ("TtlChangeOverflow #4");
997                         } catch (SocketException ex) {
998                                 Assert.AreEqual (10022, ex.ErrorCode,
999                                                  "TtlChangeOverflow #5");
1000                         } catch {
1001                                 Assert.Fail ("TtlChangeOverflow #6");
1002                         } finally {
1003                                 sock.Close ();
1004                         }
1005 */
1006
1007                 [Test]
1008                 [ExpectedException (typeof(ObjectDisposedException))]
1009                 public void TtlClosed ()
1010                 {
1011                         Socket sock = new Socket (AddressFamily.InterNetwork,
1012                                                   SocketType.Stream,
1013                                                   ProtocolType.Tcp);
1014                         
1015                         sock.Close ();
1016                         
1017                         int val = sock.Ttl;
1018                 }
1019                 
1020                 [Test]
1021                 public void UseOnlyOverlappedIODefault ()
1022                 {
1023                         Socket sock = new Socket (AddressFamily.InterNetwork,
1024                                                   SocketType.Stream,
1025                                                   ProtocolType.Tcp);
1026                         
1027                         Assert.AreEqual (false, sock.UseOnlyOverlappedIO, "UseOnlyOverlappedIODefault");
1028                         
1029                         sock.Close ();
1030                 }
1031
1032                 //
1033                 // We need this because the Linux kernel in certain configurations
1034                 // will end up rounding up the values passed on to the kernel
1035                 // for socket send/receive timeouts.
1036                 //
1037                 int Approximate (int target, int value)
1038                 {
1039                         int epsilon = 10;
1040                         
1041                         if (value > target-10 && value < target+10)
1042                                 return target;
1043                         return value;
1044                 }
1045                 
1046                 [Test]
1047                 public void UseOnlyOverlappedIOChange ()
1048                 {
1049                         Socket sock = new Socket (AddressFamily.InterNetwork,
1050                                                   SocketType.Stream,
1051                                                   ProtocolType.Tcp);
1052                         
1053                         sock.UseOnlyOverlappedIO = true;
1054                         
1055                         Assert.AreEqual (true, sock.UseOnlyOverlappedIO, "UseOnlyOverlappedIOChange");
1056                         
1057                         sock.Close ();
1058                 }
1059
1060                 [Test]
1061                 /* Should not throw an exception */
1062                 public void UseOnlyOverlappedIOClosed ()
1063                 {
1064                         Socket sock = new Socket (AddressFamily.InterNetwork,
1065                                                   SocketType.Stream,
1066                                                   ProtocolType.Tcp);
1067                         
1068                         sock.Close ();
1069                         
1070                         bool val = sock.UseOnlyOverlappedIO;
1071                 }
1072                 
1073                 [Test]
1074                 public void SendTimeoutDefault ()
1075                 {
1076                         Socket sock = new Socket (AddressFamily.InterNetwork,
1077                                                   SocketType.Stream,
1078                                                   ProtocolType.Tcp);
1079                         
1080                         Assert.AreEqual (0, sock.SendTimeout, "SendTimeoutDefault");
1081                         
1082                         sock.Close ();
1083                 }
1084
1085                 [Test]
1086                 public void SendTimeoutChange ()
1087                 {
1088                         Socket sock = new Socket (AddressFamily.InterNetwork,
1089                                                   SocketType.Stream,
1090                                                   ProtocolType.Tcp);
1091                         
1092                         /* Should be rounded up to 500, according to
1093                          * the MSDN docs, but the MS runtime doesn't
1094                          */
1095                         sock.SendTimeout = 50;
1096                         Assert.AreEqual (50, Approximate (50, sock.SendTimeout), "SendTimeoutChange #1");
1097                         
1098                         sock.SendTimeout = 2000;
1099                         Assert.AreEqual (2000, Approximate (2000, sock.SendTimeout), "SendTimeoutChange #2");
1100                         
1101                         sock.SendTimeout = 0;
1102                         Assert.AreEqual (0, Approximate (0, sock.SendTimeout), "SendTimeoutChange #3");
1103                         
1104                         /* Should be the same as setting 0 */
1105                         sock.SendTimeout = -1;
1106                         Assert.AreEqual (0, sock.SendTimeout, "SendTimeoutChange #4");
1107
1108                         sock.SendTimeout = 65536;
1109                         Assert.AreEqual (65536, Approximate (65536, sock.SendTimeout), "SendTimeoutChange #5");
1110                         
1111                         try {
1112                                 sock.SendTimeout = -2;
1113                                 Assert.Fail ("SendTimeoutChange #8");
1114                         } catch (ArgumentOutOfRangeException) {
1115                         } catch {
1116                                 Assert.Fail ("SendTimeoutChange #9");
1117                         } finally {
1118                                 sock.Close ();
1119                         }
1120                 }
1121
1122                 [Test]
1123                 [ExpectedException (typeof(ObjectDisposedException))]
1124                 public void SendTimeoutClosed ()
1125                 {
1126                         Socket sock = new Socket (AddressFamily.InterNetwork,
1127                                                   SocketType.Stream,
1128                                                   ProtocolType.Tcp);
1129                         
1130                         sock.Close ();
1131                         
1132                         int val = sock.SendTimeout;
1133                 }
1134                 
1135                 [Test]
1136                 public void ReceiveTimeoutDefault ()
1137                 {
1138                         Socket sock = new Socket (AddressFamily.InterNetwork,
1139                                                   SocketType.Stream,
1140                                                   ProtocolType.Tcp);
1141                         
1142                         Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutDefault");
1143                         
1144                         sock.Close ();
1145                 }
1146
1147                 [Test]
1148                 public void ReceiveTimeoutChange ()
1149                 {
1150                         Socket sock = new Socket (AddressFamily.InterNetwork,
1151                                                   SocketType.Stream,
1152                                                   ProtocolType.Tcp);
1153                         
1154                         sock.ReceiveTimeout = 50;
1155                         Assert.AreEqual (50, Approximate (50, sock.ReceiveTimeout), "ReceiveTimeoutChange #1");
1156                         
1157                         sock.ReceiveTimeout = 2000;
1158                         Assert.AreEqual (2000, Approximate (2000, sock.ReceiveTimeout), "ReceiveTimeoutChange #2");
1159                         
1160                         sock.ReceiveTimeout = 0;
1161                         Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutChange #3");
1162                         
1163                         /* Should be the same as setting 0 */
1164                         sock.ReceiveTimeout = -1;
1165                         Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutChange #4");
1166
1167                         sock.ReceiveTimeout = 65536;
1168                         Assert.AreEqual (65536, Approximate (65536, sock.ReceiveTimeout), "ReceiveTimeoutChange #5");
1169                         
1170                         try {
1171                                 sock.ReceiveTimeout = -2;
1172                                 Assert.Fail ("ReceiveTimeoutChange #8");
1173                         } catch (ArgumentOutOfRangeException) {
1174                         } catch {
1175                                 Assert.Fail ("ReceiveTimeoutChange #9");
1176                         } finally {
1177                                 sock.Close ();
1178                         }
1179                 }
1180
1181                 [Test]
1182                 [ExpectedException (typeof(ObjectDisposedException))]
1183                 public void ReceiveTimeoutClosed ()
1184                 {
1185                         Socket sock = new Socket (AddressFamily.InterNetwork,
1186                                                   SocketType.Stream,
1187                                                   ProtocolType.Tcp);
1188                         
1189                         sock.Close ();
1190                         
1191                         int val = sock.ReceiveTimeout;
1192                 }
1193                 
1194                 [Test]
1195                 public void NoDelayDefaultTcp ()
1196                 {
1197                         Socket sock = new Socket (AddressFamily.InterNetwork,
1198                                                   SocketType.Stream,
1199                                                   ProtocolType.Tcp);
1200                         
1201                         Assert.AreEqual (false, sock.NoDelay, "NoDelayDefaultTcp");
1202                         
1203                         sock.Close ();
1204                 }
1205
1206                 [Test]
1207                 public void NoDelayChangeTcp ()
1208                 {
1209                         Socket sock = new Socket (AddressFamily.InterNetwork,
1210                                                   SocketType.Stream,
1211                                                   ProtocolType.Tcp);
1212                         
1213                         sock.NoDelay = true;
1214                         
1215                         Assert.AreEqual (true, sock.NoDelay, "NoDelayChangeTcp");
1216                         
1217                         sock.Close ();
1218                 }
1219                 
1220                 [Test]
1221                 public void NoDelayDefaultUdp ()
1222                 {
1223                         Socket sock = new Socket (AddressFamily.InterNetwork,
1224                                                   SocketType.Dgram,
1225                                                   ProtocolType.Udp);
1226                         
1227                         try {
1228                                 bool val = sock.NoDelay;
1229                                 Assert.Fail ("NoDelayDefaultUdp #1");
1230                         } catch (SocketException ex) {
1231                                 Assert.AreEqual (10042, ex.ErrorCode,
1232                                                  "NoDelayDefaultUdp #2");
1233                         } catch {
1234                                 Assert.Fail ("NoDelayDefaultUdp #3");
1235                         } finally {
1236                                 sock.Close ();
1237                         }
1238                 }
1239
1240                 [Test]
1241                 public void NoDelayChangeUdp ()
1242                 {
1243                         Socket sock = new Socket (AddressFamily.InterNetwork,
1244                                                   SocketType.Dgram,
1245                                                   ProtocolType.Udp);
1246                         
1247                         try {
1248                                 sock.NoDelay = true;
1249                                 Assert.Fail ("NoDelayChangeUdp #1");
1250                         } catch (SocketException ex) {
1251                                 Assert.AreEqual (10042, ex.ErrorCode,
1252                                                  "NoDelayChangeUdp #2");
1253                         } catch {
1254                                 Assert.Fail ("NoDelayChangeUdp #3");
1255                         } finally {
1256                                 sock.Close ();
1257                         }
1258                 }
1259                 
1260                 [Test]
1261                 [ExpectedException (typeof(ObjectDisposedException))]
1262                 public void NoDelayClosed ()
1263                 {
1264                         Socket sock = new Socket (AddressFamily.InterNetwork,
1265                                                   SocketType.Stream,
1266                                                   ProtocolType.Tcp);
1267                         
1268                         sock.Close ();
1269                         
1270                         bool val = sock.NoDelay;
1271                 }
1272
1273                 static bool BAAccepted = false;
1274                 static Socket BASocket = null;
1275                 static ManualResetEvent BACalledBack = new ManualResetEvent (false);
1276                 
1277                 private static void BACallback (IAsyncResult asyncResult)
1278                 {
1279                         Socket sock = (Socket)asyncResult.AsyncState;
1280                         
1281                         BASocket = sock.EndAccept (asyncResult);
1282                         
1283                         BAAccepted = true;
1284                         BACalledBack.Set ();
1285                 }
1286                 
1287                 [Test]
1288                 [ExpectedException (typeof(InvalidOperationException))]
1289                 public void BeginAcceptNotBound ()
1290                 {
1291                         Socket sock = new Socket (AddressFamily.InterNetwork,
1292                                                   SocketType.Stream,
1293                                                   ProtocolType.Tcp);
1294
1295                         sock.BeginAccept (BACallback, sock);
1296                         
1297                         sock.Close ();
1298                 }
1299                 
1300                 [Test]
1301                 [ExpectedException (typeof(InvalidOperationException))]
1302                 public void BeginAcceptNotListening ()
1303                 {
1304                         Socket sock = new Socket (AddressFamily.InterNetwork,
1305                                                   SocketType.Stream,
1306                                                   ProtocolType.Tcp);
1307
1308                         sock.Bind (new IPEndPoint (IPAddress.Any, 1236));
1309                         
1310                         sock.BeginAccept (BACallback, sock);
1311                         
1312                         sock.Close ();
1313                 }
1314
1315                 [Test]
1316                 public void BeginAccept ()
1317                 {
1318                         Socket sock = new Socket (AddressFamily.InterNetwork,
1319                                                   SocketType.Stream,
1320                                                   ProtocolType.Tcp);
1321                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1322                                                         1237);
1323                         
1324                         sock.Bind (ep);
1325                         sock.Listen (1);
1326                         
1327                         BACalledBack.Reset ();
1328                         
1329                         sock.BeginAccept (BACallback, sock);
1330
1331                         Socket conn = new Socket (AddressFamily.InterNetwork,
1332                                                   SocketType.Stream,
1333                                                   ProtocolType.Tcp);
1334                         
1335                         conn.Connect (ep);
1336
1337                         if (BACalledBack.WaitOne (2000, false) == false) {
1338                                 Assert.Fail ("BeginAccept wait timed out");
1339                         }
1340                         
1341                         Assert.AreEqual (true, BAAccepted, "BeginAccept #1");
1342                         Assert.AreEqual (true, BASocket.Connected, "BeginAccept #2");
1343                         Assert.AreEqual (false, sock.Connected, "BeginAccept #3");
1344                         Assert.AreEqual (true, conn.Connected, "BeginAccept #4");
1345
1346                         BASocket.Close ();
1347                         conn.Close ();
1348                         sock.Close ();
1349                 }
1350
1351                 [Test]
1352                 [ExpectedException (typeof(ObjectDisposedException))]
1353                 public void BeginAcceptClosed ()
1354                 {
1355                         Socket sock = new Socket (AddressFamily.InterNetwork,
1356                                                   SocketType.Stream,
1357                                                   ProtocolType.Tcp);
1358                         
1359                         sock.Close ();
1360                         
1361                         sock.BeginAccept (BACallback, sock);
1362                 }
1363
1364                 static bool BADAccepted = false;
1365                 static Socket BADSocket = null;
1366                 static byte[] BADBytes;
1367                 static int BADByteCount;
1368                 static ManualResetEvent BADCalledBack = new ManualResetEvent (false);
1369                 
1370                 private static void BADCallback (IAsyncResult asyncResult)
1371                 {
1372                         Socket sock = (Socket)asyncResult.AsyncState;
1373                         
1374                         BADSocket = sock.EndAccept (out BADBytes,
1375                                                     out BADByteCount,
1376                                                     asyncResult);
1377                         
1378                         BADAccepted = true;
1379                         BADCalledBack.Set ();
1380                 }
1381
1382                 [Test]
1383                 public void BeginAcceptData ()
1384                 {
1385                         Socket sock = new Socket (AddressFamily.InterNetwork,
1386                                                   SocketType.Stream,
1387                                                   ProtocolType.Tcp);
1388                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1389                                                         1238);
1390                         
1391                         sock.Bind (ep);
1392                         sock.Listen (1);
1393                         
1394                         BADCalledBack.Reset ();
1395                         
1396                         sock.BeginAccept (256, BADCallback, sock);
1397
1398                         Socket conn = new Socket (AddressFamily.InterNetwork,
1399                                                   SocketType.Stream,
1400                                                   ProtocolType.Tcp);
1401                         byte[] send_bytes = new byte[] {10, 11, 12, 13};
1402                         
1403                         conn.Connect (ep);
1404                         conn.Send (send_bytes);
1405
1406                         if (BADCalledBack.WaitOne (2000, false) == false) {
1407                                 Assert.Fail ("BeginAcceptData wait timed out");
1408                         }
1409                         
1410                         Assert.AreEqual (true, BADAccepted, "BeginAcceptData #1");
1411                         Assert.AreEqual (true, BADSocket.Connected, "BeginAcceptData #2");
1412                         Assert.AreEqual (false, sock.Connected, "BeginAcceptData #3");
1413                         Assert.AreEqual (true, conn.Connected, "BeginAcceptData #4");
1414                         Assert.AreEqual (send_bytes.Length, BADByteCount, "BeginAcceptData #5");
1415                         
1416                         /* The MS runtime gives the returned data in a
1417                          * much bigger array.  TODO: investigate
1418                          * whether it the size correlates to the first
1419                          * parameter in BeginAccept()
1420                          */
1421                         Assert.IsFalse (BADBytes.Length == send_bytes.Length,
1422                                         "BeginAcceptData #6");
1423
1424                         for(int i = 0; i < send_bytes.Length; i++) {
1425                                 Assert.AreEqual (send_bytes[i], BADBytes[i], "BeginAcceptData #" + (i+7).ToString ());
1426                         }
1427
1428                         BADSocket.Close ();
1429                         conn.Close ();
1430                         sock.Close ();
1431                 }
1432
1433                 [Test]
1434                 [ExpectedException (typeof(ObjectDisposedException))]
1435                 public void BeginAcceptDataClosed ()
1436                 {
1437                         Socket sock = new Socket (AddressFamily.InterNetwork,
1438                                                   SocketType.Stream,
1439                                                   ProtocolType.Tcp);
1440                         
1441                         sock.Close ();
1442                         
1443                         sock.BeginAccept (256, BADCallback, sock);
1444                 }
1445
1446                 [Test]
1447                 public void BeginAcceptSocketUdp ()
1448                 {
1449                         Socket sock = new Socket (AddressFamily.InterNetwork,
1450                                                   SocketType.Stream,
1451                                                   ProtocolType.Tcp);
1452                         Socket acc = new Socket (AddressFamily.InterNetwork,
1453                                                  SocketType.Dgram,
1454                                                  ProtocolType.Udp);
1455                         
1456                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1457                                                         1239);
1458                         
1459                         sock.Bind (ep);
1460                         sock.Listen (1);
1461                         
1462                         try {
1463                                 sock.BeginAccept (acc, 256, BADCallback, sock);
1464                                 Assert.Fail ("BeginAcceptSocketUdp #1");
1465                         } catch (SocketException ex) {
1466                                 Assert.AreEqual (10022, ex.ErrorCode, "BeginAcceptSocketUdp #2");
1467                         } catch {
1468                                 Assert.Fail ("BeginAcceptSocketUdp #3");
1469                         } finally {
1470                                 acc.Close ();
1471                                 sock.Close ();
1472                         }
1473                 }
1474                 
1475                 [Test]
1476                 public void BeginAcceptSocketBound ()
1477                 {
1478                         Socket sock = new Socket (AddressFamily.InterNetwork,
1479                                                   SocketType.Stream,
1480                                                   ProtocolType.Tcp);
1481                         Socket acc = new Socket (AddressFamily.InterNetwork,
1482                                                  SocketType.Stream,
1483                                                  ProtocolType.Tcp);
1484                         
1485                         IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
1486                                                          1240);
1487                         
1488                         IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
1489                                                          1241);
1490                         
1491                         sock.Bind (ep1);
1492                         sock.Listen (1);
1493
1494                         acc.Bind (ep2);
1495                         
1496                         try {
1497                                 sock.BeginAccept (acc, 256, BADCallback, sock);
1498                                 Assert.Fail ("BeginAcceptSocketBound #1");
1499                         } catch (InvalidOperationException) {
1500                         } catch {
1501                                 Assert.Fail ("BeginAcceptSocketBound #2");
1502                         } finally {
1503                                 acc.Close ();
1504                                 sock.Close ();
1505                         }
1506                 }
1507                 
1508                 [Test]
1509                 public void BeginAcceptSocket ()
1510                 {
1511                         Socket sock = new Socket (AddressFamily.InterNetwork,
1512                                                   SocketType.Stream,
1513                                                   ProtocolType.Tcp);
1514                         Socket acc = new Socket (AddressFamily.InterNetwork,
1515                                                  SocketType.Stream,
1516                                                  ProtocolType.Tcp);
1517                         
1518                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1519                                                         1242);
1520                         
1521                         sock.Bind (ep);
1522                         sock.Listen (1);
1523                         
1524                         BADCalledBack.Reset ();
1525                         
1526                         sock.BeginAccept (acc, 256, BADCallback, sock);
1527
1528                         Socket conn = new Socket (AddressFamily.InterNetwork,
1529                                                   SocketType.Stream,
1530                                                   ProtocolType.Tcp);
1531                         byte[] send_bytes = new byte[] {10, 11, 12, 13};
1532                         
1533                         conn.Connect (ep);
1534                         conn.Send (send_bytes);
1535
1536                         if (BADCalledBack.WaitOne (2000, false) == false) {
1537                                 Assert.Fail ("BeginAcceptSocket wait timed out");
1538                         }
1539                         
1540                         Assert.AreEqual (true, BADAccepted, "BeginAcceptSocket #1");
1541                         Assert.AreEqual (true, BADSocket.Connected, "BeginAcceptSocket #2");
1542                         Assert.AreEqual (false, sock.Connected, "BeginAcceptSocket #3");
1543                         Assert.AreEqual (true, conn.Connected, "BeginAcceptSocket #4");
1544                         Assert.AreEqual (send_bytes.Length, BADByteCount, "BeginAcceptSocket #5");
1545                         Assert.AreEqual (AddressFamily.InterNetwork, acc.AddressFamily, "BeginAcceptSocket #6");
1546                         Assert.AreEqual (SocketType.Stream, acc.SocketType, "BeginAcceptSocket #7");
1547                         Assert.AreEqual (ProtocolType.Tcp, acc.ProtocolType, "BeginAcceptSocket #8");
1548                         Assert.AreEqual (conn.LocalEndPoint, acc.RemoteEndPoint, "BeginAcceptSocket #9");
1549                         
1550                         /* The MS runtime gives the returned data in a
1551                          * much bigger array.  TODO: investigate
1552                          * whether it the size correlates to the first
1553                          * parameter in BeginAccept()
1554                          */
1555                         Assert.IsFalse (BADBytes.Length == send_bytes.Length,
1556                                         "BeginAcceptSocket #10");
1557
1558                         for(int i = 0; i < send_bytes.Length; i++) {
1559                                 Assert.AreEqual (send_bytes[i], BADBytes[i], "BeginAcceptSocket #" + (i+11).ToString ());
1560                         }
1561
1562                         BADSocket.Close ();
1563                         conn.Close ();
1564                         acc.Close ();
1565                         sock.Close ();
1566                 }
1567
1568                 [Test]
1569                 public void BeginAcceptSocketClosed ()
1570                 {
1571                         Socket sock = new Socket (AddressFamily.InterNetwork,
1572                                                   SocketType.Stream,
1573                                                   ProtocolType.Tcp);
1574                         Socket acc = new Socket (AddressFamily.InterNetwork,
1575                                                  SocketType.Stream,
1576                                                  ProtocolType.Tcp);
1577                         
1578                         sock.Close ();
1579                         
1580                         try {
1581                                 sock.BeginAccept (acc, 256, BADCallback, null);
1582                                 Assert.Fail ("BeginAcceptSocketClosed #1");
1583                         } catch (ObjectDisposedException) {
1584                         } catch {
1585                                 Assert.Fail ("BeginAcceptSocketClosed #2");
1586                         } finally {
1587                                 acc.Close ();
1588                         }
1589                 }
1590
1591                 [Test]
1592                 public void BeginAcceptSocketAccClosed ()
1593                 {
1594                         Socket sock = new Socket (AddressFamily.InterNetwork,
1595                                                   SocketType.Stream,
1596                                                   ProtocolType.Tcp);
1597                         Socket acc = new Socket (AddressFamily.InterNetwork,
1598                                                  SocketType.Stream,
1599                                                  ProtocolType.Tcp);
1600                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1601                                                         1243);
1602
1603                         sock.Bind (ep);
1604                         sock.Listen (1);
1605                         
1606                         acc.Close ();
1607                         
1608                         BADCalledBack.Reset ();
1609                         
1610                         try {
1611                                 sock.BeginAccept (acc, 256, BADCallback, null);
1612                                 Assert.Fail ("BeginAcceptSocketAccClosed #1");
1613                         } catch (ObjectDisposedException) {
1614                         } catch {
1615                                 Assert.Fail ("BeginAcceptSocketAccClosed #2");
1616                         } finally {
1617                                 sock.Close ();
1618                         }
1619                 }
1620                 
1621                 static bool BCConnected = false;
1622                 static ManualResetEvent BCCalledBack = new ManualResetEvent (false);
1623                 
1624                 private static void BCCallback (IAsyncResult asyncResult)
1625                 {
1626                         Socket sock = (Socket)asyncResult.AsyncState;
1627                         
1628                         sock.EndConnect (asyncResult);
1629                         BCConnected = true;
1630                         
1631                         BCCalledBack.Set ();
1632                 }
1633                 
1634                 [Test]
1635                 public void BeginConnectAddressPort ()
1636                 {
1637                         Socket sock = new Socket (AddressFamily.InterNetwork,
1638                                                   SocketType.Stream,
1639                                                   ProtocolType.Tcp);
1640                         Socket listen = new Socket (AddressFamily.InterNetwork,
1641                                                     SocketType.Stream,
1642                                                     ProtocolType.Tcp);
1643                         IPAddress ip = IPAddress.Loopback;
1644                         IPEndPoint ep = new IPEndPoint (ip, 1244);
1645
1646                         listen.Bind (ep);
1647                         listen.Listen (1);
1648                         
1649                         BCCalledBack.Reset ();
1650                         
1651                         BCConnected = false;
1652                         
1653                         sock.BeginConnect (ip, 1244, BCCallback, sock);
1654
1655                         if (BCCalledBack.WaitOne (2000, false) == false) {
1656                                 Assert.Fail ("BeginConnectAddressPort wait timed out");
1657                         }
1658                         
1659                         Assert.AreEqual (true, BCConnected, "BeginConnectAddressPort #1");
1660                         
1661                         sock.Close ();
1662                         listen.Close ();
1663                 }
1664
1665                 [Test]
1666                 public void BeginConnectAddressPortNull ()
1667                 {
1668                         Socket sock = new Socket (AddressFamily.InterNetwork,
1669                                                   SocketType.Stream,
1670                                                   ProtocolType.Tcp);
1671                         IPAddress ip = null;
1672
1673                         try {
1674                                 sock.BeginConnect (ip, 1244, BCCallback,
1675                                                    sock);
1676                                 Assert.Fail ("BeginConnectAddressPortNull #1");
1677                         } catch (ArgumentNullException) {
1678                         } catch {
1679                                 Assert.Fail ("BeginConnectAddressPortNull #2");
1680                         } finally {
1681                                 sock.Close ();
1682                         }
1683                 }
1684
1685                 [Test]
1686                 public void BeginConnectAddressPortListen ()
1687                 {
1688                         Socket sock = new Socket (AddressFamily.InterNetwork,
1689                                                   SocketType.Stream,
1690                                                   ProtocolType.Tcp);
1691                         IPAddress ip = IPAddress.Loopback;
1692                         IPEndPoint ep = new IPEndPoint (ip, 1245);
1693
1694                         sock.Bind (ep);
1695                         sock.Listen (1);
1696                         
1697                         try {
1698                                 sock.BeginConnect (ip, 1245, BCCallback, sock);
1699                                 Assert.Fail ("BeginConnectAddressPortListen #1");
1700                         } catch (InvalidOperationException) {
1701                         } catch {
1702                                 Assert.Fail ("BeginConnectAddressPortListen #2");
1703                         } finally {
1704                                 sock.Close ();
1705                         }
1706                 }
1707                 
1708                 [Test]
1709                 [ExpectedException (typeof(ObjectDisposedException))]
1710                 public void BeginConnectAddressPortClosed ()
1711                 {
1712                         Socket sock = new Socket (AddressFamily.InterNetwork,
1713                                                   SocketType.Stream,
1714                                                   ProtocolType.Tcp);
1715                         IPAddress ip = IPAddress.Loopback;
1716                         
1717                         sock.Close ();
1718                         
1719                         sock.BeginConnect (ip, 1244, BCCallback, sock);
1720                 }
1721                 
1722                 [Test]
1723                 [Category ("NotOnMac")] // MacOSX will block attempting to connect to 127.0.0.4 causing the test to fail
1724                 public void BeginConnectMultiple ()
1725                 {
1726                         Socket sock = new Socket (AddressFamily.InterNetwork,
1727                                                   SocketType.Stream,
1728                                                   ProtocolType.Tcp);
1729                         Socket listen = new Socket (AddressFamily.InterNetwork,
1730                                                     SocketType.Stream,
1731                                                     ProtocolType.Tcp);
1732                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1733                                                         1246);
1734                         IPAddress[] ips = new IPAddress[4];
1735                         
1736                         ips[0] = IPAddress.Parse ("127.0.0.4");
1737                         ips[1] = IPAddress.Parse ("127.0.0.3");
1738                         ips[2] = IPAddress.Parse ("127.0.0.2");
1739                         ips[3] = IPAddress.Parse ("127.0.0.1");
1740
1741                         listen.Bind (ep);
1742                         listen.Listen (1);
1743                         
1744                         BCCalledBack.Reset ();
1745                         
1746                         BCConnected = false;
1747                         
1748                         sock.BeginConnect (ips, 1246, BCCallback, sock);
1749                         
1750                         /* Longer wait here, because the ms runtime
1751                          * takes a lot longer to not connect
1752                          */
1753                         if (BCCalledBack.WaitOne (10000, false) == false) {
1754                                 Assert.Fail ("BeginConnectMultiple wait failed");
1755                         }
1756                         
1757                         Assert.AreEqual (true, BCConnected, "BeginConnectMultiple #1");
1758                         Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "BeginConnectMultiple #2");
1759                         IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
1760                         
1761                         Assert.AreEqual (IPAddress.Loopback, remep.Address, "BeginConnectMultiple #2");
1762                         
1763                         sock.Close ();
1764                         listen.Close ();
1765                 }
1766
1767                 [Test]
1768                 public void BeginConnectMultipleNull ()
1769                 {
1770                         Socket sock = new Socket (AddressFamily.InterNetwork,
1771                                                   SocketType.Stream,
1772                                                   ProtocolType.Tcp);
1773                         IPAddress[] ips = null;
1774                         
1775                         try {
1776                                 sock.BeginConnect (ips, 1246, BCCallback,
1777                                                    sock);
1778                                 Assert.Fail ("BeginConnectMultipleNull #1");
1779                         } catch (ArgumentNullException) {
1780                         } catch {
1781                                 Assert.Fail ("BeginConnectMultipleNull #2");
1782                         } finally {
1783                                 sock.Close ();
1784                         }
1785                 }
1786
1787                 [Test]
1788                 public void BeginConnectMultipleListen ()
1789                 {
1790                         Socket sock = new Socket (AddressFamily.InterNetwork,
1791                                                   SocketType.Stream,
1792                                                   ProtocolType.Tcp);
1793                         IPAddress[] ips = new IPAddress[4];
1794                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1795                                                         1247);
1796                         
1797                         ips[0] = IPAddress.Parse ("127.0.0.4");
1798                         ips[1] = IPAddress.Parse ("127.0.0.3");
1799                         ips[2] = IPAddress.Parse ("127.0.0.2");
1800                         ips[3] = IPAddress.Parse ("127.0.0.1");
1801                         
1802                         sock.Bind (ep);
1803                         sock.Listen (1);
1804                         
1805                         try {
1806                                 sock.BeginConnect (ips, 1247, BCCallback,
1807                                                    sock);
1808                                 Assert.Fail ("BeginConnectMultipleListen #1");
1809                         } catch (InvalidOperationException) {
1810                         } catch {
1811                                 Assert.Fail ("BeginConnectMultipleListen #2");
1812                         } finally {
1813                                 sock.Close ();
1814                         }
1815                 }
1816                 
1817                 [Test]
1818                 [ExpectedException (typeof(ObjectDisposedException))]
1819                 public void BeginConnectMultipleClosed ()
1820                 {
1821                         Socket sock = new Socket (AddressFamily.InterNetwork,
1822                                                   SocketType.Stream,
1823                                                   ProtocolType.Tcp);
1824                         IPAddress[] ips = new IPAddress[4];
1825                         
1826                         ips[0] = IPAddress.Parse ("127.0.0.4");
1827                         ips[1] = IPAddress.Parse ("127.0.0.3");
1828                         ips[2] = IPAddress.Parse ("127.0.0.2");
1829                         ips[3] = IPAddress.Parse ("127.0.0.1");
1830                         
1831                         sock.Close ();
1832                         
1833                         sock.BeginConnect (ips, 1247, BCCallback, sock);
1834                 }
1835                 
1836                 [Test]
1837                 public void BeginConnectHostPortNull ()
1838                 {
1839                         Socket sock = new Socket (AddressFamily.InterNetwork,
1840                                                   SocketType.Stream,
1841                                                   ProtocolType.Tcp);
1842                         
1843                         try {
1844                                 sock.BeginConnect ((string)null, 0,
1845                                                    BCCallback, sock);
1846                                 Assert.Fail ("BeginConnectHostPort #1");
1847                         } catch (ArgumentNullException) {
1848                         } catch {
1849                                 Assert.Fail ("BeginConnectHostPort #2");
1850                         } finally {
1851                                 sock.Close ();
1852                         }
1853                 }
1854
1855                 [Test]
1856                 public void BeginConnectHostPortListen ()
1857                 {
1858                         Socket sock = new Socket (AddressFamily.InterNetwork,
1859                                                   SocketType.Stream,
1860                                                   ProtocolType.Tcp);
1861                         IPAddress ip = IPAddress.Loopback;
1862                         IPEndPoint ep = new IPEndPoint (ip, 1248);
1863                         
1864                         sock.Bind (ep);
1865                         sock.Listen (1);
1866                         
1867                         try {
1868                                 sock.BeginConnect ("localhost", 1248,
1869                                                    BCCallback, sock);
1870                                 Assert.Fail ("BeginConnectHostPortListen #1");
1871                         } catch (InvalidOperationException) {
1872                         } catch {
1873                                 Assert.Fail ("BeginConnectHostPortListen #2");
1874                         } finally {
1875                                 sock.Close ();
1876                         }
1877                 }
1878
1879                 [Test]
1880                 [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
1881                 public void BeginConnectHostPortNotIP ()
1882                 {
1883                         Socket sock = new Socket (AddressFamily.NetBios,
1884                                                   SocketType.Seqpacket,
1885                                                   ProtocolType.Unspecified);
1886                         
1887                         try {
1888                                 sock.BeginConnect ("localhost", 0, BCCallback,
1889                                                    sock);
1890                                 Assert.Fail ("BeginConnectHostPortNotIP #1");
1891                         } catch (NotSupportedException) {
1892                         } catch {
1893                                 Assert.Fail ("BeginConnectHostPortNotIP #2");
1894                         } finally {
1895                                 sock.Close ();
1896                         }
1897                 }
1898
1899                 [Test]
1900                 [ExpectedException (typeof(ObjectDisposedException))]
1901                 public void BeginConnectHostPortClosed ()
1902                 {
1903                         Socket sock = new Socket (AddressFamily.InterNetwork,
1904                                                   SocketType.Stream,
1905                                                   ProtocolType.Tcp);
1906                         
1907                         sock.Close ();
1908                         
1909                         sock.BeginConnect ("localhost", 0, BCCallback, sock);
1910                 }
1911                 
1912                 static bool BDDisconnected = false;
1913                 static ManualResetEvent BDCalledBack = new ManualResetEvent (false);
1914                 
1915                 private static void BDCallback (IAsyncResult asyncResult)
1916                 {
1917                         Socket sock = (Socket)asyncResult.AsyncState;
1918                         
1919                         sock.EndDisconnect (asyncResult);
1920                         BDDisconnected = true;
1921                         
1922                         BDCalledBack.Set ();
1923                 }
1924                 
1925                 [Test]
1926                 [Category ("NotDotNet")] // "Needs XP or later"
1927                 public void BeginDisconnect ()
1928                 {
1929                         Socket sock = new Socket (AddressFamily.InterNetwork,
1930                                                   SocketType.Stream,
1931                                                   ProtocolType.Tcp);
1932                         Socket listen = new Socket (AddressFamily.InterNetwork,
1933                                                     SocketType.Stream,
1934                                                     ProtocolType.Tcp);
1935                         IPAddress ip = IPAddress.Loopback;
1936                         IPEndPoint ep = new IPEndPoint (ip, 1254);
1937                         
1938                         listen.Bind (ep);
1939                         listen.Listen (1);
1940                         
1941                         sock.Connect (ip, 1254);
1942                         
1943                         Assert.AreEqual (true, sock.Connected, "BeginDisconnect #1");
1944                         
1945                         sock.Shutdown (SocketShutdown.Both);
1946
1947                         BDCalledBack.Reset ();
1948                         BDDisconnected = false;
1949                         
1950                         sock.BeginDisconnect (false, BDCallback, sock);
1951                 
1952                         if (BDCalledBack.WaitOne (2000, false) == false) {
1953                                 Assert.Fail ("BeginDisconnect wait timed out");
1954                         }
1955                         
1956                         Assert.AreEqual (true, BDDisconnected, "BeginDisconnect #2");
1957                         Assert.AreEqual (false, sock.Connected, "BeginDisconnect #3");
1958                         
1959                         sock.Close ();
1960                         listen.Close ();
1961                 }
1962                 
1963                 [Test]
1964                 public void BeginReceiveSocketError ()
1965                 {
1966                 }
1967                 
1968                 [Test]
1969                 public void BeginReceiveGeneric ()
1970                 {
1971                 }
1972                 
1973                 [Test]
1974                 public void BeginReceiveGenericSocketError ()
1975                 {
1976                 }
1977                 
1978                 private static void BSCallback (IAsyncResult asyncResult)
1979                 {
1980                         Socket sock = (Socket)asyncResult.AsyncState;
1981                         
1982                         sock.EndSend (asyncResult);
1983                 }
1984                 
1985                 [Test]
1986                 public void BeginSendNotConnected ()
1987                 {
1988                         Socket sock = new Socket (AddressFamily.InterNetwork,
1989                                                   SocketType.Stream,
1990                                                   ProtocolType.Tcp);
1991                         
1992                         byte[] send_bytes = new byte[] {10, 11, 12, 13};
1993                         
1994                         try {
1995                                 sock.BeginSend (send_bytes, 0,
1996                                                 send_bytes.Length,
1997                                                 SocketFlags.None, BSCallback,
1998                                                 sock);
1999                                 Assert.Fail ("BeginSendNotConnected #1");
2000                         } catch (SocketException ex) {
2001                                 Assert.AreEqual (10057, ex.ErrorCode, "BeginSendNotConnected #2");
2002                         } catch {
2003                                 Assert.Fail ("BeginSendNotConnected #3");
2004                         } finally {
2005                                 sock.Close ();
2006                         }
2007                 }
2008                 
2009                 [Test]
2010                 public void BeginSendSocketError ()
2011                 {
2012                 }
2013                 
2014                 [Test]
2015                 public void BeginSendGeneric ()
2016                 {
2017                 }
2018                 
2019                 [Test]
2020                 public void BeginSendGenericSocketError ()
2021                 {
2022                 }
2023                 
2024                 [Test]
2025                 public void BindTwice ()
2026                 {
2027                         Socket sock = new Socket (AddressFamily.InterNetwork,
2028                                                   SocketType.Stream,
2029                                                   ProtocolType.Tcp);
2030                         IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
2031                                                         1256);
2032                         IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
2033                                                          1257);
2034                         
2035                         sock.Bind (ep1);
2036                         
2037                         try {
2038                                 sock.Bind (ep2);
2039                                 Assert.Fail ("BindTwice #1");
2040                         } catch (SocketException ex) {
2041                                 Assert.AreEqual (10022, ex.ErrorCode, "BindTwice #2");
2042                         } catch {
2043                                 Assert.Fail ("BindTwice #3");
2044                         } finally {
2045                                 sock.Close ();
2046                         }
2047                 }
2048                 
2049                 [Test]
2050                 public void Close ()
2051                 {
2052                         Socket sock = new Socket (AddressFamily.InterNetwork,
2053                                                   SocketType.Stream,
2054                                                   ProtocolType.Tcp);
2055                         Socket listen = new Socket (AddressFamily.InterNetwork,
2056                                                     SocketType.Stream,
2057                                                     ProtocolType.Tcp);
2058                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2059                                                         1258);
2060                         
2061                         listen.Bind (ep);
2062                         listen.Listen (1);
2063                         
2064                         sock.Connect (ep);
2065
2066                         Assert.AreEqual (true, sock.Connected, "Close #1");
2067                         
2068                         sock.Close (2);
2069                         
2070                         Thread.Sleep (3000);
2071                         
2072                         Assert.AreEqual (false, sock.Connected, "Close #2");
2073                         
2074                         listen.Close ();
2075                 }
2076                 
2077                 [Test]
2078                 public void ConnectAddressPort ()
2079                 {
2080                         Socket sock = new Socket (AddressFamily.InterNetwork,
2081                                                   SocketType.Stream,
2082                                                   ProtocolType.Tcp);
2083                         Socket listen = new Socket (AddressFamily.InterNetwork,
2084                                                     SocketType.Stream,
2085                                                     ProtocolType.Tcp);
2086                         IPAddress ip = IPAddress.Loopback;
2087                         IPEndPoint ep = new IPEndPoint (ip, 1249);
2088
2089                         listen.Bind (ep);
2090                         listen.Listen (1);
2091                         
2092                         sock.Connect (ip, 1249);
2093                         
2094                         Assert.AreEqual (true, sock.Connected, "ConnectAddressPort #1");
2095                         
2096                         sock.Close ();
2097                         listen.Close ();
2098                 }
2099
2100                 [Test]
2101                 public void ConnectAddressPortNull ()
2102                 {
2103                         Socket sock = new Socket (AddressFamily.InterNetwork,
2104                                                   SocketType.Stream,
2105                                                   ProtocolType.Tcp);
2106                         IPAddress ip = null;
2107
2108                         try {
2109                                 sock.Connect (ip, 1249);
2110                                 Assert.Fail ("ConnectAddressPortNull #1");
2111                         } catch (ArgumentNullException) {
2112                         } catch {
2113                                 Assert.Fail ("ConnectAddressPortNull #2");
2114                         } finally {
2115                                 sock.Close ();
2116                         }
2117                 }
2118
2119                 [Test]
2120                 public void ConnectAddressPortListen ()
2121                 {
2122                         Socket sock = new Socket (AddressFamily.InterNetwork,
2123                                                   SocketType.Stream,
2124                                                   ProtocolType.Tcp);
2125                         IPAddress ip = IPAddress.Loopback;
2126                         IPEndPoint ep = new IPEndPoint (ip, 1250);
2127
2128                         sock.Bind (ep);
2129                         sock.Listen (1);
2130                         
2131                         try {
2132                                 sock.Connect (ip, 1250);
2133                                 Assert.Fail ("ConnectAddressPortListen #1");
2134                         } catch (InvalidOperationException) {
2135                         } catch {
2136                                 Assert.Fail ("ConnectAddressPortListen #2");
2137                         } finally {
2138                                 sock.Close ();
2139                         }
2140                 }
2141                 
2142                 [Test]
2143                 [ExpectedException (typeof(ObjectDisposedException))]
2144                 public void ConnectAddressPortClosed ()
2145                 {
2146                         Socket sock = new Socket (AddressFamily.InterNetwork,
2147                                                   SocketType.Stream,
2148                                                   ProtocolType.Tcp);
2149                         IPAddress ip = IPAddress.Loopback;
2150                         
2151                         sock.Close ();
2152                         
2153                         sock.Connect (ip, 1250);
2154                 }
2155                 
2156                 [Test]
2157                 [Category ("NotOnMac")] // MacOSX trashes the fd after the failed connect attempt to 127.0.0.4
2158                 public void ConnectMultiple ()
2159                 {
2160                         Socket sock = new Socket (AddressFamily.InterNetwork,
2161                                                   SocketType.Stream,
2162                                                   ProtocolType.Tcp);
2163                         Socket listen = new Socket (AddressFamily.InterNetwork,
2164                                                     SocketType.Stream,
2165                                                     ProtocolType.Tcp);
2166                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2167                                                         1251);
2168                         IPAddress[] ips = new IPAddress[4];
2169                         
2170                         ips[0] = IPAddress.Parse ("127.0.0.4");
2171                         ips[1] = IPAddress.Parse ("127.0.0.3");
2172                         ips[2] = IPAddress.Parse ("127.0.0.2");
2173                         ips[3] = IPAddress.Parse ("127.0.0.1");
2174
2175                         listen.Bind (ep);
2176                         listen.Listen (1);
2177                         
2178                         sock.Connect (ips, 1251);
2179                         
2180                         Assert.AreEqual (true, sock.Connected, "ConnectMultiple #1");
2181                         Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "ConnectMultiple #2");
2182                         IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
2183                         
2184                         Assert.AreEqual (IPAddress.Loopback, remep.Address, "ConnectMultiple #2");
2185                         
2186                         sock.Close ();
2187                         listen.Close ();
2188                 }
2189
2190                 [Test]
2191                 public void ConnectMultipleNull ()
2192                 {
2193                         Socket sock = new Socket (AddressFamily.InterNetwork,
2194                                                   SocketType.Stream,
2195                                                   ProtocolType.Tcp);
2196                         IPAddress[] ips = null;
2197                         
2198                         try {
2199                                 sock.Connect (ips, 1251);
2200                                 Assert.Fail ("ConnectMultipleNull #1");
2201                         } catch (ArgumentNullException) {
2202                         } catch {
2203                                 Assert.Fail ("ConnectMultipleNull #2");
2204                         } finally {
2205                                 sock.Close ();
2206                         }
2207                 }
2208
2209                 [Test]
2210                 public void ConnectMultipleListen ()
2211                 {
2212                         Socket sock = new Socket (AddressFamily.InterNetwork,
2213                                                   SocketType.Stream,
2214                                                   ProtocolType.Tcp);
2215                         IPAddress[] ips = new IPAddress[4];
2216                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2217                                                         1252);
2218                         
2219                         ips[0] = IPAddress.Parse ("127.0.0.4");
2220                         ips[1] = IPAddress.Parse ("127.0.0.3");
2221                         ips[2] = IPAddress.Parse ("127.0.0.2");
2222                         ips[3] = IPAddress.Parse ("127.0.0.1");
2223                         
2224                         sock.Bind (ep);
2225                         sock.Listen (1);
2226                         
2227                         try {
2228                                 sock.Connect (ips, 1252);
2229                                 Assert.Fail ("ConnectMultipleListen #1");
2230                         } catch (InvalidOperationException) {
2231                         } catch {
2232                                 Assert.Fail ("ConnectMultipleListen #2");
2233                         } finally {
2234                                 sock.Close ();
2235                         }
2236                 }
2237                 
2238                 [Test]
2239                 [ExpectedException (typeof(ObjectDisposedException))]
2240                 public void ConnectMultipleClosed ()
2241                 {
2242                         Socket sock = new Socket (AddressFamily.InterNetwork,
2243                                                   SocketType.Stream,
2244                                                   ProtocolType.Tcp);
2245                         IPAddress[] ips = new IPAddress[4];
2246                         
2247                         ips[0] = IPAddress.Parse ("127.0.0.4");
2248                         ips[1] = IPAddress.Parse ("127.0.0.3");
2249                         ips[2] = IPAddress.Parse ("127.0.0.2");
2250                         ips[3] = IPAddress.Parse ("127.0.0.1");
2251                         
2252                         sock.Close ();
2253                         
2254                         sock.Connect (ips, 1252);
2255                 }
2256                 
2257                 [Test]
2258                 public void ConnectHostPortNull ()
2259                 {
2260                         Socket sock = new Socket (AddressFamily.InterNetwork,
2261                                                   SocketType.Stream,
2262                                                   ProtocolType.Tcp);
2263                         
2264                         try {
2265                                 sock.Connect ((string)null, 0);
2266                                 Assert.Fail ("ConnectHostPort #1");
2267                         } catch (ArgumentNullException) {
2268                         } catch {
2269                                 Assert.Fail ("ConnectHostPort #2");
2270                         } finally {
2271                                 sock.Close ();
2272                         }
2273                 }
2274
2275                 [Test]
2276                 public void ConnectHostPortListen ()
2277                 {
2278                         Socket sock = new Socket (AddressFamily.InterNetwork,
2279                                                   SocketType.Stream,
2280                                                   ProtocolType.Tcp);
2281                         IPAddress ip = IPAddress.Loopback;
2282                         IPEndPoint ep = new IPEndPoint (ip, 1253);
2283                         
2284                         sock.Bind (ep);
2285                         sock.Listen (1);
2286                         
2287                         try {
2288                                 sock.Connect ("localhost", 1253);
2289                                 Assert.Fail ("ConnectHostPortListen #1");
2290                         } catch (InvalidOperationException) {
2291                         } catch {
2292                                 Assert.Fail ("ConnectHostPortListen #2");
2293                         } finally {
2294                                 sock.Close ();
2295                         }
2296                 }
2297
2298                 [Test]
2299                 [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
2300                 public void ConnectHostPortNotIP ()
2301                 {
2302                         Socket sock = new Socket (AddressFamily.NetBios,
2303                                                   SocketType.Seqpacket,
2304                                                   ProtocolType.Unspecified);
2305                         
2306                         try {
2307                                 sock.Connect ("localhost", 0);
2308                                 Assert.Fail ("ConnectHostPortNotIP #1");
2309                         } catch (NotSupportedException) {
2310                         } catch {
2311                                 Assert.Fail ("ConnectHostPortNotIP #2");
2312                         } finally {
2313                                 sock.Close ();
2314                         }
2315                 }
2316
2317                 [Test]
2318                 [ExpectedException (typeof(ObjectDisposedException))]
2319                 public void ConnectHostPortClosed ()
2320                 {
2321                         Socket sock = new Socket (AddressFamily.InterNetwork,
2322                                                   SocketType.Stream,
2323                                                   ProtocolType.Tcp);
2324                         
2325                         sock.Close ();
2326                         
2327                         sock.Connect ("localhost", 0);
2328                 }
2329                 
2330                 [Test]
2331                 [Category ("NotDotNet")] // "Needs XP or later"
2332                 public void Disconnect ()
2333                 {
2334                         Socket sock = new Socket (AddressFamily.InterNetwork,
2335                                                   SocketType.Stream,
2336                                                   ProtocolType.Tcp);
2337                         Socket listen = new Socket (AddressFamily.InterNetwork,
2338                                                     SocketType.Stream,
2339                                                     ProtocolType.Tcp);
2340                         IPAddress ip = IPAddress.Loopback;
2341                         IPEndPoint ep = new IPEndPoint (ip, 1255);
2342                         
2343                         listen.Bind (ep);
2344                         listen.Listen (1);
2345                         
2346                         sock.Connect (ip, 1255);
2347                         
2348                         Assert.AreEqual (true, sock.Connected, "Disconnect #1");
2349                         
2350                         sock.Shutdown (SocketShutdown.Both);
2351
2352                         sock.Disconnect (false);
2353
2354                         Assert.AreEqual (false, sock.Connected, "BeginDisconnect #3");
2355                         
2356                         sock.Close ();
2357                         listen.Close ();
2358                 }
2359                 
2360                 [Test]
2361                 public void DuplicateAndClose ()
2362                 {
2363                 }
2364                 
2365                 [Test]
2366                 public void IOControl ()
2367                 {
2368                 }
2369                 
2370                 [Test]
2371                 public void ReceiveGeneric ()
2372                 {
2373                         int i;
2374
2375                         IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, 1258);
2376
2377                         Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2378                         listensock.Bind (endpoint);
2379                         listensock.Listen(1);
2380
2381                         Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2382                         sendsock.Connect(endpoint);
2383
2384                         Socket clientsock = listensock.Accept();
2385                         
2386                         byte[] sendbuf = new byte[256];
2387
2388                         for(i = 0; i < 256; i++) {
2389                                 sendbuf[i] = (byte)i;
2390                         }
2391                         for (i = 4; i < 6; i++) {
2392                                 Assert.AreEqual (sendbuf[i], (byte)i,
2393                                                  "#1/" + i.ToString());
2394                         }
2395
2396                         SocketError err;
2397                         sendsock.Send (sendbuf, 0, 256, SocketFlags.None,
2398                                        out err);
2399
2400
2401                         byte[] recvbuf = new byte[256];
2402                         List<ArraySegment<byte>> recvbuflist = new List<ArraySegment<byte>>(2);
2403                         recvbuflist.Add(new ArraySegment<byte>(recvbuf, 4, 2));
2404                         recvbuflist.Add(new ArraySegment<byte>(recvbuf, 20, 230));
2405                         
2406                         clientsock.Receive (recvbuflist);
2407
2408                         /* recvbuf should now hold the first 2 bytes
2409                          * of sendbuf from pos 4, and the next 230
2410                          * bytes of sendbuf from pos 20
2411                          */
2412
2413                         for (i = 0; i < 2; i++) {
2414                                 Assert.AreEqual (sendbuf[i], recvbuf[i + 4],
2415                                                  "#2/" + i.ToString());
2416                         }
2417                         for (i = 2; i < 232; i++) {
2418                                 Assert.AreEqual (sendbuf[i], recvbuf[i + 18],
2419                                                  "#2/" + i.ToString());
2420                         }
2421
2422                         sendsock.Close ();
2423                         clientsock.Close ();
2424                         listensock.Close ();
2425                 }
2426                 
2427                 [Test]
2428                 public void SendGeneric ()
2429                 {
2430                         int i;
2431
2432                         IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, 1259);
2433
2434                         Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2435                         listensock.Bind (endpoint);
2436                         listensock.Listen(1);
2437
2438                         Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2439                         sendsock.Connect(endpoint);
2440
2441                         Socket clientsock = listensock.Accept();
2442
2443                         byte[] sendbuf = new byte[256];
2444                         List<ArraySegment<byte>> sendbuflist = new List<ArraySegment<byte>>(2);
2445
2446                         sendbuflist.Add(new ArraySegment<byte>(sendbuf, 4, 2));
2447                         sendbuflist.Add(new ArraySegment<byte>(sendbuf, 20, 230));
2448
2449                         for(i = 0; i < 256; i++) {
2450                                 sendbuf[i] = (byte)i;
2451                         }
2452                         for (i = 4; i < 6; i++) {
2453                                 Assert.AreEqual (sendbuf[i], (byte)i,
2454                                                  "#1/" + i.ToString());
2455                         }
2456
2457                         SocketError err;
2458                         sendsock.Send (sendbuflist, SocketFlags.None, out err);
2459
2460                         
2461                         byte[] recvbuf = new byte[256];
2462
2463                         clientsock.Receive (recvbuf);
2464
2465                         /* The first 2 bytes of recvbuf should now
2466                          * hold 2 bytes of sendbuf from pos 4, and the
2467                          * next 230 bytes of recvbuf should be sendbuf
2468                          * from pos 20
2469                          */
2470
2471                         for (i = 0; i < 2; i++) {
2472                                 Assert.AreEqual (recvbuf[i], sendbuf[i + 4],
2473                                                  "#2/" + i.ToString());
2474                         }
2475                         for (i = 2; i < 232; i++) {
2476                                 Assert.AreEqual (recvbuf[i], sendbuf[i + 18],
2477                                                  "#2/" + i.ToString());
2478                         }
2479
2480                         sendsock.Close ();
2481                         clientsock.Close ();
2482                         listensock.Close ();
2483                 }
2484
2485                 [Test]
2486                 public void ListenNotBound ()
2487                 {
2488                         Socket sock = new Socket (AddressFamily.InterNetwork,
2489                                                   SocketType.Stream,
2490                                                   ProtocolType.Tcp);
2491                         
2492                         try {
2493                                 sock.Listen (1);
2494                                 Assert.Fail ("ListenNotBound #1");
2495                         } catch (SocketException ex) {
2496                                 Assert.AreEqual (10022, ex.ErrorCode, "ListenNotBound #2");
2497                         } catch {
2498                                 Assert.Fail ("ListenNotBound #3");
2499                         } finally {
2500                                 sock.Close ();
2501                         }
2502                 }
2503 #endif
2504
2505                 static Socket CWRSocket;
2506                 static bool CWRReceiving = true;
2507                 static ManualResetEvent CWRReady = new ManualResetEvent (false);
2508                 
2509                 private static void CWRReceiveThread ()
2510                 {
2511                         byte[] buf = new byte[256];
2512                         
2513                         try {
2514                                 CWRSocket.Receive (buf);
2515                         } catch (SocketException) {
2516                                 CWRReceiving = false;
2517                         }
2518
2519                         CWRReady.Set ();
2520                 }
2521                 
2522                 [Test]
2523                 public void CloseWhileReceiving ()
2524                 {
2525                         CWRSocket = new Socket (AddressFamily.InterNetwork,
2526                                                 SocketType.Dgram,
2527                                                 ProtocolType.Udp);
2528                         CWRSocket.Bind (new IPEndPoint (IPAddress.Loopback,
2529                                                         1256));
2530                         
2531                         Thread recv_thread = new Thread (new ThreadStart (CWRReceiveThread));
2532                         CWRReady.Reset ();
2533                         recv_thread.Start ();
2534                         Thread.Sleep (250);     /* Wait for the thread to be already receiving */
2535
2536                         CWRSocket.Close ();
2537                         if (CWRReady.WaitOne (1000, false) == false) {
2538                                 Assert.Fail ("CloseWhileReceiving wait timed out");
2539                         }
2540                         
2541                         Assert.IsFalse (CWRReceiving);
2542                 }
2543
2544                 static bool RRCLastRead = false;
2545                 static ManualResetEvent RRCReady = new ManualResetEvent (false);
2546                 
2547                 private static void RRCClientThread ()
2548                 {
2549                         byte[] bytes = new byte[8];
2550                         int readbyte;
2551                         
2552                         Socket sock = new Socket (AddressFamily.InterNetwork,
2553                                                   SocketType.Stream,
2554                                                   ProtocolType.Tcp);
2555                         sock.Connect (new IPEndPoint (IPAddress.Loopback,
2556                                                       1257));
2557                         
2558                         NetworkStream stream = new NetworkStream (sock);
2559
2560                         readbyte = stream.ReadByte ();
2561                         Assert.AreEqual (0, readbyte, "ReceiveRemoteClosed #1");
2562                         
2563                         stream.Read (bytes, 0, 0);
2564
2565                         readbyte = stream.ReadByte ();
2566                         Assert.AreEqual (0, readbyte, "ReceiveRemoteClosed #2");
2567                         
2568                         stream.Read (bytes, 0, 0);
2569
2570                         readbyte = stream.ReadByte ();
2571                         Assert.AreEqual (-1, readbyte, "ReceiveRemoteClosed #3");
2572
2573                         sock.Close ();
2574
2575                         RRCLastRead = true;
2576                         RRCReady.Set ();
2577                 }
2578
2579                 [Test] // Receive (Byte [])
2580                 public void Receive1_Buffer_Null ()
2581                 {
2582                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2583                                 ProtocolType.Tcp);
2584
2585                         try {
2586                                 s.Receive ((byte []) null);
2587                                 Assert.Fail ("#1");
2588                         } catch (ArgumentNullException ex) {
2589                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2590                                 Assert.IsNull (ex.InnerException, "#3");
2591                                 Assert.IsNotNull (ex.Message, "#4");
2592                                 Assert.AreEqual ("buffer", ex.ParamName, "#5");
2593                         } finally {
2594                                 s.Close ();
2595                         }
2596                 }
2597
2598                 [Test] // Receive (Byte [])
2599                 public void Receive1_Socket_Closed ()
2600                 {
2601                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2602                                 ProtocolType.Tcp);
2603                         s.Close ();
2604
2605                         try {
2606                                 s.Receive ((byte []) null);
2607                                 Assert.Fail ("#1");
2608                         } catch (ObjectDisposedException ex) {
2609                                 // Cannot access a disposed object
2610                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2611                                 Assert.IsNull (ex.InnerException, "#3");
2612                                 Assert.IsNotNull (ex.Message, "#4");
2613                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2614                         }
2615                 }
2616
2617                 [Test] // Receive (Byte [], SocketFlags)
2618                 public void Receive2_Buffer_Null ()
2619                 {
2620                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2621                                 ProtocolType.Tcp);
2622
2623                         try {
2624                                 s.Receive ((byte []) null, (SocketFlags) 666);
2625                                 Assert.Fail ("#1");
2626                         } catch (ArgumentNullException ex) {
2627                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2628                                 Assert.IsNull (ex.InnerException, "#3");
2629                                 Assert.IsNotNull (ex.Message, "#4");
2630                                 Assert.AreEqual ("buffer", ex.ParamName, "#5");
2631                         } finally {
2632                                 s.Close ();
2633                         }
2634                 }
2635
2636                 [Test] // Receive (Byte [], SocketFlags)
2637                 public void Receive2_Socket_Closed ()
2638                 {
2639                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2640                                 ProtocolType.Tcp);
2641                         s.Close ();
2642
2643                         try {
2644                                 s.Receive ((byte []) null, (SocketFlags) 666);
2645                                 Assert.Fail ("#1");
2646                         } catch (ObjectDisposedException ex) {
2647                                 // Cannot access a disposed object
2648                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2649                                 Assert.IsNull (ex.InnerException, "#3");
2650                                 Assert.IsNotNull (ex.Message, "#4");
2651                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2652                         }
2653                 }
2654
2655                 [Test] // Receive (Byte [], Int32, SocketFlags)
2656                 public void Receive3_Buffer_Null ()
2657                 {
2658                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2659                                 ProtocolType.Tcp);
2660
2661                         try {
2662                                 s.Receive ((byte []) null, 0, (SocketFlags) 666);
2663                                 Assert.Fail ("#1");
2664                         } catch (ArgumentNullException ex) {
2665                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2666                                 Assert.IsNull (ex.InnerException, "#3");
2667                                 Assert.IsNotNull (ex.Message, "#4");
2668                                 Assert.AreEqual ("buffer", ex.ParamName, "#5");
2669                         } finally {
2670                                 s.Close ();
2671                         }
2672                 }
2673
2674                 [Test] // Receive (Byte [], Int32, SocketFlags)
2675                 public void Receive3_Socket_Closed ()
2676                 {
2677                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2678                                 ProtocolType.Tcp);
2679                         s.Close ();
2680
2681                         try {
2682                                 s.Receive ((byte []) null, 0, (SocketFlags) 666);
2683                                 Assert.Fail ("#1");
2684                         } catch (ObjectDisposedException ex) {
2685                                 // Cannot access a disposed object
2686                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2687                                 Assert.IsNull (ex.InnerException, "#3");
2688                                 Assert.IsNotNull (ex.Message, "#4");
2689                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2690                         }
2691                 }
2692
2693                 [Test] // Receive (Byte [], Int32, Int32, SocketFlags)
2694                 public void Receive4_Buffer_Null ()
2695                 {
2696                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2697                                 ProtocolType.Tcp);
2698
2699                         try {
2700                                 s.Receive ((byte []) null, 0, 0, (SocketFlags) 666);
2701                                 Assert.Fail ("#1");
2702                         } catch (ArgumentNullException ex) {
2703                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2704                                 Assert.IsNull (ex.InnerException, "#3");
2705                                 Assert.IsNotNull (ex.Message, "#4");
2706                                 Assert.AreEqual ("buffer", ex.ParamName, "#5");
2707                         } finally {
2708                                 s.Close ();
2709                         }
2710                 }
2711
2712                 [Test] // Receive (Byte [], Int32, Int32, SocketFlags)
2713                 public void Receive4_Socket_Closed ()
2714                 {
2715                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2716                                 ProtocolType.Tcp);
2717                         s.Close ();
2718
2719                         try {
2720                                 s.Receive ((byte []) null, 0, 0, (SocketFlags) 666);
2721                                 Assert.Fail ("#1");
2722                         } catch (ObjectDisposedException ex) {
2723                                 // Cannot access a disposed object
2724                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2725                                 Assert.IsNull (ex.InnerException, "#3");
2726                                 Assert.IsNotNull (ex.Message, "#4");
2727                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2728                         }
2729                 }
2730
2731 #if NET_2_0
2732                 [Test] // Receive (Byte [], Int32, Int32, SocketFlags, out SocketError)
2733                 public void Receive5_Buffer_Null ()
2734                 {
2735                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2736                                 ProtocolType.Tcp);
2737
2738                         SocketError error;
2739                         try {
2740                                 s.Receive ((byte []) null, 0, 0, SocketFlags.None, out error);
2741                                 Assert.Fail ("#1");
2742                         } catch (ArgumentNullException ex) {
2743                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2744                                 Assert.IsNull (ex.InnerException, "#3");
2745                                 Assert.IsNotNull (ex.Message, "#4");
2746                                 Assert.AreEqual ("buffer", ex.ParamName, "#5");
2747                         } finally {
2748                                 s.Close ();
2749                         }
2750                 }
2751
2752                 [Test] // Receive (Byte [], Int32, Int32, SocketFlags, out SocketError)
2753                 public void Receive5_Socket_Closed ()
2754                 {
2755                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2756                                 ProtocolType.Tcp);
2757                         s.Close ();
2758
2759                         SocketError error;
2760                         try {
2761                                 s.Receive ((byte []) null, 0, 0, SocketFlags.None, out error);
2762                                 Assert.Fail ("#1");
2763                         } catch (ObjectDisposedException ex) {
2764                                 // Cannot access a disposed object
2765                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2766                                 Assert.IsNull (ex.InnerException, "#3");
2767                                 Assert.IsNotNull (ex.Message, "#4");
2768                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2769                         }
2770                 }
2771
2772                 [Test] // Receive (IList<ArraySegment<Byte>>)
2773                 public void Receive6_Buffers_Null ()
2774                 {
2775                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2776                                 ProtocolType.Tcp);
2777
2778                         try {
2779                                 s.Receive ((IList<ArraySegment<byte>>) null);
2780                                 Assert.Fail ("#1");
2781                         } catch (ArgumentNullException ex) {
2782                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2783                                 Assert.IsNull (ex.InnerException, "#3");
2784                                 Assert.IsNotNull (ex.Message, "#4");
2785                                 Assert.AreEqual ("buffers", ex.ParamName, "#5");
2786                         } finally {
2787                                 s.Close ();
2788                         }
2789                 }
2790
2791                 [Test] // Receive (IList<ArraySegment<Byte>>)
2792                 public void Receive6_Socket_Closed ()
2793                 {
2794                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2795                                 ProtocolType.Tcp);
2796                         s.Close ();
2797
2798                         try {
2799                                 s.Receive ((IList<ArraySegment<byte>>) null);
2800                                 Assert.Fail ("#1");
2801                         } catch (ObjectDisposedException ex) {
2802                                 // Cannot access a disposed object
2803                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2804                                 Assert.IsNull (ex.InnerException, "#3");
2805                                 Assert.IsNotNull (ex.Message, "#4");
2806                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2807                         }
2808                 }
2809
2810                 [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags)
2811                 public void Receive7_Buffers_Null ()
2812                 {
2813                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2814                                 ProtocolType.Tcp);
2815
2816                         try {
2817                                 s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666);
2818                                 Assert.Fail ("#1");
2819                         } catch (ArgumentNullException ex) {
2820                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2821                                 Assert.IsNull (ex.InnerException, "#3");
2822                                 Assert.IsNotNull (ex.Message, "#4");
2823                                 Assert.AreEqual ("buffers", ex.ParamName, "#5");
2824                         } finally {
2825                                 s.Close ();
2826                         }
2827                 }
2828
2829                 [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags)
2830                 public void Receive7_Socket_Closed ()
2831                 {
2832                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2833                                 ProtocolType.Tcp);
2834                         s.Close ();
2835
2836                         try {
2837                                 s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666);
2838                                 Assert.Fail ("#1");
2839                         } catch (ObjectDisposedException ex) {
2840                                 // Cannot access a disposed object
2841                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2842                                 Assert.IsNull (ex.InnerException, "#3");
2843                                 Assert.IsNotNull (ex.Message, "#4");
2844                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2845                         }
2846                 }
2847
2848                 [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags, out SocketError)
2849                 public void Receive8_Buffers_Null ()
2850                 {
2851                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2852                                 ProtocolType.Tcp);
2853
2854                         SocketError error;
2855                         try {
2856                                 s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666,
2857                                         out error);
2858                                 Assert.Fail ("#1");
2859                         } catch (ArgumentNullException ex) {
2860                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2861                                 Assert.IsNull (ex.InnerException, "#3");
2862                                 Assert.IsNotNull (ex.Message, "#4");
2863                                 Assert.AreEqual ("buffers", ex.ParamName, "#5");
2864                         } finally {
2865                                 s.Close ();
2866                         }
2867                 }
2868
2869                 [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags, out SocketError)
2870                 public void Receive8_Socket_Closed ()
2871                 {
2872                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2873                                 ProtocolType.Tcp);
2874                         s.Close ();
2875
2876                         SocketError error;
2877                         try {
2878                                 s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666,
2879                                         out error);
2880                                 Assert.Fail ("#1");
2881                         } catch (ObjectDisposedException ex) {
2882                                 // Cannot access a disposed object
2883                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2884                                 Assert.IsNull (ex.InnerException, "#3");
2885                                 Assert.IsNotNull (ex.Message, "#4");
2886                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2887                         } finally {
2888                                 s.Close ();
2889                         }
2890                 }
2891 #endif
2892
2893                 [Test] // ReceiveFrom (Byte [], ref EndPoint)
2894                 public void ReceiveFrom1_Buffer_Null ()
2895                 {
2896                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2897                                 ProtocolType.Tcp);
2898
2899                         EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
2900                         try {
2901                                 s.ReceiveFrom ((Byte []) null, ref remoteEP);
2902                                 Assert.Fail ("#1");
2903                         } catch (ArgumentNullException ex) {
2904                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2905                                 Assert.IsNull (ex.InnerException, "#3");
2906                                 Assert.IsNotNull (ex.Message, "#4");
2907                                 Assert.AreEqual ("buffer", ex.ParamName, "#5");
2908                         } finally {
2909                                 s.Close ();
2910                         }
2911                 }
2912
2913                 [Test] // ReceiveFrom (Byte [], ref EndPoint)
2914                 public void ReceiveFrom1_RemoteEP_Null ()
2915                 {
2916                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2917                                 ProtocolType.Tcp);
2918
2919                         byte [] buffer = new byte [0];
2920                         EndPoint remoteEP = null;
2921                         try {
2922                                 s.ReceiveFrom (buffer, ref remoteEP);
2923                                 Assert.Fail ("#1");
2924                         } catch (ArgumentNullException ex) {
2925                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2926                                 Assert.IsNull (ex.InnerException, "#3");
2927                                 Assert.IsNotNull (ex.Message, "#4");
2928                                 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
2929                         } finally {
2930                                 s.Close ();
2931                         }
2932                 }
2933
2934                 [Test] // ReceiveFrom (Byte [], ref EndPoint)
2935                 public void ReceiveFrom1_Socket_Closed ()
2936                 {
2937                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2938                                 ProtocolType.Tcp);
2939                         s.Close ();
2940
2941                         EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
2942                         try {
2943                                 s.ReceiveFrom ((Byte []) null, ref remoteEP);
2944                                 Assert.Fail ("#1");
2945                         } catch (ObjectDisposedException ex) {
2946                                 // Cannot access a disposed object
2947                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2948                                 Assert.IsNull (ex.InnerException, "#3");
2949                                 Assert.IsNotNull (ex.Message, "#4");
2950                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2951                         }
2952                 }
2953
2954                 [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
2955                 public void ReceiveFrom2_Buffer_Null ()
2956                 {
2957                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2958                                 ProtocolType.Tcp);
2959
2960                         EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
2961                         try {
2962                                 s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP);
2963                                 Assert.Fail ("#1");
2964                         } catch (ArgumentNullException ex) {
2965                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2966                                 Assert.IsNull (ex.InnerException, "#3");
2967                                 Assert.IsNotNull (ex.Message, "#4");
2968                                 Assert.AreEqual ("buffer", ex.ParamName, "#5");
2969                         } finally {
2970                                 s.Close ();
2971                         }
2972                 }
2973
2974                 [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
2975                 public void ReceiveFrom2_RemoteEP_Null ()
2976                 {
2977                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2978                                 ProtocolType.Tcp);
2979
2980                         byte [] buffer = new byte [5];
2981                         EndPoint remoteEP = null;
2982                         try {
2983                                 s.ReceiveFrom (buffer, (SocketFlags) 666, ref remoteEP);
2984                                 Assert.Fail ("#1");
2985                         } catch (ArgumentNullException ex) {
2986                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2987                                 Assert.IsNull (ex.InnerException, "#3");
2988                                 Assert.IsNotNull (ex.Message, "#4");
2989                                 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
2990                         } finally {
2991                                 s.Close ();
2992                         }
2993                 }
2994
2995                 [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
2996                 public void ReceiveFrom2_Socket_Closed ()
2997                 {
2998                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2999                                 ProtocolType.Tcp);
3000                         s.Close ();
3001
3002                         EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
3003                         try {
3004                                 s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP);
3005                                 Assert.Fail ("#1");
3006                         } catch (ObjectDisposedException ex) {
3007                                 // Cannot access a disposed object
3008                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3009                                 Assert.IsNull (ex.InnerException, "#3");
3010                                 Assert.IsNotNull (ex.Message, "#4");
3011                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3012                         }
3013                 }
3014
3015                 [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
3016                 public void ReceiveFrom3_Buffer_Null ()
3017                 {
3018                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3019                                 ProtocolType.Tcp);
3020
3021                         EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
3022                         try {
3023                                 s.ReceiveFrom ((Byte []) null, -1, (SocketFlags) 666,
3024                                         ref remoteEP);
3025                                 Assert.Fail ("#1");
3026                         } catch (ArgumentNullException ex) {
3027                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3028                                 Assert.IsNull (ex.InnerException, "#3");
3029                                 Assert.IsNotNull (ex.Message, "#4");
3030                                 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3031                         } finally {
3032                                 s.Close ();
3033                         }
3034                 }
3035
3036                 [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
3037                 public void ReceiveFrom3_RemoteEP_Null ()
3038                 {
3039                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3040                                 ProtocolType.Tcp);
3041
3042                         byte [] buffer = new byte [5];
3043                         EndPoint remoteEP = null;
3044                         try {
3045                                 s.ReceiveFrom (buffer, -1, (SocketFlags) 666, ref remoteEP);
3046                                 Assert.Fail ("#1");
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 ("remoteEP", ex.ParamName, "#5");
3052                         } finally {
3053                                 s.Close ();
3054                         }
3055                 }
3056
3057                 [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
3058                 public void ReceiveFrom3_Size_OutOfRange ()
3059                 {
3060                         Socket s;
3061                         byte [] buffer = new byte [5];
3062                         EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
3063
3064                         // size negative
3065                         s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3066                                                         ProtocolType.Tcp);
3067                         try {
3068                                 s.ReceiveFrom (buffer, -1, (SocketFlags) 666, ref remoteEP);
3069                                 Assert.Fail ("#A1");
3070                         } catch (ArgumentOutOfRangeException ex) {
3071                                 // Specified argument was out of the range of valid values
3072                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
3073                                 Assert.IsNull (ex.InnerException, "#A3");
3074                                 Assert.IsNotNull (ex.Message, "#A4");
3075                                 Assert.AreEqual ("size", ex.ParamName, "#A5");
3076                         } finally {
3077                                 s.Close ();
3078                         }
3079
3080                         // size > buffer length
3081                         s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3082                                                         ProtocolType.Tcp);
3083                         try {
3084                                 s.ReceiveFrom (buffer, (buffer.Length + 1), (SocketFlags) 666,
3085                                         ref remoteEP);
3086                                 Assert.Fail ("#B1");
3087                         } catch (ArgumentOutOfRangeException ex) {
3088                                 // Specified argument was out of the range of valid values
3089                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
3090                                 Assert.IsNull (ex.InnerException, "#B3");
3091                                 Assert.IsNotNull (ex.Message, "#B4");
3092                                 Assert.AreEqual ("size", ex.ParamName, "#B5");
3093                         } finally {
3094                                 s.Close ();
3095                         }
3096                 }
3097
3098                 [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
3099                 public void ReceiveFrom3_Socket_Closed ()
3100                 {
3101                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3102                                 ProtocolType.Tcp);
3103                         s.Close ();
3104
3105                         EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
3106                         try {
3107                                 s.ReceiveFrom ((Byte []) null, -1, (SocketFlags) 666,
3108                                         ref remoteEP);
3109                                 Assert.Fail ("#1");
3110                         } catch (ObjectDisposedException ex) {
3111                                 // Cannot access a disposed object
3112                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3113                                 Assert.IsNull (ex.InnerException, "#3");
3114                                 Assert.IsNotNull (ex.Message, "#4");
3115                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3116                         }
3117                 }
3118
3119                 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
3120                 public void ReceiveFrom4_Buffer_Null ()
3121                 {
3122                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3123                                 ProtocolType.Tcp);
3124                         EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
3125
3126                         try {
3127                                 s.ReceiveFrom ((Byte []) null, -1, -1, (SocketFlags) 666,
3128                                         ref remoteEP);
3129                                 Assert.Fail ("#1");
3130                         } catch (ArgumentNullException ex) {
3131                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3132                                 Assert.IsNull (ex.InnerException, "#3");
3133                                 Assert.IsNotNull (ex.Message, "#4");
3134                                 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3135                         }
3136                 }
3137
3138                 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
3139                 public void ReceiveFrom4_Offset_OutOfRange ()
3140                 {
3141                         Socket s;
3142                         byte [] buffer = new byte [5];
3143                         EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
3144
3145                         // offset negative
3146                         s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3147                                                         ProtocolType.Tcp);
3148                         try {
3149                                 s.ReceiveFrom (buffer, -1, 0, (SocketFlags) 666,
3150                                         ref remoteEP);
3151                                 Assert.Fail ("#A1");
3152                         } catch (ArgumentOutOfRangeException ex) {
3153                                 // Specified argument was out of the range of valid values
3154                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
3155                                 Assert.IsNull (ex.InnerException, "#A3");
3156                                 Assert.IsNotNull (ex.Message, "#A4");
3157                                 Assert.AreEqual ("offset", ex.ParamName, "#A5");
3158                         } finally {
3159                                 s.Close ();
3160                         }
3161
3162                         // offset > buffer length
3163                         s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3164                                                         ProtocolType.Tcp);
3165                         try {
3166                                 s.ReceiveFrom (buffer, (buffer.Length + 1), 0, (SocketFlags) 666,
3167                                         ref remoteEP);
3168                                 Assert.Fail ("#B1");
3169                         } catch (ArgumentOutOfRangeException ex) {
3170                                 // Specified argument was out of the range of valid values
3171                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
3172                                 Assert.IsNull (ex.InnerException, "#B3");
3173                                 Assert.IsNotNull (ex.Message, "#B4");
3174                                 Assert.AreEqual ("offset", ex.ParamName, "#B5");
3175                         } finally {
3176                                 s.Close ();
3177                         }
3178                 }
3179
3180                 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, ref IPEndPoint)
3181                 public void ReceiveFrom4_RemoteEP_Null ()
3182                 {
3183                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3184                                 ProtocolType.Tcp);
3185                         byte [] buffer = new byte [5];
3186                         EndPoint remoteEP = null;
3187
3188                         try {
3189                                 s.ReceiveFrom (buffer, -1, -1, (SocketFlags) 666, ref remoteEP);
3190                                 Assert.Fail ("#1");
3191                         } catch (ArgumentNullException ex) {
3192                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3193                                 Assert.IsNull (ex.InnerException, "#3");
3194                                 Assert.IsNotNull (ex.Message, "#4");
3195                                 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
3196                         } finally {
3197                                 s.Close ();
3198                         }
3199                 }
3200
3201                 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
3202                 public void ReceiveFrom4_Size_OutOfRange ()
3203                 {
3204                         Socket s;
3205                         byte [] buffer = new byte [5];
3206                         EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
3207
3208                         // size negative
3209                         s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3210                                                         ProtocolType.Tcp);
3211                         try {
3212                                 s.ReceiveFrom (buffer, 0, -1, (SocketFlags) 666,
3213                                         ref remoteEP);
3214                                 Assert.Fail ("#A1");
3215                         } catch (ArgumentOutOfRangeException ex) {
3216                                 // Specified argument was out of the range of valid values
3217                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
3218                                 Assert.IsNull (ex.InnerException, "#A3");
3219                                 Assert.IsNotNull (ex.Message, "#A4");
3220                                 Assert.AreEqual ("size", ex.ParamName, "#A5");
3221                         } finally {
3222                                 s.Close ();
3223                         }
3224
3225                         // size > buffer length
3226                         s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3227                                                         ProtocolType.Tcp);
3228                         try {
3229                                 s.ReceiveFrom (buffer, 0, (buffer.Length + 1), (SocketFlags) 666,
3230                                         ref remoteEP);
3231                                 Assert.Fail ("#B1");
3232                         } catch (ArgumentOutOfRangeException ex) {
3233                                 // Specified argument was out of the range of valid values
3234                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
3235                                 Assert.IsNull (ex.InnerException, "#B3");
3236                                 Assert.IsNotNull (ex.Message, "#B4");
3237                                 Assert.AreEqual ("size", ex.ParamName, "#B5");
3238                         } finally {
3239                                 s.Close ();
3240                         }
3241
3242                         // offset + size > buffer length
3243                         s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3244                                                         ProtocolType.Tcp);
3245                         try {
3246                                 s.ReceiveFrom (buffer, 2, 4, (SocketFlags) 666, ref remoteEP);
3247                                 Assert.Fail ("#C1");
3248                         } catch (ArgumentOutOfRangeException ex) {
3249                                 // Specified argument was out of the range of valid values
3250                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
3251                                 Assert.IsNull (ex.InnerException, "#C3");
3252                                 Assert.IsNotNull (ex.Message, "#C4");
3253                                 Assert.AreEqual ("size", ex.ParamName, "#C5");
3254                         } finally {
3255                                 s.Close ();
3256                         }
3257                 }
3258
3259                 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, ref EndPoint)
3260                 public void ReceiveFrom4_Socket_Closed ()
3261                 {
3262                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3263                                 ProtocolType.Tcp);
3264                         s.Close ();
3265
3266                         byte [] buffer = new byte [5];
3267                         EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
3268                         try {
3269                                 s.ReceiveFrom (buffer, -1, -1, (SocketFlags) 666,
3270                                         ref remoteEP);
3271                                 Assert.Fail ("#1");
3272                         } catch (ObjectDisposedException ex) {
3273                                 // Cannot access a disposed object
3274                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3275                                 Assert.IsNull (ex.InnerException, "#3");
3276                                 Assert.IsNotNull (ex.Message, "#4");
3277                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3278                         }
3279                 }
3280
3281                 [Test]
3282                 public void ReceiveRemoteClosed ()
3283                 {
3284                         Socket sock = new Socket (AddressFamily.InterNetwork,
3285                                                   SocketType.Stream,
3286                                                   ProtocolType.Tcp);
3287                         sock.Bind (new IPEndPoint (IPAddress.Loopback, 1257));
3288                         sock.Listen (1);
3289                         
3290                         RRCReady.Reset ();
3291                         Thread client_thread = new Thread (new ThreadStart (RRCClientThread));
3292                         client_thread.Start ();
3293                         
3294                         Socket client = sock.Accept ();
3295                         NetworkStream stream = new NetworkStream (client);
3296                         stream.WriteByte (0x00);
3297                         stream.WriteByte (0x00);
3298                         client.Close ();
3299                         sock.Close ();
3300
3301                         RRCReady.WaitOne (1000, false);
3302                         Assert.IsTrue (RRCLastRead);
3303                 }
3304
3305                 //
3306                 // Test case for bug #471580
3307                 [Test]
3308                 public void UdpDoubleBind ()
3309                 {
3310                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
3311                         s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
3312                         
3313                         s.Bind (new IPEndPoint (IPAddress.Any, 12345));
3314                         
3315                         Socket ss = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
3316                         ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
3317                         
3318                         ss.Bind (new IPEndPoint (IPAddress.Any, 12345));
3319
3320                         // If we make it this far, we succeeded.
3321                         
3322                         ss.Close ();
3323                         s.Close ();
3324                 }
3325                 
3326 #if NET_2_0
3327                 [Test]
3328                 public void ConnectedProperty ()
3329                 {
3330                         TcpListener listener = new TcpListener (IPAddress.Loopback, 23456);
3331                         listener.Start();
3332
3333                         Socket client = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3334                         client.Connect (IPAddress.Loopback, 23456);
3335                         Socket server = listener.AcceptSocket ();
3336
3337                         try {
3338                                 server.EndSend(server.BeginSend (new byte[10], 0, 10, SocketFlags.None, null, null));
3339                                 client.Close ();
3340                                 try {
3341                                         server.EndReceive (server.BeginReceive (new byte[10], 0, 10, SocketFlags.None, null, null));
3342                                 } catch {
3343                                 }
3344                                 Assert.IsTrue (!client.Connected);
3345                                 Assert.IsTrue (!server.Connected);
3346                         } finally {
3347                                 listener.Stop ();
3348                                 client.Close ();
3349                                 server.Close ();
3350                         }
3351                 }
3352 #endif
3353
3354                 [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName)
3355                 public void GetSocketOption1_Socket_Closed ()
3356                 {
3357                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3358                         s.Close ();
3359                         try {
3360                                 s.GetSocketOption (0, 0);
3361                                 Assert.Fail ("#1");
3362                         } catch (ObjectDisposedException ex) {
3363                                 // Cannot access a disposed object
3364                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3365                                 Assert.IsNull (ex.InnerException, "#3");
3366                                 Assert.IsNotNull (ex.Message, "#4");
3367                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3368                         }
3369                 }
3370
3371                 [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3372                 public void GetSocketOption2_OptionValue_Null ()
3373                 {
3374                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3375                         try {
3376                                 s.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
3377                                         (byte []) null);
3378                                 Assert.Fail ("#1");
3379                                 } catch (SocketException ex) {
3380                                         // The system detected an invalid pointer address in attempting
3381                                         // to use a pointer argument in a call
3382                                         Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
3383                                         Assert.AreEqual (10014, ex.ErrorCode, "#3");
3384                                         Assert.IsNull (ex.InnerException, "#4");
3385                                         Assert.IsNotNull (ex.Message, "#5");
3386                                         Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
3387 #if NET_2_0
3388                                         Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
3389 #endif
3390                                 }
3391                 }
3392
3393                 [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3394                 public void GetSocketOption2_Socket_Closed ()
3395                 {
3396                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3397                         s.Close ();
3398                         try {
3399                                 s.GetSocketOption (0, 0, (byte []) null);
3400                                 Assert.Fail ("#1");
3401                         } catch (ObjectDisposedException ex) {
3402                                 // Cannot access a disposed object
3403                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3404                                 Assert.IsNull (ex.InnerException, "#3");
3405                                 Assert.IsNotNull (ex.Message, "#4");
3406                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3407                         }
3408                 }
3409
3410                 [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
3411                 public void GetSocketOption3_Socket_Closed ()
3412                 {
3413                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3414                         s.Close ();
3415                         try {
3416                                 s.GetSocketOption (0, 0, 0);
3417                                 Assert.Fail ("#1");
3418                         } catch (ObjectDisposedException ex) {
3419                                 // Cannot access a disposed object
3420                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3421                                 Assert.IsNull (ex.InnerException, "#3");
3422                                 Assert.IsNotNull (ex.Message, "#4");
3423                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3424                         }
3425                 }
3426
3427                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3428                 public void SetSocketOption1_DontLinger ()
3429                 {
3430                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3431                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
3432                                         new byte [] { 0x00 });
3433                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
3434                                         new byte [] { 0x01 });
3435                         }
3436                 }
3437
3438                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3439                 public void SetSocketOption1_DontLinger_Null ()
3440                 {
3441                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3442                                 try {
3443                                         s.SetSocketOption (SocketOptionLevel.Socket,
3444                                                 SocketOptionName.DontLinger, (byte []) null);
3445                                         Assert.Fail ("#1");
3446                                 } catch (SocketException ex) {
3447                                         // The system detected an invalid pointer address in attempting
3448                                         // to use a pointer argument in a call
3449                                         Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
3450                                         Assert.AreEqual (10014, ex.ErrorCode, "#3");
3451                                         Assert.IsNull (ex.InnerException, "#4");
3452                                         Assert.IsNotNull (ex.Message, "#5");
3453                                         Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
3454 #if NET_2_0
3455                                         Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
3456 #endif
3457                                 }
3458                         }
3459                 }
3460
3461                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3462                 public void SetSocketOption1_Linger_Null ()
3463                 {
3464                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3465                                 try {
3466                                         s.SetSocketOption (SocketOptionLevel.Socket,
3467                                                 SocketOptionName.DontLinger, (byte []) null);
3468                                         Assert.Fail ("#1");
3469                                 } catch (SocketException ex) {
3470                                         // The system detected an invalid pointer address in attempting
3471                                         // to use a pointer argument in a call
3472                                         Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
3473                                         Assert.AreEqual (10014, ex.ErrorCode, "#3");
3474                                         Assert.IsNull (ex.InnerException, "#4");
3475                                         Assert.IsNotNull (ex.Message, "#5");
3476                                         Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
3477 #if NET_2_0
3478                                         Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
3479 #endif
3480                                 }
3481                         }
3482                 }
3483
3484                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3485                 public void SetSocketOption1_Socket_Close ()
3486                 {
3487                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3488                         s.Close ();
3489                         try {
3490                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
3491                                         new byte [] { 0x00 });
3492                                 Assert.Fail ("#1");
3493                         } catch (ObjectDisposedException ex) {
3494                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3495                                 Assert.IsNull (ex.InnerException, "#3");
3496                                 Assert.IsNotNull (ex.Message, "#4");
3497                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3498                         }
3499                 }
3500
3501                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
3502                 public void SetSocketOption2_DontLinger ()
3503                 {
3504                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3505                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 0);
3506                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 5);
3507                         }
3508                 }
3509
3510                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
3511                 [Category ("NotWorking")]
3512                 public void SetSocketOption2_Linger ()
3513                 {
3514                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3515                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, 0);
3516                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, 5);
3517                         }
3518                 }
3519
3520                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
3521                 public void SetSocketOption2_Socket_Closed ()
3522                 {
3523                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3524                         s.Close ();
3525                         try {
3526                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 0);
3527                                 Assert.Fail ("#1");
3528                         } catch (ObjectDisposedException ex) {
3529                                 // Cannot access a disposed object
3530                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3531                                 Assert.IsNull (ex.InnerException, "#3");
3532                                 Assert.IsNotNull (ex.Message, "#4");
3533                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3534                         }
3535                 }
3536
3537                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3538                 public void SetSocketOption3_AddMembershipIPv4_IPv6MulticastOption ()
3539                 {
3540                         IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
3541
3542                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
3543                                 s.Bind (new IPEndPoint (IPAddress.Any, 1901));
3544                                 try {
3545                                         s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
3546                                                 new IPv6MulticastOption (mcast_addr));
3547                                         Assert.Fail ("#1");
3548                                 } catch (ArgumentException ex) {
3549                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
3550                                         Assert.IsNull (ex.InnerException, "#3");
3551                                         Assert.IsNotNull (ex.Message, "#4");
3552 #if NET_2_0
3553                                         // The specified value is not a valid 'MulticastOption'
3554                                         Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
3555                                         Assert.AreEqual ("optionValue", ex.ParamName, "#6");
3556 #else
3557                                         Assert.AreEqual ("optionValue", ex.Message, "#5");
3558                                         Assert.IsNull (ex.ParamName, "#6");
3559 #endif
3560                                 }
3561                         }
3562                 }
3563
3564                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3565                 public void SetSocketOption3_AddMembershipIPv4_MulticastOption ()
3566                 {
3567                         IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
3568
3569                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
3570                                 s.Bind (new IPEndPoint (IPAddress.Any, 1901));
3571                                 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
3572                                         new MulticastOption (mcast_addr));
3573                         }
3574                 }
3575
3576                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3577                 [Category ("NotWorking")]
3578                 public void SetSocketOption3_AddMembershipIPv4_Socket_NotBound ()
3579                 {
3580                         IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
3581
3582                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
3583                         try {
3584                                 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
3585                                         new MulticastOption (mcast_addr));
3586                                 Assert.Fail ("#1");
3587                         } catch (SocketException ex) {
3588                                 // An invalid argument was supplied
3589                                 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
3590                                 Assert.AreEqual (10022, ex.ErrorCode, "#3");
3591                                 Assert.IsNull (ex.InnerException, "#4");
3592                                 Assert.IsNotNull (ex.Message, "#5");
3593                                 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
3594 #if NET_2_0
3595                                 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
3596 #endif
3597                         } finally {
3598                                 s.Close ();
3599                         }
3600                 }
3601
3602                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3603                 public void SetSocketOption3_AddMembershipIPv6_IPv6MulticastOption ()
3604                 {
3605 #if NET_2_0
3606                         if (!Socket.OSSupportsIPv6)
3607 #else
3608                         if (!Socket.SupportsIPv6)
3609 #endif
3610                                 Assert.Ignore ("IPv6 not enabled.");
3611
3612                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
3613
3614                         using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
3615                                 s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
3616                                 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
3617                                         new IPv6MulticastOption (mcast_addr));
3618                         }
3619                 }
3620
3621                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3622                 public void SetSocketOption3_AddMembershipIPv6_MulticastOption ()
3623                 {
3624 #if NET_2_0
3625                         if (!Socket.OSSupportsIPv6)
3626 #else
3627                         if (!Socket.SupportsIPv6)
3628 #endif
3629                                 Assert.Ignore ("IPv6 not enabled.");
3630
3631                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
3632
3633                         using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
3634                                 s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
3635                                 try {
3636                                         s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
3637                                                 new MulticastOption (mcast_addr));
3638                                         Assert.Fail ("#1");
3639                                 } catch (ArgumentException ex) {
3640                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
3641                                         Assert.IsNull (ex.InnerException, "#3");
3642                                         Assert.IsNotNull (ex.Message, "#4");
3643 #if NET_2_0
3644                                         // The specified value is not a valid 'IPv6MulticastOption'
3645                                         Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
3646                                         Assert.AreEqual ("optionValue", ex.ParamName, "#6");
3647 #else
3648                                         Assert.AreEqual ("optionValue", ex.Message, "#5");
3649                                         Assert.IsNull (ex.ParamName, "#6");
3650 #endif
3651                                 }
3652                         }
3653                 }
3654
3655                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3656                 [Category ("NotWorking")]
3657                 public void SetSocketOption3_AddMembershipIPv6_Socket_NotBound ()
3658                 {
3659                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
3660
3661                         Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
3662                         try {
3663                                 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
3664                                         new IPv6MulticastOption (mcast_addr));
3665                                 Assert.Fail ("#1");
3666                         } catch (SocketException ex) {
3667                                 // An invalid argument was supplied
3668                                 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
3669                                 Assert.AreEqual (10022, ex.ErrorCode, "#3");
3670                                 Assert.IsNull (ex.InnerException, "#4");
3671                                 Assert.IsNotNull (ex.Message, "#5");
3672                                 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
3673 #if NET_2_0
3674                                 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
3675 #endif
3676                         } finally {
3677                                 s.Close ();
3678                         }
3679                 }
3680
3681                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3682                 public void SetSocketOption3_DontLinger_Boolean ()
3683                 {
3684                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3685                                 try {
3686                                         s.SetSocketOption (SocketOptionLevel.Socket,
3687                                                 SocketOptionName.DontLinger, (object) false);
3688                                         Assert.Fail ("#1");
3689                                 } catch (ArgumentException ex) {
3690                                         // The specified value is not valid
3691                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
3692                                         Assert.IsNull (ex.InnerException, "#3");
3693 #if NET_2_0
3694                                         Assert.IsNotNull (ex.Message, "#4");
3695                                         Assert.AreEqual ("optionValue", ex.ParamName, "#5");
3696 #else
3697                                         Assert.AreEqual ("optionValue", ex.Message, "#4");
3698                                         Assert.IsNull (ex.ParamName, "#5");
3699 #endif
3700                                 }
3701                         }
3702                 }
3703
3704                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3705                 public void SetSocketOption3_DontLinger_Int32 ()
3706                 {
3707                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3708                                 try {
3709                                         s.SetSocketOption (SocketOptionLevel.Socket,
3710                                                 SocketOptionName.DontLinger, (object) 0);
3711                                         Assert.Fail ("#1");
3712                                 } catch (ArgumentException ex) {
3713                                         // The specified value is not valid
3714                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
3715                                         Assert.IsNull (ex.InnerException, "#3");
3716 #if NET_2_0
3717                                         Assert.IsNotNull (ex.Message, "#4");
3718                                         Assert.AreEqual ("optionValue", ex.ParamName, "#5");
3719 #else
3720                                         Assert.AreEqual ("optionValue", ex.Message, "#4");
3721                                         Assert.IsNull (ex.ParamName, "#5");
3722 #endif
3723                                 }
3724                         }
3725                 }
3726
3727                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3728                 public void SetSocketOption3_DontLinger_LingerOption ()
3729                 {
3730                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3731                                 try {
3732                                         s.SetSocketOption (SocketOptionLevel.Socket,
3733                                                 SocketOptionName.DontLinger, new LingerOption (true, 1000));
3734                                         Assert.Fail ("#1");
3735                                 } catch (ArgumentException ex) {
3736                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
3737                                         Assert.IsNull (ex.InnerException, "#3");
3738 #if NET_2_0
3739                                         // The specified value is not valid
3740                                         Assert.IsNotNull (ex.Message, "#4");
3741                                         Assert.AreEqual ("optionValue", ex.ParamName, "#5");
3742 #else
3743                                         Assert.AreEqual ("optionValue", ex.Message, "#4");
3744                                         Assert.IsNull (ex.ParamName, "#5");
3745 #endif
3746                                 }
3747                         }
3748                 }
3749
3750                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3751                 public void SetSocketOption3_Linger_Boolean ()
3752                 {
3753                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3754                                 try {
3755                                         s.SetSocketOption (SocketOptionLevel.Socket,
3756                                                 SocketOptionName.Linger, (object) false);
3757                                         Assert.Fail ("#1");
3758                                 } catch (ArgumentException ex) {
3759                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
3760                                         Assert.IsNull (ex.InnerException, "#3");
3761 #if NET_2_0
3762                                         // The specified value is not valid
3763                                         Assert.IsNotNull (ex.Message, "#4");
3764                                         Assert.AreEqual ("optionValue", ex.ParamName, "#5");
3765 #else
3766                                         Assert.AreEqual ("optionValue", ex.Message, "#4");
3767                                         Assert.IsNull (ex.ParamName, "#5");
3768 #endif
3769                                 }
3770                         }
3771                 }
3772
3773                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3774                 public void SetSocketOption3_Linger_Int32 ()
3775                 {
3776                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3777                                 try {
3778                                         s.SetSocketOption (SocketOptionLevel.Socket,
3779                                                 SocketOptionName.Linger, (object) 0);
3780                                         Assert.Fail ("#1");
3781                                 } catch (ArgumentException ex) {
3782                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
3783                                         Assert.IsNull (ex.InnerException, "#3");
3784 #if NET_2_0
3785                                         // The specified value is not valid
3786                                         Assert.IsNotNull (ex.Message, "#4");
3787                                         Assert.AreEqual ("optionValue", ex.ParamName, "#5");
3788 #else
3789                                         Assert.AreEqual ("optionValue", ex.Message, "#4");
3790                                         Assert.IsNull (ex.ParamName, "#5");
3791 #endif
3792                                 }
3793                         }
3794                 }
3795
3796                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3797                 public void SetSocketOption3_Linger_LingerOption ()
3798                 {
3799                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3800                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
3801                                         new LingerOption (false, 0));
3802                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
3803                                         new LingerOption (true, 0));
3804                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
3805                                         new LingerOption (false, 1000));
3806                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
3807                                         new LingerOption (true, 1000));
3808                         }
3809                 }
3810
3811                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3812                 public void SetSocketOption3_DropMembershipIPv4_IPv6MulticastOption ()
3813                 {
3814                         IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
3815
3816                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
3817                                 s.Bind (new IPEndPoint (IPAddress.Any, 1901));
3818                                 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
3819                                         new MulticastOption (mcast_addr));
3820                                 try {
3821                                         s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
3822                                                 new IPv6MulticastOption (mcast_addr));
3823                                         Assert.Fail ("#1");
3824                                 } catch (ArgumentException ex) {
3825                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
3826                                         Assert.IsNull (ex.InnerException, "#3");
3827                                         Assert.IsNotNull (ex.Message, "#4");
3828 #if NET_2_0
3829                                         // The specified value is not a valid 'MulticastOption'
3830                                         Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
3831                                         Assert.AreEqual ("optionValue", ex.ParamName, "#6");
3832 #else
3833                                         Assert.AreEqual ("optionValue", ex.Message, "#5");
3834                                         Assert.IsNull (ex.ParamName, "#6");
3835 #endif
3836                                 }
3837                         }
3838                 }
3839
3840                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3841                 public void SetSocketOption3_DropMembershipIPv4_MulticastOption ()
3842                 {
3843                         IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
3844
3845                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
3846                                 MulticastOption option = new MulticastOption (mcast_addr);
3847
3848                                 s.Bind (new IPEndPoint (IPAddress.Any, 1901));
3849                                 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
3850                                         option);
3851                                 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
3852                                         option);
3853                         }
3854                 }
3855
3856                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3857                 [Category ("NotWorking")]
3858                 public void SetSocketOption3_DropMembershipIPv4_Socket_NotBound ()
3859                 {
3860                         IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
3861
3862                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
3863                         try {
3864                                 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
3865                                         new MulticastOption (mcast_addr));
3866                                 Assert.Fail ("#1");
3867                         } catch (SocketException ex) {
3868                                 // An invalid argument was supplied
3869                                 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
3870                                 Assert.AreEqual (10022, ex.ErrorCode, "#3");
3871                                 Assert.IsNull (ex.InnerException, "#4");
3872                                 Assert.IsNotNull (ex.Message, "#5");
3873                                 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
3874 #if NET_2_0
3875                                 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
3876 #endif
3877                         } finally {
3878                                 s.Close ();
3879                         }
3880                 }
3881
3882                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3883                 public void SetSocketOption3_DropMembershipIPv6_IPv6MulticastOption ()
3884                 {
3885 #if NET_2_0
3886                         if (!Socket.OSSupportsIPv6)
3887 #else
3888                         if (!Socket.SupportsIPv6)
3889 #endif
3890                                 Assert.Ignore ("IPv6 not enabled.");
3891
3892                         using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
3893                                 IPv6MulticastOption option = new IPv6MulticastOption (
3894                                         IPAddress.Parse ("ff02::1"));
3895
3896                                 s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
3897                                 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
3898                                         option);
3899                                 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
3900                                         option);
3901                         }
3902                 }
3903
3904                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3905                 public void SetSocketOption3_DropMembershipIPv6_MulticastOption ()
3906                 {
3907 #if NET_2_0
3908                         if (!Socket.OSSupportsIPv6)
3909 #else
3910                         if (!Socket.SupportsIPv6)
3911 #endif
3912                                 Assert.Ignore ("IPv6 not enabled.");
3913
3914                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
3915
3916                         using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
3917                                 s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
3918                                 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
3919                                         new IPv6MulticastOption (mcast_addr));
3920                                 try {
3921                                         s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
3922                                                 new MulticastOption (mcast_addr));
3923                                         Assert.Fail ("#1");
3924                                 } catch (ArgumentException ex) {
3925                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
3926                                         Assert.IsNull (ex.InnerException, "#3");
3927                                         Assert.IsNotNull (ex.Message, "#4");
3928 #if NET_2_0
3929                                         // The specified value is not a valid 'IPv6MulticastOption'
3930                                         Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
3931                                         Assert.AreEqual ("optionValue", ex.ParamName, "#6");
3932 #else
3933                                         Assert.AreEqual ("optionValue", ex.Message, "#5");
3934                                         Assert.IsNull (ex.ParamName, "#6");
3935 #endif
3936                                 }
3937                         }
3938                 }
3939
3940                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3941                 [Category ("NotWorking")]
3942                 public void SetSocketOption3_DropMembershipIPv6_Socket_NotBound ()
3943                 {
3944                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
3945
3946                         Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
3947                         try {
3948                                 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
3949                                         new IPv6MulticastOption (mcast_addr));
3950                                 Assert.Fail ("#1");
3951                         } catch (SocketException ex) {
3952                                 // An invalid argument was supplied
3953                                 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
3954                                 Assert.AreEqual (10022, ex.ErrorCode, "#3");
3955                                 Assert.IsNull (ex.InnerException, "#4");
3956                                 Assert.IsNotNull (ex.Message, "#5");
3957                                 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
3958 #if NET_2_0
3959                                 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
3960 #endif
3961                         } finally {
3962                                 s.Close ();
3963                         }
3964                 }
3965
3966                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3967                 public void SetSocketOption3_OptionValue_Null ()
3968                 {
3969                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3970                                 try {
3971                                         s.SetSocketOption (SocketOptionLevel.Socket,
3972                                                 SocketOptionName.Linger, (object) null);
3973                                         Assert.Fail ("#1");
3974                                 } catch (ArgumentNullException ex) {
3975                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3976                                         Assert.IsNull (ex.InnerException, "#3");
3977                                         Assert.IsNotNull (ex.Message, "#4");
3978                                         Assert.AreEqual ("optionValue", ex.ParamName, "#5");
3979                                 }
3980                         }
3981                 }
3982
3983                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3984                 public void SetSocketOption3_Socket_Closed ()
3985                 {
3986                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3987                         s.Close ();
3988                         try {
3989                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
3990                                         new LingerOption (false, 0));
3991                                 Assert.Fail ("#1");
3992                         } catch (ObjectDisposedException ex) {
3993                                 // Cannot access a disposed object
3994                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3995                                 Assert.IsNull (ex.InnerException, "#3");
3996                                 Assert.IsNotNull (ex.Message, "#4");
3997                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3998                         }
3999                 }
4000         }
4001 }