Merge pull request #1542 from ninjarobot/UriTemplateMatchException
[mono.git] / mcs / class / System.Security / Test / System.Security.Cryptography.Pkcs / Pkcs9SigningTimeTest.cs
1 //
2 // Pkcs9SigningTimeTest.cs - NUnit tests for Pkcs9SigningTime
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 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
30
31 using NUnit.Framework;
32
33 using System;
34 using System.Collections;
35 using System.Security.Cryptography;
36 using System.Security.Cryptography.Pkcs;
37 using System.Text;
38
39 namespace MonoTests.System.Security.Cryptography.Pkcs {
40
41         [TestFixture]
42         public class Pkcs9SigningTimeTest {
43
44                 static string signingTimeOid = "1.2.840.113549.1.9.5";
45                 static string signingTimeName = "Signing Time";
46                 static DateTime mono10release = new DateTime (632241648000000000);
47
48                 [Test]
49                 public void DateTime_Mono10Release ()
50                 {
51                         // some tests fails if the assumption fails
52                         Assert.AreEqual ("040630040000Z", mono10release.ToString ("yyMMddhhmmssZ"), "Z");
53                 }
54
55                 [Test]
56                 public void Constructor_Empty () 
57                 {
58                         Pkcs9SigningTime st = new Pkcs9SigningTime ();
59                         Assert.AreEqual (signingTimeName, st.Oid.FriendlyName, "Oid.FriendlyName");
60                         Assert.AreEqual (signingTimeOid, st.Oid.Value, "Oid.Value");
61                         Assert.AreEqual (15, st.RawData.Length, "RawData.Length");
62                         Assert.AreEqual (BitConverter.ToString (st.RawData).ToLower ().Replace ("-", " "), st.Format (true), "Format(true)");
63                         Assert.AreEqual (BitConverter.ToString (st.RawData).ToLower ().Replace ("-", " "), st.Format (false), "Format(false)");
64                 }
65
66                 [Test]
67                 public void Constructor_DateTime_Now () 
68                 {
69                         Pkcs9SigningTime st = new Pkcs9SigningTime (DateTime.UtcNow);
70                         Assert.AreEqual (signingTimeName, st.Oid.FriendlyName, "Oid.FriendlyName");
71                         Assert.AreEqual (signingTimeOid, st.Oid.Value, "Oid.Value");
72                         Assert.AreEqual (15, st.RawData.Length, "RawData.Length");
73                         Assert.AreEqual (BitConverter.ToString (st.RawData).ToLower ().Replace ("-", " "), st.Format (true), "Format(true)");
74                         Assert.AreEqual (BitConverter.ToString (st.RawData).ToLower ().Replace ("-", " "), st.Format (false), "Format(false)");
75                 }
76
77                 [Test]
78                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
79                 public void Constructor_DateTime_MinValue () 
80                 {
81                         Pkcs9SigningTime st = new Pkcs9SigningTime (DateTime.MinValue);
82                 }
83
84                 [Test]
85                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
86                 public void Constructor_DateTime_1600 ()
87                 {
88                         DateTime dt = new DateTime (1600, 12, 31, 11, 59, 59);
89                         Pkcs9SigningTime st = new Pkcs9SigningTime (dt);
90                 }
91
92                 [Test]
93                 [ExpectedException (typeof (CryptographicException))]
94                 public void Constructor_DateTime_1601 ()
95                 {
96                         DateTime dt = new DateTime (1601, 01, 01, 00, 00, 00);
97                         Pkcs9SigningTime st = new Pkcs9SigningTime (dt);
98                 }
99
100                 [Test]
101                 [ExpectedException (typeof (CryptographicException))]
102                 public void Constructor_DateTime_MaxValue ()
103                 {
104                         Pkcs9SigningTime st = new Pkcs9SigningTime (DateTime.MaxValue);
105                 }
106
107                 [Test]
108                 [ExpectedException (typeof (CryptographicException))]
109                 public void Constructor_DateTime_Before1950 ()
110                 {
111                         DateTime dt = new DateTime (1949, 12, 31, 11, 59, 59);
112                         // UTCTIME (0x17), i.e. 2 digits years, limited to 1950-2050
113                         Pkcs9SigningTime st = new Pkcs9SigningTime (dt);
114                 }
115
116                 [Test]
117                 public void Constructor_DateTime_After1950 ()
118                 {
119                         DateTime dt = new DateTime (1950, 01, 01, 00, 00, 00);
120                         // UTCTIME (0x17), i.e. 2 digits years, limited to 1950-2050
121                         Pkcs9SigningTime st = new Pkcs9SigningTime (dt);
122                         Assert.AreEqual (signingTimeName, st.Oid.FriendlyName, "Oid.FriendlyName");
123                         Assert.AreEqual (signingTimeOid, st.Oid.Value, "Oid.Value");
124                         Assert.AreEqual (15, st.RawData.Length, "RawData.Length");
125                         Assert.AreEqual ("17-0D-35-30-30-31-30-31-30-30-30-30-30-30-5A", BitConverter.ToString (st.RawData));
126                         Assert.AreEqual (dt, st.SigningTime, "st.SigningTime");
127                         Assert.AreEqual ("17 0d 35 30 30 31 30 31 30 30 30 30 30 30 5a", st.Format (true), "Format(true)");
128                         Assert.AreEqual ("17 0d 35 30 30 31 30 31 30 30 30 30 30 30 5a", st.Format (false), "Format(false)");
129                 }
130
131                 [Test]
132                 public void Constructor_DateTime_Before2050 ()
133                 {
134                         DateTime dt = new DateTime (2049, 12, 31, 11, 59, 59);
135                         // up to 2050 encoding should stay with UTCTIME (0x17), i.e. 2 digits years
136                         Pkcs9SigningTime st = new Pkcs9SigningTime (dt);
137                         Assert.AreEqual (signingTimeName, st.Oid.FriendlyName, "Oid.FriendlyName");
138                         Assert.AreEqual (signingTimeOid, st.Oid.Value, "Oid.Value");
139                         Assert.AreEqual (15, st.RawData.Length, "RawData.Length");
140                         Assert.AreEqual ("17-0D-34-39-31-32-33-31-31-31-35-39-35-39-5A", BitConverter.ToString (st.RawData));
141                         Assert.AreEqual (dt, st.SigningTime, "st.SigningTime");
142                         Assert.AreEqual ("17 0d 34 39 31 32 33 31 31 31 35 39 35 39 5a", st.Format (true), "Format(true)");
143                         Assert.AreEqual ("17 0d 34 39 31 32 33 31 31 31 35 39 35 39 5a", st.Format (false), "Format(false)");
144                 }
145
146                 [Test]
147                 [ExpectedException (typeof (CryptographicException))]
148                 public void Constructor_DateTime_After2050 ()
149                 {
150                         DateTime dt = new DateTime (2050, 01, 01, 00, 00, 00);
151                         // in 2050 encoding should switch to GENERALIZEDTIME (0x18), i.e. 4 digits years
152                         Pkcs9SigningTime st = new Pkcs9SigningTime (dt);
153                 }
154
155                 [Test]
156                 public void Constructor_DateTime ()
157                 {
158                         Pkcs9SigningTime st = new Pkcs9SigningTime (mono10release);
159                         Assert.AreEqual (signingTimeName, st.Oid.FriendlyName, "Oid.FriendlyName");
160                         Assert.AreEqual (signingTimeOid, st.Oid.Value, "Oid.Value");
161                         Assert.AreEqual (15, st.RawData.Length, "RawData.Length");
162                         Assert.AreEqual ("17-0D-30-34-30-36-33-30-30-34-30-30-30-30-5A", BitConverter.ToString (st.RawData), "RawData");
163                         Assert.AreEqual (mono10release, st.SigningTime, "st.SigningTime");
164                         Assert.AreEqual ("17 0d 30 34 30 36 33 30 30 34 30 30 30 30 5a", st.Format (true), "Format(true)");
165                         Assert.AreEqual ("17 0d 30 34 30 36 33 30 30 34 30 30 30 30 5a", st.Format (false), "Format(false)");
166                 }
167
168                 [Test]
169                 public void Constructor_Bytes () 
170                 {
171                         byte[] date = new byte [15] { 0x17, 0x0D, 0x30, 0x34, 0x30, 0x36, 0x33, 0x30, 0x30, 0x34, 0x30, 0x30, 0x30, 0x30, 0x5A };
172                         Pkcs9SigningTime st = new Pkcs9SigningTime (date);
173                         Assert.AreEqual (signingTimeName, st.Oid.FriendlyName, "Oid.FriendlyName");
174                         Assert.AreEqual (signingTimeOid, st.Oid.Value, "Oid.Value");
175                         Assert.AreEqual (15, st.RawData.Length, "RawData.Length");
176                         Assert.AreEqual ("17-0D-30-34-30-36-33-30-30-34-30-30-30-30-5A", BitConverter.ToString (st.RawData), "RawData");
177                         Assert.AreEqual (mono10release, st.SigningTime, "st.SigningTime");
178                         Assert.AreEqual ("17 0d 30 34 30 36 33 30 30 34 30 30 30 30 5a", st.Format (true), "Format(true)");
179                         Assert.AreEqual ("17 0d 30 34 30 36 33 30 30 34 30 30 30 30 5a", st.Format (false), "Format(false)");
180                 }
181
182                 [Test]
183                 [ExpectedException (typeof (ArgumentNullException))]
184                 public void Constructor_Bytes_Null ()
185                 {
186                         Pkcs9SigningTime st = new Pkcs9SigningTime (null);
187                 }
188
189                 [Test]
190 //              [Ignore ("MS returns bad results (original time) - Mono needs to override CopyFrom to fix")]
191                 // http://lab.msdn.microsoft.com/ProductFeedback/viewfeedback.aspx?feedbackid=66943396-ad73-497f-82ae-090b87ffcb4e
192                 public void CopyFrom () 
193                 {
194                         Pkcs9SigningTime st1 = new Pkcs9SigningTime (mono10release);
195                         Pkcs9SigningTime st2 = new Pkcs9SigningTime (DateTime.UtcNow);
196                         st1.CopyFrom (st2);
197                         Assert.AreEqual (st2.Oid.FriendlyName, st1.Oid.FriendlyName, "Oid.FriendlyName");
198                         Assert.AreEqual (st2.Oid.Value, st1.Oid.Value, "Oid.Value");
199                         Assert.AreEqual (BitConverter.ToString (st2.RawData), BitConverter.ToString (st1.RawData), "RawData");
200                         // Note: Some timing resolution is lost by goind to ASN.1
201                         Assert.AreEqual (st2.SigningTime.ToString (), st1.SigningTime.ToString (), "SigningTime");
202                 }
203
204                 [Test]
205                 [ExpectedException (typeof (ArgumentNullException))]
206                 public void CopyFrom_Null ()
207                 {
208                         new Pkcs9SigningTime (mono10release).CopyFrom (null);
209                 }
210
211                 [Test]
212 //              [Ignore ("MS doesn't throw but returns bad results - Mono needs to override CopyFrom to fix")]
213                 // http://lab.msdn.microsoft.com/ProductFeedback/viewfeedback.aspx?feedbackid=66943396-ad73-497f-82ae-090b87ffcb4e
214                 [ExpectedException (typeof (CryptographicException))]
215                 public void CopyFrom_Bad ()
216                 {
217                         Pkcs9SigningTime st = new Pkcs9SigningTime (mono10release);
218                         Pkcs9DocumentName dn = new Pkcs9DocumentName ("Mono");
219                         st.CopyFrom (dn);
220                         Assert.AreEqual (dn.Oid.FriendlyName, st.Oid.FriendlyName, "Oid.FriendlyName");
221                         Assert.AreEqual (dn.Oid.Value, st.Oid.Value, "Oid.Value");
222                         Assert.AreEqual (BitConverter.ToString (dn.RawData), BitConverter.ToString (st.RawData), "RawData");
223                         // wrong ASN.1
224                         Assert.AreEqual (mono10release, st.SigningTime, "SigningTime");
225                 }
226         }
227 }
228