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.File;
022: import java.io.FileInputStream;
023: import java.security.KeyStore;
024: import java.security.cert.CertificateException;
025: import java.security.cert.CertificateFactory;
026: import java.security.cert.X509Certificate;
027:
028: import junit.framework.TestCase;
029:
030: /**
031: * Tests for <code>TrustManagerImpl</code> constructor and methods
032: *
033: */
034: public class TrustManagerImplTest extends TestCase {
035:
036: // Cert. encoding.was generated by using of classes
037: // from org.apache.harmony.security.asn1 package and encoded
038: // by org.apache.harmony.misc.Base64 class.
039: // Source:
040: // org.apache.harmony.security.tests.support.provider.cert.CertFactoryTestData
041: private static String base64certEncoding = "-----BEGIN CERTIFICATE-----\n"
042: + "MIIC+jCCAragAwIBAgICAiswDAYHKoZIzjgEAwEBADAdMRswGQYDVQQKExJDZXJ0a"
043: + "WZpY2F0ZSBJc3N1ZXIwIhgPMTk3MDAxMTIxMzQ2NDBaGA8xOTcwMDEyNDAzMzMyMF"
044: + "owHzEdMBsGA1UEChMUU3ViamVjdCBPcmdhbml6YXRpb24wGTAMBgcqhkjOOAQDAQE"
045: + "AAwkAAQIDBAUGBwiBAgCqggIAVaOCAhQwggIQMA8GA1UdDwEB/wQFAwMBqoAwEgYD"
046: + "VR0TAQH/BAgwBgEB/wIBBTAUBgNVHSABAf8ECjAIMAYGBFUdIAAwZwYDVR0RAQH/B"
047: + "F0wW4EMcmZjQDgyMi5OYW1lggdkTlNOYW1lpBcxFTATBgNVBAoTDE9yZ2FuaXphdG"
048: + "lvboYaaHR0cDovL3VuaWZvcm0uUmVzb3VyY2UuSWSHBP///wCIByoDolyDsgMwDAY"
049: + "DVR0eAQH/BAIwADAMBgNVHSQBAf8EAjAAMIGZBgNVHSUBAf8EgY4wgYsGBFUdJQAG"
050: + "CCsGAQUFBwMBBggrBgEFBQcDAQYIKwYBBQUHAwIGCCsGAQUFBwMDBggrBgEFBQcDB"
051: + "AYIKwYBBQUHAwUGCCsGAQUFBwMGBggrBgEFBQcDBwYIKwYBBQUHAwgGCCsGAQUFBw"
052: + "MJBggrBgEFBQgCAgYKKwYBBAGCNwoDAwYJYIZIAYb4QgQBMA0GA1UdNgEB/wQDAgE"
053: + "BMA4GBCpNhgkBAf8EAwEBATBkBgNVHRIEXTBbgQxyZmNAODIyLk5hbWWCB2ROU05h"
054: + "bWWkFzEVMBMGA1UEChMMT3JnYW5pemF0aW9uhhpodHRwOi8vdW5pZm9ybS5SZXNvd"
055: + "XJjZS5JZIcE////AIgHKgOiXIOyAzAJBgNVHR8EAjAAMAoGA1UdIwQDAQEBMAoGA1"
056: + "UdDgQDAQEBMAoGA1UdIQQDAQEBMAwGByqGSM44BAMBAQADMAAwLQIUAL4QvoazNWP"
057: + "7jrj84/GZlhm09DsCFQCBKGKCGbrP64VtUt4JPmLjW1VxQA==\n"
058: + "-----END CERTIFICATE-----\n";
059:
060: private X509Certificate[] untrustedChain;
061:
062: protected void setUp() throws Exception {
063: super .setUp();
064: CertificateFactory certFactory = CertificateFactory
065: .getInstance("X509");
066: ByteArrayInputStream bais = new ByteArrayInputStream(
067: base64certEncoding.getBytes());
068: X509Certificate cert = (X509Certificate) certFactory
069: .generateCertificate(bais);
070: untrustedChain = new X509Certificate[] { cert };
071: }
072:
073: public void testTrustManagerImpl_1() throws Exception {
074: KeyStore ks = KeyStore.getInstance("BKS");
075: ks.load(null, null);
076:
077: TrustManagerImpl tm = new TrustManagerImpl(ks);
078: assertEquals(0, tm.getAcceptedIssuers().length);
079: checkTrustManager(tm);
080: }
081:
082: public void testTrustManagerImpl_2() throws Exception {
083: KeyStore ks = JSSETestData.getKeyStore();
084:
085: TrustManagerImpl tm = new TrustManagerImpl(ks);
086: assertEquals(1, tm.getAcceptedIssuers().length);
087: checkTrustManager(tm);
088: }
089:
090: private void checkTrustManager(TrustManagerImpl tm)
091: throws Exception {
092: try {
093: tm.checkClientTrusted(null, "RSA");
094: fail("No expected IllegalArgumentException ");
095: } catch (IllegalArgumentException e) {
096: }
097:
098: try {
099: tm.checkClientTrusted(new X509Certificate[0], "RSA");
100: fail("No expected IllegalArgumentException ");
101: } catch (IllegalArgumentException e) {
102: }
103:
104: try {
105: tm.checkClientTrusted(untrustedChain, "RSA");
106: fail("No expected CertificateException ");
107: } catch (CertificateException e) {
108: }
109:
110: try {
111: tm.checkServerTrusted(null, "RSA");
112: fail("No expected IllegalArgumentException ");
113: } catch (IllegalArgumentException e) {
114: }
115:
116: try {
117: tm.checkServerTrusted(new X509Certificate[0], "RSA");
118: fail("No expected IllegalArgumentException ");
119: } catch (IllegalArgumentException e) {
120: }
121:
122: try {
123: tm.checkServerTrusted(untrustedChain, "RSA");
124: fail("No expected CertificateException ");
125: } catch (CertificateException e) {
126: }
127: }
128: }
|