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.xnet.provider.jsse;
19
20import java.io.ByteArrayInputStream;
21import java.io.IOException;
22import java.security.cert.CertificateException;
23import java.security.cert.CertificateFactory;
24import java.security.cert.X509Certificate;
25import java.util.Arrays;
26
27import javax.security.auth.x500.X500Principal;
28
29import junit.framework.TestCase;
30
31/**
32 * Test for <code>CertificateRequest</code> constructors and methods
33 *
34 */
35public class CertificateRequestTest extends TestCase {
36
37    private static String base64certEncoding = "-----BEGIN CERTIFICATE-----\n"
38            + "MIIC+jCCAragAwIBAgICAiswDAYHKoZIzjgEAwEBADAdMRswGQYDVQQKExJDZXJ0a"
39            + "WZpY2F0ZSBJc3N1ZXIwIhgPMTk3MDAxMTIxMzQ2NDBaGA8xOTcwMDEyNDAzMzMyMF"
40            + "owHzEdMBsGA1UEChMUU3ViamVjdCBPcmdhbml6YXRpb24wGTAMBgcqhkjOOAQDAQE"
41            + "AAwkAAQIDBAUGBwiBAgCqggIAVaOCAhQwggIQMA8GA1UdDwEB/wQFAwMBqoAwEgYD"
42            + "VR0TAQH/BAgwBgEB/wIBBTAUBgNVHSABAf8ECjAIMAYGBFUdIAAwZwYDVR0RAQH/B"
43            + "F0wW4EMcmZjQDgyMi5OYW1lggdkTlNOYW1lpBcxFTATBgNVBAoTDE9yZ2FuaXphdG"
44            + "lvboYaaHR0cDovL3VuaWZvcm0uUmVzb3VyY2UuSWSHBP///wCIByoDolyDsgMwDAY"
45            + "DVR0eAQH/BAIwADAMBgNVHSQBAf8EAjAAMIGZBgNVHSUBAf8EgY4wgYsGBFUdJQAG"
46            + "CCsGAQUFBwMBBggrBgEFBQcDAQYIKwYBBQUHAwIGCCsGAQUFBwMDBggrBgEFBQcDB"
47            + "AYIKwYBBQUHAwUGCCsGAQUFBwMGBggrBgEFBQcDBwYIKwYBBQUHAwgGCCsGAQUFBw"
48            + "MJBggrBgEFBQgCAgYKKwYBBAGCNwoDAwYJYIZIAYb4QgQBMA0GA1UdNgEB/wQDAgE"
49            + "BMA4GBCpNhgkBAf8EAwEBATBkBgNVHRIEXTBbgQxyZmNAODIyLk5hbWWCB2ROU05h"
50            + "bWWkFzEVMBMGA1UEChMMT3JnYW5pemF0aW9uhhpodHRwOi8vdW5pZm9ybS5SZXNvd"
51            + "XJjZS5JZIcE////AIgHKgOiXIOyAzAJBgNVHR8EAjAAMAoGA1UdIwQDAQEBMAoGA1"
52            + "UdDgQDAQEBMAoGA1UdIQQDAQEBMAwGByqGSM44BAMBAQADMAAwLQIUAL4QvoazNWP"
53            + "7jrj84/GZlhm09DsCFQCBKGKCGbrP64VtUt4JPmLjW1VxQA==\n"
54            + "-----END CERTIFICATE-----\n";
55
56public void testCertificateRequest() throws Exception {
57
58        CertificateFactory certFactory = CertificateFactory.getInstance("X509");
59        ByteArrayInputStream bais = new ByteArrayInputStream(base64certEncoding
60                .getBytes("UTF-8"));
61        X509Certificate cert = (X509Certificate) certFactory.generateCertificate(bais);
62        X509Certificate[] accepted = {cert};
63        X500Principal[] certificate_authorities = {cert.getIssuerX500Principal()};
64
65        byte[] certificate_types = new byte[] { CertificateRequest.RSA_SIGN,
66                CertificateRequest.RSA_FIXED_DH };
67        CertificateRequest message = new CertificateRequest(certificate_types,
68                accepted);
69        assertEquals("incorrect type", Handshake.CERTIFICATE_REQUEST, message
70                .getType());
71        assertTrue("incorrect CertificateRequest", Arrays.equals(
72                message.certificate_types, certificate_types));
73        assertTrue("incorrect CertificateRequest", Arrays.equals(
74                message.certificate_authorities, certificate_authorities));
75
76		HandshakeIODataStream out = new HandshakeIODataStream();
77		message.send(out);
78		byte[] encoded = out.getData(1000);
79        assertEquals("incorrect out data length", message.length(), encoded.length);
80
81		HandshakeIODataStream in = new HandshakeIODataStream();
82		in.append(encoded);
83		CertificateRequest message_2 = new CertificateRequest(in, message.length());
84        assertTrue("incorrect message decoding",
85                Arrays.equals(message.certificate_types, message_2.certificate_types));
86        assertTrue("incorrect message decoding",
87                Arrays.equals(message.certificate_authorities, message_2.certificate_authorities));
88
89		in.append(encoded);
90		try {
91			message_2 = new CertificateRequest(in, message.length() - 1);
92			fail("Small length: No expected AlertException");
93		} catch (AlertException e) {
94		}
95
96		in.append(encoded);
97		in.append(new byte[] { 1, 2, 3 });
98		try {
99			message_2 = new CertificateRequest(in, message.length() + 3);
100			fail("Extra bytes: No expected AlertException ");
101		} catch (AlertException e) {
102		}
103	}
104}
105