TcpClientTest.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // System.Net.Sockets.TcpClientTest.cs
  2. //
  3. // Authors:
  4. // Phillip Pearson ([email protected])
  5. // Martin Willemoes Hansen ([email protected])
  6. //
  7. // (C) Copyright 2001 Phillip Pearson (http://www.myelin.co.nz)
  8. // (C) Copyright 2003 Martin Willemoes Hansen
  9. //
  10. using System;
  11. using System.Net;
  12. using System.Net.Sockets;
  13. using NUnit.Framework;
  14. using MonoTests.Helpers;
  15. namespace MonoTests.System.Net.Sockets
  16. {
  17. /// <summary>
  18. /// Tests System.Net.Sockets.TcpClient
  19. /// </summary>
  20. [TestFixture]
  21. public class TcpClientTest
  22. {
  23. /// <summary>
  24. /// Tests the TcpClient object
  25. /// (from System.Net.Sockets)
  26. /// </summary>
  27. [Test]
  28. #if FEATURE_NO_BSD_SOCKETS
  29. [ExpectedException (typeof (PlatformNotSupportedException))]
  30. #endif
  31. public void TcpClient()
  32. {
  33. // set up a listening Socket
  34. Socket lSock = new Socket(AddressFamily.InterNetwork,
  35. SocketType.Stream, ProtocolType.Tcp);
  36. lSock.Bind(IPAddress.Any, out int port);
  37. lSock.Listen(-1);
  38. // connect to it with a TcpClient
  39. TcpClient outClient = new TcpClient("localhost", port);
  40. Socket inSock = lSock.Accept();
  41. // now try exchanging data
  42. NetworkStream stream = outClient.GetStream();
  43. const int len = 1024;
  44. byte[] outBuf = new Byte[len];
  45. for (int i=0; i<len; i++)
  46. {
  47. outBuf[i] = (byte)(i % 256);
  48. }
  49. // send it
  50. stream.Write(outBuf,0,len);
  51. // and see if it comes back
  52. byte[] inBuf = new Byte[len];
  53. int ret = inSock.Receive(inBuf, 0, len, 0);
  54. Assert.IsTrue (ret != 0);
  55. for (int i=0; i<len; i++)
  56. {
  57. Assert.IsTrue (inBuf[i] == outBuf[i]);
  58. }
  59. // tidy up
  60. inSock.Close();
  61. outClient.Close();
  62. lSock.Close();
  63. }
  64. [Test] // bug #81105
  65. #if FEATURE_NO_BSD_SOCKETS
  66. [ExpectedException (typeof (PlatformNotSupportedException))]
  67. #endif
  68. public void CloseTest ()
  69. {
  70. var port = 0;
  71. IPEndPoint localEP;
  72. using (SocketResponder sr = new SocketResponder (out localEP, s => CloseRequestHandler (s))) {
  73. port = localEP.Port;
  74. TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), port);
  75. NetworkStream ns = tcpClient.GetStream ();
  76. Assert.IsNotNull (ns, "#A1");
  77. Assert.AreEqual (0, tcpClient.Available, "#A2");
  78. Assert.IsTrue (tcpClient.Connected, "#A3");
  79. // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#A4");
  80. tcpClient.Close ();
  81. Assert.IsNotNull (tcpClient.Client, "#A5");
  82. try {
  83. int available = tcpClient.Available;
  84. Assert.Fail ("#A6: " + available);
  85. } catch (ObjectDisposedException) {
  86. }
  87. Assert.IsFalse (tcpClient.Connected, "#A7");
  88. // not supported on linux
  89. /*
  90. try {
  91. bool exclusive = tcpClient.ExclusiveAddressUse;
  92. Assert.Fail ("#A8: " + exclusive);
  93. } catch (ObjectDisposedException) {
  94. }
  95. */
  96. }
  97. using (SocketResponder sr = new SocketResponder (localEP, s => CloseRequestHandler (s))) {
  98. TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), port);
  99. Assert.AreEqual (0, tcpClient.Available, "#B1");
  100. Assert.IsTrue (tcpClient.Connected, "#B2");
  101. // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#B3");
  102. tcpClient.Close ();
  103. Assert.IsNull (tcpClient.Client, "#B4");
  104. try {
  105. int available = tcpClient.Available;
  106. Assert.Fail ("#B5: " + available);
  107. } catch (NullReferenceException) {
  108. }
  109. try {
  110. bool connected = tcpClient.Connected;
  111. Assert.Fail ("#B6: " + connected);
  112. } catch (NullReferenceException) {
  113. }
  114. // not supported on linux
  115. /*
  116. try {
  117. bool exclusive = tcpClient.ExclusiveAddressUse;
  118. Assert.Fail ("#B7: " + exclusive);
  119. } catch (NullReferenceException) {
  120. }
  121. */
  122. }
  123. }
  124. byte [] CloseRequestHandler (Socket socket)
  125. {
  126. return new byte [0];
  127. }
  128. [Test]
  129. #if FEATURE_NO_BSD_SOCKETS
  130. [ExpectedException (typeof (PlatformNotSupportedException))]
  131. #else
  132. [ExpectedException (typeof(ArgumentNullException))]
  133. #endif
  134. public void ConnectMultiNull ()
  135. {
  136. TcpClient client = new TcpClient ();
  137. IPAddress[] ipAddresses = null;
  138. client.Connect (ipAddresses, NetworkHelpers.FindFreePort ());
  139. }
  140. [Test]
  141. #if FEATURE_NO_BSD_SOCKETS
  142. [ExpectedException (typeof (PlatformNotSupportedException))]
  143. #endif
  144. public void ConnectMultiAny ()
  145. {
  146. TcpClient client = new TcpClient ();
  147. IPAddress[] ipAddresses = new IPAddress[1];
  148. ipAddresses[0] = IPAddress.Any;
  149. try {
  150. client.Connect (ipAddresses, NetworkHelpers.FindFreePort ());
  151. Assert.Fail ("ConnectMultiAny #1");
  152. } catch (SocketException ex) {
  153. Assert.AreEqual (10049, ex.ErrorCode, "ConnectMultiAny #2");
  154. } catch {
  155. Assert.Fail ("ConnectMultiAny #3");
  156. }
  157. }
  158. [Test]
  159. #if FEATURE_NO_BSD_SOCKETS
  160. [ExpectedException (typeof (PlatformNotSupportedException))]
  161. #endif
  162. public void ConnectMultiRefused ()
  163. {
  164. TcpClient client = new TcpClient ();
  165. IPAddress[] ipAddresses = new IPAddress[1];
  166. ipAddresses[0] = IPAddress.Loopback;
  167. try {
  168. client.Connect (ipAddresses, NetworkHelpers.FindFreePort ());
  169. Assert.Fail ("ConnectMultiRefused #1");
  170. } catch (SocketException ex) {
  171. Assert.AreEqual (10061, ex.ErrorCode, "ConnectMultiRefused #2");
  172. } catch {
  173. Assert.Fail ("ConnectMultiRefused #3");
  174. }
  175. }
  176. [Test]
  177. #if FEATURE_NO_BSD_SOCKETS
  178. [ExpectedException (typeof (PlatformNotSupportedException))]
  179. #endif
  180. public void ExclusiveAddressUse ()
  181. {
  182. using (TcpClient client = new TcpClient ()) {
  183. client.ExclusiveAddressUse = false;
  184. }
  185. }
  186. }
  187. }