AsnEncodedDataTest.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // AsnEncodedDataTest.cs - NUnit tests for AsnEncodedData
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
  8. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using NUnit.Framework;
  31. using System;
  32. using System.Security.Cryptography;
  33. namespace MonoTests.System.Security.Cryptography {
  34. [TestFixture]
  35. public class AsnEncodedDataTest {
  36. static byte[] asnNullBytes = { 0x05, 0x00 };
  37. static string asnNullString = "05 00";
  38. static byte[] asnLongBytes = { 0x30,0x5C,0x02,0x55,0x2D,0x58,0xE9,0xBF,0xF0,0x31,0xCD,0x79,0x06,0x50,0x5A,0xD5,0x9E,0x0E,0x2C,0xE6,0xC2,0xF7,0xF9,0xD2,0xCE,0x55,0x64,0x85,0xB1,0x90,0x9A,0x92,0xB3,0x36,0xC1,0xBC,0xEA,0xC8,0x23,0xB7,0xAB,0x3A,0xA7,0x64,0x63,0x77,0x5F,0x84,0x22,0x8E,0xE5,0xB6,0x45,0xDD,0x46,0xAE,0x0A,0xDD,0x00,0xC2,0x1F,0xBA,0xD9,0xAD,0xC0,0x75,0x62,0xF8,0x95,0x82,0xA2,0x80,0xB1,0x82,0x69,0xFA,0xE1,0xAF,0x7F,0xBC,0x7D,0xE2,0x7C,0x76,0xD5,0xBC,0x2A,0x80,0xFB,0x02,0x03,0x01,0x00,0x01 };
  39. static string asnLongString = "30 5c 02 55 2d 58 e9 bf f0 31 cd 79 06 50 5a d5 9e 0e 2c e6 c2 f7 f9 d2 ce 55 64 85 b1 90 9a 92 b3 36 c1 bc ea c8 23 b7 ab 3a a7 64 63 77 5f 84 22 8e e5 b6 45 dd 46 ae 0a dd 00 c2 1f ba d9 ad c0 75 62 f8 95 82 a2 80 b1 82 69 fa e1 af 7f bc 7d e2 7c 76 d5 bc 2a 80 fb 02 03 01 00 01";
  40. [Test]
  41. public void Constructor_StringData ()
  42. {
  43. AsnEncodedData aed = new AsnEncodedData ("oid", asnNullBytes);
  44. Assert.AreEqual ("oid", aed.Oid.Value, "Oid.Value");
  45. Assert.IsNull (aed.Oid.FriendlyName, "Oid.FriendlyName");
  46. Assert.AreEqual (BitConverter.ToString (asnNullBytes), BitConverter.ToString (aed.RawData), "RawData");
  47. Assert.AreEqual (asnNullString, aed.Format (true), "Format");
  48. }
  49. [Test]
  50. [ExpectedException (typeof (ArgumentNullException))]
  51. public void Constructor_StringNullData ()
  52. {
  53. string oid = null; // do not confuse compiler
  54. AsnEncodedData aed = new AsnEncodedData (oid, asnNullBytes);
  55. }
  56. [Test]
  57. [ExpectedException (typeof (ArgumentNullException))]
  58. public void Constructor_StringDataNull ()
  59. {
  60. AsnEncodedData aed = new AsnEncodedData ("oid", null);
  61. }
  62. [Test]
  63. public void Constructor_OidData ()
  64. {
  65. Oid o = new Oid ("1.0");
  66. AsnEncodedData aed = new AsnEncodedData (o, asnNullBytes);
  67. Assert.AreEqual ("1.0", aed.Oid.Value, "Oid.Value");
  68. Assert.IsNull (aed.Oid.FriendlyName, "Oid.FriendlyName");
  69. Assert.AreEqual (BitConverter.ToString (asnNullBytes), BitConverter.ToString (aed.RawData), "RawData");
  70. Assert.AreEqual (asnNullString, aed.Format (true), "Format");
  71. }
  72. [Test]
  73. // BUG [ExpectedException (typeof (ArgumentNullException))]
  74. public void Constructor_OidNullData ()
  75. {
  76. Oid o = null;
  77. AsnEncodedData aed = new AsnEncodedData (o, asnNullBytes);
  78. Assert.IsNull (aed.Oid, "Oid");
  79. Assert.AreEqual (BitConverter.ToString (asnNullBytes), BitConverter.ToString (aed.RawData), "RawData");
  80. Assert.AreEqual (asnNullString, aed.Format (true), "Format");
  81. }
  82. [Test]
  83. [ExpectedException (typeof (ArgumentNullException))]
  84. public void Constructor_OidDataNull ()
  85. {
  86. Oid o = new Oid ("1.0");
  87. AsnEncodedData aed = new AsnEncodedData (o, null);
  88. }
  89. [Test]
  90. public void Constructor_Asn ()
  91. {
  92. AsnEncodedData aed = new AsnEncodedData ("oid", asnNullBytes);
  93. AsnEncodedData aed2 = new AsnEncodedData (aed);
  94. Assert.AreEqual (aed.Oid.Value, aed2.Oid.Value, "Oid.Value");
  95. Assert.AreEqual (aed.Oid.FriendlyName, aed2.Oid.FriendlyName, "Oid.FriendlyName");
  96. Assert.AreEqual (BitConverter.ToString (aed.RawData), BitConverter.ToString (aed2.RawData), "RawData");
  97. string s1 = aed.Format (false);
  98. string s2 = aed.Format (true);
  99. Assert.AreEqual (s1, s2, "Format");
  100. }
  101. [Test]
  102. [ExpectedException (typeof (ArgumentNullException))]
  103. public void Constructor_ByteArrayNull ()
  104. {
  105. byte[] array = null;
  106. AsnEncodedData aed = new AsnEncodedData (array);
  107. }
  108. [Test]
  109. [ExpectedException (typeof (ArgumentNullException))]
  110. public void Constructor_AsnNull ()
  111. {
  112. AsnEncodedData asn = null;
  113. AsnEncodedData aed = new AsnEncodedData (asn);
  114. }
  115. [Test]
  116. public void Format ()
  117. {
  118. AsnEncodedData aed = new AsnEncodedData ("1.2.840.113549.1.1.1", asnLongBytes);
  119. Assert.AreEqual ("1.2.840.113549.1.1.1", aed.Oid.Value, "Oid.Value");
  120. Assert.AreEqual ("RSA", aed.Oid.FriendlyName, "Oid.FriendlyName");
  121. Assert.AreEqual (BitConverter.ToString (asnLongBytes), BitConverter.ToString (aed.RawData), "RawData");
  122. string result = aed.Format (false);
  123. Assert.AreEqual (asnLongString, result, "Format(false)");
  124. }
  125. [Test]
  126. public void FormatMultiline ()
  127. {
  128. AsnEncodedData aed = new AsnEncodedData ("1.2.840.113549.1.1.1", asnLongBytes);
  129. Assert.AreEqual ("1.2.840.113549.1.1.1", aed.Oid.Value, "Oid.Value");
  130. Assert.AreEqual ("RSA", aed.Oid.FriendlyName, "Oid.FriendlyName");
  131. Assert.AreEqual (BitConverter.ToString (asnLongBytes), BitConverter.ToString (aed.RawData), "RawData");
  132. string result = aed.Format (true);
  133. Assert.AreEqual (asnLongString, result, "Format(true)");
  134. }
  135. }
  136. }
  137. #endif