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.CertificateEncodingException;
023: import java.security.cert.CertificateException;
024: import java.security.cert.CertificateFactory;
025: import java.security.cert.X509Certificate;
026: import java.util.Arrays;
027:
028: import junit.framework.TestCase;
029:
030: /**
031: * Tests for <code>CertificateMessage</code> constructor and methods
032: *
033: */
034: public class CertificateMessageTest extends TestCase {
035:
036: private static String base64certEncoding = "-----BEGIN CERTIFICATE-----\n"
037: + "MIIC+jCCAragAwIBAgICAiswDAYHKoZIzjgEAwEBADAdMRswGQYDVQQKExJDZXJ0a"
038: + "WZpY2F0ZSBJc3N1ZXIwIhgPMTk3MDAxMTIxMzQ2NDBaGA8xOTcwMDEyNDAzMzMyMF"
039: + "owHzEdMBsGA1UEChMUU3ViamVjdCBPcmdhbml6YXRpb24wGTAMBgcqhkjOOAQDAQE"
040: + "AAwkAAQIDBAUGBwiBAgCqggIAVaOCAhQwggIQMA8GA1UdDwEB/wQFAwMBqoAwEgYD"
041: + "VR0TAQH/BAgwBgEB/wIBBTAUBgNVHSABAf8ECjAIMAYGBFUdIAAwZwYDVR0RAQH/B"
042: + "F0wW4EMcmZjQDgyMi5OYW1lggdkTlNOYW1lpBcxFTATBgNVBAoTDE9yZ2FuaXphdG"
043: + "lvboYaaHR0cDovL3VuaWZvcm0uUmVzb3VyY2UuSWSHBP///wCIByoDolyDsgMwDAY"
044: + "DVR0eAQH/BAIwADAMBgNVHSQBAf8EAjAAMIGZBgNVHSUBAf8EgY4wgYsGBFUdJQAG"
045: + "CCsGAQUFBwMBBggrBgEFBQcDAQYIKwYBBQUHAwIGCCsGAQUFBwMDBggrBgEFBQcDB"
046: + "AYIKwYBBQUHAwUGCCsGAQUFBwMGBggrBgEFBQcDBwYIKwYBBQUHAwgGCCsGAQUFBw"
047: + "MJBggrBgEFBQgCAgYKKwYBBAGCNwoDAwYJYIZIAYb4QgQBMA0GA1UdNgEB/wQDAgE"
048: + "BMA4GBCpNhgkBAf8EAwEBATBkBgNVHRIEXTBbgQxyZmNAODIyLk5hbWWCB2ROU05h"
049: + "bWWkFzEVMBMGA1UEChMMT3JnYW5pemF0aW9uhhpodHRwOi8vdW5pZm9ybS5SZXNvd"
050: + "XJjZS5JZIcE////AIgHKgOiXIOyAzAJBgNVHR8EAjAAMAoGA1UdIwQDAQEBMAoGA1"
051: + "UdDgQDAQEBMAoGA1UdIQQDAQEBMAwGByqGSM44BAMBAQADMAAwLQIUAL4QvoazNWP"
052: + "7jrj84/GZlhm09DsCFQCBKGKCGbrP64VtUt4JPmLjW1VxQA==\n"
053: + "-----END CERTIFICATE-----\n";
054:
055: /*
056: * Test for CertificateMessage(null) and
057: * CertificateMessage(HandshakeIODataStream, int)
058: */
059: public void testCertificateMessage1() throws Exception {
060:
061: CertificateMessage message = new CertificateMessage(null);
062: assertEquals("incorrect type", Handshake.CERTIFICATE, message
063: .getType());
064: assertEquals("incorrect message", 3, message.length());
065: assertEquals("incorrect message", 0, message.certs.length);
066:
067: HandshakeIODataStream out = new HandshakeIODataStream();
068: message.send(out);
069: byte[] encoded = out.getData(1000);
070: assertEquals("incorrect out data length", message.length(),
071: encoded.length);
072:
073: HandshakeIODataStream in = new HandshakeIODataStream();
074: in.append(encoded);
075:
076: CertificateMessage message_2 = new CertificateMessage(in,
077: message.length());
078: assertEquals("incorrect message_2", 3, message_2.length());
079: assertEquals("incorrect message_2", 0, message_2.certs.length);
080: }
081:
082: /*
083: * Test for void CertificateMessage(X509Certificate[]),
084: * CertificateMessage(HandshakeIODataStream, int)
085: */
086: public void testCertificateMessage2() throws Exception {
087: CertificateFactory certFactory = CertificateFactory
088: .getInstance("X509");
089:
090: ByteArrayInputStream bais = new ByteArrayInputStream(
091: base64certEncoding.getBytes());
092: X509Certificate cert = (X509Certificate) certFactory
093: .generateCertificate(bais);
094: CertificateMessage message = new CertificateMessage(
095: new X509Certificate[] { cert });
096: assertEquals("incorrect type", Handshake.CERTIFICATE, message
097: .getType());
098:
099: assertTrue("incorrect cert encoding", Arrays.equals(
100: message.certs[0].getEncoded(), cert.getEncoded()));
101:
102: HandshakeIODataStream out = new HandshakeIODataStream();
103: message.send(out);
104: byte[] encoded = out.getData(1000);
105: assertEquals("incorrect out data length", message.length(),
106: encoded.length);
107:
108: HandshakeIODataStream in = new HandshakeIODataStream();
109: in.append(encoded);
110: CertificateMessage message_2 = new CertificateMessage(in,
111: message.length());
112: assertEquals("Incorrect message decoding",
113: message.certs.length, message_2.certs.length);
114: assertTrue("incorrect cert encoding", Arrays.equals(
115: message.certs[0].getEncoded(), message_2.certs[0]
116: .getEncoded()));
117:
118: in.append(encoded);
119: try {
120: message_2 = new CertificateMessage(in, message.length() - 1);
121: fail("Small length: No expected AlertException");
122: } catch (AlertException e) {
123: }
124:
125: in.append(encoded);
126: in.append(new byte[] { 1, 2, 3 });
127: try {
128: message_2 = new CertificateMessage(in, message.length() + 3);
129: fail("Extra bytes: No expected AlertException ");
130: } catch (AlertException e) {
131: }
132: }
133:
134: }
|