[bcl] Fix a few hardcoded socket ports in tests (#5471)
[mono.git] / mcs / class / System / Test / System.Net.Sockets / TcpClientTest.cs
index d4769eb811bc6eeb297046a2b44df0c597c9d36a..d827ae4de367cc074666e49c3a182b7ad4148c52 100644 (file)
@@ -29,18 +29,22 @@ namespace MonoTests.System.Net.Sockets
                /// (from System.Net.Sockets)
                /// </summary>
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void TcpClient()
                {
                        // set up a listening Socket
                        Socket lSock = new Socket(AddressFamily.InterNetwork,
                                SocketType.Stream, ProtocolType.Tcp);
                        
-                       lSock.Bind(new IPEndPoint(IPAddress.Any, 8765));
+                       var port = NetworkHelpers.FindFreePort ();
+                       lSock.Bind(new IPEndPoint(IPAddress.Any, port));
                        lSock.Listen(-1);
 
 
                        // connect to it with a TcpClient
-                       TcpClient outClient = new TcpClient("localhost", 8765);
+                       TcpClient outClient = new TcpClient("localhost", port);
                        Socket inSock = lSock.Accept();
 
                        
@@ -75,13 +79,15 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test] // bug #81105
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void CloseTest ()
                {
-                       IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8765);
-                       using (SocketResponder sr = new SocketResponder (localEP, new SocketRequestHandler (CloseRequestHandler))) {
-                               sr.Start ();
-
-                               TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), 8765);
+                       var port = NetworkHelpers.FindFreePort ();
+                       IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, port);
+                       using (SocketResponder sr = new SocketResponder (localEP, s => CloseRequestHandler (s))) {
+                               TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), port);
                                NetworkStream ns = tcpClient.GetStream ();
                                Assert.IsNotNull (ns, "#A1");
                                Assert.AreEqual (0, tcpClient.Available, "#A2");
@@ -105,10 +111,8 @@ namespace MonoTests.System.Net.Sockets
                                */
                        }
 
-                       using (SocketResponder sr = new SocketResponder (localEP, new SocketRequestHandler (CloseRequestHandler))) {
-                               sr.Start ();
-
-                               TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), 8765);
+                       using (SocketResponder sr = new SocketResponder (localEP, s => CloseRequestHandler (s))) {
+                               TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), port);
                                Assert.AreEqual (0, tcpClient.Available, "#B1");
                                Assert.IsTrue (tcpClient.Connected, "#B2");
                                // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#B3");
@@ -141,16 +145,23 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#else
                [ExpectedException (typeof(ArgumentNullException))]
+#endif
                public void ConnectMultiNull ()
                {
                        TcpClient client = new TcpClient ();
                        IPAddress[] ipAddresses = null;
                        
-                       client.Connect (ipAddresses, 1234);
+                       client.Connect (ipAddresses, NetworkHelpers.FindFreePort ());
                }
                
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ConnectMultiAny ()
                {
                        TcpClient client = new TcpClient ();
@@ -159,7 +170,7 @@ namespace MonoTests.System.Net.Sockets
                        ipAddresses[0] = IPAddress.Any;
                        
                        try {
-                               client.Connect (ipAddresses, 1234);
+                               client.Connect (ipAddresses, NetworkHelpers.FindFreePort ());
                                Assert.Fail ("ConnectMultiAny #1");
                        } catch (SocketException ex) {
                                Assert.AreEqual (10049, ex.ErrorCode, "ConnectMultiAny #2");
@@ -169,6 +180,9 @@ namespace MonoTests.System.Net.Sockets
                }
                
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ConnectMultiRefused ()
                {
                        TcpClient client = new TcpClient ();
@@ -177,7 +191,7 @@ namespace MonoTests.System.Net.Sockets
                        ipAddresses[0] = IPAddress.Loopback;
                        
                        try {
-                               client.Connect (ipAddresses, 1234);
+                               client.Connect (ipAddresses, NetworkHelpers.FindFreePort ());
                                Assert.Fail ("ConnectMultiRefused #1");
                        } catch (SocketException ex) {
                                Assert.AreEqual (10061, ex.ErrorCode, "ConnectMultiRefused #2");
@@ -185,5 +199,16 @@ namespace MonoTests.System.Net.Sockets
                                Assert.Fail ("ConnectMultiRefused #3");
                        }
                }
+
+               [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+               public void ExclusiveAddressUse ()
+               {
+                       using (TcpClient client = new TcpClient ()) {
+                               client.ExclusiveAddressUse = false;
+                       }
+               }
        }
 }