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    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"), 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("1.2.840.113549.1.9.1",
52                new AttributeValue("a@b.com", false)));
53
54        CertificationRequestInfo certReqInfo = new CertificationRequestInfo(
55                version, subject, spki, attributes);
56
57        // check what we have constructed
58        assertEquals(version, certReqInfo.getVersion());
59        assertEquals(subject.getName(X500Principal.RFC1779), certReqInfo
60                .getSubject().getName(X500Principal.RFC1779));
61        assertTrue(Arrays.equals(spki.getEncoded(), certReqInfo
62                .getSubjectPublicKeyInfo().getEncoded()));
63        assertEquals(attributes, certReqInfo.getAttributes());
64
65        // decode the encoded CertificationRequestInfo
66        byte[] encoding = certReqInfo.getEncoded();
67        CertificationRequestInfo decoded =
68                (CertificationRequestInfo) CertificationRequestInfo.ASN1
69                        .decode(encoding);
70
71        // check what was decoded
72        assertEquals(certReqInfo.getVersion(), decoded.getVersion());
73        assertEquals(certReqInfo.getSubject().getName(X500Principal.CANONICAL),
74                decoded.getSubject().getName(X500Principal.CANONICAL));
75        assertTrue(Arrays.equals(certReqInfo.getSubjectPublicKeyInfo()
76                .getEncoded(), decoded.getSubjectPublicKeyInfo().getEncoded()));
77
78        AttributeTypeAndValue certReqInfoATaV = (AttributeTypeAndValue) certReqInfo
79                .getAttributes().get(0);
80        AttributeTypeAndValue decodedATaV = (AttributeTypeAndValue) decoded
81                .getAttributes().get(0);
82        assertEquals(certReqInfoATaV.getType(), decodedATaV.getType());
83    }
84}
85
86