Socks5ConnectorTest_Connect_UserNamePasswordAuthentication_UserNameExceedsMaximumLength.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Threading;
  7. using Microsoft.VisualStudio.TestTools.UnitTesting;
  8. using Moq;
  9. using Renci.SshNet.Common;
  10. using Renci.SshNet.Tests.Common;
  11. namespace Renci.SshNet.Tests.Classes.Connection
  12. {
  13. [TestClass]
  14. public class Socks5ConnectorTest_Connect_UserNamePasswordAuthentication_UserNameExceedsMaximumLength : Socks5ConnectorTestBase
  15. {
  16. private ConnectionInfo _connectionInfo;
  17. private AsyncSocketListener _proxyServer;
  18. private Socket _clientSocket;
  19. private List<byte> _bytesReceivedByProxy;
  20. private bool _disconnected;
  21. private ProxyException _actualException;
  22. protected override void SetupData()
  23. {
  24. base.SetupData();
  25. _connectionInfo = CreateConnectionInfo(new string('a', 256), new string('b', 255));
  26. _connectionInfo.Timeout = TimeSpan.FromMilliseconds(100);
  27. _bytesReceivedByProxy = new List<byte>();
  28. _actualException = null;
  29. _clientSocket = SocketFactory.Create(SocketType.Stream, ProtocolType.Tcp);
  30. _proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _connectionInfo.ProxyPort));
  31. _proxyServer.Disconnected += socket => _disconnected = true;
  32. _proxyServer.BytesReceived += (bytesReceived, socket) =>
  33. {
  34. _bytesReceivedByProxy.AddRange(bytesReceived);
  35. // Wait until we received the greeting
  36. if (_bytesReceivedByProxy.Count == 4)
  37. {
  38. _ = socket.Send(new byte[]
  39. {
  40. // SOCKS version
  41. 0x05,
  42. // Username/password authentication
  43. 0x02
  44. });
  45. }
  46. };
  47. _proxyServer.Start();
  48. }
  49. protected override void SetupMocks()
  50. {
  51. _ = SocketFactoryMock.Setup(p => p.Create(SocketType.Stream, ProtocolType.Tcp))
  52. .Returns(_clientSocket);
  53. }
  54. protected override void TearDown()
  55. {
  56. base.TearDown();
  57. _proxyServer?.Dispose();
  58. _clientSocket?.Dispose();
  59. }
  60. protected override void Act()
  61. {
  62. try
  63. {
  64. _ = Connector.Connect(_connectionInfo);
  65. Assert.Fail();
  66. }
  67. catch (ProxyException ex)
  68. {
  69. _actualException = ex;
  70. }
  71. // Give some time to process all messages
  72. Thread.Sleep(200);
  73. }
  74. [TestMethod]
  75. public void ConnectShouldHaveThrownProxyException()
  76. {
  77. Assert.IsNotNull(_actualException);
  78. Assert.IsNull(_actualException.InnerException);
  79. Assert.AreEqual("Proxy username is too long.", _actualException.Message);
  80. }
  81. [TestMethod]
  82. public void ProxyShouldHaveReceivedExpectedSocksRequest()
  83. {
  84. // Client greeting
  85. var expectedSocksRequest = new List<byte>
  86. {
  87. // SOCKS version
  88. 0x05,
  89. // Number of authentication methods supported
  90. 0x02,
  91. // No authentication
  92. 0x00,
  93. // Username/password
  94. 0x02
  95. };
  96. var errorText = string.Format("Expected:{0}{1}{0}but was:{0}{2}",
  97. Environment.NewLine,
  98. PacketDump.Create(expectedSocksRequest, 2),
  99. PacketDump.Create(_bytesReceivedByProxy, 2));
  100. Assert.IsTrue(expectedSocksRequest.SequenceEqual(_bytesReceivedByProxy), errorText);
  101. }
  102. [TestMethod]
  103. public void ConnectionToProxyShouldHaveBeenShutDown()
  104. {
  105. Assert.IsTrue(_disconnected);
  106. }
  107. [TestMethod]
  108. public void ClientSocketShouldHaveBeenDisposed()
  109. {
  110. try
  111. {
  112. _ = _clientSocket.Receive(new byte[0]);
  113. Assert.Fail();
  114. }
  115. catch (ObjectDisposedException)
  116. {
  117. }
  118. }
  119. [TestMethod]
  120. public void CreateOnSocketFactoryShouldHaveBeenInvokedOnce()
  121. {
  122. SocketFactoryMock.Verify(p => p.Create(SocketType.Stream, ProtocolType.Tcp),
  123. Times.Once());
  124. }
  125. }
  126. }