1 /*
   2  * Copyright (c) 2001, 2015, 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  * @modules java.base/com.sun.net.ssl
  31  *          java.base/com.sun.net.ssl.internal.www.protocol.https
  32  * @run main/othervm ComURLNulls
  33  *
  34  *     SunJSSE does not support dynamic system properties, no way to re-use
  35  *     system properties in samevm/agentvm mode.
  36  * @author Brad Wetmore
  37  */
  38 
  39 import java.net.*;
  40 import java.io.*;
  41 import javax.net.ssl.*;
  42 import com.sun.net.ssl.HttpsURLConnection;
  43 import com.sun.net.ssl.HostnameVerifier;
  44 
  45 /*
  46  * Tests that the com null argument changes made it in ok.
  47  */
  48 
  49 public class ComURLNulls {
  50 
  51     private static class ComSunHTTPSHandlerFactory implements URLStreamHandlerFactory {
  52         private static String SUPPORTED_PROTOCOL = "https";
  53 
  54         public URLStreamHandler createURLStreamHandler(String protocol) {
  55             if (!protocol.equalsIgnoreCase(SUPPORTED_PROTOCOL))
  56                 return null;
  57 
  58             return new com.sun.net.ssl.internal.www.protocol.https.Handler();
  59         }
  60     }
  61 
  62     public static void main(String[] args) throws Exception {
  63         HostnameVerifier reservedHV =
  64             HttpsURLConnection.getDefaultHostnameVerifier();
  65         try {
  66             URL.setURLStreamHandlerFactory(new ComSunHTTPSHandlerFactory());
  67 
  68             /**
  69              * This test does not establish any connection to the specified
  70              * URL, hence a dummy URL is used.
  71              */
  72             URL foobar = new URL("https://example.com/");
  73 
  74             HttpsURLConnection urlc =
  75                 (HttpsURLConnection) foobar.openConnection();
  76 
  77             try {
  78                 urlc.getCipherSuite();
  79             } catch (IllegalStateException e) {
  80                 System.out.print("Caught proper exception: ");
  81                 System.out.println(e.getMessage());
  82             }
  83 
  84             try {
  85                 urlc.getServerCertificates();
  86             } catch (IllegalStateException e) {
  87                 System.out.print("Caught proper exception: ");
  88                 System.out.println(e.getMessage());
  89             }
  90 
  91             try {
  92                 urlc.setDefaultHostnameVerifier(null);
  93             } catch (IllegalArgumentException e) {
  94                 System.out.print("Caught proper exception: ");
  95                 System.out.println(e.getMessage());
  96             }
  97 
  98             try {
  99                 urlc.setHostnameVerifier(null);
 100             } catch (IllegalArgumentException e) {
 101                 System.out.print("Caught proper exception: ");
 102                 System.out.println(e.getMessage());
 103             }
 104 
 105             try {
 106                 urlc.setDefaultSSLSocketFactory(null);
 107             } catch (IllegalArgumentException e) {
 108                 System.out.print("Caught proper exception: ");
 109                 System.out.println(e.getMessage());
 110             }
 111 
 112             try {
 113                 urlc.setSSLSocketFactory(null);
 114             } catch (IllegalArgumentException e) {
 115                 System.out.print("Caught proper exception");
 116                 System.out.println(e.getMessage());
 117             }
 118             System.out.println("TESTS PASSED");
 119         } finally {
 120             HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);
 121         }
 122     }
 123 }