2
0

HostKeyAlgorithmTests.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using Renci.SshNet.Common;
  2. using Renci.SshNet.IntegrationTests.Common;
  3. using Renci.SshNet.TestTools.OpenSSH;
  4. namespace Renci.SshNet.IntegrationTests
  5. {
  6. [TestClass]
  7. public class HostKeyAlgorithmTests : IntegrationTestBase
  8. {
  9. private IConnectionInfoFactory _connectionInfoFactory;
  10. private RemoteSshdConfig _remoteSshdConfig;
  11. [TestInitialize]
  12. public void SetUp()
  13. {
  14. _connectionInfoFactory = new LinuxVMConnectionFactory(SshServerHostName, SshServerPort);
  15. _remoteSshdConfig = new RemoteSshd(new LinuxAdminConnectionFactory(SshServerHostName, SshServerPort)).OpenConfig();
  16. }
  17. [TestCleanup]
  18. public void TearDown()
  19. {
  20. _remoteSshdConfig?.Reset();
  21. }
  22. [TestMethod]
  23. public void SshDss()
  24. {
  25. DoTest(HostKeyAlgorithm.SshDss, HostKeyFile.Dsa);
  26. }
  27. [TestMethod]
  28. public void SshRsa()
  29. {
  30. DoTest(HostKeyAlgorithm.SshRsa, HostKeyFile.Rsa);
  31. }
  32. [TestMethod]
  33. public void SshRsaSha256()
  34. {
  35. DoTest(HostKeyAlgorithm.RsaSha2256, HostKeyFile.Rsa);
  36. }
  37. [TestMethod]
  38. public void SshRsaSha512()
  39. {
  40. DoTest(HostKeyAlgorithm.RsaSha2512, HostKeyFile.Rsa);
  41. }
  42. [TestMethod]
  43. public void SshEd25519()
  44. {
  45. DoTest(HostKeyAlgorithm.SshEd25519, HostKeyFile.Ed25519);
  46. }
  47. [TestMethod]
  48. public void Ecdsa256()
  49. {
  50. DoTest(HostKeyAlgorithm.EcdsaSha2Nistp256, HostKeyFile.Ecdsa256);
  51. }
  52. [TestMethod]
  53. public void Ecdsa384()
  54. {
  55. DoTest(HostKeyAlgorithm.EcdsaSha2Nistp384, HostKeyFile.Ecdsa384);
  56. }
  57. [TestMethod]
  58. public void Ecdsa521()
  59. {
  60. DoTest(HostKeyAlgorithm.EcdsaSha2Nistp521, HostKeyFile.Ecdsa521);
  61. }
  62. private void DoTest(HostKeyAlgorithm hostKeyAlgorithm, HostKeyFile hostKeyFile)
  63. {
  64. _remoteSshdConfig.ClearHostKeyAlgorithms()
  65. .AddHostKeyAlgorithm(hostKeyAlgorithm)
  66. .ClearHostKeyFiles()
  67. .AddHostKeyFile(hostKeyFile.FilePath)
  68. .Update()
  69. .Restart();
  70. HostKeyEventArgs hostKeyEventsArgs = null;
  71. using (var client = new SshClient(_connectionInfoFactory.Create()))
  72. {
  73. client.HostKeyReceived += (sender, e) => hostKeyEventsArgs = e;
  74. client.Connect();
  75. client.Disconnect();
  76. }
  77. Assert.IsNotNull(hostKeyEventsArgs);
  78. Assert.AreEqual(hostKeyAlgorithm.Name, hostKeyEventsArgs.HostKeyName);
  79. Assert.AreEqual(hostKeyFile.KeyLength, hostKeyEventsArgs.KeyLength);
  80. CollectionAssert.AreEqual(hostKeyFile.FingerPrint, hostKeyEventsArgs.FingerPrint);
  81. }
  82. }
  83. }