fixed tests
[mono.git] / mcs / class / System / Test / System.Net.Sockets / SocketTest.jvm.cs
1 // System.Net.Sockets.SocketTest.cs
2 //
3 // Authors:
4 //    Brad Fitzpatrick (brad@danga.com)
5 //    Gonzalo Paniagua Javier (gonzalo@novell.com)
6 //
7 // (C) Copyright 2003 Brad Fitzpatrick
8 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
9 //
10
11 using System;
12 using System.Collections;
13 using System.Threading;
14 using System.Net;
15 using System.Net.Sockets;
16 using NUnit.Framework;
17
18 namespace MonoTests.System.Net.Sockets
19 {
20         [TestFixture]
21         public class SocketTest
22         {
23                 // note: also used in SocketCas tests
24                 public const string BogusAddress = "192.168.244.244";
25                 public const int BogusPort = 23483;
26
27                 [Test]
28                 public void ConnectIPAddressAny ()
29                 {
30                         IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
31 #if !TARGET_JVM 
32 //udp sockets are not supported
33                         try {
34                                 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
35                                         s.Connect (ep);
36                                         s.Close ();
37                                 }
38                                 Assert.Fail ("#1");
39                         } catch (SocketException ex) {
40                                 Assert.AreEqual (10049, ex.ErrorCode, "#2");
41                         }
42 #endif                  
43
44                         try {
45                                 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
46                                         s.Connect (ep);
47                                         s.Close ();
48                                 }
49                                 Assert.Fail ("#3");
50                         } catch (SocketException ex) {
51                                 Assert.AreEqual (10049, ex.ErrorCode, "#4");
52                         }
53                 }
54
55                 [Test]
56                 [Ignore ("Bug #75158")]
57                 public void IncompatibleAddress ()
58                 {
59                         IPEndPoint epIPv6 = new IPEndPoint (IPAddress.IPv6Any,
60                                                                 0);
61
62                         try {
63                                 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)) {
64                                         s.Connect (epIPv6);
65                                         s.Close ();
66                                 }
67                                 Assert.Fail ("#1");
68                         } catch (SocketException ex) {
69 #if !NET_2_0
70                                 // invalid argument
71                                 int expectedError = 10022;
72 #else
73                                 // address incompatible with protocol
74                                 int expectedError = 10047;
75 #endif
76                                 Assert.AreEqual (expectedError, ex.ErrorCode,
77                                                 "#2");
78                         }
79                 }
80
81                 [Test]
82                 [Category ("InetAccess")]
83                 public void EndConnect ()
84                 {
85                     IPAddress ipOne = IPAddress.Parse (BogusAddress);
86                     IPEndPoint ipEP = new IPEndPoint (ipOne, BogusPort);
87                     Socket sock = new Socket (ipEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
88                     IAsyncResult ar = sock.BeginConnect (ipEP, null, null);
89                     bool gotException = false;
90
91                     try {
92                         sock.EndConnect (ar);  // should raise an exception because connect was bogus
93                     } catch {
94                         gotException = true;
95                     }
96
97                     Assertion.AssertEquals ("A01", gotException, true);
98                 }
99
100                 [Test]
101                 [ExpectedException (typeof (ArgumentNullException))]
102                 public void SelectEmpty ()
103                 {
104                         ArrayList list = new ArrayList ();
105                         Socket.Select (list, list, list, 1000);
106                 }
107                 
108                 private bool BlockingConnect (bool block)
109                 {
110                         IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 1234);
111                         Socket server = new Socket(AddressFamily.InterNetwork,
112                                                    SocketType.Stream,
113                                                    ProtocolType.Tcp);
114                         server.Bind(ep);
115                         server.Blocking=block;
116
117                         server.Listen(0);
118
119                         Socket conn = new Socket (AddressFamily.InterNetwork,
120                                                   SocketType.Stream,
121                                                   ProtocolType.Tcp);
122                         conn.Connect (ep);
123
124                         Socket client = server.Accept();
125                         bool client_block = client.Blocking;
126
127                         client.Close();
128                         conn.Close();
129                         server.Close();
130                         
131                         return(client_block);
132                 }
133
134                 [Test]
135                 public void AcceptBlockingStatus()
136                 {
137                         bool block;
138
139                         block = BlockingConnect(true);
140                         Assertion.AssertEquals ("BlockingStatus01",
141                                                 block, true);
142
143                         block = BlockingConnect(false);
144                         Assertion.AssertEquals ("BlockingStatus02",
145                                                 block, false);
146                 }
147
148                 static bool CFAConnected = false;
149                 static ManualResetEvent CFACalledBack;
150                 
151                 private static void CFACallback (IAsyncResult asyncResult)
152                 {
153                         Socket sock = (Socket)asyncResult.AsyncState;
154                         CFAConnected = sock.Connected;
155                         
156                         if (sock.Connected) {
157                                 sock.EndConnect (asyncResult);
158                         }
159
160                         CFACalledBack.Set ();
161                 }
162
163                 [Test]
164                 public void ConnectFailAsync ()
165                 {
166                         Socket sock = new Socket (AddressFamily.InterNetwork,
167                                                   SocketType.Stream,
168                                                   ProtocolType.Tcp);
169                         sock.Blocking = false;
170                         CFACalledBack = new ManualResetEvent (false);
171                         CFACalledBack.Reset ();
172
173                         /* Need a port that is not being used for
174                          * anything...
175                          */
176                         sock.BeginConnect (new IPEndPoint (IPAddress.Loopback,
177                                                            114),
178                                            new AsyncCallback (CFACallback),
179                                            sock);
180                         CFACalledBack.WaitOne ();
181
182                         Assertion.AssertEquals ("ConnectFail", CFAConnected,
183                                                 false);
184                 }
185                 
186 #if !TARGET_JVM
187                 [Test]
188 #if !NET_2_0
189                 [ExpectedException (typeof (ArgumentException))]
190 #endif
191                 public void SetSocketOptionBoolean ()
192                 {
193                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1);
194                         Socket sock = new Socket (ep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
195                         try {
196                                 sock.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
197                         } finally {
198                                 sock.Close ();
199                         }
200                 }
201 #endif
202                 [Test]
203 #if TARGET_JVM
204                 [Ignore ("NMA")]
205 #endif          
206                 public void TestSelect1 ()
207                 {
208                         Socket srv = CreateServer ();
209                         ClientSocket clnt = new ClientSocket (srv.LocalEndPoint);
210                         Thread th = new Thread (new ThreadStart (clnt.ConnectSleepClose));
211                         Socket acc = null;
212                         try {
213                                 th.Start ();
214                                 acc = srv.Accept ();
215                                 clnt.Write ();
216                                 ArrayList list = new ArrayList ();
217                                 ArrayList empty = new ArrayList ();
218                                 list.Add (acc);
219                                 Socket.Select (list, empty, empty, 100);
220                                 Assertion.AssertEquals ("#01", 0, empty.Count);
221                                 Assertion.AssertEquals ("#02", 1, list.Count);
222                                 Socket.Select (empty, list, empty, 100);
223                                 Assertion.AssertEquals ("#03", 0, empty.Count);
224                                 Assertion.AssertEquals ("#04", 1, list.Count);
225                                 Socket.Select (list, empty, empty, -1);
226                                 Assertion.AssertEquals ("#05", 0, empty.Count);
227                                 Assertion.AssertEquals ("#06", 1, list.Count);
228                         } finally {
229                                 if (acc != null)
230                                         acc.Close ();
231                                 srv.Close ();
232                         }
233                 }
234
235                 static Socket CreateServer ()
236                 {
237                         Socket sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
238                         sock.Bind (new IPEndPoint (IPAddress.Loopback, 0));
239                         sock.Listen (1);
240                         return sock;
241                 }
242
243                 class ClientSocket {
244                         Socket sock;
245                         EndPoint ep;
246
247                         public ClientSocket (EndPoint ep)
248                         {
249                                 this.ep = ep;
250                                 sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
251                         }
252
253                         public void ConnectSleepClose ()
254                         {
255                                 sock.Connect (ep);
256                                 Thread.Sleep (2000);
257                                 sock.Close ();
258                         }
259
260                         public void Write ()
261                         {
262                                 byte [] b = new byte [10];
263                                 sock.Send (b);
264                         }
265                 }
266
267                 byte[] buf = new byte[100];
268
269                 [Test]
270                 [ExpectedException (typeof (ObjectDisposedException))]
271                 public void Disposed1 ()
272                 {
273 #if TARGET_JVM
274             //UDP sockets are not supported in GH
275             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
276 #else
277             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
278 #endif
279             EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
280                         s.Close();
281
282                         s.ReceiveFrom (buf, ref ep);
283                 }
284
285                 [Test]
286                 [ExpectedException (typeof (ObjectDisposedException))]
287                 public void Disposed2 ()
288                 {
289 #if TARGET_JVM
290             //UDP sockets are not supported in GH
291             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
292 #else
293             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
294 #endif
295                         s.Close();
296
297                         s.Blocking = true;
298                 }
299
300                 [Test]
301                 [ExpectedException (typeof (ObjectDisposedException))]
302                 public void Disposed3 ()
303                 {
304 #if TARGET_JVM 
305             //UDP sockets are not supported in GH
306             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
307 #else
308             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
309 #endif
310                         s.Close();
311
312                         s.GetSocketOption (0, 0);
313                 }
314
315                 [Test]
316                 [ExpectedException (typeof (ObjectDisposedException))]
317                 public void Disposed4 ()
318                 {
319 #if TARGET_JVM 
320             //UDP sockets are not supported in GH
321             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
322 #else
323             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
324 #endif
325                         s.Close();
326
327                         s.GetSocketOption (0, 0, null);
328                 }
329
330                 [Test]
331                 [ExpectedException (typeof (ObjectDisposedException))]
332                 public void Disposed5 ()
333                 {
334 #if TARGET_JVM 
335             //UDP sockets are not supported in GH
336             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
337 #else
338                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
339 #endif
340                         s.Close();
341
342                         s.GetSocketOption (0, 0, 0);
343                 }
344
345                 [Test]
346                 [ExpectedException (typeof (ObjectDisposedException))]
347                 public void Disposed6 ()
348                 {
349 #if TARGET_JVM 
350             //UDP sockets are not supported in GH
351             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
352 #else
353             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
354 #endif
355                         s.Close();
356
357                         s.Listen (5);
358                 }
359
360                 [Test]
361                 [ExpectedException (typeof (ObjectDisposedException))]
362                 public void Disposed7 ()
363                 {
364 #if TARGET_JVM 
365             //UDP sockets are not supported in GH
366             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
367 #else
368             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
369 #endif
370                         s.Close();
371
372                         s.Poll (100, 0);
373                 }
374
375                 [Test]
376                 [ExpectedException (typeof (ObjectDisposedException))]
377                 public void Disposed8 ()
378                 {
379 #if TARGET_JVM 
380             //UDP sockets are not supported in GH
381             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
382 #else
383             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
384 #endif
385                         s.Close();
386
387                         s.Receive (buf);
388                 }
389
390                 [Test]
391                 [ExpectedException (typeof (ObjectDisposedException))]
392                 public void Disposed9 ()
393                 {
394 #if TARGET_JVM 
395             //UDP sockets are not supported in GH
396             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
397 #else
398             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
399 #endif
400                         s.Close();
401
402                         s.Receive (buf, 0);
403                 }
404
405                 [Test]
406                 [ExpectedException (typeof (ObjectDisposedException))]
407                 public void Disposed10 ()
408                 {
409 #if TARGET_JVM 
410             //UDP sockets are not supported in GH
411             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
412 #else
413             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
414 #endif
415                         s.Close();
416
417                         s.Receive (buf, 10, 0);
418                 }
419
420                 [Test]
421                 [ExpectedException (typeof (ObjectDisposedException))]
422                 public void Disposed11 ()
423                 {
424 #if TARGET_JVM 
425             //UDP sockets are not supported in GH
426             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
427 #else
428             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
429 #endif
430                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
431                         s.Close();
432
433                         s.Receive (buf, 0, 10, 0);
434                 }
435
436                 [Test]
437                 [ExpectedException (typeof (ObjectDisposedException))]
438                 public void Disposed12 ()
439                 {
440 #if TARGET_JVM 
441             //UDP sockets are not supported in GH
442             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
443 #else
444             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
445 #endif
446                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
447                         s.Close();
448
449                         s.ReceiveFrom (buf, 0, ref ep);
450                 }
451
452                 [Test]
453                 [ExpectedException (typeof (ObjectDisposedException))]
454                 public void Disposed13 ()
455                 {
456 #if TARGET_JVM 
457             //UDP sockets are not supported in GH
458             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
459 #else
460             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
461 #endif
462                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
463                         s.Close();
464
465                         s.ReceiveFrom (buf, 10, 0, ref ep);
466                 }
467
468                 [Test]
469                 [ExpectedException (typeof (ObjectDisposedException))]
470                 public void Disposed14 ()
471                 {
472 #if TARGET_JVM 
473             //UDP sockets are not supported in GH
474             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
475 #else
476             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
477 #endif
478                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
479                         s.Close();
480
481                         s.ReceiveFrom (buf, 0, 10, 0, ref ep);
482                 }
483
484                 [Test]
485                 [ExpectedException (typeof (ObjectDisposedException))]
486                 public void Disposed15 ()
487                 {
488 #if TARGET_JVM 
489             //UDP sockets are not supported in GH
490             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
491 #else
492             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
493 #endif
494                         s.Close();
495
496                         s.Send (buf);
497                 }
498
499                 [Test]
500                 [ExpectedException (typeof (ObjectDisposedException))]
501                 public void Disposed16 ()
502                 {
503 #if TARGET_JVM 
504             //UDP sockets are not supported in GH
505             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
506 #else
507             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
508 #endif
509                         s.Close();
510
511                         s.Send (buf, 0);
512                 }
513
514                 [Test]
515                 [ExpectedException (typeof (ObjectDisposedException))]
516                 public void Disposed17 ()
517                 {
518 #if TARGET_JVM 
519             //UDP sockets are not supported in GH
520             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
521 #else
522             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
523 #endif
524                         s.Close();
525
526                         s.Send (buf, 10, 0);
527                 }
528
529                 [Test]
530                 [ExpectedException (typeof (ObjectDisposedException))]
531                 public void Disposed18 ()
532                 {
533 #if TARGET_JVM 
534             //UDP sockets are not supported in GH
535             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
536 #else
537             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
538 #endif
539                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
540                         s.Close();
541
542                         s.Send (buf, 0, 10, 0);
543                 }
544
545                 [Test]
546                 [ExpectedException (typeof (ObjectDisposedException))]
547                 public void Disposed19 ()
548                 {
549 #if TARGET_JVM 
550             //UDP sockets are not supported in GH
551             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
552 #else
553             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
554 #endif
555                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
556                         s.Close();
557
558                         s.SendTo (buf, 0, ep);
559                 }
560
561                 [Test]
562                 [ExpectedException (typeof (ObjectDisposedException))]
563                 public void Disposed20 ()
564                 {
565 #if TARGET_JVM 
566             //UDP sockets are not supported in GH
567             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
568 #else
569             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
570 #endif
571                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
572                         s.Close();
573
574                         s.SendTo (buf, 10, 0, ep);
575                 }
576
577                 [Test]
578                 [ExpectedException (typeof (ObjectDisposedException))]
579                 public void Disposed21 ()
580                 {
581 #if TARGET_JVM 
582             //UDP sockets are not supported in GH
583             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
584 #else
585             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
586 #endif
587             EndPoint ep = new IPEndPoint(IPAddress.Any, 31337);
588             s.Close();
589
590             s.SendTo(buf, 0, 10, 0, ep);
591             
592                 }
593
594                 [Test]
595                 [ExpectedException (typeof (ObjectDisposedException))]
596                 public void Disposed22 ()
597                 {
598 #if TARGET_JVM 
599             //UDP sockets are not supported in GH
600             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
601 #else
602             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
603 #endif
604                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
605                         s.Close();
606
607                         s.SendTo (buf, ep);
608                 }
609
610                 [Test]
611                 [ExpectedException (typeof (ObjectDisposedException))]
612                 public void Disposed23 ()
613                 {
614 #if TARGET_JVM 
615             //UDP sockets are not supported in GH
616             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
617 #else
618             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
619 #endif
620                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
621                         s.Close();
622
623                         s.Shutdown (0);
624                 }
625
626                 static ManualResetEvent SocketError_event = new ManualResetEvent (false);
627
628                 private static void SocketError_callback (IAsyncResult ar)
629                 {
630                         Socket sock = (Socket)ar.AsyncState;
631                         
632                         if(sock.Connected) {
633                                 sock.EndConnect (ar);
634                         }
635
636                         SocketError_event.Set ();
637                 }
638
639                 [Test]
640 #if TARGET_JVM
641                 [Ignore ("NMA")]
642 #endif          
643                 public void SocketError ()
644                 {
645                         Socket sock = new Socket (AddressFamily.InterNetwork,
646                                                   SocketType.Stream,
647                                                   ProtocolType.Tcp);
648                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
649                                                         BogusPort);
650                         
651                         SocketError_event.Reset ();
652
653                         sock.Blocking = false;
654                         sock.BeginConnect (ep, SocketError_callback,
655                                            sock);
656
657                         if (SocketError_event.WaitOne (2000, false) == false) {
658                                 Assert.Fail ("SocketError wait timed out");
659                         }
660
661                         Assertion.AssertEquals ("SocketError #1", false,
662                                                 sock.Connected);
663
664                         int error;
665
666                         error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
667                         Assertion.AssertEquals ("SocketError #2", 10061,
668                                                 error);
669
670                         error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
671                         Assertion.AssertEquals ("SocketError #3", 10061,
672                                                 error);
673
674                         sock.Close ();
675                 }
676                 
677
678 #if NET_2_0
679                 [Test]
680                 public void SocketInformationCtor ()
681                 {
682                 }
683                 
684                 [Test]
685 #if TARGET_JVM
686         [Ignore ("Socket.DontFragment property is not supported")]
687 #endif
688         public void DontFragmentDefaultTcp ()
689                 {
690                         Socket sock = new Socket (AddressFamily.InterNetwork,
691                                                   SocketType.Stream,
692                                                   ProtocolType.Tcp);
693                         
694                         Assertion.AssertEquals ("DontFragmentDefaultTcp",
695                                                 false, sock.DontFragment);
696
697                         sock.Close ();
698                 }
699
700                 [Test]
701 #if TARGET_JVM
702         [Ignore("Socket.DontFragment property is not supported")]
703 #endif
704                 public void DontFragmentChangeTcp ()
705                 {
706                         Socket sock = new Socket (AddressFamily.InterNetwork,
707                                                   SocketType.Stream,
708                                                   ProtocolType.Tcp);
709                         
710                         sock.DontFragment = true;
711                         
712                         Assertion.AssertEquals ("DontFragmentChangeTcp",
713                                                 true, sock.DontFragment);
714
715                         sock.Close ();
716                 }
717                 
718                 [Test]
719 #if TARGET_JVM
720         [Ignore("Socket.DontFragment property is not supported")]
721 #endif
722                 public void DontFragmentDefaultUdp ()
723                 {
724                         Socket sock = new Socket (AddressFamily.InterNetwork,
725                                                   SocketType.Dgram,
726                                                   ProtocolType.Udp);
727                         
728                         Assertion.AssertEquals ("DontFragmentDefaultUdp",
729                                                 false, sock.DontFragment);
730
731                         sock.Close ();
732                 }
733
734                 [Test]
735 #if TARGET_JVM
736         [Ignore("Socket.DontFragment property is not supported")]
737 #endif
738                 public void DontFragmentChangeUdp ()
739                 {
740                         Socket sock = new Socket (AddressFamily.InterNetwork,
741                                                   SocketType.Dgram,
742                                                   ProtocolType.Udp);
743                         
744                         sock.DontFragment = true;
745                         
746                         Assertion.AssertEquals ("DontFragmentChangeUdp",
747                                                 true, sock.DontFragment);
748
749                         sock.Close ();
750                 }
751
752                 [Test]
753                 [ExpectedException (typeof(ObjectDisposedException))]
754 #if TARGET_JVM
755         [Ignore("Socket.DontFragment property is not supported")]
756 #endif
757                 public void DontFragmentClosed ()
758                 {
759                         Socket sock = new Socket (AddressFamily.InterNetwork,
760                                                   SocketType.Stream,
761                                                   ProtocolType.Tcp);
762                         
763                         sock.Close ();
764                         
765                         bool val = sock.DontFragment;
766                 }
767                 
768                 [Test]
769                 [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
770                 public void DontFragment ()
771                 {
772                         Socket sock = new Socket (AddressFamily.NetBios,
773                                                   SocketType.Seqpacket,
774                                                   ProtocolType.Unspecified);
775                         
776                         try {
777                                 sock.DontFragment = true;
778                                 Assert.Fail ("DontFragment #1");
779                         } catch (NotSupportedException) {
780                         } catch {
781                                 Assert.Fail ("DontFragment #2");
782                         } finally {
783                                 sock.Close ();
784                         }
785                 }
786                 
787                 [Test]
788 #if TARGET_JVM
789         [Ignore("System.Net.Sockets.Socket.EnableBroadcast property is not supported")]
790 #endif
791                 public void EnableBroadcastDefaultTcp ()
792                 {
793                         Socket sock = new Socket (AddressFamily.InterNetwork,
794                                                   SocketType.Stream,
795                                                   ProtocolType.Tcp);
796                         
797                         try {
798                                 bool value = sock.EnableBroadcast;
799                                 Assert.Fail ("EnableBroadcastDefaultTcp #1");
800                         } catch (SocketException ex) {
801                                 Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastDefaultTcp #2");
802                         } catch {
803                                 Assert.Fail ("EnableBroadcastDefaultTcp #2");
804                         } finally {
805                                 sock.Close ();
806                         }
807                 }
808
809                 [Test]
810 #if TARGET_JVM
811         [Ignore("System.Net.Sockets.Socket.EnableBroadcast property is not supported")]
812 #endif
813                 public void EnableBroadcastDefaultUdp ()
814                 {
815                         Socket sock = new Socket (AddressFamily.InterNetwork,
816                                                   SocketType.Dgram,
817                                                   ProtocolType.Udp);
818                         
819                         Assertion.AssertEquals ("EnableBroadcastDefaultUdp",
820                                                 false, sock.EnableBroadcast);
821
822                         sock.Close ();
823                 }
824                 
825                 [Test]
826 #if TARGET_JVM
827         [Ignore ("System.Net.Sockets.Socket.EnableBroadcast property is not supported")]
828 #endif
829                 public void EnableBroadcastChangeTcp ()
830                 {
831                         Socket sock = new Socket (AddressFamily.InterNetwork,
832                                                   SocketType.Stream,
833                                                   ProtocolType.Tcp);
834                         
835                         try {
836                                 sock.EnableBroadcast = true;
837                                 Assert.Fail ("EnableBroadcastChangeTcp #1");
838                         } catch (SocketException ex) {
839                                 Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastChangeTcp #2");
840                         } catch {
841                                 Assert.Fail ("EnableBroadcastChangeTcp #2");
842                         } finally {
843                                 sock.Close ();
844                         }
845                 }
846                 
847                 [Test]
848 #if TARGET_JVM
849         [Ignore("System.Net.Sockets.Socket.EnableBroadcast property is not supported")]
850 #endif
851                 public void EnableBroadcastChangeUdp ()
852                 {
853                         Socket sock = new Socket (AddressFamily.InterNetwork,
854                                                   SocketType.Dgram,
855                                                   ProtocolType.Udp);
856                         
857                         sock.EnableBroadcast = true;
858                         
859                         Assertion.AssertEquals ("EnableBroadcastChangeUdp",
860                                                 true, sock.EnableBroadcast);
861
862                         sock.Close ();
863                 }
864
865                 [Test]
866                 [ExpectedException (typeof(ObjectDisposedException))]
867 #if TARGET_JVM
868         [Ignore("System.Net.Sockets.Socket.EnableBroadcast property is not supported")]
869 #endif
870                 public void EnableBroadcastClosed ()
871                 {
872                         Socket sock = new Socket (AddressFamily.InterNetwork,
873                                                   SocketType.Dgram,
874                                                   ProtocolType.Udp);
875                         
876                         sock.Close ();
877                         
878                         bool val = sock.EnableBroadcast;
879                 }
880
881                 /* Can't test the default for ExclusiveAddressUse as
882                  * it's different on different versions and service
883                  * packs of windows
884                  */
885                 [Test]
886                 [Category ("NotWorking")] // Not supported on Linux
887                 public void ExclusiveAddressUseUnbound ()
888                 {
889                         Socket sock = new Socket (AddressFamily.InterNetwork,
890                                                   SocketType.Stream,
891                                                   ProtocolType.Tcp);
892                         
893                         sock.ExclusiveAddressUse = true;
894                         
895                         Assertion.AssertEquals ("ExclusiveAddressUseUnbound",
896                                                 true,
897                                                 sock.ExclusiveAddressUse);
898                         
899                         sock.Close ();
900                 }
901
902                 [Test]
903                 [ExpectedException (typeof(InvalidOperationException))]
904                 [Category ("NotWorking")] // Not supported on Linux
905                 public void ExclusiveAddressUseBound ()
906                 {
907                         Socket sock = new Socket (AddressFamily.InterNetwork,
908                                                   SocketType.Stream,
909                                                   ProtocolType.Tcp);
910                         
911                         sock.Bind (new IPEndPoint (IPAddress.Any, 1235));
912                         sock.ExclusiveAddressUse = true;
913                         sock.Close ();
914                 }
915
916                 [Test]
917                 [ExpectedException (typeof(ObjectDisposedException))]
918 #if TARGET_JVM
919         [Ignore ("System.Net.Sockets.Socket.ExclusiveAddressUse is not supported")]
920 #endif
921                 public void ExclusiveAddressUseClosed ()
922                 {
923                         Socket sock = new Socket (AddressFamily.InterNetwork,
924                                                   SocketType.Stream,
925                                                   ProtocolType.Tcp);
926                         
927                         sock.Close ();
928                         
929                         bool val = sock.ExclusiveAddressUse;
930                 }
931                 
932                 [Test]
933 #if TARGET_JVM
934         [Ignore ("System.Net.Sockets.Socket.IsBound property isn't supported")]
935 #endif
936                 public void IsBoundTcp ()
937                 {
938                         Socket sock = new Socket (AddressFamily.InterNetwork,
939                                                   SocketType.Stream,
940                                                   ProtocolType.Tcp);
941                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
942                                                         BogusPort);
943                         
944                         Assertion.AssertEquals ("IsBoundTcp #1", false,
945                                                 sock.IsBound);
946                         
947                         sock.Bind (ep);
948                         Assertion.AssertEquals ("IsBoundTcp #2", true,
949                                                 sock.IsBound);
950
951                         sock.Listen (1);
952                         
953                         Socket sock2 = new Socket (AddressFamily.InterNetwork,
954                                                    SocketType.Stream,
955                                                    ProtocolType.Tcp);
956                         
957                         Assertion.AssertEquals ("IsBoundTcp #3", false,
958                                                 sock2.IsBound);
959                         
960                         sock2.Connect (ep);
961                         Assertion.AssertEquals ("IsBoundTcp #4", true,
962                                                 sock2.IsBound);
963                         
964                         sock2.Close ();
965                         Assertion.AssertEquals ("IsBoundTcp #5", true,
966                                                 sock2.IsBound);
967
968                         sock.Close ();
969                         Assertion.AssertEquals ("IsBoundTcp #6", true,
970                                                 sock.IsBound);
971                 }
972
973                 [Test]
974 #if TARGET_JVM
975         [Ignore("System.Net.Sockets.Socket.IsBound property isn't supported")]
976 #endif
977                 public void IsBoundUdp ()
978                 {
979                         Socket sock = new Socket (AddressFamily.InterNetwork,
980                                                   SocketType.Dgram,
981                                                   ProtocolType.Udp);
982                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
983                                                         BogusPort);
984                         
985                         Assertion.AssertEquals ("IsBoundUdp #1", false,
986                                                 sock.IsBound);
987                         
988                         sock.Bind (ep);
989                         Assertion.AssertEquals ("IsBoundUdp #2", true,
990                                                 sock.IsBound);
991                         
992                         sock.Close ();
993                         Assertion.AssertEquals ("IsBoundUdp #3", true,
994                                                 sock.IsBound);
995                         
996
997                         sock = new Socket (AddressFamily.InterNetwork,
998                                            SocketType.Dgram,
999                                            ProtocolType.Udp);
1000                         
1001                         Assertion.AssertEquals ("IsBoundUdp #4", false,
1002                                                 sock.IsBound);
1003                         
1004                         sock.Connect (ep);
1005                         Assertion.AssertEquals ("IsBoundUdp #5", true,
1006                                                 sock.IsBound);
1007                         
1008                         sock.Close ();
1009                         Assertion.AssertEquals ("IsBoundUdp #6", true,
1010                                                 sock.IsBound);
1011                 }
1012
1013                 [Test]
1014 #if TARGET_JVM
1015         [Ignore ("System.Net.Sockets.Socket.IsBound property is not supported")]
1016 #endif
1017         /* Should not throw an exception */
1018                 public void IsBoundClosed ()
1019                 {
1020                         Socket sock = new Socket (AddressFamily.InterNetwork,
1021                                                   SocketType.Stream,
1022                                                   ProtocolType.Tcp);
1023                         
1024                         sock.Close ();
1025                         
1026                         bool val = sock.IsBound;
1027                 }
1028                 
1029                 /* Nothing much to test for LingerState */
1030                 
1031                 [Test]
1032 #if TARGET_JVM
1033         [Ignore("System.Net.Sockets.Socket.MulticastLoopback property is not supported")]
1034 #endif
1035                 public void MulticastLoopbackDefaultTcp ()
1036                 {
1037                         Socket sock = new Socket (AddressFamily.InterNetwork,
1038                                                   SocketType.Stream,
1039                                                   ProtocolType.Tcp);
1040                         
1041                         try {
1042                                 bool value = sock.MulticastLoopback;
1043                                 Assert.Fail ("MulticastLoopbackDefaultTcp #1");
1044                         } catch (SocketException ex) {
1045                                 Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackDefaultTcp #2");
1046                         } catch {
1047                                 Assert.Fail ("MulticastLoopbackDefaultTcp #2");
1048                         } finally {
1049                                 sock.Close ();
1050                         }
1051                 }
1052
1053                 [Test]
1054 #if TARGET_JVM
1055         [Ignore ("System.Net.Sockets.Socket.MulticastLoopback property is not supported")]
1056 #endif
1057                 public void MulticastLoopbackChangeTcp ()
1058                 {
1059                         Socket sock = new Socket (AddressFamily.InterNetwork,
1060                                                   SocketType.Stream,
1061                                                   ProtocolType.Tcp);
1062                         
1063                         try {
1064                                 sock.MulticastLoopback = false;
1065                                 Assert.Fail ("MulticastLoopbackChangeTcp #1");
1066                         } catch (SocketException ex) {
1067                                 Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackChangeTcp #2");
1068                         } catch {
1069                                 Assert.Fail ("MulticastLoopbackChangeTcp #2");
1070                         } finally {
1071                                 sock.Close ();
1072                         }
1073                 }
1074                 
1075                 [Test]
1076 #if TARGET_JVM
1077         [Ignore ("Udp sockets are not supported")]
1078 #endif
1079                 public void MulticastLoopbackDefaultUdp ()
1080                 {
1081                         Socket sock = new Socket (AddressFamily.InterNetwork,
1082                                                   SocketType.Dgram,
1083                                                   ProtocolType.Udp);
1084                         
1085                         Assertion.AssertEquals ("MulticastLoopbackDefaultUdp",
1086                                                 true, sock.MulticastLoopback);
1087                         
1088                         sock.Close ();
1089                 }
1090                 
1091                 [Test]
1092 #if TARGET_JVM
1093         [Ignore ("Udp sockets are not supported")]
1094 #endif
1095                 public void MulticastLoopbackChangeUdp ()
1096                 {
1097                         Socket sock = new Socket (AddressFamily.InterNetwork,
1098                                                   SocketType.Dgram,
1099                                                   ProtocolType.Udp);
1100                         
1101                         sock.MulticastLoopback = false;
1102                         
1103                         Assertion.AssertEquals ("MulticastLoopbackChangeUdp",
1104                                                 false, sock.MulticastLoopback);
1105                         
1106                         sock.Close ();
1107                 }
1108
1109                 [Test]
1110                 [ExpectedException (typeof(ObjectDisposedException))]
1111 #if TARGET_JVM
1112         [Ignore("System.Net.Sockets.Socket.MulticastLoopback property is not supported")]
1113 #endif
1114                 public void MulticastLoopbackClosed ()
1115                 {
1116                         Socket sock = new Socket (AddressFamily.InterNetwork,
1117                                                   SocketType.Stream,
1118                                                   ProtocolType.Tcp);
1119                         
1120                         sock.Close ();
1121                         
1122                         bool val = sock.MulticastLoopback;
1123                 }
1124                 
1125                 /* OSSupportsIPv6 depends on the environment */
1126                 
1127                 [Test]
1128 #if TARGET_JVM
1129         [Ignore("System.Net.Sockets.Socket.ReseiveBufferSize property is not supported")]
1130 #endif
1131                 public void ReceiveBufferSizeDefault ()
1132                 {
1133                         Socket sock = new Socket (AddressFamily.InterNetwork,
1134                                                   SocketType.Stream,
1135                                                   ProtocolType.Tcp);
1136                         
1137                         Assertion.AssertEquals ("ReceiveBufferSizeDefault",
1138                                                 8192, sock.ReceiveBufferSize);
1139                         
1140                         sock.Close ();
1141                 }
1142                 
1143                 [Test]
1144 #if TARGET_JVM
1145         [Ignore("System.Net.Sockets.Socket.ReseiveBufferSize property is not supported")]
1146 #endif
1147                 public void ReceiveBufferSizeDefaultUdp ()
1148                 {
1149                         Socket sock = new Socket (AddressFamily.InterNetwork,
1150                                                   SocketType.Dgram,
1151                                                   ProtocolType.Udp);
1152                         
1153                         Assertion.AssertEquals ("ReceiveBufferSizeDefaultUdp",
1154                                                 8192, sock.ReceiveBufferSize);
1155                         
1156                         sock.Close ();
1157                 }
1158
1159                 [Test]
1160 #if TARGET_JVM
1161         [Ignore ("System.Net.Sockets.Socket.ReseiveBufferSize property is not supported")]
1162 #endif
1163                 public void ReceiveBufferSizeChange ()
1164                 {
1165                         Socket sock = new Socket (AddressFamily.InterNetwork,
1166                                                   SocketType.Stream,
1167                                                   ProtocolType.Tcp);
1168                         
1169                         sock.ReceiveBufferSize = 16384;
1170                         
1171                         Assertion.AssertEquals ("ReceiveBufferSizeChange",
1172                                                 16384, sock.ReceiveBufferSize);
1173                         
1174                         sock.Close ();
1175                 }
1176
1177                 [Test]
1178                 [ExpectedException (typeof(ObjectDisposedException))]
1179 #if TARGET_JVM
1180         [Ignore("System.Net.Sockets.Socket.ReseiveBufferSize property is not supported")]
1181 #endif
1182                 public void ReceiveBufferSizeClosed ()
1183                 {
1184                         Socket sock = new Socket (AddressFamily.InterNetwork,
1185                                                   SocketType.Stream,
1186                                                   ProtocolType.Tcp);
1187                         
1188                         sock.Close ();
1189                         
1190                         int val = sock.ReceiveBufferSize;
1191                 }
1192                 
1193                 [Test]
1194 #if TARGET_JVM
1195         [Ignore("System.Net.Sockets.Socket.SendBufferSize property is not supported")]
1196 #endif
1197                 public void SendBufferSizeDefault ()
1198                 {
1199                         Socket sock = new Socket (AddressFamily.InterNetwork,
1200                                                   SocketType.Stream,
1201                                                   ProtocolType.Tcp);
1202                         
1203                         Assertion.AssertEquals ("SendBufferSizeDefault",
1204                                                 8192, sock.SendBufferSize);
1205                         
1206                         sock.Close ();
1207                 }
1208                 
1209                 [Test]
1210 #if TARGET_JVM
1211         [Ignore("System.Net.Sockets.Socket.SendBufferSize property is not supported")]
1212 #endif
1213                 public void SendBufferSizeDefaultUdp ()
1214                 {
1215                         Socket sock = new Socket (AddressFamily.InterNetwork,
1216                                                   SocketType.Dgram,
1217                                                   ProtocolType.Udp);
1218                         
1219                         Assertion.AssertEquals ("SendBufferSizeDefaultUdp",
1220                                                 8192, sock.SendBufferSize);
1221                         
1222                         sock.Close ();
1223                 }
1224
1225                 [Test]
1226 #if TARGET_JVM
1227         [Ignore ("System.Net.Sockets.Socket.SendBufferSize property is not supported")]
1228 #endif
1229                 public void SendBufferSizeChange ()
1230                 {
1231                         Socket sock = new Socket (AddressFamily.InterNetwork,
1232                                                   SocketType.Stream,
1233                                                   ProtocolType.Tcp);
1234                         
1235                         sock.SendBufferSize = 16384;
1236                         
1237                         Assertion.AssertEquals ("SendBufferSizeChange",
1238                                                 16384, sock.SendBufferSize);
1239                         
1240                         sock.Close ();
1241                 }
1242
1243                 [Test]
1244                 [ExpectedException (typeof(ObjectDisposedException))]
1245 #if TARGET_JVM
1246         [Ignore("System.Net.Sockets.Socket.SendBufferSize property is not supported")]
1247 #endif
1248                 public void SendBufferSizeClosed ()
1249                 {
1250                         Socket sock = new Socket (AddressFamily.InterNetwork,
1251                                                   SocketType.Stream,
1252                                                   ProtocolType.Tcp);
1253                         
1254                         sock.Close ();
1255                         
1256                         int val = sock.SendBufferSize;
1257                 }
1258                 
1259                 /* No test for TTL default as it's platform dependent */
1260                 [Test]
1261 #if TARGET_JVM
1262         [Ignore("System.Net.Sockets.Socket.Ttl property is not supported")]
1263 #endif
1264                 public void TtlChange ()
1265                 {
1266                         Socket sock = new Socket (AddressFamily.InterNetwork,
1267                                                   SocketType.Stream,
1268                                                   ProtocolType.Tcp);
1269                         
1270                         sock.Ttl = 255;
1271                         
1272                         Assertion.AssertEquals ("TtlChange", 255, sock.Ttl);
1273                         
1274                         sock.Close ();
1275                 }
1276
1277                 [Test]
1278 #if TARGET_JVM
1279         [Ignore("System.Net.Sockets.Socket.Ttl property is not supported")]
1280 #endif
1281                 public void TtlChangeOverflow ()
1282                 {
1283                         Socket sock = new Socket (AddressFamily.InterNetwork,
1284                                                   SocketType.Stream,
1285                                                   ProtocolType.Tcp);
1286                         
1287                         try {
1288                                 sock.Ttl = 256;
1289                                 Assert.Fail ("TtlChangeOverflow #1");
1290                         } catch (SocketException ex) {
1291                                 Assert.AreEqual (10022, ex.ErrorCode,
1292                                                  "TtlChangeOverflow #2");
1293                         } catch {
1294                                 Assert.Fail ("TtlChangeoverflow #3");
1295                         } finally {
1296                                 sock.Close ();
1297                         }
1298                 }
1299                 
1300 /* Apparently you can set TTL=0 on the ms runtime!!
1301                         try {
1302                                 sock.Ttl = 0;
1303                                 Assert.Fail ("TtlChangeOverflow #4");
1304                         } catch (SocketException ex) {
1305                                 Assert.AreEqual (10022, ex.ErrorCode,
1306                                                  "TtlChangeOverflow #5");
1307                         } catch {
1308                                 Assert.Fail ("TtlChangeOverflow #6");
1309                         } finally {
1310                                 sock.Close ();
1311                         }
1312 */
1313
1314                 [Test]
1315                 [ExpectedException (typeof(ObjectDisposedException))]
1316 #if TARGET_JVM
1317         [Ignore("System.Net.Sockets.Socket.Ttl property is not supported")]
1318 #endif
1319                 public void TtlClosed ()
1320                 {
1321                         Socket sock = new Socket (AddressFamily.InterNetwork,
1322                                                   SocketType.Stream,
1323                                                   ProtocolType.Tcp);
1324                         
1325                         sock.Close ();
1326                         
1327                         int val = sock.Ttl;
1328                 }
1329                 
1330                 [Test]
1331 #if TARGET_JVM
1332         [Ignore("System.Net.Sockets.Socket.UseOnlyOverlappedIO property is not supported")]
1333 #endif
1334                 public void UseOnlyOverlappedIODefault ()
1335                 {
1336                         Socket sock = new Socket (AddressFamily.InterNetwork,
1337                                                   SocketType.Stream,
1338                                                   ProtocolType.Tcp);
1339                         
1340                         Assertion.AssertEquals ("UseOnlyOverlappedIODefault",
1341                                                 false,
1342                                                 sock.UseOnlyOverlappedIO);
1343                         
1344                         sock.Close ();
1345                 }
1346
1347                 //
1348                 // We need this because the Linux kernel in certain configurations
1349                 // will end up rounding up the values passed on to the kernel
1350                 // for socket send/receive timeouts.
1351                 //
1352                 int Approximate (int target, int value)
1353                 {
1354                         int epsilon = 10;
1355                         
1356                         if (value > target-10 && value < target+10)
1357                                 return target;
1358                         return value;
1359                 }
1360                 
1361                 [Test]
1362 #if TARGET_JVM
1363         [Ignore("System.Net.Sockets.Socket.UseOnlyOverlappedIO property is not supported")]
1364 #endif
1365                 public void UseOnlyOverlappedIOChange ()
1366                 {
1367                         Socket sock = new Socket (AddressFamily.InterNetwork,
1368                                                   SocketType.Stream,
1369                                                   ProtocolType.Tcp);
1370                         
1371                         sock.UseOnlyOverlappedIO = true;
1372                         
1373                         Assertion.AssertEquals ("UseOnlyOverlappedIOChange",
1374                                                 true,
1375                                                 sock.UseOnlyOverlappedIO);
1376                         
1377                         sock.Close ();
1378                 }
1379
1380                 [Test]
1381 #if TARGET_JVM
1382         [Ignore("System.Net.Sockets.Socket.UseOnlyOverlappedIO property is not supported")]
1383 #endif
1384                 /* Should not throw an exception */
1385                 public void UseOnlyOverlappedIOClosed ()
1386                 {
1387                         Socket sock = new Socket (AddressFamily.InterNetwork,
1388                                                   SocketType.Stream,
1389                                                   ProtocolType.Tcp);
1390                         
1391                         sock.Close ();
1392                         
1393                         bool val = sock.UseOnlyOverlappedIO;
1394                 }
1395                 
1396                 [Test]
1397 #if TARGET_JVM
1398         [Ignore("System.Net.Sockets.Socket.SendTimeout property is not supported")]
1399 #endif
1400                 public void SendTimeoutDefault ()
1401                 {
1402                         Socket sock = new Socket (AddressFamily.InterNetwork,
1403                                                   SocketType.Stream,
1404                                                   ProtocolType.Tcp);
1405                         
1406                         Assertion.AssertEquals ("SendTimeoutDefault",
1407                                                 0, sock.SendTimeout);
1408                         
1409                         sock.Close ();
1410                 }
1411
1412                 [Test]
1413 #if TARGET_JVM
1414         [Ignore("System.Net.Sockets.Socket.SendTimeout property is not supported")]
1415 #endif
1416                 public void SendTimeoutChange ()
1417                 {
1418                         Socket sock = new Socket (AddressFamily.InterNetwork,
1419                                                   SocketType.Stream,
1420                                                   ProtocolType.Tcp);
1421                         
1422                         /* Should be rounded up to 500, according to
1423                          * the MSDN docs, but the MS runtime doesn't
1424                          */
1425                         sock.SendTimeout = 50;
1426                         Assertion.AssertEquals ("SendTimeoutChange #1",
1427                                                 50, Approximate (50, sock.SendTimeout));
1428                         
1429                         sock.SendTimeout = 2000;
1430                         Assertion.AssertEquals ("SendTimeoutChange #2",
1431                                                 2000, Approximate (2000, sock.SendTimeout));
1432                         
1433                         sock.SendTimeout = 0;
1434                         Assertion.AssertEquals ("SendTimeoutChange #3",
1435                                                 0, Approximate (0, sock.SendTimeout));
1436                         
1437                         /* Should be the same as setting 0 */
1438                         sock.SendTimeout = -1;
1439                         Assertion.AssertEquals ("SendTimeoutChange #4",
1440                                                 0, sock.SendTimeout);
1441
1442                         sock.SendTimeout = 65536;
1443                         Assertion.AssertEquals ("SendTimeoutChange #5",
1444                                                 65536, Approximate (65536, sock.SendTimeout));
1445                         
1446                         try {
1447                                 sock.SendTimeout = -2;
1448                                 Assert.Fail ("SendTimeoutChange #8");
1449                         } catch (ArgumentOutOfRangeException) {
1450                         } catch {
1451                                 Assert.Fail ("SendTimeoutChange #9");
1452                         } finally {
1453                                 sock.Close ();
1454                         }
1455                 }
1456
1457                 [Test]
1458                 [ExpectedException (typeof(ObjectDisposedException))]
1459 #if TARGET_JVM
1460         [Ignore("System.Net.Sockets.Socket.SendTimeout property is not supported")]
1461 #endif
1462                 public void SendTimeoutClosed ()
1463                 {
1464                         Socket sock = new Socket (AddressFamily.InterNetwork,
1465                                                   SocketType.Stream,
1466                                                   ProtocolType.Tcp);
1467                         
1468                         sock.Close ();
1469                         
1470                         int val = sock.SendTimeout;
1471                 }
1472                 
1473                 [Test]
1474 #if TARGET_JVM
1475         [Ignore("System.Net.Sockets.Socket.ReceiveTimeout property is not supported")]
1476 #endif
1477                 public void ReceiveTimeoutDefault ()
1478                 {
1479                         Socket sock = new Socket (AddressFamily.InterNetwork,
1480                                                   SocketType.Stream,
1481                                                   ProtocolType.Tcp);
1482                         
1483                         Assertion.AssertEquals ("ReceiveTimeoutDefault",
1484                                                 0, sock.ReceiveTimeout);
1485                         
1486                         sock.Close ();
1487                 }
1488
1489                 [Test]
1490 #if TARGET_JVM
1491         [Ignore("System.Net.Sockets.Socket.ReceiveTimeout property is not supported")]
1492 #endif
1493                 public void ReceiveTimeoutChange ()
1494                 {
1495                         Socket sock = new Socket (AddressFamily.InterNetwork,
1496                                                   SocketType.Stream,
1497                                                   ProtocolType.Tcp);
1498                         
1499                         sock.ReceiveTimeout = 50;
1500                         Assertion.AssertEquals ("ReceiveTimeoutChange #1",
1501                                                 50, Approximate (50, sock.ReceiveTimeout));
1502                         
1503                         sock.ReceiveTimeout = 2000;
1504                         Assertion.AssertEquals ("ReceiveTimeoutChange #2",
1505                                                 2000, Approximate (2000, sock.ReceiveTimeout));
1506                         
1507                         sock.ReceiveTimeout = 0;
1508                         Assertion.AssertEquals ("ReceiveTimeoutChange #3",
1509                                                 0, sock.ReceiveTimeout);
1510                         
1511                         /* Should be the same as setting 0 */
1512                         sock.ReceiveTimeout = -1;
1513                         Assertion.AssertEquals ("ReceiveTimeoutChange #4",
1514                                                 0, sock.ReceiveTimeout);
1515
1516                         sock.ReceiveTimeout = 65536;
1517                         Assertion.AssertEquals ("ReceiveTimeoutChange #5",
1518                                                 65536, Approximate (65536, sock.ReceiveTimeout));
1519                         
1520                         try {
1521                                 sock.ReceiveTimeout = -2;
1522                                 Assert.Fail ("ReceiveTimeoutChange #8");
1523                         } catch (ArgumentOutOfRangeException) {
1524                         } catch {
1525                                 Assert.Fail ("ReceiveTimeoutChange #9");
1526                         } finally {
1527                                 sock.Close ();
1528                         }
1529                 }
1530
1531                 [Test]
1532                 [ExpectedException (typeof(ObjectDisposedException))]
1533 #if TARGET_JVM
1534         [Ignore("System.Net.Sockets.Socket.ReceiveTimeout property is not supported")]
1535 #endif
1536                 public void ReceiveTimeoutClosed ()
1537                 {
1538                         Socket sock = new Socket (AddressFamily.InterNetwork,
1539                                                   SocketType.Stream,
1540                                                   ProtocolType.Tcp);
1541                         
1542                         sock.Close ();
1543                         
1544                         int val = sock.ReceiveTimeout;
1545                 }
1546                 
1547                 [Test]
1548 #if TARGET_JVM
1549         [Ignore("System.Net.Sockets.Socket.NoDelay property is not supported")]
1550 #endif
1551                 public void NoDelayDefaultTcp ()
1552                 {
1553                         Socket sock = new Socket (AddressFamily.InterNetwork,
1554                                                   SocketType.Stream,
1555                                                   ProtocolType.Tcp);
1556                         
1557                         Assertion.AssertEquals ("NoDelayDefaultTcp", false,
1558                                                 sock.NoDelay);
1559                         
1560                         sock.Close ();
1561                 }
1562
1563                 [Test]
1564 #if TARGET_JVM
1565         [Ignore("System.Net.Sockets.Socket.NoDelay property is not supported")]
1566 #endif
1567                 public void NoDelayChangeTcp ()
1568                 {
1569                         Socket sock = new Socket (AddressFamily.InterNetwork,
1570                                                   SocketType.Stream,
1571                                                   ProtocolType.Tcp);
1572                         
1573                         sock.NoDelay = true;
1574                         
1575                         Assertion.AssertEquals ("NoDelayChangeTcp", true,
1576                                                 sock.NoDelay);
1577                         
1578                         sock.Close ();
1579                 }
1580                 
1581                 [Test]
1582 #if TARGET_JVM
1583         [Ignore("System.Net.Sockets.Socket.NoDelay property is not supported")]
1584 #endif
1585                 public void NoDelayDefaultUdp ()
1586                 {
1587                         Socket sock = new Socket (AddressFamily.InterNetwork,
1588                                                   SocketType.Dgram,
1589                                                   ProtocolType.Udp);
1590                         
1591                         try {
1592                                 bool val = sock.NoDelay;
1593                                 Assert.Fail ("NoDelayDefaultUdp #1");
1594                         } catch (SocketException ex) {
1595                                 Assert.AreEqual (10042, ex.ErrorCode,
1596                                                  "NoDelayDefaultUdp #2");
1597                         } catch {
1598                                 Assert.Fail ("NoDelayDefaultUdp #3");
1599                         } finally {
1600                                 sock.Close ();
1601                         }
1602                 }
1603
1604                 [Test]
1605 #if TARGET_JVM
1606         [Ignore("System.Net.Sockets.Socket.NoDelay property is not supported")]
1607 #endif
1608                 public void NoDelayChangeUdp ()
1609                 {
1610                         Socket sock = new Socket (AddressFamily.InterNetwork,
1611                                                   SocketType.Dgram,
1612                                                   ProtocolType.Udp);
1613                         
1614                         try {
1615                                 sock.NoDelay = true;
1616                                 Assert.Fail ("NoDelayChangeUdp #1");
1617                         } catch (SocketException ex) {
1618                                 Assert.AreEqual (10042, ex.ErrorCode,
1619                                                  "NoDelayChangeUdp #2");
1620                         } catch {
1621                                 Assert.Fail ("NoDelayChangeUdp #3");
1622                         } finally {
1623                                 sock.Close ();
1624                         }
1625                 }
1626                 
1627                 [Test]
1628                 [ExpectedException (typeof(ObjectDisposedException))]
1629 #if TARGET_JVM
1630         [Ignore("System.Net.Sockets.Socket.NoDelay property is not supported")]
1631 #endif
1632                 public void NoDelayClosed ()
1633                 {
1634                         Socket sock = new Socket (AddressFamily.InterNetwork,
1635                                                   SocketType.Stream,
1636                                                   ProtocolType.Tcp);
1637                         
1638                         sock.Close ();
1639                         
1640                         bool val = sock.NoDelay;
1641                 }
1642
1643                 static bool BAAccepted = false;
1644                 static Socket BASocket = null;
1645                 static ManualResetEvent BACalledBack = new ManualResetEvent (false);
1646                 
1647                 private static void BACallback (IAsyncResult asyncResult)
1648                 {
1649                         Socket sock = (Socket)asyncResult.AsyncState;
1650                         
1651                         BASocket = sock.EndAccept (asyncResult);
1652                         
1653                         BAAccepted = true;
1654                         BACalledBack.Set ();
1655                 }
1656                 
1657                 [Test]
1658 #if TARGET_JVM
1659                 [Ignore ("NMA")]
1660 #endif          
1661                 [ExpectedException (typeof(InvalidOperationException))]
1662                 public void BeginAcceptNotBound ()
1663                 {
1664                         Socket sock = new Socket (AddressFamily.InterNetwork,
1665                                                   SocketType.Stream,
1666                                                   ProtocolType.Tcp);
1667
1668                         sock.BeginAccept (BACallback, sock);
1669                         
1670                         sock.Close ();
1671                 }
1672                 
1673                 [Test]
1674 #if TARGET_JVM
1675                 [Ignore ("NMA")]
1676 #endif          
1677                 [ExpectedException (typeof(InvalidOperationException))]
1678                 public void BeginAcceptNotListening ()
1679                 {
1680                         Socket sock = new Socket (AddressFamily.InterNetwork,
1681                                                   SocketType.Stream,
1682                                                   ProtocolType.Tcp);
1683
1684                         sock.Bind (new IPEndPoint (IPAddress.Any, 1236));
1685                         
1686                         sock.BeginAccept (BACallback, sock);
1687                         
1688                         sock.Close ();
1689                 }
1690
1691                 [Test]
1692                 public void BeginAccept ()
1693                 {
1694                         Socket sock = new Socket (AddressFamily.InterNetwork,
1695                                                   SocketType.Stream,
1696                                                   ProtocolType.Tcp);
1697                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1698                                                         1237);
1699                         
1700                         sock.Bind (ep);
1701                         sock.Listen (1);
1702                         
1703                         BACalledBack.Reset ();
1704                         
1705                         sock.BeginAccept (BACallback, sock);
1706
1707                         Socket conn = new Socket (AddressFamily.InterNetwork,
1708                                                   SocketType.Stream,
1709                                                   ProtocolType.Tcp);
1710                         
1711                         conn.Connect (ep);
1712
1713                         if (BACalledBack.WaitOne (2000, false) == false) {
1714                                 Assert.Fail ("BeginAccept wait timed out");
1715                         }
1716                         
1717                         Assertion.AssertEquals ("BeginAccept #1", true,
1718                                                 BAAccepted);
1719                         Assertion.AssertEquals ("BeginAccept #2", true,
1720                                                 BASocket.Connected);
1721                         Assertion.AssertEquals ("BeginAccept #3", false,
1722                                                 sock.Connected);
1723                         Assertion.AssertEquals ("BeginAccept #4", true,
1724                                                 conn.Connected);
1725
1726                         BASocket.Close ();
1727                         conn.Close ();
1728                         sock.Close ();
1729                 }
1730
1731                 [Test]
1732                 [ExpectedException (typeof(ObjectDisposedException))]
1733                 public void BeginAcceptClosed ()
1734                 {
1735                         Socket sock = new Socket (AddressFamily.InterNetwork,
1736                                                   SocketType.Stream,
1737                                                   ProtocolType.Tcp);
1738                         
1739                         sock.Close ();
1740                         
1741                         sock.BeginAccept (BACallback, sock);
1742                 }
1743
1744                 static bool BADAccepted = false;
1745                 static Socket BADSocket = null;
1746                 static byte[] BADBytes;
1747                 static int BADByteCount;
1748                 static ManualResetEvent BADCalledBack = new ManualResetEvent (false);
1749                 
1750                 private static void BADCallback (IAsyncResult asyncResult)
1751                 {
1752                         Socket sock = (Socket)asyncResult.AsyncState;
1753                         
1754                         BADSocket = sock.EndAccept (out BADBytes,
1755                                                     out BADByteCount,
1756                                                     asyncResult);
1757                         
1758                         BADAccepted = true;
1759                         BADCalledBack.Set ();
1760                 }
1761
1762                 [Test]
1763 #if TARGET_JVM
1764         [Ignore ("System.Net.Sockets.Socket.BeginAccept(int,AsyncCallback,object) is not supported")]
1765 #endif
1766                 public void BeginAcceptData ()
1767                 {
1768                         Socket sock = new Socket (AddressFamily.InterNetwork,
1769                                                   SocketType.Stream,
1770                                                   ProtocolType.Tcp);
1771                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1772                                                         1238);
1773                         
1774                         sock.Bind (ep);
1775                         sock.Listen (1);
1776                         
1777                         BADCalledBack.Reset ();
1778                         
1779                         sock.BeginAccept (256, BADCallback, sock);
1780
1781                         Socket conn = new Socket (AddressFamily.InterNetwork,
1782                                                   SocketType.Stream,
1783                                                   ProtocolType.Tcp);
1784                         byte[] send_bytes = new byte[] {10, 11, 12, 13};
1785                         
1786                         conn.Connect (ep);
1787                         conn.Send (send_bytes);
1788
1789                         if (BADCalledBack.WaitOne (2000, false) == false) {
1790                                 Assert.Fail ("BeginAcceptData wait timed out");
1791                         }
1792                         
1793                         Assertion.AssertEquals ("BeginAcceptData #1", true,
1794                                                 BADAccepted);
1795                         Assertion.AssertEquals ("BeginAcceptData #2", true,
1796                                                 BADSocket.Connected);
1797                         Assertion.AssertEquals ("BeginAcceptData #3", false,
1798                                                 sock.Connected);
1799                         Assertion.AssertEquals ("BeginAcceptData #4", true,
1800                                                 conn.Connected);
1801                         Assertion.AssertEquals ("BeginAcceptData #5",
1802                                                 send_bytes.Length,
1803                                                 BADByteCount);
1804                         
1805                         /* The MS runtime gives the returned data in a
1806                          * much bigger array.  TODO: investigate
1807                          * whether it the size correlates to the first
1808                          * parameter in BeginAccept()
1809                          */
1810                         Assert.IsFalse (BADBytes.Length == send_bytes.Length,
1811                                         "BeginAcceptData #6");
1812
1813                         for(int i = 0; i < send_bytes.Length; i++) {
1814                                 Assertion.AssertEquals ("BeginAcceptData #" + (i+7).ToString (), send_bytes[i], BADBytes[i]);
1815                         }
1816
1817                         BADSocket.Close ();
1818                         conn.Close ();
1819                         sock.Close ();
1820                 }
1821
1822                 [Test]
1823                 [ExpectedException (typeof(ObjectDisposedException))]
1824 #if TARGET_JVM
1825         [Ignore("System.Net.Sockets.Socket.BeginAccept(int,AsyncCallback,object) is not supported")]
1826 #endif
1827                 public void BeginAcceptDataClosed ()
1828                 {
1829                         Socket sock = new Socket (AddressFamily.InterNetwork,
1830                                                   SocketType.Stream,
1831                                                   ProtocolType.Tcp);
1832                         
1833                         sock.Close ();
1834                         
1835                         sock.BeginAccept (256, BADCallback, sock);
1836                 }
1837
1838                 [Test]
1839 #if TARGET_JVM
1840         [Ignore("System.Net.Sockets.Socket.BeginAccept(Socket,int,AsyncCallback,object) is not supported")]
1841 #endif
1842                 public void BeginAcceptSocketUdp ()
1843                 {
1844                         Socket sock = new Socket (AddressFamily.InterNetwork,
1845                                                   SocketType.Stream,
1846                                                   ProtocolType.Tcp);
1847                         Socket acc = new Socket (AddressFamily.InterNetwork,
1848                                                  SocketType.Dgram,
1849                                                  ProtocolType.Udp);
1850                         
1851                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1852                                                         1239);
1853                         
1854                         sock.Bind (ep);
1855                         sock.Listen (1);
1856                         
1857                         try {
1858                                 sock.BeginAccept (acc, 256, BADCallback, sock);
1859                                 Assert.Fail ("BeginAcceptSocketUdp #1");
1860                         } catch (SocketException ex) {
1861                                 Assertion.AssertEquals ("BeginAcceptSocketUdp #2", 10022, ex.ErrorCode);
1862                         } catch {
1863                                 Assert.Fail ("BeginAcceptSocketUdp #3");
1864                         } finally {
1865                                 acc.Close ();
1866                                 sock.Close ();
1867                         }
1868                 }
1869                 
1870                 [Test]
1871 #if TARGET_JVM
1872         [Ignore("System.Net.Sockets.Socket.BeginAccept(Socket,int,AsyncCallback,object) is not supported")]
1873 #endif
1874                 public void BeginAcceptSocketBound ()
1875                 {
1876                         Socket sock = new Socket (AddressFamily.InterNetwork,
1877                                                   SocketType.Stream,
1878                                                   ProtocolType.Tcp);
1879                         Socket acc = new Socket (AddressFamily.InterNetwork,
1880                                                  SocketType.Stream,
1881                                                  ProtocolType.Tcp);
1882                         
1883                         IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
1884                                                          1240);
1885                         
1886                         IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
1887                                                          1241);
1888                         
1889                         sock.Bind (ep1);
1890                         sock.Listen (1);
1891
1892                         acc.Bind (ep2);
1893                         
1894                         try {
1895                                 sock.BeginAccept (acc, 256, BADCallback, sock);
1896                                 Assert.Fail ("BeginAcceptSocketBound #1");
1897                         } catch (InvalidOperationException) {
1898                         } catch {
1899                                 Assert.Fail ("BeginAcceptSocketBound #2");
1900                         } finally {
1901                                 acc.Close ();
1902                                 sock.Close ();
1903                         }
1904                 }
1905                 
1906                 [Test]
1907 #if TARGET_JVM
1908         [Ignore("System.Net.Sockets.Socket.BeginAccept(Socket,int,AsyncCallback,object) is not supported")]
1909 #endif
1910                 public void BeginAcceptSocket ()
1911                 {
1912                         Socket sock = new Socket (AddressFamily.InterNetwork,
1913                                                   SocketType.Stream,
1914                                                   ProtocolType.Tcp);
1915                         Socket acc = new Socket (AddressFamily.InterNetwork,
1916                                                  SocketType.Stream,
1917                                                  ProtocolType.Tcp);
1918                         
1919                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1920                                                         1242);
1921                         
1922                         sock.Bind (ep);
1923                         sock.Listen (1);
1924                         
1925                         BADCalledBack.Reset ();
1926                         
1927                         sock.BeginAccept (acc, 256, BADCallback, sock);
1928
1929                         Socket conn = new Socket (AddressFamily.InterNetwork,
1930                                                   SocketType.Stream,
1931                                                   ProtocolType.Tcp);
1932                         byte[] send_bytes = new byte[] {10, 11, 12, 13};
1933                         
1934                         conn.Connect (ep);
1935                         conn.Send (send_bytes);
1936
1937                         if (BADCalledBack.WaitOne (2000, false) == false) {
1938                                 Assert.Fail ("BeginAcceptSocket wait timed out");
1939                         }
1940                         
1941                         Assertion.AssertEquals ("BeginAcceptSocket #1", true,
1942                                                 BADAccepted);
1943                         Assertion.AssertEquals ("BeginAcceptSocket #2", true,
1944                                                 BADSocket.Connected);
1945                         Assertion.AssertEquals ("BeginAcceptSocket #3", false,
1946                                                 sock.Connected);
1947                         Assertion.AssertEquals ("BeginAcceptSocket #4", true,
1948                                                 conn.Connected);
1949                         Assertion.AssertEquals ("BeginAcceptSocket #5",
1950                                                 send_bytes.Length,
1951                                                 BADByteCount);
1952                         Assertion.AssertEquals ("BeginAcceptSocket #6",
1953                                                 AddressFamily.InterNetwork,
1954                                                 acc.AddressFamily);
1955                         Assertion.AssertEquals ("BeginAcceptSocket #7",
1956                                                 SocketType.Stream,
1957                                                 acc.SocketType);
1958                         Assertion.AssertEquals ("BeginAcceptSocket #8",
1959                                                 ProtocolType.Tcp,
1960                                                 acc.ProtocolType);
1961                         Assertion.AssertEquals ("BeginAcceptSocket #9",
1962                                                 conn.LocalEndPoint,
1963                                                 acc.RemoteEndPoint);
1964                         
1965                         /* The MS runtime gives the returned data in a
1966                          * much bigger array.  TODO: investigate
1967                          * whether it the size correlates to the first
1968                          * parameter in BeginAccept()
1969                          */
1970                         Assert.IsFalse (BADBytes.Length == send_bytes.Length,
1971                                         "BeginAcceptSocket #10");
1972
1973                         for(int i = 0; i < send_bytes.Length; i++) {
1974                                 Assertion.AssertEquals ("BeginAcceptSocket #" + (i+11).ToString (), send_bytes[i], BADBytes[i]);
1975                         }
1976
1977                         BADSocket.Close ();
1978                         conn.Close ();
1979                         acc.Close ();
1980                         sock.Close ();
1981                 }
1982
1983                 [Test]
1984 #if TARGET_JVM
1985         [Ignore("System.Net.Sockets.Socket.BeginAccept(Socket,int,AsyncCallback,object) is not supported")]
1986 #endif
1987                 public void BeginAcceptSocketClosed ()
1988                 {
1989                         Socket sock = new Socket (AddressFamily.InterNetwork,
1990                                                   SocketType.Stream,
1991                                                   ProtocolType.Tcp);
1992                         Socket acc = new Socket (AddressFamily.InterNetwork,
1993                                                  SocketType.Stream,
1994                                                  ProtocolType.Tcp);
1995                         
1996                         sock.Close ();
1997                         
1998                         try {
1999                                 sock.BeginAccept (acc, 256, BADCallback, null);
2000                                 Assert.Fail ("BeginAcceptSocketClosed #1");
2001                         } catch (ObjectDisposedException) {
2002                         } catch {
2003                                 Assert.Fail ("BeginAcceptSocketClosed #2");
2004                         } finally {
2005                                 acc.Close ();
2006                         }
2007                 }
2008
2009                 [Test]
2010 #if TARGET_JVM
2011         [Ignore("System.Net.Sockets.Socket.BeginAccept(Socket,int,AsyncCallback,object) is not supported")]
2012 #endif
2013                 public void BeginAcceptSocketAccClosed ()
2014                 {
2015                         Socket sock = new Socket (AddressFamily.InterNetwork,
2016                                                   SocketType.Stream,
2017                                                   ProtocolType.Tcp);
2018                         Socket acc = new Socket (AddressFamily.InterNetwork,
2019                                                  SocketType.Stream,
2020                                                  ProtocolType.Tcp);
2021                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2022                                                         1243);
2023
2024                         sock.Bind (ep);
2025                         sock.Listen (1);
2026                         
2027                         acc.Close ();
2028                         
2029                         BADCalledBack.Reset ();
2030                         
2031                         try {
2032                                 sock.BeginAccept (acc, 256, BADCallback, null);
2033                                 Assert.Fail ("BeginAcceptSocketAccClosed #1");
2034                         } catch (ObjectDisposedException) {
2035                         } catch {
2036                                 Assert.Fail ("BeginAcceptSocketAccClosed #2");
2037                         } finally {
2038                                 sock.Close ();
2039                         }
2040                 }
2041                 
2042                 static bool BCConnected = false;
2043                 static ManualResetEvent BCCalledBack = new ManualResetEvent (false);
2044                 
2045                 private static void BCCallback (IAsyncResult asyncResult)
2046                 {
2047                         Socket sock = (Socket)asyncResult.AsyncState;
2048                         
2049                         sock.EndConnect (asyncResult);
2050                         BCConnected = true;
2051                         
2052                         BCCalledBack.Set ();
2053                 }
2054                 
2055                 [Test]
2056 #if TARGET_JVM
2057         [Ignore("System.Net.Sockets.Socket.BeginConnect(IPAddress,int,AsyncCallback,object) is not supported")]
2058 #endif
2059                 public void BeginConnectAddressPort ()
2060                 {
2061                         Socket sock = new Socket (AddressFamily.InterNetwork,
2062                                                   SocketType.Stream,
2063                                                   ProtocolType.Tcp);
2064                         Socket listen = new Socket (AddressFamily.InterNetwork,
2065                                                     SocketType.Stream,
2066                                                     ProtocolType.Tcp);
2067                         IPAddress ip = IPAddress.Loopback;
2068                         IPEndPoint ep = new IPEndPoint (ip, 1244);
2069
2070                         listen.Bind (ep);
2071                         listen.Listen (1);
2072                         
2073                         BCCalledBack.Reset ();
2074                         
2075                         BCConnected = false;
2076                         
2077                         sock.BeginConnect (ip, 1244, BCCallback, sock);
2078
2079                         if (BCCalledBack.WaitOne (2000, false) == false) {
2080                                 Assert.Fail ("BeginConnectAddressPort wait timed out");
2081                         }
2082                         
2083                         Assertion.AssertEquals ("BeginConnectAddressPort #1",
2084                                                 true, BCConnected);
2085                         
2086                         sock.Close ();
2087                         listen.Close ();
2088                 }
2089
2090                 [Test]
2091 #if TARGET_JVM
2092         [Ignore("System.Net.Sockets.Socket.BeginConnect(IPAddress,int,AsyncCallback,object) is not supported")]
2093 #endif
2094                 public void BeginConnectAddressPortNull ()
2095                 {
2096                         Socket sock = new Socket (AddressFamily.InterNetwork,
2097                                                   SocketType.Stream,
2098                                                   ProtocolType.Tcp);
2099                         IPAddress ip = null;
2100
2101                         try {
2102                                 sock.BeginConnect (ip, 1244, BCCallback,
2103                                                    sock);
2104                                 Assert.Fail ("BeginConnectAddressPortNull #1");
2105                         } catch (ArgumentNullException) {
2106                         } catch {
2107                                 Assert.Fail ("BeginConnectAddressPortNull #2");
2108                         } finally {
2109                                 sock.Close ();
2110                         }
2111                 }
2112
2113                 [Test]
2114 #if TARGET_JVM
2115         [Ignore("System.Net.Sockets.Socket.BeginConnect(IPAddress,int,AsyncCallback,object) is not supported")]
2116 #endif
2117                 public void BeginConnectAddressPortListen ()
2118                 {
2119                         Socket sock = new Socket (AddressFamily.InterNetwork,
2120                                                   SocketType.Stream,
2121                                                   ProtocolType.Tcp);
2122                         IPAddress ip = IPAddress.Loopback;
2123                         IPEndPoint ep = new IPEndPoint (ip, 1245);
2124
2125                         sock.Bind (ep);
2126                         sock.Listen (1);
2127                         
2128                         try {
2129                                 sock.BeginConnect (ip, 1245, BCCallback, sock);
2130                                 Assert.Fail ("BeginConnectAddressPortListen #1");
2131                         } catch (InvalidOperationException) {
2132                         } catch {
2133                                 Assert.Fail ("BeginConnectAddressPortListen #2");
2134                         } finally {
2135                                 sock.Close ();
2136                         }
2137                 }
2138                 
2139                 [Test]
2140                 [ExpectedException (typeof(ObjectDisposedException))]
2141 #if TARGET_JVM
2142         [Ignore("System.Net.Sockets.Socket.BeginConnect(IPAddress,int,AsyncCallback,object) is not supported")]
2143 #endif
2144                 public void BeginConnectAddressPortClosed ()
2145                 {
2146                         Socket sock = new Socket (AddressFamily.InterNetwork,
2147                                                   SocketType.Stream,
2148                                                   ProtocolType.Tcp);
2149                         IPAddress ip = IPAddress.Loopback;
2150                         
2151                         sock.Close ();
2152                         
2153                         sock.BeginConnect (ip, 1244, BCCallback, sock);
2154                 }
2155                 
2156                 [Test]
2157 #if TARGET_JVM
2158         [Ignore("System.Net.Sockets.Socket.BeginConnect(IPAddress[],int,AsyncCallback,object) is not supported")]
2159 #endif
2160                 public void BeginConnectMultiple ()
2161                 {
2162                         Socket sock = new Socket (AddressFamily.InterNetwork,
2163                                                   SocketType.Stream,
2164                                                   ProtocolType.Tcp);
2165                         Socket listen = new Socket (AddressFamily.InterNetwork,
2166                                                     SocketType.Stream,
2167                                                     ProtocolType.Tcp);
2168                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2169                                                         1246);
2170                         IPAddress[] ips = new IPAddress[4];
2171                         
2172                         ips[0] = IPAddress.Parse ("127.0.0.4");
2173                         ips[1] = IPAddress.Parse ("127.0.0.3");
2174                         ips[2] = IPAddress.Parse ("127.0.0.2");
2175                         ips[3] = IPAddress.Parse ("127.0.0.1");
2176
2177                         listen.Bind (ep);
2178                         listen.Listen (1);
2179                         
2180                         BCCalledBack.Reset ();
2181                         
2182                         BCConnected = false;
2183                         
2184                         sock.BeginConnect (ips, 1246, BCCallback, sock);
2185                         
2186                         /* Longer wait here, because the ms runtime
2187                          * takes a lot longer to not connect
2188                          */
2189                         if (BCCalledBack.WaitOne (10000, false) == false) {
2190                                 Assert.Fail ("BeginConnectMultiple wait failed");
2191                         }
2192                         
2193                         Assertion.AssertEquals ("BeginConnectMultiple #1",
2194                                                 true, BCConnected);
2195                         Assertion.AssertEquals ("BeginConnectMultiple #2",
2196                                                 AddressFamily.InterNetwork,
2197                                                 sock.RemoteEndPoint.AddressFamily);
2198                         IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
2199                         
2200                         Assertion.AssertEquals ("BeginConnectMultiple #2",
2201                                                 IPAddress.Loopback,
2202                                                 remep.Address);
2203                         
2204                         sock.Close ();
2205                         listen.Close ();
2206                 }
2207
2208                 [Test]
2209 #if TARGET_JVM
2210         [Ignore("System.Net.Sockets.Socket.BeginConnect(IPAddress[],int,AsyncCallback,object) is not supported")]
2211 #endif
2212                 public void BeginConnectMultipleNull ()
2213                 {
2214                         Socket sock = new Socket (AddressFamily.InterNetwork,
2215                                                   SocketType.Stream,
2216                                                   ProtocolType.Tcp);
2217                         IPAddress[] ips = null;
2218                         
2219                         try {
2220                                 sock.BeginConnect (ips, 1246, BCCallback,
2221                                                    sock);
2222                                 Assert.Fail ("BeginConnectMultipleNull #1");
2223                         } catch (ArgumentNullException) {
2224                         } catch {
2225                                 Assert.Fail ("BeginConnectMultipleNull #2");
2226                         } finally {
2227                                 sock.Close ();
2228                         }
2229                 }
2230
2231                 [Test]
2232 #if TARGET_JVM
2233         [Ignore("System.Net.Sockets.Socket.BeginConnect(IPAddress[],int,AsyncCallback,object) is not supported")]
2234 #endif
2235                 public void BeginConnectMultipleListen ()
2236                 {
2237                         Socket sock = new Socket (AddressFamily.InterNetwork,
2238                                                   SocketType.Stream,
2239                                                   ProtocolType.Tcp);
2240                         IPAddress[] ips = new IPAddress[4];
2241                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2242                                                         1247);
2243                         
2244                         ips[0] = IPAddress.Parse ("127.0.0.4");
2245                         ips[1] = IPAddress.Parse ("127.0.0.3");
2246                         ips[2] = IPAddress.Parse ("127.0.0.2");
2247                         ips[3] = IPAddress.Parse ("127.0.0.1");
2248                         
2249                         sock.Bind (ep);
2250                         sock.Listen (1);
2251                         
2252                         try {
2253                                 sock.BeginConnect (ips, 1247, BCCallback,
2254                                                    sock);
2255                                 Assert.Fail ("BeginConnectMultipleListen #1");
2256                         } catch (InvalidOperationException) {
2257                         } catch {
2258                                 Assert.Fail ("BeginConnectMultipleListen #2");
2259                         } finally {
2260                                 sock.Close ();
2261                         }
2262                 }
2263                 
2264                 [Test]
2265                 [ExpectedException (typeof(ObjectDisposedException))]
2266 #if TARGET_JVM
2267         [Ignore("System.Net.Sockets.Socket.BeginConnect(IPAddress[],int,AsyncCallback,object) is not supported")]
2268 #endif
2269                 public void BeginConnectMultipleClosed ()
2270                 {
2271                         Socket sock = new Socket (AddressFamily.InterNetwork,
2272                                                   SocketType.Stream,
2273                                                   ProtocolType.Tcp);
2274                         IPAddress[] ips = new IPAddress[4];
2275                         
2276                         ips[0] = IPAddress.Parse ("127.0.0.4");
2277                         ips[1] = IPAddress.Parse ("127.0.0.3");
2278                         ips[2] = IPAddress.Parse ("127.0.0.2");
2279                         ips[3] = IPAddress.Parse ("127.0.0.1");
2280                         
2281                         sock.Close ();
2282                         
2283                         sock.BeginConnect (ips, 1247, BCCallback, sock);
2284                 }
2285                 
2286                 [Test]
2287 #if TARGET_JVM
2288         [Ignore("System.Net.Sockets.Socket.BeginConnect(IPAddress,int,AsyncCallback,object) is not supported")]
2289 #endif
2290                 public void BeginConnectHostPortNull ()
2291                 {
2292                         Socket sock = new Socket (AddressFamily.InterNetwork,
2293                                                   SocketType.Stream,
2294                                                   ProtocolType.Tcp);
2295                         
2296                         try {
2297                                 sock.BeginConnect ((string)null, 0,
2298                                                    BCCallback, sock);
2299                                 Assert.Fail ("BeginConnectHostPort #1");
2300                         } catch (ArgumentNullException) {
2301                         } catch {
2302                                 Assert.Fail ("BeginConnectHostPort #2");
2303                         } finally {
2304                                 sock.Close ();
2305                         }
2306                 }
2307
2308                 [Test]
2309 #if TARGET_JVM
2310         [Ignore("System.Net.Sockets.Socket.BeginConnect(string,int,AsyncCallback,object) is not supported")]
2311 #endif
2312                 public void BeginConnectHostPortListen ()
2313                 {
2314                         Socket sock = new Socket (AddressFamily.InterNetwork,
2315                                                   SocketType.Stream,
2316                                                   ProtocolType.Tcp);
2317                         IPAddress ip = IPAddress.Loopback;
2318                         IPEndPoint ep = new IPEndPoint (ip, 1248);
2319                         
2320                         sock.Bind (ep);
2321                         sock.Listen (1);
2322                         
2323                         try {
2324                                 sock.BeginConnect ("localhost", 1248,
2325                                                    BCCallback, sock);
2326                                 Assert.Fail ("BeginConnectHostPortListen #1");
2327                         } catch (InvalidOperationException) {
2328                         } catch {
2329                                 Assert.Fail ("BeginConnectHostPortListen #2");
2330                         } finally {
2331                                 sock.Close ();
2332                         }
2333                 }
2334
2335                 [Test]
2336                 [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
2337                 public void BeginConnectHostPortNotIP ()
2338                 {
2339                         Socket sock = new Socket (AddressFamily.NetBios,
2340                                                   SocketType.Seqpacket,
2341                                                   ProtocolType.Unspecified);
2342                         
2343                         try {
2344                                 sock.BeginConnect ("localhost", 0, BCCallback,
2345                                                    sock);
2346                                 Assert.Fail ("BeginConnectHostPortNotIP #1");
2347                         } catch (NotSupportedException) {
2348                         } catch {
2349                                 Assert.Fail ("BeginConnectHostPortNotIP #2");
2350                         } finally {
2351                                 sock.Close ();
2352                         }
2353                 }
2354
2355                 [Test]
2356                 [ExpectedException (typeof(ObjectDisposedException))]
2357 #if TARGET_JVM
2358         [Ignore("System.Net.Sockets.Socket.BeginConnect(string,int,AsyncCallback,object) is not supported")]
2359 #endif
2360                 public void BeginConnectHostPortClosed ()
2361                 {
2362                         Socket sock = new Socket (AddressFamily.InterNetwork,
2363                                                   SocketType.Stream,
2364                                                   ProtocolType.Tcp);
2365                         
2366                         sock.Close ();
2367                         
2368                         sock.BeginConnect ("localhost", 0, BCCallback, sock);
2369                 }
2370                 
2371                 static bool BDDisconnected = false;
2372                 static ManualResetEvent BDCalledBack = new ManualResetEvent (false);
2373                 
2374                 private static void BDCallback (IAsyncResult asyncResult)
2375                 {
2376                         Socket sock = (Socket)asyncResult.AsyncState;
2377                         
2378                         sock.EndDisconnect (asyncResult);
2379                         BDDisconnected = true;
2380                         
2381                         BDCalledBack.Set ();
2382                 }
2383                 
2384                 [Test]
2385                 [Category ("NotDotNet")] // "Needs XP or later"
2386  #if TARGET_JVM
2387         [Ignore("System.Net.Sockets.Socket.BeginDisconnect method is not supported")]
2388 #endif
2389                 public void BeginDisconnect ()
2390                 {
2391                         Socket sock = new Socket (AddressFamily.InterNetwork,
2392                                                   SocketType.Stream,
2393                                                   ProtocolType.Tcp);
2394                         Socket listen = new Socket (AddressFamily.InterNetwork,
2395                                                     SocketType.Stream,
2396                                                     ProtocolType.Tcp);
2397                         IPAddress ip = IPAddress.Loopback;
2398                         IPEndPoint ep = new IPEndPoint (ip, 1254);
2399                         
2400                         listen.Bind (ep);
2401                         listen.Listen (1);
2402                         
2403                         sock.Connect (ip, 1254);
2404                         
2405                         Assertion.AssertEquals ("BeginDisconnect #1", true,
2406                                                 sock.Connected);
2407                         
2408                         sock.Shutdown (SocketShutdown.Both);
2409
2410                         BDCalledBack.Reset ();
2411                         BDDisconnected = false;
2412                         
2413                         sock.BeginDisconnect (false, BDCallback, sock);
2414                 
2415                         if (BDCalledBack.WaitOne (2000, false) == false) {
2416                                 Assert.Fail ("BeginDisconnect wait timed out");
2417                         }
2418                         
2419                         Assertion.AssertEquals ("BeginDisconnect #2", true,
2420                                                 BDDisconnected);
2421                         Assertion.AssertEquals ("BeginDisconnect #3", false,
2422                                                 sock.Connected);
2423                         
2424                         sock.Close ();
2425                         listen.Close ();
2426                 }
2427                 
2428                 [Test]
2429                 public void BeginReceiveSocketError ()
2430                 {
2431                 }
2432                 
2433                 [Test]
2434                 public void BeginReceiveGeneric ()
2435                 {
2436                 }
2437                 
2438                 [Test]
2439                 public void BeginReceiveGenericSocketError ()
2440                 {
2441                 }
2442                 
2443                 private static void BSCallback (IAsyncResult asyncResult)
2444                 {
2445                         Socket sock = (Socket)asyncResult.AsyncState;
2446                         
2447                         sock.EndSend (asyncResult);
2448                 }
2449                 
2450                 [Test]
2451 #if TARGET_JVM
2452         [Ignore ("System.Net.Sockets.Socket.BeginSend(byte[] ...) is not supported")]
2453 #endif
2454                 public void BeginSendNotConnected ()
2455                 {
2456                         Socket sock = new Socket (AddressFamily.InterNetwork,
2457                                                   SocketType.Stream,
2458                                                   ProtocolType.Tcp);
2459                         
2460                         byte[] send_bytes = new byte[] {10, 11, 12, 13};
2461                         
2462                         try {
2463                                 sock.BeginSend (send_bytes, 0,
2464                                                 send_bytes.Length,
2465                                                 SocketFlags.None, BSCallback,
2466                                                 sock);
2467                                 Assert.Fail ("BeginSendNotConnected #1");
2468                         } catch (SocketException ex) {
2469                                 Assertion.AssertEquals ("BeginSendNotConnected #2", 10057, ex.ErrorCode);
2470                         } catch {
2471                                 Assert.Fail ("BeginSendNotConnected #3");
2472                         } finally {
2473                                 sock.Close ();
2474                         }
2475                 }
2476                 
2477                 [Test]
2478                 public void BeginSendSocketError ()
2479                 {
2480                 }
2481                 
2482                 [Test]
2483                 public void BeginSendGeneric ()
2484                 {
2485                 }
2486                 
2487                 [Test]
2488                 public void BeginSendGenericSocketError ()
2489                 {
2490                 }
2491                 
2492                 [Test]
2493                 public void BindTwice ()
2494                 {
2495                         Socket sock = new Socket (AddressFamily.InterNetwork,
2496                                                   SocketType.Stream,
2497                                                   ProtocolType.Tcp);
2498                         IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
2499                                                         1256);
2500                         IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
2501                                                          1257);
2502                         
2503                         sock.Bind (ep1);
2504                         
2505                         try {
2506                                 sock.Bind (ep2);
2507                                 Assert.Fail ("BindTwice #1");
2508                         } catch (SocketException ex) {
2509                                 Assertion.AssertEquals ("BindTwice #2",
2510                                                         10022, ex.ErrorCode);
2511                         } catch {
2512                                 Assert.Fail ("BindTwice #3");
2513                         } finally {
2514                                 sock.Close ();
2515                         }
2516                 }
2517                 
2518                 [Test]
2519 #if TARGET_JVM
2520         [Ignore ("System.Net.Sockets.Socket.Close(int) method is not supported")]
2521 #endif
2522                 public void Close ()
2523                 {
2524                         Socket sock = new Socket (AddressFamily.InterNetwork,
2525                                                   SocketType.Stream,
2526                                                   ProtocolType.Tcp);
2527                         Socket listen = new Socket (AddressFamily.InterNetwork,
2528                                                     SocketType.Stream,
2529                                                     ProtocolType.Tcp);
2530                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2531                                                         1258);
2532                         
2533                         listen.Bind (ep);
2534                         listen.Listen (1);
2535                         
2536                         sock.Connect (ep);
2537
2538                         Assertion.AssertEquals ("Close #1", true,
2539                                                 sock.Connected);
2540                         
2541                         sock.Close (2);
2542                         
2543                         Thread.Sleep (3000);
2544                         
2545                         Assertion.AssertEquals ("Close #2", false,
2546                                                 sock.Connected);
2547                         
2548                         listen.Close ();
2549                 }
2550                 
2551                 [Test]
2552 #if TARGET_JVM
2553         [Ignore("System.Net.Sockets.Socket.Connect(IPAddress,int) is not supported")]
2554 #endif
2555                 public void ConnectAddressPort ()
2556                 {
2557                         Socket sock = new Socket (AddressFamily.InterNetwork,
2558                                                   SocketType.Stream,
2559                                                   ProtocolType.Tcp);
2560                         Socket listen = new Socket (AddressFamily.InterNetwork,
2561                                                     SocketType.Stream,
2562                                                     ProtocolType.Tcp);
2563                         IPAddress ip = IPAddress.Loopback;
2564                         IPEndPoint ep = new IPEndPoint (ip, 1249);
2565
2566                         listen.Bind (ep);
2567                         listen.Listen (1);
2568                         
2569                         sock.Connect (ip, 1249);
2570                         
2571                         Assertion.AssertEquals ("ConnectAddressPort #1",
2572                                                 true, sock.Connected);
2573                         
2574                         sock.Close ();
2575                         listen.Close ();
2576                 }
2577
2578                 [Test]
2579 #if TARGET_JVM
2580         [Ignore("System.Net.Sockets.Socket.Connect(IPAddress,int) is not supported")]
2581 #endif
2582                 public void ConnectAddressPortNull ()
2583                 {
2584                         Socket sock = new Socket (AddressFamily.InterNetwork,
2585                                                   SocketType.Stream,
2586                                                   ProtocolType.Tcp);
2587                         IPAddress ip = null;
2588
2589                         try {
2590                                 sock.Connect (ip, 1249);
2591                                 Assert.Fail ("ConnectAddressPortNull #1");
2592                         } catch (ArgumentNullException) {
2593                         } catch {
2594                                 Assert.Fail ("ConnectAddressPortNull #2");
2595                         } finally {
2596                                 sock.Close ();
2597                         }
2598                 }
2599
2600                 [Test]
2601 #if TARGET_JVM
2602         [Ignore("System.Net.Sockets.Socket.Connect(IPAddress,int) is not supported")]
2603 #endif
2604                 public void ConnectAddressPortListen ()
2605                 {
2606                         Socket sock = new Socket (AddressFamily.InterNetwork,
2607                                                   SocketType.Stream,
2608                                                   ProtocolType.Tcp);
2609                         IPAddress ip = IPAddress.Loopback;
2610                         IPEndPoint ep = new IPEndPoint (ip, 1250);
2611
2612                         sock.Bind (ep);
2613                         sock.Listen (1);
2614                         
2615                         try {
2616                                 sock.Connect (ip, 1250);
2617                                 Assert.Fail ("ConnectAddressPortListen #1");
2618                         } catch (InvalidOperationException) {
2619                         } catch {
2620                                 Assert.Fail ("ConnectAddressPortListen #2");
2621                         } finally {
2622                                 sock.Close ();
2623                         }
2624                 }
2625                 
2626                 [Test]
2627                 [ExpectedException (typeof(ObjectDisposedException))]
2628 #if TARGET_JVM
2629         [Ignore("System.Net.Sockets.Socket.Connect(IPAddress,int) is not supported")]
2630 #endif
2631                 public void ConnectAddressPortClosed ()
2632                 {
2633                         Socket sock = new Socket (AddressFamily.InterNetwork,
2634                                                   SocketType.Stream,
2635                                                   ProtocolType.Tcp);
2636                         IPAddress ip = IPAddress.Loopback;
2637                         
2638                         sock.Close ();
2639                         
2640                         sock.Connect (ip, 1250);
2641                 }
2642                 
2643                 [Test]
2644 #if TARGET_JVM
2645         [Ignore("System.Net.Sockets.Socket.Connect(IPAddress[],int) is not supported")]
2646 #endif
2647                 public void ConnectMultiple ()
2648                 {
2649                         Socket sock = new Socket (AddressFamily.InterNetwork,
2650                                                   SocketType.Stream,
2651                                                   ProtocolType.Tcp);
2652                         Socket listen = new Socket (AddressFamily.InterNetwork,
2653                                                     SocketType.Stream,
2654                                                     ProtocolType.Tcp);
2655                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2656                                                         1251);
2657                         IPAddress[] ips = new IPAddress[4];
2658                         
2659                         ips[0] = IPAddress.Parse ("127.0.0.4");
2660                         ips[1] = IPAddress.Parse ("127.0.0.3");
2661                         ips[2] = IPAddress.Parse ("127.0.0.2");
2662                         ips[3] = IPAddress.Parse ("127.0.0.1");
2663
2664                         listen.Bind (ep);
2665                         listen.Listen (1);
2666                         
2667                         sock.Connect (ips, 1251);
2668                         
2669                         Assertion.AssertEquals ("ConnectMultiple #1",
2670                                                 true, sock.Connected);
2671                         Assertion.AssertEquals ("ConnectMultiple #2",
2672                                                 AddressFamily.InterNetwork,
2673                                                 sock.RemoteEndPoint.AddressFamily);
2674                         IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
2675                         
2676                         Assertion.AssertEquals ("ConnectMultiple #2",
2677                                                 IPAddress.Loopback,
2678                                                 remep.Address);
2679                         
2680                         sock.Close ();
2681                         listen.Close ();
2682                 }
2683
2684                 [Test]
2685 #if TARGET_JVM
2686         [Ignore("System.Net.Sockets.Socket.Connect(IPAddress[],int) is not supported")]
2687 #endif
2688                 public void ConnectMultipleNull ()
2689                 {
2690                         Socket sock = new Socket (AddressFamily.InterNetwork,
2691                                                   SocketType.Stream,
2692                                                   ProtocolType.Tcp);
2693                         IPAddress[] ips = null;
2694                         
2695                         try {
2696                                 sock.Connect (ips, 1251);
2697                                 Assert.Fail ("ConnectMultipleNull #1");
2698                         } catch (ArgumentNullException) {
2699                         } catch {
2700                                 Assert.Fail ("ConnectMultipleNull #2");
2701                         } finally {
2702                                 sock.Close ();
2703                         }
2704                 }
2705
2706                 [Test]
2707 #if TARGET_JVM
2708         [Ignore("System.Net.Sockets.Socket.Connect(IPAddress[],int) is not supported")]
2709 #endif
2710                 public void ConnectMultipleListen ()
2711                 {
2712                         Socket sock = new Socket (AddressFamily.InterNetwork,
2713                                                   SocketType.Stream,
2714                                                   ProtocolType.Tcp);
2715                         IPAddress[] ips = new IPAddress[4];
2716                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2717                                                         1252);
2718                         
2719                         ips[0] = IPAddress.Parse ("127.0.0.4");
2720                         ips[1] = IPAddress.Parse ("127.0.0.3");
2721                         ips[2] = IPAddress.Parse ("127.0.0.2");
2722                         ips[3] = IPAddress.Parse ("127.0.0.1");
2723                         
2724                         sock.Bind (ep);
2725                         sock.Listen (1);
2726                         
2727                         try {
2728                                 sock.Connect (ips, 1252);
2729                                 Assert.Fail ("ConnectMultipleListen #1");
2730                         } catch (InvalidOperationException) {
2731                         } catch {
2732                                 Assert.Fail ("ConnectMultipleListen #2");
2733                         } finally {
2734                                 sock.Close ();
2735                         }
2736                 }
2737                 
2738                 [Test]
2739                 [ExpectedException (typeof(ObjectDisposedException))]
2740 #if TARGET_JVM
2741         [Ignore("System.Net.Sockets.Socket.Connect(IPAddress[],int) is not supported")]
2742 #endif
2743                 public void ConnectMultipleClosed ()
2744                 {
2745                         Socket sock = new Socket (AddressFamily.InterNetwork,
2746                                                   SocketType.Stream,
2747                                                   ProtocolType.Tcp);
2748                         IPAddress[] ips = new IPAddress[4];
2749                         
2750                         ips[0] = IPAddress.Parse ("127.0.0.4");
2751                         ips[1] = IPAddress.Parse ("127.0.0.3");
2752                         ips[2] = IPAddress.Parse ("127.0.0.2");
2753                         ips[3] = IPAddress.Parse ("127.0.0.1");
2754                         
2755                         sock.Close ();
2756                         
2757                         sock.Connect (ips, 1252);
2758                 }
2759                 
2760                 [Test]
2761 #if TARGET_JVM
2762         [Ignore("System.Net.Sockets.Socket.Connect(string,int) is not supported")]
2763 #endif
2764                 public void ConnectHostPortNull ()
2765                 {
2766                         Socket sock = new Socket (AddressFamily.InterNetwork,
2767                                                   SocketType.Stream,
2768                                                   ProtocolType.Tcp);
2769                         
2770                         try {
2771                                 sock.Connect ((string)null, 0);
2772                                 Assert.Fail ("ConnectHostPort #1");
2773                         } catch (ArgumentNullException) {
2774                         } catch {
2775                                 Assert.Fail ("ConnectHostPort #2");
2776                         } finally {
2777                                 sock.Close ();
2778                         }
2779                 }
2780
2781                 [Test]
2782 #if TARGET_JVM
2783         [Ignore("System.Net.Sockets.Socket.Connect(string,int) is not supported")]
2784 #endif
2785                 public void ConnectHostPortListen ()
2786                 {
2787                         Socket sock = new Socket (AddressFamily.InterNetwork,
2788                                                   SocketType.Stream,
2789                                                   ProtocolType.Tcp);
2790                         IPAddress ip = IPAddress.Loopback;
2791                         IPEndPoint ep = new IPEndPoint (ip, 1253);
2792                         
2793                         sock.Bind (ep);
2794                         sock.Listen (1);
2795                         
2796                         try {
2797                                 sock.Connect ("localhost", 1253);
2798                                 Assert.Fail ("ConnectHostPortListen #1");
2799                         } catch (InvalidOperationException) {
2800                         } catch {
2801                                 Assert.Fail ("ConnectHostPortListen #2");
2802                         } finally {
2803                                 sock.Close ();
2804                         }
2805                 }
2806
2807                 [Test]
2808                 [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
2809                 public void ConnectHostPortNotIP ()
2810                 {
2811                         Socket sock = new Socket (AddressFamily.NetBios,
2812                                                   SocketType.Seqpacket,
2813                                                   ProtocolType.Unspecified);
2814                         
2815                         try {
2816                                 sock.Connect ("localhost", 0);
2817                                 Assert.Fail ("ConnectHostPortNotIP #1");
2818                         } catch (NotSupportedException) {
2819                         } catch {
2820                                 Assert.Fail ("ConnectHostPortNotIP #2");
2821                         } finally {
2822                                 sock.Close ();
2823                         }
2824                 }
2825
2826                 [Test]
2827                 [ExpectedException (typeof(ObjectDisposedException))]
2828 #if TARGET_JVM
2829         [Ignore("System.Net.Sockets.Socket.Connect(string,int) is not supported")]
2830 #endif
2831                 public void ConnectHostPortClosed ()
2832                 {
2833                         Socket sock = new Socket (AddressFamily.InterNetwork,
2834                                                   SocketType.Stream,
2835                                                   ProtocolType.Tcp);
2836                         
2837                         sock.Close ();
2838                         
2839                         sock.Connect ("localhost", 0);
2840                 }
2841                 
2842                 [Test]
2843                 [Category ("NotDotNet")] // "Needs XP or later"
2844 #if TARGET_JVM
2845         [Ignore ("System.Net.Sockets.Socket.Connect(IPAddress,int) is not supported")]
2846 #endif
2847                 public void Disconnect ()
2848                 {
2849                         Socket sock = new Socket (AddressFamily.InterNetwork,
2850                                                   SocketType.Stream,
2851                                                   ProtocolType.Tcp);
2852                         Socket listen = new Socket (AddressFamily.InterNetwork,
2853                                                     SocketType.Stream,
2854                                                     ProtocolType.Tcp);
2855                         IPAddress ip = IPAddress.Loopback;
2856                         IPEndPoint ep = new IPEndPoint (ip, 1255);
2857                         
2858                         listen.Bind (ep);
2859                         listen.Listen (1);
2860                         
2861                         sock.Connect (ip, 1255);
2862                         
2863                         Assertion.AssertEquals ("Disconnect #1", true,
2864                                                 sock.Connected);
2865                         
2866                         sock.Shutdown (SocketShutdown.Both);
2867
2868                         sock.Disconnect (false);
2869
2870                         Assertion.AssertEquals ("BeginDisconnect #3", false,
2871                                                 sock.Connected);
2872                         
2873                         sock.Close ();
2874                         listen.Close ();
2875                 }
2876                 
2877                 [Test]
2878                 public void DuplicateAndClose ()
2879                 {
2880                 }
2881                 
2882                 [Test]
2883                 public void IOControl ()
2884                 {
2885                 }
2886                 
2887                 [Test]
2888                 public void ReceiveGeneric ()
2889                 {
2890                 }
2891                 
2892                 [Test]
2893                 public void ReceiveGenericSocketFlags ()
2894                 {
2895                 }
2896                 
2897                 [Test]
2898                 public void ReceiveGenericSocketFlagsSocketError ()
2899                 {
2900                 }
2901                 
2902                 [Test]
2903                 public void SendGeneric ()
2904                 {
2905                 }
2906                 
2907                 [Test]
2908                 public void SendGenericSocketFlags ()
2909                 {
2910                 }
2911                 
2912                 [Test]
2913                 public void SendGenericSocketFlagsSocketError ()
2914                 {
2915                 }
2916
2917                 [Test]
2918                 public void ListenNotBound ()
2919                 {
2920                         Socket sock = new Socket (AddressFamily.InterNetwork,
2921                                                   SocketType.Stream,
2922                                                   ProtocolType.Tcp);
2923                         
2924                         try {
2925                                 sock.Listen (1);
2926                                 Assert.Fail ("ListenNotBound #1");
2927                         } catch (SocketException ex) {
2928                                 Assertion.AssertEquals ("ListenNotBound #2",
2929                                                         10022, ex.ErrorCode);
2930                         } catch {
2931                                 Assert.Fail ("ListenNotBound #3");
2932                         } finally {
2933                                 sock.Close ();
2934                         }
2935                 }
2936 #endif
2937         }
2938 }
2939