Reduce/remove the probability of having test failures wrt UdpClient.Available
[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 #if !TARGET_JVM
1753                         Socket sock = (Socket)asyncResult.AsyncState;
1754                         
1755                         BADSocket = sock.EndAccept (out BADBytes,
1756                                                     out BADByteCount,
1757                                                     asyncResult);
1758                         
1759                         BADAccepted = true;
1760                         BADCalledBack.Set ();
1761 #endif
1762                 }
1763
1764                 [Test]
1765 #if TARGET_JVM
1766         [Ignore ("System.Net.Sockets.Socket.BeginAccept(int,AsyncCallback,object) is not supported")]
1767 #endif
1768                 public void BeginAcceptData ()
1769                 {
1770                         Socket sock = new Socket (AddressFamily.InterNetwork,
1771                                                   SocketType.Stream,
1772                                                   ProtocolType.Tcp);
1773                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1774                                                         1238);
1775                         
1776                         sock.Bind (ep);
1777                         sock.Listen (1);
1778                         
1779                         BADCalledBack.Reset ();
1780                         
1781                         sock.BeginAccept (256, BADCallback, sock);
1782
1783                         Socket conn = new Socket (AddressFamily.InterNetwork,
1784                                                   SocketType.Stream,
1785                                                   ProtocolType.Tcp);
1786                         byte[] send_bytes = new byte[] {10, 11, 12, 13};
1787                         
1788                         conn.Connect (ep);
1789                         conn.Send (send_bytes);
1790
1791                         if (BADCalledBack.WaitOne (2000, false) == false) {
1792                                 Assert.Fail ("BeginAcceptData wait timed out");
1793                         }
1794                         
1795                         Assertion.AssertEquals ("BeginAcceptData #1", true,
1796                                                 BADAccepted);
1797                         Assertion.AssertEquals ("BeginAcceptData #2", true,
1798                                                 BADSocket.Connected);
1799                         Assertion.AssertEquals ("BeginAcceptData #3", false,
1800                                                 sock.Connected);
1801                         Assertion.AssertEquals ("BeginAcceptData #4", true,
1802                                                 conn.Connected);
1803                         Assertion.AssertEquals ("BeginAcceptData #5",
1804                                                 send_bytes.Length,
1805                                                 BADByteCount);
1806                         
1807                         /* The MS runtime gives the returned data in a
1808                          * much bigger array.  TODO: investigate
1809                          * whether it the size correlates to the first
1810                          * parameter in BeginAccept()
1811                          */
1812                         Assert.IsFalse (BADBytes.Length == send_bytes.Length,
1813                                         "BeginAcceptData #6");
1814
1815                         for(int i = 0; i < send_bytes.Length; i++) {
1816                                 Assertion.AssertEquals ("BeginAcceptData #" + (i+7).ToString (), send_bytes[i], BADBytes[i]);
1817                         }
1818
1819                         BADSocket.Close ();
1820                         conn.Close ();
1821                         sock.Close ();
1822                 }
1823
1824                 [Test]
1825                 [ExpectedException (typeof(ObjectDisposedException))]
1826 #if TARGET_JVM
1827         [Ignore("System.Net.Sockets.Socket.BeginAccept(int,AsyncCallback,object) is not supported")]
1828 #endif
1829                 public void BeginAcceptDataClosed ()
1830                 {
1831                         Socket sock = new Socket (AddressFamily.InterNetwork,
1832                                                   SocketType.Stream,
1833                                                   ProtocolType.Tcp);
1834                         
1835                         sock.Close ();
1836                         
1837                         sock.BeginAccept (256, BADCallback, sock);
1838                 }
1839
1840                 [Test]
1841 #if TARGET_JVM
1842         [Ignore("System.Net.Sockets.Socket.BeginAccept(Socket,int,AsyncCallback,object) is not supported")]
1843 #endif
1844                 public void BeginAcceptSocketUdp ()
1845                 {
1846                         Socket sock = new Socket (AddressFamily.InterNetwork,
1847                                                   SocketType.Stream,
1848                                                   ProtocolType.Tcp);
1849                         Socket acc = new Socket (AddressFamily.InterNetwork,
1850                                                  SocketType.Dgram,
1851                                                  ProtocolType.Udp);
1852                         
1853                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1854                                                         1239);
1855                         
1856                         sock.Bind (ep);
1857                         sock.Listen (1);
1858                         
1859                         try {
1860                                 sock.BeginAccept (acc, 256, BADCallback, sock);
1861                                 Assert.Fail ("BeginAcceptSocketUdp #1");
1862                         } catch (SocketException ex) {
1863                                 Assertion.AssertEquals ("BeginAcceptSocketUdp #2", 10022, ex.ErrorCode);
1864                         } catch {
1865                                 Assert.Fail ("BeginAcceptSocketUdp #3");
1866                         } finally {
1867                                 acc.Close ();
1868                                 sock.Close ();
1869                         }
1870                 }
1871                 
1872                 [Test]
1873 #if TARGET_JVM
1874         [Ignore("System.Net.Sockets.Socket.BeginAccept(Socket,int,AsyncCallback,object) is not supported")]
1875 #endif
1876                 public void BeginAcceptSocketBound ()
1877                 {
1878                         Socket sock = new Socket (AddressFamily.InterNetwork,
1879                                                   SocketType.Stream,
1880                                                   ProtocolType.Tcp);
1881                         Socket acc = new Socket (AddressFamily.InterNetwork,
1882                                                  SocketType.Stream,
1883                                                  ProtocolType.Tcp);
1884                         
1885                         IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
1886                                                          1240);
1887                         
1888                         IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
1889                                                          1241);
1890                         
1891                         sock.Bind (ep1);
1892                         sock.Listen (1);
1893
1894                         acc.Bind (ep2);
1895                         
1896                         try {
1897                                 sock.BeginAccept (acc, 256, BADCallback, sock);
1898                                 Assert.Fail ("BeginAcceptSocketBound #1");
1899                         } catch (InvalidOperationException) {
1900                         } catch {
1901                                 Assert.Fail ("BeginAcceptSocketBound #2");
1902                         } finally {
1903                                 acc.Close ();
1904                                 sock.Close ();
1905                         }
1906                 }
1907                 
1908                 [Test]
1909 #if TARGET_JVM
1910         [Ignore("System.Net.Sockets.Socket.BeginAccept(Socket,int,AsyncCallback,object) is not supported")]
1911 #endif
1912                 public void BeginAcceptSocket ()
1913                 {
1914                         Socket sock = new Socket (AddressFamily.InterNetwork,
1915                                                   SocketType.Stream,
1916                                                   ProtocolType.Tcp);
1917                         Socket acc = new Socket (AddressFamily.InterNetwork,
1918                                                  SocketType.Stream,
1919                                                  ProtocolType.Tcp);
1920                         
1921                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1922                                                         1242);
1923                         
1924                         sock.Bind (ep);
1925                         sock.Listen (1);
1926                         
1927                         BADCalledBack.Reset ();
1928                         
1929                         sock.BeginAccept (acc, 256, BADCallback, sock);
1930
1931                         Socket conn = new Socket (AddressFamily.InterNetwork,
1932                                                   SocketType.Stream,
1933                                                   ProtocolType.Tcp);
1934                         byte[] send_bytes = new byte[] {10, 11, 12, 13};
1935                         
1936                         conn.Connect (ep);
1937                         conn.Send (send_bytes);
1938
1939                         if (BADCalledBack.WaitOne (2000, false) == false) {
1940                                 Assert.Fail ("BeginAcceptSocket wait timed out");
1941                         }
1942                         
1943                         Assertion.AssertEquals ("BeginAcceptSocket #1", true,
1944                                                 BADAccepted);
1945                         Assertion.AssertEquals ("BeginAcceptSocket #2", true,
1946                                                 BADSocket.Connected);
1947                         Assertion.AssertEquals ("BeginAcceptSocket #3", false,
1948                                                 sock.Connected);
1949                         Assertion.AssertEquals ("BeginAcceptSocket #4", true,
1950                                                 conn.Connected);
1951                         Assertion.AssertEquals ("BeginAcceptSocket #5",
1952                                                 send_bytes.Length,
1953                                                 BADByteCount);
1954                         Assertion.AssertEquals ("BeginAcceptSocket #6",
1955                                                 AddressFamily.InterNetwork,
1956                                                 acc.AddressFamily);
1957                         Assertion.AssertEquals ("BeginAcceptSocket #7",
1958                                                 SocketType.Stream,
1959                                                 acc.SocketType);
1960                         Assertion.AssertEquals ("BeginAcceptSocket #8",
1961                                                 ProtocolType.Tcp,
1962                                                 acc.ProtocolType);
1963                         Assertion.AssertEquals ("BeginAcceptSocket #9",
1964                                                 conn.LocalEndPoint,
1965                                                 acc.RemoteEndPoint);
1966                         
1967                         /* The MS runtime gives the returned data in a
1968                          * much bigger array.  TODO: investigate
1969                          * whether it the size correlates to the first
1970                          * parameter in BeginAccept()
1971                          */
1972                         Assert.IsFalse (BADBytes.Length == send_bytes.Length,
1973                                         "BeginAcceptSocket #10");
1974
1975                         for(int i = 0; i < send_bytes.Length; i++) {
1976                                 Assertion.AssertEquals ("BeginAcceptSocket #" + (i+11).ToString (), send_bytes[i], BADBytes[i]);
1977                         }
1978
1979                         BADSocket.Close ();
1980                         conn.Close ();
1981                         acc.Close ();
1982                         sock.Close ();
1983                 }
1984
1985                 [Test]
1986 #if TARGET_JVM
1987         [Ignore("System.Net.Sockets.Socket.BeginAccept(Socket,int,AsyncCallback,object) is not supported")]
1988 #endif
1989                 public void BeginAcceptSocketClosed ()
1990                 {
1991                         Socket sock = new Socket (AddressFamily.InterNetwork,
1992                                                   SocketType.Stream,
1993                                                   ProtocolType.Tcp);
1994                         Socket acc = new Socket (AddressFamily.InterNetwork,
1995                                                  SocketType.Stream,
1996                                                  ProtocolType.Tcp);
1997                         
1998                         sock.Close ();
1999                         
2000                         try {
2001                                 sock.BeginAccept (acc, 256, BADCallback, null);
2002                                 Assert.Fail ("BeginAcceptSocketClosed #1");
2003                         } catch (ObjectDisposedException) {
2004                         } catch {
2005                                 Assert.Fail ("BeginAcceptSocketClosed #2");
2006                         } finally {
2007                                 acc.Close ();
2008                         }
2009                 }
2010
2011                 [Test]
2012 #if TARGET_JVM
2013         [Ignore("System.Net.Sockets.Socket.BeginAccept(Socket,int,AsyncCallback,object) is not supported")]
2014 #endif
2015                 public void BeginAcceptSocketAccClosed ()
2016                 {
2017                         Socket sock = new Socket (AddressFamily.InterNetwork,
2018                                                   SocketType.Stream,
2019                                                   ProtocolType.Tcp);
2020                         Socket acc = new Socket (AddressFamily.InterNetwork,
2021                                                  SocketType.Stream,
2022                                                  ProtocolType.Tcp);
2023                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2024                                                         1243);
2025
2026                         sock.Bind (ep);
2027                         sock.Listen (1);
2028                         
2029                         acc.Close ();
2030                         
2031                         BADCalledBack.Reset ();
2032                         
2033                         try {
2034                                 sock.BeginAccept (acc, 256, BADCallback, null);
2035                                 Assert.Fail ("BeginAcceptSocketAccClosed #1");
2036                         } catch (ObjectDisposedException) {
2037                         } catch {
2038                                 Assert.Fail ("BeginAcceptSocketAccClosed #2");
2039                         } finally {
2040                                 sock.Close ();
2041                         }
2042                 }
2043                 
2044                 static bool BCConnected = false;
2045                 static ManualResetEvent BCCalledBack = new ManualResetEvent (false);
2046                 
2047                 private static void BCCallback (IAsyncResult asyncResult)
2048                 {
2049                         Socket sock = (Socket)asyncResult.AsyncState;
2050                         
2051                         sock.EndConnect (asyncResult);
2052                         BCConnected = true;
2053                         
2054                         BCCalledBack.Set ();
2055                 }
2056                 
2057                 [Test]
2058 #if TARGET_JVM
2059         [Ignore("System.Net.Sockets.Socket.BeginConnect(IPAddress,int,AsyncCallback,object) is not supported")]
2060 #endif
2061                 public void BeginConnectAddressPort ()
2062                 {
2063                         Socket sock = new Socket (AddressFamily.InterNetwork,
2064                                                   SocketType.Stream,
2065                                                   ProtocolType.Tcp);
2066                         Socket listen = new Socket (AddressFamily.InterNetwork,
2067                                                     SocketType.Stream,
2068                                                     ProtocolType.Tcp);
2069                         IPAddress ip = IPAddress.Loopback;
2070                         IPEndPoint ep = new IPEndPoint (ip, 1244);
2071
2072                         listen.Bind (ep);
2073                         listen.Listen (1);
2074                         
2075                         BCCalledBack.Reset ();
2076                         
2077                         BCConnected = false;
2078                         
2079                         sock.BeginConnect (ip, 1244, BCCallback, sock);
2080
2081                         if (BCCalledBack.WaitOne (2000, false) == false) {
2082                                 Assert.Fail ("BeginConnectAddressPort wait timed out");
2083                         }
2084                         
2085                         Assertion.AssertEquals ("BeginConnectAddressPort #1",
2086                                                 true, BCConnected);
2087                         
2088                         sock.Close ();
2089                         listen.Close ();
2090                 }
2091
2092                 [Test]
2093 #if TARGET_JVM
2094         [Ignore("System.Net.Sockets.Socket.BeginConnect(IPAddress,int,AsyncCallback,object) is not supported")]
2095 #endif
2096                 public void BeginConnectAddressPortNull ()
2097                 {
2098                         Socket sock = new Socket (AddressFamily.InterNetwork,
2099                                                   SocketType.Stream,
2100                                                   ProtocolType.Tcp);
2101                         IPAddress ip = null;
2102
2103                         try {
2104                                 sock.BeginConnect (ip, 1244, BCCallback,
2105                                                    sock);
2106                                 Assert.Fail ("BeginConnectAddressPortNull #1");
2107                         } catch (ArgumentNullException) {
2108                         } catch {
2109                                 Assert.Fail ("BeginConnectAddressPortNull #2");
2110                         } finally {
2111                                 sock.Close ();
2112                         }
2113                 }
2114
2115                 [Test]
2116 #if TARGET_JVM
2117         [Ignore("System.Net.Sockets.Socket.BeginConnect(IPAddress,int,AsyncCallback,object) is not supported")]
2118 #endif
2119                 public void BeginConnectAddressPortListen ()
2120                 {
2121                         Socket sock = new Socket (AddressFamily.InterNetwork,
2122                                                   SocketType.Stream,
2123                                                   ProtocolType.Tcp);
2124                         IPAddress ip = IPAddress.Loopback;
2125                         IPEndPoint ep = new IPEndPoint (ip, 1245);
2126
2127                         sock.Bind (ep);
2128                         sock.Listen (1);
2129                         
2130                         try {
2131                                 sock.BeginConnect (ip, 1245, BCCallback, sock);
2132                                 Assert.Fail ("BeginConnectAddressPortListen #1");
2133                         } catch (InvalidOperationException) {
2134                         } catch {
2135                                 Assert.Fail ("BeginConnectAddressPortListen #2");
2136                         } finally {
2137                                 sock.Close ();
2138                         }
2139                 }
2140                 
2141                 [Test]
2142                 [ExpectedException (typeof(ObjectDisposedException))]
2143 #if TARGET_JVM
2144         [Ignore("System.Net.Sockets.Socket.BeginConnect(IPAddress,int,AsyncCallback,object) is not supported")]
2145 #endif
2146                 public void BeginConnectAddressPortClosed ()
2147                 {
2148                         Socket sock = new Socket (AddressFamily.InterNetwork,
2149                                                   SocketType.Stream,
2150                                                   ProtocolType.Tcp);
2151                         IPAddress ip = IPAddress.Loopback;
2152                         
2153                         sock.Close ();
2154                         
2155                         sock.BeginConnect (ip, 1244, BCCallback, sock);
2156                 }
2157                 
2158                 [Test]
2159 #if TARGET_JVM
2160         [Ignore("System.Net.Sockets.Socket.BeginConnect(IPAddress[],int,AsyncCallback,object) is not supported")]
2161 #endif
2162                 public void BeginConnectMultiple ()
2163                 {
2164                         Socket sock = new Socket (AddressFamily.InterNetwork,
2165                                                   SocketType.Stream,
2166                                                   ProtocolType.Tcp);
2167                         Socket listen = new Socket (AddressFamily.InterNetwork,
2168                                                     SocketType.Stream,
2169                                                     ProtocolType.Tcp);
2170                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2171                                                         1246);
2172                         IPAddress[] ips = new IPAddress[4];
2173                         
2174                         ips[0] = IPAddress.Parse ("127.0.0.4");
2175                         ips[1] = IPAddress.Parse ("127.0.0.3");
2176                         ips[2] = IPAddress.Parse ("127.0.0.2");
2177                         ips[3] = IPAddress.Parse ("127.0.0.1");
2178
2179                         listen.Bind (ep);
2180                         listen.Listen (1);
2181                         
2182                         BCCalledBack.Reset ();
2183                         
2184                         BCConnected = false;
2185                         
2186                         sock.BeginConnect (ips, 1246, BCCallback, sock);
2187                         
2188                         /* Longer wait here, because the ms runtime
2189                          * takes a lot longer to not connect
2190                          */
2191                         if (BCCalledBack.WaitOne (10000, false) == false) {
2192                                 Assert.Fail ("BeginConnectMultiple wait failed");
2193                         }
2194                         
2195                         Assertion.AssertEquals ("BeginConnectMultiple #1",
2196                                                 true, BCConnected);
2197                         Assertion.AssertEquals ("BeginConnectMultiple #2",
2198                                                 AddressFamily.InterNetwork,
2199                                                 sock.RemoteEndPoint.AddressFamily);
2200                         IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
2201                         
2202                         Assertion.AssertEquals ("BeginConnectMultiple #2",
2203                                                 IPAddress.Loopback,
2204                                                 remep.Address);
2205                         
2206                         sock.Close ();
2207                         listen.Close ();
2208                 }
2209
2210                 [Test]
2211 #if TARGET_JVM
2212         [Ignore("System.Net.Sockets.Socket.BeginConnect(IPAddress[],int,AsyncCallback,object) is not supported")]
2213 #endif
2214                 public void BeginConnectMultipleNull ()
2215                 {
2216                         Socket sock = new Socket (AddressFamily.InterNetwork,
2217                                                   SocketType.Stream,
2218                                                   ProtocolType.Tcp);
2219                         IPAddress[] ips = null;
2220                         
2221                         try {
2222                                 sock.BeginConnect (ips, 1246, BCCallback,
2223                                                    sock);
2224                                 Assert.Fail ("BeginConnectMultipleNull #1");
2225                         } catch (ArgumentNullException) {
2226                         } catch {
2227                                 Assert.Fail ("BeginConnectMultipleNull #2");
2228                         } finally {
2229                                 sock.Close ();
2230                         }
2231                 }
2232
2233                 [Test]
2234 #if TARGET_JVM
2235         [Ignore("System.Net.Sockets.Socket.BeginConnect(IPAddress[],int,AsyncCallback,object) is not supported")]
2236 #endif
2237                 public void BeginConnectMultipleListen ()
2238                 {
2239                         Socket sock = new Socket (AddressFamily.InterNetwork,
2240                                                   SocketType.Stream,
2241                                                   ProtocolType.Tcp);
2242                         IPAddress[] ips = new IPAddress[4];
2243                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2244                                                         1247);
2245                         
2246                         ips[0] = IPAddress.Parse ("127.0.0.4");
2247                         ips[1] = IPAddress.Parse ("127.0.0.3");
2248                         ips[2] = IPAddress.Parse ("127.0.0.2");
2249                         ips[3] = IPAddress.Parse ("127.0.0.1");
2250                         
2251                         sock.Bind (ep);
2252                         sock.Listen (1);
2253                         
2254                         try {
2255                                 sock.BeginConnect (ips, 1247, BCCallback,
2256                                                    sock);
2257                                 Assert.Fail ("BeginConnectMultipleListen #1");
2258                         } catch (InvalidOperationException) {
2259                         } catch {
2260                                 Assert.Fail ("BeginConnectMultipleListen #2");
2261                         } finally {
2262                                 sock.Close ();
2263                         }
2264                 }
2265                 
2266                 [Test]
2267                 [ExpectedException (typeof(ObjectDisposedException))]
2268 #if TARGET_JVM
2269         [Ignore("System.Net.Sockets.Socket.BeginConnect(IPAddress[],int,AsyncCallback,object) is not supported")]
2270 #endif
2271                 public void BeginConnectMultipleClosed ()
2272                 {
2273                         Socket sock = new Socket (AddressFamily.InterNetwork,
2274                                                   SocketType.Stream,
2275                                                   ProtocolType.Tcp);
2276                         IPAddress[] ips = new IPAddress[4];
2277                         
2278                         ips[0] = IPAddress.Parse ("127.0.0.4");
2279                         ips[1] = IPAddress.Parse ("127.0.0.3");
2280                         ips[2] = IPAddress.Parse ("127.0.0.2");
2281                         ips[3] = IPAddress.Parse ("127.0.0.1");
2282                         
2283                         sock.Close ();
2284                         
2285                         sock.BeginConnect (ips, 1247, BCCallback, sock);
2286                 }
2287                 
2288                 [Test]
2289 #if TARGET_JVM
2290         [Ignore("System.Net.Sockets.Socket.BeginConnect(IPAddress,int,AsyncCallback,object) is not supported")]
2291 #endif
2292                 public void BeginConnectHostPortNull ()
2293                 {
2294                         Socket sock = new Socket (AddressFamily.InterNetwork,
2295                                                   SocketType.Stream,
2296                                                   ProtocolType.Tcp);
2297                         
2298                         try {
2299                                 sock.BeginConnect ((string)null, 0,
2300                                                    BCCallback, sock);
2301                                 Assert.Fail ("BeginConnectHostPort #1");
2302                         } catch (ArgumentNullException) {
2303                         } catch {
2304                                 Assert.Fail ("BeginConnectHostPort #2");
2305                         } finally {
2306                                 sock.Close ();
2307                         }
2308                 }
2309
2310                 [Test]
2311 #if TARGET_JVM
2312         [Ignore("System.Net.Sockets.Socket.BeginConnect(string,int,AsyncCallback,object) is not supported")]
2313 #endif
2314                 public void BeginConnectHostPortListen ()
2315                 {
2316                         Socket sock = new Socket (AddressFamily.InterNetwork,
2317                                                   SocketType.Stream,
2318                                                   ProtocolType.Tcp);
2319                         IPAddress ip = IPAddress.Loopback;
2320                         IPEndPoint ep = new IPEndPoint (ip, 1248);
2321                         
2322                         sock.Bind (ep);
2323                         sock.Listen (1);
2324                         
2325                         try {
2326                                 sock.BeginConnect ("localhost", 1248,
2327                                                    BCCallback, sock);
2328                                 Assert.Fail ("BeginConnectHostPortListen #1");
2329                         } catch (InvalidOperationException) {
2330                         } catch {
2331                                 Assert.Fail ("BeginConnectHostPortListen #2");
2332                         } finally {
2333                                 sock.Close ();
2334                         }
2335                 }
2336
2337                 [Test]
2338                 [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
2339                 public void BeginConnectHostPortNotIP ()
2340                 {
2341                         Socket sock = new Socket (AddressFamily.NetBios,
2342                                                   SocketType.Seqpacket,
2343                                                   ProtocolType.Unspecified);
2344                         
2345                         try {
2346                                 sock.BeginConnect ("localhost", 0, BCCallback,
2347                                                    sock);
2348                                 Assert.Fail ("BeginConnectHostPortNotIP #1");
2349                         } catch (NotSupportedException) {
2350                         } catch {
2351                                 Assert.Fail ("BeginConnectHostPortNotIP #2");
2352                         } finally {
2353                                 sock.Close ();
2354                         }
2355                 }
2356
2357                 [Test]
2358                 [ExpectedException (typeof(ObjectDisposedException))]
2359 #if TARGET_JVM
2360         [Ignore("System.Net.Sockets.Socket.BeginConnect(string,int,AsyncCallback,object) is not supported")]
2361 #endif
2362                 public void BeginConnectHostPortClosed ()
2363                 {
2364                         Socket sock = new Socket (AddressFamily.InterNetwork,
2365                                                   SocketType.Stream,
2366                                                   ProtocolType.Tcp);
2367                         
2368                         sock.Close ();
2369                         
2370                         sock.BeginConnect ("localhost", 0, BCCallback, sock);
2371                 }
2372                 
2373                 static bool BDDisconnected = false;
2374                 static ManualResetEvent BDCalledBack = new ManualResetEvent (false);
2375                 
2376                 private static void BDCallback (IAsyncResult asyncResult)
2377                 {
2378 #if !TARGET_JVM
2379                         Socket sock = (Socket)asyncResult.AsyncState;
2380                         
2381                         sock.EndDisconnect (asyncResult);
2382                         BDDisconnected = true;
2383                         
2384                         BDCalledBack.Set ();
2385 #endif
2386                 }
2387                 
2388                 [Test]
2389                 [Category ("NotDotNet")] // "Needs XP or later"
2390  #if TARGET_JVM
2391         [Ignore("System.Net.Sockets.Socket.BeginDisconnect method is not supported")]
2392 #endif
2393                 public void BeginDisconnect ()
2394                 {
2395 #if !TARGET_JVM
2396                         Socket sock = new Socket (AddressFamily.InterNetwork,
2397                                                   SocketType.Stream,
2398                                                   ProtocolType.Tcp);
2399                         Socket listen = new Socket (AddressFamily.InterNetwork,
2400                                                     SocketType.Stream,
2401                                                     ProtocolType.Tcp);
2402                         IPAddress ip = IPAddress.Loopback;
2403                         IPEndPoint ep = new IPEndPoint (ip, 1254);
2404                         
2405                         listen.Bind (ep);
2406                         listen.Listen (1);
2407                         
2408                         sock.Connect (ip, 1254);
2409                         
2410                         Assertion.AssertEquals ("BeginDisconnect #1", true,
2411                                                 sock.Connected);
2412                         
2413                         sock.Shutdown (SocketShutdown.Both);
2414
2415                         BDCalledBack.Reset ();
2416                         BDDisconnected = false;
2417                         
2418                         sock.BeginDisconnect (false, BDCallback, sock);
2419                 
2420                         if (BDCalledBack.WaitOne (2000, false) == false) {
2421                                 Assert.Fail ("BeginDisconnect wait timed out");
2422                         }
2423                         
2424                         Assertion.AssertEquals ("BeginDisconnect #2", true,
2425                                                 BDDisconnected);
2426                         Assertion.AssertEquals ("BeginDisconnect #3", false,
2427                                                 sock.Connected);
2428                         
2429                         sock.Close ();
2430                         listen.Close ();
2431 #endif
2432                 }
2433                 
2434                 [Test]
2435                 public void BeginReceiveSocketError ()
2436                 {
2437                 }
2438                 
2439                 [Test]
2440                 public void BeginReceiveGeneric ()
2441                 {
2442                 }
2443                 
2444                 [Test]
2445                 public void BeginReceiveGenericSocketError ()
2446                 {
2447                 }
2448                 
2449                 private static void BSCallback (IAsyncResult asyncResult)
2450                 {
2451                         Socket sock = (Socket)asyncResult.AsyncState;
2452                         
2453                         sock.EndSend (asyncResult);
2454                 }
2455                 
2456                 [Test]
2457 #if TARGET_JVM
2458         [Ignore ("System.Net.Sockets.Socket.BeginSend(byte[] ...) is not supported")]
2459 #endif
2460                 public void BeginSendNotConnected ()
2461                 {
2462                         Socket sock = new Socket (AddressFamily.InterNetwork,
2463                                                   SocketType.Stream,
2464                                                   ProtocolType.Tcp);
2465                         
2466                         byte[] send_bytes = new byte[] {10, 11, 12, 13};
2467                         
2468                         try {
2469                                 sock.BeginSend (send_bytes, 0,
2470                                                 send_bytes.Length,
2471                                                 SocketFlags.None, BSCallback,
2472                                                 sock);
2473                                 Assert.Fail ("BeginSendNotConnected #1");
2474                         } catch (SocketException ex) {
2475                                 Assertion.AssertEquals ("BeginSendNotConnected #2", 10057, ex.ErrorCode);
2476                         } catch {
2477                                 Assert.Fail ("BeginSendNotConnected #3");
2478                         } finally {
2479                                 sock.Close ();
2480                         }
2481                 }
2482                 
2483                 [Test]
2484                 public void BeginSendSocketError ()
2485                 {
2486                 }
2487                 
2488                 [Test]
2489                 public void BeginSendGeneric ()
2490                 {
2491                 }
2492                 
2493                 [Test]
2494                 public void BeginSendGenericSocketError ()
2495                 {
2496                 }
2497                 
2498                 [Test]
2499                 public void BindTwice ()
2500                 {
2501                         Socket sock = new Socket (AddressFamily.InterNetwork,
2502                                                   SocketType.Stream,
2503                                                   ProtocolType.Tcp);
2504                         IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
2505                                                         1256);
2506                         IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
2507                                                          1257);
2508                         
2509                         sock.Bind (ep1);
2510                         
2511                         try {
2512                                 sock.Bind (ep2);
2513                                 Assert.Fail ("BindTwice #1");
2514                         } catch (SocketException ex) {
2515                                 Assertion.AssertEquals ("BindTwice #2",
2516                                                         10022, ex.ErrorCode);
2517                         } catch {
2518                                 Assert.Fail ("BindTwice #3");
2519                         } finally {
2520                                 sock.Close ();
2521                         }
2522                 }
2523                 
2524                 [Test]
2525 #if TARGET_JVM
2526         [Ignore ("System.Net.Sockets.Socket.Close(int) method is not supported")]
2527 #endif
2528                 public void Close ()
2529                 {
2530                         Socket sock = new Socket (AddressFamily.InterNetwork,
2531                                                   SocketType.Stream,
2532                                                   ProtocolType.Tcp);
2533                         Socket listen = new Socket (AddressFamily.InterNetwork,
2534                                                     SocketType.Stream,
2535                                                     ProtocolType.Tcp);
2536                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2537                                                         1258);
2538                         
2539                         listen.Bind (ep);
2540                         listen.Listen (1);
2541                         
2542                         sock.Connect (ep);
2543
2544                         Assertion.AssertEquals ("Close #1", true,
2545                                                 sock.Connected);
2546                         
2547                         sock.Close (2);
2548                         
2549                         Thread.Sleep (3000);
2550                         
2551                         Assertion.AssertEquals ("Close #2", false,
2552                                                 sock.Connected);
2553                         
2554                         listen.Close ();
2555                 }
2556                 
2557                 [Test]
2558 #if TARGET_JVM
2559         [Ignore("System.Net.Sockets.Socket.Connect(IPAddress,int) is not supported")]
2560 #endif
2561                 public void ConnectAddressPort ()
2562                 {
2563                         Socket sock = new Socket (AddressFamily.InterNetwork,
2564                                                   SocketType.Stream,
2565                                                   ProtocolType.Tcp);
2566                         Socket listen = new Socket (AddressFamily.InterNetwork,
2567                                                     SocketType.Stream,
2568                                                     ProtocolType.Tcp);
2569                         IPAddress ip = IPAddress.Loopback;
2570                         IPEndPoint ep = new IPEndPoint (ip, 1249);
2571
2572                         listen.Bind (ep);
2573                         listen.Listen (1);
2574                         
2575                         sock.Connect (ip, 1249);
2576                         
2577                         Assertion.AssertEquals ("ConnectAddressPort #1",
2578                                                 true, sock.Connected);
2579                         
2580                         sock.Close ();
2581                         listen.Close ();
2582                 }
2583
2584                 [Test]
2585 #if TARGET_JVM
2586         [Ignore("System.Net.Sockets.Socket.Connect(IPAddress,int) is not supported")]
2587 #endif
2588                 public void ConnectAddressPortNull ()
2589                 {
2590                         Socket sock = new Socket (AddressFamily.InterNetwork,
2591                                                   SocketType.Stream,
2592                                                   ProtocolType.Tcp);
2593                         IPAddress ip = null;
2594
2595                         try {
2596                                 sock.Connect (ip, 1249);
2597                                 Assert.Fail ("ConnectAddressPortNull #1");
2598                         } catch (ArgumentNullException) {
2599                         } catch {
2600                                 Assert.Fail ("ConnectAddressPortNull #2");
2601                         } finally {
2602                                 sock.Close ();
2603                         }
2604                 }
2605
2606                 [Test]
2607 #if TARGET_JVM
2608         [Ignore("System.Net.Sockets.Socket.Connect(IPAddress,int) is not supported")]
2609 #endif
2610                 public void ConnectAddressPortListen ()
2611                 {
2612                         Socket sock = new Socket (AddressFamily.InterNetwork,
2613                                                   SocketType.Stream,
2614                                                   ProtocolType.Tcp);
2615                         IPAddress ip = IPAddress.Loopback;
2616                         IPEndPoint ep = new IPEndPoint (ip, 1250);
2617
2618                         sock.Bind (ep);
2619                         sock.Listen (1);
2620                         
2621                         try {
2622                                 sock.Connect (ip, 1250);
2623                                 Assert.Fail ("ConnectAddressPortListen #1");
2624                         } catch (InvalidOperationException) {
2625                         } catch {
2626                                 Assert.Fail ("ConnectAddressPortListen #2");
2627                         } finally {
2628                                 sock.Close ();
2629                         }
2630                 }
2631                 
2632                 [Test]
2633                 [ExpectedException (typeof(ObjectDisposedException))]
2634 #if TARGET_JVM
2635         [Ignore("System.Net.Sockets.Socket.Connect(IPAddress,int) is not supported")]
2636 #endif
2637                 public void ConnectAddressPortClosed ()
2638                 {
2639                         Socket sock = new Socket (AddressFamily.InterNetwork,
2640                                                   SocketType.Stream,
2641                                                   ProtocolType.Tcp);
2642                         IPAddress ip = IPAddress.Loopback;
2643                         
2644                         sock.Close ();
2645                         
2646                         sock.Connect (ip, 1250);
2647                 }
2648                 
2649                 [Test]
2650 #if TARGET_JVM
2651         [Ignore("System.Net.Sockets.Socket.Connect(IPAddress[],int) is not supported")]
2652 #endif
2653                 public void ConnectMultiple ()
2654                 {
2655                         Socket sock = new Socket (AddressFamily.InterNetwork,
2656                                                   SocketType.Stream,
2657                                                   ProtocolType.Tcp);
2658                         Socket listen = new Socket (AddressFamily.InterNetwork,
2659                                                     SocketType.Stream,
2660                                                     ProtocolType.Tcp);
2661                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2662                                                         1251);
2663                         IPAddress[] ips = new IPAddress[4];
2664                         
2665                         ips[0] = IPAddress.Parse ("127.0.0.4");
2666                         ips[1] = IPAddress.Parse ("127.0.0.3");
2667                         ips[2] = IPAddress.Parse ("127.0.0.2");
2668                         ips[3] = IPAddress.Parse ("127.0.0.1");
2669
2670                         listen.Bind (ep);
2671                         listen.Listen (1);
2672                         
2673                         sock.Connect (ips, 1251);
2674                         
2675                         Assertion.AssertEquals ("ConnectMultiple #1",
2676                                                 true, sock.Connected);
2677                         Assertion.AssertEquals ("ConnectMultiple #2",
2678                                                 AddressFamily.InterNetwork,
2679                                                 sock.RemoteEndPoint.AddressFamily);
2680                         IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
2681                         
2682                         Assertion.AssertEquals ("ConnectMultiple #2",
2683                                                 IPAddress.Loopback,
2684                                                 remep.Address);
2685                         
2686                         sock.Close ();
2687                         listen.Close ();
2688                 }
2689
2690                 [Test]
2691 #if TARGET_JVM
2692         [Ignore("System.Net.Sockets.Socket.Connect(IPAddress[],int) is not supported")]
2693 #endif
2694                 public void ConnectMultipleNull ()
2695                 {
2696                         Socket sock = new Socket (AddressFamily.InterNetwork,
2697                                                   SocketType.Stream,
2698                                                   ProtocolType.Tcp);
2699                         IPAddress[] ips = null;
2700                         
2701                         try {
2702                                 sock.Connect (ips, 1251);
2703                                 Assert.Fail ("ConnectMultipleNull #1");
2704                         } catch (ArgumentNullException) {
2705                         } catch {
2706                                 Assert.Fail ("ConnectMultipleNull #2");
2707                         } finally {
2708                                 sock.Close ();
2709                         }
2710                 }
2711
2712                 [Test]
2713 #if TARGET_JVM
2714         [Ignore("System.Net.Sockets.Socket.Connect(IPAddress[],int) is not supported")]
2715 #endif
2716                 public void ConnectMultipleListen ()
2717                 {
2718                         Socket sock = new Socket (AddressFamily.InterNetwork,
2719                                                   SocketType.Stream,
2720                                                   ProtocolType.Tcp);
2721                         IPAddress[] ips = new IPAddress[4];
2722                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2723                                                         1252);
2724                         
2725                         ips[0] = IPAddress.Parse ("127.0.0.4");
2726                         ips[1] = IPAddress.Parse ("127.0.0.3");
2727                         ips[2] = IPAddress.Parse ("127.0.0.2");
2728                         ips[3] = IPAddress.Parse ("127.0.0.1");
2729                         
2730                         sock.Bind (ep);
2731                         sock.Listen (1);
2732                         
2733                         try {
2734                                 sock.Connect (ips, 1252);
2735                                 Assert.Fail ("ConnectMultipleListen #1");
2736                         } catch (InvalidOperationException) {
2737                         } catch {
2738                                 Assert.Fail ("ConnectMultipleListen #2");
2739                         } finally {
2740                                 sock.Close ();
2741                         }
2742                 }
2743                 
2744                 [Test]
2745                 [ExpectedException (typeof(ObjectDisposedException))]
2746 #if TARGET_JVM
2747         [Ignore("System.Net.Sockets.Socket.Connect(IPAddress[],int) is not supported")]
2748 #endif
2749                 public void ConnectMultipleClosed ()
2750                 {
2751                         Socket sock = new Socket (AddressFamily.InterNetwork,
2752                                                   SocketType.Stream,
2753                                                   ProtocolType.Tcp);
2754                         IPAddress[] ips = new IPAddress[4];
2755                         
2756                         ips[0] = IPAddress.Parse ("127.0.0.4");
2757                         ips[1] = IPAddress.Parse ("127.0.0.3");
2758                         ips[2] = IPAddress.Parse ("127.0.0.2");
2759                         ips[3] = IPAddress.Parse ("127.0.0.1");
2760                         
2761                         sock.Close ();
2762                         
2763                         sock.Connect (ips, 1252);
2764                 }
2765                 
2766                 [Test]
2767 #if TARGET_JVM
2768         [Ignore("System.Net.Sockets.Socket.Connect(string,int) is not supported")]
2769 #endif
2770                 public void ConnectHostPortNull ()
2771                 {
2772                         Socket sock = new Socket (AddressFamily.InterNetwork,
2773                                                   SocketType.Stream,
2774                                                   ProtocolType.Tcp);
2775                         
2776                         try {
2777                                 sock.Connect ((string)null, 0);
2778                                 Assert.Fail ("ConnectHostPort #1");
2779                         } catch (ArgumentNullException) {
2780                         } catch {
2781                                 Assert.Fail ("ConnectHostPort #2");
2782                         } finally {
2783                                 sock.Close ();
2784                         }
2785                 }
2786
2787                 [Test]
2788 #if TARGET_JVM
2789         [Ignore("System.Net.Sockets.Socket.Connect(string,int) is not supported")]
2790 #endif
2791                 public void ConnectHostPortListen ()
2792                 {
2793                         Socket sock = new Socket (AddressFamily.InterNetwork,
2794                                                   SocketType.Stream,
2795                                                   ProtocolType.Tcp);
2796                         IPAddress ip = IPAddress.Loopback;
2797                         IPEndPoint ep = new IPEndPoint (ip, 1253);
2798                         
2799                         sock.Bind (ep);
2800                         sock.Listen (1);
2801                         
2802                         try {
2803                                 sock.Connect ("localhost", 1253);
2804                                 Assert.Fail ("ConnectHostPortListen #1");
2805                         } catch (InvalidOperationException) {
2806                         } catch {
2807                                 Assert.Fail ("ConnectHostPortListen #2");
2808                         } finally {
2809                                 sock.Close ();
2810                         }
2811                 }
2812
2813                 [Test]
2814                 [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
2815                 public void ConnectHostPortNotIP ()
2816                 {
2817                         Socket sock = new Socket (AddressFamily.NetBios,
2818                                                   SocketType.Seqpacket,
2819                                                   ProtocolType.Unspecified);
2820                         
2821                         try {
2822                                 sock.Connect ("localhost", 0);
2823                                 Assert.Fail ("ConnectHostPortNotIP #1");
2824                         } catch (NotSupportedException) {
2825                         } catch {
2826                                 Assert.Fail ("ConnectHostPortNotIP #2");
2827                         } finally {
2828                                 sock.Close ();
2829                         }
2830                 }
2831
2832                 [Test]
2833                 [ExpectedException (typeof(ObjectDisposedException))]
2834 #if TARGET_JVM
2835         [Ignore("System.Net.Sockets.Socket.Connect(string,int) is not supported")]
2836 #endif
2837                 public void ConnectHostPortClosed ()
2838                 {
2839                         Socket sock = new Socket (AddressFamily.InterNetwork,
2840                                                   SocketType.Stream,
2841                                                   ProtocolType.Tcp);
2842                         
2843                         sock.Close ();
2844                         
2845                         sock.Connect ("localhost", 0);
2846                 }
2847                 
2848                 [Test]
2849                 [Category ("NotDotNet")] // "Needs XP or later"
2850 #if TARGET_JVM
2851         [Ignore ("System.Net.Sockets.Socket.Connect(IPAddress,int) is not supported")]
2852 #endif
2853                 public void Disconnect ()
2854                 {
2855 #if !TARGET_JVM
2856                         Socket sock = new Socket (AddressFamily.InterNetwork,
2857                                                   SocketType.Stream,
2858                                                   ProtocolType.Tcp);
2859                         Socket listen = new Socket (AddressFamily.InterNetwork,
2860                                                     SocketType.Stream,
2861                                                     ProtocolType.Tcp);
2862                         IPAddress ip = IPAddress.Loopback;
2863                         IPEndPoint ep = new IPEndPoint (ip, 1255);
2864                         
2865                         listen.Bind (ep);
2866                         listen.Listen (1);
2867                         
2868                         sock.Connect (ip, 1255);
2869                         
2870                         Assertion.AssertEquals ("Disconnect #1", true,
2871                                                 sock.Connected);
2872                         
2873                         sock.Shutdown (SocketShutdown.Both);
2874
2875                         sock.Disconnect (false);
2876
2877                         Assertion.AssertEquals ("BeginDisconnect #3", false,
2878                                                 sock.Connected);
2879                         
2880                         sock.Close ();
2881                         listen.Close ();
2882 #endif
2883                 }
2884                 
2885                 [Test]
2886                 public void DuplicateAndClose ()
2887                 {
2888                 }
2889                 
2890                 [Test]
2891                 public void IOControl ()
2892                 {
2893                 }
2894                 
2895                 [Test]
2896                 public void ReceiveGeneric ()
2897                 {
2898                 }
2899                 
2900                 [Test]
2901                 public void ReceiveGenericSocketFlags ()
2902                 {
2903                 }
2904                 
2905                 [Test]
2906                 public void ReceiveGenericSocketFlagsSocketError ()
2907                 {
2908                 }
2909                 
2910                 [Test]
2911                 public void SendGeneric ()
2912                 {
2913                 }
2914                 
2915                 [Test]
2916                 public void SendGenericSocketFlags ()
2917                 {
2918                 }
2919                 
2920                 [Test]
2921                 public void SendGenericSocketFlagsSocketError ()
2922                 {
2923                 }
2924
2925                 [Test]
2926                 public void ListenNotBound ()
2927                 {
2928                         Socket sock = new Socket (AddressFamily.InterNetwork,
2929                                                   SocketType.Stream,
2930                                                   ProtocolType.Tcp);
2931                         
2932                         try {
2933                                 sock.Listen (1);
2934                                 Assert.Fail ("ListenNotBound #1");
2935                         } catch (SocketException ex) {
2936                                 Assertion.AssertEquals ("ListenNotBound #2",
2937                                                         10022, ex.ErrorCode);
2938                         } catch {
2939                                 Assert.Fail ("ListenNotBound #3");
2940                         } finally {
2941                                 sock.Close ();
2942                         }
2943                 }
2944 #endif
2945         }
2946 }
2947