**** Merged from MCS ****
[mono-project.git] / mcs / class / Mono.Security / Test / Mono.Security / ASN1ConvertTest.cs
blobf36f6e424674cccb104b0e04fa040bccfd92c01a
1 //
2 // ASN1ConvertTest.cs - NUnit Test Cases for ASN1Convert
3 //
4 // Author:
5 // Sebastien Pouliot (sebastien@ximian.com)
6 //
7 // (C) 2004 Novell (http://www.novell.com)
8 //
10 using System;
11 using System.Security.Cryptography;
12 using System.Text;
14 using Mono.Security;
15 using NUnit.Framework;
17 namespace MonoTests.Mono.Security {
19 [TestFixture]
20 public class ASN1ConvertTest : Assertion {
22 [Test]
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 ());
32 [Test]
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 ());
42 [Test]
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 ());
52 [Test]
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");
61 [Test]
62 [ExpectedException (typeof (ArgumentNullException))]
63 public void ConvertToDate_Null ()
65 ASN1Convert.ToDateTime (null);
68 [Test]
69 public void ConvertInt32_Negative ()
71 Int32 expected = -1;
72 ASN1 integer = ASN1Convert.FromInt32 (expected);
73 Int32 actual = ASN1Convert.ToInt32 (integer);
74 AssertEquals ("Int32_Negative", expected, actual);
77 [Test]
78 public void ConvertInt32_Zero ()
80 Int32 expected = 0;
81 ASN1 integer = ASN1Convert.FromInt32 (expected);
82 Int32 actual = ASN1Convert.ToInt32 (integer);
83 AssertEquals ("Int32_Zero", expected, actual);
85 [Test]
86 public void ConvertInt32_One ()
88 Int32 expected = 1;
89 ASN1 integer = ASN1Convert.FromInt32 (expected);
90 Int32 actual = ASN1Convert.ToInt32 (integer);
91 AssertEquals ("Int32_Zero", expected, actual);
94 [Test]
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);
103 [Test]
104 [ExpectedException (typeof (FormatException))]
105 public void ConvertToInt32_WrongTag ()
107 ASN1 nul = new ASN1 (0x05);
108 Int32 actual = ASN1Convert.ToInt32 (nul);
111 [Test]
112 [ExpectedException (typeof (ArgumentNullException))]
113 public void ConvertToInt32_Null ()
115 ASN1Convert.ToInt32 (null);
118 [Test]
119 [ExpectedException (typeof (ArgumentNullException))]
120 public void ConvertFromUnsignedBigInteger_Null ()
122 ASN1Convert.FromUnsignedBigInteger (null);
125 [Test]
126 public void ConvertFromUnsignedBigInteger ()
128 byte[] big = new byte [16];
129 big [0] = 1;
130 ASN1 bigint = ASN1Convert.FromUnsignedBigInteger (big);
131 // one byte added as 0x00 to be sure for sign
132 AssertEquals ("BigInteger", 17, bigint.Value.Length);
135 [Test]
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);
144 [Test]
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 ()));
153 [Test]
154 [ExpectedException (typeof (ArgumentNullException))]
155 public void ConvertFromOid_Null ()
157 ASN1Convert.FromOid (null);
160 [Test]
161 [ExpectedException (typeof (ArgumentNullException))]
162 public void ConvertToOid_Null ()
164 ASN1Convert.ToOid (null);