merge -r 60439:60440
[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         public class AsnEncodedDataTest {
42
43                 static byte[] asnNullBytes = { 0x05, 0x00 };
44                 static string asnNullString = "05 00";
45                 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 };
46                 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";
47
48                 [Test]
49                 public void Constructor_StringData ()
50                 {
51                         AsnEncodedData aed = new AsnEncodedData ("oid", asnNullBytes);
52                         Assert.AreEqual ("oid", aed.Oid.Value, "Oid.Value");
53                         Assert.IsNull (aed.Oid.FriendlyName, "Oid.FriendlyName");
54                         Assert.AreEqual (BitConverter.ToString (asnNullBytes), BitConverter.ToString (aed.RawData), "RawData");
55                         Assert.AreEqual (asnNullString, aed.Format (true), "Format");
56                 }
57
58                 [Test]
59                 [ExpectedException (typeof (ArgumentNullException))]
60                 public void Constructor_StringNullData () 
61                 {
62                         string oid = null; // do not confuse compiler
63                         AsnEncodedData aed = new AsnEncodedData (oid, asnNullBytes);
64                 }
65
66                 [Test]
67                 [ExpectedException (typeof (ArgumentNullException))]
68                 public void Constructor_StringDataNull () 
69                 {
70                         AsnEncodedData aed = new AsnEncodedData ("oid", null);
71                 }
72
73                 [Test]
74                 public void Constructor_OidData () 
75                 {
76                         Oid o = new Oid ("1.0");
77                         AsnEncodedData aed = new AsnEncodedData (o, asnNullBytes);
78                         Assert.AreEqual ("1.0", aed.Oid.Value, "Oid.Value");
79                         Assert.IsNull (aed.Oid.FriendlyName, "Oid.FriendlyName");
80                         Assert.AreEqual (BitConverter.ToString (asnNullBytes), BitConverter.ToString (aed.RawData), "RawData");
81                         Assert.AreEqual (asnNullString, aed.Format (true), "Format");
82                 }
83
84                 [Test]
85                 public void Constructor_OidNullData () 
86                 {
87                         // this is legal - http://lab.msdn.microsoft.com/ProductFeedback/viewfeedback.aspx?feedbackid=38336cfa-3b97-47da-ad4e-9522d557f001
88                         Oid o = null;
89                         AsnEncodedData aed = new AsnEncodedData (o, asnNullBytes);
90                         Assert.IsNull (aed.Oid, "Oid");
91                         Assert.AreEqual (BitConverter.ToString (asnNullBytes), BitConverter.ToString (aed.RawData), "RawData");
92                         Assert.AreEqual (asnNullString, aed.Format (true), "Format");
93                 }
94
95                 [Test]
96                 [ExpectedException (typeof (ArgumentNullException))]
97                 public void Constructor_OidDataNull () 
98                 {
99                         Oid o = new Oid ("1.0");
100                         AsnEncodedData aed = new AsnEncodedData (o, null);
101                 }
102
103                 [Test]
104                 public void Constructor_Asn () 
105                 {
106                         AsnEncodedData aed = new AsnEncodedData ("oid", asnNullBytes);
107                         AsnEncodedData aed2 = new AsnEncodedData (aed);
108                         Assert.AreEqual (aed.Oid.Value, aed2.Oid.Value, "Oid.Value");
109                         Assert.AreEqual (aed.Oid.FriendlyName, aed2.Oid.FriendlyName, "Oid.FriendlyName");
110                         Assert.AreEqual (BitConverter.ToString (aed.RawData), BitConverter.ToString (aed2.RawData), "RawData");
111                         string s1 = aed.Format (false); 
112                         string s2 = aed.Format (true);
113                         Assert.AreEqual (s1, s2, "Format");
114                 }
115
116                 [Test]
117                 [ExpectedException (typeof (ArgumentNullException))]
118                 public void Constructor_ByteArrayNull ()
119                 {
120                         byte[] array = null;
121                         AsnEncodedData aed = new AsnEncodedData (array);
122                 }
123
124                 [Test]
125                 [ExpectedException (typeof (ArgumentNullException))]
126                 public void Constructor_AsnNull ()
127                 {
128                         AsnEncodedData asn = null;
129                         AsnEncodedData aed = new AsnEncodedData (asn);
130                 }
131
132                 [Test]
133                 public void Oid_CreatedNull ()
134                 {
135                         AsnEncodedData aed = new AsnEncodedData ((Oid)null, asnNullBytes);
136                         Assert.IsNull (aed.Oid, "Oid 1");
137                         Oid o = new Oid ("1.2.3");
138                         aed.Oid = o;
139                         Assert.AreEqual ("1.2.3", aed.Oid.Value, "Oid 2");
140                         o.Value = "1.2.4";
141                         Assert.AreEqual ("1.2.3", aed.Oid.Value, "Oid 3"); // didn't change (copy)
142                         aed.Oid = null;
143                         Assert.IsNull (aed.Oid, "Oid 4");
144                 }
145
146                 [Test]
147                 public void Oid ()
148                 {
149                         AsnEncodedData aed = new AsnEncodedData ("1.2.3", asnNullBytes);
150                         Assert.AreEqual ("1.2.3", aed.Oid.Value, "Oid 1");
151                         aed.Oid.Value = "1.2.4";
152                         Assert.AreEqual ("1.2.4", aed.Oid.Value, "Oid 2"); // didn't change (copy)
153                         aed.Oid = null;
154                         Assert.IsNull (aed.Oid, "Oid 3");
155                 }
156
157                 [Test]
158                 public void RawData_CanModify ()
159                 {
160                         byte[] data = (byte[])asnNullBytes.Clone ();
161                         AsnEncodedData aed = new AsnEncodedData ("1.2.3", data);
162                         Assert.AreEqual (asnNullString, aed.Format (true), "Format 1");
163                         data[0] = 0x06;
164                         Assert.AreEqual (asnNullString, aed.Format (true), "Format 2"); ; // didn't change (copy)
165                         aed.RawData[0] = 0x07;
166                         Assert.AreEqual ("07 00", aed.Format (true), "Format 3"); // changed!
167                 }
168
169                 [Test]
170                 [ExpectedException (typeof (ArgumentNullException))]
171                 public void RawData ()
172                 {
173                         AsnEncodedData aed = new AsnEncodedData ((Oid)null, asnNullBytes);
174                         Assert.AreEqual (asnNullString, aed.Format (true), "Format 1");
175                         aed.RawData = null;
176                 }
177
178                 [Test]
179                 [ExpectedException (typeof (ArgumentNullException))]
180                 public void CopyFrom_Null ()
181                 {
182                         AsnEncodedData aed = new AsnEncodedData ((Oid)null, asnNullBytes);
183                         aed.CopyFrom (null);
184                 }
185
186                 [Test]
187                 public void CopyFrom ()
188                 {
189                         Oid o = new Oid ("1.2.3");
190                         byte[] data = (byte[])asnNullBytes.Clone ();
191                         AsnEncodedData aed = new AsnEncodedData (o, asnNullBytes);
192                         AsnEncodedData copy = new AsnEncodedData ((Oid)null, new byte [0]);
193                         copy.CopyFrom (aed);
194
195                         Assert.AreEqual (aed.Oid.Value, copy.Oid.Value, "Oid 1");
196                         Assert.AreEqual (aed.Format (true), copy.Format (true), "Format 1");
197
198                         aed.Oid = new Oid ("1.2.4");
199                         aed.RawData = new byte[1];
200
201                         Assert.AreEqual ("1.2.3", copy.Oid.Value, "Oid 2");
202                         Assert.AreEqual (asnNullString, copy.Format (true), "Format 2");
203                 }
204
205                 [Test]
206                 public void Format () 
207                 {
208                         AsnEncodedData aed = new AsnEncodedData ("1.2.840.113549.1.1.1", asnLongBytes);
209                         Assert.AreEqual ("1.2.840.113549.1.1.1", aed.Oid.Value, "Oid.Value");
210                         Assert.AreEqual ("RSA", aed.Oid.FriendlyName, "Oid.FriendlyName");
211                         Assert.AreEqual (BitConverter.ToString (asnLongBytes), BitConverter.ToString (aed.RawData), "RawData");
212                         string result = aed.Format (false);
213                         Assert.AreEqual (asnLongString, result, "Format(false)");
214                 }
215
216                 [Test]
217                 public void FormatMultiline ()
218                 {
219                         AsnEncodedData aed = new AsnEncodedData ("1.2.840.113549.1.1.1", asnLongBytes);
220                         Assert.AreEqual ("1.2.840.113549.1.1.1", aed.Oid.Value, "Oid.Value");
221                         Assert.AreEqual ("RSA", aed.Oid.FriendlyName, "Oid.FriendlyName");
222                         Assert.AreEqual (BitConverter.ToString (asnLongBytes), BitConverter.ToString (aed.RawData), "RawData");
223                         string result = aed.Format (true);
224                         Assert.AreEqual (asnLongString, result, "Format(true)");
225                 }
226
227                 [Test]
228                 [Category ("NotDotNet")] // FriendlyName should not only be English.
229                 public void Build_X509EnhancedKeyUsageExtension ()
230                 {
231                         AsnEncodedData aed = new AsnEncodedData (new byte[] { 0x30, 0x05, 0x06, 0x03, 0x2A, 0x03, 0x04 });
232                         Assert.AreEqual ("30 05 06 03 2a 03 04", aed.Format (true), "Format(true)");
233                         Assert.AreEqual ("30 05 06 03 2a 03 04", aed.Format (false), "Format(false)");
234                         aed.Oid = new Oid ("2.5.29.37");
235                         // and now "AsnEncodedData" knows how to (magically) decode the data without involving the class
236                         // FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
237                         Assert.AreEqual ("Unknown Key Usage (1.2.3.4)" + Environment.NewLine, aed.Format (true), "aed.Format(true)");
238                         Assert.AreEqual ("Unknown Key Usage (1.2.3.4)", aed.Format (false), "aed.Format(false)");
239                         // compare with the output of the "appropriate" class
240                         X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension (aed, false);
241                         // FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
242                         Assert.AreEqual ("Unknown Key Usage (1.2.3.4)" + Environment.NewLine, eku.Format (true), "eku.Format(true)");
243                         Assert.AreEqual ("Unknown Key Usage (1.2.3.4)", eku.Format (false), "eku.Format(false)");
244                 }
245
246                 [Test]
247                 [Category ("NotDotNet")] // FriendlyName should not only be English.
248                 // note: important to emulate in Mono because we need it for SSL/TLS
249                 public void Build_NetscapeCertTypeExtension ()
250                 {
251                         AsnEncodedData aed = new AsnEncodedData (new byte[] { 0x03, 0x02, 0x01, 0x06 });
252                         Assert.AreEqual ("03 02 01 06", aed.Format (true), "Format(true)");
253                         Assert.AreEqual ("03 02 01 06", aed.Format (false), "Format(false)");
254                         aed.Oid = new Oid ("2.16.840.1.113730.1.1");
255                         // and now "AsnEncodedData" knows how to (magically) decode the data without involving the class
256                         Assert.AreEqual ("SSL CA, SMIME CA (06)", aed.Format (true), "aed.Format(true)");
257                         Assert.AreEqual ("SSL CA, SMIME CA (06)", aed.Format (false), "aed.Format(false)");
258                         // note that the Fx doesn't "really" support this extension
259                         // and strangely no NewLine is being appended to Format(true)
260                         // finally this also means that the Oid "knowns" about oid not used in the Fx itself
261                         // FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
262                         Assert.AreEqual ("Netscape Cert Type", aed.Oid.FriendlyName, "FriendlyName");
263                         // anyway the answer is most probably CryptoAPI
264                 }
265
266                 [Test]
267                 [Category ("NotDotNet")] // FriendlyName should not only be English.
268                 // note: important to emulate in Mono because we need it for SSL/TLS
269                 public void Build_SubjectAltNameExtension ()
270                 {
271                         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 });
272                         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)");
273                         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)");
274                         aed.Oid = new Oid ("2.5.29.17");
275                         // and now "AsnEncodedData" knows how to (magically) decode the data without involving the class
276                         Assert.AreEqual ("DNS Name=www.mono-project.com" + Environment.NewLine, aed.Format (true), "aed.Format(true)");
277                         Assert.AreEqual ("DNS Name=www.mono-project.com", aed.Format (false), "aed.Format(false)");
278                         // note that the Fx doesn't "really" support this extension
279                         // finally this also means that the Oid "knowns" about oid not used in the Fx itself
280                         // FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows.
281                         Assert.AreEqual ("Subject Alternative Name", aed.Oid.FriendlyName, "FriendlyName");
282                         // anyway the answer is most probably CryptoAPI
283                 }
284         }
285 }
286
287 #endif