KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > model > ca > certextensions > TestBasicCertificateExtension


1 package org.ejbca.core.model.ca.certextensions;
2
3 import java.util.Properties JavaDoc;
4
5 import junit.framework.TestCase;
6
7 import org.bouncycastle.asn1.DERBitString;
8 import org.bouncycastle.asn1.DERBoolean;
9 import org.bouncycastle.asn1.DEREncodable;
10 import org.bouncycastle.asn1.DERInteger;
11 import org.bouncycastle.asn1.DERNull;
12 import org.bouncycastle.asn1.DEROctetString;
13 import org.bouncycastle.asn1.DERPrintableString;
14 import org.bouncycastle.asn1.DERUTF8String;
15
16 public class TestBasicCertificateExtension extends TestCase {
17
18     
19     public void test01NullBasicExtension() throws Exception JavaDoc{
20         Properties JavaDoc props = new Properties JavaDoc();
21         props.put("id1.property.encoding", "DERNULL");
22         
23         BasicCertificateExtension baseExt = new BasicCertificateExtension();
24         baseExt.init(1, "1.2.3", false, props);
25         
26         DEREncodable value = baseExt.getValue(null, null, null);
27         assertTrue(value.getClass().toString(),value instanceof DERNull);
28         assertTrue(baseExt.getOID().equals("1.2.3"));
29         assertTrue(baseExt.getId() == 1);
30         assertFalse(baseExt.isCriticalFlag());
31     }
32     
33     public void test02IntegerBasicExtension() throws Exception JavaDoc{
34         Properties JavaDoc props = new Properties JavaDoc();
35         props.put("id1.property.encoding", "DERINTEGER");
36         props.put("id1.property.value", "1234");
37         
38         BasicCertificateExtension baseExt = new BasicCertificateExtension();
39         baseExt.init(1, "1.2.3", false, props);
40         
41         DEREncodable value = baseExt.getValue(null, null, null);
42         assertTrue(value.getClass().toString(),value instanceof DERInteger);
43         assertTrue(((DERInteger)value).toString(),((DERInteger)value).toString().equals("1234"));
44         assertTrue(baseExt.getOID().equals("1.2.3"));
45         assertTrue(baseExt.getId() == 1);
46         assertFalse(baseExt.isCriticalFlag());
47         
48         props = new Properties JavaDoc();
49         props.put("id1.property.encoding", "DERINTEGER");
50         props.put("id1.property.value", "123SA4");
51         boolean exceptionThrown = false;
52         try{
53           baseExt = new BasicCertificateExtension();
54           baseExt.init(1, "1.2.3", false, props);
55           value = baseExt.getValue(null, null, null);
56         }catch(CertificateExtentionConfigurationException e){
57             exceptionThrown = true;
58         }
59         assertTrue(exceptionThrown);
60     
61     }
62     
63     public void test03BitStringBasicExtension() throws Exception JavaDoc{
64         Properties JavaDoc props = new Properties JavaDoc();
65         props.put("id1.property.encoding", "DERBITSTRING");
66         props.put("id1.property.value", "1111");
67         
68         BasicCertificateExtension baseExt = new BasicCertificateExtension();
69         baseExt.init(1, "1.2.3", false, props);
70         
71         byte[] result = {15};
72         DEREncodable value = baseExt.getValue(null, null, null);
73         assertTrue(value.getClass().toString(),value instanceof DERBitString);
74         assertTrue(((DERBitString)value).getBytes()[0]+"",((DERBitString)value).getBytes()[0] == result[0]);
75         assertTrue(((DERBitString)value).getPadBits() == 4);
76         assertTrue(baseExt.getOID().equals("1.2.3"));
77         assertTrue(baseExt.getId() == 1);
78         assertFalse(baseExt.isCriticalFlag());
79     }
80     
81     public void test04BooleanBasicExtension() throws Exception JavaDoc{
82         Properties JavaDoc props = new Properties JavaDoc();
83         props.put("id1.property.encoding", "DERBOOLEAN");
84         props.put("id1.property.value", "true");
85         
86         BasicCertificateExtension baseExt = new BasicCertificateExtension();
87         baseExt.init(1, "1.2.3", false, props);
88         
89         DEREncodable value = baseExt.getValue(null, null, null);
90         assertTrue(value.getClass().toString(),value instanceof DERBoolean);
91         assertTrue(((DERBoolean)value).toString(),((DERBoolean)value).toString().equals("TRUE"));
92         assertTrue(baseExt.getOID().equals("1.2.3"));
93         assertTrue(baseExt.getId() == 1);
94         assertFalse(baseExt.isCriticalFlag());
95         
96         props.put("id1.property.value", "false");
97         
98         baseExt = new BasicCertificateExtension();
99         baseExt.init(1, "1.2.3", false, props);
100         
101         value = baseExt.getValue(null, null, null);
102         assertTrue(((DERBoolean)value).toString(),((DERBoolean)value).toString().equals("FALSE"));
103         
104         props = new Properties JavaDoc();
105         props.put("id1.property.encoding", "DERBOOLEAN");
106         props.put("id1.property.value", "1sdf");
107         boolean exceptionThrown = false;
108         try{
109           baseExt = new BasicCertificateExtension();
110           baseExt.init(1, "1.2.3", false, props);
111           value = baseExt.getValue(null, null, null);
112         }catch(CertificateExtentionConfigurationException e){
113             exceptionThrown = true;
114         }
115         assertTrue(exceptionThrown);
116     }
117     
118     public void test05OctetBasicExtension() throws Exception JavaDoc{
119         Properties JavaDoc props = new Properties JavaDoc();
120         props.put("id1.property.encoding", "DEROCTETSTRING");
121         props.put("id1.property.value", "DBE81232");
122         
123         BasicCertificateExtension baseExt = new BasicCertificateExtension();
124         baseExt.init(1, "1.2.3", false, props);
125         
126         DEREncodable value = baseExt.getValue(null, null, null);
127         assertTrue(value.getClass().toString(),value instanceof DEROctetString);
128         assertTrue(((DEROctetString)value).toString(),((DEROctetString)value).toString().equalsIgnoreCase("#DBE81232"));
129         
130         props = new Properties JavaDoc();
131         props.put("id1.property.encoding", "DEROCTETSTRING");
132         props.put("id1.property.value", "123SA4");
133         boolean exceptionThrown = false;
134         try{
135           baseExt = new BasicCertificateExtension();
136           baseExt.init(1, "1.2.3", false, props);
137           value = baseExt.getValue(null, null, null);
138         }catch(CertificateExtentionConfigurationException e){
139             exceptionThrown = true;
140         }
141         assertTrue(exceptionThrown);
142
143     }
144     
145     public void test06PritableStringExtension() throws Exception JavaDoc{
146         Properties JavaDoc props = new Properties JavaDoc();
147         props.put("id1.property.encoding", "DERPRINTABLESTRING");
148         props.put("id1.property.value", "This is a printable string");
149         
150         BasicCertificateExtension baseExt = new BasicCertificateExtension();
151         baseExt.init(1, "1.2.3", false, props);
152         
153         DEREncodable value = baseExt.getValue(null, null, null);
154         assertTrue(value.getClass().toString(),value instanceof DERPrintableString);
155         assertTrue(((DERPrintableString)value).toString(),((DERPrintableString)value).toString().equals("This is a printable string"));
156         
157         props = new Properties JavaDoc();
158         props.put("id1.property.encoding", "DERPRINTABLESTRING");
159         props.put("id1.property.value", "This is a non printable string ");
160         boolean exceptionThrown = false;
161         try{
162           baseExt = new BasicCertificateExtension();
163           baseExt.init(1, "1.2.3", false, props);
164           value = baseExt.getValue(null, null, null);
165         }catch(CertificateExtentionConfigurationException e){
166             exceptionThrown = true;
167         }
168         assertTrue(exceptionThrown);
169         
170     }
171     
172     public void test07UTF8StringExtension() throws Exception JavaDoc{
173         Properties JavaDoc props = new Properties JavaDoc();
174         props.put("id1.property.encoding", "DERUTF8STRING");
175         props.put("id1.property.value", "This is a utf8 string");
176         
177         BasicCertificateExtension baseExt = new BasicCertificateExtension();
178         baseExt.init(1, "1.2.3", false, props);
179         
180         DEREncodable value = baseExt.getValue(null, null, null);
181         assertTrue(value.getClass().toString(),value instanceof DERUTF8String);
182         assertTrue(((DERUTF8String)value).getString(),((DERUTF8String)value).getString().equals("This is a utf8 string"));
183         
184     }
185     
186     public void test08WrongEncoding() throws Exception JavaDoc{
187         Properties JavaDoc props = new Properties JavaDoc();
188         props.put("id1.property.encoding", "DERUTF8sdfTRING");
189         props.put("id1.property.value", "This is a utf8 string");
190
191         BasicCertificateExtension baseExt = new BasicCertificateExtension();
192         baseExt.init(1, "1.2.3", false, props);
193         boolean exceptionThrown =false;
194         try{
195
196             baseExt.getValue(null, null, null);
197         }catch(CertificateExtentionConfigurationException e){
198             exceptionThrown = true;
199         }
200         assertTrue(exceptionThrown);
201     }
202     
203 }
204
Popular Tags