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     public static void main(String[] args) throws Exception {
  50         HostnameVerifier reservedHV =
  51             HttpsURLConnection.getDefaultHostnameVerifier();
  52         try {
  53             System.setProperty("java.protocol.handler.pkgs",
  54                                     "com.sun.net.ssl.internal.www.protocol");
  55             /**
  56              * This test does not establish any connection to the specified
  57              * URL, hence a dummy URL is used.
  58              */
  59             URL foobar = new URL("https://example.com/");
  60 
  61             HttpsURLConnection urlc =
  62                 (HttpsURLConnection) foobar.openConnection();
  63 
  64             try {
  65                 urlc.getCipherSuite();
  66             } catch (IllegalStateException e) {
  67                 System.out.print("Caught proper exception: ");
  68                 System.out.println(e.getMessage());
  69             }
  70 
  71             try {
  72                 urlc.getServerCertificateChain();
  73             } catch (IllegalStateException e) {
  74                 System.out.print("Caught proper exception: ");
  75                 System.out.println(e.getMessage());
  76             }
  77 
  78             try {
  79                 urlc.setDefaultHostnameVerifier(null);
  80             } catch (IllegalArgumentException e) {
  81                 System.out.print("Caught proper exception: ");
  82                 System.out.println(e.getMessage());
  83             }
  84 
  85             try {
  86                 urlc.setHostnameVerifier(null);
  87             } catch (IllegalArgumentException e) {
  88                 System.out.print("Caught proper exception: ");
  89                 System.out.println(e.getMessage());
  90             }
  91 
  92             try {
  93                 urlc.setDefaultSSLSocketFactory(null);
  94             } catch (IllegalArgumentException e) {
  95                 System.out.print("Caught proper exception: ");
  96                 System.out.println(e.getMessage());
  97             }
  98 
  99             try {
 100                 urlc.setSSLSocketFactory(null);
 101             } catch (IllegalArgumentException e) {
 102                 System.out.print("Caught proper exception");
 103                 System.out.println(e.getMessage());
 104             }
 105             System.out.println("TESTS PASSED");
 106         } finally {
 107             HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);
 108         }
 109     }
 110 }