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