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 javax.security.auth.x500.X500Principal;
26:
27: import junit.framework.TestCase;
28:
29: import org.apache.harmony.security.pkcs10.CertificationRequestInfo;
30: import org.apache.harmony.security.x501.AttributeTypeAndValue;
31: import org.apache.harmony.security.x501.AttributeValue;
32: import org.apache.harmony.security.x501.Name;
33: import org.apache.harmony.security.x509.AlgorithmIdentifier;
34: import org.apache.harmony.security.x509.SubjectPublicKeyInfo;
35:
36: public class CertificationRequestInfoTest extends TestCase {
37:
38: /**
39: * Test method for CertificationRequestInfo. Creates
40: * CertificationRequestInfo, gets its values, encodes and decodes the
41: * encoded form.
42: *
43: */
44: public void testCertificationRequestInfo() throws IOException {
45: int version = 2;// X.509 v3
46: Name subject = new Name("O=subject");
47: SubjectPublicKeyInfo spki = new SubjectPublicKeyInfo(
48: new AlgorithmIdentifier("1.2.840.113549.1.1.2"),
49: new byte[4]);
50: List attributes = new ArrayList();
51: // 1.2.840.113549.1.9.1 is OID of EMAILADDRESS
52: attributes.add(new AttributeTypeAndValue(
53: "1.2.840.113549.1.9.1", new AttributeValue("a@b.com",
54: false)));
55:
56: CertificationRequestInfo certReqInfo = new CertificationRequestInfo(
57: version, subject, spki, attributes);
58:
59: // check what we have constructed
60: assertEquals(version, certReqInfo.getVersion());
61: assertEquals(subject.getName(X500Principal.RFC1779),
62: certReqInfo.getSubject().getName(X500Principal.RFC1779));
63: assertTrue(Arrays.equals(spki.getEncoded(), certReqInfo
64: .getSubjectPublicKeyInfo().getEncoded()));
65: assertEquals(attributes, certReqInfo.getAttributes());
66:
67: // decode the encoded CertificationRequestInfo
68: byte[] encoding = certReqInfo.getEncoded();
69: CertificationRequestInfo decoded = (CertificationRequestInfo) CertificationRequestInfo.ASN1
70: .decode(encoding);
71:
72: // check what was decoded
73: assertEquals(certReqInfo.getVersion(), decoded.getVersion());
74: assertEquals(certReqInfo.getSubject().getName(
75: X500Principal.CANONICAL), decoded.getSubject().getName(
76: X500Principal.CANONICAL));
77: assertTrue(Arrays.equals(certReqInfo.getSubjectPublicKeyInfo()
78: .getEncoded(), decoded.getSubjectPublicKeyInfo()
79: .getEncoded()));
80:
81: AttributeTypeAndValue certReqInfoATaV = (AttributeTypeAndValue) certReqInfo
82: .getAttributes().get(0);
83: AttributeTypeAndValue decodedATaV = (AttributeTypeAndValue) decoded
84: .getAttributes().get(0);
85: assertEquals(certReqInfoATaV.getType(), decodedATaV.getType());
86: }
87: }
|