2 // ASN1ConvertTest.cs - NUnit Test Cases for ASN1Convert
5 // Sebastien Pouliot (sebastien@ximian.com)
7 // (C) 2004 Novell (http://www.novell.com)
11 using System
.Security
.Cryptography
;
15 using NUnit
.Framework
;
17 namespace MonoTests
.Mono
.Security
{
20 public class ASN1ConvertTest
: Assertion
{
23 public void ConvertDateTimeBefore2000 ()
25 DateTime expected
= DateTime
.UtcNow
.AddYears (-50);
26 ASN1 dt
= ASN1Convert
.FromDateTime (expected
);
27 AssertEquals ("UTCTIME", 0x17, dt
.Tag
);
28 DateTime actual
= ASN1Convert
.ToDateTime (dt
);
29 AssertEquals ("DateTime", expected
.ToString (), actual
.ToString ());
33 public void ConvertDateTimeAfter2000 ()
35 DateTime expected
= DateTime
.UtcNow
;
36 ASN1 dt
= ASN1Convert
.FromDateTime (expected
);
37 AssertEquals ("UTCTIME", 0x17, dt
.Tag
);
38 DateTime actual
= ASN1Convert
.ToDateTime (dt
);
39 AssertEquals ("DateTime", expected
.ToString (), actual
.ToString ());
43 public void ConvertDateTimeAfter2050 ()
45 DateTime expected
= DateTime
.UtcNow
.AddYears (50);
46 ASN1 dt
= ASN1Convert
.FromDateTime (expected
);
47 AssertEquals ("GENERALIZEDTIME", 0x18, dt
.Tag
);
48 DateTime actual
= ASN1Convert
.ToDateTime (dt
);
49 AssertEquals ("DateTime", expected
.ToString (), actual
.ToString ());
53 public void ConvertDateTimeInvalidButExistingFormat ()
55 string nosecs
= "9912312359Z";
56 ASN1 dt
= new ASN1 (0x18, Encoding
.ASCII
.GetBytes (nosecs
));
57 DateTime actual
= ASN1Convert
.ToDateTime (dt
);
58 AssertEquals ("DateTime", nosecs
, actual
.ToUniversalTime ().ToString ("yyMMddHHmm") + "Z");
62 [ExpectedException (typeof (ArgumentNullException
))]
63 public void ConvertToDate_Null ()
65 ASN1Convert
.ToDateTime (null);
69 public void ConvertInt32_Negative ()
72 ASN1 integer
= ASN1Convert
.FromInt32 (expected
);
73 Int32 actual
= ASN1Convert
.ToInt32 (integer
);
74 AssertEquals ("Int32_Negative", expected
, actual
);
78 public void ConvertInt32_Zero ()
81 ASN1 integer
= ASN1Convert
.FromInt32 (expected
);
82 Int32 actual
= ASN1Convert
.ToInt32 (integer
);
83 AssertEquals ("Int32_Zero", expected
, actual
);
86 public void ConvertInt32_One ()
89 ASN1 integer
= ASN1Convert
.FromInt32 (expected
);
90 Int32 actual
= ASN1Convert
.ToInt32 (integer
);
91 AssertEquals ("Int32_Zero", expected
, actual
);
95 public void ConvertInt32_Positive ()
97 Int32 expected
= Int32
.MaxValue
;
98 ASN1 integer
= ASN1Convert
.FromInt32 (expected
);
99 Int32 actual
= ASN1Convert
.ToInt32 (integer
);
100 AssertEquals ("Int32_Positive", expected
, actual
);
104 [ExpectedException (typeof (FormatException
))]
105 public void ConvertToInt32_WrongTag ()
107 ASN1 nul
= new ASN1 (0x05);
108 Int32 actual
= ASN1Convert
.ToInt32 (nul
);
112 [ExpectedException (typeof (ArgumentNullException
))]
113 public void ConvertToInt32_Null ()
115 ASN1Convert
.ToInt32 (null);
119 [ExpectedException (typeof (ArgumentNullException
))]
120 public void ConvertFromUnsignedBigInteger_Null ()
122 ASN1Convert
.FromUnsignedBigInteger (null);
126 public void ConvertFromUnsignedBigInteger ()
128 byte[] big
= new byte [16];
130 ASN1 bigint
= ASN1Convert
.FromUnsignedBigInteger (big
);
131 // one byte added as 0x00 to be sure for sign
132 AssertEquals ("BigInteger", 17, bigint
.Value
.Length
);
136 public void ConvertOID ()
138 string expected
= "1.2.840.113549.1.7.6";
139 ASN1 oid
= ASN1Convert
.FromOid (expected
);
140 string actual
= ASN1Convert
.ToOid (oid
);
141 AssertEquals ("OID", expected
, actual
);
145 public void ConvertOID_LargeX ()
147 ASN1 asn
= new ASN1 (0x06, new byte [] { 0xA8, 0x00, 0x00 }
);
148 string oid
= ASN1Convert
.ToOid (asn
);
149 AssertEquals ("ToOID", "2.88.0.0", oid
);
150 AssertEquals ("FromOID", BitConverter
.ToString (asn
.GetBytes ()),
151 BitConverter
.ToString (ASN1Convert
.FromOid (oid
).GetBytes ()));
154 [ExpectedException (typeof (ArgumentNullException
))]
155 public void ConvertFromOid_Null ()
157 ASN1Convert
.FromOid (null);
161 [ExpectedException (typeof (ArgumentNullException
))]
162 public void ConvertToOid_Null ()
164 ASN1Convert
.ToOid (null);