Socks5ConnectorTest_Connect_UserNamePasswordAuthentication_AuthenticationFailed.cs 5.8 KB

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