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.CertificateEncodingException; 23import java.security.cert.CertificateException; 24import java.security.cert.CertificateFactory; 25import java.security.cert.X509Certificate; 26import java.util.Arrays; 27 28import junit.framework.TestCase; 29 30/** 31 * Tests for <code>CertificateMessage</code> constructor and methods 32 */ 33public class CertificateMessageTest extends TestCase { 34 35 private static String base64certEncoding = "-----BEGIN CERTIFICATE-----\n" 36 + "MIIC+jCCAragAwIBAgICAiswDAYHKoZIzjgEAwEBADAdMRswGQYDVQQKExJDZXJ0a" 37 + "WZpY2F0ZSBJc3N1ZXIwIhgPMTk3MDAxMTIxMzQ2NDBaGA8xOTcwMDEyNDAzMzMyMF" 38 + "owHzEdMBsGA1UEChMUU3ViamVjdCBPcmdhbml6YXRpb24wGTAMBgcqhkjOOAQDAQE" 39 + "AAwkAAQIDBAUGBwiBAgCqggIAVaOCAhQwggIQMA8GA1UdDwEB/wQFAwMBqoAwEgYD" 40 + "VR0TAQH/BAgwBgEB/wIBBTAUBgNVHSABAf8ECjAIMAYGBFUdIAAwZwYDVR0RAQH/B" 41 + "F0wW4EMcmZjQDgyMi5OYW1lggdkTlNOYW1lpBcxFTATBgNVBAoTDE9yZ2FuaXphdG" 42 + "lvboYaaHR0cDovL3VuaWZvcm0uUmVzb3VyY2UuSWSHBP///wCIByoDolyDsgMwDAY" 43 + "DVR0eAQH/BAIwADAMBgNVHSQBAf8EAjAAMIGZBgNVHSUBAf8EgY4wgYsGBFUdJQAG" 44 + "CCsGAQUFBwMBBggrBgEFBQcDAQYIKwYBBQUHAwIGCCsGAQUFBwMDBggrBgEFBQcDB" 45 + "AYIKwYBBQUHAwUGCCsGAQUFBwMGBggrBgEFBQcDBwYIKwYBBQUHAwgGCCsGAQUFBw" 46 + "MJBggrBgEFBQgCAgYKKwYBBAGCNwoDAwYJYIZIAYb4QgQBMA0GA1UdNgEB/wQDAgE" 47 + "BMA4GBCpNhgkBAf8EAwEBATBkBgNVHRIEXTBbgQxyZmNAODIyLk5hbWWCB2ROU05h" 48 + "bWWkFzEVMBMGA1UEChMMT3JnYW5pemF0aW9uhhpodHRwOi8vdW5pZm9ybS5SZXNvd" 49 + "XJjZS5JZIcE////AIgHKgOiXIOyAzAJBgNVHR8EAjAAMAoGA1UdIwQDAQEBMAoGA1" 50 + "UdDgQDAQEBMAoGA1UdIQQDAQEBMAwGByqGSM44BAMBAQADMAAwLQIUAL4QvoazNWP" 51 + "7jrj84/GZlhm09DsCFQCBKGKCGbrP64VtUt4JPmLjW1VxQA==\n" 52 + "-----END CERTIFICATE-----\n"; 53 54 /* 55 * Test for CertificateMessage(null) and 56 * CertificateMessage(HandshakeIODataStream, int) 57 */ 58 public void testCertificateMessage1() throws Exception { 59 60 CertificateMessage message = new CertificateMessage(null); 61 assertEquals("incorrect type", Handshake.CERTIFICATE, message.getType()); 62 assertEquals("incorrect message", 3, message.length()); 63 assertEquals("incorrect message", 0, message.certs.length); 64 65 HandshakeIODataStream out = new HandshakeIODataStream(); 66 message.send(out); 67 byte[] encoded = out.getData(1000); 68 assertEquals("incorrect out data length", message.length(), encoded.length); 69 70 HandshakeIODataStream in = new HandshakeIODataStream(); 71 in.append(encoded); 72 73 CertificateMessage message_2 = new CertificateMessage(in, message.length()); 74 assertEquals("incorrect message_2", 3, message_2.length()); 75 assertEquals("incorrect message_2", 0, message_2.certs.length); 76 } 77 78 /* 79 * Test for void CertificateMessage(X509Certificate[]), 80 * CertificateMessage(HandshakeIODataStream, int) 81 */ 82 public void testCertificateMessage2() throws Exception { 83 CertificateFactory certFactory = CertificateFactory.getInstance("X509"); 84 85 ByteArrayInputStream bais = new ByteArrayInputStream(base64certEncoding 86 .getBytes("UTF-8")); 87 X509Certificate cert = (X509Certificate) certFactory.generateCertificate(bais); 88 CertificateMessage message = new CertificateMessage( 89 new X509Certificate[] { cert }); 90 assertEquals("incorrect type", Handshake.CERTIFICATE, message.getType()); 91 92 assertTrue("incorrect cert encoding", Arrays.equals(message.certs[0] 93 .getEncoded(), cert.getEncoded())); 94 95 HandshakeIODataStream out = new HandshakeIODataStream(); 96 message.send(out); 97 byte[] encoded = out.getData(1000); 98 assertEquals("incorrect out data length", message.length(), encoded.length); 99 100 HandshakeIODataStream in = new HandshakeIODataStream(); 101 in.append(encoded); 102 CertificateMessage message_2 = new CertificateMessage(in, message.length()); 103 assertEquals("Incorrect message decoding", message.certs.length, message_2.certs.length); 104 assertTrue("incorrect cert encoding", Arrays.equals(message.certs[0] 105 .getEncoded(), message_2.certs[0].getEncoded())); 106 107 in.append(encoded); 108 try { 109 message_2 = new CertificateMessage(in, message.length() - 1); 110 fail("Small length: No expected AlertException"); 111 } catch (AlertException e) { 112 } 113 114 in.append(encoded); 115 in.append(new byte[] { 1, 2, 3 }); 116 try { 117 message_2 = new CertificateMessage(in, message.length() + 3); 118 fail("Extra bytes: No expected AlertException "); 119 } catch (AlertException e) { 120 } 121 } 122 123} 124