PrivateKeyFileTest.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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("drieseng")]
  140. [TestCategory("PrivateKey")]
  141. public void Test_PrivateKey_SSH2_DSA()
  142. {
  143. using (var stream = this.GetData("Key.SSH2.DSA.txt"))
  144. {
  145. new PrivateKeyFile(stream);
  146. }
  147. }
  148. [TestMethod]
  149. [Owner("drieseng")]
  150. [TestCategory("PrivateKey")]
  151. public void Test_PrivateKey_SSH2_RSA()
  152. {
  153. using (var stream = this.GetData("Key.SSH2.RSA.txt"))
  154. {
  155. new PrivateKeyFile(stream);
  156. }
  157. }
  158. [TestMethod]
  159. [Owner("drieseng")]
  160. [TestCategory("PrivateKey")]
  161. public void Test_PrivateKey_SSH2_Encrypted_DSA_DES_CBC()
  162. {
  163. using (var stream = this.GetData("Key.SSH2.DSA.Encrypted.Des.CBC.12345.txt"))
  164. {
  165. new PrivateKeyFile(stream, "12345");
  166. }
  167. }
  168. [TestMethod]
  169. [Owner("drieseng")]
  170. [TestCategory("PrivateKey")]
  171. public void Test_PrivateKey_SSH2_Encrypted_RSA_DES_CBC()
  172. {
  173. using (var stream = this.GetData("Key.SSH2.RSA.Encrypted.Des.CBC.12345.txt"))
  174. {
  175. new PrivateKeyFile(stream, "12345");
  176. }
  177. }
  178. [TestMethod]
  179. [Owner("drieseng")]
  180. [TestCategory("PrivateKey")]
  181. public void Test_PrivateKey_SSH2_Encrypted_ShouldThrowSshExceptionWhenPassphraseIsWrong()
  182. {
  183. using (var stream = this.GetData("Key.SSH2.RSA.Encrypted.Des.CBC.12345.txt"))
  184. {
  185. try
  186. {
  187. new PrivateKeyFile(stream, "34567");
  188. Assert.Fail();
  189. }
  190. catch (SshException ex)
  191. {
  192. Assert.IsInstanceOfType(ex, typeof(SshException));
  193. Assert.IsNull(ex.InnerException);
  194. Assert.AreEqual("Invalid passphrase.", ex.Message);
  195. }
  196. }
  197. }
  198. [TestMethod]
  199. [Owner("drieseng")]
  200. [TestCategory("PrivateKey")]
  201. public void Test_PrivateKey_SSH2_Encrypted_ShouldThrowSshPassPhraseNullOrEmptyExceptionWhenPassphraseIsNull()
  202. {
  203. using (var stream = this.GetData("Key.SSH2.RSA.Encrypted.Des.CBC.12345.txt"))
  204. {
  205. try
  206. {
  207. new PrivateKeyFile(stream, null);
  208. Assert.Fail();
  209. }
  210. catch (SshPassPhraseNullOrEmptyException ex)
  211. {
  212. Assert.IsInstanceOfType(ex, typeof(SshPassPhraseNullOrEmptyException));
  213. Assert.IsNull(ex.InnerException);
  214. Assert.AreEqual("Private key is encrypted but passphrase is empty.", ex.Message);
  215. }
  216. }
  217. }
  218. [TestMethod]
  219. [Owner("drieseng")]
  220. [TestCategory("PrivateKey")]
  221. public void Test_PrivateKey_SSH2_Encrypted_ShouldThrowSshPassPhraseNullOrEmptyExceptionWhenPassphraseIsEmpty()
  222. {
  223. using (var stream = this.GetData("Key.SSH2.RSA.Encrypted.Des.CBC.12345.txt"))
  224. {
  225. try
  226. {
  227. new PrivateKeyFile(stream, string.Empty);
  228. Assert.Fail();
  229. }
  230. catch (SshPassPhraseNullOrEmptyException ex)
  231. {
  232. Assert.IsInstanceOfType(ex, typeof(SshPassPhraseNullOrEmptyException));
  233. Assert.IsNull(ex.InnerException);
  234. Assert.AreEqual("Private key is encrypted but passphrase is empty.", ex.Message);
  235. }
  236. }
  237. }
  238. [TestMethod]
  239. [Owner("olegkap")]
  240. [TestCategory("PrivateKey")]
  241. public void Test_PrivateKey_RSA_DES_CBC()
  242. {
  243. using (var stream = this.GetData("Key.RSA.Encrypted.Des.CBC.12345.txt"))
  244. {
  245. new PrivateKeyFile(stream, "12345");
  246. }
  247. }
  248. [TestMethod]
  249. [Owner("olegkap")]
  250. [TestCategory("PrivateKey")]
  251. public void Test_PrivateKey_RSA_DES_EDE3_CBC()
  252. {
  253. using (var stream = this.GetData("Key.RSA.Encrypted.Des.Ede3.CBC.12345.txt"))
  254. {
  255. new PrivateKeyFile(stream, "12345");
  256. }
  257. }
  258. [TestMethod]
  259. [Owner("olegkap")]
  260. [TestCategory("PrivateKey")]
  261. public void Test_PrivateKey_RSA_AES_128_CBC()
  262. {
  263. using (var stream = this.GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
  264. {
  265. new PrivateKeyFile(stream, "12345");
  266. }
  267. }
  268. [TestMethod]
  269. [Owner("olegkap")]
  270. [TestCategory("PrivateKey")]
  271. public void Test_PrivateKey_RSA_AES_192_CBC()
  272. {
  273. using (var stream = this.GetData("Key.RSA.Encrypted.Aes.192.CBC.12345.txt"))
  274. {
  275. new PrivateKeyFile(stream, "12345");
  276. }
  277. }
  278. [TestMethod]
  279. [Owner("olegkap")]
  280. [TestCategory("PrivateKey")]
  281. public void Test_PrivateKey_RSA_AES_256_CBC()
  282. {
  283. using (var stream = this.GetData("Key.RSA.Encrypted.Aes.256.CBC.12345.txt"))
  284. {
  285. new PrivateKeyFile(stream, "12345");
  286. }
  287. }
  288. [TestMethod]
  289. [Owner("olegkap")]
  290. [TestCategory("PrivateKey")]
  291. public void Test_PrivateKey_RSA_DES_EDE3_CFB()
  292. {
  293. using (var stream = this.GetData("Key.RSA.Encrypted.Des.Ede3.CFB.1234567890.txt"))
  294. {
  295. new PrivateKeyFile(stream, "1234567890");
  296. }
  297. }
  298. /// <summary>
  299. ///A test for Dispose
  300. ///</summary>
  301. [TestMethod()]
  302. public void DisposeTest()
  303. {
  304. using (var privateKeyStream = GetData("Key.RSA.txt"))
  305. {
  306. var target = new PrivateKeyFile(privateKeyStream);
  307. target.Dispose();
  308. }
  309. }
  310. /// <summary>
  311. /// A test for <see cref="PrivateKeyFile(Stream, string)"/> ctor.
  312. ///</summary>
  313. [TestMethod()]
  314. public void ConstructorWithStreamAndPassphrase()
  315. {
  316. using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
  317. {
  318. var privateKeyFile = new PrivateKeyFile(stream, "12345");
  319. Assert.IsNotNull(privateKeyFile.HostKey);
  320. }
  321. }
  322. /// <summary>
  323. /// A test for <see cref="PrivateKeyFile(string, string)"/> ctor.
  324. ///</summary>
  325. [TestMethod()]
  326. public void ConstructorWithFileNameAndPassphrase()
  327. {
  328. using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
  329. {
  330. SaveStreamToFile(stream, _temporaryFile);
  331. }
  332. using (var fs = File.Open(_temporaryFile, FileMode.Open, FileAccess.Read, FileShare.Read))
  333. {
  334. var privateKeyFile = new PrivateKeyFile(_temporaryFile, "12345");
  335. Assert.IsNotNull(privateKeyFile.HostKey);
  336. fs.Close();
  337. }
  338. }
  339. /// <summary>
  340. /// A test for <see cref="PrivateKeyFile(string, string)"/> ctor.
  341. ///</summary>
  342. [TestMethod()]
  343. public void ConstructorWithFileNameAndPassphraseShouldThrowSshPassPhraseNullOrEmptyExceptionWhenPrivateKeyIsEncryptedAndPassphraseIsEmpty()
  344. {
  345. var passphrase = string.Empty;
  346. using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
  347. {
  348. SaveStreamToFile(stream, _temporaryFile);
  349. }
  350. try
  351. {
  352. new PrivateKeyFile(_temporaryFile, passphrase);
  353. Assert.Fail();
  354. }
  355. catch (SshPassPhraseNullOrEmptyException ex)
  356. {
  357. Assert.IsNull(ex.InnerException);
  358. Assert.AreEqual("Private key is encrypted but passphrase is empty.", ex.Message);
  359. }
  360. }
  361. /// <summary>
  362. /// A test for <see cref="PrivateKeyFile(string, string)"/> ctor.
  363. ///</summary>
  364. [TestMethod()]
  365. public void ConstructorWithFileNameAndPassphraseShouldThrowSshPassPhraseNullOrEmptyExceptionWhenPrivateKeyIsEncryptedAndPassphraseIsNull()
  366. {
  367. string passphrase = null;
  368. using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
  369. {
  370. SaveStreamToFile(stream, _temporaryFile);
  371. }
  372. try
  373. {
  374. new PrivateKeyFile(_temporaryFile, passphrase);
  375. Assert.Fail();
  376. }
  377. catch (SshPassPhraseNullOrEmptyException ex)
  378. {
  379. Assert.IsNull(ex.InnerException);
  380. Assert.AreEqual("Private key is encrypted but passphrase is empty.", ex.Message);
  381. }
  382. }
  383. /// <summary>
  384. /// A test for <see cref="PrivateKeyFile(string)"/> ctor.
  385. ///</summary>
  386. [TestMethod()]
  387. public void ConstructorWithFileName()
  388. {
  389. using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
  390. {
  391. SaveStreamToFile(stream, _temporaryFile);
  392. }
  393. var privateKeyFile = new PrivateKeyFile(_temporaryFile, "12345");
  394. Assert.IsNotNull(privateKeyFile.HostKey);
  395. }
  396. /// <summary>
  397. /// A test for <see cref="PrivateKeyFile(Stream)"/> ctor.
  398. ///</summary>
  399. [TestMethod()]
  400. public void ConstructorWithStream()
  401. {
  402. using (var stream = GetData("Key.RSA.txt"))
  403. {
  404. var privateKeyFile = new PrivateKeyFile(stream);
  405. Assert.IsNotNull(privateKeyFile.HostKey);
  406. }
  407. }
  408. [TestMethod]
  409. [TestCategory("PrivateKey")]
  410. public void ConstructorWithFileNameShouldBeAbleToReadFileThatIsSharedForReadAccess()
  411. {
  412. using (var stream = GetData("Key.RSA.txt"))
  413. {
  414. SaveStreamToFile(stream, _temporaryFile);
  415. }
  416. using (var fs = File.Open(_temporaryFile, FileMode.Open, FileAccess.Read, FileShare.Read))
  417. {
  418. var privateKeyFile = new PrivateKeyFile(_temporaryFile);
  419. Assert.IsNotNull(privateKeyFile.HostKey);
  420. fs.Close();
  421. }
  422. }
  423. [TestMethod]
  424. [TestCategory("PrivateKey")]
  425. public void ConstructorWithFileNameAndPassPhraseShouldBeAbleToReadFileThatIsSharedForReadAccess()
  426. {
  427. using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
  428. {
  429. SaveStreamToFile(stream, _temporaryFile);
  430. }
  431. using (var fs = File.Open(_temporaryFile, FileMode.Open, FileAccess.Read, FileShare.Read))
  432. {
  433. var privateKeyFile = new PrivateKeyFile(_temporaryFile, "12345");
  434. Assert.IsNotNull(privateKeyFile.HostKey);
  435. fs.Close();
  436. }
  437. }
  438. private void SaveStreamToFile(Stream stream, string fileName)
  439. {
  440. var buffer = new byte[4000];
  441. using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
  442. {
  443. var bytesRead = stream.Read(buffer, 0, buffer.Length);
  444. while (bytesRead > 0)
  445. {
  446. fs.Write(buffer, 0, bytesRead);
  447. bytesRead = stream.Read(buffer, 0, buffer.Length);
  448. }
  449. }
  450. }
  451. private string GetTempFileName()
  452. {
  453. var tempFile = Path.GetTempFileName();
  454. File.Delete(tempFile);
  455. return tempFile;
  456. }
  457. }
  458. }