Compare commits

...

3 Commits

Author SHA1 Message Date
Justin Lu
47f62aa273 Address offline review -> comments for maintainers, simplify exc and JAR_PATH 2025-06-12 11:00:41 -07:00
Justin Lu
a9cad71666 removing a typo 2025-06-12 09:31:03 -07:00
Justin Lu
afda9fe1fa Jai's review - Cleanup & JAR_PATH String -> Path 2025-06-12 09:29:14 -07:00

View File

@ -25,10 +25,8 @@
* @bug 4957669 5017871 8358729
* @summary cannot load class names containing some JSR 202 characters;
* plugin does not escape unicode character in http request
* @library /test/lib
* @modules java.base/sun.net.www
* jdk.httpserver
* @compile -XDignore.symbol.file=true ClassnameCharTest.java
* @run main ClassnameCharTest
*/
@ -36,15 +34,14 @@ import java.io.*;
import java.lang.classfile.ClassFile;
import java.lang.constant.ClassDesc;
import java.net.*;
import java.nio.file.Path;
import java.security.CodeSource;
import java.util.jar.*;
import com.sun.net.httpserver.*;
import jdk.test.lib.Utils;
import sun.net.www.ParseUtil;
public class ClassnameCharTest {
private static final String JAR_PATH = Utils.TEST_CLASSES + Utils.FILE_SEPARATOR + "testclasses.jar";
static File classesJar = new File(JAR_PATH);
private static final Path JAR_PATH = Path.of("testclasses.jar");
static HttpServer server;
public static void realMain(String[] args) throws Exception {
@ -56,7 +53,7 @@ public class ClassnameCharTest {
String filename = exchange.getRequestURI().getPath();
System.out.println("getRequestURI = " + exchange.getRequestURI());
System.out.println("filename = " + filename);
try (FileInputStream fis = new FileInputStream(classesJar);
try (FileInputStream fis = new FileInputStream(JAR_PATH.toFile());
JarInputStream jis = new JarInputStream(fis)) {
JarEntry entry;
while ((entry = jis.getNextJarEntry()) != null) {
@ -107,7 +104,7 @@ public class ClassnameCharTest {
}
@Override
public Class<?> findClass(String name) throws ClassNotFoundException {
public Class<?> findClass(String name) {
int index = name.indexOf(';');
String cookie = "";
if(index != -1) {
@ -118,13 +115,15 @@ public class ClassnameCharTest {
// check loaded JAR files
try {
return super.findClass(name);
} catch (ClassNotFoundException e) {
}
} catch (ClassNotFoundException _) {}
// Otherwise, try loading the class from the code base URL
// final String path = name.replace('.', '/').concat(".class").concat(cookie);
String encodedName = ParseUtil.encodePath(name.replace('.', '/'), false);
final String path = (new StringBuffer(encodedName)).append(".class").append(cookie).toString();
Exception exc = null;
// try block used for checked exceptions as well as ClassFormatError
// from defineClass call
try {
URL finalURL = new URL(base, path);
// Make sure the codebase won't be modified
@ -134,8 +133,13 @@ public class ClassnameCharTest {
byte[] b = getBytes(finalURL);
return defineClass(name, b, 0, b.length, codesource);
}
} catch (Exception _) {}
throw new ClassNotFoundException(name);
// protocol/host/port mismatch, fail with RuntimeExc
} catch (Exception underlyingE) {
exc = underlyingE; // Most likely CFE from defineClass
}
// Fail if there was either a protocol/host/port mismatch
// or an exception was thrown (which is propagated)
throw new RuntimeException(name, exc);
}
/*
@ -216,7 +220,7 @@ public class ClassnameCharTest {
// Create the class file and write it to the testable jar
static void buildJar() throws IOException {
var bytes = ClassFile.of().build(ClassDesc.of("fo o"), _ -> {});
try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(JAR_PATH))) {
try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(JAR_PATH.toFile()))) {
jos.putNextEntry(new JarEntry("fo o.class"));
jos.write(bytes, 0, bytes.length);
jos.closeEntry();