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