HostKeyAlgorithmTests.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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, 2048);
  26. }
  27. [TestMethod]
  28. public void SshRsa()
  29. {
  30. DoTest(HostKeyAlgorithm.SshRsa, HostKeyFile.Rsa, 3072);
  31. }
  32. [TestMethod]
  33. public void SshRsaSha256()
  34. {
  35. DoTest(HostKeyAlgorithm.RsaSha2256, HostKeyFile.Rsa, 3072);
  36. }
  37. [TestMethod]
  38. public void SshRsaSha512()
  39. {
  40. DoTest(HostKeyAlgorithm.RsaSha2512, HostKeyFile.Rsa, 3072);
  41. }
  42. [TestMethod]
  43. public void SshEd25519()
  44. {
  45. DoTest(HostKeyAlgorithm.SshEd25519, HostKeyFile.Ed25519, 256);
  46. }
  47. private void DoTest(HostKeyAlgorithm hostKeyAlgorithm, HostKeyFile hostKeyFile, int keyLength)
  48. {
  49. _remoteSshdConfig.ClearHostKeyAlgorithms()
  50. .AddHostKeyAlgorithm(hostKeyAlgorithm)
  51. .ClearHostKeyFiles()
  52. .AddHostKeyFile(hostKeyFile.FilePath)
  53. .Update()
  54. .Restart();
  55. HostKeyEventArgs hostKeyEventsArgs = null;
  56. using (var client = new SshClient(_connectionInfoFactory.Create()))
  57. {
  58. client.HostKeyReceived += (sender, e) => hostKeyEventsArgs = e;
  59. client.Connect();
  60. client.Disconnect();
  61. }
  62. Assert.IsNotNull(hostKeyEventsArgs);
  63. Assert.AreEqual(hostKeyAlgorithm.Name, hostKeyEventsArgs.HostKeyName);
  64. Assert.AreEqual(keyLength, hostKeyEventsArgs.KeyLength);
  65. CollectionAssert.AreEqual(hostKeyFile.FingerPrint, hostKeyEventsArgs.FingerPrint);
  66. }
  67. }
  68. }