-// System.Net.Sockets.TcpClientTest.cs\r
-//\r
-// Authors:\r
-// Phillip Pearson (pp@myelin.co.nz)\r
-// Martin Willemoes Hansen (mwh@sysrq.dk)\r
-//\r
-// (C) Copyright 2001 Phillip Pearson (http://www.myelin.co.nz)\r
-// (C) Copyright 2003 Martin Willemoes Hansen\r
-//\r
-\r
-using System;\r
-using System.Net;\r
-using System.Net.Sockets;\r
-using NUnit.Framework;\r
-\r
-namespace MonoTests.System.Net.Sockets {\r
-\r
- /// <summary>\r
- /// Tests System.Net.Sockets.TcpClient\r
- /// </summary>\r
- [TestFixture]\r
- public class TcpClientTest {\r
- \r
- /// <summary>\r
- /// Tests the TcpClient object\r
- /// (from System.Net.Sockets)\r
- /// </summary>\r
- [Test]\r
- public void TcpClient()\r
- {\r
- // set up a listening Socket\r
- Socket lSock = new Socket(AddressFamily.InterNetwork,\r
- SocketType.Stream, ProtocolType.Tcp);\r
- \r
- lSock.Bind(new IPEndPoint(IPAddress.Any, 8765));\r
- lSock.Listen(-1);\r
-\r
-\r
- // connect to it with a TcpClient\r
- TcpClient outClient = new TcpClient("localhost", 8765);\r
- Socket inSock = lSock.Accept();\r
-\r
- \r
- // now try exchanging data\r
- NetworkStream stream = outClient.GetStream();\r
-\r
- const int len = 1024;\r
- byte[] outBuf = new Byte[len];\r
- for (int i=0; i<len; i++) \r
- {\r
- outBuf[i] = (byte)(i % 256);\r
- }\r
-\r
- // send it\r
- stream.Write(outBuf,0,len);\r
-\r
- // and see if it comes back\r
- byte[] inBuf = new Byte[len];\r
- int ret = inSock.Receive(inBuf, 0, len, 0);\r
- Assertion.Assert(ret != 0);\r
-\r
- for (int i=0; i<len; i++) \r
- {\r
- Assertion.Assert(inBuf[i] == outBuf[i]);\r
- }\r
- \r
-\r
- // tidy up\r
- inSock.Close();\r
- outClient.Close();\r
- lSock.Close();\r
- \r
- }\r
-\r
- [Test] // bug #81105\r
- [Category ("NotWorking")]\r
-/*\r
- This test flagged as not working as its producing this:\r
-\r
-1) MonoTests.System.Net.Sockets.TcpClientTest.CloseTest : System.Net.Sockets.SocketException : Address already in use\r
- at System.Net.Sockets.Socket.Bind (System.Net.EndPoint local_end) [0x00059] in /home/cvs/mcs/class/System/System.Net.Sockets/Socket.cs:2015\r
- at System.Net.Sockets.TcpListener.Start (Int32 backlog) [0x00023] in /home/cvs/mcs/class/System/System.Net.Sockets/TcpListener.cs:265\r
- at System.Net.Sockets.TcpListener.Start () [0x00000] in /home/cvs/mcs/class/System/System.Net.Sockets/TcpListener.cs:240\r
- at MonoTests.System.Net.SocketResponder.Start () [0x00011] in /home/cvs/mcs/class/System/Test/System.Net/SocketResponder.cs:67\r
- at MonoTests.System.Net.Sockets.TcpClientTest.CloseTest () [0x0007d] in /home/cvs/mcs/class/System/Test/System.Net.Sockets/TcpClientTest.cs:111\r
- at <0x00000> <unknown method>\r
- at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[])\r
- at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00040] in /home/cvs/mcs/class/corlib/System.Reflection/MonoMethod.cs:143\r
-*/\r
- public void CloseTest ()\r
- {\r
- IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8765);\r
- using (SocketResponder sr = new SocketResponder (localEP, new SocketRequestHandler (CloseRequestHandler))) {\r
- sr.Start ();\r
-\r
- TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), 8765);\r
- NetworkStream ns = tcpClient.GetStream ();\r
- Assert.IsNotNull (ns, "#A1");\r
-#if NET_2_0\r
- Assert.AreEqual (0, tcpClient.Available, "#A2");\r
- Assert.IsTrue (tcpClient.Connected, "#A3");\r
- // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#A4");\r
-#endif\r
- tcpClient.Close ();\r
-#if NET_2_0\r
- Assert.IsNotNull (tcpClient.Client, "#A5");\r
- try {\r
- int available = tcpClient.Available;\r
- Assert.Fail ("#A6: " + available);\r
- } catch (ObjectDisposedException) {\r
- }\r
- Assert.IsFalse (tcpClient.Connected, "#A7");\r
- // not supported on linux\r
- /*\r
- try {\r
- bool exclusive = tcpClient.ExclusiveAddressUse;\r
- Assert.Fail ("#A8: " + exclusive);\r
- } catch (ObjectDisposedException) {\r
- }\r
- */\r
-#endif\r
- }\r
-\r
- using (SocketResponder sr = new SocketResponder (localEP, new SocketRequestHandler (CloseRequestHandler))) {\r
- sr.Start ();\r
-\r
- TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), 8765);\r
-#if NET_2_0\r
- Assert.AreEqual (0, tcpClient.Available, "#B1");\r
- Assert.IsTrue (tcpClient.Connected, "#B2");\r
- // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#B3");\r
-#endif\r
- tcpClient.Close ();\r
-#if NET_2_0\r
- Assert.IsNull (tcpClient.Client, "#B4");\r
- try {\r
- int available = tcpClient.Available;\r
- Assert.Fail ("#B5: " + available);\r
- } catch (NullReferenceException) {\r
- }\r
- try {\r
- bool connected = tcpClient.Connected;\r
- Assert.Fail ("#B6: " + connected);\r
- } catch (NullReferenceException) {\r
- }\r
- // not supported on linux\r
- /*\r
- try {\r
- bool exclusive = tcpClient.ExclusiveAddressUse;\r
- Assert.Fail ("#B7: " + exclusive);\r
- } catch (NullReferenceException) {\r
- }\r
- */\r
-#endif\r
- }\r
- }\r
-\r
- byte [] CloseRequestHandler (Socket socket)\r
- {\r
- return new byte [0];\r
- }\r
-\r
-#if NET_2_0\r
- [Test]\r
- [ExpectedException (typeof(ArgumentNullException))]\r
- public void ConnectMultiNull ()\r
- {\r
- TcpClient client = new TcpClient ();\r
- IPAddress[] ipAddresses = null;\r
- \r
- client.Connect (ipAddresses, 1234);\r
- }\r
- \r
- [Test]\r
- public void ConnectMultiAny ()\r
- {\r
- TcpClient client = new TcpClient ();\r
- IPAddress[] ipAddresses = new IPAddress[1];\r
- \r
- ipAddresses[0] = IPAddress.Any;\r
- \r
- try {\r
- client.Connect (ipAddresses, 1234);\r
- Assert.Fail ("ConnectMultiAny #1");\r
- } catch (SocketException ex) {\r
- Assertion.AssertEquals ("ConnectMultiAny #2",\r
- 10049, ex.ErrorCode);\r
- } catch {\r
- Assert.Fail ("ConnectMultiAny #3");\r
- }\r
- }\r
- \r
- [Test]\r
- public void ConnectMultiRefused ()\r
- {\r
- TcpClient client = new TcpClient ();\r
- IPAddress[] ipAddresses = new IPAddress[1];\r
- \r
- ipAddresses[0] = IPAddress.Loopback;\r
- \r
- try {\r
- client.Connect (ipAddresses, 1234);\r
- Assert.Fail ("ConnectMultiRefused #1");\r
- } catch (SocketException ex) {\r
- Assertion.AssertEquals ("ConnectMultiRefused #2", 10061, ex.ErrorCode);\r
- } catch {\r
- Assert.Fail ("ConnectMultiRefused #3");\r
- }\r
- }\r
- \r
-#endif\r
- \r
- }\r
-\r
-}\r
+// System.Net.Sockets.TcpClientTest.cs
+//
+// Authors:
+// Phillip Pearson (pp@myelin.co.nz)
+// Martin Willemoes Hansen (mwh@sysrq.dk)
+//
+// (C) Copyright 2001 Phillip Pearson (http://www.myelin.co.nz)
+// (C) Copyright 2003 Martin Willemoes Hansen
+//
+
+using System;
+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
+ {
+
+ /// <summary>
+ /// Tests the TcpClient object
+ /// (from System.Net.Sockets)
+ /// </summary>
+ [Test]
+ public void TcpClient()
+ {
+ // set up a listening Socket
+ Socket lSock = new Socket(AddressFamily.InterNetwork,
+ SocketType.Stream, ProtocolType.Tcp);
+
+ 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", port);
+ Socket inSock = lSock.Accept();
+
+
+ // now try exchanging data
+ NetworkStream stream = outClient.GetStream();
+
+ const int len = 1024;
+ byte[] outBuf = new Byte[len];
+ for (int i=0; i<len; i++)
+ {
+ outBuf[i] = (byte)(i % 256);
+ }
+
+ // send it
+ stream.Write(outBuf,0,len);
+
+ // and see if it comes back
+ byte[] inBuf = new Byte[len];
+ int ret = inSock.Receive(inBuf, 0, len, 0);
+ Assert.IsTrue (ret != 0);
+
+ for (int i=0; i<len; i++)
+ {
+ Assert.IsTrue (inBuf[i] == outBuf[i]);
+ }
+
+ // tidy up
+ inSock.Close();
+ outClient.Close();
+ lSock.Close();
+
+ }
+
+ [Test] // bug #81105
+ public void CloseTest ()
+ {
+ 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");
+ Assert.IsTrue (tcpClient.Connected, "#A3");
+ // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#A4");
+ tcpClient.Close ();
+ Assert.IsNotNull (tcpClient.Client, "#A5");
+ try {
+ int available = tcpClient.Available;
+ Assert.Fail ("#A6: " + available);
+ } catch (ObjectDisposedException) {
+ }
+ Assert.IsFalse (tcpClient.Connected, "#A7");
+ // not supported on linux
+ /*
+ try {
+ bool exclusive = tcpClient.ExclusiveAddressUse;
+ Assert.Fail ("#A8: " + exclusive);
+ } catch (ObjectDisposedException) {
+ }
+ */
+ }
+
+ 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");
+ tcpClient.Close ();
+ Assert.IsNull (tcpClient.Client, "#B4");
+ try {
+ int available = tcpClient.Available;
+ Assert.Fail ("#B5: " + available);
+ } catch (NullReferenceException) {
+ }
+ try {
+ bool connected = tcpClient.Connected;
+ Assert.Fail ("#B6: " + connected);
+ } catch (NullReferenceException) {
+ }
+ // not supported on linux
+ /*
+ try {
+ bool exclusive = tcpClient.ExclusiveAddressUse;
+ Assert.Fail ("#B7: " + exclusive);
+ } catch (NullReferenceException) {
+ }
+ */
+ }
+ }
+
+ byte [] CloseRequestHandler (Socket socket)
+ {
+ return new byte [0];
+ }
+
+ [Test]
+ [ExpectedException (typeof(ArgumentNullException))]
+ public void ConnectMultiNull ()
+ {
+ TcpClient client = new TcpClient ();
+ IPAddress[] ipAddresses = null;
+
+ client.Connect (ipAddresses, 1234);
+ }
+
+ [Test]
+ public void ConnectMultiAny ()
+ {
+ TcpClient client = new TcpClient ();
+ IPAddress[] ipAddresses = new IPAddress[1];
+
+ ipAddresses[0] = IPAddress.Any;
+
+ try {
+ client.Connect (ipAddresses, 1234);
+ Assert.Fail ("ConnectMultiAny #1");
+ } catch (SocketException ex) {
+ Assert.AreEqual (10049, ex.ErrorCode, "ConnectMultiAny #2");
+ } catch {
+ Assert.Fail ("ConnectMultiAny #3");
+ }
+ }
+
+ [Test]
+ public void ConnectMultiRefused ()
+ {
+ TcpClient client = new TcpClient ();
+ IPAddress[] ipAddresses = new IPAddress[1];
+
+ ipAddresses[0] = IPAddress.Loopback;
+
+ try {
+ client.Connect (ipAddresses, 1234);
+ Assert.Fail ("ConnectMultiRefused #1");
+ } catch (SocketException ex) {
+ Assert.AreEqual (10061, ex.ErrorCode, "ConnectMultiRefused #2");
+ } catch {
+ Assert.Fail ("ConnectMultiRefused #3");
+ }
+ }
+ }
+}