using System.Net;
using System.Net.Sockets;
using System.Threading;
+using System.Threading.Tasks;
using NUnit.Framework;
[TestFixture]
public class UdpClientTest {
[Test] // .ctor ()
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void Constructor1 ()
{
MyUdpClient client;
}
[Test] // .ctor (AddressFamily)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void Constructor2 ()
{
MyUdpClient client;
}
[Test] // .ctor (AddressFamily)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void Constructor2_Family_Invalid ()
{
try {
}
[Test] // .ctor (Int32)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void Constructor3 ()
{
Socket s;
}
[Test] // .ctor (Int32)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void Constructor3_Port_OutOfRange ()
{
try {
}
[Test] // .ctor (IPEndPoint)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void Constructor4 ()
{
Socket s;
}
[Test] // .ctor (IPEndPoint)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void Constructor4_LocalEP_Null ()
{
try {
}
[Test] // .ctor (Int32, AddressFamily)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void Constructor5 ()
{
Socket s;
}
[Test] // .ctor (Int32, AddressFamily)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void Constructor5_Family_Invalid ()
{
try {
}
[Test] // .ctor (Int32, AddressFamily)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void Constructor5_Port_OutOfRange ()
{
try {
}
[Test] // .ctor (String, Int32)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void Constructor6 ()
{
Socket s;
}
[Test] // .ctor (String, Int32)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void Constructor6_HostName_Null ()
{
try {
}
[Test] // .ctor (String, Int32)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void Constructor6_Port_OutOfRange ()
{
try {
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void UdpClientBroadcastTest ()
{
UdpClient client = new UdpClient ();
}
[Test] // JoinMulticastGroup (IPAddress)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void JoinMulticastGroup1_IPv4 ()
{
IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
}
[Test] // JoinMulticastGroup (IPAddress)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void JoinMulticastGroup1_IPv6 ()
{
if (!Socket.OSSupportsIPv6)
}
[Test] // JoinMulticastGroup (IPAddress)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void JoinMulticastGroup1_MulticastAddr_Null ()
{
using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
}
[Test] // JoinMulticastGroup (IPAddress)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void JoinMulticastGroup1_Socket_Closed ()
{
IPAddress mcast_addr = null;
}
[Test] // JoinMulticastGroup (In32, IPAddress)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void JoinMulticastGroup2_IPv4 ()
{
IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
}
[Test] // JoinMulticastGroup (In32, IPAddress)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void JoinMulticastGroup2_IPv6 ()
{
if (!Socket.OSSupportsIPv6)
}
[Test] // JoinMulticastGroup (Int32, IPAddress)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void JoinMulticastGroup2_MulticastAddr_Null ()
{
using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
}
[Test] // JoinMulticastGroup (Int32, IPAddress)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void JoinMulticastGroup2_Socket_Closed ()
{
if (!Socket.OSSupportsIPv6)
}
[Test] // JoinMulticastGroup (IPAddress, Int32)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void JoinMulticastGroup3_IPv4 ()
{
IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
}
[Test] // JoinMulticastGroup (IPAddress, Int32)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void JoinMulticastGroup3_IPv6 ()
{
if (!Socket.OSSupportsIPv6)
}
[Test] // JoinMulticastGroup (IPAddress, Int32)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void JoinMulticastGroup3_MulticastAddr_Null ()
{
using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
}
[Test] // JoinMulticastGroup (IPAddress, Int32)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void JoinMulticastGroup3_Socket_Closed ()
{
IPAddress mcast_addr = null;
}
[Test] // JoinMulticastGroup (IPAddress, IPAddress)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void JoinMulticastGroup4_IPv4 ()
{
IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
}
[Test] // JoinMulticastGroup (IPAddress, IPAddress)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void JoinMulticastGroup4_IPv6 ()
{
if (!Socket.OSSupportsIPv6)
}
[Test] // JoinMulticastGroup (IPAddress, IPAddress)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void JoinMulticastGroup4_LocalAddress_Null ()
{
IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
}
[Test] // JoinMulticastGroup (IPAddress, IPAddress)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void JoinMulticastGroup4_MulticastAddr_Null ()
{
using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
}
[Test] // JoinMulticastGroup (IPAddress, IPAddress)
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void JoinMulticastGroup4_Socket_Closed ()
{
IPAddress mcast_addr = null;
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void CloseInReceive ()
{
UdpClient client = null;
var rnd = new Random ();
- for (int i = 0; i < 5; i++) {
+ for (int i = 0, max = 5; i < max; i++) {
int port = rnd.Next (1025, 65534);
try {
client = new UdpClient (port);
break;
} catch (Exception) {
- if (i == 5)
+ if (i == max - 1)
throw;
}
}
- new Thread(delegate() {
- Thread.Sleep(2000);
- client.Close();
- }).Start();
-
+ ManualResetEvent ready = new ManualResetEvent (false);
bool got_exc = false;
- IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
- try {
- client.Receive(ref ep);
- } catch (SocketException) {
- got_exc = true;
- } finally {
- client.Close ();
- }
+
+ Task receive_task = Task.Factory.StartNew (() => {
+ IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
+ try {
+ ready.Set ();
+ client.Receive(ref ep);
+ } catch (SocketException) {
+ got_exc = true;
+ } finally {
+ client.Close ();
+ }
+ });
+
+ ready.WaitOne (2000);
+ Thread.Sleep (20);
+ client.Close();
+
+ Assert.IsTrue (receive_task.Wait (1000));
Assert.IsTrue (got_exc);
}
// Test for bug 324033
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void JoinMulticastGroupWithLocal ()
{
UdpClient client = new UdpClient (9001);
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#else
[ExpectedException (typeof(ArgumentNullException))]
+#endif
public void BeginSendNull ()
{
UdpClient client = new UdpClient ();
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void BeginSend ()
{
UdpClient client = new UdpClient ();
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void BeginReceive ()
{
UdpClient client = new UdpClient (1237);
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void Available ()
{
using (UdpClient client = new UdpClient (1238)) {
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void EnableBroadcastDefault ()
{
UdpClient client = new UdpClient ();
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void MulticastLoopbackDefault ()
{
UdpClient client = new UdpClient ();
client.Close ();
}
+
+ [Test] // #6057
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+ public void ReceiveIPv6 ()
+ {
+ if (!Socket.OSSupportsIPv6)
+ Assert.Ignore ("IPv6 not enabled.");
+
+ int PORT = 9997;
+ using(var udpClient = new UdpClient (PORT, AddressFamily.InterNetworkV6))
+ using(var udpClient2 = new UdpClient (PORT+1, AddressFamily.InterNetworkV6))
+ {
+ var dataSent = new byte [] {1,2,3};
+ udpClient2.SendAsync (dataSent, dataSent.Length, "::1", PORT);
+
+ IPEndPoint endPoint = new IPEndPoint (IPAddress.IPv6Any, 0);
+ var data = udpClient.Receive (ref endPoint);
+
+ Assert.AreEqual (dataSent.Length, data.Length);
+ }
+ }
/* No test for Ttl default as it is platform dependent */