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