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
36
37public class CertificationRequestTest extends TestCase {
38
39    /**
40     * Test method for 'com.openintel.drl.security.pkcs10.CertificationRequest'.
41     * Creates CertificationRequest, gets its values, encodes and decodes the
42     * encoded form.
43     *
44     */
45    public void testCertificationRequest() throws IOException {
46        int version = 2;// 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        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(certReqInfo,
62                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.getSubject());
78        assertEquals(certReqInfo.getSubjectPublicKeyInfo(), decodedCRinfo
79                .getSubjectPublicKeyInfo());
80        assertEquals(certReqInfo.getVersion(), decodedCRinfo.getVersion());
81        assertEquals(certReqInfo.getAttributes(), decodedCRinfo.getAttributes());
82
83        assertEquals(certReq.getAlgId(), decoded.getAlgId());
84        assertTrue(Arrays.equals(certReq.getSignature(), decoded.getSignature()));
85    }
86}
87
88