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