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