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