TcpClientTest.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. [Category ("RequiresBSDSockets")]
  22. public class TcpClientTest
  23. {
  24. /// <summary>
  25. /// Tests the TcpClient object
  26. /// (from System.Net.Sockets)
  27. /// </summary>
  28. [Test]
  29. public void TcpClient()
  30. {
  31. // set up a listening Socket
  32. Socket lSock = new Socket(AddressFamily.InterNetwork,
  33. SocketType.Stream, ProtocolType.Tcp);
  34. var port = NetworkHelpers.FindFreePort ();
  35. lSock.Bind(new IPEndPoint(IPAddress.Any, port));
  36. lSock.Listen(-1);
  37. // connect to it with a TcpClient
  38. TcpClient outClient = new TcpClient("localhost", port);
  39. Socket inSock = lSock.Accept();
  40. // now try exchanging data
  41. NetworkStream stream = outClient.GetStream();
  42. const int len = 1024;
  43. byte[] outBuf = new Byte[len];
  44. for (int i=0; i<len; i++)
  45. {
  46. outBuf[i] = (byte)(i % 256);
  47. }
  48. // send it
  49. stream.Write(outBuf,0,len);
  50. // and see if it comes back
  51. byte[] inBuf = new Byte[len];
  52. int ret = inSock.Receive(inBuf, 0, len, 0);
  53. Assert.IsTrue (ret != 0);
  54. for (int i=0; i<len; i++)
  55. {
  56. Assert.IsTrue (inBuf[i] == outBuf[i]);
  57. }
  58. // tidy up
  59. inSock.Close();
  60. outClient.Close();
  61. lSock.Close();
  62. }
  63. [Test] // bug #81105
  64. public void CloseTest ()
  65. {
  66. var port = NetworkHelpers.FindFreePort ();
  67. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, port);
  68. using (SocketResponder sr = new SocketResponder (localEP, s => CloseRequestHandler (s))) {
  69. TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), port);
  70. NetworkStream ns = tcpClient.GetStream ();
  71. Assert.IsNotNull (ns, "#A1");
  72. Assert.AreEqual (0, tcpClient.Available, "#A2");
  73. Assert.IsTrue (tcpClient.Connected, "#A3");
  74. // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#A4");
  75. tcpClient.Close ();
  76. Assert.IsNotNull (tcpClient.Client, "#A5");
  77. try {
  78. int available = tcpClient.Available;
  79. Assert.Fail ("#A6: " + available);
  80. } catch (ObjectDisposedException) {
  81. }
  82. Assert.IsFalse (tcpClient.Connected, "#A7");
  83. // not supported on linux
  84. /*
  85. try {
  86. bool exclusive = tcpClient.ExclusiveAddressUse;
  87. Assert.Fail ("#A8: " + exclusive);
  88. } catch (ObjectDisposedException) {
  89. }
  90. */
  91. }
  92. using (SocketResponder sr = new SocketResponder (localEP, s => CloseRequestHandler (s))) {
  93. TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), port);
  94. Assert.AreEqual (0, tcpClient.Available, "#B1");
  95. Assert.IsTrue (tcpClient.Connected, "#B2");
  96. // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#B3");
  97. tcpClient.Close ();
  98. Assert.IsNull (tcpClient.Client, "#B4");
  99. try {
  100. int available = tcpClient.Available;
  101. Assert.Fail ("#B5: " + available);
  102. } catch (NullReferenceException) {
  103. }
  104. try {
  105. bool connected = tcpClient.Connected;
  106. Assert.Fail ("#B6: " + connected);
  107. } catch (NullReferenceException) {
  108. }
  109. // not supported on linux
  110. /*
  111. try {
  112. bool exclusive = tcpClient.ExclusiveAddressUse;
  113. Assert.Fail ("#B7: " + exclusive);
  114. } catch (NullReferenceException) {
  115. }
  116. */
  117. }
  118. }
  119. byte [] CloseRequestHandler (Socket socket)
  120. {
  121. return new byte [0];
  122. }
  123. [Test]
  124. [ExpectedException (typeof(ArgumentNullException))]
  125. public void ConnectMultiNull ()
  126. {
  127. TcpClient client = new TcpClient ();
  128. IPAddress[] ipAddresses = null;
  129. client.Connect (ipAddresses, 1234);
  130. }
  131. [Test]
  132. public void ConnectMultiAny ()
  133. {
  134. TcpClient client = new TcpClient ();
  135. IPAddress[] ipAddresses = new IPAddress[1];
  136. ipAddresses[0] = IPAddress.Any;
  137. try {
  138. client.Connect (ipAddresses, 1234);
  139. Assert.Fail ("ConnectMultiAny #1");
  140. } catch (SocketException ex) {
  141. Assert.AreEqual (10049, ex.ErrorCode, "ConnectMultiAny #2");
  142. } catch {
  143. Assert.Fail ("ConnectMultiAny #3");
  144. }
  145. }
  146. [Test]
  147. public void ConnectMultiRefused ()
  148. {
  149. TcpClient client = new TcpClient ();
  150. IPAddress[] ipAddresses = new IPAddress[1];
  151. ipAddresses[0] = IPAddress.Loopback;
  152. try {
  153. client.Connect (ipAddresses, 1234);
  154. Assert.Fail ("ConnectMultiRefused #1");
  155. } catch (SocketException ex) {
  156. Assert.AreEqual (10061, ex.ErrorCode, "ConnectMultiRefused #2");
  157. } catch {
  158. Assert.Fail ("ConnectMultiRefused #3");
  159. }
  160. }
  161. }
  162. }