01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.harmony.security.tests.pkcs10;
19:
20: import java.io.IOException;
21: import java.util.ArrayList;
22: import java.util.Arrays;
23: import java.util.List;
24:
25: import junit.framework.TestCase;
26:
27: import org.apache.harmony.security.pkcs10.CertificationRequest;
28: import org.apache.harmony.security.pkcs10.CertificationRequestInfo;
29: import org.apache.harmony.security.x501.AttributeTypeAndValue;
30: import org.apache.harmony.security.x501.AttributeValue;
31: import org.apache.harmony.security.x501.Name;
32: import org.apache.harmony.security.x509.AlgorithmIdentifier;
33: import org.apache.harmony.security.x509.SubjectPublicKeyInfo;
34:
35: public class CertificationRequestTest extends TestCase {
36:
37: /**
38: * Test method for 'com.openintel.drl.security.pkcs10.CertificationRequest'.
39: * Creates CertificationRequest, gets its values, encodes and decodes the
40: * encoded form.
41: *
42: */
43: public void testCertificationRequest() throws IOException {
44: int version = 2;// v3
45: Name subject = new Name("O=subject");
46: SubjectPublicKeyInfo spki = new SubjectPublicKeyInfo(
47: new AlgorithmIdentifier("1.2.840.113549.1.1.2"),
48: new byte[4]);
49: List attributes = new ArrayList();
50: // 1.2.840.113549.1.9.1 is OID of EMAILADDRESS
51: attributes.add(new AttributeTypeAndValue(
52: "1.2.840.113549.1.9.1", new AttributeValue("a@b.com",
53: false)));
54: CertificationRequestInfo certReqInfo = new CertificationRequestInfo(
55: version, subject, spki, attributes);
56: AlgorithmIdentifier signatureAlgId = new AlgorithmIdentifier(
57: "1.2.3.44.555");
58: byte[] signature = { (byte) 0x01, (byte) 0x02, (byte) 0x03,
59: (byte) 0x04, (byte) 0x05 };
60:
61: CertificationRequest certReq = new CertificationRequest(
62: certReqInfo, signatureAlgId, signature);
63:
64: // check what we have constructed
65: assertEquals(certReqInfo, certReq.getInfo());
66: assertEquals(signatureAlgId, certReq.getAlgId());
67: assertTrue(Arrays.equals(signature, certReq.getSignature()));
68:
69: // decode the encoded CSR
70: byte[] encoding = certReq.getEncoded();
71: CertificationRequest decoded = (CertificationRequest) CertificationRequest.ASN1
72: .decode(encoding);
73:
74: // check what was decoded
75: CertificationRequestInfo decodedCRinfo = certReq.getInfo();
76:
77: assertEquals(certReqInfo.getSubject(), decodedCRinfo
78: .getSubject());
79: assertEquals(certReqInfo.getSubjectPublicKeyInfo(),
80: decodedCRinfo.getSubjectPublicKeyInfo());
81: assertEquals(certReqInfo.getVersion(), decodedCRinfo
82: .getVersion());
83: assertEquals(certReqInfo.getAttributes(), decodedCRinfo
84: .getAttributes());
85:
86: assertEquals(certReq.getAlgId(), decoded.getAlgId());
87: assertTrue(Arrays.equals(certReq.getSignature(), decoded
88: .getSignature()));
89: }
90: }
|