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