1 /* 2 * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4387882 4451038 27 * @summary Need to revisit the javadocs for JSSE, especially the 28 * promoted classes, and HttpsURLConnection.getCipherSuite throws 29 * NullPointerException 30 * @run main/othervm ComURLNulls 31 * 32 * SunJSSE does not support dynamic system properties, no way to re-use 33 * system properties in samevm/agentvm mode. 34 * @author Brad Wetmore 35 */ 36 37 import java.net.*; 38 import java.io.*; 39 import javax.net.ssl.*; 40 import com.sun.net.ssl.HttpsURLConnection; 41 import com.sun.net.ssl.HostnameVerifier; 42 43 /* 44 * Tests that the com null argument changes made it in ok. 45 */ 46 47 public class ComURLNulls { 48 49 private static class ComSunHTTPSHandlerFactory implements URLStreamHandlerFactory { 50 private static String SUPPORTED_PROTOCOL = "https"; 51 52 public URLStreamHandler createURLStreamHandler(String protocol) { 53 if (!protocol.equalsIgnoreCase(SUPPORTED_PROTOCOL)) 54 return null; 55 56 return new com.sun.net.ssl.internal.www.protocol.https.Handler(); 57 } 58 } 59 60 public static void main(String[] args) throws Exception { 61 HostnameVerifier reservedHV = 62 HttpsURLConnection.getDefaultHostnameVerifier(); 63 try { 64 URL.addURLStreamHandlerFactory(new ComSunHTTPSHandlerFactory()); 65 66 /** 67 * This test does not establish any connection to the specified 68 * URL, hence a dummy URL is used. 69 */ 70 URL foobar = new URL("https://example.com/"); 71 72 HttpsURLConnection urlc = 73 (HttpsURLConnection) foobar.openConnection(); 74 75 try { 76 urlc.getCipherSuite(); 77 } catch (IllegalStateException e) { 78 System.out.print("Caught proper exception: "); 79 System.out.println(e.getMessage()); 80 } 81 82 try { 83 urlc.getServerCertificateChain(); 84 } catch (IllegalStateException e) { 85 System.out.print("Caught proper exception: "); 86 System.out.println(e.getMessage()); 87 } 88 89 try { 90 urlc.setDefaultHostnameVerifier(null); 91 } catch (IllegalArgumentException e) { 92 System.out.print("Caught proper exception: "); 93 System.out.println(e.getMessage()); 94 } 95 96 try { 97 urlc.setHostnameVerifier(null); 98 } catch (IllegalArgumentException e) { 99 System.out.print("Caught proper exception: "); 100 System.out.println(e.getMessage()); 101 } 102 103 try { 104 urlc.setDefaultSSLSocketFactory(null); 105 } catch (IllegalArgumentException e) { 106 System.out.print("Caught proper exception: "); 107 System.out.println(e.getMessage()); 108 } 109 110 try { 111 urlc.setSSLSocketFactory(null); 112 } catch (IllegalArgumentException e) { 113 System.out.print("Caught proper exception"); 114 System.out.println(e.getMessage()); 115 } 116 System.out.println("TESTS PASSED"); 117 } finally { 118 HttpsURLConnection.setDefaultHostnameVerifier(reservedHV); 119 } 120 } 121 }