< prev index next >

test/javax/net/ssl/FixingJavadocs/SSLSessionNulls.java

Print this page


   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
  27  * @summary Need to revisit the javadocs for JSSE, especially the
  28  *      promoted classes.

  29  * @run main/othervm SSLSessionNulls
  30  *
  31  *     SunJSSE does not support dynamic system properties, no way to re-use
  32  *     system properties in samevm/agentvm mode.
  33  * @author Brad Wetmore
  34  */
  35 
  36 import java.io.*;
  37 import java.net.*;
  38 import javax.net.ssl.*;
  39 
  40 public class SSLSessionNulls {
  41 
  42     /*
  43      * =============================================================
  44      * Set the various variables needed for the tests, then
  45      * specify what tests to run on each side.
  46      */
  47 
  48     /*
  49      * Should we run the client or server in a separate thread?
  50      * Both sides can throw exceptions, but do you have a preference
  51      * as to which side should be the main thread.
  52      */
  53     static boolean separateServerThread = true;
  54 
  55     /*
  56      * Where do we find the keystores?
  57      */
  58     static String pathToStores = "../etc";
  59     static String keyStoreFile = "keystore";
  60     static String trustStoreFile = "truststore";
  61     static String passwd = "passphrase";
  62 
  63     /*
  64      * Is the server ready to serve?
  65      */
  66     volatile static boolean serverReady = false;
  67 
  68     /*
  69      * Turn on SSL debugging?
  70      */
  71     static boolean debug = false;
  72 
  73     /*
  74      * If the client or server is doing some kind of object creation
  75      * that the other side depends on, and that thread prematurely
  76      * exits, you may experience a hang.  The test harness will
  77      * terminate all hung threads after its timeout has expired,
  78      * currently 3 minutes by default, but you might try to be
  79      * smart about it....
  80      */
  81 
  82     /*
  83      * Define the server side of the test.
  84      *
  85      * If the server prematurely exits, serverReady will be set to true
  86      * to avoid infinite hangs.
  87      */
  88     void doServerSide() throws Exception {
  89         SSLServerSocketFactory sslssf =
  90             (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
  91         SSLServerSocket sslServerSocket =
  92             (SSLServerSocket) sslssf.createServerSocket(serverPort);
  93         serverPort = sslServerSocket.getLocalPort();
  94 
  95         /*
  96          * Signal Client, we're ready for his connect.
  97          */
  98         serverReady = true;
  99 
 100         SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
 101         InputStream sslIS = sslSocket.getInputStream();
 102         OutputStream sslOS = sslSocket.getOutputStream();

 103 
 104         sslIS.read();
 105         sslOS.write(85);
 106         sslOS.flush();
 107 
 108         sslSocket.close();
 109     }
 110 
 111     /*
 112      * Define the client side of the test.
 113      *
 114      * If the server prematurely exits, serverReady will be set to true
 115      * to avoid infinite hangs.
 116      */
 117     void doClientSide() throws Exception {
 118 
 119         /*
 120          * Wait for server to get started.
 121          */
 122         while (!serverReady) {
 123             Thread.sleep(50);
 124         }
 125 
 126         SSLSocketFactory sslsf =
 127             (SSLSocketFactory) SSLSocketFactory.getDefault();
 128         SSLSocket sslSocket = (SSLSocket)
 129             sslsf.createSocket("localhost", serverPort);
 130 
 131         InputStream sslIS = sslSocket.getInputStream();
 132         OutputStream sslOS = sslSocket.getOutputStream();
 133 
 134         sslOS.write(280);
 135         sslOS.flush();
 136         sslIS.read();
 137 
 138         SSLSession sslSession = sslSocket.getSession();
 139 
 140         try {
 141             sslSession.getValue(null);
 142         } catch (IllegalArgumentException e) {
 143             System.out.print("Caught proper exception: ");
 144             System.out.println(e.getMessage());
 145         }
 146 
 147         try {
 148             sslSession.putValue(null, null);
 149         } catch (IllegalArgumentException e) {
 150             System.out.print("Caught proper exception: ");
 151             System.out.println(e.getMessage());
 152         }
 153 
 154         try {
 155             sslSession.removeValue(null);
 156         } catch (IllegalArgumentException e) {
 157             System.out.print("Caught proper exception: ");
 158             System.out.println(e.getMessage());
 159         }
 160 
 161         String [] names = sslSession.getValueNames();
 162         if ((names == null) || (names.length != 0)) {
 163             throw new IOException(
 164                 "getValueNames didn't return 0-length arrary");
 165         }
 166 
 167         sslSocket.close();
 168     }
 169 
 170     /*
 171      * =============================================================
 172      * The remainder is just support stuff
 173      */
 174 
 175     // use any free port by default
 176     volatile int serverPort = 0;
 177 
 178     volatile Exception serverException = null;
 179     volatile Exception clientException = null;
 180 
 181     public static void main(String[] args) throws Exception {
 182         String keyFilename =
 183             System.getProperty("test.src", "./") + "/" + pathToStores +
 184                 "/" + keyStoreFile;
 185         String trustFilename =
 186             System.getProperty("test.src", "./") + "/" + pathToStores +
 187                 "/" + trustStoreFile;
 188 
 189         System.setProperty("javax.net.ssl.keyStore", keyFilename);
 190         System.setProperty("javax.net.ssl.keyStorePassword", passwd);
 191         System.setProperty("javax.net.ssl.trustStore", trustFilename);
 192         System.setProperty("javax.net.ssl.trustStorePassword", passwd);
 193 
 194         if (debug)
 195             System.setProperty("javax.net.debug", "all");
 196 
 197         /*
 198          * Start the tests.
 199          */
 200         new SSLSessionNulls();
 201     }
 202 
 203     Thread clientThread = null;
 204     Thread serverThread = null;
 205 
 206     /*
 207      * Primary constructor, used to drive remainder of the test.
 208      *
 209      * Fork off the other side, then do your work.
 210      */
 211     SSLSessionNulls() throws Exception {
 212         if (separateServerThread) {
 213             startServer(true);
 214             startClient(false);
 215         } else {
 216             startClient(true);
 217             startServer(false);
 218         }
 219 
 220         /*
 221          * Wait for other side to close down.
 222          */
 223         if (separateServerThread) {
 224             serverThread.join();
 225         } else {
 226             clientThread.join();
 227         }
 228 
 229         /*
 230          * When we get here, the test is pretty much over.
 231          *
 232          * If the main thread excepted, that propagates back
 233          * immediately.  If the other thread threw an exception, we
 234          * should report back.
 235          */
 236         if (serverException != null) {
 237             System.out.print("Server Exception:");
 238             throw serverException;
 239         }
 240         if (clientException != null) {
 241             System.out.print("Client Exception:");
 242             throw clientException;
 243         }
 244     }
 245 
 246     void startServer(boolean newThread) throws Exception {
 247         if (newThread) {
 248             serverThread = new Thread() {
 249                 public void run() {
 250                     try {
 251                         doServerSide();
 252                     } catch (Exception e) {
 253                         /*
 254                          * Our server thread just died.
 255                          *
 256                          * Release the client, if not active already...
 257                          */
 258                         System.err.println("Server died...");
 259                         serverReady = true;
 260                         serverException = e;
 261                     }
 262                 }
 263             };
 264             serverThread.start();
 265         } else {
 266             doServerSide();
 267         }
 268     }
 269 
 270     void startClient(boolean newThread) throws Exception {
 271         if (newThread) {
 272             clientThread = new Thread() {
 273                 public void run() {
 274                     try {
 275                         doClientSide();
 276                     } catch (Exception e) {
 277                         /*
 278                          * Our client thread just died.
 279                          */
 280                         System.err.println("Client died...");
 281                         clientException = e;
 282                     }
 283                 }
 284             };
 285             clientThread.start();
 286         } else {
 287             doClientSide();
 288         }
 289     }
 290 }
   1 /*
   2  * Copyright (c) 2001, 2016, 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
  27  * @summary Need to revisit the javadocs for JSSE, especially the
  28  *      promoted classes.
  29  * @library /javax/net/ssl/templates
  30  * @run main/othervm SSLSessionNulls
  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.io.IOException;
  38 import java.io.InputStream;
  39 import java.io.OutputStream;
  40 
  41 import javax.net.ssl.SSLSession;






  42 
  43 public class SSLSessionNulls {





  44 
  45     /*
  46      * Where do we find the keystores?
  47      */
  48     static String pathToStores = "../etc";
  49     static String keyStoreFile = "keystore";
  50     static String trustStoreFile = "truststore";
  51     static String passwd = "passphrase";
  52 
  53     public static void main(String[] args) throws Exception {
  54         String keyFilename =
  55             System.getProperty("test.src", "./") + "/" + pathToStores +
  56                 "/" + keyStoreFile;
  57         String trustFilename =
  58             System.getProperty("test.src", "./") + "/" + pathToStores +
  59                 "/" + trustStoreFile;
  60         SSLTest.setup(keyFilename, trustFilename, passwd);




























  61 
  62         new SSLTest()
  63             .setServerApplication((socket, test) -> {
  64                 InputStream sslIS = socket.getInputStream();
  65                 OutputStream sslOS = socket.getOutputStream();
  66 
  67                 sslIS.read();
  68                 sslOS.write(85);
  69                 sslOS.flush();
  70             })
  71             .setClientApplication((socket, test) -> {
  72                 InputStream sslIS = socket.getInputStream();
  73                 OutputStream sslOS = socket.getOutputStream();






















  74 
  75                 sslOS.write(280);
  76                 sslOS.flush();
  77                 sslIS.read();
  78 
  79                 SSLSession sslSession = socket.getSession();
  80 
  81                 try {
  82                     sslSession.getValue(null);
  83                 } catch (IllegalArgumentException e) {
  84                     System.out.print("Caught proper exception: ");
  85                     System.out.println(e.getMessage());
  86                 }
  87 
  88                 try {
  89                     sslSession.putValue(null, null);
  90                 } catch (IllegalArgumentException e) {
  91                     System.out.print("Caught proper exception: ");
  92                     System.out.println(e.getMessage());
  93                 }
  94 
  95                 try {
  96                     sslSession.removeValue(null);
  97                 } catch (IllegalArgumentException e) {
  98                     System.out.print("Caught proper exception: ");
  99                     System.out.println(e.getMessage());
 100                 }
 101 
 102                 String [] names = sslSession.getValueNames();
 103                 if ((names == null) || (names.length != 0)) {
 104                     throw new IOException(
 105                         "getValueNames didn't return 0-length arrary");
 106                 }
 107             })
 108             .runTest();

























































































































 109     }
 110 }
< prev index next >