2 // X509ExtensionTest.cs
3 // - NUnit tests for X509Extension
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
32 using NUnit.Framework;
35 using System.Security.Cryptography;
36 using System.Security.Cryptography.X509Certificates;
38 namespace MonoTests.System.Security.Cryptography.X509Certificates {
40 // used to test the protected constructor properly
41 public class X509Ex : X509Extension {
49 public class X509ExtensionTest {
52 public void ConstructorEmpty ()
54 X509Ex ex = new X509Ex ();
55 Assert.IsFalse (ex.Critical, "Critical");
56 Assert.IsNull (ex.RawData, "RawData");
57 Assert.IsNull (ex.Oid, "Oid");
58 Assert.AreEqual (String.Empty, ex.Format (true), "Format(true)");
59 Assert.AreEqual (String.Empty, ex.Format (false), "Format(false)");
62 Assert.IsTrue (ex.Critical, "Critical 2");
63 ex.Oid = new Oid ("2.5.29.37");
64 Assert.AreEqual ("2.5.29.37", ex.Oid.Value, "Oid.Value");
65 // FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
66 //Assert.AreEqual ("Enhanced Key Usage", ex.Oid.FriendlyName, "Oid.FriendlyName");
67 ex.RawData = new byte[] { 0x30, 0x05, 0x06, 0x03, 0x2A, 0x03, 0x04 };
68 // FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
69 //Assert.AreEqual ("Unknown Key Usage (1.2.3.4)" + Environment.NewLine, ex.Format (true), "Format(true)");
70 //Assert.AreEqual ("Unknown Key Usage (1.2.3.4)", ex.Format (false), "Format(false)");
74 [ExpectedException (typeof (ArgumentNullException))]
75 public void ConstructorAsnEncodedData_WithNullOid ()
77 AsnEncodedData aed = new AsnEncodedData (new byte[] { 0x30, 0x05, 0x06, 0x03, 0x2A, 0x03, 0x04 });
78 X509Extension eku = new X509Extension (aed, true);
82 public void ConstructorAsnEncodedData ()
84 AsnEncodedData aed = new AsnEncodedData (new Oid ("2.5.29.37"), new byte[] { 0x30, 0x05, 0x06, 0x03, 0x2A, 0x03, 0x04 });
85 X509Extension ex = new X509Extension (aed, true);
86 Assert.IsTrue (ex.Critical, "Critical");
87 Assert.AreEqual (7, ex.RawData.Length, "RawData"); // original Oid ignored
88 Assert.AreEqual ("2.5.29.37", ex.Oid.Value, "Oid.Value");
89 // FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
90 //Assert.AreEqual ("Enhanced Key Usage", ex.Oid.FriendlyName, "Oid.FriendlyName");
91 //Assert.AreEqual ("Unknown Key Usage (1.2.3.4)" + Environment.NewLine, ex.Format (true), "Format(true)");
92 //Assert.AreEqual ("Unknown Key Usage (1.2.3.4)", ex.Format (false), "Format(false)");
96 public void ConstructorAsnEncodedData_BadAsn ()
98 AsnEncodedData aed = new AsnEncodedData ("1.2.3", new byte[0]);
99 X509Extension ex = new X509Extension (aed, true);
100 Assert.AreEqual (String.Empty, ex.Format (true), "Format(true)");
101 Assert.AreEqual (String.Empty, ex.Format (false), "Format(false)");
102 // no exception for an "empty" extension
106 public void ConstructorAsnEncodedData_BadAsnTag ()
108 AsnEncodedData aed = new AsnEncodedData ("1.2.3", new byte[] { 0x05, 0x00 });
109 X509Extension ex = new X509Extension (aed, true);
110 Assert.AreEqual ("05 00", ex.Format (true), "Format(true)");
111 Assert.AreEqual ("05 00", ex.Format (false), "Format(false)");
112 // no exception for an "unknown" (ASN.1 NULL) extension
116 public void ConstructorAsnEncodedData_BadAsnLength ()
118 AsnEncodedData aed = new AsnEncodedData ("1.2.3", new byte[] { 0x30, 0x01 });
119 X509Extension ex = new X509Extension (aed, true);
120 Assert.AreEqual ("30 01", ex.Format (true), "Format(true)");
121 Assert.AreEqual ("30 01", ex.Format (false), "Format(false)");
122 // no exception for an bad (invalid length) extension
126 [ExpectedException (typeof (NullReferenceException))]
127 public void ConstructorAsnEncodedData_Null ()
129 X509Extension ex = new X509Extension ((AsnEncodedData)null, true);
133 [ExpectedException (typeof (ArgumentNullException))]
134 public void ConstructorOid_Null ()
136 X509Extension ex = new X509Extension ((Oid)null, new byte[] { 0x30, 0x01 }, true);
140 [ExpectedException (typeof (ArgumentNullException))]
141 public void ConstructorOid_RawNull ()
143 X509Extension ex = new X509Extension (new Oid ("1.2.3"), null, true);
147 [ExpectedException (typeof (ArgumentNullException))]
148 public void ConstructorString_Null ()
150 X509Extension ex = new X509Extension ((string)null, new byte[] { 0x30, 0x01 }, true);
154 [ExpectedException (typeof (ArgumentNullException))]
155 public void ConstructorString_RawNull ()
157 X509Extension ex = new X509Extension ("1.2.3", null, true);
161 [ExpectedException (typeof (ArgumentNullException))]
162 public void CopyFrom_Null ()
164 X509Ex ex = new X509Ex ();
169 [ExpectedException (typeof (ArgumentException))]
170 public void CopyFrom_AsnEncodedData ()
172 AsnEncodedData aed = new AsnEncodedData (new Oid ("2.5.29.37"), new byte[] { 0x30, 0x05, 0x06, 0x03, 0x2A, 0x03, 0x04 });
173 // this is recognized as an Enhanced Key Usages extension
174 // FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
175 //Assert.AreEqual ("Unknown Key Usage (1.2.3.4)" + Environment.NewLine, aed.Format (true), "aed.Format(true)");
176 //Assert.AreEqual ("Unknown Key Usage (1.2.3.4)", aed.Format (false), "aed.Format(false)");
177 X509Ex ex = new X509Ex ();
178 // but won't be accepted by the CopyFrom method (no a X509Extension)
183 public void Build_NetscapeCertTypeExtension ()
185 X509Extension ex = new X509Extension (new Oid ("2.16.840.1.113730.1.1"), new byte[] { 0x03, 0x02, 0x00, 0xFF }, false);
186 // strangely no NewLine is being appended to Format(true)
187 // FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
188 //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)");
189 //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)");