2010-03-02 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Mono.Security / Test / Mono.Security.X509.Extensions / KeyUsageExtensionTest.cs
blob17bb8d743de0e7b2221f30e39282518878aa04af
1 //
2 // KeyUsageExtensionTest.cs - NUnit Test Cases for
3 // Mono.Security.X509.Extensions.KeyUsageExtension
4 //
5 // Authors:
6 // Sebastien Pouliot <sebastien@ximian.com>
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
11 using System;
12 using System.IO;
14 using Mono.Security;
15 using Mono.Security.X509;
16 using Mono.Security.X509.Extensions;
18 using NUnit.Framework;
20 namespace MonoTests.Mono.Security.X509.Extensions {
22 [TestFixture]
23 public class KeyUsageExtensionTest {
25 private void Empty (KeyUsageExtension kue)
27 Assert.IsFalse (kue.Critical, "Critical");
28 Assert.AreEqual ("2.5.29.15", kue.Oid, "Oid");
29 Assert.IsNotNull (kue.Name, "Name");
30 Assert.IsFalse (kue.Name == kue.Oid, "Name!=Oid");
31 Assert.AreEqual (KeyUsages.none, kue.KeyUsage, "KeyUsage");
32 Assert.IsTrue (kue.Support (KeyUsages.none), "Support(none)");
33 Assert.IsFalse (kue.Support (KeyUsages.digitalSignature), "Support(digitalSignature)");
34 Assert.IsFalse (kue.Support (KeyUsages.decipherOnly), "Support(decipherOnly)");
37 [Test]
38 public void Constructor_Empty ()
40 KeyUsageExtension kue = new KeyUsageExtension ();
41 Empty (kue);
44 [Test]
45 public void Constructor_Extension ()
47 KeyUsageExtension ext = new KeyUsageExtension ();
48 KeyUsageExtension kue = new KeyUsageExtension (ext);
49 Empty (kue);
52 [Test]
53 public void Constructor_ASN1 ()
55 KeyUsageExtension ext = new KeyUsageExtension ();
56 KeyUsageExtension kue = new KeyUsageExtension (ext.ASN1);
57 Empty (kue);
60 [Test]
61 public void KeyUsage ()
63 KeyUsageExtension kue = new KeyUsageExtension ();
64 foreach (KeyUsages ku in Enum.GetValues (typeof (KeyUsages))) {
65 kue.KeyUsage = ku;
66 byte[] rawext = kue.GetBytes ();
67 int length = 13;
68 if ((int) ku > Byte.MaxValue) {
69 length++;
70 Assert.AreEqual ((byte) ku, rawext[rawext.Length - 2], ku.ToString () + ".Value2");
71 Assert.AreEqual ((byte) ((int) ku >> 8), rawext[rawext.Length - 1], ku.ToString () + ".Value1");
72 } else {
73 Assert.AreEqual ((byte) ku, rawext[rawext.Length - 1], ku.ToString () + ".Value");
75 Assert.AreEqual (length, rawext.Length, ku.ToString () + ".Length");
79 [Test]
80 public void KeyUsage_MaxValue ()
82 KeyUsageExtension kue = new KeyUsageExtension ();
83 kue.KeyUsage = (KeyUsages) Int32.MaxValue;
84 Assert.IsTrue (kue.Support (KeyUsages.none), "Support(none)");
85 Assert.IsTrue (kue.Support (KeyUsages.digitalSignature), "Support(digitalSignature)");
86 Assert.IsTrue (kue.Support (KeyUsages.nonRepudiation), "Support(nonRepudiation)");
87 Assert.IsTrue (kue.Support (KeyUsages.keyEncipherment), "Support(keyEncipherment)");
88 Assert.IsTrue (kue.Support (KeyUsages.dataEncipherment), "Support(dataEncipherment)");
89 Assert.IsTrue (kue.Support (KeyUsages.keyAgreement), "Support(keyAgreement)");
90 Assert.IsTrue (kue.Support (KeyUsages.keyCertSign), "Support(keyCertSign)");
91 Assert.IsTrue (kue.Support (KeyUsages.cRLSign), "Support(cRLSign)");
92 Assert.IsTrue (kue.Support (KeyUsages.encipherOnly), "Support(encipherOnly)");
93 Assert.IsTrue (kue.Support (KeyUsages.decipherOnly), "Support(decipherOnly)");
96 [Test]
97 public void Critical ()
99 KeyUsageExtension kue = new KeyUsageExtension ();
100 kue.Critical = true;
101 foreach (KeyUsages ku in Enum.GetValues (typeof (KeyUsages))) {
102 kue.KeyUsage = ku;
103 byte[] rawext = kue.GetBytes ();
104 int length = 16;
105 if ((int) ku > Byte.MaxValue) {
106 length++;
107 Assert.AreEqual ((byte) ku, rawext[rawext.Length - 2], ku.ToString () + ".Value2");
108 Assert.AreEqual ((byte) ((int)ku >> 8), rawext[rawext.Length - 1], ku.ToString () + ".Value1");
109 } else {
110 Assert.AreEqual ((byte) ku, rawext[rawext.Length - 1], ku.ToString () + ".Value");
112 Assert.AreEqual (length, rawext.Length, ku.ToString () + ".Length");
113 Assert.AreEqual (1, rawext[7], ku.ToString () + ".Critical.Tag");
114 Assert.AreEqual (1, rawext[8], ku.ToString () + ".Critical.Length");
115 Assert.AreEqual (255, rawext[9], ku.ToString () + ".Critical.Value");