[Mono.Security] Added AuthorityKeyIdentifierExtensionTest and SubjectKeyIdentifierExt...
[mono.git] / mcs / class / Mono.Security / Test / Mono.Security.X509.Extensions / SubjectKeyIdentifierExtensionTest.cs
1 //
2 // SubjectKeyIdentifierExtensionTest.cs - NUnit Test Cases for 
3 //      Mono.Security.X509.Extensions.SubjectKeyIdentifierExtension
4 //
5 // Authors:
6 //      Lex Li  <support@lextm.com>
7 //
8 // Copyright (C) 2014 Lex Li
9 // 
10 // Permission is hereby granted, free of charge, to any person obtaining a copy of this
11 // software and associated documentation files (the "Software"), to deal in the Software
12 // without restriction, including without limitation the rights to use, copy, modify, merge,
13 // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
14 // to whom the Software is furnished to do so, subject to the following conditions:
15 // 
16 // The above copyright notice and this permission notice shall be included in all copies or
17 // substantial portions of the Software.
18 // 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
20 // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
21 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
22 // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
25
26 using System;
27 using Mono.Security.X509.Extensions;
28 using NUnit.Framework;
29
30 namespace MonoTests.Mono.Security.X509.Extensions
31 {
32         [TestFixture]
33         public class SubjectKeyIdentifierExtensionTest
34         {
35                 private void Empty (SubjectKeyIdentifierExtension ski)
36                 {
37                         Assert.IsFalse (ski.Critical, "Critical");
38                         Assert.AreEqual ("2.5.29.14", ski.Oid, "Oid");
39                         Assert.IsNotNull (ski.Name, "Name");
40                         Assert.IsFalse (ski.Name == ski.Oid, "Name!=Oid");
41                         Assert.AreEqual (new byte[] {
42                                 0x00, 0x00, 0x00, 0x00, 0x00,
43                                 0x00, 0x00, 0x00, 0x00, 0x00,
44                                 0x00, 0x00, 0x00, 0x00, 0x00,
45                                 0x00, 0x00, 0x00, 0x00, 0x00
46                         }, ski.Identifier, "Identifier");
47                 }
48
49                 [Test]
50                 public void Constructor_Empty ()
51                 {
52                         SubjectKeyIdentifierExtension ski = new SubjectKeyIdentifierExtension ();
53                         ski.Identifier = new byte[] {
54                                 0x00, 0x00, 0x00, 0x00, 0x00,
55                                 0x00, 0x00, 0x00, 0x00, 0x00,
56                                 0x00, 0x00, 0x00, 0x00, 0x00,
57                                 0x00, 0x00, 0x00, 0x00, 0x00
58                         };
59                         Empty (ski);
60                 }
61
62                 [Test]
63                 public void Constructor_Extension ()
64                 {
65                         SubjectKeyIdentifierExtension ext = new SubjectKeyIdentifierExtension ();
66                         ext.Identifier = new byte[] {
67                                 0x00, 0x00, 0x00, 0x00, 0x00,
68                                 0x00, 0x00, 0x00, 0x00, 0x00,
69                                 0x00, 0x00, 0x00, 0x00, 0x00,
70                                 0x00, 0x00, 0x00, 0x00, 0x00
71                         };
72                         SubjectKeyIdentifierExtension ski = new SubjectKeyIdentifierExtension (ext);
73                         Empty (ski);
74                 }
75
76                 [Test]
77                 public void Constructor_ASN1 ()
78                 {
79                         SubjectKeyIdentifierExtension ext = new SubjectKeyIdentifierExtension ();
80                         ext.Identifier = new byte[] {
81                                 0x00, 0x00, 0x00, 0x00, 0x00,
82                                 0x00, 0x00, 0x00, 0x00, 0x00,
83                                 0x00, 0x00, 0x00, 0x00, 0x00,
84                                 0x00, 0x00, 0x00, 0x00, 0x00
85                         };
86                         SubjectKeyIdentifierExtension ski = new SubjectKeyIdentifierExtension (ext.ASN1);
87                         Empty (ski);
88                 }
89
90                 [Test]
91                 public void AuthorityKeyIdentifier_Critical ()
92                 {
93                         SubjectKeyIdentifierExtension ski = new SubjectKeyIdentifierExtension ();
94                         ski.Critical = true;
95                         ski.Identifier = new byte[] {
96                                 0x00, 0x00, 0x00, 0x00, 0x00,
97                                 0x00, 0x00, 0x00, 0x00, 0x00,
98                                 0x00, 0x00, 0x00, 0x00, 0x00,
99                                 0x00, 0x00, 0x00, 0x00, 0x00
100                         };
101                         Assert.AreEqual ("30-20-06-03-55-1D-0E-01-01-FF-04-16-04-14-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00", BitConverter.ToString (ski.GetBytes ()), "GetBytes");
102
103                         SubjectKeyIdentifierExtension ski2 = new SubjectKeyIdentifierExtension (ski.ASN1);
104                         Assert.IsTrue (ski2.Critical, "Critical");
105                         Assert.AreEqual (new byte[] {
106                                 0x00, 0x00, 0x00, 0x00, 0x00,
107                                 0x00, 0x00, 0x00, 0x00, 0x00,
108                                 0x00, 0x00, 0x00, 0x00, 0x00,
109                                 0x00, 0x00, 0x00, 0x00, 0x00
110                         }, ski2.Identifier, "Identifier");
111                 }
112
113                 [Test]
114                 [ExpectedException (typeof(InvalidOperationException))]
115                 public void EmptyIdentifier ()
116                 {
117                         SubjectKeyIdentifierExtension ext = new SubjectKeyIdentifierExtension ();
118                         SubjectKeyIdentifierExtension ski = new SubjectKeyIdentifierExtension (ext);
119                         Empty (ski);
120                 }
121         }
122 }