HostKeyEventArgsTest.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Renci.SshNet.Common;
  3. using Renci.SshNet.Security;
  4. using Renci.SshNet.Tests.Common;
  5. using System.Linq;
  6. using System.Reflection;
  7. namespace Renci.SshNet.Tests.Classes.Common
  8. {
  9. /// <summary>
  10. ///This is a test class for HostKeyEventArgsTest and is intended
  11. ///to contain all HostKeyEventArgsTest Unit Tests
  12. ///</summary>
  13. [TestClass]
  14. public class HostKeyEventArgsTest : TestBase
  15. {
  16. /// <summary>
  17. ///A test for HostKeyEventArgs Constructor
  18. ///</summary>
  19. [TestMethod]
  20. public void HostKeyEventArgsConstructorTest()
  21. {
  22. HostKeyEventArgs target = new HostKeyEventArgs(GetKeyHostAlgorithm());
  23. Assert.IsTrue(target.CanTrust);
  24. Assert.IsTrue(new byte[] {
  25. 0x00, 0x00, 0x00, 0x07, 0x73, 0x73, 0x68, 0x2d, 0x72, 0x73, 0x61, 0x00, 0x00, 0x00, 0x01, 0x23,
  26. 0x00, 0x00, 0x01, 0x01, 0x00, 0xb9, 0x3b, 0x57, 0x9f, 0xe0, 0x5a, 0xb5, 0x7d, 0x68, 0x26, 0xeb,
  27. 0xe1, 0xa9, 0xf2, 0x59, 0xc3, 0x98, 0xdc, 0xfe, 0x97, 0x08, 0xc4, 0x95, 0x0f, 0x9a, 0xea, 0x05,
  28. 0x08, 0x7d, 0xfe, 0x6d, 0x77, 0xca, 0x04, 0x9f, 0xfd, 0xe2, 0x2c, 0x4d, 0x11, 0x3c, 0xd9, 0x05,
  29. 0xab, 0x32, 0xbd, 0x3f, 0xe8, 0xcd, 0xba, 0x00, 0x6c, 0x21, 0xb7, 0xa9, 0xc2, 0x4e, 0x63, 0x17,
  30. 0xf6, 0x04, 0x47, 0x93, 0x00, 0x85, 0xde, 0xd6, 0x32, 0xc0, 0xa1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x8e, 0x32, 0x8e, 0x61, 0xfc,
  42. 0x5b, 0xbd, 0xd4, 0x46, 0xe1
  43. }.SequenceEqual(target.HostKey));
  44. Assert.AreEqual("rsa-sha2-512", target.HostKeyName);
  45. Assert.AreEqual(2048, target.KeyLength);
  46. }
  47. /// <summary>
  48. ///A test for MD5 calculation in HostKeyEventArgs Constructor
  49. ///</summary>
  50. [TestMethod]
  51. public void HostKeyEventArgsConstructorTest_VerifyMD5()
  52. {
  53. HostKeyEventArgs target = new HostKeyEventArgs(GetKeyHostAlgorithm());
  54. Assert.IsTrue(new byte[] {
  55. 0x92, 0xea, 0x54, 0xa1, 0x01, 0xf9, 0x95, 0x9c, 0x71, 0xd9, 0xbb, 0x51, 0xb2, 0x55, 0xf8, 0xd9
  56. }.SequenceEqual(target.FingerPrint));
  57. Assert.AreEqual("92:ea:54:a1:01:f9:95:9c:71:d9:bb:51:b2:55:f8:d9", target.FingerPrintMD5);
  58. }
  59. /// <summary>
  60. ///A test for SHA256 calculation in HostKeyEventArgs Constructor
  61. ///</summary>
  62. [TestMethod]
  63. public void HostKeyEventArgsConstructorTest_VerifySHA256()
  64. {
  65. HostKeyEventArgs target = new HostKeyEventArgs(GetKeyHostAlgorithm());
  66. Assert.AreEqual("93LkmoWksp9ytNVZIPXi9KJU1uvlC9clZ/CkUHf6uEE", target.FingerPrintSHA256);
  67. }
  68. /// <summary>
  69. ///A test for CanTrust
  70. ///</summary>
  71. [TestMethod]
  72. public void CanTrustTest()
  73. {
  74. HostKeyEventArgs target = new HostKeyEventArgs(GetKeyHostAlgorithm());
  75. bool expected = false;
  76. bool actual;
  77. target.CanTrust = expected;
  78. actual = target.CanTrust;
  79. Assert.AreEqual(expected, actual);
  80. }
  81. private static KeyHostAlgorithm GetKeyHostAlgorithm()
  82. {
  83. var executingAssembly = Assembly.GetExecutingAssembly();
  84. using (var s = executingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt")))
  85. {
  86. var privateKey = new PrivateKeyFile(s);
  87. return (KeyHostAlgorithm)privateKey.HostKeyAlgorithms.First();
  88. }
  89. }
  90. }
  91. }