jdk/test/sun/security/tools/keytool/PrintSSL.java
changeset 39461 62d04767cf47
parent 23237 0c0c7f131faa
child 41960 916bb3d29d7b
equal deleted inserted replaced
39460:93e3e4d9b378 39461:62d04767cf47
     1 /*
     1 /*
     2  * Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    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
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 // Read printssl.sh, this Java program starts an SSL server
    24 /*
       
    25  * @test
       
    26  * @bug 6480981 8160624
       
    27  * @modules java.base/sun.security.tools.keytool
       
    28  * @summary keytool should be able to import certificates from remote SSL server
       
    29  * @run main/othervm PrintSSL
       
    30  */
    25 
    31 
       
    32 import java.io.IOException;
    26 import java.net.ServerSocket;
    33 import java.net.ServerSocket;
       
    34 import java.util.concurrent.CountDownLatch;
    27 import javax.net.ssl.SSLServerSocketFactory;
    35 import javax.net.ssl.SSLServerSocketFactory;
    28 import javax.net.ssl.SSLSocket;
    36 import javax.net.ssl.SSLSocket;
    29 
    37 
    30 public class PrintSSL {
    38 public class PrintSSL {
       
    39 
    31     public static void main(String[] args) throws Exception {
    40     public static void main(String[] args) throws Exception {
    32         System.setProperty("javax.net.ssl.keyStorePassword", "passphrase");
    41 
    33         System.setProperty("javax.net.ssl.keyStore",
    42         int port = new Server().start();
    34                            System.getProperty("test.src", "./")
    43         if(port == -1) {
    35                                + "/../../../../javax/net/ssl/etc/keystore");
    44             throw new RuntimeException("Unable start ssl server.");
    36         SSLServerSocketFactory sslssf =
    45         }
    37                 (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
    46         String vmOpt = System.getProperty("TESTTOOLVMOPTS");
    38         final ServerSocket server = sslssf.createServerSocket(0);
    47         String cmd = String.format(
    39         System.out.println(server.getLocalPort());
    48                 "-debug %s -printcert -sslserver localhost:%s",
    40         System.out.flush();
    49                 ((vmOpt == null) ? "" : vmOpt ), port);
    41         Thread t = new Thread() {
    50         sun.security.tools.keytool.Main.main(cmd.split("\\s+"));
    42             public void run() {
    51     }
    43                 try {
    52 
    44                     Thread.sleep(30000);
    53     private static class Server implements Runnable {
    45                     server.close();
    54 
    46                 } catch (Exception e) {
    55         private volatile int serverPort = -1;
    47                     ;
    56         private final CountDownLatch untilServerReady = new CountDownLatch(1);
    48                 }
    57 
    49                 throw new RuntimeException("Timeout");
    58         public int start() throws InterruptedException {
       
    59 
       
    60             Thread server = new Thread(this);
       
    61             server.setDaemon(true);
       
    62             server.start();
       
    63             untilServerReady.await();
       
    64             return this.getServerPort();
       
    65         }
       
    66 
       
    67         @Override
       
    68         public void run() {
       
    69 
       
    70             System.setProperty("javax.net.ssl.keyStorePassword", "passphrase");
       
    71             System.setProperty("javax.net.ssl.keyStore",
       
    72                     System.getProperty("test.src", "./")
       
    73                     + "/../../../../javax/net/ssl/etc/keystore");
       
    74             SSLServerSocketFactory sslssf =
       
    75                     (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
       
    76             try (ServerSocket server = sslssf.createServerSocket(0)) {
       
    77                 this.serverPort = server.getLocalPort();
       
    78                 System.out.printf("%nServer started on: %s%n", getServerPort());
       
    79                 untilServerReady.countDown();
       
    80                 ((SSLSocket)server.accept()).startHandshake();
       
    81             } catch (Throwable e) {
       
    82                 e.printStackTrace(System.out);
       
    83                 untilServerReady.countDown();
    50             }
    84             }
    51         };
    85 
    52         t.setDaemon(true);
    86         }
    53         t.start();
    87 
    54         ((SSLSocket)server.accept()).startHandshake();
    88         public int getServerPort() {
       
    89             return this.serverPort;
       
    90         }
       
    91 
    55     }
    92     }
       
    93 
    56 }
    94 }