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.CertificateEncodingException;
     23 import java.security.cert.CertificateException;
     24 import java.security.cert.CertificateFactory;
     25 import java.security.cert.X509Certificate;
     26 import java.util.Arrays;
     27 
     28 import junit.framework.TestCase;
     29 
     30 /**
     31  * Tests for <code>CertificateMessage</code> constructor and methods
     32  *
     33  */
     34 public class CertificateMessageTest 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 	/*
     56 	 * Test for CertificateMessage(null) and
     57 	 * CertificateMessage(HandshakeIODataStream, int)
     58 	 */
     59 	public void testCertificateMessage1() throws Exception {
     60 
     61 		CertificateMessage message = new CertificateMessage(null);
     62         assertEquals("incorrect type", Handshake.CERTIFICATE, message.getType());
     63         assertEquals("incorrect message", 3, message.length());
     64         assertEquals("incorrect message", 0, message.certs.length);
     65 
     66 		HandshakeIODataStream out = new HandshakeIODataStream();
     67 		message.send(out);
     68 		byte[] encoded = out.getData(1000);
     69         assertEquals("incorrect out data length", message.length(), encoded.length);
     70 
     71 		HandshakeIODataStream in = new HandshakeIODataStream();
     72 		in.append(encoded);
     73 
     74 		CertificateMessage message_2 = new CertificateMessage(in, message.length());
     75         assertEquals("incorrect message_2", 3, message_2.length());
     76         assertEquals("incorrect message_2", 0, message_2.certs.length);
     77 	}
     78 
     79 	/*
     80 	 * Test for void CertificateMessage(X509Certificate[]),
     81 	 * CertificateMessage(HandshakeIODataStream, int)
     82 	 */
     83 	public void testCertificateMessage2() throws Exception {
     84 		CertificateFactory certFactory = CertificateFactory.getInstance("X509");
     85 
     86 		ByteArrayInputStream bais = new ByteArrayInputStream(base64certEncoding
     87 				.getBytes("UTF-8"));
     88 		X509Certificate cert = (X509Certificate) certFactory.generateCertificate(bais);
     89 		CertificateMessage message = new CertificateMessage(
     90 				new X509Certificate[] { cert });
     91         assertEquals("incorrect type", Handshake.CERTIFICATE, message.getType());
     92 
     93         assertTrue("incorrect cert encoding", Arrays.equals(message.certs[0]
     94                 .getEncoded(), cert.getEncoded()));
     95 
     96 		HandshakeIODataStream out = new HandshakeIODataStream();
     97 		message.send(out);
     98 		byte[] encoded = out.getData(1000);
     99         assertEquals("incorrect out data length", message.length(), encoded.length);
    100 
    101 		HandshakeIODataStream in = new HandshakeIODataStream();
    102 		in.append(encoded);
    103 		CertificateMessage message_2 = new CertificateMessage(in, message.length());
    104         assertEquals("Incorrect message decoding", message.certs.length, message_2.certs.length);
    105         assertTrue("incorrect cert encoding", Arrays.equals(message.certs[0]
    106                 .getEncoded(), message_2.certs[0].getEncoded()));
    107 
    108 		in.append(encoded);
    109 		try {
    110 			message_2 = new CertificateMessage(in, message.length() - 1);
    111 			fail("Small length: No expected AlertException");
    112 		} catch (AlertException e) {
    113 		}
    114 
    115 		in.append(encoded);
    116 		in.append(new byte[] { 1, 2, 3 });
    117 		try {
    118 			message_2 = new CertificateMessage(in, message.length() + 3);
    119 			fail("Extra bytes: No expected AlertException ");
    120 		} catch (AlertException e) {
    121 		}
    122 	}
    123 
    124 }
    125