Merge pull request #1057 from lextm/master
[mono.git] / mcs / class / Mono.Security / Test / Mono.Security.X509.Extensions / AuthorityKeyIdentifierExtensionTest.cs
1 //
2 // AuthorityKeyIdentifierExtensionTest.cs - NUnit Test Cases for 
3 //      Mono.Security.X509.Extensions.AuthorityKeyIdentifierExtension
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 AuthorityKeyIdentifierExtensionTest
34         {
35                 private void Empty (AuthorityKeyIdentifierExtension aki)
36                 {
37                         Assert.IsFalse (aki.Critical, "Critical");
38                         Assert.AreEqual ("2.5.29.35", aki.Oid, "Oid");
39                         Assert.IsNotNull (aki.Name, "Name");
40                         Assert.IsFalse (aki.Name == aki.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                         }, aki.Identifier, "Identifier");
47                 }
48
49                 [Test]
50                 public void Constructor_Empty ()
51                 {
52                         AuthorityKeyIdentifierExtension aki = new AuthorityKeyIdentifierExtension ();
53                         aki.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 (aki);
60                 }
61
62                 [Test]
63                 public void Constructor_Extension ()
64                 {
65                         AuthorityKeyIdentifierExtension ext = new AuthorityKeyIdentifierExtension ();
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                         AuthorityKeyIdentifierExtension aki = new AuthorityKeyIdentifierExtension (ext);
73                         Empty (aki);
74                 }
75
76                 [Test]
77                 public void Constructor_ASN1 ()
78                 {
79                         AuthorityKeyIdentifierExtension ext = new AuthorityKeyIdentifierExtension ();
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                         AuthorityKeyIdentifierExtension aki = new AuthorityKeyIdentifierExtension (ext.ASN1);
87                         Empty (aki);
88                 }
89
90                 [Test]
91                 public void AuthorityKeyIdentifier_Critical ()
92                 {
93                         AuthorityKeyIdentifierExtension aki = new AuthorityKeyIdentifierExtension ();
94                         aki.Critical = true;
95                         aki.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-22-06-03-55-1D-23-01-01-FF-04-18-30-16-80-14-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00", BitConverter.ToString (aki.GetBytes ()), "GetBytes");
102
103                         AuthorityKeyIdentifierExtension aki2 = new AuthorityKeyIdentifierExtension (aki.ASN1);
104                         Assert.IsTrue (aki2.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                         }, aki2.Identifier, "Identifier");
111                 }
112
113                 [Test]
114                 [ExpectedException (typeof(InvalidOperationException))]
115                 public void EmptyIdentifier ()
116                 {
117                         AuthorityKeyIdentifierExtension ext = new AuthorityKeyIdentifierExtension ();
118                         AuthorityKeyIdentifierExtension aki = new AuthorityKeyIdentifierExtension (ext);
119                         Empty (aki);
120                 }
121         }
122 }