1 /*
   2  * Copyright (c) 2012, 2016, 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 /* @test
  25  * @bug 4957669 5017871
  26  * @summary cannot load class names containing some JSR 202 characters;
  27  *          plugin does not escape unicode character in http request
  28  * @modules java.desktop/sun.applet
  29  *          jdk.httpserver
  30  * @compile -XDignore.symbol.file=true ClassnameCharTest.java
  31  * @run main ClassnameCharTest
  32  */
  33 
  34 import java.io.*;
  35 import java.net.*;
  36 import java.util.jar.*;
  37 import com.sun.net.httpserver.*;
  38 import sun.applet.AppletClassLoader;
  39 
  40 public class ClassnameCharTest {
  41     static String FNPrefix = System.getProperty("test.src", ".") + File.separator;
  42     static File classesJar = new File(FNPrefix + "testclasses.jar");
  43     static HttpServer server;
  44 
  45     public static void realMain(String[] args) throws Exception {
  46         server = HttpServer.create(new InetSocketAddress(0), 0);
  47         server.createContext("/", new HttpHandler() {
  48             @Override
  49             public void handle(HttpExchange exchange) {
  50                 try {
  51                     String filename = exchange.getRequestURI().getPath();
  52                     System.out.println("getRequestURI = " + exchange.getRequestURI());
  53                     System.out.println("filename = " + filename);
  54                     try (FileInputStream fis = new FileInputStream(classesJar);
  55                          JarInputStream jis = new JarInputStream(fis)) {
  56                         JarEntry entry;
  57                         while ((entry = jis.getNextJarEntry()) != null) {
  58                             if (filename.endsWith(entry.getName())) {
  59                                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
  60                                 byte[] buf = new byte[8092];
  61                                 int count = 0;
  62                                 while ((count = jis.read(buf)) != -1)
  63                                     baos.write(buf, 0, count);
  64                                 exchange.sendResponseHeaders(200, baos.size());
  65                                 try (OutputStream os = exchange.getResponseBody()) {
  66                                     baos.writeTo(os);
  67                                 }
  68                                 return;
  69                             }
  70                         }
  71                         fail("Failed to find " + filename);
  72                     }
  73                 } catch (IOException e) {
  74                     unexpected(e);
  75                 }
  76             }
  77         });
  78         server.start();
  79         try {
  80             URL base = new URL("http://localhost:" + server.getAddress().getPort());
  81             System.out.println ("Server: listening on " + base);
  82             MyAppletClassLoader acl = new MyAppletClassLoader(base);
  83             Class<?> class1 = acl.findClass("fo o");
  84             System.out.println("class1 = " + class1);
  85             pass();
  86             // can't test the following class unless platform in unicode locale
  87             // Class class2 = acl.findClass("\u624b\u518c");
  88             // System.out.println("class2 = "+class2);
  89         } finally {
  90             server.stop(0);
  91         }
  92     }
  93 
  94     static class MyAppletClassLoader extends AppletClassLoader {
  95         MyAppletClassLoader(URL base) {
  96             super(base);
  97         }
  98 
  99         @Override
 100         public Class<?> findClass(String name) throws ClassNotFoundException {
 101             return super.findClass(name);
 102         }
 103     }
 104 
 105     //--------------------- Infrastructure ---------------------------
 106     static volatile int passed = 0, failed = 0;
 107 
 108     static boolean pass() {
 109         passed++;
 110         return true;
 111     }
 112 
 113     static boolean fail() {
 114         failed++;
 115         if (server != null) {
 116             server.stop(0);
 117         }
 118         Thread.dumpStack();
 119         return false;
 120     }
 121 
 122     static boolean fail(String msg) {
 123         System.out.println(msg);
 124         return fail();
 125     }
 126 
 127     static void unexpected(Throwable t) {
 128         failed++;
 129         if (server != null) {
 130             server.stop(0);
 131         }
 132         t.printStackTrace();
 133     }
 134 
 135     static boolean check(boolean cond) {
 136         if (cond) {
 137             pass();
 138         } else {
 139             fail();
 140         }
 141         return cond;
 142     }
 143 
 144     static boolean equal(Object x, Object y) {
 145         if (x == null ? y == null : x.equals(y)) {
 146             return pass();
 147         } else {
 148             return fail(x + " not equal to " + y);
 149         }
 150     }
 151 
 152     public static void main(String[] args) throws Throwable {
 153         try {
 154             realMain(args);
 155         } catch (Throwable t) {
 156             unexpected(t);
 157         }
 158         System.out.println("\nPassed = " + passed + " failed = " + failed);
 159         if (failed > 0) {
 160             throw new AssertionError("Some tests failed");
 161         }
 162     }
 163 }