Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/ssl/X509TrustManagerImpl/CheckNullEntity.java
38853 views
/*1* Copyright (c) 2005, 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 505381526* @summary unspecified exceptions in X509TrustManager.checkClient[Server]Truste27d28* @author Xuelei Fan29*/3031import java.io.*;32import java.net.*;33import javax.net.ssl.*;34import java.security.cert.X509Certificate;35import java.security.*;36import java.util.Enumeration;3738import com.sun.net.ssl.internal.ssl.X509ExtendedTrustManager;3940public class CheckNullEntity {4142/*43* =============================================================44* Set the various variables needed for the tests, then45* specify what tests to run on each side.46*/4748/*49* Should we run the client or server in a separate thread?50* Both sides can throw exceptions, but do you have a preference51* as to which side should be the main thread.52*/53static boolean separateServerThread = true;5455/*56* Where do we find the keystores?57*/58static String pathToStores = "../../../../javax/net/ssl/etc";59static String keyStoreFile = "keystore";60static String trustStoreFile = "truststore";61static String passwd = "passphrase";6263private void initialize() throws Exception {64String trustFilename =65System.getProperty("test.src", "./") + "/" + pathToStores +66"/" + trustStoreFile;67char[] passphrase = "passphrase".toCharArray();6869KeyStore ks = KeyStore.getInstance("JKS");70ks.load(new FileInputStream(trustFilename), passphrase);7172for (Enumeration e = ks.aliases() ; e.hasMoreElements() ;) {73String alias = (String)e.nextElement();74if (ks.isCertificateEntry(alias)) {75certChain[0] = (X509Certificate)ks.getCertificate(alias);76break;77}78}7980TrustManagerFactory tmf =81TrustManagerFactory.getInstance("SunX509");82tmf.init(ks);8384trustManager = (X509TrustManager)(tmf.getTrustManagers())[0];85}8687/*88* =============================================================89* The remainder is just support stuff90*/91public static void main(String[] args) throws Exception {92/*93* Start the tests.94*/95new CheckNullEntity();96}9798X509Certificate[] certChain = {null, null};99X509TrustManager trustManager = null;100101/*102* Primary constructor, used to drive remainder of the test.103*104* Fork off the other side, then do your work.105*/106CheckNullEntity() throws Exception {107String authType = "RSA";108int failed = 0x3F; // indicate six tests for normal TM109int extFailed = 0x3F; // indicate six tests for extended TM110111initialize();112try {113try {114trustManager.checkClientTrusted(certChain, (String)null);115} catch (IllegalArgumentException iae) {116// get the right exception117failed >>= 1;118}119120try {121trustManager.checkServerTrusted(certChain, (String)null);122} catch (IllegalArgumentException iae) {123// get the right exception124failed >>= 1;125}126127try {128trustManager.checkClientTrusted(certChain, "");129} catch (IllegalArgumentException iae) {130// get the right exception131failed >>= 1;132}133134try {135trustManager.checkServerTrusted(certChain, "");136} catch (IllegalArgumentException iae) {137// get the right exception138failed >>= 1;139}140141try {142trustManager.checkClientTrusted(null, authType);143} catch (IllegalArgumentException iae) {144// get the right exception145failed >>= 1;146}147148try {149trustManager.checkServerTrusted(null, authType);150} catch (IllegalArgumentException iae) {151// get the right exception152failed >>= 1;153}154155if (trustManager instanceof X509ExtendedTrustManager) {156try {157((X509ExtendedTrustManager)trustManager).checkClientTrusted(158certChain, (String)null, "localhost", null);159} catch (IllegalArgumentException iae) {160// get the right exception161extFailed >>= 1;162}163164try {165((X509ExtendedTrustManager)trustManager).checkServerTrusted(166certChain, (String)null, "localhost", null);167} catch (IllegalArgumentException iae) {168// get the right exception169extFailed >>= 1;170}171172try {173((X509ExtendedTrustManager)trustManager).checkClientTrusted(174certChain, "", "localhost", null);175} catch (IllegalArgumentException iae) {176// get the right exception177extFailed >>= 1;178}179180try {181((X509ExtendedTrustManager)trustManager).checkServerTrusted(182certChain, "", "localhost", null);183} catch (IllegalArgumentException iae) {184// get the right exception185extFailed >>= 1;186}187188try {189((X509ExtendedTrustManager)trustManager).checkClientTrusted(190null, authType, "localhost", null);191} catch (IllegalArgumentException iae) {192// get the right exception193extFailed >>= 1;194}195196try {197((X509ExtendedTrustManager)trustManager).checkServerTrusted(198null, authType, "localhost", null);199} catch (IllegalArgumentException iae) {200// get the right exception201extFailed >>= 1;202}203} else {204extFailed = 0;205}206} catch (NullPointerException npe) {207// IllegalArgumentException should be thrown208failed = 1;209} catch (Exception e) {210// ignore211System.out.println("Got another exception e" + e);212}213214if (failed != 0 || extFailed != 0) {215throw new Exception("Should throw IllegalArgumentException");216}217}218}219220221