PrivateKeyAuthenticationTests.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 Ecdsa256()
  23. {
  24. _remoteSshdConfig.AddPublicKeyAcceptedAlgorithms(PublicKeyAlgorithm.EcdsaSha2Nistp256)
  25. .Update()
  26. .Restart();
  27. var connectionInfo = _connectionInfoFactory.Create(CreatePrivateKeyAuthenticationMethod("key_ecdsa_256_openssh"));
  28. using (var client = new SshClient(connectionInfo))
  29. {
  30. client.Connect();
  31. }
  32. }
  33. [TestMethod]
  34. public void Ecdsa384()
  35. {
  36. _remoteSshdConfig.AddPublicKeyAcceptedAlgorithms(PublicKeyAlgorithm.EcdsaSha2Nistp384)
  37. .Update()
  38. .Restart();
  39. var connectionInfo = _connectionInfoFactory.Create(CreatePrivateKeyAuthenticationMethod("key_ecdsa_384_openssh"));
  40. using (var client = new SshClient(connectionInfo))
  41. {
  42. client.Connect();
  43. }
  44. }
  45. [TestMethod]
  46. public void EcdsaA521()
  47. {
  48. _remoteSshdConfig.AddPublicKeyAcceptedAlgorithms(PublicKeyAlgorithm.EcdsaSha2Nistp521)
  49. .Update()
  50. .Restart();
  51. var connectionInfo = _connectionInfoFactory.Create(CreatePrivateKeyAuthenticationMethod("key_ecdsa_521_openssh"));
  52. using (var client = new SshClient(connectionInfo))
  53. {
  54. client.Connect();
  55. }
  56. }
  57. private PrivateKeyAuthenticationMethod CreatePrivateKeyAuthenticationMethod(string keyResource)
  58. {
  59. var privateKey = CreatePrivateKeyFromManifestResource("Renci.SshNet.IntegrationTests.resources.client." + keyResource);
  60. return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKey);
  61. }
  62. private PrivateKeyFile CreatePrivateKeyFromManifestResource(string resourceName)
  63. {
  64. using (var stream = GetManifestResourceStream(resourceName))
  65. {
  66. return new PrivateKeyFile(stream);
  67. }
  68. }
  69. }
  70. }