Socks5ConnectorTest_Connect_UserNamePasswordAuthentication_AuthenticationFailed.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using Microsoft.VisualStudio.TestTools.UnitTesting;
  9. using Moq;
  10. using Renci.SshNet.Common;
  11. using Renci.SshNet.Tests.Common;
  12. namespace Renci.SshNet.Tests.Classes.Connection
  13. {
  14. [TestClass]
  15. public class Socks5ConnectorTest_Connect_UserNamePasswordAuthentication_AuthenticationFailed : Socks5ConnectorTestBase
  16. {
  17. private ConnectionInfo _connectionInfo;
  18. private AsyncSocketListener _proxyServer;
  19. private Socket _clientSocket;
  20. private List<byte> _bytesReceivedByProxy;
  21. private bool _disconnected;
  22. private ProxyException _actualException;
  23. protected override void SetupData()
  24. {
  25. base.SetupData();
  26. _connectionInfo = CreateConnectionInfo("aa", "bbbb");
  27. _connectionInfo.Timeout = TimeSpan.FromMilliseconds(100);
  28. _bytesReceivedByProxy = new List<byte>();
  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. if (_bytesReceivedByProxy.Count == 4)
  36. {
  37. // We received the greeting
  38. _ = socket.Send(new byte[]
  39. {
  40. // SOCKS version
  41. 0x05,
  42. // Require username/password authentication
  43. 0x02
  44. });
  45. }
  46. else if (_bytesReceivedByProxy.Count == 4 + (1 + 1 + 2 + 1 + 4))
  47. {
  48. // We received the username/password authentication request
  49. _ = socket.Send(new byte[]
  50. {
  51. // Authentication version
  52. 0x01,
  53. // Authentication failed
  54. 0x01
  55. });
  56. }
  57. };
  58. _proxyServer.Start();
  59. }
  60. protected override void SetupMocks()
  61. {
  62. _ = SocketFactoryMock.Setup(p => p.Create(SocketType.Stream, ProtocolType.Tcp))
  63. .Returns(_clientSocket);
  64. }
  65. protected override void TearDown()
  66. {
  67. base.TearDown();
  68. _proxyServer?.Dispose();
  69. _clientSocket?.Dispose();
  70. }
  71. protected override void Act()
  72. {
  73. try
  74. {
  75. _ = Connector.Connect(_connectionInfo);
  76. Assert.Fail();
  77. }
  78. catch (ProxyException ex)
  79. {
  80. _actualException = ex;
  81. }
  82. // Give some time to process all messages
  83. Thread.Sleep(200);
  84. }
  85. [TestMethod]
  86. public void ConnectShouldHaveThrownProxyException()
  87. {
  88. Assert.IsNotNull(_actualException);
  89. Assert.IsNull(_actualException.InnerException);
  90. Assert.AreEqual("SOCKS5: Username/Password authentication failed.", _actualException.Message);
  91. }
  92. [TestMethod]
  93. public void ProxyShouldHaveReceivedExpectedSocksRequest()
  94. {
  95. var expectedSocksRequest = new List<byte>();
  96. //
  97. // Client greeting
  98. //
  99. // SOCKS version
  100. expectedSocksRequest.Add(0x05);
  101. // Number of authentication methods supported
  102. expectedSocksRequest.Add(0x02);
  103. // No authentication
  104. expectedSocksRequest.Add(0x00);
  105. // Username/password
  106. expectedSocksRequest.Add(0x02);
  107. //
  108. // Username/password authentication request
  109. //
  110. // Version of the negotiation
  111. expectedSocksRequest.Add(0x01);
  112. // Length of the username
  113. expectedSocksRequest.Add((byte)_connectionInfo.ProxyUsername.Length);
  114. // Username
  115. expectedSocksRequest.AddRange(Encoding.ASCII.GetBytes(_connectionInfo.ProxyUsername));
  116. // Length of the password
  117. expectedSocksRequest.Add((byte)_connectionInfo.ProxyPassword.Length);
  118. // Password
  119. expectedSocksRequest.AddRange(Encoding.ASCII.GetBytes(_connectionInfo.ProxyPassword));
  120. var errorText = string.Format("Expected:{0}{1}{0}but was:{0}{2}",
  121. Environment.NewLine,
  122. PacketDump.Create(expectedSocksRequest, 2),
  123. PacketDump.Create(_bytesReceivedByProxy, 2));
  124. Assert.IsTrue(expectedSocksRequest.SequenceEqual(_bytesReceivedByProxy), errorText);
  125. }
  126. [TestMethod]
  127. public void ConnectionToProxyShouldHaveBeenShutDown()
  128. {
  129. Assert.IsTrue(_disconnected);
  130. }
  131. [TestMethod]
  132. public void ClientSocketShouldHaveBeenDisposed()
  133. {
  134. try
  135. {
  136. _ = _clientSocket.Receive(new byte[0]);
  137. Assert.Fail();
  138. }
  139. catch (ObjectDisposedException)
  140. {
  141. }
  142. }
  143. [TestMethod]
  144. public void CreateOnSocketFactoryShouldHaveBeenInvokedOnce()
  145. {
  146. SocketFactoryMock.Verify(p => p.Create(SocketType.Stream, ProtocolType.Tcp),
  147. Times.Once());
  148. }
  149. }
  150. }