1 // System.Net.Sockets.UdpClientTest.cs
4 // Chris Bacon <chris.bacon@docobo.co.uk>
5 // Gert Driesen <drieseng@users.sourceforge.net>
10 using System.Net.Sockets;
11 using System.Threading;
13 using NUnit.Framework;
15 namespace MonoTests.System.Net.Sockets {
17 [Ignore("UdpClient is not supported - since UDP sockets are not supported")]
20 public class UdpClientTest {
22 public void Constructor1 ()
27 client = new MyUdpClient ();
29 Assert.IsNotNull (s, "Client");
30 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "Client:AddressFamily");
31 Assert.IsFalse (s.Connected, "Client:Connected");
33 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
35 Assert.IsNull (s.LocalEndPoint, "Client:LocalEndPoint");
36 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "Client:ProtocolType");
37 Assert.IsNull (s.RemoteEndPoint, "Client:RemoteEndPoint");
38 Assert.AreEqual (SocketType.Dgram, s.SocketType, "Client:SocketType");
39 Assert.IsFalse (client.Active, "Active");
41 Assert.IsFalse (client.DontFragment, "DontFragment");
42 Assert.IsFalse (client.EnableBroadcast, "EnableBroadcast");
43 //Assert.IsFalse (client.ExclusiveAddressUse, "ExclusiveAddressUse");
44 Assert.IsTrue (client.MulticastLoopback, "MulticastLoopback");
45 //Assert.AreEqual (32, client.Ttl, "Ttl");
49 [Test] // .ctor (AddressFamily)
50 public void Constructor2 ()
55 client = new MyUdpClient (AddressFamily.InterNetwork);
57 Assert.IsNotNull (s, "#A:Client");
58 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
59 Assert.IsFalse (s.Connected, "#A:Client:Connected");
61 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
63 Assert.IsNull (s.LocalEndPoint, "#A:Client:LocalEndPoint");
64 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
65 Assert.IsNull (s.RemoteEndPoint, "#A:Client:RemoteEndPoint");
66 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
67 Assert.IsFalse (client.Active, "#A:Active");
69 //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
70 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
71 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
72 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
73 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
76 client = new MyUdpClient (AddressFamily.InterNetworkV6);
78 Assert.IsNotNull (s, "#B:Client");
79 Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
80 Assert.IsFalse (s.Connected, "#B:Client:Connected");
82 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
84 Assert.IsNull (s.LocalEndPoint, "#B:Client:LocalEndPoint");
85 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
86 Assert.IsNull (s.RemoteEndPoint, "#B:Client:RemoteEndPoint");
87 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
88 Assert.IsFalse (client.Active, "#B:Active");
90 //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
91 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
92 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
93 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
94 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
98 [Test] // .ctor (AddressFamily)
99 public void Constructor2_Family_Invalid ()
102 new UdpClient (AddressFamily.NetBios);
104 } catch (ArgumentException ex) {
105 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
106 Assert.IsNull (ex.InnerException, "#A3");
108 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
110 Assert.IsNotNull (ex.Message, "#A4");
111 Assert.AreEqual ("family", ex.ParamName, "#A5");
113 Assert.AreEqual ("family", ex.Message, "#A4");
114 Assert.IsNull (ex.ParamName, "#A5");
119 new UdpClient ((AddressFamily) 666);
121 } catch (ArgumentException ex) {
122 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
123 Assert.IsNull (ex.InnerException, "#B3");
125 Assert.IsNotNull (ex.Message, "#B4");
126 Assert.AreEqual ("family", ex.ParamName, "#B5");
128 Assert.AreEqual ("family", ex.Message, "#B4");
129 Assert.IsNull (ex.ParamName, "#B5");
134 [Test] // .ctor (Int32)
135 public void Constructor3 ()
141 client = new MyUdpClient (IPEndPoint.MinPort);
143 Assert.IsNotNull (s, "#A:Client");
144 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
145 Assert.IsFalse (s.Connected, "#A:Client:Connected");
147 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
149 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
150 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
151 Assert.IsFalse (client.Active, "#A:Active");
153 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
154 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
155 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
156 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
157 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
159 localEP = s.LocalEndPoint as IPEndPoint;
160 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
161 Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
162 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
164 client = new MyUdpClient (IPEndPoint.MaxPort);
166 Assert.IsNotNull (s, "#B:Client");
167 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
168 Assert.IsFalse (s.Connected, "#B:Client:Connected");
170 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
172 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
173 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
174 Assert.IsFalse (client.Active, "#B:Active");
176 Assert.IsFalse (client.DontFragment, "#B:DontFragment");
177 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
178 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
179 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
180 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
182 localEP = s.LocalEndPoint as IPEndPoint;
183 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
184 Assert.AreEqual (IPAddress.Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
185 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
186 Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
189 [Test] // .ctor (Int32)
190 public void Constructor3_Port_OutOfRange ()
193 new UdpClient (IPEndPoint.MaxPort + 1);
195 } catch (ArgumentOutOfRangeException ex) {
196 // Specified argument was out of the range of valid values
197 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
198 Assert.IsNull (ex.InnerException, "#A3");
199 Assert.IsNotNull (ex.Message, "#A4");
200 Assert.AreEqual ("port", ex.ParamName, "#A5");
204 new UdpClient (IPEndPoint.MinPort - 1);
206 } catch (ArgumentOutOfRangeException ex) {
207 // Specified argument was out of the range of valid values
208 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
209 Assert.IsNull (ex.InnerException, "#A3");
210 Assert.IsNotNull (ex.Message, "#A4");
211 Assert.AreEqual ("port", ex.ParamName, "#A5");
215 [Test] // .ctor (IPEndPoint)
216 public void Constructor4 ()
223 clientEP = new IPEndPoint (IPAddress.Loopback, 8001);
224 client = new MyUdpClient (clientEP);
226 Assert.IsNotNull (s, "#A:Client");
227 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
228 Assert.IsFalse (s.Connected, "#A:Client:Connected");
230 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
232 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
233 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
234 Assert.IsFalse (client.Active, "#A:Active");
236 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
237 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
238 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
239 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
240 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
242 localEP = s.LocalEndPoint as IPEndPoint;
243 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
244 Assert.IsFalse (object.ReferenceEquals (clientEP, localEP), "#A:Client:LocalEndPoint/ReferenceEquality");
245 Assert.AreEqual (clientEP.Address, localEP.Address, "#A:Client:LocalEndPoint/Address");
246 Assert.AreEqual (clientEP.AddressFamily, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
247 Assert.AreEqual (clientEP.Port, localEP.Port, "#A:Client:LocalEndPoint/Port");
250 [Test] // .ctor (IPEndPoint)
251 public void Constructor4_LocalEP_Null ()
254 new UdpClient ((IPEndPoint) null);
256 } catch (ArgumentNullException ex) {
257 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
258 Assert.IsNull (ex.InnerException, "#3");
259 Assert.IsNotNull (ex.Message, "#4");
260 Assert.AreEqual ("localEP", ex.ParamName, "#5");
264 [Test] // .ctor (Int32, AddressFamily)
265 [Category ("NotOnMac")]
266 public void Constructor5 ()
272 client = new MyUdpClient (IPEndPoint.MinPort, AddressFamily.InterNetwork);
274 Assert.IsNotNull (s, "#A:Client");
275 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
276 Assert.IsFalse (s.Connected, "#A:Client:Connected");
278 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
280 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
281 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
282 Assert.IsFalse (client.Active, "#A:Active");
284 //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
285 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
286 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
287 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
288 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
290 localEP = s.LocalEndPoint as IPEndPoint;
291 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
292 Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
293 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
295 client = new MyUdpClient (IPEndPoint.MaxPort, AddressFamily.InterNetworkV6);
297 Assert.IsNotNull (s, "#B:Client");
298 Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
299 Assert.IsFalse (s.Connected, "#B:Client:Connected");
301 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
303 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
304 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
305 Assert.IsFalse (client.Active, "#B:Active");
307 //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
308 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
309 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
310 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
311 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
313 localEP = s.LocalEndPoint as IPEndPoint;
314 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
315 Assert.AreEqual (IPAddress.IPv6Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
316 Assert.AreEqual (AddressFamily.InterNetworkV6, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
317 Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
320 [Test] // .ctor (Int32, AddressFamily)
321 public void Constructor5_Family_Invalid ()
324 new UdpClient (80, AddressFamily.NetBios);
326 } catch (ArgumentException ex) {
328 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
329 Assert.IsNull (ex.InnerException, "#A3");
331 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
333 Assert.IsNotNull (ex.Message, "#A4");
334 Assert.AreEqual ("family", ex.ParamName, "#A5");
336 Assert.AreEqual ("family", ex.Message, "#A4");
337 Assert.IsNull (ex.ParamName, "#A5");
342 new UdpClient (80, (AddressFamily) 666);
344 } catch (ArgumentException ex) {
346 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
347 Assert.IsNull (ex.InnerException, "#B3");
349 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
351 Assert.IsNotNull (ex.Message, "#B4");
352 Assert.AreEqual ("family", ex.ParamName, "#B5");
354 Assert.AreEqual ("family", ex.Message, "#B4");
355 Assert.IsNull (ex.ParamName, "#B5");
360 [Test] // .ctor (Int32, AddressFamily)
361 public void Constructor5_Port_OutOfRange ()
364 new UdpClient (IPEndPoint.MaxPort + 1, AddressFamily.InterNetwork);
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");
375 new UdpClient (IPEndPoint.MinPort - 1, AddressFamily.InterNetwork);
377 } catch (ArgumentOutOfRangeException ex) {
378 // Specified argument was out of the range of valid values
379 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
380 Assert.IsNull (ex.InnerException, "#A3");
381 Assert.IsNotNull (ex.Message, "#A4");
382 Assert.AreEqual ("port", ex.ParamName, "#A5");
386 [Test] // .ctor (String, Int32)
387 [Category ("NotOnMac")]
388 public void Constructor6 ()
394 client = new MyUdpClient ("localhost", IPEndPoint.MinPort);
396 Assert.IsNotNull (s, "#A:Client");
397 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
398 Assert.IsTrue (s.Connected, "#A:Client:Connected");
400 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
402 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
403 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
404 Assert.IsTrue (client.Active, "#A:Active");
406 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
407 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
408 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
409 //Assert.IsFalse (client.MulticastLoopback, "#A:MulticastLoopback");
410 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
412 localEP = s.LocalEndPoint as IPEndPoint;
413 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
414 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#A:Client:LocalEndPoint/Address");
415 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
417 client = new MyUdpClient ("localhost", IPEndPoint.MaxPort);
419 Assert.IsNotNull (s, "#B:Client");
420 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
421 Assert.IsTrue (s.Connected, "#B:Client:Connected");
423 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
425 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
426 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
427 Assert.IsTrue (client.Active, "#B:Active");
429 Assert.IsFalse (client.DontFragment, "#B:DontFragment");
430 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
431 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
432 //Assert.IsFalse (client.MulticastLoopback, "#B:MulticastLoopback");
433 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
435 localEP = s.LocalEndPoint as IPEndPoint;
436 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
437 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#B:Client:LocalEndPoint/Address");
438 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
441 [Test] // .ctor (String, Int32)
442 public void Constructor6_HostName_Null ()
445 new UdpClient ((string) null, int.MaxValue);
447 } catch (ArgumentNullException ex) {
448 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
449 Assert.IsNull (ex.InnerException, "#3");
450 Assert.IsNotNull (ex.Message, "#4");
451 Assert.AreEqual ("hostname", ex.ParamName, "#5");
455 [Test] // .ctor (String, Int32)
456 public void Constructor6_Port_OutOfRange ()
459 new UdpClient ("local", IPEndPoint.MaxPort + 1);
461 } catch (ArgumentOutOfRangeException ex) {
462 // Specified argument was out of the range of valid values
463 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
464 Assert.IsNull (ex.InnerException, "#A3");
465 Assert.IsNotNull (ex.Message, "#A4");
466 Assert.AreEqual ("port", ex.ParamName, "#A5");
470 new UdpClient ("local", IPEndPoint.MinPort - 1);
472 } catch (ArgumentOutOfRangeException ex) {
473 // Specified argument was out of the range of valid values
474 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
475 Assert.IsNull (ex.InnerException, "#A3");
476 Assert.IsNotNull (ex.Message, "#A4");
477 Assert.AreEqual ("port", ex.ParamName, "#A5");
482 [Category ("NotOnMac")]
483 public void UdpClientBroadcastTest ()
485 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
486 byte[] bytes = new byte[] {10, 11, 12, 13};
489 client.Send (bytes, bytes.Length, new IPEndPoint (IPAddress.Broadcast, 1235));
495 [Test] // JoinMulticastGroup (IPAddress)
496 public void JoinMulticastGroup1_IPv4 ()
498 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
500 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
501 client.JoinMulticastGroup (mcast_addr);
505 [Test] // JoinMulticastGroup (IPAddress)
506 [Category ("NotOnMac")]
507 public void JoinMulticastGroup1_IPv6 ()
510 if (!Socket.OSSupportsIPv6)
512 if (!Socket.SupportsIPv6)
514 Assert.Ignore ("IPv6 not enabled.");
516 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
518 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
519 client.JoinMulticastGroup (mcast_addr);
523 [Test] // JoinMulticastGroup (IPAddress)
524 public void JoinMulticastGroup1_MulticastAddr_Null ()
526 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
528 client.JoinMulticastGroup ((IPAddress) null);
531 } catch (ArgumentNullException ex) {
532 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
533 Assert.IsNull (ex.InnerException, "#3");
534 Assert.IsNotNull (ex.Message, "#4");
535 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
538 } catch (NullReferenceException ex) {
539 Assert.AreEqual (typeof (NullReferenceException), ex.GetType (), "#2");
540 Assert.IsNull (ex.InnerException, "#3");
541 Assert.IsNotNull (ex.Message, "#4");
547 [Test] // JoinMulticastGroup (IPAddress)
548 public void JoinMulticastGroup1_Socket_Closed ()
550 IPAddress mcast_addr = null;
552 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
555 client.JoinMulticastGroup (mcast_addr);
557 } catch (ObjectDisposedException ex) {
558 // Cannot access a disposed object
559 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
560 Assert.IsNull (ex.InnerException, "#3");
561 Assert.IsNotNull (ex.Message, "#4");
562 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
566 [Test] // JoinMulticastGroup (IPAddress)
567 [Category ("NotWorking")]
568 public void JoinMulticastGroup1_Socket_NotBound ()
570 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
572 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
574 client.JoinMulticastGroup (mcast_addr);
576 } catch (SocketException ex) {
577 // An invalid argument was supplied
578 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
579 Assert.AreEqual (10022, ex.ErrorCode, "#3");
580 Assert.IsNull (ex.InnerException, "#4");
581 Assert.IsNotNull (ex.Message, "#5");
582 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
584 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
591 [Test] // JoinMulticastGroup (In32, IPAddress)
592 public void JoinMulticastGroup2_IPv4 ()
594 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
596 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
598 client.JoinMulticastGroup (0, mcast_addr);
600 } catch (SocketException ex) {
601 // The attempted operation is not supported for the type of
603 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
604 Assert.AreEqual (10045, ex.ErrorCode, "#3");
605 Assert.IsNull (ex.InnerException, "#4");
606 Assert.IsNotNull (ex.Message, "#5");
607 Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
609 Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
615 [Test] // JoinMulticastGroup (In32, IPAddress)
616 [Category ("NotOnMac")]
617 public void JoinMulticastGroup2_IPv6 ()
620 if (!Socket.OSSupportsIPv6)
622 if (!Socket.SupportsIPv6)
624 Assert.Ignore ("IPv6 not enabled.");
626 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
628 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
629 client.JoinMulticastGroup (0, mcast_addr);
633 [Test] // JoinMulticastGroup (Int32, IPAddress)
634 public void JoinMulticastGroup2_MulticastAddr_Null ()
636 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
638 client.JoinMulticastGroup (0, (IPAddress) null);
640 } catch (ArgumentNullException ex) {
641 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
642 Assert.IsNull (ex.InnerException, "#3");
643 Assert.IsNotNull (ex.Message, "#4");
644 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
649 [Test] // JoinMulticastGroup (Int32, IPAddress)
650 public void JoinMulticastGroup2_Socket_Closed ()
652 IPAddress mcast_addr = null;
654 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234));
657 client.JoinMulticastGroup (0, mcast_addr);
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");
668 [Test] // JoinMulticastGroup (Int32, IPAddress)
669 [Category ("NotWorking")]
670 public void JoinMulticastGroup2_Socket_NotBound ()
672 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
674 UdpClient client = new UdpClient (AddressFamily.InterNetworkV6);
676 client.JoinMulticastGroup (0, mcast_addr);
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");
686 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
693 [Test] // JoinMulticastGroup (IPAddress, Int32)
694 public void JoinMulticastGroup3_IPv4 ()
696 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
698 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
699 client.JoinMulticastGroup (mcast_addr, 0);
702 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
703 client.JoinMulticastGroup (mcast_addr, 255);
707 [Test] // JoinMulticastGroup (IPAddress, Int32)
708 [Category ("NotOnMac")]
709 public void JoinMulticastGroup3_IPv6 ()
712 if (!Socket.OSSupportsIPv6)
714 if (!Socket.SupportsIPv6)
716 Assert.Ignore ("IPv6 not enabled.");
718 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
720 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
721 client.JoinMulticastGroup (mcast_addr, 0);
724 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
725 client.JoinMulticastGroup (mcast_addr, 255);
729 [Test] // JoinMulticastGroup (IPAddress, Int32)
730 public void JoinMulticastGroup3_MulticastAddr_Null ()
732 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
734 client.JoinMulticastGroup ((IPAddress) null, int.MaxValue);
736 } catch (ArgumentNullException ex) {
737 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
738 Assert.IsNull (ex.InnerException, "#3");
739 Assert.IsNotNull (ex.Message, "#4");
740 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
745 [Test] // JoinMulticastGroup (IPAddress, Int32)
746 public void JoinMulticastGroup3_Socket_Closed ()
748 IPAddress mcast_addr = null;
750 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
753 client.JoinMulticastGroup (mcast_addr, 0);
755 } catch (ObjectDisposedException ex) {
756 // Cannot access a disposed object
757 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
758 Assert.IsNull (ex.InnerException, "#3");
759 Assert.IsNotNull (ex.Message, "#4");
760 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
764 [Test] // JoinMulticastGroup (IPAddress, Int32)
765 [Category ("NotWorking")]
766 public void JoinMulticastGroup3_Socket_NotBound ()
768 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
770 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
772 client.JoinMulticastGroup (mcast_addr, 5);
774 } catch (SocketException ex) {
775 // An invalid argument was supplied
776 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
777 Assert.AreEqual (10022, ex.ErrorCode, "#3");
778 Assert.IsNull (ex.InnerException, "#4");
779 Assert.IsNotNull (ex.Message, "#5");
780 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
782 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
790 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
791 public void JoinMulticastGroup4_IPv4 ()
793 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
794 IPAddress local_addr = IPAddress.Any;
796 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
797 client.JoinMulticastGroup (mcast_addr, local_addr);
801 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
802 public void JoinMulticastGroup4_IPv6 ()
804 if (!Socket.OSSupportsIPv6)
805 Assert.Ignore ("IPv6 not enabled.");
807 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
808 IPAddress local_addr = IPAddress.IPv6Any;
810 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
812 client.JoinMulticastGroup (mcast_addr, local_addr);
814 } catch (SocketException ex) {
815 // The attempted operation is not supported for the type of
817 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
818 Assert.AreEqual (10045, ex.ErrorCode, "#3");
819 Assert.IsNull (ex.InnerException, "#4");
820 Assert.IsNotNull (ex.Message, "#5");
821 Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
822 Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
827 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
828 public void JoinMulticastGroup4_LocalAddress_Null ()
830 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
832 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
834 client.JoinMulticastGroup (mcast_addr, (IPAddress) null);
836 } catch (ArgumentNullException ex) {
837 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
838 Assert.IsNull (ex.InnerException, "#3");
839 Assert.IsNotNull (ex.Message, "#4");
840 Assert.AreEqual ("mcint", ex.ParamName, "#5");
845 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
846 public void JoinMulticastGroup4_MulticastAddr_Null ()
848 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
850 client.JoinMulticastGroup ((IPAddress) null, IPAddress.Loopback);
852 } catch (ArgumentNullException ex) {
853 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
854 Assert.IsNull (ex.InnerException, "#3");
855 Assert.IsNotNull (ex.Message, "#4");
856 Assert.AreEqual ("group", ex.ParamName, "#5");
861 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
862 public void JoinMulticastGroup4_Socket_Closed ()
864 IPAddress mcast_addr = null;
865 IPAddress local_addr = null;
867 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
870 client.JoinMulticastGroup (mcast_addr, local_addr);
872 } catch (ObjectDisposedException ex) {
873 // Cannot access a disposed object
874 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
875 Assert.IsNull (ex.InnerException, "#3");
876 Assert.IsNotNull (ex.Message, "#4");
877 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
881 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
882 [Category ("NotWorking")]
883 public void JoinMulticastGroup4_Socket_NotBound ()
885 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
886 IPAddress local_addr = Dns.GetHostEntry (string.Empty).AddressList [0];
888 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
890 client.JoinMulticastGroup (mcast_addr, local_addr);
892 } catch (SocketException ex) {
893 // An invalid argument was supplied
894 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
895 Assert.AreEqual (10022, ex.ErrorCode, "#3");
896 Assert.IsNull (ex.InnerException, "#4");
897 Assert.IsNotNull (ex.Message, "#5");
898 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
899 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
906 public void CloseInReceive ()
908 UdpClient client = new UdpClient (50000);
909 new Thread(delegate() {
914 bool got_exc = false;
915 IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
917 client.Receive(ref ep);
918 } catch (SocketException) {
923 Assert.IsTrue (got_exc);
926 // Test for bug 324033
928 public void JoinMulticastGroupWithLocal ()
930 UdpClient client = new UdpClient (9001);
931 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.24");
932 IPAddress local_addr = IPAddress.Any;
935 client.JoinMulticastGroup (mcast_addr, local_addr);
942 [ExpectedException (typeof(ArgumentNullException))]
943 public void BeginSendNull ()
945 UdpClient client = new UdpClient ();
946 client.BeginSend (null, 0, null, null);
950 static bool BSSent = false;
952 static ManualResetEvent BSCalledBack = new ManualResetEvent (false);
954 private static void BSCallback (IAsyncResult asyncResult)
956 UdpClient client = (UdpClient)asyncResult.AsyncState;
958 BSBytes = client.EndSend (asyncResult);
965 [Category ("NotOnMac")]
966 public void BeginSend ()
968 UdpClient client = new UdpClient ();
969 byte[] bytes = new byte[] {10, 11, 12, 13};
972 client.BeginSend (bytes, bytes.Length, new AsyncCallback (BSCallback), client);
973 Assert.Fail ("BeginSend #1");
974 } catch (SocketException ex) {
975 Assert.AreEqual (10057, ex.ErrorCode,
980 client.BeginSend (bytes, bytes.Length, null, new AsyncCallback (BSCallback), client);
981 Assert.Fail ("BeginSend #3");
982 } catch (SocketException ex) {
983 Assert.AreEqual (10057, ex.ErrorCode,
987 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1236);
989 BSCalledBack.Reset ();
991 client.BeginSend (bytes, bytes.Length, ep,
992 new AsyncCallback (BSCallback),
994 if (BSCalledBack.WaitOne (2000, false) == false) {
995 Assert.Fail ("BeginSend wait timed out");
998 Assert.AreEqual (true, BSSent, "BeginSend #5");
999 Assert.AreEqual (4, BSBytes, "BeginSend #6");
1004 static bool BRReceived = false;
1005 static byte[] BRBytes;
1006 static IPEndPoint BRFrom;
1007 static ManualResetEvent BRCalledBack = new ManualResetEvent (false);
1009 private static void BRCallback (IAsyncResult asyncResult)
1011 UdpClient client = (UdpClient)asyncResult.AsyncState;
1013 BRBytes = client.EndReceive (asyncResult, ref BRFrom);
1016 BRCalledBack.Set ();
1020 [Category ("NotOnMac")]
1021 public void BeginReceive ()
1023 UdpClient client = new UdpClient (1237);
1025 BRCalledBack.Reset ();
1027 client.BeginReceive (BRCallback, client);
1029 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1237);
1030 byte[] send_bytes = new byte[] {10, 11, 12, 13};
1031 client.Send (send_bytes, send_bytes.Length, ep);
1033 if (BRCalledBack.WaitOne (2000, false) == false) {
1034 Assert.Fail ("BeginReceive wait timed out");
1037 Assert.AreEqual (true, BRReceived, "BeginReceive #1");
1038 Assert.AreEqual (4, BRBytes.Length, "BeginReceive #2");
1039 Assert.AreEqual (ep. Port, BRFrom.Port, "BeginReceive #3");
1040 Assert.AreEqual (ep.Address, BRFrom.Address, "BeginReceive #4");
1046 [Category ("NotOnMac")]
1047 public void Available ()
1049 UdpClient client = new UdpClient (1238);
1050 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1238);
1051 byte[] bytes = new byte[] {10, 11, 12, 13};
1053 client.Send (bytes, bytes.Length, ep);
1054 int avail = client.Available;
1056 Assert.AreEqual (bytes.Length, avail, "Available #1");
1062 [Category ("NotWorking")] // Using PMTU settings workaround on Linux, default true
1063 public void DontFragmentDefault ()
1065 UdpClient client = new UdpClient ();
1067 /* Ignore the docs, testing shows the default
1068 * here is in fact false
1070 Assert.AreEqual (false, client.DontFragment, "DontFragmentDefault");
1076 public void EnableBroadcastDefault ()
1078 UdpClient client = new UdpClient ();
1080 Assert.AreEqual (false, client.EnableBroadcast, "EnableBroadcastDefault");
1085 /* Can't test the default for ExclusiveAddressUse as
1086 * it's different on different versions and service
1090 [Category ("NotWorking")] // Not supported on Linux
1091 public void ExclusiveAddressUseUnbound ()
1093 UdpClient client = new UdpClient ();
1095 client.ExclusiveAddressUse = true;
1097 Assert.AreEqual (true, client.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
1103 [ExpectedException (typeof(InvalidOperationException))]
1104 [Category ("NotWorking")] // Not supported on Linux
1105 public void ExclusiveAddressUseBound ()
1107 UdpClient client = new UdpClient (1239);
1109 client.ExclusiveAddressUse = true;
1115 public void MulticastLoopbackDefault ()
1117 UdpClient client = new UdpClient ();
1119 Assert.AreEqual (true, client.MulticastLoopback, "MulticastLoopbackDefault");
1124 /* No test for Ttl default as it is platform dependent */
1127 class MyUdpClient : UdpClient
1129 public MyUdpClient ()
1133 public MyUdpClient (AddressFamily family)
1138 public MyUdpClient (Int32 port)
1144 public MyUdpClient (IPEndPoint localEP)
1149 public MyUdpClient (int port, AddressFamily family)
1150 : base (port, family)
1154 public MyUdpClient (string hostname, int port)
1155 : base (hostname, port)
1159 public new bool Active {
1160 get { return base.Active; }
1161 set { base.Active = value; }
1165 public new Socket Client {
1166 get { return base.Client; }
1167 set { base.Client = value; }