001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.xnet.provider.jsse;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.IOException;
022: import java.security.cert.CertificateException;
023: import java.security.cert.CertificateFactory;
024: import java.security.cert.X509Certificate;
025: import java.util.Arrays;
026:
027: import javax.security.auth.x500.X500Principal;
028:
029: import junit.framework.TestCase;
030:
031: /**
032: * Test for <code>CertificateRequest</code> constructors and methods
033: *
034: */
035: public class CertificateRequestTest extends TestCase {
036:
037: private static String base64certEncoding = "-----BEGIN CERTIFICATE-----\n"
038: + "MIIC+jCCAragAwIBAgICAiswDAYHKoZIzjgEAwEBADAdMRswGQYDVQQKExJDZXJ0a"
039: + "WZpY2F0ZSBJc3N1ZXIwIhgPMTk3MDAxMTIxMzQ2NDBaGA8xOTcwMDEyNDAzMzMyMF"
040: + "owHzEdMBsGA1UEChMUU3ViamVjdCBPcmdhbml6YXRpb24wGTAMBgcqhkjOOAQDAQE"
041: + "AAwkAAQIDBAUGBwiBAgCqggIAVaOCAhQwggIQMA8GA1UdDwEB/wQFAwMBqoAwEgYD"
042: + "VR0TAQH/BAgwBgEB/wIBBTAUBgNVHSABAf8ECjAIMAYGBFUdIAAwZwYDVR0RAQH/B"
043: + "F0wW4EMcmZjQDgyMi5OYW1lggdkTlNOYW1lpBcxFTATBgNVBAoTDE9yZ2FuaXphdG"
044: + "lvboYaaHR0cDovL3VuaWZvcm0uUmVzb3VyY2UuSWSHBP///wCIByoDolyDsgMwDAY"
045: + "DVR0eAQH/BAIwADAMBgNVHSQBAf8EAjAAMIGZBgNVHSUBAf8EgY4wgYsGBFUdJQAG"
046: + "CCsGAQUFBwMBBggrBgEFBQcDAQYIKwYBBQUHAwIGCCsGAQUFBwMDBggrBgEFBQcDB"
047: + "AYIKwYBBQUHAwUGCCsGAQUFBwMGBggrBgEFBQcDBwYIKwYBBQUHAwgGCCsGAQUFBw"
048: + "MJBggrBgEFBQgCAgYKKwYBBAGCNwoDAwYJYIZIAYb4QgQBMA0GA1UdNgEB/wQDAgE"
049: + "BMA4GBCpNhgkBAf8EAwEBATBkBgNVHRIEXTBbgQxyZmNAODIyLk5hbWWCB2ROU05h"
050: + "bWWkFzEVMBMGA1UEChMMT3JnYW5pemF0aW9uhhpodHRwOi8vdW5pZm9ybS5SZXNvd"
051: + "XJjZS5JZIcE////AIgHKgOiXIOyAzAJBgNVHR8EAjAAMAoGA1UdIwQDAQEBMAoGA1"
052: + "UdDgQDAQEBMAoGA1UdIQQDAQEBMAwGByqGSM44BAMBAQADMAAwLQIUAL4QvoazNWP"
053: + "7jrj84/GZlhm09DsCFQCBKGKCGbrP64VtUt4JPmLjW1VxQA==\n"
054: + "-----END CERTIFICATE-----\n";
055:
056: public void testCertificateRequest() throws Exception {
057:
058: CertificateFactory certFactory = CertificateFactory
059: .getInstance("X509");
060: ByteArrayInputStream bais = new ByteArrayInputStream(
061: base64certEncoding.getBytes());
062: X509Certificate cert = (X509Certificate) certFactory
063: .generateCertificate(bais);
064: X509Certificate[] accepted = { cert };
065: X500Principal[] certificate_authorities = { cert
066: .getIssuerX500Principal() };
067:
068: byte[] certificate_types = new byte[] {
069: CertificateRequest.RSA_SIGN,
070: CertificateRequest.RSA_FIXED_DH };
071: CertificateRequest message = new CertificateRequest(
072: certificate_types, accepted);
073: assertEquals("incorrect type", Handshake.CERTIFICATE_REQUEST,
074: message.getType());
075: assertTrue("incorrect CertificateRequest", Arrays.equals(
076: message.certificate_types, certificate_types));
077: assertTrue("incorrect CertificateRequest", Arrays.equals(
078: message.certificate_authorities,
079: certificate_authorities));
080:
081: HandshakeIODataStream out = new HandshakeIODataStream();
082: message.send(out);
083: byte[] encoded = out.getData(1000);
084: assertEquals("incorrect out data length", message.length(),
085: encoded.length);
086:
087: HandshakeIODataStream in = new HandshakeIODataStream();
088: in.append(encoded);
089: CertificateRequest message_2 = new CertificateRequest(in,
090: message.length());
091: assertTrue("incorrect message decoding", Arrays.equals(
092: message.certificate_types, message_2.certificate_types));
093: assertTrue("incorrect message decoding", Arrays.equals(
094: message.certificate_authorities,
095: message_2.certificate_authorities));
096:
097: in.append(encoded);
098: try {
099: message_2 = new CertificateRequest(in, message.length() - 1);
100: fail("Small length: No expected AlertException");
101: } catch (AlertException e) {
102: }
103:
104: in.append(encoded);
105: in.append(new byte[] { 1, 2, 3 });
106: try {
107: message_2 = new CertificateRequest(in, message.length() + 3);
108: fail("Extra bytes: No expected AlertException ");
109: } catch (AlertException e) {
110: }
111: }
112: }
|