X509ExtensionTest.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // X509ExtensionTest.cs
  3. // - NUnit tests for X509Extension
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 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.Security.Cryptography;
  33. using System.Security.Cryptography.X509Certificates;
  34. namespace MonoTests.System.Security.Cryptography.X509Certificates {
  35. // used to test the protected constructor properly
  36. public class X509Ex : X509Extension {
  37. public X509Ex ()
  38. {
  39. }
  40. }
  41. [TestFixture]
  42. public class X509ExtensionTest {
  43. [Test]
  44. public void ConstructorEmpty ()
  45. {
  46. X509Ex ex = new X509Ex ();
  47. Assert.IsFalse (ex.Critical, "Critical");
  48. Assert.IsNull (ex.RawData, "RawData");
  49. Assert.IsNull (ex.Oid, "Oid");
  50. Assert.AreEqual (String.Empty, ex.Format (true), "Format(true)");
  51. Assert.AreEqual (String.Empty, ex.Format (false), "Format(false)");
  52. ex.Critical = true;
  53. Assert.IsTrue (ex.Critical, "Critical 2");
  54. ex.Oid = new Oid ("2.5.29.37");
  55. Assert.AreEqual ("2.5.29.37", ex.Oid.Value, "Oid.Value");
  56. Assert.AreEqual ("Enhanced Key Usage", ex.Oid.FriendlyName, "Oid.FriendlyName");
  57. ex.RawData = new byte[] { 0x30, 0x05, 0x06, 0x03, 0x2A, 0x03, 0x04 };
  58. Assert.AreEqual ("Unknown Key Usage (1.2.3.4)" + Environment.NewLine, ex.Format (true), "Format(true)");
  59. Assert.AreEqual ("Unknown Key Usage (1.2.3.4)", ex.Format (false), "Format(false)");
  60. }
  61. [Test]
  62. [ExpectedException (typeof (ArgumentNullException))]
  63. public void ConstructorAsnEncodedData_WithNullOid ()
  64. {
  65. AsnEncodedData aed = new AsnEncodedData (new byte[] { 0x30, 0x05, 0x06, 0x03, 0x2A, 0x03, 0x04 });
  66. X509Extension eku = new X509Extension (aed, true);
  67. }
  68. [Test]
  69. public void ConstructorAsnEncodedData ()
  70. {
  71. AsnEncodedData aed = new AsnEncodedData (new Oid ("2.5.29.37"), new byte[] { 0x30, 0x05, 0x06, 0x03, 0x2A, 0x03, 0x04 });
  72. X509Extension ex = new X509Extension (aed, true);
  73. Assert.IsTrue (ex.Critical, "Critical");
  74. Assert.AreEqual (7, ex.RawData.Length, "RawData"); // original Oid ignored
  75. Assert.AreEqual ("2.5.29.37", ex.Oid.Value, "Oid.Value");
  76. Assert.AreEqual ("Enhanced Key Usage", ex.Oid.FriendlyName, "Oid.FriendlyName");
  77. Assert.AreEqual ("Unknown Key Usage (1.2.3.4)" + Environment.NewLine, ex.Format (true), "Format(true)");
  78. Assert.AreEqual ("Unknown Key Usage (1.2.3.4)", ex.Format (false), "Format(false)");
  79. }
  80. [Test]
  81. public void ConstructorAsnEncodedData_BadAsn ()
  82. {
  83. AsnEncodedData aed = new AsnEncodedData ("1.2.3", new byte[0]);
  84. X509Extension ex = new X509Extension (aed, true);
  85. Assert.AreEqual (String.Empty, ex.Format (true), "Format(true)");
  86. Assert.AreEqual (String.Empty, ex.Format (false), "Format(false)");
  87. // no exception for an "empty" extension
  88. }
  89. [Test]
  90. public void ConstructorAsnEncodedData_BadAsnTag ()
  91. {
  92. AsnEncodedData aed = new AsnEncodedData ("1.2.3", new byte[] { 0x05, 0x00 });
  93. X509Extension ex = new X509Extension (aed, true);
  94. Assert.AreEqual ("05 00", ex.Format (true), "Format(true)");
  95. Assert.AreEqual ("05 00", ex.Format (false), "Format(false)");
  96. // no exception for an "unknown" (ASN.1 NULL) extension
  97. }
  98. [Test]
  99. public void ConstructorAsnEncodedData_BadAsnLength ()
  100. {
  101. AsnEncodedData aed = new AsnEncodedData ("1.2.3", new byte[] { 0x30, 0x01 });
  102. X509Extension ex = new X509Extension (aed, true);
  103. Assert.AreEqual ("30 01", ex.Format (true), "Format(true)");
  104. Assert.AreEqual ("30 01", ex.Format (false), "Format(false)");
  105. // no exception for an bad (invalid length) extension
  106. }
  107. [Test]
  108. [ExpectedException (typeof (NullReferenceException))]
  109. public void ConstructorAsnEncodedData_Null ()
  110. {
  111. X509Extension ex = new X509Extension ((AsnEncodedData)null, true);
  112. }
  113. [Test]
  114. [ExpectedException (typeof (ArgumentNullException))]
  115. public void ConstructorOid_Null ()
  116. {
  117. X509Extension ex = new X509Extension ((Oid)null, new byte[] { 0x30, 0x01 }, true);
  118. }
  119. [Test]
  120. [ExpectedException (typeof (ArgumentNullException))]
  121. public void ConstructorOid_RawNull ()
  122. {
  123. X509Extension ex = new X509Extension (new Oid ("1.2.3"), null, true);
  124. }
  125. [Test]
  126. [ExpectedException (typeof (ArgumentNullException))]
  127. public void ConstructorString_Null ()
  128. {
  129. X509Extension ex = new X509Extension ((string)null, new byte[] { 0x30, 0x01 }, true);
  130. }
  131. [Test]
  132. [ExpectedException (typeof (ArgumentNullException))]
  133. public void ConstructorString_RawNull ()
  134. {
  135. X509Extension ex = new X509Extension ("1.2.3", null, true);
  136. }
  137. [Test]
  138. [ExpectedException (typeof (ArgumentNullException))]
  139. public void CopyFrom_Null ()
  140. {
  141. X509Ex ex = new X509Ex ();
  142. ex.CopyFrom (null);
  143. }
  144. [Test]
  145. [ExpectedException (typeof (ArgumentException))]
  146. public void CopyFrom_AsnEncodedData ()
  147. {
  148. AsnEncodedData aed = new AsnEncodedData (new Oid ("2.5.29.37"), new byte[] { 0x30, 0x05, 0x06, 0x03, 0x2A, 0x03, 0x04 });
  149. // this is recognized as an Enhanced Key Usages extension
  150. Assert.AreEqual ("Unknown Key Usage (1.2.3.4)" + Environment.NewLine, aed.Format (true), "aed.Format(true)");
  151. Assert.AreEqual ("Unknown Key Usage (1.2.3.4)", aed.Format (false), "aed.Format(false)");
  152. X509Ex ex = new X509Ex ();
  153. // but won't be accepted by the CopyFrom method (no a X509Extension)
  154. ex.CopyFrom (aed);
  155. }
  156. [Test]
  157. public void Build_NetscapeCertTypeExtension ()
  158. {
  159. X509Extension ex = new X509Extension (new Oid ("2.16.840.1.113730.1.1"), new byte[] { 0x03, 0x02, 0x00, 0xFF }, false);
  160. // strangely no NewLine is being appended to Format(true)
  161. Assert.AreEqual ("SSL Client Authentication, SSL Server Authentication, SMIME, Signature, Unknown cert type, SSL CA, SMIME CA, Signature CA (ff)", ex.Format (true), "aed.Format(true)");
  162. Assert.AreEqual ("SSL Client Authentication, SSL Server Authentication, SMIME, Signature, Unknown cert type, SSL CA, SMIME CA, Signature CA (ff)", ex.Format (false), "aed.Format(false)");
  163. }
  164. }
  165. }
  166. #endif