PageRenderTime 26ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Security/Test/System.Security.Cryptography.Pkcs/Pkcs9SigningTimeTest.cs

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