[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / Mono.Security / Test / Mono.Security / ASN1ConvertTest.cs
1 //
2 // ASN1ConvertTest.cs - NUnit Test Cases for ASN1Convert
3 //
4 // Author:
5 //      Sebastien Pouliot (sebastien@ximian.com)
6 //
7 // Copyright (C) 2004-2007 Novell, Inc (http://www.novell.com)
8 //
9
10 using System;
11 using System.Security.Cryptography;
12 using System.Text;
13
14 using Mono.Security;
15 using NUnit.Framework;
16
17 namespace MonoTests.Mono.Security {
18
19         [TestFixture]
20         public class ASN1ConvertTest {
21
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)
26                 {
27                         // expected is full precision, actual has second-level precision
28                         double seconds = new TimeSpan (expected.Ticks - actual.Ticks).TotalSeconds;
29                         if (seconds >= 1.0) {
30                                 Assert.Fail ("Expected {0} has more than one second ({1}) difference with actual data {2}",
31                                         expected, seconds, actual);
32                         }
33                 }
34
35                 [Test]
36                 public void ConvertDateTimeBefore2000 () 
37                 {
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");
44                 }
45
46                 [Test]
47                 public void ConvertDateTimeAfter2000 () 
48                 {
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");
55                 }
56
57                 [Test]
58                 public void ConvertDateTimeAfter2050 () 
59                 {
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");
66                 }
67
68                 [Test]
69                 public void ConvertDateTimeInvalidButExistingFormat () 
70                 {
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");
76                 }
77
78                 [Test]
79                 [ExpectedException (typeof (ArgumentNullException))]
80                 public void ConvertToDate_Null () 
81                 {
82                         ASN1Convert.ToDateTime (null);
83                 }
84
85                 [Test]
86                 public void ConvertInt32_Negative () 
87                 {
88                         Int32 expected = -1;
89                         ASN1 integer = ASN1Convert.FromInt32 (expected);
90                         Int32 actual = ASN1Convert.ToInt32 (integer);
91                         Assert.AreEqual (expected, actual, "Int32_Negative");
92                 }
93
94                 [Test]
95                 public void ConvertInt32_Zero () 
96                 {
97                         Int32 expected = 0;
98                         ASN1 integer = ASN1Convert.FromInt32 (expected);
99                         Int32 actual = ASN1Convert.ToInt32 (integer);
100                         Assert.AreEqual (expected, actual, "Int32_Zero");
101                 }
102                 [Test]
103                 public void ConvertInt32_One () 
104                 {
105                         Int32 expected = 1;
106                         ASN1 integer = ASN1Convert.FromInt32 (expected);
107                         Int32 actual = ASN1Convert.ToInt32 (integer);
108                         Assert.AreEqual (expected, actual, "Int32_Zero");
109                 }
110
111                 [Test]
112                 public void ConvertInt32_Positive () 
113                 {
114                         Int32 expected = Int32.MaxValue;
115                         ASN1 integer = ASN1Convert.FromInt32 (expected);
116                         Int32 actual = ASN1Convert.ToInt32 (integer);
117                         Assert.AreEqual (expected, actual, "Int32_Positive");
118                 }
119
120                 [Test]
121                 [ExpectedException (typeof (FormatException))]
122                 public void ConvertToInt32_WrongTag () 
123                 {
124                         ASN1 nul = new ASN1 (0x05);
125                         Int32 actual = ASN1Convert.ToInt32 (nul);
126                 }
127                 
128                 [Test]
129                 [ExpectedException (typeof (ArgumentNullException))]
130                 public void ConvertToInt32_Null () 
131                 {
132                         ASN1Convert.ToInt32 (null);
133                 }
134
135                 [Test]
136                 [ExpectedException (typeof (ArgumentNullException))]
137                 public void ConvertFromUnsignedBigInteger_Null () 
138                 {
139                         ASN1Convert.FromUnsignedBigInteger (null);
140                 }
141
142                 [Test]
143                 public void ConvertFromUnsignedBigInteger () 
144                 {
145                         byte[] big = new byte [16];
146
147                         big [0] = 1;
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");
151
152                         big [0] = 0x80;
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");
156                 }
157
158                 [Test]
159                 public void ConvertOID () 
160                 {
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");
165                 }
166
167                 [Test]
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 () 
173                 {
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");
179                 }
180
181                 [Test]
182                 [ExpectedException (typeof (ArgumentNullException))]
183                 public void ConvertFromOid_Null () 
184                 {
185                         ASN1Convert.FromOid (null);
186                 }
187                 
188                 [Test]
189                 [ExpectedException (typeof (ArgumentNullException))]
190                 public void ConvertToOid_Null () 
191                 {
192                         ASN1Convert.ToOid (null);
193                 }
194         }
195 }