SessionTest.HttpProxy.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 ConnectShouldThrowProxyExceptionWhenHttpProxyReturnsHttpStatusOtherThan200()
  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("HTTP/1.0 501 Custom\r\n"));
  20. proxyStub.Start();
  21. using (var session = new Session(CreateConnectionInfoWithProxy(proxyEndPoint, serverEndPoint, "anon")))
  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: Status code 501, \"Custom\"", ex.Message);
  32. }
  33. }
  34. }
  35. }
  36. [TestMethod]
  37. public void ConnectShouldSkipHeadersWhenHttpProxyReturnsHttpStatus200()
  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 200 OK\r\n"));
  44. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("Content-Type: application/octet-stream\r\n"));
  45. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("\r\n"));
  46. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("SSH-666-SshStub"));
  47. proxyStub.Start();
  48. using (var session = new Session(CreateConnectionInfoWithProxy(proxyEndPoint, serverEndPoint, "anon")))
  49. {
  50. try
  51. {
  52. session.Connect();
  53. Assert.Fail();
  54. }
  55. catch (SshConnectionException ex)
  56. {
  57. Assert.IsNull(ex.InnerException);
  58. Assert.AreEqual("Server version '666' is not supported.", ex.Message);
  59. }
  60. }
  61. }
  62. }
  63. [TestMethod]
  64. public void ConnectShouldSkipContentWhenHttpProxyReturnsHttpStatus200()
  65. {
  66. var proxyEndPoint = new IPEndPoint(IPAddress.Loopback, 8123);
  67. var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
  68. using (var proxyStub = new HttpProxyStub(proxyEndPoint))
  69. {
  70. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("HTTP/1.0 200 OK\r\n"));
  71. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("Content-Length: 13\r\n"));
  72. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("Content-Type: application/octet-stream\r\n"));
  73. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("\r\n"));
  74. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("DUMMY_CONTENT"));
  75. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("SSH-666-SshStub"));
  76. proxyStub.Start();
  77. using (var session = new Session(CreateConnectionInfoWithProxy(proxyEndPoint, serverEndPoint, "anon")))
  78. {
  79. try
  80. {
  81. session.Connect();
  82. Assert.Fail();
  83. }
  84. catch (SshConnectionException ex)
  85. {
  86. Assert.IsNull(ex.InnerException);
  87. Assert.AreEqual("Server version '666' is not supported.", ex.Message);
  88. }
  89. }
  90. }
  91. }
  92. [TestMethod]
  93. public void ConnectShouldWriteConnectMethodToHttpProxy()
  94. {
  95. var proxyEndPoint = new IPEndPoint(IPAddress.Loopback, 8123);
  96. var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
  97. using (var proxyStub = new HttpProxyStub(proxyEndPoint))
  98. {
  99. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("HTTP/1.0 501 Custom\r\n"));
  100. proxyStub.Start();
  101. using (var session = new Session(CreateConnectionInfoWithProxy(proxyEndPoint, serverEndPoint, "anon")))
  102. {
  103. try
  104. {
  105. session.Connect();
  106. Assert.Fail();
  107. }
  108. catch (ProxyException)
  109. {
  110. }
  111. }
  112. Assert.AreEqual(string.Format("CONNECT {0} HTTP/1.0", serverEndPoint), proxyStub.HttpRequest.RequestLine);
  113. }
  114. }
  115. [TestMethod]
  116. public void ConnectShouldWriteProxyAuthorizationToHttpProxyWhenProxyUserNameIsNotNullAndNotEmpty()
  117. {
  118. var proxyEndPoint = new IPEndPoint(IPAddress.Loopback, 8123);
  119. var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
  120. using (var proxyStub = new HttpProxyStub(proxyEndPoint))
  121. {
  122. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("HTTP/1.0 501 Custom\r\n"));
  123. proxyStub.Start();
  124. var connectionInfo = CreateConnectionInfoWithProxy(proxyEndPoint, serverEndPoint, "anon");
  125. using (var session = new Session(connectionInfo))
  126. {
  127. try
  128. {
  129. session.Connect();
  130. Assert.Fail();
  131. }
  132. catch (ProxyException)
  133. {
  134. }
  135. }
  136. var expectedProxyAuthorizationHeader = CreateProxyAuthorizationHeader(connectionInfo);
  137. Assert.IsNotNull(proxyStub.HttpRequest.Headers.SingleOrDefault(p => p == expectedProxyAuthorizationHeader));
  138. }
  139. }
  140. [TestMethod]
  141. public void ConnectShouldNotWriteProxyAuthorizationToHttpProxyWhenProxyUserNameIsEmpty()
  142. {
  143. var proxyEndPoint = new IPEndPoint(IPAddress.Loopback, 8123);
  144. var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
  145. using (var proxyStub = new HttpProxyStub(proxyEndPoint))
  146. {
  147. proxyStub.Responses.Add(Encoding.ASCII.GetBytes("HTTP/1.0 501 Custom\r\n"));
  148. proxyStub.Start();
  149. var connectionInfo = CreateConnectionInfoWithProxy(proxyEndPoint, serverEndPoint, string.Empty);
  150. using (var session = new Session(connectionInfo))
  151. {
  152. try
  153. {
  154. session.Connect();
  155. Assert.Fail();
  156. }
  157. catch (ProxyException)
  158. {
  159. }
  160. }
  161. Assert.IsFalse(proxyStub.HttpRequest.Headers.Any(p => p.StartsWith("Proxy-Authorization:")));
  162. }
  163. }
  164. [TestMethod]
  165. public void ConnectShouldNotWriteProxyAuthorizationToHttpProxyWhenProxyUserNameIsNull()
  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, null);
  174. using (var session = new Session(connectionInfo))
  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. private static ConnectionInfo CreateConnectionInfoWithProxy(IPEndPoint proxyEndPoint, IPEndPoint serverEndPoint, string proxyUserName)
  189. {
  190. return new ConnectionInfo(
  191. serverEndPoint.Address.ToString(),
  192. serverEndPoint.Port,
  193. "eric",
  194. ProxyTypes.Http,
  195. proxyEndPoint.Address.ToString(),
  196. proxyEndPoint.Port,
  197. proxyUserName,
  198. "proxypwd",
  199. new NoneAuthenticationMethod("eric"));
  200. }
  201. private static string CreateProxyAuthorizationHeader(ConnectionInfo connectionInfo)
  202. {
  203. return string.Format("Proxy-Authorization: Basic {0}",
  204. Convert.ToBase64String(
  205. Encoding.ASCII.GetBytes(string.Format("{0}:{1}", connectionInfo.ProxyUsername,
  206. connectionInfo.ProxyPassword))));
  207. }
  208. }
  209. }