[System*] Add category for tests that use API that require BSD sockets.
[mono.git] / mcs / class / System / Test / System.Net.Sockets / TcpClientTest.cs
index a02e150915b70a06f5f7964ca09f55a038370fdb..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();
 
                        
@@ -75,11 +79,10 @@ 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");
                                Assert.AreEqual (0, tcpClient.Available, "#A2");
@@ -103,10 +106,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");