Merge pull request #1132 from mattleibow/bugfix-20925
[mono.git] / mcs / class / System / Test / System.Net.Sockets / UdpClientTest.cs
1 // System.Net.Sockets.UdpClientTest.cs
2 //
3 // Authors:
4 //      Chris Bacon <chris.bacon@docobo.co.uk>
5 //      Gert Driesen <drieseng@users.sourceforge.net>
6 //
7
8 using System;
9 using System.Net;
10 using System.Net.Sockets;
11 using System.Threading;
12
13 using NUnit.Framework;
14
15 namespace MonoTests.System.Net.Sockets {
16 #if TARGET_JVM
17         [Ignore("UdpClient is not supported - since UDP sockets are not supported")]
18 #endif
19         [TestFixture]
20         public class UdpClientTest {
21                 [Test] // .ctor ()
22                 public void Constructor1 ()
23                 {
24                         MyUdpClient client;
25                         Socket s;
26
27                         client = new MyUdpClient ();
28                         s = client.Client;
29                         Assert.IsNotNull (s, "Client");
30                         Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "Client:AddressFamily");
31                         Assert.IsFalse (s.Connected, "Client:Connected");
32 #if NET_2_0
33                         Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
34 #endif
35                         Assert.IsNull (s.LocalEndPoint, "Client:LocalEndPoint");
36                         Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "Client:ProtocolType");
37                         Assert.IsNull (s.RemoteEndPoint, "Client:RemoteEndPoint");
38                         Assert.AreEqual (SocketType.Dgram, s.SocketType, "Client:SocketType");
39                         Assert.IsFalse (client.Active, "Active");
40 #if NET_2_0
41                         Assert.IsFalse (client.DontFragment, "DontFragment");
42                         Assert.IsFalse (client.EnableBroadcast, "EnableBroadcast");
43                         //Assert.IsFalse (client.ExclusiveAddressUse, "ExclusiveAddressUse");
44                         Assert.IsTrue (client.MulticastLoopback, "MulticastLoopback");
45                         //Assert.AreEqual (32, client.Ttl, "Ttl");
46 #endif
47                         client.Close ();
48                 }
49
50                 [Test] // .ctor (AddressFamily)
51                 public void Constructor2 ()
52                 {
53                         MyUdpClient client;
54                         Socket s;
55
56                         client = new MyUdpClient (AddressFamily.InterNetwork);
57                         s = client.Client;
58                         Assert.IsNotNull (s, "#A:Client");
59                         Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
60                         Assert.IsFalse (s.Connected, "#A:Client:Connected");
61 #if NET_2_0
62                         Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
63 #endif
64                         Assert.IsNull (s.LocalEndPoint, "#A:Client:LocalEndPoint");
65                         Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
66                         Assert.IsNull (s.RemoteEndPoint, "#A:Client:RemoteEndPoint");
67                         Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
68                         Assert.IsFalse (client.Active, "#A:Active");
69 #if NET_2_0
70                         //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
71                         Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
72                         //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
73                         Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
74                         //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
75 #endif
76
77 #if NET_2_0
78                         if (!Socket.OSSupportsIPv6)
79 #else
80                         if (!Socket.SupportsIPv6)
81 #endif
82                                 Assert.Ignore ("IPv6 not enabled.");
83
84                         client = new MyUdpClient (AddressFamily.InterNetworkV6);
85                         s = client.Client;
86                         Assert.IsNotNull (s, "#B:Client");
87                         Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
88                         Assert.IsFalse (s.Connected, "#B:Client:Connected");
89 #if NET_2_0
90                         Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
91 #endif
92                         Assert.IsNull (s.LocalEndPoint, "#B:Client:LocalEndPoint");
93                         Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
94                         Assert.IsNull (s.RemoteEndPoint, "#B:Client:RemoteEndPoint");
95                         Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
96                         Assert.IsFalse (client.Active, "#B:Active");
97 #if NET_2_0
98                         //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
99                         Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
100                         //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
101                         Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
102                         //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
103 #endif
104                         client.Close ();
105                 }
106
107                 [Test] // .ctor (AddressFamily)
108                 public void Constructor2_Family_Invalid ()
109                 {
110                         try {
111                                 new UdpClient (AddressFamily.NetBios);
112                                 Assert.Fail ("#A1");
113                         } catch (ArgumentException ex) {
114                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
115                                 Assert.IsNull (ex.InnerException, "#A3");
116 #if NET_2_0
117                                 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
118                                 // addresses
119                                 Assert.IsNotNull (ex.Message, "#A4");
120                                 Assert.AreEqual ("family", ex.ParamName, "#A5");
121 #else
122                                 Assert.AreEqual ("family", ex.Message, "#A4");
123                                 Assert.IsNull (ex.ParamName, "#A5");
124 #endif
125                         }
126
127                         try {
128                                 new UdpClient ((AddressFamily) 666);
129                                 Assert.Fail ("#B1");
130                         } catch (ArgumentException ex) {
131                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
132                                 Assert.IsNull (ex.InnerException, "#B3");
133 #if NET_2_0
134                                 Assert.IsNotNull (ex.Message, "#B4");
135                                 Assert.AreEqual ("family", ex.ParamName, "#B5");
136 #else
137                                 Assert.AreEqual ("family", ex.Message, "#B4");
138                                 Assert.IsNull (ex.ParamName, "#B5");
139 #endif
140                         }
141                 }
142
143                 [Test] // .ctor (Int32)
144                 public void Constructor3 ()
145                 {
146                         Socket s;
147                         IPEndPoint localEP;
148
149                         using (MyUdpClient client = new MyUdpClient (IPEndPoint.MinPort)) 
150                         {
151                                 s = client.Client;
152                                 Assert.IsNotNull (s, "#A:Client");
153                                 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
154                                 Assert.IsFalse (s.Connected, "#A:Client:Connected");
155         #if NET_2_0
156                                 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
157         #endif
158                                 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
159                                 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
160                                 Assert.IsFalse (client.Active, "#A:Active");
161         #if NET_2_0
162                                 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
163                                 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
164                                 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
165                                 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
166                                 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
167         #endif
168                                 localEP = s.LocalEndPoint as IPEndPoint;
169                                 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
170                                 Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
171                                 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
172                         }
173                         using (MyUdpClient client = new MyUdpClient (IPEndPoint.MaxPort))
174                         {
175                                 s = client.Client;
176                                 Assert.IsNotNull (s, "#B:Client");
177                                 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
178                                 Assert.IsFalse (s.Connected, "#B:Client:Connected");
179         #if NET_2_0
180                                 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
181         #endif
182                                 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
183                                 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
184                                 Assert.IsFalse (client.Active, "#B:Active");
185         #if NET_2_0
186                                 Assert.IsFalse (client.DontFragment, "#B:DontFragment");
187                                 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
188                                 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
189                                 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
190                                 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
191         #endif
192                                 localEP = s.LocalEndPoint as IPEndPoint;
193                                 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
194                                 Assert.AreEqual (IPAddress.Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
195                                 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
196                                 Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
197                         }
198                 }
199
200                 [Test] // .ctor (Int32)
201                 public void Constructor3_Port_OutOfRange ()
202                 {
203                         try {
204                                 new UdpClient (IPEndPoint.MaxPort + 1);
205                                 Assert.Fail ("#A1");
206                         } catch (ArgumentOutOfRangeException ex) {
207                                 // Specified argument was out of the range of valid values
208                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
209                                 Assert.IsNull (ex.InnerException, "#A3");
210                                 Assert.IsNotNull (ex.Message, "#A4");
211                                 Assert.AreEqual ("port", ex.ParamName, "#A5");
212                         }
213
214                         try {
215                                 new UdpClient (IPEndPoint.MinPort - 1);
216                                 Assert.Fail ("#A1");
217                         } catch (ArgumentOutOfRangeException ex) {
218                                 // Specified argument was out of the range of valid values
219                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
220                                 Assert.IsNull (ex.InnerException, "#A3");
221                                 Assert.IsNotNull (ex.Message, "#A4");
222                                 Assert.AreEqual ("port", ex.ParamName, "#A5");
223                         }
224                 }
225
226                 [Test] // .ctor (IPEndPoint)
227                 public void Constructor4 ()
228                 {
229                         Socket s;
230                         IPEndPoint localEP;
231                         IPEndPoint clientEP;
232
233                         clientEP = new IPEndPoint (IPAddress.Loopback, 8001);
234                         using (MyUdpClient client = new MyUdpClient (clientEP))
235                         {
236                                 s = client.Client;
237                                 Assert.IsNotNull (s, "#A:Client");
238                                 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
239                                 Assert.IsFalse (s.Connected, "#A:Client:Connected");
240 #if NET_2_0
241                                 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
242 #endif
243                                 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
244                                 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
245                                 Assert.IsFalse (client.Active, "#A:Active");
246 #if NET_2_0
247                                 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
248                                 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
249                                 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
250                                 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
251                                 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
252 #endif
253                                 localEP = s.LocalEndPoint as IPEndPoint;
254                                 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
255                                 Assert.IsFalse (object.ReferenceEquals (clientEP, localEP), "#A:Client:LocalEndPoint/ReferenceEquality");
256                                 Assert.AreEqual (clientEP.Address, localEP.Address, "#A:Client:LocalEndPoint/Address");
257                                 Assert.AreEqual (clientEP.AddressFamily, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
258                                 Assert.AreEqual (clientEP.Port, localEP.Port, "#A:Client:LocalEndPoint/Port");
259                         }
260                 }
261
262                 [Test] // .ctor (IPEndPoint)
263                 public void Constructor4_LocalEP_Null ()
264                 {
265                         try {
266                                 new UdpClient ((IPEndPoint) null);
267                                 Assert.Fail ("#1");
268                         } catch (ArgumentNullException ex) {
269                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
270                                 Assert.IsNull (ex.InnerException, "#3");
271                                 Assert.IsNotNull (ex.Message, "#4");
272                                 Assert.AreEqual ("localEP", ex.ParamName, "#5");
273                         }
274                 }
275
276                 [Test] // .ctor (Int32, AddressFamily)
277                 public void Constructor5 ()
278                 {
279                         Socket s;
280                         IPEndPoint localEP;
281
282                         using (MyUdpClient client = new MyUdpClient (IPEndPoint.MinPort, AddressFamily.InterNetwork))
283                         {
284                                 s = client.Client;
285                                 Assert.IsNotNull (s, "#A:Client");
286                                 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
287                                 Assert.IsFalse (s.Connected, "#A:Client:Connected");
288 #if NET_2_0
289                                 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
290 #endif
291                                 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
292                                 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
293                                 Assert.IsFalse (client.Active, "#A:Active");
294 #if NET_2_0
295                                 //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
296                                 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
297                                 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
298                                 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
299                                 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
300 #endif
301                                 localEP = s.LocalEndPoint as IPEndPoint;
302                                 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
303                                 Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
304                                 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
305                         }
306
307 #if NET_2_0
308                         if (!Socket.OSSupportsIPv6)
309 #else
310                         if (!Socket.SupportsIPv6)
311 #endif
312                                 Assert.Ignore ("IPv6 not enabled.");
313
314                         using (MyUdpClient client = new MyUdpClient (IPEndPoint.MaxPort, AddressFamily.InterNetworkV6))
315                         {
316                                 s = client.Client;
317                                 Assert.IsNotNull (s, "#B:Client");
318                                 Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
319                                 Assert.IsFalse (s.Connected, "#B:Client:Connected");
320 #if NET_2_0
321                                 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
322 #endif
323                                 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
324                                 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
325                                 Assert.IsFalse (client.Active, "#B:Active");
326 #if NET_2_0
327                                 //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
328                                 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
329                                 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
330                                 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
331                                 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
332 #endif
333                                 localEP = s.LocalEndPoint as IPEndPoint;
334                                 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
335                                 Assert.AreEqual (IPAddress.IPv6Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
336                                 Assert.AreEqual (AddressFamily.InterNetworkV6, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
337                                 Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
338                         }
339                 }
340
341                 [Test] // .ctor (Int32, AddressFamily)
342                 public void Constructor5_Family_Invalid ()
343                 {
344                         try {
345                                 new UdpClient (80, AddressFamily.NetBios);
346                                 Assert.Fail ("#A1");
347                         } catch (ArgumentException ex) {
348                                 // family
349                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
350                                 Assert.IsNull (ex.InnerException, "#A3");
351 #if NET_2_0
352                                 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
353                                 // addresses
354                                 Assert.IsNotNull (ex.Message, "#A4");
355                                 Assert.AreEqual ("family", ex.ParamName, "#A5");
356 #else
357                                 Assert.AreEqual ("family", ex.Message, "#A4");
358                                 Assert.IsNull (ex.ParamName, "#A5");
359 #endif
360                         }
361
362                         try {
363                                 new UdpClient (80, (AddressFamily) 666);
364                                 Assert.Fail ("#B1");
365                         } catch (ArgumentException ex) {
366                                 // family
367                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
368                                 Assert.IsNull (ex.InnerException, "#B3");
369 #if NET_2_0
370                                 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
371                                 // addresses
372                                 Assert.IsNotNull (ex.Message, "#B4");
373                                 Assert.AreEqual ("family", ex.ParamName, "#B5");
374 #else
375                                 Assert.AreEqual ("family", ex.Message, "#B4");
376                                 Assert.IsNull (ex.ParamName, "#B5");
377 #endif
378                         }
379                 }
380
381                 [Test] // .ctor (Int32, AddressFamily)
382                 public void Constructor5_Port_OutOfRange ()
383                 {
384                         try {
385                                 new UdpClient (IPEndPoint.MaxPort + 1, AddressFamily.InterNetwork);
386                                 Assert.Fail ("#A1");
387                         } catch (ArgumentOutOfRangeException ex) {
388                                 // Specified argument was out of the range of valid values
389                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
390                                 Assert.IsNull (ex.InnerException, "#A3");
391                                 Assert.IsNotNull (ex.Message, "#A4");
392                                 Assert.AreEqual ("port", ex.ParamName, "#A5");
393                         }
394
395                         try {
396                                 new UdpClient (IPEndPoint.MinPort - 1, AddressFamily.InterNetwork);
397                                 Assert.Fail ("#A1");
398                         } catch (ArgumentOutOfRangeException ex) {
399                                 // Specified argument was out of the range of valid values
400                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
401                                 Assert.IsNull (ex.InnerException, "#A3");
402                                 Assert.IsNotNull (ex.Message, "#A4");
403                                 Assert.AreEqual ("port", ex.ParamName, "#A5");
404                         }
405                 }
406
407                 [Test] // .ctor (String, Int32)
408                 public void Constructor6 ()
409                 {
410                         Socket s;
411                         IPEndPoint localEP;
412
413                         // Bug #5503
414                         // UDP port 0 doesn't seem to be valid.
415                         using (MyUdpClient client = new MyUdpClient ("127.0.0.1", 53))
416                         {
417                                 s = client.Client;
418                                 Assert.IsNotNull (s, "#A:Client");
419                                 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
420                                 Assert.IsTrue (s.Connected, "#A:Client:Connected");
421 #if NET_2_0
422                                 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
423 #endif
424                                 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
425                                 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
426                                 Assert.IsTrue (client.Active, "#A:Active");
427 #if NET_2_0
428                                 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
429                                 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
430                                 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
431                                 //Assert.IsFalse (client.MulticastLoopback, "#A:MulticastLoopback");
432                                 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
433 #endif
434                                 localEP = s.LocalEndPoint as IPEndPoint;
435                                 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
436                                 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#A:Client:LocalEndPoint/Address");
437                                 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
438                         }
439                         using (MyUdpClient client = new MyUdpClient ("127.0.0.1", IPEndPoint.MaxPort))
440                         {
441                                 s = client.Client;
442                                 Assert.IsNotNull (s, "#B:Client");
443                                 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
444                                 Assert.IsTrue (s.Connected, "#B:Client:Connected");
445 #if NET_2_0
446                                 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
447 #endif
448                                 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
449                                 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
450                                 Assert.IsTrue (client.Active, "#B:Active");
451 #if NET_2_0
452                                 Assert.IsFalse (client.DontFragment, "#B:DontFragment");
453                                 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
454                                 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
455                                 //Assert.IsFalse (client.MulticastLoopback, "#B:MulticastLoopback");
456                                 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
457 #endif
458                                 localEP = s.LocalEndPoint as IPEndPoint;
459                                 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
460                                 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#B:Client:LocalEndPoint/Address");
461                                 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
462                         }
463                 }
464
465                 [Test] // .ctor (String, Int32)
466                 public void Constructor6_HostName_Null ()
467                 {
468                         try {
469                                 new UdpClient ((string) null, int.MaxValue);
470                                 Assert.Fail ("#1");
471                         } catch (ArgumentNullException ex) {
472                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
473                                 Assert.IsNull (ex.InnerException, "#3");
474                                 Assert.IsNotNull (ex.Message, "#4");
475                                 Assert.AreEqual ("hostname", ex.ParamName, "#5");
476                         }
477                 }
478
479                 [Test] // .ctor (String, Int32)
480                 public void Constructor6_Port_OutOfRange ()
481                 {
482                         try {
483                                 new UdpClient ("local", IPEndPoint.MaxPort + 1);
484                                 Assert.Fail ("#A1");
485                         } catch (ArgumentOutOfRangeException ex) {
486                                 // Specified argument was out of the range of valid values
487                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
488                                 Assert.IsNull (ex.InnerException, "#A3");
489                                 Assert.IsNotNull (ex.Message, "#A4");
490                                 Assert.AreEqual ("port", ex.ParamName, "#A5");
491                         }
492
493                         try {
494                                 new UdpClient ("local", IPEndPoint.MinPort - 1);
495                                 Assert.Fail ("#A1");
496                         } catch (ArgumentOutOfRangeException ex) {
497                                 // Specified argument was out of the range of valid values
498                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
499                                 Assert.IsNull (ex.InnerException, "#A3");
500                                 Assert.IsNotNull (ex.Message, "#A4");
501                                 Assert.AreEqual ("port", ex.ParamName, "#A5");
502                         }
503                 }
504
505                 [Test]
506                 public void UdpClientBroadcastTest () 
507                 {
508                         UdpClient client = new UdpClient ();
509                         byte[] bytes = new byte[] {10, 11, 12, 13};
510
511                         try {
512                                 client.Send (bytes, bytes.Length, new IPEndPoint (IPAddress.Broadcast, 1235));
513                         } finally {
514                                 client.Close ();
515                         }
516                 }
517
518                 [Test] // JoinMulticastGroup (IPAddress)
519                 public void JoinMulticastGroup1_IPv4 ()
520                 {
521                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
522
523                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
524                                 client.JoinMulticastGroup (mcast_addr);
525                         }
526                 }
527
528                 [Test] // JoinMulticastGroup (IPAddress)
529                 public void JoinMulticastGroup1_IPv6 ()
530                 {
531 #if NET_2_0
532                         if (!Socket.OSSupportsIPv6)
533 #else
534                         if (!Socket.SupportsIPv6)
535 #endif
536                                 Assert.Ignore ("IPv6 not enabled.");
537
538                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
539
540                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
541                                 client.JoinMulticastGroup (mcast_addr);
542                         }
543                 }
544
545                 [Test] // JoinMulticastGroup (IPAddress)
546                 public void JoinMulticastGroup1_MulticastAddr_Null ()
547                 {
548                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
549                                 try {
550                                         client.JoinMulticastGroup ((IPAddress) null);
551                                         Assert.Fail ("#1");
552 #if NET_2_0
553                                 } catch (ArgumentNullException ex) {
554                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
555                                         Assert.IsNull (ex.InnerException, "#3");
556                                         Assert.IsNotNull (ex.Message, "#4");
557                                         Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
558                                 }
559 #else
560                                 } catch (NullReferenceException ex) {
561                                         Assert.AreEqual (typeof (NullReferenceException), ex.GetType (), "#2");
562                                         Assert.IsNull (ex.InnerException, "#3");
563                                         Assert.IsNotNull (ex.Message, "#4");
564                                 }
565 #endif
566                         }
567                 }
568
569                 [Test] // JoinMulticastGroup (IPAddress)
570                 public void JoinMulticastGroup1_Socket_Closed ()
571                 {
572                         IPAddress mcast_addr = null;
573
574                         UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
575                         client.Close ();
576                         try {
577                                 client.JoinMulticastGroup (mcast_addr);
578                                 Assert.Fail ("#1");
579                         } catch (ObjectDisposedException ex) {
580                                 // Cannot access a disposed object
581                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
582                                 Assert.IsNull (ex.InnerException, "#3");
583                                 Assert.IsNotNull (ex.Message, "#4");
584                                 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
585                         }
586                 }
587
588                 [Test] // JoinMulticastGroup (IPAddress)
589                 [Category ("NotWorking")]
590                 public void JoinMulticastGroup1_Socket_NotBound ()
591                 {
592                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
593
594                         UdpClient client = new UdpClient (AddressFamily.InterNetwork);
595                         try {
596                                 client.JoinMulticastGroup (mcast_addr);
597                                 Assert.Fail ("#1");
598                         } catch (SocketException ex) {
599                                 // An invalid argument was supplied
600                                 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
601                                 Assert.AreEqual (10022, ex.ErrorCode, "#3");
602                                 Assert.IsNull (ex.InnerException, "#4");
603                                 Assert.IsNotNull (ex.Message, "#5");
604                                 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
605 #if NET_2_0
606                                 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
607 #endif
608                         } finally {
609                                 client.Close ();
610                         }
611                 }
612
613                 [Test] // JoinMulticastGroup (In32, IPAddress)
614                 public void JoinMulticastGroup2_IPv4 ()
615                 {
616                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
617
618                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
619                                 try {
620                                         client.JoinMulticastGroup (0, mcast_addr);
621                                         Assert.Fail ("#1");
622                                 } catch (SocketException ex) {
623                                         // The attempted operation is not supported for the type of
624                                         // object referenced
625                                         Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
626                                         Assert.AreEqual (10045, ex.ErrorCode, "#3");
627                                         Assert.IsNull (ex.InnerException, "#4");
628                                         Assert.IsNotNull (ex.Message, "#5");
629                                         Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
630 #if NET_2_0
631                                         Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
632 #endif
633                                 }
634                         }
635                 }
636
637                 [Test] // JoinMulticastGroup (In32, IPAddress)
638                 public void JoinMulticastGroup2_IPv6 ()
639                 {
640 #if NET_2_0
641                         if (!Socket.OSSupportsIPv6)
642 #else
643                         if (!Socket.SupportsIPv6)
644 #endif
645                                 Assert.Ignore ("IPv6 not enabled.");
646
647                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
648
649                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
650                                 client.JoinMulticastGroup (0, mcast_addr);
651                         }
652                 }
653
654                 [Test] // JoinMulticastGroup (Int32, IPAddress)
655                 public void JoinMulticastGroup2_MulticastAddr_Null ()
656                 {
657                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
658                                 try {
659                                         client.JoinMulticastGroup (0, (IPAddress) null);
660                                         Assert.Fail ("#1");
661                                 } catch (ArgumentNullException ex) {
662                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
663                                         Assert.IsNull (ex.InnerException, "#3");
664                                         Assert.IsNotNull (ex.Message, "#4");
665                                         Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
666                                 }
667                         }
668                 }
669
670                 [Test] // JoinMulticastGroup (Int32, IPAddress)
671                 public void JoinMulticastGroup2_Socket_Closed ()
672                 {
673 #if NET_2_0
674                         if (!Socket.OSSupportsIPv6)
675 #else
676                         if (!Socket.SupportsIPv6)
677 #endif
678                                 Assert.Ignore ("IPv6 not enabled.");
679
680                         IPAddress mcast_addr = null;
681
682                         UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234));
683                         client.Close ();
684                         try {
685                                 client.JoinMulticastGroup (0, mcast_addr);
686                                 Assert.Fail ("#1");
687                         } catch (ObjectDisposedException ex) {
688                                 // Cannot access a disposed object
689                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
690                                 Assert.IsNull (ex.InnerException, "#3");
691                                 Assert.IsNotNull (ex.Message, "#4");
692                                 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
693                         }
694                 }
695
696                 [Test] // JoinMulticastGroup (Int32, IPAddress)
697                 [Category ("NotWorking")]
698                 public void JoinMulticastGroup2_Socket_NotBound ()
699                 {
700                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
701
702                         UdpClient client = new UdpClient (AddressFamily.InterNetworkV6);
703                         try {
704                                 client.JoinMulticastGroup (0, mcast_addr);
705                                 Assert.Fail ("#1");
706                         } catch (SocketException ex) {
707                                 // An invalid argument was supplied
708                                 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
709                                 Assert.AreEqual (10022, ex.ErrorCode, "#3");
710                                 Assert.IsNull (ex.InnerException, "#4");
711                                 Assert.IsNotNull (ex.Message, "#5");
712                                 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
713 #if NET_2_0
714                                 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
715 #endif
716                         } finally {
717                                 client.Close ();
718                         }
719                 }
720
721                 [Test] // JoinMulticastGroup (IPAddress, Int32)
722                 public void JoinMulticastGroup3_IPv4 ()
723                 {
724                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
725
726                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
727                                 client.JoinMulticastGroup (mcast_addr, 0);
728                         }
729
730                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
731                                 client.JoinMulticastGroup (mcast_addr, 255);
732                         }
733                 }
734
735                 [Test] // JoinMulticastGroup (IPAddress, Int32)
736                 public void JoinMulticastGroup3_IPv6 ()
737                 {
738 #if NET_2_0
739                         if (!Socket.OSSupportsIPv6)
740 #else
741                         if (!Socket.SupportsIPv6)
742 #endif
743                                 Assert.Ignore ("IPv6 not enabled.");
744
745                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
746
747                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
748                                 client.JoinMulticastGroup (mcast_addr, 0);
749                         }
750
751                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
752                                 client.JoinMulticastGroup (mcast_addr, 255);
753                         }
754                 }
755
756                 [Test] // JoinMulticastGroup (IPAddress, Int32)
757                 public void JoinMulticastGroup3_MulticastAddr_Null ()
758                 {
759                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
760                                 try {
761                                         client.JoinMulticastGroup ((IPAddress) null, int.MaxValue);
762                                         Assert.Fail ("#1");
763                                 } catch (ArgumentNullException ex) {
764                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
765                                         Assert.IsNull (ex.InnerException, "#3");
766                                         Assert.IsNotNull (ex.Message, "#4");
767                                         Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
768                                 }
769                         }
770                 }
771
772                 [Test] // JoinMulticastGroup (IPAddress, Int32)
773                 public void JoinMulticastGroup3_Socket_Closed ()
774                 {
775                         IPAddress mcast_addr = null;
776
777                         UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
778                         client.Close ();
779                         try {
780                                 client.JoinMulticastGroup (mcast_addr, 0);
781                                 Assert.Fail ("#1");
782                         } catch (ObjectDisposedException ex) {
783                                 // Cannot access a disposed object
784                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
785                                 Assert.IsNull (ex.InnerException, "#3");
786                                 Assert.IsNotNull (ex.Message, "#4");
787                                 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
788                         }
789                 }
790
791                 [Test] // JoinMulticastGroup (IPAddress, Int32)
792                 [Category ("NotWorking")]
793                 public void JoinMulticastGroup3_Socket_NotBound ()
794                 {
795                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
796
797                         UdpClient client = new UdpClient (AddressFamily.InterNetwork);
798                         try {
799                                 client.JoinMulticastGroup (mcast_addr, 5);
800                                 Assert.Fail ("#1");
801                         } catch (SocketException ex) {
802                                 // An invalid argument was supplied
803                                 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
804                                 Assert.AreEqual (10022, ex.ErrorCode, "#3");
805                                 Assert.IsNull (ex.InnerException, "#4");
806                                 Assert.IsNotNull (ex.Message, "#5");
807                                 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
808 #if NET_2_0
809                                 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
810 #endif
811                         } finally {
812                                 client.Close ();
813                         }
814                 }
815
816 #if NET_2_0
817                 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
818                 public void JoinMulticastGroup4_IPv4 ()
819                 {
820                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
821                         IPAddress local_addr = IPAddress.Any;
822
823                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
824                                 client.JoinMulticastGroup (mcast_addr, local_addr);
825                         }
826                 }
827
828                 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
829                 public void JoinMulticastGroup4_IPv6 ()
830                 {
831                         if (!Socket.OSSupportsIPv6)
832                                 Assert.Ignore ("IPv6 not enabled.");
833
834                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
835                         IPAddress local_addr = IPAddress.IPv6Any;
836
837                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
838                                 try {
839                                         client.JoinMulticastGroup (mcast_addr, local_addr);
840                                         Assert.Fail ("#1");
841                                 } catch (SocketException ex) {
842                                         // The attempted operation is not supported for the type of
843                                         // object referenced
844                                         Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
845                                         Assert.AreEqual (10045, ex.ErrorCode, "#3");
846                                         Assert.IsNull (ex.InnerException, "#4");
847                                         Assert.IsNotNull (ex.Message, "#5");
848                                         Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
849                                         Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
850                                 }
851                         }
852                 }
853
854                 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
855                 public void JoinMulticastGroup4_LocalAddress_Null ()
856                 {
857                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
858
859                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
860                                 try {
861                                         client.JoinMulticastGroup (mcast_addr, (IPAddress) null);
862                                         Assert.Fail ("#1");
863                                 } catch (ArgumentNullException ex) {
864                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
865                                         Assert.IsNull (ex.InnerException, "#3");
866                                         Assert.IsNotNull (ex.Message, "#4");
867                                         Assert.AreEqual ("mcint", ex.ParamName, "#5");
868                                 }
869                         }
870                 }
871
872                 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
873                 public void JoinMulticastGroup4_MulticastAddr_Null ()
874                 {
875                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
876                                 try {
877                                         client.JoinMulticastGroup ((IPAddress) null, IPAddress.Loopback);
878                                         Assert.Fail ("#1");
879                                 } catch (ArgumentNullException ex) {
880                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
881                                         Assert.IsNull (ex.InnerException, "#3");
882                                         Assert.IsNotNull (ex.Message, "#4");
883                                         Assert.AreEqual ("group", ex.ParamName, "#5");
884                                 }
885                         }
886                 }
887
888                 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
889                 public void JoinMulticastGroup4_Socket_Closed ()
890                 {
891                         IPAddress mcast_addr = null;
892                         IPAddress local_addr = null;
893
894                         UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
895                         client.Close ();
896                         try {
897                                 client.JoinMulticastGroup (mcast_addr, local_addr);
898                                 Assert.Fail ("#1");
899                         } catch (ObjectDisposedException ex) {
900                                 // Cannot access a disposed object
901                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
902                                 Assert.IsNull (ex.InnerException, "#3");
903                                 Assert.IsNotNull (ex.Message, "#4");
904                                 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
905                         }
906                 }
907
908                 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
909                 [Category ("NotWorking")]
910                 public void JoinMulticastGroup4_Socket_NotBound ()
911                 {
912                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
913                         IPAddress local_addr = Dns.GetHostEntry (string.Empty).AddressList [0];
914
915                         UdpClient client = new UdpClient (AddressFamily.InterNetwork);
916                         try {
917                                 client.JoinMulticastGroup (mcast_addr, local_addr);
918                                 Assert.Fail ("#1");
919                         } catch (SocketException ex) {
920                                 // An invalid argument was supplied
921                                 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
922                                 Assert.AreEqual (10022, ex.ErrorCode, "#3");
923                                 Assert.IsNull (ex.InnerException, "#4");
924                                 Assert.IsNotNull (ex.Message, "#5");
925                                 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
926                                 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
927                         } finally {
928                                 client.Close ();
929                         }
930                 }
931
932                 [Test]
933                 public void CloseInReceive ()
934                 {
935                         UdpClient client = null;
936                         var rnd = new Random ();
937                         for (int i = 0; i < 5; i++) {
938                                 int port = rnd.Next (1025, 65534);
939                                 try {
940                                         client = new UdpClient (port);
941                                         break;
942                                 } catch (Exception ex) {
943                                         if (i == 5)
944                                                 throw;
945                                 }
946                         }
947
948                         new Thread(delegate() {
949                                 Thread.Sleep(2000);
950                                 client.Close();
951                                 }).Start();
952
953                         bool got_exc = false;
954                         IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
955                         try {
956                                 client.Receive(ref ep);
957                         } catch (SocketException) {
958                                 got_exc = true;
959                         } finally {
960                                 client.Close ();
961                         }
962                         Assert.IsTrue (got_exc);
963                 }
964
965                 // Test for bug 324033
966                 [Test]
967                 public void JoinMulticastGroupWithLocal ()
968                 {
969                         UdpClient client = new UdpClient (9001);
970                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.24");
971                         IPAddress local_addr = IPAddress.Any;
972
973                         try {
974                                 client.JoinMulticastGroup (mcast_addr, local_addr);
975                         } finally {
976                                 client.Close ();
977                         }
978                 }
979                 
980                 [Test]
981                 [ExpectedException (typeof(ArgumentNullException))]
982                 public void BeginSendNull ()
983                 {
984                         UdpClient client = new UdpClient ();
985                         client.BeginSend (null, 0, null, null);
986                         client.Close ();
987                 }
988                 
989                 static bool BSSent = false;
990                 static int BSBytes;
991                 static ManualResetEvent BSCalledBack = new ManualResetEvent (false);
992                 
993                 private static void BSCallback (IAsyncResult asyncResult)
994                 {
995                         UdpClient client = (UdpClient)asyncResult.AsyncState;
996                         
997                         BSBytes = client.EndSend (asyncResult);
998                         
999                         BSSent = true;
1000                         BSCalledBack.Set ();
1001                 }
1002                 
1003                 [Test]
1004                 public void BeginSend ()
1005                 {
1006                         UdpClient client = new UdpClient ();
1007                         byte[] bytes = new byte[] {10, 11, 12, 13};
1008
1009                         try {
1010                                 client.BeginSend (bytes, bytes.Length, new AsyncCallback (BSCallback), client);
1011                                 Assert.Fail ("BeginSend #1");
1012                         } catch (SocketException ex) {
1013                                 Assert.AreEqual (10057, ex.ErrorCode,
1014                                                  "BeginSend #2");
1015                         }
1016                         
1017                         try {
1018                                 client.BeginSend (bytes, bytes.Length, null, new AsyncCallback (BSCallback), client);
1019                                 Assert.Fail ("BeginSend #3");
1020                         } catch (SocketException ex) {
1021                                 Assert.AreEqual (10057, ex.ErrorCode,
1022                                                  "BeginSend #4");
1023                         }
1024
1025                         IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1236);
1026                         
1027                         BSCalledBack.Reset ();
1028                         
1029                         client.BeginSend (bytes, bytes.Length, ep,
1030                                           new AsyncCallback (BSCallback),
1031                                           client);
1032                         if (BSCalledBack.WaitOne (2000, false) == false) {
1033                                 Assert.Fail ("BeginSend wait timed out");
1034                         }
1035                         
1036                         Assert.AreEqual (true, BSSent, "BeginSend #5");
1037                         Assert.AreEqual (4, BSBytes, "BeginSend #6");
1038
1039                         client.Close ();
1040                 }
1041                 
1042                 static bool BRReceived = false;
1043                 static byte[] BRBytes;
1044                 static IPEndPoint BRFrom;
1045                 static ManualResetEvent BRCalledBack = new ManualResetEvent (false);
1046                 
1047                 private static void BRCallback (IAsyncResult asyncResult)
1048                 {
1049                         UdpClient client = (UdpClient)asyncResult.AsyncState;
1050                         
1051                         BRBytes = client.EndReceive (asyncResult, ref BRFrom);
1052                         
1053                         BRReceived = true;
1054                         BRCalledBack.Set ();
1055                 }
1056                 
1057                 [Test]
1058                 public void BeginReceive ()
1059                 {
1060                         UdpClient client = new UdpClient (1237);
1061                         
1062                         BRCalledBack.Reset ();
1063                         
1064                         client.BeginReceive (BRCallback, client);
1065
1066                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1237);
1067                         byte[] send_bytes = new byte[] {10, 11, 12, 13};
1068                         client.Send (send_bytes, send_bytes.Length, ep);
1069
1070                         if (BRCalledBack.WaitOne (2000, false) == false) {
1071                                 Assert.Fail ("BeginReceive wait timed out");
1072                         }
1073                         
1074                         Assert.AreEqual (true, BRReceived, "BeginReceive #1");
1075                         Assert.AreEqual (4, BRBytes.Length, "BeginReceive #2");
1076                         Assert.AreEqual (ep. Port, BRFrom.Port, "BeginReceive #3");
1077                         Assert.AreEqual (ep.Address, BRFrom.Address, "BeginReceive #4");
1078
1079                         client.Close ();
1080                 }
1081                 
1082                 [Test]
1083                 public void Available ()
1084                 {
1085                         using (UdpClient client = new UdpClient (1238)) {
1086                                 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1238);
1087                                 byte[] bytes = new byte[] {10, 11, 12, 13};
1088                                 
1089                                 int res = client.Send (bytes, bytes.Length, ep);
1090                                 Assert.AreEqual (bytes.Length, res, "Send");
1091
1092                                 // that might happen too quickly, data sent and not yet received/available
1093                                 Thread.Sleep (100);
1094                                 int avail = client.Available;
1095                                 
1096                                 Assert.AreEqual (bytes.Length, avail, "Available #1");
1097
1098                                 client.Close ();
1099                         }
1100                 }
1101                 
1102                 [Test]
1103                 [Category ("NotWorking")]  // Using PMTU settings workaround on Linux, default true
1104                 public void DontFragmentDefault ()
1105                 {
1106                         UdpClient client = new UdpClient ();
1107                         
1108                         /* Ignore the docs, testing shows the default
1109                          * here is in fact false
1110                          */
1111                         Assert.AreEqual (false, client.DontFragment, "DontFragmentDefault");
1112
1113                         client.Close ();
1114                 }
1115                 
1116                 [Test]
1117                 public void EnableBroadcastDefault ()
1118                 {
1119                         UdpClient client = new UdpClient ();
1120                         
1121                         Assert.AreEqual (false, client.EnableBroadcast, "EnableBroadcastDefault");
1122
1123                         client.Close ();
1124                 }
1125                 
1126                 /* Can't test the default for ExclusiveAddressUse as
1127                  * it's different on different versions and service
1128                  * packs of windows
1129                  */
1130                 [Test]
1131                 [Category ("NotWorking")] // Not supported on Linux
1132                 public void ExclusiveAddressUseUnbound ()
1133                 {
1134                         UdpClient client = new UdpClient ();
1135
1136                         client.ExclusiveAddressUse = true;
1137
1138                         Assert.AreEqual (true, client.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
1139
1140                         client.Close ();
1141                 }
1142                 
1143                 [Test]
1144                 [ExpectedException (typeof(InvalidOperationException))]
1145                 [Category ("NotWorking")] // Not supported on Linux
1146                 public void ExclusiveAddressUseBound ()
1147                 {
1148                         UdpClient client = new UdpClient (1239);
1149
1150                         client.ExclusiveAddressUse = true;
1151
1152                         client.Close ();
1153                 }
1154                 
1155                 [Test]
1156                 public void MulticastLoopbackDefault ()
1157                 {
1158                         UdpClient client = new UdpClient ();
1159                         
1160                         Assert.AreEqual (true, client.MulticastLoopback, "MulticastLoopbackDefault");
1161
1162                         client.Close ();
1163                 }
1164                 
1165                 /* No test for Ttl default as it is platform dependent */
1166 #endif
1167
1168                 class MyUdpClient : UdpClient
1169                 {
1170                         public MyUdpClient ()
1171                         {
1172                         }
1173
1174                         public MyUdpClient (AddressFamily family)
1175                                 : base (family)
1176                         {
1177                         }
1178
1179                         public MyUdpClient (Int32 port)
1180                                 : base (port)
1181                         {
1182                         }
1183
1184
1185                         public MyUdpClient (IPEndPoint localEP)
1186                                 : base (localEP)
1187                         {
1188                         }
1189
1190                         public MyUdpClient (int port, AddressFamily family)
1191                                 : base (port, family)
1192                         {
1193                         }
1194
1195                         public MyUdpClient (string hostname, int port)
1196                                 : base (hostname, port)
1197                         {
1198                         }
1199
1200                         public new bool Active {
1201                                 get { return base.Active; }
1202                                 set { base.Active = value; }
1203                         }
1204
1205 #if ONLY_1_1
1206                         public new Socket Client {
1207                                 get { return base.Client; }
1208                                 set { base.Client = value; }
1209                         }
1210 #endif
1211                 }
1212         }
1213 }