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