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 public void Constructor5 ()
271 client = new MyUdpClient (IPEndPoint.MinPort, AddressFamily.InterNetwork);
273 Assert.IsNotNull (s, "#A:Client");
274 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
275 Assert.IsFalse (s.Connected, "#A:Client:Connected");
277 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
279 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
280 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
281 Assert.IsFalse (client.Active, "#A:Active");
283 //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
284 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
285 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
286 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
287 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
289 localEP = s.LocalEndPoint as IPEndPoint;
290 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
291 Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
292 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
294 client = new MyUdpClient (IPEndPoint.MaxPort, AddressFamily.InterNetworkV6);
296 Assert.IsNotNull (s, "#B:Client");
297 Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
298 Assert.IsFalse (s.Connected, "#B:Client:Connected");
300 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
302 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
303 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
304 Assert.IsFalse (client.Active, "#B:Active");
306 //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
307 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
308 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
309 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
310 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
312 localEP = s.LocalEndPoint as IPEndPoint;
313 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
314 Assert.AreEqual (IPAddress.IPv6Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
315 Assert.AreEqual (AddressFamily.InterNetworkV6, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
316 Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
319 [Test] // .ctor (Int32, AddressFamily)
320 public void Constructor5_Family_Invalid ()
323 new UdpClient (80, AddressFamily.NetBios);
325 } catch (ArgumentException ex) {
327 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
328 Assert.IsNull (ex.InnerException, "#A3");
330 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
332 Assert.IsNotNull (ex.Message, "#A4");
333 Assert.AreEqual ("family", ex.ParamName, "#A5");
335 Assert.AreEqual ("family", ex.Message, "#A4");
336 Assert.IsNull (ex.ParamName, "#A5");
341 new UdpClient (80, (AddressFamily) 666);
343 } catch (ArgumentException ex) {
345 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
346 Assert.IsNull (ex.InnerException, "#B3");
348 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
350 Assert.IsNotNull (ex.Message, "#B4");
351 Assert.AreEqual ("family", ex.ParamName, "#B5");
353 Assert.AreEqual ("family", ex.Message, "#B4");
354 Assert.IsNull (ex.ParamName, "#B5");
359 [Test] // .ctor (Int32, AddressFamily)
360 public void Constructor5_Port_OutOfRange ()
363 new UdpClient (IPEndPoint.MaxPort + 1, AddressFamily.InterNetwork);
365 } catch (ArgumentOutOfRangeException ex) {
366 // Specified argument was out of the range of valid values
367 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
368 Assert.IsNull (ex.InnerException, "#A3");
369 Assert.IsNotNull (ex.Message, "#A4");
370 Assert.AreEqual ("port", ex.ParamName, "#A5");
374 new UdpClient (IPEndPoint.MinPort - 1, AddressFamily.InterNetwork);
376 } catch (ArgumentOutOfRangeException ex) {
377 // Specified argument was out of the range of valid values
378 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
379 Assert.IsNull (ex.InnerException, "#A3");
380 Assert.IsNotNull (ex.Message, "#A4");
381 Assert.AreEqual ("port", ex.ParamName, "#A5");
385 [Test] // .ctor (String, Int32)
386 public void Constructor6 ()
392 client = new MyUdpClient ("localhost", IPEndPoint.MinPort);
394 Assert.IsNotNull (s, "#A:Client");
395 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
396 Assert.IsTrue (s.Connected, "#A:Client:Connected");
398 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
400 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
401 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
402 Assert.IsTrue (client.Active, "#A:Active");
404 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
405 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
406 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
407 //Assert.IsFalse (client.MulticastLoopback, "#A:MulticastLoopback");
408 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
410 localEP = s.LocalEndPoint as IPEndPoint;
411 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
412 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#A:Client:LocalEndPoint/Address");
413 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
415 client = new MyUdpClient ("localhost", IPEndPoint.MaxPort);
417 Assert.IsNotNull (s, "#B:Client");
418 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
419 Assert.IsTrue (s.Connected, "#B:Client:Connected");
421 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
423 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
424 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
425 Assert.IsTrue (client.Active, "#B:Active");
427 Assert.IsFalse (client.DontFragment, "#B:DontFragment");
428 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
429 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
430 //Assert.IsFalse (client.MulticastLoopback, "#B:MulticastLoopback");
431 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
433 localEP = s.LocalEndPoint as IPEndPoint;
434 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
435 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#B:Client:LocalEndPoint/Address");
436 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
439 [Test] // .ctor (String, Int32)
440 public void Constructor6_HostName_Null ()
443 new UdpClient ((string) null, int.MaxValue);
445 } catch (ArgumentNullException ex) {
446 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
447 Assert.IsNull (ex.InnerException, "#3");
448 Assert.IsNotNull (ex.Message, "#4");
449 Assert.AreEqual ("hostname", ex.ParamName, "#5");
453 [Test] // .ctor (String, Int32)
454 public void Constructor6_Port_OutOfRange ()
457 new UdpClient ("local", IPEndPoint.MaxPort + 1);
459 } catch (ArgumentOutOfRangeException ex) {
460 // Specified argument was out of the range of valid values
461 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
462 Assert.IsNull (ex.InnerException, "#A3");
463 Assert.IsNotNull (ex.Message, "#A4");
464 Assert.AreEqual ("port", ex.ParamName, "#A5");
468 new UdpClient ("local", IPEndPoint.MinPort - 1);
470 } catch (ArgumentOutOfRangeException ex) {
471 // Specified argument was out of the range of valid values
472 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
473 Assert.IsNull (ex.InnerException, "#A3");
474 Assert.IsNotNull (ex.Message, "#A4");
475 Assert.AreEqual ("port", ex.ParamName, "#A5");
480 public void UdpClientBroadcastTest ()
482 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
483 byte[] bytes = new byte[] {10, 11, 12, 13};
486 client.Send (bytes, bytes.Length, new IPEndPoint (IPAddress.Broadcast, 1235));
492 [Test] // JoinMulticastGroup (IPAddress)
493 public void JoinMulticastGroup1_IPv4 ()
495 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
497 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
498 client.JoinMulticastGroup (mcast_addr);
502 [Test] // JoinMulticastGroup (IPAddress)
503 public void JoinMulticastGroup1_IPv6 ()
506 if (!Socket.OSSupportsIPv6)
508 if (!Socket.SupportsIPv6)
510 Assert.Ignore ("IPv6 not enabled.");
512 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
514 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
515 client.JoinMulticastGroup (mcast_addr);
519 [Test] // JoinMulticastGroup (IPAddress)
520 public void JoinMulticastGroup1_MulticastAddr_Null ()
522 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
524 client.JoinMulticastGroup ((IPAddress) null);
527 } catch (ArgumentNullException ex) {
528 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
529 Assert.IsNull (ex.InnerException, "#3");
530 Assert.IsNotNull (ex.Message, "#4");
531 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
534 } catch (NullReferenceException ex) {
535 Assert.AreEqual (typeof (NullReferenceException), ex.GetType (), "#2");
536 Assert.IsNull (ex.InnerException, "#3");
537 Assert.IsNotNull (ex.Message, "#4");
543 [Test] // JoinMulticastGroup (IPAddress)
544 public void JoinMulticastGroup1_Socket_Closed ()
546 IPAddress mcast_addr = null;
548 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
551 client.JoinMulticastGroup (mcast_addr);
553 } catch (ObjectDisposedException ex) {
554 // Cannot access a disposed object
555 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
556 Assert.IsNull (ex.InnerException, "#3");
557 Assert.IsNotNull (ex.Message, "#4");
558 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
562 [Test] // JoinMulticastGroup (IPAddress)
563 [Category ("NotWorking")]
564 public void JoinMulticastGroup1_Socket_NotBound ()
566 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
568 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
570 client.JoinMulticastGroup (mcast_addr);
572 } catch (SocketException ex) {
573 // An invalid argument was supplied
574 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
575 Assert.AreEqual (10022, ex.ErrorCode, "#3");
576 Assert.IsNull (ex.InnerException, "#4");
577 Assert.IsNotNull (ex.Message, "#5");
578 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
580 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
587 [Test] // JoinMulticastGroup (In32, IPAddress)
588 public void JoinMulticastGroup2_IPv4 ()
590 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
592 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
594 client.JoinMulticastGroup (0, mcast_addr);
596 } catch (SocketException ex) {
597 // The attempted operation is not supported for the type of
599 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
600 Assert.AreEqual (10045, ex.ErrorCode, "#3");
601 Assert.IsNull (ex.InnerException, "#4");
602 Assert.IsNotNull (ex.Message, "#5");
603 Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
605 Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
611 [Test] // JoinMulticastGroup (In32, IPAddress)
612 public void JoinMulticastGroup2_IPv6 ()
615 if (!Socket.OSSupportsIPv6)
617 if (!Socket.SupportsIPv6)
619 Assert.Ignore ("IPv6 not enabled.");
621 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
623 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
624 client.JoinMulticastGroup (0, mcast_addr);
628 [Test] // JoinMulticastGroup (Int32, IPAddress)
629 public void JoinMulticastGroup2_MulticastAddr_Null ()
631 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
633 client.JoinMulticastGroup (0, (IPAddress) null);
635 } catch (ArgumentNullException ex) {
636 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
637 Assert.IsNull (ex.InnerException, "#3");
638 Assert.IsNotNull (ex.Message, "#4");
639 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
644 [Test] // JoinMulticastGroup (Int32, IPAddress)
645 public void JoinMulticastGroup2_Socket_Closed ()
647 IPAddress mcast_addr = null;
649 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234));
652 client.JoinMulticastGroup (0, mcast_addr);
654 } catch (ObjectDisposedException ex) {
655 // Cannot access a disposed object
656 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
657 Assert.IsNull (ex.InnerException, "#3");
658 Assert.IsNotNull (ex.Message, "#4");
659 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
663 [Test] // JoinMulticastGroup (Int32, IPAddress)
664 [Category ("NotWorking")]
665 public void JoinMulticastGroup2_Socket_NotBound ()
667 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
669 UdpClient client = new UdpClient (AddressFamily.InterNetworkV6);
671 client.JoinMulticastGroup (0, mcast_addr);
673 } catch (SocketException ex) {
674 // An invalid argument was supplied
675 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
676 Assert.AreEqual (10022, ex.ErrorCode, "#3");
677 Assert.IsNull (ex.InnerException, "#4");
678 Assert.IsNotNull (ex.Message, "#5");
679 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
681 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
688 [Test] // JoinMulticastGroup (IPAddress, Int32)
689 public void JoinMulticastGroup3_IPv4 ()
691 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
693 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
694 client.JoinMulticastGroup (mcast_addr, 0);
697 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
698 client.JoinMulticastGroup (mcast_addr, 255);
702 [Test] // JoinMulticastGroup (IPAddress, Int32)
703 public void JoinMulticastGroup3_IPv6 ()
706 if (!Socket.OSSupportsIPv6)
708 if (!Socket.SupportsIPv6)
710 Assert.Ignore ("IPv6 not enabled.");
712 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
714 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
715 client.JoinMulticastGroup (mcast_addr, 0);
718 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
719 client.JoinMulticastGroup (mcast_addr, 255);
723 [Test] // JoinMulticastGroup (IPAddress, Int32)
724 public void JoinMulticastGroup3_MulticastAddr_Null ()
726 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
728 client.JoinMulticastGroup ((IPAddress) null, int.MaxValue);
730 } catch (ArgumentNullException ex) {
731 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
732 Assert.IsNull (ex.InnerException, "#3");
733 Assert.IsNotNull (ex.Message, "#4");
734 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
739 [Test] // JoinMulticastGroup (IPAddress, Int32)
740 public void JoinMulticastGroup3_Socket_Closed ()
742 IPAddress mcast_addr = null;
744 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
747 client.JoinMulticastGroup (mcast_addr, 0);
749 } catch (ObjectDisposedException ex) {
750 // Cannot access a disposed object
751 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
752 Assert.IsNull (ex.InnerException, "#3");
753 Assert.IsNotNull (ex.Message, "#4");
754 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
758 [Test] // JoinMulticastGroup (IPAddress, Int32)
759 [Category ("NotWorking")]
760 public void JoinMulticastGroup3_Socket_NotBound ()
762 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
764 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
766 client.JoinMulticastGroup (mcast_addr, 5);
768 } catch (SocketException ex) {
769 // An invalid argument was supplied
770 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
771 Assert.AreEqual (10022, ex.ErrorCode, "#3");
772 Assert.IsNull (ex.InnerException, "#4");
773 Assert.IsNotNull (ex.Message, "#5");
774 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
776 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
784 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
785 public void JoinMulticastGroup4_IPv4 ()
787 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
788 IPAddress local_addr = IPAddress.Any;
790 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
791 client.JoinMulticastGroup (mcast_addr, local_addr);
795 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
796 public void JoinMulticastGroup4_IPv6 ()
798 if (!Socket.OSSupportsIPv6)
799 Assert.Ignore ("IPv6 not enabled.");
801 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
802 IPAddress local_addr = IPAddress.IPv6Any;
804 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
806 client.JoinMulticastGroup (mcast_addr, local_addr);
808 } catch (SocketException ex) {
809 // The attempted operation is not supported for the type of
811 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
812 Assert.AreEqual (10045, ex.ErrorCode, "#3");
813 Assert.IsNull (ex.InnerException, "#4");
814 Assert.IsNotNull (ex.Message, "#5");
815 Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
816 Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
821 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
822 public void JoinMulticastGroup4_LocalAddress_Null ()
824 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
826 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
828 client.JoinMulticastGroup (mcast_addr, (IPAddress) null);
830 } catch (ArgumentNullException ex) {
831 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
832 Assert.IsNull (ex.InnerException, "#3");
833 Assert.IsNotNull (ex.Message, "#4");
834 Assert.AreEqual ("mcint", ex.ParamName, "#5");
839 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
840 public void JoinMulticastGroup4_MulticastAddr_Null ()
842 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
844 client.JoinMulticastGroup ((IPAddress) null, IPAddress.Loopback);
846 } catch (ArgumentNullException ex) {
847 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
848 Assert.IsNull (ex.InnerException, "#3");
849 Assert.IsNotNull (ex.Message, "#4");
850 Assert.AreEqual ("group", ex.ParamName, "#5");
855 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
856 public void JoinMulticastGroup4_Socket_Closed ()
858 IPAddress mcast_addr = null;
859 IPAddress local_addr = null;
861 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
864 client.JoinMulticastGroup (mcast_addr, local_addr);
866 } catch (ObjectDisposedException ex) {
867 // Cannot access a disposed object
868 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
869 Assert.IsNull (ex.InnerException, "#3");
870 Assert.IsNotNull (ex.Message, "#4");
871 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
875 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
876 [Category ("NotWorking")]
877 public void JoinMulticastGroup4_Socket_NotBound ()
879 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
880 IPAddress local_addr = Dns.GetHostEntry (string.Empty).AddressList [0];
882 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
884 client.JoinMulticastGroup (mcast_addr, local_addr);
886 } catch (SocketException ex) {
887 // An invalid argument was supplied
888 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
889 Assert.AreEqual (10022, ex.ErrorCode, "#3");
890 Assert.IsNull (ex.InnerException, "#4");
891 Assert.IsNotNull (ex.Message, "#5");
892 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
893 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
900 public void CloseInReceive ()
902 UdpClient client = new UdpClient (50000);
903 new Thread(delegate() {
908 bool got_exc = false;
909 IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
911 client.Receive(ref ep);
912 } catch (SocketException) {
917 Assert.IsTrue (got_exc);
920 // Test for bug 324033
922 public void JoinMulticastGroupWithLocal ()
924 UdpClient client = new UdpClient (9001);
925 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.24");
926 IPAddress local_addr = IPAddress.Any;
929 client.JoinMulticastGroup (mcast_addr, local_addr);
936 [ExpectedException (typeof(ArgumentNullException))]
937 public void BeginSendNull ()
939 UdpClient client = new UdpClient ();
940 client.BeginSend (null, 0, null, null);
944 static bool BSSent = false;
946 static ManualResetEvent BSCalledBack = new ManualResetEvent (false);
948 private static void BSCallback (IAsyncResult asyncResult)
950 UdpClient client = (UdpClient)asyncResult.AsyncState;
952 BSBytes = client.EndSend (asyncResult);
959 public void BeginSend ()
961 UdpClient client = new UdpClient ();
962 byte[] bytes = new byte[] {10, 11, 12, 13};
965 client.BeginSend (bytes, bytes.Length, new AsyncCallback (BSCallback), client);
966 Assert.Fail ("BeginSend #1");
967 } catch (SocketException ex) {
968 Assert.AreEqual (10057, ex.ErrorCode,
973 client.BeginSend (bytes, bytes.Length, null, new AsyncCallback (BSCallback), client);
974 Assert.Fail ("BeginSend #3");
975 } catch (SocketException ex) {
976 Assert.AreEqual (10057, ex.ErrorCode,
980 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1236);
982 BSCalledBack.Reset ();
984 client.BeginSend (bytes, bytes.Length, ep,
985 new AsyncCallback (BSCallback),
987 if (BSCalledBack.WaitOne (2000, false) == false) {
988 Assert.Fail ("BeginSend wait timed out");
991 Assert.AreEqual (true, BSSent, "BeginSend #5");
992 Assert.AreEqual (4, BSBytes, "BeginSend #6");
997 static bool BRReceived = false;
998 static byte[] BRBytes;
999 static IPEndPoint BRFrom;
1000 static ManualResetEvent BRCalledBack = new ManualResetEvent (false);
1002 private static void BRCallback (IAsyncResult asyncResult)
1004 UdpClient client = (UdpClient)asyncResult.AsyncState;
1006 BRBytes = client.EndReceive (asyncResult, ref BRFrom);
1009 BRCalledBack.Set ();
1013 public void BeginReceive ()
1015 UdpClient client = new UdpClient (1237);
1017 BRCalledBack.Reset ();
1019 client.BeginReceive (BRCallback, client);
1021 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1237);
1022 byte[] send_bytes = new byte[] {10, 11, 12, 13};
1023 client.Send (send_bytes, send_bytes.Length, ep);
1025 if (BRCalledBack.WaitOne (2000, false) == false) {
1026 Assert.Fail ("BeginReceive wait timed out");
1029 Assert.AreEqual (true, BRReceived, "BeginReceive #1");
1030 Assert.AreEqual (4, BRBytes.Length, "BeginReceive #2");
1031 Assert.AreEqual (ep. Port, BRFrom.Port, "BeginReceive #3");
1032 Assert.AreEqual (ep.Address, BRFrom.Address, "BeginReceive #4");
1038 public void Available ()
1040 UdpClient client = new UdpClient (1238);
1041 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1238);
1042 byte[] bytes = new byte[] {10, 11, 12, 13};
1044 client.Send (bytes, bytes.Length, ep);
1045 int avail = client.Available;
1047 Assert.AreEqual (bytes.Length, avail, "Available #1");
1053 [Category ("NotWorking")] // Using PMTU settings workaround on Linux, default true
1054 public void DontFragmentDefault ()
1056 UdpClient client = new UdpClient ();
1058 /* Ignore the docs, testing shows the default
1059 * here is in fact false
1061 Assert.AreEqual (false, client.DontFragment, "DontFragmentDefault");
1067 public void EnableBroadcastDefault ()
1069 UdpClient client = new UdpClient ();
1071 Assert.AreEqual (false, client.EnableBroadcast, "EnableBroadcastDefault");
1076 /* Can't test the default for ExclusiveAddressUse as
1077 * it's different on different versions and service
1081 [Category ("NotWorking")] // Not supported on Linux
1082 public void ExclusiveAddressUseUnbound ()
1084 UdpClient client = new UdpClient ();
1086 client.ExclusiveAddressUse = true;
1088 Assert.AreEqual (true, client.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
1094 [ExpectedException (typeof(InvalidOperationException))]
1095 [Category ("NotWorking")] // Not supported on Linux
1096 public void ExclusiveAddressUseBound ()
1098 UdpClient client = new UdpClient (1239);
1100 client.ExclusiveAddressUse = true;
1106 public void MulticastLoopbackDefault ()
1108 UdpClient client = new UdpClient ();
1110 Assert.AreEqual (true, client.MulticastLoopback, "MulticastLoopbackDefault");
1115 /* No test for Ttl default as it is platform dependent */
1118 class MyUdpClient : UdpClient
1120 public MyUdpClient ()
1124 public MyUdpClient (AddressFamily family)
1129 public MyUdpClient (Int32 port)
1135 public MyUdpClient (IPEndPoint localEP)
1140 public MyUdpClient (int port, AddressFamily family)
1141 : base (port, family)
1145 public MyUdpClient (string hostname, int port)
1146 : base (hostname, port)
1150 public new bool Active {
1151 get { return base.Active; }
1152 set { base.Active = value; }
1156 public new Socket Client {
1157 get { return base.Client; }
1158 set { base.Client = value; }