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