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 junit.framework.TestCase;
26
27import org.apache.harmony.security.pkcs10.CertificationRequest;
28import org.apache.harmony.security.pkcs10.CertificationRequestInfo;
29import org.apache.harmony.security.x501.AttributeTypeAndValue;
30import org.apache.harmony.security.x501.AttributeValue;
31import org.apache.harmony.security.x501.Name;
32import org.apache.harmony.security.x509.AlgorithmIdentifier;
33import org.apache.harmony.security.x509.SubjectPublicKeyInfo;
34
35
36public class CertificationRequestTest extends TestCase {
37
38    /**
39     * Test method for 'com.openintel.drl.security.pkcs10.CertificationRequest'.
40     * Creates CertificationRequest, gets its values, encodes and decodes the
41     * encoded form.
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"), new byte[4]);
48        List attributes = new ArrayList();
49        // 1.2.840.113549.1.9.1 is OID of EMAILADDRESS
50        attributes.add(new AttributeTypeAndValue("1.2.840.113549.1.9.1",
51                new AttributeValue("a@b.com", false)));
52        CertificationRequestInfo certReqInfo = new CertificationRequestInfo(
53                version, subject, spki, attributes);
54        AlgorithmIdentifier signatureAlgId = new AlgorithmIdentifier(
55                "1.2.3.44.555");
56        byte[] signature = { (byte) 0x01, (byte) 0x02, (byte) 0x03,
57                (byte) 0x04, (byte) 0x05 };
58
59        CertificationRequest certReq = new CertificationRequest(certReqInfo,
60                signatureAlgId, signature);
61
62        // check what we have constructed
63        assertEquals(certReqInfo, certReq.getInfo());
64        assertEquals(signatureAlgId, certReq.getAlgId());
65        assertTrue(Arrays.equals(signature, certReq.getSignature()));
66
67        // decode the encoded CSR
68        byte[] encoding = certReq.getEncoded();
69        CertificationRequest decoded = (CertificationRequest) CertificationRequest.ASN1
70                .decode(encoding);
71
72        // check what was decoded
73        CertificationRequestInfo decodedCRinfo = certReq.getInfo();
74
75        assertEquals(certReqInfo.getSubject(), decodedCRinfo.getSubject());
76        assertEquals(certReqInfo.getSubjectPublicKeyInfo(), decodedCRinfo
77                .getSubjectPublicKeyInfo());
78        assertEquals(certReqInfo.getVersion(), decodedCRinfo.getVersion());
79        assertEquals(certReqInfo.getAttributes(), decodedCRinfo.getAttributes());
80
81        assertEquals(certReq.getAlgId(), decoded.getAlgId());
82        assertTrue(Arrays.equals(certReq.getSignature(), decoded.getSignature()));
83    }
84}
85
86