Merge pull request #347 from JamesB7/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                 [Category ("NotOnMac")]
266                 public void Constructor5 ()
267                 {
268                         MyUdpClient client;
269                         Socket s;
270                         IPEndPoint localEP;
271
272                         client = new MyUdpClient (IPEndPoint.MinPort, AddressFamily.InterNetwork);
273                         s = client.Client;
274                         Assert.IsNotNull (s, "#A:Client");
275                         Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
276                         Assert.IsFalse (s.Connected, "#A:Client:Connected");
277 #if NET_2_0
278                         Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
279 #endif
280                         Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
281                         Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
282                         Assert.IsFalse (client.Active, "#A:Active");
283 #if NET_2_0
284                         //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
285                         Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
286                         //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
287                         Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
288                         //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
289 #endif
290                         localEP = s.LocalEndPoint as IPEndPoint;
291                         Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
292                         Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
293                         Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
294
295                         client = new MyUdpClient (IPEndPoint.MaxPort, AddressFamily.InterNetworkV6);
296                         s = client.Client;
297                         Assert.IsNotNull (s, "#B:Client");
298                         Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
299                         Assert.IsFalse (s.Connected, "#B:Client:Connected");
300 #if NET_2_0
301                         Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
302 #endif
303                         Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
304                         Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
305                         Assert.IsFalse (client.Active, "#B:Active");
306 #if NET_2_0
307                         //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
308                         Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
309                         //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
310                         Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
311                         //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
312 #endif
313                         localEP = s.LocalEndPoint as IPEndPoint;
314                         Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
315                         Assert.AreEqual (IPAddress.IPv6Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
316                         Assert.AreEqual (AddressFamily.InterNetworkV6, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
317                         Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
318                 }
319
320                 [Test] // .ctor (Int32, AddressFamily)
321                 public void Constructor5_Family_Invalid ()
322                 {
323                         try {
324                                 new UdpClient (80, AddressFamily.NetBios);
325                                 Assert.Fail ("#A1");
326                         } catch (ArgumentException ex) {
327                                 // family
328                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
329                                 Assert.IsNull (ex.InnerException, "#A3");
330 #if NET_2_0
331                                 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
332                                 // addresses
333                                 Assert.IsNotNull (ex.Message, "#A4");
334                                 Assert.AreEqual ("family", ex.ParamName, "#A5");
335 #else
336                                 Assert.AreEqual ("family", ex.Message, "#A4");
337                                 Assert.IsNull (ex.ParamName, "#A5");
338 #endif
339                         }
340
341                         try {
342                                 new UdpClient (80, (AddressFamily) 666);
343                                 Assert.Fail ("#B1");
344                         } catch (ArgumentException ex) {
345                                 // family
346                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
347                                 Assert.IsNull (ex.InnerException, "#B3");
348 #if NET_2_0
349                                 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
350                                 // addresses
351                                 Assert.IsNotNull (ex.Message, "#B4");
352                                 Assert.AreEqual ("family", ex.ParamName, "#B5");
353 #else
354                                 Assert.AreEqual ("family", ex.Message, "#B4");
355                                 Assert.IsNull (ex.ParamName, "#B5");
356 #endif
357                         }
358                 }
359
360                 [Test] // .ctor (Int32, AddressFamily)
361                 public void Constructor5_Port_OutOfRange ()
362                 {
363                         try {
364                                 new UdpClient (IPEndPoint.MaxPort + 1, AddressFamily.InterNetwork);
365                                 Assert.Fail ("#A1");
366                         } catch (ArgumentOutOfRangeException ex) {
367                                 // Specified argument was out of the range of valid values
368                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
369                                 Assert.IsNull (ex.InnerException, "#A3");
370                                 Assert.IsNotNull (ex.Message, "#A4");
371                                 Assert.AreEqual ("port", ex.ParamName, "#A5");
372                         }
373
374                         try {
375                                 new UdpClient (IPEndPoint.MinPort - 1, AddressFamily.InterNetwork);
376                                 Assert.Fail ("#A1");
377                         } catch (ArgumentOutOfRangeException ex) {
378                                 // Specified argument was out of the range of valid values
379                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
380                                 Assert.IsNull (ex.InnerException, "#A3");
381                                 Assert.IsNotNull (ex.Message, "#A4");
382                                 Assert.AreEqual ("port", ex.ParamName, "#A5");
383                         }
384                 }
385
386                 [Test] // .ctor (String, Int32)
387                 [Category ("NotOnMac")]
388                 public void Constructor6 ()
389                 {
390                         MyUdpClient client;
391                         Socket s;
392                         IPEndPoint localEP;
393
394                         client = new MyUdpClient ("localhost", IPEndPoint.MinPort);
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 ("localhost", 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                 [Category ("NotOnMac")]
483                 public void UdpClientBroadcastTest () 
484                 {
485                         UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
486                         byte[] bytes = new byte[] {10, 11, 12, 13};
487
488                         try {
489                                 client.Send (bytes, bytes.Length, new IPEndPoint (IPAddress.Broadcast, 1235));
490                         } finally {
491                                 client.Close ();
492                         }
493                 }
494
495                 [Test] // JoinMulticastGroup (IPAddress)
496                 public void JoinMulticastGroup1_IPv4 ()
497                 {
498                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
499
500                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
501                                 client.JoinMulticastGroup (mcast_addr);
502                         }
503                 }
504
505                 [Test] // JoinMulticastGroup (IPAddress)
506                 [Category ("NotOnMac")]
507                 public void JoinMulticastGroup1_IPv6 ()
508                 {
509 #if NET_2_0
510                         if (!Socket.OSSupportsIPv6)
511 #else
512                         if (!Socket.SupportsIPv6)
513 #endif
514                                 Assert.Ignore ("IPv6 not enabled.");
515
516                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
517
518                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
519                                 client.JoinMulticastGroup (mcast_addr);
520                         }
521                 }
522
523                 [Test] // JoinMulticastGroup (IPAddress)
524                 public void JoinMulticastGroup1_MulticastAddr_Null ()
525                 {
526                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
527                                 try {
528                                         client.JoinMulticastGroup ((IPAddress) null);
529                                         Assert.Fail ("#1");
530 #if NET_2_0
531                                 } catch (ArgumentNullException ex) {
532                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
533                                         Assert.IsNull (ex.InnerException, "#3");
534                                         Assert.IsNotNull (ex.Message, "#4");
535                                         Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
536                                 }
537 #else
538                                 } catch (NullReferenceException ex) {
539                                         Assert.AreEqual (typeof (NullReferenceException), ex.GetType (), "#2");
540                                         Assert.IsNull (ex.InnerException, "#3");
541                                         Assert.IsNotNull (ex.Message, "#4");
542                                 }
543 #endif
544                         }
545                 }
546
547                 [Test] // JoinMulticastGroup (IPAddress)
548                 public void JoinMulticastGroup1_Socket_Closed ()
549                 {
550                         IPAddress mcast_addr = null;
551
552                         UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
553                         client.Close ();
554                         try {
555                                 client.JoinMulticastGroup (mcast_addr);
556                                 Assert.Fail ("#1");
557                         } catch (ObjectDisposedException ex) {
558                                 // Cannot access a disposed object
559                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
560                                 Assert.IsNull (ex.InnerException, "#3");
561                                 Assert.IsNotNull (ex.Message, "#4");
562                                 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
563                         }
564                 }
565
566                 [Test] // JoinMulticastGroup (IPAddress)
567                 [Category ("NotWorking")]
568                 public void JoinMulticastGroup1_Socket_NotBound ()
569                 {
570                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
571
572                         UdpClient client = new UdpClient (AddressFamily.InterNetwork);
573                         try {
574                                 client.JoinMulticastGroup (mcast_addr);
575                                 Assert.Fail ("#1");
576                         } catch (SocketException ex) {
577                                 // An invalid argument was supplied
578                                 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
579                                 Assert.AreEqual (10022, ex.ErrorCode, "#3");
580                                 Assert.IsNull (ex.InnerException, "#4");
581                                 Assert.IsNotNull (ex.Message, "#5");
582                                 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
583 #if NET_2_0
584                                 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
585 #endif
586                         } finally {
587                                 client.Close ();
588                         }
589                 }
590
591                 [Test] // JoinMulticastGroup (In32, IPAddress)
592                 public void JoinMulticastGroup2_IPv4 ()
593                 {
594                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
595
596                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
597                                 try {
598                                         client.JoinMulticastGroup (0, mcast_addr);
599                                         Assert.Fail ("#1");
600                                 } catch (SocketException ex) {
601                                         // The attempted operation is not supported for the type of
602                                         // object referenced
603                                         Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
604                                         Assert.AreEqual (10045, ex.ErrorCode, "#3");
605                                         Assert.IsNull (ex.InnerException, "#4");
606                                         Assert.IsNotNull (ex.Message, "#5");
607                                         Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
608 #if NET_2_0
609                                         Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
610 #endif
611                                 }
612                         }
613                 }
614
615                 [Test] // JoinMulticastGroup (In32, IPAddress)
616                 [Category ("NotOnMac")]
617                 public void JoinMulticastGroup2_IPv6 ()
618                 {
619 #if NET_2_0
620                         if (!Socket.OSSupportsIPv6)
621 #else
622                         if (!Socket.SupportsIPv6)
623 #endif
624                                 Assert.Ignore ("IPv6 not enabled.");
625
626                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
627
628                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
629                                 client.JoinMulticastGroup (0, mcast_addr);
630                         }
631                 }
632
633                 [Test] // JoinMulticastGroup (Int32, IPAddress)
634                 public void JoinMulticastGroup2_MulticastAddr_Null ()
635                 {
636                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
637                                 try {
638                                         client.JoinMulticastGroup (0, (IPAddress) null);
639                                         Assert.Fail ("#1");
640                                 } catch (ArgumentNullException ex) {
641                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
642                                         Assert.IsNull (ex.InnerException, "#3");
643                                         Assert.IsNotNull (ex.Message, "#4");
644                                         Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
645                                 }
646                         }
647                 }
648
649                 [Test] // JoinMulticastGroup (Int32, IPAddress)
650                 public void JoinMulticastGroup2_Socket_Closed ()
651                 {
652                         IPAddress mcast_addr = null;
653
654                         UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234));
655                         client.Close ();
656                         try {
657                                 client.JoinMulticastGroup (0, mcast_addr);
658                                 Assert.Fail ("#1");
659                         } catch (ObjectDisposedException ex) {
660                                 // Cannot access a disposed object
661                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
662                                 Assert.IsNull (ex.InnerException, "#3");
663                                 Assert.IsNotNull (ex.Message, "#4");
664                                 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
665                         }
666                 }
667
668                 [Test] // JoinMulticastGroup (Int32, IPAddress)
669                 [Category ("NotWorking")]
670                 public void JoinMulticastGroup2_Socket_NotBound ()
671                 {
672                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
673
674                         UdpClient client = new UdpClient (AddressFamily.InterNetworkV6);
675                         try {
676                                 client.JoinMulticastGroup (0, mcast_addr);
677                                 Assert.Fail ("#1");
678                         } catch (SocketException ex) {
679                                 // An invalid argument was supplied
680                                 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
681                                 Assert.AreEqual (10022, ex.ErrorCode, "#3");
682                                 Assert.IsNull (ex.InnerException, "#4");
683                                 Assert.IsNotNull (ex.Message, "#5");
684                                 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
685 #if NET_2_0
686                                 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
687 #endif
688                         } finally {
689                                 client.Close ();
690                         }
691                 }
692
693                 [Test] // JoinMulticastGroup (IPAddress, Int32)
694                 public void JoinMulticastGroup3_IPv4 ()
695                 {
696                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
697
698                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
699                                 client.JoinMulticastGroup (mcast_addr, 0);
700                         }
701
702                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
703                                 client.JoinMulticastGroup (mcast_addr, 255);
704                         }
705                 }
706
707                 [Test] // JoinMulticastGroup (IPAddress, Int32)
708                 [Category ("NotOnMac")]
709                 public void JoinMulticastGroup3_IPv6 ()
710                 {
711 #if NET_2_0
712                         if (!Socket.OSSupportsIPv6)
713 #else
714                         if (!Socket.SupportsIPv6)
715 #endif
716                                 Assert.Ignore ("IPv6 not enabled.");
717
718                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
719
720                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
721                                 client.JoinMulticastGroup (mcast_addr, 0);
722                         }
723
724                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
725                                 client.JoinMulticastGroup (mcast_addr, 255);
726                         }
727                 }
728
729                 [Test] // JoinMulticastGroup (IPAddress, Int32)
730                 public void JoinMulticastGroup3_MulticastAddr_Null ()
731                 {
732                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
733                                 try {
734                                         client.JoinMulticastGroup ((IPAddress) null, int.MaxValue);
735                                         Assert.Fail ("#1");
736                                 } catch (ArgumentNullException ex) {
737                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
738                                         Assert.IsNull (ex.InnerException, "#3");
739                                         Assert.IsNotNull (ex.Message, "#4");
740                                         Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
741                                 }
742                         }
743                 }
744
745                 [Test] // JoinMulticastGroup (IPAddress, Int32)
746                 public void JoinMulticastGroup3_Socket_Closed ()
747                 {
748                         IPAddress mcast_addr = null;
749
750                         UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
751                         client.Close ();
752                         try {
753                                 client.JoinMulticastGroup (mcast_addr, 0);
754                                 Assert.Fail ("#1");
755                         } catch (ObjectDisposedException ex) {
756                                 // Cannot access a disposed object
757                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
758                                 Assert.IsNull (ex.InnerException, "#3");
759                                 Assert.IsNotNull (ex.Message, "#4");
760                                 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
761                         }
762                 }
763
764                 [Test] // JoinMulticastGroup (IPAddress, Int32)
765                 [Category ("NotWorking")]
766                 public void JoinMulticastGroup3_Socket_NotBound ()
767                 {
768                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
769
770                         UdpClient client = new UdpClient (AddressFamily.InterNetwork);
771                         try {
772                                 client.JoinMulticastGroup (mcast_addr, 5);
773                                 Assert.Fail ("#1");
774                         } catch (SocketException ex) {
775                                 // An invalid argument was supplied
776                                 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
777                                 Assert.AreEqual (10022, ex.ErrorCode, "#3");
778                                 Assert.IsNull (ex.InnerException, "#4");
779                                 Assert.IsNotNull (ex.Message, "#5");
780                                 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
781 #if NET_2_0
782                                 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
783 #endif
784                         } finally {
785                                 client.Close ();
786                         }
787                 }
788
789 #if NET_2_0
790                 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
791                 public void JoinMulticastGroup4_IPv4 ()
792                 {
793                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
794                         IPAddress local_addr = IPAddress.Any;
795
796                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
797                                 client.JoinMulticastGroup (mcast_addr, local_addr);
798                         }
799                 }
800
801                 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
802                 public void JoinMulticastGroup4_IPv6 ()
803                 {
804                         if (!Socket.OSSupportsIPv6)
805                                 Assert.Ignore ("IPv6 not enabled.");
806
807                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
808                         IPAddress local_addr = IPAddress.IPv6Any;
809
810                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
811                                 try {
812                                         client.JoinMulticastGroup (mcast_addr, local_addr);
813                                         Assert.Fail ("#1");
814                                 } catch (SocketException ex) {
815                                         // The attempted operation is not supported for the type of
816                                         // object referenced
817                                         Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
818                                         Assert.AreEqual (10045, ex.ErrorCode, "#3");
819                                         Assert.IsNull (ex.InnerException, "#4");
820                                         Assert.IsNotNull (ex.Message, "#5");
821                                         Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
822                                         Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
823                                 }
824                         }
825                 }
826
827                 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
828                 public void JoinMulticastGroup4_LocalAddress_Null ()
829                 {
830                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
831
832                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
833                                 try {
834                                         client.JoinMulticastGroup (mcast_addr, (IPAddress) null);
835                                         Assert.Fail ("#1");
836                                 } catch (ArgumentNullException ex) {
837                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
838                                         Assert.IsNull (ex.InnerException, "#3");
839                                         Assert.IsNotNull (ex.Message, "#4");
840                                         Assert.AreEqual ("mcint", ex.ParamName, "#5");
841                                 }
842                         }
843                 }
844
845                 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
846                 public void JoinMulticastGroup4_MulticastAddr_Null ()
847                 {
848                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
849                                 try {
850                                         client.JoinMulticastGroup ((IPAddress) null, IPAddress.Loopback);
851                                         Assert.Fail ("#1");
852                                 } catch (ArgumentNullException ex) {
853                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
854                                         Assert.IsNull (ex.InnerException, "#3");
855                                         Assert.IsNotNull (ex.Message, "#4");
856                                         Assert.AreEqual ("group", ex.ParamName, "#5");
857                                 }
858                         }
859                 }
860
861                 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
862                 public void JoinMulticastGroup4_Socket_Closed ()
863                 {
864                         IPAddress mcast_addr = null;
865                         IPAddress local_addr = null;
866
867                         UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
868                         client.Close ();
869                         try {
870                                 client.JoinMulticastGroup (mcast_addr, local_addr);
871                                 Assert.Fail ("#1");
872                         } catch (ObjectDisposedException ex) {
873                                 // Cannot access a disposed object
874                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
875                                 Assert.IsNull (ex.InnerException, "#3");
876                                 Assert.IsNotNull (ex.Message, "#4");
877                                 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
878                         }
879                 }
880
881                 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
882                 [Category ("NotWorking")]
883                 public void JoinMulticastGroup4_Socket_NotBound ()
884                 {
885                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
886                         IPAddress local_addr = Dns.GetHostEntry (string.Empty).AddressList [0];
887
888                         UdpClient client = new UdpClient (AddressFamily.InterNetwork);
889                         try {
890                                 client.JoinMulticastGroup (mcast_addr, local_addr);
891                                 Assert.Fail ("#1");
892                         } catch (SocketException ex) {
893                                 // An invalid argument was supplied
894                                 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
895                                 Assert.AreEqual (10022, ex.ErrorCode, "#3");
896                                 Assert.IsNull (ex.InnerException, "#4");
897                                 Assert.IsNotNull (ex.Message, "#5");
898                                 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
899                                 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
900                         } finally {
901                                 client.Close ();
902                         }
903                 }
904
905                 [Test]
906                 public void CloseInReceive ()
907                 {
908                         UdpClient client = new UdpClient (50000);
909                         new Thread(delegate() {
910                                 Thread.Sleep(2000);
911                                 client.Close();
912                                 }).Start();
913
914                         bool got_exc = false;
915                         IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
916                         try {
917                                 client.Receive(ref ep);
918                         } catch (SocketException) {
919                                 got_exc = true;
920                         } finally {
921                                 client.Close ();
922                         }
923                         Assert.IsTrue (got_exc);
924                 }
925
926                 // Test for bug 324033
927                 [Test]
928                 public void JoinMulticastGroupWithLocal ()
929                 {
930                         UdpClient client = new UdpClient (9001);
931                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.24");
932                         IPAddress local_addr = IPAddress.Any;
933
934                         try {
935                                 client.JoinMulticastGroup (mcast_addr, local_addr);
936                         } finally {
937                                 client.Close ();
938                         }
939                 }
940                 
941                 [Test]
942                 [ExpectedException (typeof(ArgumentNullException))]
943                 public void BeginSendNull ()
944                 {
945                         UdpClient client = new UdpClient ();
946                         client.BeginSend (null, 0, null, null);
947                         client.Close ();
948                 }
949                 
950                 static bool BSSent = false;
951                 static int BSBytes;
952                 static ManualResetEvent BSCalledBack = new ManualResetEvent (false);
953                 
954                 private static void BSCallback (IAsyncResult asyncResult)
955                 {
956                         UdpClient client = (UdpClient)asyncResult.AsyncState;
957                         
958                         BSBytes = client.EndSend (asyncResult);
959                         
960                         BSSent = true;
961                         BSCalledBack.Set ();
962                 }
963                 
964                 [Test]
965                 [Category ("NotOnMac")]
966                 public void BeginSend ()
967                 {
968                         UdpClient client = new UdpClient ();
969                         byte[] bytes = new byte[] {10, 11, 12, 13};
970
971                         try {
972                                 client.BeginSend (bytes, bytes.Length, new AsyncCallback (BSCallback), client);
973                                 Assert.Fail ("BeginSend #1");
974                         } catch (SocketException ex) {
975                                 Assert.AreEqual (10057, ex.ErrorCode,
976                                                  "BeginSend #2");
977                         }
978                         
979                         try {
980                                 client.BeginSend (bytes, bytes.Length, null, new AsyncCallback (BSCallback), client);
981                                 Assert.Fail ("BeginSend #3");
982                         } catch (SocketException ex) {
983                                 Assert.AreEqual (10057, ex.ErrorCode,
984                                                  "BeginSend #4");
985                         }
986
987                         IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1236);
988                         
989                         BSCalledBack.Reset ();
990                         
991                         client.BeginSend (bytes, bytes.Length, ep,
992                                           new AsyncCallback (BSCallback),
993                                           client);
994                         if (BSCalledBack.WaitOne (2000, false) == false) {
995                                 Assert.Fail ("BeginSend wait timed out");
996                         }
997                         
998                         Assert.AreEqual (true, BSSent, "BeginSend #5");
999                         Assert.AreEqual (4, BSBytes, "BeginSend #6");
1000
1001                         client.Close ();
1002                 }
1003                 
1004                 static bool BRReceived = false;
1005                 static byte[] BRBytes;
1006                 static IPEndPoint BRFrom;
1007                 static ManualResetEvent BRCalledBack = new ManualResetEvent (false);
1008                 
1009                 private static void BRCallback (IAsyncResult asyncResult)
1010                 {
1011                         UdpClient client = (UdpClient)asyncResult.AsyncState;
1012                         
1013                         BRBytes = client.EndReceive (asyncResult, ref BRFrom);
1014                         
1015                         BRReceived = true;
1016                         BRCalledBack.Set ();
1017                 }
1018                 
1019                 [Test]
1020                 [Category ("NotOnMac")]
1021                 public void BeginReceive ()
1022                 {
1023                         UdpClient client = new UdpClient (1237);
1024                         
1025                         BRCalledBack.Reset ();
1026                         
1027                         client.BeginReceive (BRCallback, client);
1028
1029                         IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1237);
1030                         byte[] send_bytes = new byte[] {10, 11, 12, 13};
1031                         client.Send (send_bytes, send_bytes.Length, ep);
1032
1033                         if (BRCalledBack.WaitOne (2000, false) == false) {
1034                                 Assert.Fail ("BeginReceive wait timed out");
1035                         }
1036                         
1037                         Assert.AreEqual (true, BRReceived, "BeginReceive #1");
1038                         Assert.AreEqual (4, BRBytes.Length, "BeginReceive #2");
1039                         Assert.AreEqual (ep. Port, BRFrom.Port, "BeginReceive #3");
1040                         Assert.AreEqual (ep.Address, BRFrom.Address, "BeginReceive #4");
1041
1042                         client.Close ();
1043                 }
1044                 
1045                 [Test]
1046                 [Category ("NotOnMac")]
1047                 public void Available ()
1048                 {
1049                         UdpClient client = new UdpClient (1238);
1050                         IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1238);
1051                         byte[] bytes = new byte[] {10, 11, 12, 13};
1052                         
1053                         client.Send (bytes, bytes.Length, ep);
1054                         int avail = client.Available;
1055                         
1056                         Assert.AreEqual (bytes.Length, avail, "Available #1");
1057
1058                         client.Close ();
1059                 }
1060                 
1061                 [Test]
1062                 [Category ("NotWorking")]  // Using PMTU settings workaround on Linux, default true
1063                 public void DontFragmentDefault ()
1064                 {
1065                         UdpClient client = new UdpClient ();
1066                         
1067                         /* Ignore the docs, testing shows the default
1068                          * here is in fact false
1069                          */
1070                         Assert.AreEqual (false, client.DontFragment, "DontFragmentDefault");
1071
1072                         client.Close ();
1073                 }
1074                 
1075                 [Test]
1076                 public void EnableBroadcastDefault ()
1077                 {
1078                         UdpClient client = new UdpClient ();
1079                         
1080                         Assert.AreEqual (false, client.EnableBroadcast, "EnableBroadcastDefault");
1081
1082                         client.Close ();
1083                 }
1084                 
1085                 /* Can't test the default for ExclusiveAddressUse as
1086                  * it's different on different versions and service
1087                  * packs of windows
1088                  */
1089                 [Test]
1090                 [Category ("NotWorking")] // Not supported on Linux
1091                 public void ExclusiveAddressUseUnbound ()
1092                 {
1093                         UdpClient client = new UdpClient ();
1094
1095                         client.ExclusiveAddressUse = true;
1096
1097                         Assert.AreEqual (true, client.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
1098
1099                         client.Close ();
1100                 }
1101                 
1102                 [Test]
1103                 [ExpectedException (typeof(InvalidOperationException))]
1104                 [Category ("NotWorking")] // Not supported on Linux
1105                 public void ExclusiveAddressUseBound ()
1106                 {
1107                         UdpClient client = new UdpClient (1239);
1108
1109                         client.ExclusiveAddressUse = true;
1110
1111                         client.Close ();
1112                 }
1113                 
1114                 [Test]
1115                 public void MulticastLoopbackDefault ()
1116                 {
1117                         UdpClient client = new UdpClient ();
1118                         
1119                         Assert.AreEqual (true, client.MulticastLoopback, "MulticastLoopbackDefault");
1120
1121                         client.Close ();
1122                 }
1123                 
1124                 /* No test for Ttl default as it is platform dependent */
1125 #endif
1126
1127                 class MyUdpClient : UdpClient
1128                 {
1129                         public MyUdpClient ()
1130                         {
1131                         }
1132
1133                         public MyUdpClient (AddressFamily family)
1134                                 : base (family)
1135                         {
1136                         }
1137
1138                         public MyUdpClient (Int32 port)
1139                                 : base (port)
1140                         {
1141                         }
1142
1143
1144                         public MyUdpClient (IPEndPoint localEP)
1145                                 : base (localEP)
1146                         {
1147                         }
1148
1149                         public MyUdpClient (int port, AddressFamily family)
1150                                 : base (port, family)
1151                         {
1152                         }
1153
1154                         public MyUdpClient (string hostname, int port)
1155                                 : base (hostname, port)
1156                         {
1157                         }
1158
1159                         public new bool Active {
1160                                 get { return base.Active; }
1161                                 set { base.Active = value; }
1162                         }
1163
1164 #if ONLY_1_1
1165                         public new Socket Client {
1166                                 get { return base.Client; }
1167                                 set { base.Client = value; }
1168                         }
1169 #endif
1170                 }
1171         }
1172 }