CertificationRequestInfoTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     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
18package org.apache.harmony.security.tests.pkcs10;
19
20import java.io.IOException;
21import java.util.ArrayList;
22import java.util.Arrays;
23import java.util.List;
24
25import javax.security.auth.x500.X500Principal;
26
27import junit.framework.TestCase;
28
29import org.apache.harmony.security.pkcs10.CertificationRequestInfo;
30import org.apache.harmony.security.x501.AttributeTypeAndValue;
31import org.apache.harmony.security.x501.AttributeValue;
32import org.apache.harmony.security.x501.Name;
33import org.apache.harmony.security.x509.AlgorithmIdentifier;
34import org.apache.harmony.security.x509.SubjectPublicKeyInfo;
35
36
37public class CertificationRequestInfoTest extends TestCase {
38
39    /**
40     * Test method for CertificationRequestInfo. Creates
41     * CertificationRequestInfo, gets its values, encodes and decodes the
42     * encoded form.
43     *
44     */
45    public void testCertificationRequestInfo() throws IOException {
46        int version = 2;// X.509 v3
47        Name subject = new Name("O=subject");
48        SubjectPublicKeyInfo spki = new SubjectPublicKeyInfo(
49                new AlgorithmIdentifier("1.2.840.113549.1.1.2"), 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("1.2.840.113549.1.9.1",
53                new AttributeValue("a@b.com", false)));
54
55        CertificationRequestInfo certReqInfo = new CertificationRequestInfo(
56                version, subject, spki, attributes);
57
58        // check what we have constructed
59        assertEquals(version, certReqInfo.getVersion());
60        assertEquals(subject.getName(X500Principal.RFC1779), certReqInfo
61                .getSubject().getName(X500Principal.RFC1779));
62        assertTrue(Arrays.equals(spki.getEncoded(), certReqInfo
63                .getSubjectPublicKeyInfo().getEncoded()));
64        assertEquals(attributes, certReqInfo.getAttributes());
65
66        // decode the encoded CertificationRequestInfo
67        byte[] encoding = certReqInfo.getEncoded();
68        CertificationRequestInfo decoded =
69                (CertificationRequestInfo) CertificationRequestInfo.ASN1
70                        .decode(encoding);
71
72        // check what was decoded
73        assertEquals(certReqInfo.getVersion(), decoded.getVersion());
74        assertEquals(certReqInfo.getSubject().getName(X500Principal.CANONICAL),
75                decoded.getSubject().getName(X500Principal.CANONICAL));
76        assertTrue(Arrays.equals(certReqInfo.getSubjectPublicKeyInfo()
77                .getEncoded(), decoded.getSubjectPublicKeyInfo().getEncoded()));
78
79        AttributeTypeAndValue certReqInfoATaV = (AttributeTypeAndValue) certReqInfo
80                .getAttributes().get(0);
81        AttributeTypeAndValue decodedATaV = (AttributeTypeAndValue) decoded
82                .getAttributes().get(0);
83        assertEquals(certReqInfoATaV.getType(), decodedATaV.getType());
84    }
85}
86
87