HMacTest.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Renci.SshNet.Security.Cryptography;
  3. using Renci.SshNet.Tests.Common;
  4. using Renci.SshNet.Tests.Properties;
  5. using System.Linq;
  6. using System.Security.Cryptography;
  7. namespace Renci.SshNet.Tests.Classes.Security.Cryptography
  8. {
  9. /// <summary>
  10. /// Provides HMAC algorithm implementation.
  11. /// </summary>
  12. /// <typeparam name="T"></typeparam>
  13. [TestClass]
  14. public class HMacTest : TestBase
  15. {
  16. [TestMethod]
  17. [TestCategory("integration")]
  18. public void Test_HMac_MD5_Connection()
  19. {
  20. var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
  21. connectionInfo.HmacAlgorithms.Clear();
  22. connectionInfo.HmacAlgorithms.Add("hmac-md5", new HashInfo(16 * 8, (key) => { return new HMac<MD5Hash>(key); }));
  23. using (var client = new SshClient(connectionInfo))
  24. {
  25. client.Connect();
  26. client.Disconnect();
  27. }
  28. }
  29. [TestMethod]
  30. [TestCategory("integration")]
  31. public void Test_HMac_Sha1_Connection()
  32. {
  33. var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
  34. connectionInfo.HmacAlgorithms.Clear();
  35. connectionInfo.HmacAlgorithms.Add("hmac-sha1", new HashInfo(20 * 8, (key) => { return new HMac<SHA1Hash>(key); }));
  36. using (var client = new SshClient(connectionInfo))
  37. {
  38. client.Connect();
  39. client.Disconnect();
  40. }
  41. }
  42. [TestMethod]
  43. [TestCategory("integration")]
  44. public void Test_HMac_MD5_96_Connection()
  45. {
  46. var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
  47. connectionInfo.HmacAlgorithms.Clear();
  48. connectionInfo.HmacAlgorithms.Add("hmac-md5", new HashInfo(16 * 8, (key) => { return new HMac<MD5Hash>(key, 96); }));
  49. using (var client = new SshClient(connectionInfo))
  50. {
  51. client.Connect();
  52. client.Disconnect();
  53. }
  54. }
  55. [TestMethod]
  56. [TestCategory("integration")]
  57. public void Test_HMac_Sha1_96_Connection()
  58. {
  59. var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
  60. connectionInfo.HmacAlgorithms.Clear();
  61. connectionInfo.HmacAlgorithms.Add("hmac-sha1", new HashInfo(20 * 8, (key) => { return new HMac<SHA1Hash>(key, 96); }));
  62. using (var client = new SshClient(connectionInfo))
  63. {
  64. client.Connect();
  65. client.Disconnect();
  66. }
  67. }
  68. [TestMethod]
  69. [TestCategory("integration")]
  70. public void Test_HMac_Sha256_Connection()
  71. {
  72. var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
  73. connectionInfo.HmacAlgorithms.Clear();
  74. connectionInfo.HmacAlgorithms.Add("hmac-sha2-256", new HashInfo(32 * 8, (key) => { return new HMac<SHA256Hash>(key); }));
  75. using (var client = new SshClient(connectionInfo))
  76. {
  77. client.Connect();
  78. client.Disconnect();
  79. }
  80. }
  81. [TestMethod]
  82. [TestCategory("integration")]
  83. public void Test_HMac_Sha256_96_Connection()
  84. {
  85. var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
  86. connectionInfo.HmacAlgorithms.Clear();
  87. connectionInfo.HmacAlgorithms.Add("hmac-sha2-256-96", new HashInfo(32 * 8, (key) => { return new HMac<SHA256Hash>(key, 96); }));
  88. using (var client = new SshClient(connectionInfo))
  89. {
  90. client.Connect();
  91. client.Disconnect();
  92. }
  93. }
  94. [TestMethod]
  95. [TestCategory("integration")]
  96. public void Test_HMac_RIPEMD160_Connection()
  97. {
  98. var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
  99. connectionInfo.HmacAlgorithms.Clear();
  100. connectionInfo.HmacAlgorithms.Add("hmac-ripemd160", new HashInfo(160, (key) => { return new HMac<RIPEMD160Hash>(key); }));
  101. using (var client = new SshClient(connectionInfo))
  102. {
  103. client.Connect();
  104. client.Disconnect();
  105. }
  106. }
  107. [TestMethod]
  108. [TestCategory("integration")]
  109. public void Test_HMac_RIPEMD160_OPENSSH_Connection()
  110. {
  111. var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
  112. connectionInfo.HmacAlgorithms.Clear();
  113. connectionInfo.HmacAlgorithms.Add("hmac-ripemd160@openssh.com", new HashInfo(160, (key) => { return new HMac<RIPEMD160Hash>(key); }));
  114. using (var client = new SshClient(connectionInfo))
  115. {
  116. client.Connect();
  117. client.Disconnect();
  118. }
  119. }
  120. /// <summary>
  121. ///A test for HMac`1 Constructor
  122. ///</summary>
  123. public void HMacConstructorTestHelper<T>()
  124. where T : HashAlgorithm, new()
  125. {
  126. byte[] key = null; // TODO: Initialize to an appropriate value
  127. HMac<T> target = new HMac<T>(key);
  128. Assert.Inconclusive("TODO: Implement code to verify target");
  129. }
  130. [TestMethod()]
  131. public void HMacConstructorTest()
  132. {
  133. Assert.Inconclusive("No appropriate type parameter is found to satisfies the type constraint(s) of T. " +
  134. "Please call HMacConstructorTestHelper<T>() with appropriate type parameters.");
  135. }
  136. /// <summary>
  137. ///A test for Initialize
  138. ///</summary>
  139. public void InitializeTestHelper<T>()
  140. where T : HashAlgorithm, new()
  141. {
  142. byte[] key = null; // TODO: Initialize to an appropriate value
  143. HMac<T> target = new HMac<T>(key); // TODO: Initialize to an appropriate value
  144. target.Initialize();
  145. Assert.Inconclusive("A method that does not return a value cannot be verified.");
  146. }
  147. [TestMethod()]
  148. public void InitializeTest()
  149. {
  150. Assert.Inconclusive("No appropriate type parameter is found to satisfies the type constraint(s) of T. " +
  151. "Please call InitializeTestHelper<T>() with appropriate type parameters.");
  152. }
  153. /// <summary>
  154. ///A test for Key
  155. ///</summary>
  156. public void KeyTestHelper<T>()
  157. where T : HashAlgorithm, new()
  158. {
  159. byte[] key = null; // TODO: Initialize to an appropriate value
  160. HMac<T> target = new HMac<T>(key); // TODO: Initialize to an appropriate value
  161. byte[] expected = null; // TODO: Initialize to an appropriate value
  162. byte[] actual;
  163. target.Key = expected;
  164. actual = target.Key;
  165. Assert.AreEqual(expected, actual);
  166. Assert.Inconclusive("Verify the correctness of this test method.");
  167. }
  168. [TestMethod()]
  169. public void KeyTest()
  170. {
  171. Assert.Inconclusive("No appropriate type parameter is found to satisfies the type constraint(s) of T. " +
  172. "Please call KeyTestHelper<T>() with appropriate type parameters.");
  173. }
  174. }
  175. }