PrivateKeyFileTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Renci.SshNet.Common;
  3. using Renci.SshNet.Tests.Common;
  4. using System;
  5. using System.IO;
  6. namespace Renci.SshNet.Tests.Classes
  7. {
  8. /// <summary>
  9. /// old private key information/
  10. /// </summary>
  11. [TestClass]
  12. public class PrivateKeyFileTest : TestBase
  13. {
  14. private string _temporaryFile;
  15. [TestInitialize]
  16. public void SetUp()
  17. {
  18. _temporaryFile = GetTempFileName();
  19. }
  20. [TestCleanup]
  21. public void TearDown()
  22. {
  23. if (_temporaryFile != null)
  24. File.Delete(_temporaryFile);
  25. }
  26. /// <summary>
  27. /// A test for <see cref="PrivateKeyFile(string)"/> ctor.
  28. ///</summary>
  29. [WorkItem(703), TestMethod]
  30. public void ConstructorWithFileNameShouldThrowArgumentNullExceptionWhenFileNameIsEmpty()
  31. {
  32. var fileName = string.Empty;
  33. try
  34. {
  35. new PrivateKeyFile(fileName);
  36. Assert.Fail();
  37. }
  38. catch (ArgumentNullException ex)
  39. {
  40. Assert.IsNull(ex.InnerException);
  41. Assert.AreEqual("fileName", ex.ParamName);
  42. }
  43. }
  44. /// <summary>
  45. /// A test for <see cref="PrivateKeyFile(string)"/> ctor.
  46. ///</summary>
  47. [WorkItem(703), TestMethod]
  48. public void ConstructorWithFileNameShouldThrowArgumentNullExceptionWhenFileNameIsNull()
  49. {
  50. var fileName = string.Empty;
  51. try
  52. {
  53. new PrivateKeyFile(fileName);
  54. Assert.Fail();
  55. }
  56. catch (ArgumentNullException ex)
  57. {
  58. Assert.IsNull(ex.InnerException);
  59. Assert.AreEqual("fileName", ex.ParamName);
  60. }
  61. }
  62. /// <summary>
  63. /// A test for <see cref="PrivateKeyFile(string, string)"/> ctor.
  64. ///</summary>
  65. [WorkItem(703), TestMethod]
  66. public void ConstructorWithFileNameAndPassphraseShouldThrowArgumentNullExceptionWhenFileNameIsEmpty()
  67. {
  68. var fileName = string.Empty;
  69. try
  70. {
  71. new PrivateKeyFile(fileName, "12345");
  72. Assert.Fail();
  73. }
  74. catch (ArgumentNullException ex)
  75. {
  76. Assert.IsNull(ex.InnerException);
  77. Assert.AreEqual("fileName", ex.ParamName);
  78. }
  79. }
  80. /// <summary>
  81. /// A test for <see cref="PrivateKeyFile(string, string)"/> ctor.
  82. ///</summary>
  83. [WorkItem(703), TestMethod]
  84. public void ConstructorWithFileNameAndPassphraseShouldThrowArgumentNullExceptionWhenFileNameIsNull()
  85. {
  86. var fileName = string.Empty;
  87. try
  88. {
  89. new PrivateKeyFile(fileName, "12345");
  90. Assert.Fail();
  91. }
  92. catch (ArgumentNullException ex)
  93. {
  94. Assert.IsNull(ex.InnerException);
  95. Assert.AreEqual("fileName", ex.ParamName);
  96. }
  97. }
  98. [WorkItem(703), TestMethod]
  99. public void ConstructorWithPrivateKeyShouldThrowArgumentNullExceptionWhenPrivateKeyIsNull()
  100. {
  101. Stream privateKey = null;
  102. try
  103. {
  104. new PrivateKeyFile(privateKey);
  105. Assert.Fail();
  106. }
  107. catch (ArgumentNullException ex)
  108. {
  109. Assert.IsNull(ex.InnerException);
  110. Assert.AreEqual("privateKey", ex.ParamName);
  111. }
  112. }
  113. [WorkItem(703), TestMethod]
  114. public void ConstructorWithPrivateKeyAndPassphraseShouldThrowArgumentNullExceptionWhenPrivateKeyIsNull()
  115. {
  116. Stream privateKey = null;
  117. try
  118. {
  119. new PrivateKeyFile(privateKey, "12345");
  120. Assert.Fail();
  121. }
  122. catch (ArgumentNullException ex)
  123. {
  124. Assert.IsNull(ex.InnerException);
  125. Assert.AreEqual("privateKey", ex.ParamName);
  126. }
  127. }
  128. [TestMethod]
  129. [Owner("olegkap")]
  130. [TestCategory("PrivateKey")]
  131. public void Test_PrivateKey_RSA()
  132. {
  133. using (var stream = this.GetData("Key.RSA.txt"))
  134. {
  135. new PrivateKeyFile(stream);
  136. }
  137. }
  138. [TestMethod]
  139. [Owner("olegkap")]
  140. [TestCategory("PrivateKey")]
  141. public void Test_PrivateKey_RSA_DES_CBC()
  142. {
  143. using (var stream = this.GetData("Key.RSA.Encrypted.Des.CBC.12345.txt"))
  144. {
  145. new PrivateKeyFile(stream, "12345");
  146. }
  147. }
  148. [TestMethod]
  149. [Owner("olegkap")]
  150. [TestCategory("PrivateKey")]
  151. public void Test_PrivateKey_RSA_DES_EDE3_CBC()
  152. {
  153. using (var stream = this.GetData("Key.RSA.Encrypted.Des.Ede3.CBC.12345.txt"))
  154. {
  155. new PrivateKeyFile(stream, "12345");
  156. }
  157. }
  158. [TestMethod]
  159. [Owner("olegkap")]
  160. [TestCategory("PrivateKey")]
  161. public void Test_PrivateKey_RSA_AES_128_CBC()
  162. {
  163. using (var stream = this.GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
  164. {
  165. new PrivateKeyFile(stream, "12345");
  166. }
  167. }
  168. [TestMethod]
  169. [Owner("olegkap")]
  170. [TestCategory("PrivateKey")]
  171. public void Test_PrivateKey_RSA_AES_192_CBC()
  172. {
  173. using (var stream = this.GetData("Key.RSA.Encrypted.Aes.192.CBC.12345.txt"))
  174. {
  175. new PrivateKeyFile(stream, "12345");
  176. }
  177. }
  178. [TestMethod]
  179. [Owner("olegkap")]
  180. [TestCategory("PrivateKey")]
  181. public void Test_PrivateKey_RSA_AES_256_CBC()
  182. {
  183. using (var stream = this.GetData("Key.RSA.Encrypted.Aes.256.CBC.12345.txt"))
  184. {
  185. new PrivateKeyFile(stream, "12345");
  186. }
  187. }
  188. [TestMethod]
  189. [Owner("olegkap")]
  190. [TestCategory("PrivateKey")]
  191. public void Test_PrivateKey_RSA_DES_EDE3_CFB()
  192. {
  193. using (var stream = this.GetData("Key.RSA.Encrypted.Des.Ede3.CFB.1234567890.txt"))
  194. {
  195. new PrivateKeyFile(stream, "1234567890");
  196. }
  197. }
  198. /// <summary>
  199. ///A test for Dispose
  200. ///</summary>
  201. [TestMethod()]
  202. public void DisposeTest()
  203. {
  204. using (var privateKeyStream = GetData("Key.RSA.txt"))
  205. {
  206. var target = new PrivateKeyFile(privateKeyStream);
  207. target.Dispose();
  208. }
  209. }
  210. /// <summary>
  211. /// A test for <see cref="PrivateKeyFile(Stream, string)"/> ctor.
  212. ///</summary>
  213. [TestMethod()]
  214. public void ConstructorWithStreamAndPassphrase()
  215. {
  216. using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
  217. {
  218. var privateKeyFile = new PrivateKeyFile(stream, "12345");
  219. Assert.IsNotNull(privateKeyFile.HostKey);
  220. }
  221. }
  222. /// <summary>
  223. /// A test for <see cref="PrivateKeyFile(string, string)"/> ctor.
  224. ///</summary>
  225. [TestMethod()]
  226. public void ConstructorWithFileNameAndPassphrase()
  227. {
  228. using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
  229. {
  230. SaveStreamToFile(stream, _temporaryFile);
  231. }
  232. using (var fs = File.Open(_temporaryFile, FileMode.Open, FileAccess.Read, FileShare.Read))
  233. {
  234. var privateKeyFile = new PrivateKeyFile(_temporaryFile, "12345");
  235. Assert.IsNotNull(privateKeyFile.HostKey);
  236. fs.Close();
  237. }
  238. }
  239. /// <summary>
  240. /// A test for <see cref="PrivateKeyFile(string, string)"/> ctor.
  241. ///</summary>
  242. [TestMethod()]
  243. public void ConstructorWithFileNameAndPassphraseShouldThrowSshPassPhraseNullOrEmptyExceptionWhenPrivateKeyIsEncryptedAndPassphraseIsEmpty()
  244. {
  245. var passphrase = string.Empty;
  246. using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
  247. {
  248. SaveStreamToFile(stream, _temporaryFile);
  249. }
  250. try
  251. {
  252. new PrivateKeyFile(_temporaryFile, passphrase);
  253. Assert.Fail();
  254. }
  255. catch (SshPassPhraseNullOrEmptyException ex)
  256. {
  257. Assert.IsNull(ex.InnerException);
  258. Assert.AreEqual("Private key is encrypted but passphrase is empty.", ex.Message);
  259. }
  260. }
  261. /// <summary>
  262. /// A test for <see cref="PrivateKeyFile(string, string)"/> ctor.
  263. ///</summary>
  264. [TestMethod()]
  265. public void ConstructorWithFileNameAndPassphraseShouldThrowSshPassPhraseNullOrEmptyExceptionWhenPrivateKeyIsEncryptedAndPassphraseIsNull()
  266. {
  267. string passphrase = null;
  268. using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
  269. {
  270. SaveStreamToFile(stream, _temporaryFile);
  271. }
  272. try
  273. {
  274. new PrivateKeyFile(_temporaryFile, passphrase);
  275. Assert.Fail();
  276. }
  277. catch (SshPassPhraseNullOrEmptyException ex)
  278. {
  279. Assert.IsNull(ex.InnerException);
  280. Assert.AreEqual("Private key is encrypted but passphrase is empty.", ex.Message);
  281. }
  282. }
  283. /// <summary>
  284. /// A test for <see cref="PrivateKeyFile(string)"/> ctor.
  285. ///</summary>
  286. [TestMethod()]
  287. public void ConstructorWithFileName()
  288. {
  289. using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
  290. {
  291. SaveStreamToFile(stream, _temporaryFile);
  292. }
  293. var privateKeyFile = new PrivateKeyFile(_temporaryFile, "12345");
  294. Assert.IsNotNull(privateKeyFile.HostKey);
  295. }
  296. /// <summary>
  297. /// A test for <see cref="PrivateKeyFile(Stream)"/> ctor.
  298. ///</summary>
  299. [TestMethod()]
  300. public void ConstructorWithStream()
  301. {
  302. using (var stream = GetData("Key.RSA.txt"))
  303. {
  304. var privateKeyFile = new PrivateKeyFile(stream);
  305. Assert.IsNotNull(privateKeyFile.HostKey);
  306. }
  307. }
  308. [TestMethod]
  309. [TestCategory("PrivateKey")]
  310. public void ConstructorWithFileNameShouldBeAbleToReadFileThatIsSharedForReadAccess()
  311. {
  312. using (var stream = GetData("Key.RSA.txt"))
  313. {
  314. SaveStreamToFile(stream, _temporaryFile);
  315. }
  316. using (var fs = File.Open(_temporaryFile, FileMode.Open, FileAccess.Read, FileShare.Read))
  317. {
  318. var privateKeyFile = new PrivateKeyFile(_temporaryFile);
  319. Assert.IsNotNull(privateKeyFile.HostKey);
  320. fs.Close();
  321. }
  322. }
  323. [TestMethod]
  324. [TestCategory("PrivateKey")]
  325. public void ConstructorWithFileNameAndPassPhraseShouldBeAbleToReadFileThatIsSharedForReadAccess()
  326. {
  327. using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
  328. {
  329. SaveStreamToFile(stream, _temporaryFile);
  330. }
  331. using (var fs = File.Open(_temporaryFile, FileMode.Open, FileAccess.Read, FileShare.Read))
  332. {
  333. var privateKeyFile = new PrivateKeyFile(_temporaryFile, "12345");
  334. Assert.IsNotNull(privateKeyFile.HostKey);
  335. fs.Close();
  336. }
  337. }
  338. private void SaveStreamToFile(Stream stream, string fileName)
  339. {
  340. var buffer = new byte[4000];
  341. using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
  342. {
  343. var bytesRead = stream.Read(buffer, 0, buffer.Length);
  344. while (bytesRead > 0)
  345. {
  346. fs.Write(buffer, 0, bytesRead);
  347. bytesRead = stream.Read(buffer, 0, buffer.Length);
  348. }
  349. }
  350. }
  351. private string GetTempFileName()
  352. {
  353. var tempFile = Path.GetTempFileName();
  354. File.Delete(tempFile);
  355. return tempFile;
  356. }
  357. }
  358. }