| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504 |
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using Renci.SshNet.Common;
- using Renci.SshNet.Tests.Common;
- using System;
- using System.IO;
- namespace Renci.SshNet.Tests.Classes
- {
- /// <summary>
- /// old private key information/
- /// </summary>
- [TestClass]
- public class PrivateKeyFileTest : TestBase
- {
- private string _temporaryFile;
- [TestInitialize]
- public void SetUp()
- {
- _temporaryFile = GetTempFileName();
- }
- [TestCleanup]
- public void TearDown()
- {
- if (_temporaryFile != null)
- File.Delete(_temporaryFile);
- }
- /// <summary>
- /// A test for <see cref="PrivateKeyFile(string)"/> ctor.
- ///</summary>
- [WorkItem(703), TestMethod]
- public void ConstructorWithFileNameShouldThrowArgumentNullExceptionWhenFileNameIsEmpty()
- {
- var fileName = string.Empty;
- try
- {
- new PrivateKeyFile(fileName);
- Assert.Fail();
- }
- catch (ArgumentNullException ex)
- {
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual("fileName", ex.ParamName);
- }
- }
- /// <summary>
- /// A test for <see cref="PrivateKeyFile(string)"/> ctor.
- ///</summary>
- [WorkItem(703), TestMethod]
- public void ConstructorWithFileNameShouldThrowArgumentNullExceptionWhenFileNameIsNull()
- {
- var fileName = string.Empty;
- try
- {
- new PrivateKeyFile(fileName);
- Assert.Fail();
- }
- catch (ArgumentNullException ex)
- {
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual("fileName", ex.ParamName);
- }
- }
- /// <summary>
- /// A test for <see cref="PrivateKeyFile(string, string)"/> ctor.
- ///</summary>
- [WorkItem(703), TestMethod]
- public void ConstructorWithFileNameAndPassphraseShouldThrowArgumentNullExceptionWhenFileNameIsEmpty()
- {
- var fileName = string.Empty;
- try
- {
- new PrivateKeyFile(fileName, "12345");
- Assert.Fail();
- }
- catch (ArgumentNullException ex)
- {
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual("fileName", ex.ParamName);
- }
- }
- /// <summary>
- /// A test for <see cref="PrivateKeyFile(string, string)"/> ctor.
- ///</summary>
- [WorkItem(703), TestMethod]
- public void ConstructorWithFileNameAndPassphraseShouldThrowArgumentNullExceptionWhenFileNameIsNull()
- {
- var fileName = string.Empty;
- try
- {
- new PrivateKeyFile(fileName, "12345");
- Assert.Fail();
- }
- catch (ArgumentNullException ex)
- {
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual("fileName", ex.ParamName);
- }
- }
- [WorkItem(703), TestMethod]
- public void ConstructorWithPrivateKeyShouldThrowArgumentNullExceptionWhenPrivateKeyIsNull()
- {
- Stream privateKey = null;
- try
- {
- new PrivateKeyFile(privateKey);
- Assert.Fail();
- }
- catch (ArgumentNullException ex)
- {
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual("privateKey", ex.ParamName);
- }
- }
- [WorkItem(703), TestMethod]
- public void ConstructorWithPrivateKeyAndPassphraseShouldThrowArgumentNullExceptionWhenPrivateKeyIsNull()
- {
- Stream privateKey = null;
- try
- {
- new PrivateKeyFile(privateKey, "12345");
- Assert.Fail();
- }
- catch (ArgumentNullException ex)
- {
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual("privateKey", ex.ParamName);
- }
- }
- [TestMethod]
- [Owner("olegkap")]
- [TestCategory("PrivateKey")]
- public void Test_PrivateKey_RSA()
- {
- using (var stream = this.GetData("Key.RSA.txt"))
- {
- new PrivateKeyFile(stream);
- }
- }
- [TestMethod]
- [Owner("drieseng")]
- [TestCategory("PrivateKey")]
- public void Test_PrivateKey_SSH2_DSA()
- {
- using (var stream = this.GetData("Key.SSH2.DSA.txt"))
- {
- new PrivateKeyFile(stream);
- }
- }
- [TestMethod]
- [Owner("drieseng")]
- [TestCategory("PrivateKey")]
- public void Test_PrivateKey_SSH2_RSA()
- {
- using (var stream = this.GetData("Key.SSH2.RSA.txt"))
- {
- new PrivateKeyFile(stream);
- }
- }
- [TestMethod]
- [Owner("drieseng")]
- [TestCategory("PrivateKey")]
- public void Test_PrivateKey_SSH2_Encrypted_DSA_DES_CBC()
- {
- using (var stream = this.GetData("Key.SSH2.DSA.Encrypted.Des.CBC.12345.txt"))
- {
- new PrivateKeyFile(stream, "12345");
- }
- }
- [TestMethod]
- [Owner("drieseng")]
- [TestCategory("PrivateKey")]
- public void Test_PrivateKey_SSH2_Encrypted_RSA_DES_CBC()
- {
- using (var stream = this.GetData("Key.SSH2.RSA.Encrypted.Des.CBC.12345.txt"))
- {
- new PrivateKeyFile(stream, "12345");
- }
- }
- [TestMethod]
- [Owner("drieseng")]
- [TestCategory("PrivateKey")]
- public void Test_PrivateKey_SSH2_Encrypted_ShouldThrowSshExceptionWhenPassphraseIsWrong()
- {
- using (var stream = this.GetData("Key.SSH2.RSA.Encrypted.Des.CBC.12345.txt"))
- {
- try
- {
- new PrivateKeyFile(stream, "34567");
- Assert.Fail();
- }
- catch (SshException ex)
- {
- Assert.IsInstanceOfType(ex, typeof(SshException));
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual("Invalid passphrase.", ex.Message);
- }
- }
- }
- [TestMethod]
- [Owner("drieseng")]
- [TestCategory("PrivateKey")]
- public void Test_PrivateKey_SSH2_Encrypted_ShouldThrowSshPassPhraseNullOrEmptyExceptionWhenPassphraseIsNull()
- {
- using (var stream = this.GetData("Key.SSH2.RSA.Encrypted.Des.CBC.12345.txt"))
- {
- try
- {
- new PrivateKeyFile(stream, null);
- Assert.Fail();
- }
- catch (SshPassPhraseNullOrEmptyException ex)
- {
- Assert.IsInstanceOfType(ex, typeof(SshPassPhraseNullOrEmptyException));
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual("Private key is encrypted but passphrase is empty.", ex.Message);
- }
- }
- }
- [TestMethod]
- [Owner("drieseng")]
- [TestCategory("PrivateKey")]
- public void Test_PrivateKey_SSH2_Encrypted_ShouldThrowSshPassPhraseNullOrEmptyExceptionWhenPassphraseIsEmpty()
- {
- using (var stream = this.GetData("Key.SSH2.RSA.Encrypted.Des.CBC.12345.txt"))
- {
- try
- {
- new PrivateKeyFile(stream, string.Empty);
- Assert.Fail();
- }
- catch (SshPassPhraseNullOrEmptyException ex)
- {
- Assert.IsInstanceOfType(ex, typeof(SshPassPhraseNullOrEmptyException));
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual("Private key is encrypted but passphrase is empty.", ex.Message);
- }
- }
- }
- [TestMethod]
- [Owner("olegkap")]
- [TestCategory("PrivateKey")]
- public void Test_PrivateKey_RSA_DES_CBC()
- {
- using (var stream = this.GetData("Key.RSA.Encrypted.Des.CBC.12345.txt"))
- {
- new PrivateKeyFile(stream, "12345");
- }
- }
- [TestMethod]
- [Owner("olegkap")]
- [TestCategory("PrivateKey")]
- public void Test_PrivateKey_RSA_DES_EDE3_CBC()
- {
- using (var stream = this.GetData("Key.RSA.Encrypted.Des.Ede3.CBC.12345.txt"))
- {
- new PrivateKeyFile(stream, "12345");
- }
- }
- [TestMethod]
- [Owner("olegkap")]
- [TestCategory("PrivateKey")]
- public void Test_PrivateKey_RSA_AES_128_CBC()
- {
- using (var stream = this.GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
- {
- new PrivateKeyFile(stream, "12345");
- }
- }
- [TestMethod]
- [Owner("olegkap")]
- [TestCategory("PrivateKey")]
- public void Test_PrivateKey_RSA_AES_192_CBC()
- {
- using (var stream = this.GetData("Key.RSA.Encrypted.Aes.192.CBC.12345.txt"))
- {
- new PrivateKeyFile(stream, "12345");
- }
- }
- [TestMethod]
- [Owner("olegkap")]
- [TestCategory("PrivateKey")]
- public void Test_PrivateKey_RSA_AES_256_CBC()
- {
- using (var stream = this.GetData("Key.RSA.Encrypted.Aes.256.CBC.12345.txt"))
- {
- new PrivateKeyFile(stream, "12345");
- }
- }
- [TestMethod]
- [Owner("olegkap")]
- [TestCategory("PrivateKey")]
- public void Test_PrivateKey_RSA_DES_EDE3_CFB()
- {
- using (var stream = this.GetData("Key.RSA.Encrypted.Des.Ede3.CFB.1234567890.txt"))
- {
- new PrivateKeyFile(stream, "1234567890");
- }
- }
- /// <summary>
- ///A test for Dispose
- ///</summary>
- [TestMethod()]
- public void DisposeTest()
- {
- using (var privateKeyStream = GetData("Key.RSA.txt"))
- {
- var target = new PrivateKeyFile(privateKeyStream);
- target.Dispose();
- }
- }
- /// <summary>
- /// A test for <see cref="PrivateKeyFile(Stream, string)"/> ctor.
- ///</summary>
- [TestMethod()]
- public void ConstructorWithStreamAndPassphrase()
- {
- using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
- {
- var privateKeyFile = new PrivateKeyFile(stream, "12345");
- Assert.IsNotNull(privateKeyFile.HostKey);
- }
- }
- /// <summary>
- /// A test for <see cref="PrivateKeyFile(string, string)"/> ctor.
- ///</summary>
- [TestMethod()]
- public void ConstructorWithFileNameAndPassphrase()
- {
- using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
- {
- SaveStreamToFile(stream, _temporaryFile);
- }
- using (var fs = File.Open(_temporaryFile, FileMode.Open, FileAccess.Read, FileShare.Read))
- {
- var privateKeyFile = new PrivateKeyFile(_temporaryFile, "12345");
- Assert.IsNotNull(privateKeyFile.HostKey);
- fs.Close();
- }
- }
- /// <summary>
- /// A test for <see cref="PrivateKeyFile(string, string)"/> ctor.
- ///</summary>
- [TestMethod()]
- public void ConstructorWithFileNameAndPassphraseShouldThrowSshPassPhraseNullOrEmptyExceptionWhenPrivateKeyIsEncryptedAndPassphraseIsEmpty()
- {
- var passphrase = string.Empty;
- using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
- {
- SaveStreamToFile(stream, _temporaryFile);
- }
- try
- {
- new PrivateKeyFile(_temporaryFile, passphrase);
- Assert.Fail();
- }
- catch (SshPassPhraseNullOrEmptyException ex)
- {
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual("Private key is encrypted but passphrase is empty.", ex.Message);
- }
- }
- /// <summary>
- /// A test for <see cref="PrivateKeyFile(string, string)"/> ctor.
- ///</summary>
- [TestMethod()]
- public void ConstructorWithFileNameAndPassphraseShouldThrowSshPassPhraseNullOrEmptyExceptionWhenPrivateKeyIsEncryptedAndPassphraseIsNull()
- {
- string passphrase = null;
- using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
- {
- SaveStreamToFile(stream, _temporaryFile);
- }
- try
- {
- new PrivateKeyFile(_temporaryFile, passphrase);
- Assert.Fail();
- }
- catch (SshPassPhraseNullOrEmptyException ex)
- {
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual("Private key is encrypted but passphrase is empty.", ex.Message);
- }
- }
- /// <summary>
- /// A test for <see cref="PrivateKeyFile(string)"/> ctor.
- ///</summary>
- [TestMethod()]
- public void ConstructorWithFileName()
- {
- using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
- {
- SaveStreamToFile(stream, _temporaryFile);
- }
- var privateKeyFile = new PrivateKeyFile(_temporaryFile, "12345");
- Assert.IsNotNull(privateKeyFile.HostKey);
- }
- /// <summary>
- /// A test for <see cref="PrivateKeyFile(Stream)"/> ctor.
- ///</summary>
- [TestMethod()]
- public void ConstructorWithStream()
- {
- using (var stream = GetData("Key.RSA.txt"))
- {
- var privateKeyFile = new PrivateKeyFile(stream);
- Assert.IsNotNull(privateKeyFile.HostKey);
- }
- }
- [TestMethod]
- [TestCategory("PrivateKey")]
- public void ConstructorWithFileNameShouldBeAbleToReadFileThatIsSharedForReadAccess()
- {
- using (var stream = GetData("Key.RSA.txt"))
- {
- SaveStreamToFile(stream, _temporaryFile);
- }
- using (var fs = File.Open(_temporaryFile, FileMode.Open, FileAccess.Read, FileShare.Read))
- {
- var privateKeyFile = new PrivateKeyFile(_temporaryFile);
- Assert.IsNotNull(privateKeyFile.HostKey);
- fs.Close();
- }
- }
- [TestMethod]
- [TestCategory("PrivateKey")]
- public void ConstructorWithFileNameAndPassPhraseShouldBeAbleToReadFileThatIsSharedForReadAccess()
- {
- using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
- {
- SaveStreamToFile(stream, _temporaryFile);
- }
- using (var fs = File.Open(_temporaryFile, FileMode.Open, FileAccess.Read, FileShare.Read))
- {
- var privateKeyFile = new PrivateKeyFile(_temporaryFile, "12345");
- Assert.IsNotNull(privateKeyFile.HostKey);
- fs.Close();
- }
- }
- private void SaveStreamToFile(Stream stream, string fileName)
- {
- var buffer = new byte[4000];
- using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
- {
- var bytesRead = stream.Read(buffer, 0, buffer.Length);
- while (bytesRead > 0)
- {
- fs.Write(buffer, 0, bytesRead);
- bytesRead = stream.Read(buffer, 0, buffer.Length);
- }
- }
- }
- private string GetTempFileName()
- {
- var tempFile = Path.GetTempFileName();
- File.Delete(tempFile);
- return tempFile;
- }
- }
- }
|