1 /*
   2  * Copyright (c) 2009, 2017, 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 4167874
  27  * @modules java.logging
  28  *          jdk.httpserver
  29  *          jdk.compiler
  30  * @library ../../../../com/sun/net/httpserver
  31  *          /test/lib
  32  * @build jdk.test.lib.compiler.CompilerUtils
  33  *        jdk.test.lib.util.FileUtils
  34  *        jdk.test.lib.util.JarUtils
  35  *        jdk.test.lib.Platform
  36  *        FileServerHandler
  37  * @run main/othervm CloseTest
  38  * @summary URL-downloaded jar files can consume all available file descriptors
  39  */
  40 
  41 import java.io.File;
  42 import java.io.IOException;
  43 import java.lang.reflect.Method;
  44 import java.net.URLClassLoader;
  45 import java.net.InetSocketAddress;
  46 import java.net.URL;
  47 import java.nio.file.Files;
  48 import java.nio.file.Path;
  49 import java.nio.file.Paths;
  50 
  51 import jdk.test.lib.compiler.CompilerUtils;
  52 import jdk.test.lib.util.JarUtils;
  53 
  54 import com.sun.net.httpserver.HttpContext;
  55 import com.sun.net.httpserver.HttpServer;
  56 
  57 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
  58 
  59 public class CloseTest extends Common {
  60     private static final String WORK_DIR = System.getProperty("user.dir")
  61             + "/";
  62 //
  63 // needs two jar files test1.jar and test2.jar with following structure
  64 //
  65 // com/foo/TestClass
  66 // com/foo/TestClass1
  67 // com/foo/Resource1
  68 // com/foo/Resource2
  69 //
  70 // and a directory hierarchy with the same structure/contents
  71 
  72     public static void main(String args[]) throws Exception {
  73         setup();
  74 
  75         startHttpServer(WORK_DIR + "serverRoot/");
  76 
  77         String testjar = WORK_DIR + "test.jar";
  78         copyFile(WORK_DIR + "test1.jar", testjar);
  79         test(testjar, 1);
  80 
  81         // repeat test with different implementation
  82         // of test.jar (whose TestClass.getValue() returns 2
  83         copyFile(WORK_DIR + "test2.jar", testjar);
  84         test(testjar, 2);
  85 
  86         // repeat test using a directory of files
  87         String testdir = WORK_DIR + "testdir/";
  88         rm_minus_rf(new File(testdir));
  89         copyDir(WORK_DIR + "test1/", testdir);
  90         test(testdir, 1);
  91 
  92         testdir = WORK_DIR + "testdir/";
  93         rm_minus_rf(new File(testdir));
  94         copyDir(WORK_DIR + "test2/", testdir);
  95         test(testdir, 2);
  96         getHttpServer().stop(3);
  97     }
  98 
  99     // create a loader on jarfile (or directory), plus a http loader
 100     // load a class , then look for a resource
 101     // also load a class from http loader
 102     // then close the loader
 103     // check further new classes/resources cannot be loaded
 104     // check jar (or dir) can be deleted
 105     // check existing classes can be loaded
 106     // check boot classes can be loaded
 107 
 108     static void test(String name, int expectedValue) throws Exception {
 109 
 110         URL url = new URL("file", null, name);
 111         URL url2 = getServerURL();
 112         System.out.println("Doing tests with URL: " + url + " and " + url2);
 113         URL[] urls = new URL[2];
 114         urls[0] = url;
 115         urls[1] = url2;
 116         URLClassLoader loader = new URLClassLoader(urls);
 117         Class testclass = loadClass("com.foo.TestClass", loader, true);
 118         Class class2 = loadClass("Test", loader, true); // from http
 119         class2.newInstance();
 120         Object test = testclass.newInstance();
 121         Method method = testclass.getDeclaredMethods()[0]; // int getValue();
 122         int res = (Integer) method.invoke(test);
 123 
 124         if (res != expectedValue) {
 125             throw new RuntimeException("wrong value from getValue() [" + res +
 126                     "/" + expectedValue + "]");
 127         }
 128 
 129         // should find /resource1
 130         URL u1 = loader.findResource("com/foo/Resource1");
 131         if (u1 == null) {
 132             throw new RuntimeException("can't find com/foo/Resource1 in test1.jar");
 133         }
 134         loader.close();
 135 
 136         // should NOT find /resource2 even though it is in jar
 137         URL u2 = loader.findResource("com/foo/Resource2");
 138         if (u2 != null) {
 139             throw new RuntimeException("com/foo/Resource2 unexpected in test1.jar");
 140         }
 141 
 142         // load tests
 143         loadClass("com.foo.TestClass1", loader, false);
 144         loadClass("com.foo.TestClass", loader, true);
 145         loadClass("java.util.ArrayList", loader, true);
 146 
 147         // now check we can delete the path
 148         rm_minus_rf(new File(name));
 149         System.out.println(" ... OK");
 150     }
 151 
 152     static HttpServer httpServer;
 153 
 154     static HttpServer getHttpServer() {
 155         return httpServer;
 156     }
 157 
 158     static URL getServerURL() throws Exception {
 159         int port = httpServer.getAddress().getPort();
 160         String s = "http://127.0.0.1:" + port + "/";
 161         return new URL(s);
 162     }
 163 
 164     static void startHttpServer(String docroot) throws Exception {
 165         httpServer = HttpServer.create(new InetSocketAddress(0), 10);
 166         HttpContext ctx = httpServer.createContext(
 167                 "/", new FileServerHandler(docroot)
 168         );
 169         httpServer.start();
 170     }
 171 
 172     /**
 173      * Prepare jars files for the tests
 174      */
 175     private static void setup () throws IOException {
 176         String[] tests = new String[]{"test1", "test2"};
 177         Path workDir = Paths.get(WORK_DIR);
 178         Path testSrc = Paths.get(System.getProperty("test.src"));
 179         for (String test : tests) {
 180             Path testSrcDir =  testSrc.resolve(test);
 181             Path testTargetDir = workDir.resolve(test);
 182             // Compile sources for corresponding test
 183             CompilerUtils.compile(testSrcDir, testTargetDir);
 184             // Copy all resources
 185             Path packages = Paths.get("com", "foo");
 186             Path copySrcDir = testSrcDir.resolve(packages);
 187             Path copyTargetDir = testTargetDir.resolve(packages);
 188             Files.createDirectories(copyTargetDir);
 189             Path res1 = Paths.get("Resource1");
 190             Path res2 = Paths.get("Resource2");
 191             Files.copy(copySrcDir.resolve(res1), copyTargetDir.resolve(res1),
 192                        REPLACE_EXISTING);
 193             Files.copy(copySrcDir.resolve(res2), copyTargetDir.resolve(res2),
 194                        REPLACE_EXISTING);
 195             // Create jar
 196             JarUtils.createJarFile(workDir.resolve(test + ".jar"), testTargetDir);
 197         }
 198 
 199         // Copy and compile server test class
 200         Path serverRoot = Paths.get("serverRoot");
 201         Path targetDir = workDir.resolve(serverRoot);
 202         Path file = Paths.get("Test.java");
 203         Files.createDirectories(targetDir);
 204         Files.copy(testSrc.resolve(serverRoot).resolve(file),
 205                    targetDir.resolve(file), REPLACE_EXISTING);
 206         CompilerUtils.compile(targetDir, targetDir);
 207     }
 208 }