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