bf3627336f82f5f71aa938597dd7fd4db0b59a26
[mono.git] / mcs / class / System / Test / System.Security.Cryptography / AsnEncodedDataTest.cs
1 //
2 // AsnEncodedDataTest.cs - NUnit tests for AsnEncodedData
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 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 {
39
40         [TestFixture]
41 #if TARGET_JVM
42         [Ignore ("The class System.Security.Cryptography.AsnEncodedData - is not supported")]
43 #endif
44         public class AsnEncodedDataTest {
45 #if !TARGET_JVM
46                 static byte[] asnNullBytes = { 0x05, 0x00 };
47                 static string asnNullString = "05 00";
48                 static byte[] asnLongBytes = { 0x30,0x5C,0x02,0x55,0x2D,0x58,0xE9,0xBF,0xF0,0x31,0xCD,0x79,0x06,0x50,0x5A,0xD5,0x9E,0x0E,0x2C,0xE6,0xC2,0xF7,0xF9,0xD2,0xCE,0x55,0x64,0x85,0xB1,0x90,0x9A,0x92,0xB3,0x36,0xC1,0xBC,0xEA,0xC8,0x23,0xB7,0xAB,0x3A,0xA7,0x64,0x63,0x77,0x5F,0x84,0x22,0x8E,0xE5,0xB6,0x45,0xDD,0x46,0xAE,0x0A,0xDD,0x00,0xC2,0x1F,0xBA,0xD9,0xAD,0xC0,0x75,0x62,0xF8,0x95,0x82,0xA2,0x80,0xB1,0x82,0x69,0xFA,0xE1,0xAF,0x7F,0xBC,0x7D,0xE2,0x7C,0x76,0xD5,0xBC,0x2A,0x80,0xFB,0x02,0x03,0x01,0x00,0x01 };
49                 static string asnLongString = "30 5c 02 55 2d 58 e9 bf f0 31 cd 79 06 50 5a d5 9e 0e 2c e6 c2 f7 f9 d2 ce 55 64 85 b1 90 9a 92 b3 36 c1 bc ea c8 23 b7 ab 3a a7 64 63 77 5f 84 22 8e e5 b6 45 dd 46 ae 0a dd 00 c2 1f ba d9 ad c0 75 62 f8 95 82 a2 80 b1 82 69 fa e1 af 7f bc 7d e2 7c 76 d5 bc 2a 80 fb 02 03 01 00 01";
50
51                 [Test]
52                 public void Constructor_StringData ()
53                 {
54                         AsnEncodedData aed = new AsnEncodedData ("oid", asnNullBytes);
55                         Assert.AreEqual ("oid", aed.Oid.Value, "Oid.Value");
56                         Assert.IsNull (aed.Oid.FriendlyName, "Oid.FriendlyName");
57                         Assert.AreEqual (BitConverter.ToString (asnNullBytes), BitConverter.ToString (aed.RawData), "RawData");
58                         Assert.AreEqual (asnNullString, aed.Format (true), "Format");
59                 }
60
61                 [Test]
62                 [ExpectedException (typeof (ArgumentNullException))]
63                 public void Constructor_StringNullData () 
64                 {
65                         string oid = null; // do not confuse compiler
66                         AsnEncodedData aed = new AsnEncodedData (oid, asnNullBytes);
67                 }
68
69                 [Test]
70                 [ExpectedException (typeof (ArgumentNullException))]
71                 public void Constructor_StringDataNull () 
72                 {
73                         AsnEncodedData aed = new AsnEncodedData ("oid", null);
74                 }
75
76                 [Test]
77                 public void Constructor_OidData () 
78                 {
79                         Oid o = new Oid ("1.0");
80                         AsnEncodedData aed = new AsnEncodedData (o, asnNullBytes);
81                         Assert.AreEqual ("1.0", aed.Oid.Value, "Oid.Value");
82                         Assert.IsNull (aed.Oid.FriendlyName, "Oid.FriendlyName");
83                         Assert.AreEqual (BitConverter.ToString (asnNullBytes), BitConverter.ToString (aed.RawData), "RawData");
84                         Assert.AreEqual (asnNullString, aed.Format (true), "Format");
85                 }
86
87                 [Test]
88                 public void Constructor_OidNullData () 
89                 {
90                         // this is legal - http://lab.msdn.microsoft.com/ProductFeedback/viewfeedback.aspx?feedbackid=38336cfa-3b97-47da-ad4e-9522d557f001
91                         Oid o = null;
92                         AsnEncodedData aed = new AsnEncodedData (o, asnNullBytes);
93                         Assert.IsNull (aed.Oid, "Oid");
94                         Assert.AreEqual (BitConverter.ToString (asnNullBytes), BitConverter.ToString (aed.RawData), "RawData");
95                         Assert.AreEqual (asnNullString, aed.Format (true), "Format");
96                 }
97
98                 [Test]
99                 [ExpectedException (typeof (ArgumentNullException))]
100                 public void Constructor_OidDataNull () 
101                 {
102                         Oid o = new Oid ("1.0");
103                         AsnEncodedData aed = new AsnEncodedData (o, null);
104                 }
105
106                 [Test]
107                 public void Constructor_Asn () 
108                 {
109                         AsnEncodedData aed = new AsnEncodedData ("oid", asnNullBytes);
110                         AsnEncodedData aed2 = new AsnEncodedData (aed);
111                         Assert.AreEqual (aed.Oid.Value, aed2.Oid.Value, "Oid.Value");
112                         Assert.AreEqual (aed.Oid.FriendlyName, aed2.Oid.FriendlyName, "Oid.FriendlyName");
113                         Assert.AreEqual (BitConverter.ToString (aed.RawData), BitConverter.ToString (aed2.RawData), "RawData");
114                         string s1 = aed.Format (false); 
115                         string s2 = aed.Format (true);
116                         Assert.AreEqual (s1, s2, "Format");
117                 }
118
119                 [Test]
120                 [ExpectedException (typeof (ArgumentNullException))]
121                 public void Constructor_ByteArrayNull ()
122                 {
123                         byte[] array = null;
124                         AsnEncodedData aed = new AsnEncodedData (array);
125                 }
126
127                 [Test]
128                 [ExpectedException (typeof (ArgumentNullException))]
129                 public void Constructor_AsnNull ()
130                 {
131                         AsnEncodedData asn = null;
132                         AsnEncodedData aed = new AsnEncodedData (asn);
133                 }
134
135                 [Test]
136                 public void Oid_CreatedNull ()
137                 {
138                         AsnEncodedData aed = new AsnEncodedData ((Oid)null, asnNullBytes);
139                         Assert.IsNull (aed.Oid, "Oid 1");
140                         Oid o = new Oid ("1.2.3");
141                         aed.Oid = o;
142                         Assert.AreEqual ("1.2.3", aed.Oid.Value, "Oid 2");
143                         o.Value = "1.2.4";
144                         Assert.AreEqual ("1.2.3", aed.Oid.Value, "Oid 3"); // didn't change (copy)
145                         aed.Oid = null;
146                         Assert.IsNull (aed.Oid, "Oid 4");
147                 }
148
149                 [Test]
150                 public void Oid ()
151                 {
152                         AsnEncodedData aed = new AsnEncodedData ("1.2.3", asnNullBytes);
153                         Assert.AreEqual ("1.2.3", aed.Oid.Value, "Oid 1");
154                         aed.Oid.Value = "1.2.4";
155                         Assert.AreEqual ("1.2.4", aed.Oid.Value, "Oid 2"); // didn't change (copy)
156                         aed.Oid = null;
157                         Assert.IsNull (aed.Oid, "Oid 3");
158                 }
159
160                 [Test]
161                 public void RawData_CanModify ()
162                 {
163                         byte[] data = (byte[])asnNullBytes.Clone ();
164                         AsnEncodedData aed = new AsnEncodedData ("1.2.3", data);
165                         Assert.AreEqual (asnNullString, aed.Format (true), "Format 1");
166                         data[0] = 0x06;
167                         Assert.AreEqual (asnNullString, aed.Format (true), "Format 2"); ; // didn't change (copy)
168                         aed.RawData[0] = 0x07;
169                         Assert.AreEqual ("07 00", aed.Format (true), "Format 3"); // changed!
170                 }
171
172                 [Test]
173                 [ExpectedException (typeof (ArgumentNullException))]
174                 public void RawData ()
175                 {
176                         AsnEncodedData aed = new AsnEncodedData ((Oid)null, asnNullBytes);
177                         Assert.AreEqual (asnNullString, aed.Format (true), "Format 1");
178                         aed.RawData = null;
179                 }
180
181                 [Test]
182                 [ExpectedException (typeof (ArgumentNullException))]
183                 public void CopyFrom_Null ()
184                 {
185                         AsnEncodedData aed = new AsnEncodedData ((Oid)null, asnNullBytes);
186                         aed.CopyFrom (null);
187                 }
188
189                 [Test]
190                 public void CopyFrom ()
191                 {
192                         Oid o = new Oid ("1.2.3");
193                         byte[] data = (byte[])asnNullBytes.Clone ();
194                         AsnEncodedData aed = new AsnEncodedData (o, asnNullBytes);
195                         AsnEncodedData copy = new AsnEncodedData ((Oid)null, new byte [0]);
196                         copy.CopyFrom (aed);
197
198                         Assert.AreEqual (aed.Oid.Value, copy.Oid.Value, "Oid 1");
199                         Assert.AreEqual (aed.Format (true), copy.Format (true), "Format 1");
200
201                         aed.Oid = new Oid ("1.2.4");
202                         aed.RawData = new byte[1];
203
204                         Assert.AreEqual ("1.2.3", copy.Oid.Value, "Oid 2");
205                         Assert.AreEqual (asnNullString, copy.Format (true), "Format 2");
206                 }
207
208                 [Test]
209                 public void Format () 
210                 {
211                         AsnEncodedData aed = new AsnEncodedData ("1.2.840.113549.1.1.1", asnLongBytes);
212                         Assert.AreEqual ("1.2.840.113549.1.1.1", aed.Oid.Value, "Oid.Value");
213                         Assert.AreEqual ("RSA", aed.Oid.FriendlyName, "Oid.FriendlyName");
214                         Assert.AreEqual (BitConverter.ToString (asnLongBytes), BitConverter.ToString (aed.RawData), "RawData");
215                         string result = aed.Format (false);
216                         Assert.AreEqual (asnLongString, result, "Format(false)");
217                 }
218
219                 [Test]
220                 public void FormatMultiline ()
221                 {
222                         AsnEncodedData aed = new AsnEncodedData ("1.2.840.113549.1.1.1", asnLongBytes);
223                         Assert.AreEqual ("1.2.840.113549.1.1.1", aed.Oid.Value, "Oid.Value");
224                         Assert.AreEqual ("RSA", aed.Oid.FriendlyName, "Oid.FriendlyName");
225                         Assert.AreEqual (BitConverter.ToString (asnLongBytes), BitConverter.ToString (aed.RawData), "RawData");
226                         string result = aed.Format (true);
227                         Assert.AreEqual (asnLongString, result, "Format(true)");
228                 }
229
230                 [Test]
231                 [Category ("NotDotNet")] // FriendlyName should not only be English.
232                 public void Build_X509EnhancedKeyUsageExtension ()
233                 {
234                         AsnEncodedData aed = new AsnEncodedData (new byte[] { 0x30, 0x05, 0x06, 0x03, 0x2A, 0x03, 0x04 });
235                         Assert.AreEqual ("30 05 06 03 2a 03 04", aed.Format (true), "Format(true)");
236                         Assert.AreEqual ("30 05 06 03 2a 03 04", aed.Format (false), "Format(false)");
237                         aed.Oid = new Oid ("2.5.29.37");
238                         // and now "AsnEncodedData" knows how to (magically) decode the data without involving the class
239                         // FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
240                         Assert.AreEqual ("Unknown Key Usage (1.2.3.4)" + Environment.NewLine, aed.Format (true), "aed.Format(true)");
241                         Assert.AreEqual ("Unknown Key Usage (1.2.3.4)", aed.Format (false), "aed.Format(false)");
242                         // compare with the output of the "appropriate" class
243                         X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension (aed, false);
244                         // FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
245                         Assert.AreEqual ("Unknown Key Usage (1.2.3.4)" + Environment.NewLine, eku.Format (true), "eku.Format(true)");
246                         Assert.AreEqual ("Unknown Key Usage (1.2.3.4)", eku.Format (false), "eku.Format(false)");
247                 }
248
249                 [Test]
250                 [Category ("NotDotNet")] // FriendlyName should not only be English.
251                 // note: important to emulate in Mono because we need it for SSL/TLS
252                 public void Build_NetscapeCertTypeExtension ()
253                 {
254                         AsnEncodedData aed = new AsnEncodedData (new byte[] { 0x03, 0x02, 0x01, 0x06 });
255                         Assert.AreEqual ("03 02 01 06", aed.Format (true), "Format(true)");
256                         Assert.AreEqual ("03 02 01 06", aed.Format (false), "Format(false)");
257                         aed.Oid = new Oid ("2.16.840.1.113730.1.1");
258                         // and now "AsnEncodedData" knows how to (magically) decode the data without involving the class
259                         Assert.AreEqual ("SSL CA, SMIME CA (06)", aed.Format (true), "aed.Format(true)");
260                         Assert.AreEqual ("SSL CA, SMIME CA (06)", aed.Format (false), "aed.Format(false)");
261                         // note that the Fx doesn't "really" support this extension
262                         // and strangely no NewLine is being appended to Format(true)
263                         // finally this also means that the Oid "knowns" about oid not used in the Fx itself
264                         // FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
265                         Assert.AreEqual ("Netscape Cert Type", aed.Oid.FriendlyName, "FriendlyName");
266                         // anyway the answer is most probably CryptoAPI
267                 }
268
269                 [Test]
270                 [Category ("NotDotNet")] // FriendlyName should not only be English.
271                 // note: important to emulate in Mono because we need it for SSL/TLS
272                 public void Build_SubjectAltNameExtension ()
273                 {
274                         AsnEncodedData aed = new AsnEncodedData (new byte[] { 0x30, 0x16, 0x82, 0x14, 0x77, 0x77, 0x77, 0x2E, 0x6D, 0x6F, 0x6E, 0x6F, 0x2D, 0x70, 0x72, 0x6F, 0x6A, 0x65, 0x63, 0x74, 0x2E, 0x63, 0x6F, 0x6D });
275                         Assert.AreEqual ("30 16 82 14 77 77 77 2e 6d 6f 6e 6f 2d 70 72 6f 6a 65 63 74 2e 63 6f 6d", aed.Format (true), "Format(true)");
276                         Assert.AreEqual ("30 16 82 14 77 77 77 2e 6d 6f 6e 6f 2d 70 72 6f 6a 65 63 74 2e 63 6f 6d", aed.Format (false), "Format(false)");
277                         aed.Oid = new Oid ("2.5.29.17");
278                         // and now "AsnEncodedData" knows how to (magically) decode the data without involving the class
279                         Assert.AreEqual ("DNS Name=www.mono-project.com" + Environment.NewLine, aed.Format (true), "aed.Format(true)");
280                         Assert.AreEqual ("DNS Name=www.mono-project.com", aed.Format (false), "aed.Format(false)");
281                         // note that the Fx doesn't "really" support this extension
282                         // finally this also means that the Oid "knowns" about oid not used in the Fx itself
283                         // FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
284                         Assert.AreEqual ("Subject Alternative Name", aed.Oid.FriendlyName, "FriendlyName");
285                         // anyway the answer is most probably CryptoAPI
286                 }
287 #endif
288         }
289 }
290
291 #endif