8055747: Move SimpleSSLContext to jdk/testlibrary
authormichaelm
Tue, 26 Aug 2014 10:10:58 +0100
changeset 26206 fb87c4051d65
parent 26205 c073791a67de
child 26207 a02f6165d5be
8055747: Move SimpleSSLContext to jdk/testlibrary Reviewed-by: chegar
jdk/test/com/sun/net/httpserver/SelCacheTest.java
jdk/test/com/sun/net/httpserver/SimpleSSLContext.java
jdk/test/com/sun/net/httpserver/Test1.java
jdk/test/com/sun/net/httpserver/Test12.java
jdk/test/com/sun/net/httpserver/Test13.java
jdk/test/com/sun/net/httpserver/Test6a.java
jdk/test/com/sun/net/httpserver/Test7a.java
jdk/test/com/sun/net/httpserver/Test8a.java
jdk/test/com/sun/net/httpserver/Test9.java
jdk/test/com/sun/net/httpserver/Test9a.java
jdk/test/com/sun/net/httpserver/testkeys
jdk/test/java/net/URLPermission/URLTest.java
jdk/test/java/net/URLPermission/policy.1
jdk/test/java/net/URLPermission/policy.2
jdk/test/java/net/URLPermission/policy.3
jdk/test/lib/testlibrary/jdk/testlibrary/SimpleSSLContext.java
jdk/test/lib/testlibrary/jdk/testlibrary/testkeys
--- a/jdk/test/com/sun/net/httpserver/SelCacheTest.java	Mon Aug 25 18:17:24 2014 +0200
+++ b/jdk/test/com/sun/net/httpserver/SelCacheTest.java	Tue Aug 26 10:10:58 2014 +0100
@@ -24,11 +24,14 @@
 /**
  * @test
  * @bug 6270015
+ * @library /lib/testlibrary/
+ * @build jdk.testlibrary.SimpleSSLContext
  * @run main/othervm -Dsun.net.httpserver.selCacheTimeout=2 SelCacheTest
  * @summary  Light weight HTTP server
  */
 
 import com.sun.net.httpserver.*;
+import jdk.testlibrary.SimpleSSLContext;
 
 import java.util.*;
 import java.util.concurrent.*;
@@ -65,7 +68,7 @@
             executor = Executors.newCachedThreadPool();
             s1.setExecutor (executor);
             s2.setExecutor (executor);
-            ctx = new SimpleSSLContext(System.getProperty("test.src")).get();
+            ctx = new SimpleSSLContext().get();
             s2.setHttpsConfigurator(new HttpsConfigurator (ctx));
             s1.start();
             s2.start();
--- a/jdk/test/com/sun/net/httpserver/SimpleSSLContext.java	Mon Aug 25 18:17:24 2014 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,69 +0,0 @@
-/*
- * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-import com.sun.net.httpserver.*;
-
-import java.util.*;
-import java.util.concurrent.*;
-import java.io.*;
-import java.net.*;
-import java.security.*;
-import java.security.cert.*;
-import javax.net.ssl.*;
-
-public class SimpleSSLContext {
-
-    SSLContext ssl;
-
-    SimpleSSLContext (String dir) throws IOException {
-        try {
-            String file = dir+"/testkeys";
-            char[] passphrase = "passphrase".toCharArray();
-            KeyStore ks = KeyStore.getInstance("JKS");
-            ks.load(new FileInputStream(file), passphrase);
-
-            KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
-            kmf.init(ks, passphrase);
-
-            TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
-            tmf.init(ks);
-
-            ssl = SSLContext.getInstance ("TLS");
-            ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
-        } catch (KeyManagementException e) {
-            throw new RuntimeException (e.getMessage());
-        } catch (KeyStoreException e) {
-            throw new RuntimeException (e.getMessage());
-        } catch (UnrecoverableKeyException e) {
-            throw new RuntimeException (e.getMessage());
-        } catch (CertificateException e) {
-            throw new RuntimeException (e.getMessage());
-        } catch (NoSuchAlgorithmException e) {
-            throw new RuntimeException (e.getMessage());
-        }
-    }
-
-    SSLContext get () {
-        return ssl;
-    }
-}
--- a/jdk/test/com/sun/net/httpserver/Test1.java	Mon Aug 25 18:17:24 2014 +0200
+++ b/jdk/test/com/sun/net/httpserver/Test1.java	Tue Aug 26 10:10:58 2014 +0100
@@ -24,6 +24,8 @@
 /**
  * @test
  * @bug 6270015
+ * @library /lib/testlibrary/
+ * @build jdk.testlibrary.SimpleSSLContext
  * @run main/othervm Test1
  * @run main/othervm -Dsun.net.httpserver.maxReqTime=10 Test1
  * @run main/othervm -Dsun.net.httpserver.nodelay=true Test1
@@ -36,6 +38,7 @@
 import java.io.*;
 import java.net.*;
 import javax.net.ssl.*;
+import jdk.testlibrary.SimpleSSLContext;
 
 /* basic http/s connectivity test
  * Tests:
@@ -72,7 +75,7 @@
             executor = Executors.newCachedThreadPool();
             s1.setExecutor (executor);
             s2.setExecutor (executor);
-            ctx = new SimpleSSLContext(System.getProperty("test.src")).get();
+            ctx = new SimpleSSLContext().get();
             s2.setHttpsConfigurator(new HttpsConfigurator (ctx));
             s1.start();
             s2.start();
--- a/jdk/test/com/sun/net/httpserver/Test12.java	Mon Aug 25 18:17:24 2014 +0200
+++ b/jdk/test/com/sun/net/httpserver/Test12.java	Tue Aug 26 10:10:58 2014 +0100
@@ -24,6 +24,8 @@
 /**
  * @test
  * @bug 6270015
+ * @library /lib/testlibrary/
+ * @build jdk.testlibrary.SimpleSSLContext
  * @run main/othervm Test12
  * @summary  Light weight HTTP server
  */
@@ -34,6 +36,7 @@
 import java.io.*;
 import java.net.*;
 import javax.net.ssl.*;
+import jdk.testlibrary.SimpleSSLContext;
 
 /* basic http/s connectivity test
  * Tests:
@@ -62,7 +65,7 @@
             executor = Executors.newCachedThreadPool();
             s1.setExecutor (executor);
             s2.setExecutor (executor);
-            ctx = new SimpleSSLContext(System.getProperty("test.src")).get();
+            ctx = new SimpleSSLContext().get();
             s2.setHttpsConfigurator(new HttpsConfigurator (ctx));
             s1.start();
             s2.start();
--- a/jdk/test/com/sun/net/httpserver/Test13.java	Mon Aug 25 18:17:24 2014 +0200
+++ b/jdk/test/com/sun/net/httpserver/Test13.java	Tue Aug 26 10:10:58 2014 +0100
@@ -24,6 +24,8 @@
 /**
  * @test
  * @bug 6270015
+ * @library /lib/testlibrary/
+ * @build jdk.testlibrary.SimpleSSLContext
  * @run main/othervm Test13
  * @summary  Light weight HTTP server
  */
@@ -34,8 +36,8 @@
 import java.util.logging.*;
 import java.io.*;
 import java.net.*;
-
 import javax.net.ssl.*;
+import jdk.testlibrary.SimpleSSLContext;
 
 /* basic http/s connectivity test
  * Tests:
@@ -71,7 +73,7 @@
             executor = Executors.newCachedThreadPool();
             s1.setExecutor (executor);
             s2.setExecutor (executor);
-            ctx = new SimpleSSLContext(System.getProperty("test.src")).get();
+            ctx = new SimpleSSLContext().get();
             s2.setHttpsConfigurator(new HttpsConfigurator (ctx));
             s1.start();
             s2.start();
--- a/jdk/test/com/sun/net/httpserver/Test6a.java	Mon Aug 25 18:17:24 2014 +0200
+++ b/jdk/test/com/sun/net/httpserver/Test6a.java	Tue Aug 26 10:10:58 2014 +0100
@@ -24,6 +24,8 @@
 /**
  * @test
  * @bug 6270015
+ * @library /lib/testlibrary/
+ * @build jdk.testlibrary.SimpleSSLContext
  * @run main/othervm Test6a
  * @summary  Light weight HTTP server
  */
@@ -34,6 +36,7 @@
 import java.io.*;
 import java.net.*;
 import javax.net.ssl.*;
+import jdk.testlibrary.SimpleSSLContext;
 
 /**
  * Test https POST large file via chunked encoding (unusually small chunks)
@@ -47,7 +50,7 @@
         HttpsServer server = HttpsServer.create (addr, 0);
         HttpContext ctx = server.createContext ("/test", handler);
         ExecutorService executor = Executors.newCachedThreadPool();
-        SSLContext ssl = new SimpleSSLContext(System.getProperty("test.src")).get();
+        SSLContext ssl = new SimpleSSLContext().get();
         server.setExecutor (executor);
         server.setHttpsConfigurator(new HttpsConfigurator (ssl));
         server.start ();
--- a/jdk/test/com/sun/net/httpserver/Test7a.java	Mon Aug 25 18:17:24 2014 +0200
+++ b/jdk/test/com/sun/net/httpserver/Test7a.java	Tue Aug 26 10:10:58 2014 +0100
@@ -24,6 +24,8 @@
 /**
  * @test
  * @bug 6270015
+ * @library /lib/testlibrary/
+ * @build jdk.testlibrary.SimpleSSLContext
  * @run main/othervm Test7a
  * @summary  Light weight HTTP server
  */
@@ -34,6 +36,7 @@
 import java.io.*;
 import java.net.*;
 import javax.net.ssl.*;
+import jdk.testlibrary.SimpleSSLContext;
 
 /**
  * Test POST large file via chunked encoding (large chunks)
@@ -52,7 +55,7 @@
         HttpsServer server = HttpsServer.create (addr, 0);
         HttpContext ctx = server.createContext ("/test", handler);
         ExecutorService executor = Executors.newCachedThreadPool();
-        SSLContext ssl = new SimpleSSLContext(System.getProperty("test.src")).get();
+        SSLContext ssl = new SimpleSSLContext().get();
         server.setHttpsConfigurator(new HttpsConfigurator (ssl));
         server.setExecutor (executor);
         server.start ();
--- a/jdk/test/com/sun/net/httpserver/Test8a.java	Mon Aug 25 18:17:24 2014 +0200
+++ b/jdk/test/com/sun/net/httpserver/Test8a.java	Tue Aug 26 10:10:58 2014 +0100
@@ -24,6 +24,8 @@
 /**
  * @test
  * @bug 6270015
+ * @library /lib/testlibrary/
+ * @build jdk.testlibrary.SimpleSSLContext
  * @run main/othervm Test8a
  * @summary  Light weight HTTP server
  */
@@ -34,6 +36,7 @@
 import java.io.*;
 import java.net.*;
 import javax.net.ssl.*;
+import jdk.testlibrary.SimpleSSLContext;
 
 /**
  * Test POST large file via fixed len encoding
@@ -55,7 +58,7 @@
             server = HttpsServer.create (addr, 0);
             HttpContext ctx = server.createContext ("/test", handler);
             executor = Executors.newCachedThreadPool();
-            SSLContext ssl = new SimpleSSLContext(System.getProperty("test.src")).get();
+            SSLContext ssl = new SimpleSSLContext().get();
             server.setHttpsConfigurator(new HttpsConfigurator (ssl));
             server.setExecutor (executor);
             server.start ();
--- a/jdk/test/com/sun/net/httpserver/Test9.java	Mon Aug 25 18:17:24 2014 +0200
+++ b/jdk/test/com/sun/net/httpserver/Test9.java	Tue Aug 26 10:10:58 2014 +0100
@@ -24,6 +24,8 @@
 /**
  * @test
  * @bug 6270015
+ * @library /lib/testlibrary/
+ * @build jdk.testlibrary.SimpleSSLContext
  * @run main/othervm Test9
  * @summary  Light weight HTTP server
  */
@@ -34,6 +36,7 @@
 import java.io.*;
 import java.net.*;
 import javax.net.ssl.*;
+import jdk.testlibrary.SimpleSSLContext;
 
 /* Same as Test1 but requests run in parallel.
  */
@@ -59,7 +62,7 @@
             executor = Executors.newCachedThreadPool();
             s1.setExecutor (executor);
             s2.setExecutor (executor);
-            ctx = new SimpleSSLContext(System.getProperty("test.src")).get();
+            ctx = new SimpleSSLContext().get();
             s2.setHttpsConfigurator(new HttpsConfigurator (ctx));
             s1.start();
             s2.start();
--- a/jdk/test/com/sun/net/httpserver/Test9a.java	Mon Aug 25 18:17:24 2014 +0200
+++ b/jdk/test/com/sun/net/httpserver/Test9a.java	Tue Aug 26 10:10:58 2014 +0100
@@ -24,6 +24,8 @@
 /**
  * @test
  * @bug 6270015
+ * @library /lib/testlibrary/
+ * @build jdk.testlibrary.SimpleSSLContext
  * @run main/othervm Test9a
  * @summary  Light weight HTTP server
  */
@@ -34,6 +36,7 @@
 import java.io.*;
 import java.net.*;
 import javax.net.ssl.*;
+import jdk.testlibrary.SimpleSSLContext;
 
 /* Same as Test1 but requests run in parallel.
  */
@@ -56,8 +59,8 @@
             HttpContext c1 = server.createContext ("/test1", h);
             executor = Executors.newCachedThreadPool();
             server.setExecutor (executor);
-            serverCtx = new SimpleSSLContext(System.getProperty("test.src")).get();
-            clientCtx = new SimpleSSLContext(System.getProperty("test.src")).get();
+            serverCtx = new SimpleSSLContext().get();
+            clientCtx = new SimpleSSLContext().get();
             server.setHttpsConfigurator(new HttpsConfigurator (serverCtx));
             server.start();
 
Binary file jdk/test/com/sun/net/httpserver/testkeys has changed
--- a/jdk/test/java/net/URLPermission/URLTest.java	Mon Aug 25 18:17:24 2014 +0200
+++ b/jdk/test/java/net/URLPermission/URLTest.java	Tue Aug 26 10:10:58 2014 +0100
@@ -27,7 +27,8 @@
  *
  * @test
  * @bug 8010464
- * @compile ../../../com/sun/net/httpserver/SimpleSSLContext.java
+ * @library /lib/testlibrary/
+ * @build jdk.testlibrary.SimpleSSLContext
  * @run main/othervm/policy=policy.1 URLTest one
  * @run main/othervm URLTest one
  * @run main/othervm/policy=policy.2 URLTest two
@@ -43,6 +44,7 @@
 import java.util.logging.*;
 import com.sun.net.httpserver.*;
 import javax.net.ssl.*;
+import jdk.testlibrary.SimpleSSLContext;
 
 public class URLTest {
     static boolean failed = false;
@@ -209,10 +211,7 @@
         httpServer.setExecutor (e);
         httpsServer.setExecutor (es);
 
-        // take the keystore from elsewhere in test hierarchy
-        String keysdir = System.getProperty("test.src")
-                + "/../../../com/sun/net/httpserver/";
-        ctx = new SimpleSSLContext(keysdir).get();
+        ctx = new SimpleSSLContext().get();
         httpsServer.setHttpsConfigurator(new HttpsConfigurator (ctx));
 
         httpServer.start();
--- a/jdk/test/java/net/URLPermission/policy.1	Mon Aug 25 18:17:24 2014 +0200
+++ b/jdk/test/java/net/URLPermission/policy.1	Tue Aug 26 10:10:58 2014 +0100
@@ -28,12 +28,13 @@
     // needed for HttpServer
     permission "java.net.SocketPermission" "localhost:1024-", "listen,resolve,accept";
     permission "java.util.PropertyPermission" "test.src", "read";
-    permission java.io.FilePermission "${test.src}/../../../com/sun/net/httpserver/testkeys", "read";
+    permission java.io.FilePermission "${test.src}/../../../lib/testlibrary/jdk/testlibrary/testkeys", "read";
 
     //permission "java.util.logging.LoggingPermission" "control";
     //permission "java.io.FilePermission" "/tmp/-", "read,write";
     permission "java.lang.RuntimePermission" "modifyThread";
     permission "java.lang.RuntimePermission" "setFactory";
+    permission "java.util.PropertyPermission" "test.src.path", "read";
 };
 
 // Normal permissions that aren't granted when run under jtreg
--- a/jdk/test/java/net/URLPermission/policy.2	Mon Aug 25 18:17:24 2014 +0200
+++ b/jdk/test/java/net/URLPermission/policy.2	Tue Aug 26 10:10:58 2014 +0100
@@ -28,12 +28,13 @@
     // needed for HttpServer
     permission "java.net.SocketPermission" "localhost:1024-", "listen,resolve,accept";
     permission "java.util.PropertyPermission" "test.src", "read";
-    permission java.io.FilePermission "${test.src}/../../../com/sun/net/httpserver/testkeys", "read";
+    permission java.io.FilePermission "${test.src}/../../../lib/testlibrary/jdk/testlibrary/testkeys", "read";
 
     //permission "java.util.logging.LoggingPermission" "control";
     //permission "java.io.FilePermission" "/tmp/-", "read,write";
     permission "java.lang.RuntimePermission" "modifyThread";
     permission "java.lang.RuntimePermission" "setFactory";
+    permission "java.util.PropertyPermission" "test.src.path", "read";
 };
 
 // Normal permissions that aren't granted when run under jtreg
--- a/jdk/test/java/net/URLPermission/policy.3	Mon Aug 25 18:17:24 2014 +0200
+++ b/jdk/test/java/net/URLPermission/policy.3	Tue Aug 26 10:10:58 2014 +0100
@@ -28,12 +28,13 @@
     // needed for HttpServer
     permission "java.net.SocketPermission" "localhost:1024-", "listen,resolve,accept";
     permission "java.util.PropertyPermission" "test.src", "read";
-    permission java.io.FilePermission "${test.src}/../../../com/sun/net/httpserver/testkeys", "read";
+    permission java.io.FilePermission "${test.src}/../../../lib/testlibrary/jdk/testlibrary/testkeys", "read";
 
     //permission "java.util.logging.LoggingPermission" "control";
     //permission "java.io.FilePermission" "/tmp/-", "read,write";
     permission "java.lang.RuntimePermission" "modifyThread";
     permission "java.lang.RuntimePermission" "setFactory";
+    permission "java.util.PropertyPermission" "test.src.path", "read";
 };
 
 // Normal permissions that aren't granted when run under jtreg
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/lib/testlibrary/jdk/testlibrary/SimpleSSLContext.java	Tue Aug 26 10:10:58 2014 +0100
@@ -0,0 +1,119 @@
+/*
+ * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package jdk.testlibrary;
+
+import com.sun.net.httpserver.*;
+
+import java.util.*;
+import java.util.concurrent.*;
+import java.io.*;
+import java.net.*;
+import java.security.*;
+import java.security.cert.*;
+import javax.net.ssl.*;
+
+/**
+ * Creates a simple usable SSLContext for SSLSocketFactory
+ * or a HttpsServer using either a given keystore or a default
+ * one in the test tree.
+ *
+ * Using this class with a security manager requires the following
+ * permissions to be granted:
+ *
+ * permission "java.util.PropertyPermission" "test.src.path", "read";
+ * permission java.io.FilePermission
+ *    "${test.src}/../../../lib/testlibrary/jdk/testlibrary/testkeys", "read";
+ * The exact path above depends on the location of the test.
+ */
+public class SimpleSSLContext {
+
+    SSLContext ssl;
+
+    /**
+     * loads default keystore from SimpleSSLContext
+     * source directory
+     */
+    public SimpleSSLContext () throws IOException {
+        String paths = System.getProperty("test.src.path");
+        StringTokenizer st = new StringTokenizer(paths,":");
+        boolean securityExceptions = false;
+        while (st.hasMoreTokens()) {
+            String path = st.nextToken();
+            try {
+                File f = new File(path, "jdk/testlibrary/testkeys");
+                if (f.exists()) {
+                    init (new FileInputStream(f));
+                    return;
+                }
+            } catch (SecurityException e) {
+                // catch and ignore because permission only required
+                // for one entry on path (at most)
+                securityExceptions = true;
+            }
+        }
+        if (securityExceptions) {
+            System.err.println("SecurityExceptions thrown on loading testkeys");
+        }
+    }
+
+    /**
+     * loads default keystore from given directory
+     */
+    public SimpleSSLContext (String dir) throws IOException {
+        String file = dir+"/testkeys";
+        FileInputStream fis = new FileInputStream(file);
+        init(fis);
+    }
+
+    private void init (InputStream i) throws IOException {
+        try {
+            char[] passphrase = "passphrase".toCharArray();
+            KeyStore ks = KeyStore.getInstance("JKS");
+            ks.load(i, passphrase);
+
+            KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
+            kmf.init(ks, passphrase);
+
+            TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
+            tmf.init(ks);
+
+            ssl = SSLContext.getInstance ("TLS");
+            ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
+        } catch (KeyManagementException e) {
+            throw new RuntimeException (e.getMessage());
+        } catch (KeyStoreException e) {
+            throw new RuntimeException (e.getMessage());
+        } catch (UnrecoverableKeyException e) {
+            throw new RuntimeException (e.getMessage());
+        } catch (CertificateException e) {
+            throw new RuntimeException (e.getMessage());
+        } catch (NoSuchAlgorithmException e) {
+            throw new RuntimeException (e.getMessage());
+        }
+    }
+
+    public SSLContext get () {
+        return ssl;
+    }
+}
Binary file jdk/test/lib/testlibrary/jdk/testlibrary/testkeys has changed