PrivateKeyAuthenticationTests.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Renci.SshNet.IntegrationTests.Common;
  2. using Renci.SshNet.TestTools.OpenSSH;
  3. namespace Renci.SshNet.IntegrationTests
  4. {
  5. [TestClass]
  6. public class PrivateKeyAuthenticationTests : TestBase
  7. {
  8. private IConnectionInfoFactory _connectionInfoFactory;
  9. private RemoteSshdConfig _remoteSshdConfig;
  10. [TestInitialize]
  11. public void SetUp()
  12. {
  13. _connectionInfoFactory = new LinuxVMConnectionFactory(SshServerHostName, SshServerPort);
  14. _remoteSshdConfig = new RemoteSshd(new LinuxAdminConnectionFactory(SshServerHostName, SshServerPort)).OpenConfig();
  15. }
  16. [TestCleanup]
  17. public void TearDown()
  18. {
  19. _remoteSshdConfig?.Reset();
  20. }
  21. [TestMethod]
  22. public void SshDss()
  23. {
  24. DoTest(PublicKeyAlgorithm.SshDss, "Data.Key.SSH2.DSA.Encrypted.Des.CBC.12345.txt", "12345");
  25. }
  26. [TestMethod]
  27. public void SshRsa()
  28. {
  29. DoTest(PublicKeyAlgorithm.SshRsa, "Data.Key.RSA.txt");
  30. }
  31. [TestMethod]
  32. public void SshRsaSha256()
  33. {
  34. DoTest(PublicKeyAlgorithm.RsaSha2256, "Data.Key.RSA.txt");
  35. }
  36. [TestMethod]
  37. public void SshRsaSha512()
  38. {
  39. DoTest(PublicKeyAlgorithm.RsaSha2512, "Data.Key.RSA.txt");
  40. }
  41. [TestMethod]
  42. public void Ecdsa256()
  43. {
  44. DoTest(PublicKeyAlgorithm.EcdsaSha2Nistp256, "Data.Key.ECDSA.Encrypted.txt", "12345");
  45. }
  46. [TestMethod]
  47. public void Ecdsa384()
  48. {
  49. DoTest(PublicKeyAlgorithm.EcdsaSha2Nistp384, "Data.Key.OPENSSH.ECDSA384.Encrypted.txt", "12345");
  50. }
  51. [TestMethod]
  52. public void Ecdsa521()
  53. {
  54. DoTest(PublicKeyAlgorithm.EcdsaSha2Nistp521, "Data.Key.OPENSSH.ECDSA521.Encrypted.txt", "12345");
  55. }
  56. [TestMethod]
  57. public void Ed25519()
  58. {
  59. DoTest(PublicKeyAlgorithm.SshEd25519, "Data.Key.OPENSSH.ED25519.Encrypted.txt", "12345");
  60. }
  61. private void DoTest(PublicKeyAlgorithm publicKeyAlgorithm, string keyResource, string passPhrase = null)
  62. {
  63. _remoteSshdConfig.ClearPublicKeyAcceptedAlgorithms()
  64. .AddPublicKeyAcceptedAlgorithm(publicKeyAlgorithm)
  65. .Update()
  66. .Restart();
  67. var connectionInfo = _connectionInfoFactory.Create(CreatePrivateKeyAuthenticationMethod(keyResource, passPhrase));
  68. using (var client = new SshClient(connectionInfo))
  69. {
  70. client.Connect();
  71. }
  72. }
  73. private static PrivateKeyAuthenticationMethod CreatePrivateKeyAuthenticationMethod(string keyResource, string passPhrase)
  74. {
  75. using (var stream = GetData(keyResource))
  76. {
  77. return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, new PrivateKeyFile(stream, passPhrase));
  78. }
  79. }
  80. }
  81. }