DerDataTest.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using Renci.SshNet.Common;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using System;
  4. namespace Renci.SshNet.Tests.Common
  5. {
  6. [TestClass()]
  7. public class DerDataTest
  8. {
  9. private TestContext testContextInstance;
  10. /// <summary>
  11. ///Gets or sets the test context which provides
  12. ///information about and functionality for the current test run.
  13. ///</summary>
  14. public TestContext TestContext
  15. {
  16. get
  17. {
  18. return testContextInstance;
  19. }
  20. set
  21. {
  22. testContextInstance = value;
  23. }
  24. }
  25. #region Additional test attributes
  26. //
  27. //You can use the following additional attributes as you write your tests:
  28. //
  29. //Use ClassInitialize to run code before running the first test in the class
  30. //[ClassInitialize()]
  31. //public static void MyClassInitialize(TestContext testContext)
  32. //{
  33. //}
  34. //
  35. //Use ClassCleanup to run code after all tests in a class have run
  36. //[ClassCleanup()]
  37. //public static void MyClassCleanup()
  38. //{
  39. //}
  40. //
  41. //Use TestInitialize to run code before running each test
  42. //[TestInitialize()]
  43. //public void MyTestInitialize()
  44. //{
  45. //}
  46. //
  47. //Use TestCleanup to run code after each test has run
  48. //[TestCleanup()]
  49. //public void MyTestCleanup()
  50. //{
  51. //}
  52. //
  53. #endregion
  54. [TestMethod]
  55. [TestCategory("DER")]
  56. [Description("Long form, test example given in 8.1.3.5")]
  57. [Owner("Kenneth_aa")]
  58. [DeploymentItem("Renci.SshNet.dll")]
  59. public void Test_Der_GetLength_LongForm_MustNotFail()
  60. {
  61. // Taken from example in 8.1.3.5
  62. // L = 201 can be encoded as:
  63. // 1 0 0 0 0 0 0 1
  64. // 1 1 0 0 1 0 0 1
  65. int length = 201;
  66. byte[] expected = new byte[2]
  67. {
  68. 0x81, // 1 0 0 0 0 0 0 1
  69. 0xC9 // 1 1 0 0 1 0 0 1
  70. };
  71. byte[] actual = Helper_GetLength(length);
  72. Helper_CompareByteArray(expected, actual);
  73. Assert.Inconclusive("Verify the correctness of this test method.");
  74. }
  75. [TestMethod]
  76. [TestCategory("DER")]
  77. [Description("Short form, test example given in 8.1.3.5")]
  78. [Owner("Kenneth_aa")]
  79. [DeploymentItem("Renci.SshNet.dll")]
  80. public void Test_Der_GetLength_ShortForm_MustNotFail()
  81. {
  82. int length = 127;
  83. byte[] expected = new byte[1]
  84. {
  85. 0x7F // 0 1 1 1 1 1 1 1
  86. };
  87. byte[] actual = Helper_GetLength(length);
  88. Helper_CompareByteArray(expected, actual);
  89. }
  90. /// <summary>
  91. /// Compares 2 byte arrays.
  92. /// </summary>
  93. /// <param name="expected">Expected result.</param>
  94. /// <param name="actual">Actual result.</param>
  95. void Helper_CompareByteArray(byte[] expected, byte[] actual)
  96. {
  97. Assert.AreEqual(expected.Length, actual.Length, "Length mismatch: Expected.Length = {0} - Actual.Length = {1}", expected.Length, actual.Length);
  98. for (int i = 0; i < expected.Length; i++)
  99. Assert.AreEqual<byte>(expected[i], actual[i], "Byte mismatch at index {0}", i);
  100. }
  101. /// <summary>
  102. /// Wrapper for calling DerData.GetLength()
  103. /// </summary>
  104. byte[] Helper_GetLength(int length)
  105. {
  106. DerData_Accessor target = new DerData_Accessor();
  107. return target.GetLength(length);
  108. }
  109. }
  110. }