2 // ASN1ConvertTest.cs - NUnit Test Cases for ASN1Convert
5 // Sebastien Pouliot (sebastien@ximian.com)
7 // Copyright (C) 2004-2007 Novell, Inc (http://www.novell.com)
11 using System.Security.Cryptography;
15 using NUnit.Framework;
17 namespace MonoTests.Mono.Security {
20 public class ASN1ConvertTest {
22 // UtcNow has more precision than the ASN.1 encoded has
23 // we need to ignore the extra subseconds but using ToString
24 // won't work under 2.0 (Utc string compared to Local string)
25 private void AssertDate (DateTime expected, DateTime actual, string message)
27 // expected is full precision, actual has second-level precision
28 double seconds = new TimeSpan (expected.Ticks - actual.Ticks).TotalSeconds;
30 Assert.Fail ("Expected {0} has more than one second ({1}) difference with actual data {2}",
31 expected, seconds, actual);
36 public void ConvertDateTimeBefore2000 ()
38 DateTime expected = DateTime.Now.AddYears (-50);
39 ASN1 dt = ASN1Convert.FromDateTime (expected);
40 Assert.AreEqual (0x17, dt.Tag, "UTCTIME");
41 DateTime actual = ASN1Convert.ToDateTime (dt);
42 Assert.AreEqual (DateTimeKind.Utc, actual.Kind, "Kind");
43 AssertDate (expected, actual.ToLocalTime(), "DateTime");
47 public void ConvertDateTimeAfter2000 ()
49 DateTime expected = DateTime.Now;
50 ASN1 dt = ASN1Convert.FromDateTime (expected);
51 Assert.AreEqual (0x17, dt.Tag, "UTCTIME");
52 DateTime actual = ASN1Convert.ToDateTime (dt);
53 Assert.AreEqual (DateTimeKind.Utc, actual.Kind, "Kind");
54 AssertDate (expected, actual.ToLocalTime(), "DateTime");
58 public void ConvertDateTimeAfter2050 ()
60 DateTime expected = DateTime.Now.AddYears (50);
61 ASN1 dt = ASN1Convert.FromDateTime (expected);
62 Assert.AreEqual (0x18, dt.Tag, "GENERALIZEDTIME");
63 DateTime actual = ASN1Convert.ToDateTime (dt);
64 Assert.AreEqual (DateTimeKind.Utc, actual.Kind, "Kind");
65 AssertDate (expected, actual.ToLocalTime(), "DateTime");
69 public void ConvertDateTimeInvalidButExistingFormat ()
71 string nosecs = "9912312359Z";
72 ASN1 dt = new ASN1 (0x18, Encoding.ASCII.GetBytes (nosecs));
73 DateTime actual = ASN1Convert.ToDateTime (dt);
74 Assert.AreEqual (DateTimeKind.Utc, actual.Kind, "Kind");
75 Assert.AreEqual (nosecs, actual.ToUniversalTime ().ToString ("yyMMddHHmm") + "Z", "DateTime");
79 [ExpectedException (typeof (ArgumentNullException))]
80 public void ConvertToDate_Null ()
82 ASN1Convert.ToDateTime (null);
86 public void ConvertInt32_Negative ()
89 ASN1 integer = ASN1Convert.FromInt32 (expected);
90 Int32 actual = ASN1Convert.ToInt32 (integer);
91 Assert.AreEqual (expected, actual, "Int32_Negative");
95 public void ConvertInt32_Zero ()
98 ASN1 integer = ASN1Convert.FromInt32 (expected);
99 Int32 actual = ASN1Convert.ToInt32 (integer);
100 Assert.AreEqual (expected, actual, "Int32_Zero");
103 public void ConvertInt32_One ()
106 ASN1 integer = ASN1Convert.FromInt32 (expected);
107 Int32 actual = ASN1Convert.ToInt32 (integer);
108 Assert.AreEqual (expected, actual, "Int32_Zero");
112 public void ConvertInt32_Positive ()
114 Int32 expected = Int32.MaxValue;
115 ASN1 integer = ASN1Convert.FromInt32 (expected);
116 Int32 actual = ASN1Convert.ToInt32 (integer);
117 Assert.AreEqual (expected, actual, "Int32_Positive");
121 [ExpectedException (typeof (FormatException))]
122 public void ConvertToInt32_WrongTag ()
124 ASN1 nul = new ASN1 (0x05);
125 Int32 actual = ASN1Convert.ToInt32 (nul);
129 [ExpectedException (typeof (ArgumentNullException))]
130 public void ConvertToInt32_Null ()
132 ASN1Convert.ToInt32 (null);
136 [ExpectedException (typeof (ArgumentNullException))]
137 public void ConvertFromUnsignedBigInteger_Null ()
139 ASN1Convert.FromUnsignedBigInteger (null);
143 public void ConvertFromUnsignedBigInteger ()
145 byte[] big = new byte [16];
148 ASN1 bigint = ASN1Convert.FromUnsignedBigInteger (big);
149 // no byte is required for the sign (100% positive it's positive ;-)
150 Assert.AreEqual (16, bigint.Value.Length, "BigInteger-NotExtended");
153 bigint = ASN1Convert.FromUnsignedBigInteger (big);
154 // one byte added as 0x00 to be sure it's not interpreted as a negative
155 Assert.AreEqual (17, bigint.Value.Length, "BigInteger-SignExtended");
159 public void ConvertOID ()
161 string expected = "1.2.840.113549.1.7.6";
162 ASN1 oid = ASN1Convert.FromOid (expected);
163 string actual = ASN1Convert.ToOid (oid);
164 Assert.AreEqual (expected, actual, "OID");
168 // the large X test tries to encode an invalid OID (second part being > 40).
169 // In 1.x CryptoConfig.EncodeOID just encoded the binary (so we copied the
170 // *bad* behaviour) but 2.0 encode it differently (sigh)
171 [Category ("NotDotNet")]
172 public void ConvertOID_LargeX ()
174 ASN1 asn = new ASN1 (0x06, new byte [] { 0xA8, 0x00, 0x00 });
175 string oid = ASN1Convert.ToOid (asn);
176 Assert.AreEqual ("2.88.0.0", oid, "ToOID");
177 Assert.AreEqual (BitConverter.ToString (asn.GetBytes ()),
178 BitConverter.ToString (ASN1Convert.FromOid (oid).GetBytes ()), "FromOID");
182 [ExpectedException (typeof (ArgumentNullException))]
183 public void ConvertFromOid_Null ()
185 ASN1Convert.FromOid (null);
189 [ExpectedException (typeof (ArgumentNullException))]
190 public void ConvertToOid_Null ()
192 ASN1Convert.ToOid (null);