1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
//
// Pkcs9SigningTimeTest.cs - NUnit tests for Pkcs9SigningTime
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if NET_2_0
using NUnit.Framework;
using System;
using System.Collections;
using System.Security.Cryptography;
using System.Security.Cryptography.Pkcs;
using System.Text;
namespace MonoTests.System.Security.Cryptography.Pkcs {
[TestFixture]
public class Pkcs9SigningTimeTest {
static string signingTimeOid = "1.2.840.113549.1.9.5";
static string signingTimeName = "Signing Time";
static DateTime mono10release = new DateTime (632241648000000000);
[Test]
public void DateTime_Mono10Release ()
{
// some tests fails if the assumption fails
Assert.AreEqual ("040630040000Z", mono10release.ToString ("yyMMddhhmmssZ"), "Z");
}
[Test]
public void Constructor_Empty ()
{
Pkcs9SigningTime st = new Pkcs9SigningTime ();
Assert.AreEqual (signingTimeName, st.Oid.FriendlyName, "Oid.FriendlyName");
Assert.AreEqual (signingTimeOid, st.Oid.Value, "Oid.Value");
Assert.AreEqual (15, st.RawData.Length, "RawData.Length");
Assert.AreEqual (BitConverter.ToString (st.RawData).ToLower ().Replace ("-", " "), st.Format (true), "Format(true)");
Assert.AreEqual (BitConverter.ToString (st.RawData).ToLower ().Replace ("-", " "), st.Format (false), "Format(false)");
}
[Test]
public void Constructor_DateTime_Now ()
{
Pkcs9SigningTime st = new Pkcs9SigningTime (DateTime.UtcNow);
Assert.AreEqual (signingTimeName, st.Oid.FriendlyName, "Oid.FriendlyName");
Assert.AreEqual (signingTimeOid, st.Oid.Value, "Oid.Value");
Assert.AreEqual (15, st.RawData.Length, "RawData.Length");
Assert.AreEqual (BitConverter.ToString (st.RawData).ToLower ().Replace ("-", " "), st.Format (true), "Format(true)");
Assert.AreEqual (BitConverter.ToString (st.RawData).ToLower ().Replace ("-", " "), st.Format (false), "Format(false)");
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void Constructor_DateTime_MinValue ()
{
Pkcs9SigningTime st = new Pkcs9SigningTime (DateTime.MinValue);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void Constructor_DateTime_1600 ()
{
DateTime dt = new DateTime (1600, 12, 31, 11, 59, 59);
Pkcs9SigningTime st = new Pkcs9SigningTime (dt);
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void Constructor_DateTime_1601 ()
{
DateTime dt = new DateTime (1601, 01, 01, 00, 00, 00);
Pkcs9SigningTime st = new Pkcs9SigningTime (dt);
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void Constructor_DateTime_MaxValue ()
{
Pkcs9SigningTime st = new Pkcs9SigningTime (DateTime.MaxValue);
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void Constructor_DateTime_Before1950 ()
{
DateTime dt = new DateTime (1949, 12, 31, 11, 59, 59);
// UTCTIME (0x17), i.e. 2 digits years, limited to 1950-2050
Pkcs9SigningTime st = new Pkcs9SigningTime (dt);
}
[Test]
public void Constructor_DateTime_After1950 ()
{
DateTime dt = new DateTime (1950, 01, 01, 00, 00, 00);
// UTCTIME (0x17), i.e. 2 digits years, limited to 1950-2050
Pkcs9SigningTime st = new Pkcs9SigningTime (dt);
Assert.AreEqual (signingTimeName, st.Oid.FriendlyName, "Oid.FriendlyName");
Assert.AreEqual (signingTimeOid, st.Oid.Value, "Oid.Value");
Assert.AreEqual (15, st.RawData.Length, "RawData.Length");
Assert.AreEqual ("17-0D-35-30-30-31-30-31-30-30-30-30-30-30-5A", BitConverter.ToString (st.RawData));
Assert.AreEqual (dt, st.SigningTime, "st.SigningTime");
Assert.AreEqual ("17 0d 35 30 30 31 30 31 30 30 30 30 30 30 5a", st.Format (true), "Format(true)");
Assert.AreEqual ("17 0d 35 30 30 31 30 31 30 30 30 30 30 30 5a", st.Format (false), "Format(false)");
}
[Test]
public void Constructor_DateTime_Before2050 ()
{
DateTime dt = new DateTime (2049, 12, 31, 11, 59, 59);
// up to 2050 encoding should stay with UTCTIME (0x17), i.e. 2 digits years
Pkcs9SigningTime st = new Pkcs9SigningTime (dt);
Assert.AreEqual (signingTimeName, st.Oid.FriendlyName, "Oid.FriendlyName");
Assert.AreEqual (signingTimeOid, st.Oid.Value, "Oid.Value");
Assert.AreEqual (15, st.RawData.Length, "RawData.Length");
Assert.AreEqual ("17-0D-34-39-31-32-33-31-31-31-35-39-35-39-5A", BitConverter.ToString (st.RawData));
Assert.AreEqual (dt, st.SigningTime, "st.SigningTime");
Assert.AreEqual ("17 0d 34 39 31 32 33 31 31 31 35 39 35 39 5a", st.Format (true), "Format(true)");
Assert.AreEqual ("17 0d 34 39 31 32 33 31 31 31 35 39 35 39 5a", st.Format (false), "Format(false)");
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void Constructor_DateTime_After2050 ()
{
DateTime dt = new DateTime (2050, 01, 01, 00, 00, 00);
// in 2050 encoding should switch to GENERALIZEDTIME (0x18), i.e. 4 digits years
Pkcs9SigningTime st = new Pkcs9SigningTime (dt);
}
[Test]
public void Constructor_DateTime ()
{
Pkcs9SigningTime st = new Pkcs9SigningTime (mono10release);
Assert.AreEqual (signingTimeName, st.Oid.FriendlyName, "Oid.FriendlyName");
Assert.AreEqual (signingTimeOid, st.Oid.Value, "Oid.Value");
Assert.AreEqual (15, st.RawData.Length, "RawData.Length");
Assert.AreEqual ("17-0D-30-34-30-36-33-30-30-34-30-30-30-30-5A", BitConverter.ToString (st.RawData), "RawData");
Assert.AreEqual (mono10release, st.SigningTime, "st.SigningTime");
Assert.AreEqual ("17 0d 30 34 30 36 33 30 30 34 30 30 30 30 5a", st.Format (true), "Format(true)");
Assert.AreEqual ("17 0d 30 34 30 36 33 30 30 34 30 30 30 30 5a", st.Format (false), "Format(false)");
}
[Test]
public void Constructor_Bytes ()
{
byte[] date = new byte [15] { 0x17, 0x0D, 0x30, 0x34, 0x30, 0x36, 0x33, 0x30, 0x30, 0x34, 0x30, 0x30, 0x30, 0x30, 0x5A };
Pkcs9SigningTime st = new Pkcs9SigningTime (date);
Assert.AreEqual (signingTimeName, st.Oid.FriendlyName, "Oid.FriendlyName");
Assert.AreEqual (signingTimeOid, st.Oid.Value, "Oid.Value");
Assert.AreEqual (15, st.RawData.Length, "RawData.Length");
Assert.AreEqual ("17-0D-30-34-30-36-33-30-30-34-30-30-30-30-5A", BitConverter.ToString (st.RawData), "RawData");
Assert.AreEqual (mono10release, st.SigningTime, "st.SigningTime");
Assert.AreEqual ("17 0d 30 34 30 36 33 30 30 34 30 30 30 30 5a", st.Format (true), "Format(true)");
Assert.AreEqual ("17 0d 30 34 30 36 33 30 30 34 30 30 30 30 5a", st.Format (false), "Format(false)");
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void Constructor_Bytes_Null ()
{
Pkcs9SigningTime st = new Pkcs9SigningTime (null);
}
[Test]
// [Ignore ("MS returns bad results (original time) - Mono needs to override CopyFrom to fix")]
// http://lab.msdn.microsoft.com/ProductFeedback/viewfeedback.aspx?feedbackid=66943396-ad73-497f-82ae-090b87ffcb4e
public void CopyFrom ()
{
Pkcs9SigningTime st1 = new Pkcs9SigningTime (mono10release);
Pkcs9SigningTime st2 = new Pkcs9SigningTime (DateTime.UtcNow);
st1.CopyFrom (st2);
Assert.AreEqual (st2.Oid.FriendlyName, st1.Oid.FriendlyName, "Oid.FriendlyName");
Assert.AreEqual (st2.Oid.Value, st1.Oid.Value, "Oid.Value");
Assert.AreEqual (BitConverter.ToString (st2.RawData), BitConverter.ToString (st1.RawData), "RawData");
// Note: Some timing resolution is lost by goind to ASN.1
Assert.AreEqual (st2.SigningTime.ToString (), st1.SigningTime.ToString (), "SigningTime");
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void CopyFrom_Null ()
{
new Pkcs9SigningTime (mono10release).CopyFrom (null);
}
[Test]
// [Ignore ("MS doesn't throw but returns bad results - Mono needs to override CopyFrom to fix")]
// http://lab.msdn.microsoft.com/ProductFeedback/viewfeedback.aspx?feedbackid=66943396-ad73-497f-82ae-090b87ffcb4e
[ExpectedException (typeof (CryptographicException))]
public void CopyFrom_Bad ()
{
Pkcs9SigningTime st = new Pkcs9SigningTime (mono10release);
Pkcs9DocumentName dn = new Pkcs9DocumentName ("Mono");
st.CopyFrom (dn);
Assert.AreEqual (dn.Oid.FriendlyName, st.Oid.FriendlyName, "Oid.FriendlyName");
Assert.AreEqual (dn.Oid.Value, st.Oid.Value, "Oid.Value");
Assert.AreEqual (BitConverter.ToString (dn.RawData), BitConverter.ToString (st.RawData), "RawData");
// wrong ASN.1
Assert.AreEqual (mono10release, st.SigningTime, "SigningTime");
}
}
}
#endif
|