SessionTest.HttpProxy.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using System;
  2. using System.Net;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Renci.SshNet.Common;
  7. using Renci.SshNet.Tests.Common;
  8. namespace Renci.SshNet.Tests.Classes
  9. {
  10. public partial class SessionTest
  11. {
  12. [TestMethod]
  13. public void ConnectShouldThrowProxyExceptionWhenHttpProxyResponseDoesNotContainStatusLine()
  14. {
  15. var proxyEndPoint = new IPEndPoint(IPAddress.Loopback, 8123);
  16. var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
  17. using (var proxyStub = new HttpProxyStub(proxyEndPoint))
  18. {
  19. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("Whatever\r\n"));
  20. proxyStub.Start();
  21. using (var session = new Session(CreateConnectionInfoWithProxy(proxyEndPoint, serverEndPoint, "anon"), _serviceFactoryMock.Object))
  22. {
  23. try
  24. {
  25. session.Connect();
  26. Assert.Fail();
  27. }
  28. catch (ProxyException ex)
  29. {
  30. Assert.IsNull(ex.InnerException);
  31. Assert.AreEqual("HTTP response does not contain status line.", ex.Message);
  32. }
  33. }
  34. }
  35. }
  36. [TestMethod]
  37. public void ConnectShouldThrowProxyExceptionWhenHttpProxyReturnsHttpStatusOtherThan200()
  38. {
  39. var proxyEndPoint = new IPEndPoint(IPAddress.Loopback, 8123);
  40. var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
  41. using (var proxyStub = new HttpProxyStub(proxyEndPoint))
  42. {
  43. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("HTTP/1.0 501 Custom\r\n"));
  44. proxyStub.Start();
  45. using (var session = new Session(CreateConnectionInfoWithProxy(proxyEndPoint, serverEndPoint, "anon"), _serviceFactoryMock.Object))
  46. {
  47. try
  48. {
  49. session.Connect();
  50. Assert.Fail();
  51. }
  52. catch (ProxyException ex)
  53. {
  54. Assert.IsNull(ex.InnerException);
  55. Assert.AreEqual("HTTP: Status code 501, \"Custom\"", ex.Message);
  56. }
  57. }
  58. }
  59. }
  60. [TestMethod]
  61. public void ConnectShouldSkipHeadersWhenHttpProxyReturnsHttpStatus200()
  62. {
  63. var proxyEndPoint = new IPEndPoint(IPAddress.Loopback, 8123);
  64. var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
  65. using (var proxyStub = new HttpProxyStub(proxyEndPoint))
  66. {
  67. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("HTTP/1.0 200 OK\r\n"));
  68. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("Content-Type: application/octet-stream\r\n"));
  69. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("\r\n"));
  70. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("SSH-666-SshStub"));
  71. proxyStub.Start();
  72. using (var session = new Session(CreateConnectionInfoWithProxy(proxyEndPoint, serverEndPoint, "anon"), _serviceFactoryMock.Object))
  73. {
  74. try
  75. {
  76. session.Connect();
  77. Assert.Fail();
  78. }
  79. catch (SshConnectionException ex)
  80. {
  81. Assert.IsNull(ex.InnerException);
  82. Assert.AreEqual("Server version '666' is not supported.", ex.Message);
  83. }
  84. }
  85. }
  86. }
  87. [TestMethod]
  88. public void ConnectShouldSkipContentWhenHttpProxyReturnsHttpStatus200()
  89. {
  90. var proxyEndPoint = new IPEndPoint(IPAddress.Loopback, 8123);
  91. var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
  92. using (var proxyStub = new HttpProxyStub(proxyEndPoint))
  93. {
  94. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("HTTP/1.0 200 OK\r\n"));
  95. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("Content-Length: 13\r\n"));
  96. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("Content-Type: application/octet-stream\r\n"));
  97. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("\r\n"));
  98. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("DUMMY_CONTENT"));
  99. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("SSH-666-SshStub"));
  100. proxyStub.Start();
  101. using (var session = new Session(CreateConnectionInfoWithProxy(proxyEndPoint, serverEndPoint, "anon"), _serviceFactoryMock.Object))
  102. {
  103. try
  104. {
  105. session.Connect();
  106. Assert.Fail();
  107. }
  108. catch (SshConnectionException ex)
  109. {
  110. Assert.IsNull(ex.InnerException);
  111. Assert.AreEqual("Server version '666' is not supported.", ex.Message);
  112. }
  113. }
  114. }
  115. }
  116. [TestMethod]
  117. public void ConnectShouldWriteConnectMethodToHttpProxy()
  118. {
  119. var proxyEndPoint = new IPEndPoint(IPAddress.Loopback, 8123);
  120. var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
  121. using (var proxyStub = new HttpProxyStub(proxyEndPoint))
  122. {
  123. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("HTTP/1.0 501 Custom\r\n"));
  124. proxyStub.Start();
  125. using (var session = new Session(CreateConnectionInfoWithProxy(proxyEndPoint, serverEndPoint, "anon"), _serviceFactoryMock.Object))
  126. {
  127. try
  128. {
  129. session.Connect();
  130. Assert.Fail();
  131. }
  132. catch (ProxyException)
  133. {
  134. }
  135. }
  136. Assert.AreEqual(string.Format("CONNECT {0} HTTP/1.0", serverEndPoint), proxyStub.HttpRequest.RequestLine);
  137. }
  138. }
  139. [TestMethod]
  140. public void ConnectShouldWriteProxyAuthorizationToHttpProxyWhenProxyUserNameIsNotNullAndNotEmpty()
  141. {
  142. var proxyEndPoint = new IPEndPoint(IPAddress.Loopback, 8123);
  143. var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
  144. using (var proxyStub = new HttpProxyStub(proxyEndPoint))
  145. {
  146. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("HTTP/1.0 501 Custom\r\n"));
  147. proxyStub.Start();
  148. var connectionInfo = CreateConnectionInfoWithProxy(proxyEndPoint, serverEndPoint, "anon");
  149. using (var session = new Session(connectionInfo, _serviceFactoryMock.Object))
  150. {
  151. try
  152. {
  153. session.Connect();
  154. Assert.Fail();
  155. }
  156. catch (ProxyException)
  157. {
  158. }
  159. }
  160. var expectedProxyAuthorizationHeader = CreateProxyAuthorizationHeader(connectionInfo);
  161. Assert.IsNotNull(proxyStub.HttpRequest.Headers.SingleOrDefault(p => p == expectedProxyAuthorizationHeader));
  162. }
  163. }
  164. [TestMethod]
  165. public void ConnectShouldNotWriteProxyAuthorizationToHttpProxyWhenProxyUserNameIsEmpty()
  166. {
  167. var proxyEndPoint = new IPEndPoint(IPAddress.Loopback, 8123);
  168. var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
  169. using (var proxyStub = new HttpProxyStub(proxyEndPoint))
  170. {
  171. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("HTTP/1.0 501 Custom\r\n"));
  172. proxyStub.Start();
  173. var connectionInfo = CreateConnectionInfoWithProxy(proxyEndPoint, serverEndPoint, string.Empty);
  174. using (var session = new Session(connectionInfo, _serviceFactoryMock.Object))
  175. {
  176. try
  177. {
  178. session.Connect();
  179. Assert.Fail();
  180. }
  181. catch (ProxyException)
  182. {
  183. }
  184. }
  185. Assert.IsFalse(proxyStub.HttpRequest.Headers.Any(p => p.StartsWith("Proxy-Authorization:")));
  186. }
  187. }
  188. [TestMethod]
  189. public void ConnectShouldNotWriteProxyAuthorizationToHttpProxyWhenProxyUserNameIsNull()
  190. {
  191. var proxyEndPoint = new IPEndPoint(IPAddress.Loopback, 8123);
  192. var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
  193. using (var proxyStub = new HttpProxyStub(proxyEndPoint))
  194. {
  195. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("HTTP/1.0 501 Custom\r\n"));
  196. proxyStub.Start();
  197. var connectionInfo = CreateConnectionInfoWithProxy(proxyEndPoint, serverEndPoint, null);
  198. using (var session = new Session(connectionInfo, _serviceFactoryMock.Object))
  199. {
  200. try
  201. {
  202. session.Connect();
  203. Assert.Fail();
  204. }
  205. catch (ProxyException)
  206. {
  207. }
  208. }
  209. Assert.IsFalse(proxyStub.HttpRequest.Headers.Any(p => p.StartsWith("Proxy-Authorization:")));
  210. }
  211. }
  212. private static ConnectionInfo CreateConnectionInfoWithProxy(IPEndPoint proxyEndPoint, IPEndPoint serverEndPoint, string proxyUserName)
  213. {
  214. return new ConnectionInfo(
  215. serverEndPoint.Address.ToString(),
  216. serverEndPoint.Port,
  217. "eric",
  218. ProxyTypes.Http,
  219. proxyEndPoint.Address.ToString(),
  220. proxyEndPoint.Port,
  221. proxyUserName,
  222. "proxypwd",
  223. new NoneAuthenticationMethod("eric"));
  224. }
  225. private static string CreateProxyAuthorizationHeader(ConnectionInfo connectionInfo)
  226. {
  227. return string.Format("Proxy-Authorization: Basic {0}",
  228. Convert.ToBase64String(
  229. Encoding.ASCII.GetBytes(string.Format("{0}:{1}", connectionInfo.ProxyUsername,
  230. connectionInfo.ProxyPassword))));
  231. }
  232. }
  233. }