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