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