* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System / Test / System.Security.Cryptography.X509Certificates / X509ExtensionTest.cs
1 //
2 // X509ExtensionTest.cs 
3 //      - NUnit tests for X509Extension
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
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
30 #if NET_2_0
31
32 using NUnit.Framework;
33
34 using System;
35 using System.Security.Cryptography;
36 using System.Security.Cryptography.X509Certificates;
37
38 namespace MonoTests.System.Security.Cryptography.X509Certificates {
39
40         // used to test the protected constructor properly
41         public class X509Ex : X509Extension {
42
43                 public X509Ex ()
44                 {
45                 }
46         }
47
48         [TestFixture]
49         public class X509ExtensionTest {
50
51                 [Test]
52                 public void ConstructorEmpty ()
53                 {
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)");
60                         
61                         ex.Critical = true;
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)");
71                 }
72
73                 [Test]
74                 [ExpectedException (typeof (ArgumentNullException))]
75                 public void ConstructorAsnEncodedData_WithNullOid ()
76                 {
77                         AsnEncodedData aed = new AsnEncodedData (new byte[] { 0x30, 0x05, 0x06, 0x03, 0x2A, 0x03, 0x04 });
78                         X509Extension eku = new X509Extension (aed, true);
79                 }
80
81                 [Test]
82                 public void ConstructorAsnEncodedData ()
83                 {
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)");
93                 }
94
95                 [Test]
96                 public void ConstructorAsnEncodedData_BadAsn ()
97                 {
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
103                 }
104
105                 [Test]
106                 public void ConstructorAsnEncodedData_BadAsnTag ()
107                 {
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
113                 }
114
115                 [Test]
116                 public void ConstructorAsnEncodedData_BadAsnLength ()
117                 {
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
123                 }
124
125                 [Test]
126                 [ExpectedException (typeof (NullReferenceException))]
127                 public void ConstructorAsnEncodedData_Null ()
128                 {
129                         X509Extension ex = new X509Extension ((AsnEncodedData)null, true);
130                 }
131
132                 [Test]
133                 [ExpectedException (typeof (ArgumentNullException))]
134                 public void ConstructorOid_Null ()
135                 {
136                         X509Extension ex = new X509Extension ((Oid)null, new byte[] { 0x30, 0x01 }, true);
137                 }
138
139                 [Test]
140                 [ExpectedException (typeof (ArgumentNullException))]
141                 public void ConstructorOid_RawNull ()
142                 {
143                         X509Extension ex = new X509Extension (new Oid ("1.2.3"), null, true);
144                 }
145
146                 [Test]
147                 [ExpectedException (typeof (ArgumentNullException))]
148                 public void ConstructorString_Null ()
149                 {
150                         X509Extension ex = new X509Extension ((string)null, new byte[] { 0x30, 0x01 }, true);
151                 }
152
153                 [Test]
154                 [ExpectedException (typeof (ArgumentNullException))]
155                 public void ConstructorString_RawNull ()
156                 {
157                         X509Extension ex = new X509Extension ("1.2.3", null, true);
158                 }
159
160                 [Test]
161                 [ExpectedException (typeof (ArgumentNullException))]
162                 public void CopyFrom_Null ()
163                 {
164                         X509Ex ex = new X509Ex ();
165                         ex.CopyFrom (null);
166                 }
167
168                 [Test]
169                 [ExpectedException (typeof (ArgumentException))]
170                 public void CopyFrom_AsnEncodedData ()
171                 {
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)
179                         ex.CopyFrom (aed);
180                 }
181
182                 [Test]
183                 public void Build_NetscapeCertTypeExtension ()
184                 {
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)");
190                 }
191         }
192 }
193
194 #endif