[System*] Add category for tests that use API that require BSD sockets.
[mono.git] / mcs / class / System / Test / System.Net.Sockets / TcpClientTest.cs
index ffee12e97541f2e008e2ce6faee0c7b009fe33e6..4d3b8325048d9038ebfcaf8210960ebc71562f1a 100644 (file)
@@ -13,12 +13,15 @@ using System.Net;
 using System.Net.Sockets;
 using NUnit.Framework;
 
+using MonoTests.Helpers;
+
 namespace MonoTests.System.Net.Sockets
 {
        /// <summary>
        /// Tests System.Net.Sockets.TcpClient
        /// </summary>
        [TestFixture]
+       [Category ("RequiresBSDSockets")]
        public class TcpClientTest
        {
                
@@ -33,12 +36,13 @@ namespace MonoTests.System.Net.Sockets
                        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();
 
                        
@@ -58,11 +62,11 @@ namespace MonoTests.System.Net.Sockets
                        // and see if it comes back
                        byte[] inBuf = new Byte[len];
                        int ret = inSock.Receive(inBuf, 0, len, 0);
-                       Assertion.Assert(ret != 0);
+                       Assert.IsTrue (ret != 0);
 
                        for (int i=0; i<len; i++) 
                        {
-                               Assertion.Assert(inBuf[i] == outBuf[i]);
+                               Assert.IsTrue (inBuf[i] == outBuf[i]);
                        }
 
                        // tidy up
@@ -75,20 +79,16 @@ namespace MonoTests.System.Net.Sockets
                [Test] // bug #81105
                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");
-#if NET_2_0
                                Assert.AreEqual (0, tcpClient.Available, "#A2");
                                Assert.IsTrue (tcpClient.Connected, "#A3");
                                // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#A4");
-#endif
                                tcpClient.Close ();
-#if NET_2_0
                                Assert.IsNotNull (tcpClient.Client, "#A5");
                                try {
                                        int available = tcpClient.Available;
@@ -104,20 +104,14 @@ namespace MonoTests.System.Net.Sockets
                                } catch (ObjectDisposedException) {
                                }
                                */
-#endif
                        }
 
-                       using (SocketResponder sr = new SocketResponder (localEP, new SocketRequestHandler (CloseRequestHandler))) {
-                               sr.Start ();
-
-                               TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), 8765);
-#if NET_2_0
+                       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");
-#endif
                                tcpClient.Close ();
-#if NET_2_0
                                Assert.IsNull (tcpClient.Client, "#B4");
                                try {
                                        int available = tcpClient.Available;
@@ -137,7 +131,6 @@ namespace MonoTests.System.Net.Sockets
                                } catch (NullReferenceException) {
                                }
                                */
-#endif
                        }
                }
 
@@ -146,7 +139,6 @@ namespace MonoTests.System.Net.Sockets
                        return new byte [0];
                }
 
-#if NET_2_0
                [Test]
                [ExpectedException (typeof(ArgumentNullException))]
                public void ConnectMultiNull ()
@@ -169,8 +161,7 @@ namespace MonoTests.System.Net.Sockets
                                client.Connect (ipAddresses, 1234);
                                Assert.Fail ("ConnectMultiAny #1");
                        } catch (SocketException ex) {
-                               Assertion.AssertEquals ("ConnectMultiAny #2",
-                                                       10049, ex.ErrorCode);
+                               Assert.AreEqual (10049, ex.ErrorCode, "ConnectMultiAny #2");
                        } catch {
                                Assert.Fail ("ConnectMultiAny #3");
                        }
@@ -188,11 +179,10 @@ namespace MonoTests.System.Net.Sockets
                                client.Connect (ipAddresses, 1234);
                                Assert.Fail ("ConnectMultiRefused #1");
                        } catch (SocketException ex) {
-                               Assertion.AssertEquals ("ConnectMultiRefused #2", 10061, ex.ErrorCode);
+                               Assert.AreEqual (10061, ex.ErrorCode, "ConnectMultiRefused #2");
                        } catch {
                                Assert.Fail ("ConnectMultiRefused #3");
                        }
                }
-#endif
        }
 }