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