Skip to content
Permalink
Browse files
Merge
  • Loading branch information
coffeys committed Jun 11, 2020
2 parents bb86779 + 6dec50a commit 15999a9f21d30a63993caeffd8fc561388338534
Showing 3 changed files with 64 additions and 60 deletions.
@@ -109,7 +109,7 @@ public synchronized List<EventType> getRegisteredEventTypes() {
}

public synchronized EventType getEventType(Class<? extends jdk.internal.event.Event> eventClass) {
EventHandler h = getHandler(eventClass);
EventHandler h = getHandler(eventClass, false);
if (h != null && h.isRegistered()) {
return h.getEventType();
}
@@ -118,7 +118,7 @@ public synchronized EventType getEventType(Class<? extends jdk.internal.event.Ev

public synchronized void unregister(Class<? extends Event> eventClass) {
Utils.checkRegisterPermission();
EventHandler handler = getHandler(eventClass);
EventHandler handler = getHandler(eventClass, false);
if (handler != null) {
handler.setRegistered(false);
}
@@ -130,7 +130,7 @@ public synchronized EventType register(Class<? extends jdk.internal.event.Event>

public synchronized EventType register(Class<? extends jdk.internal.event.Event> eventClass, List<AnnotationElement> dynamicAnnotations, List<ValueDescriptor> dynamicFields) {
Utils.checkRegisterPermission();
EventHandler handler = getHandler(eventClass);
EventHandler handler = getHandler(eventClass, true);
if (handler == null) {
if (eventClass.getAnnotation(MirrorEvent.class) != null) {
// don't register mirrors
@@ -165,10 +165,12 @@ private PlatformEventType findMirrorType(Class<? extends jdk.internal.event.Even
return et;
}

private EventHandler getHandler(Class<? extends jdk.internal.event.Event> eventClass) {
private EventHandler getHandler(Class<? extends jdk.internal.event.Event> eventClass, boolean ensureInitialized) {
Utils.ensureValidEventSubclass(eventClass);
SecuritySupport.makeVisibleToJFR(eventClass);
Utils.ensureInitialized(eventClass);
if (ensureInitialized) {
Utils.ensureInitialized(eventClass);
}
return Utils.getHandler(eventClass);
}

@@ -432,9 +432,22 @@ static void setAccessible(Constructor<?> constructor) {

static void ensureClassIsInitialized(Class<?> clazz) {
try {
MethodHandles.privateLookupIn(clazz, LOOKUP).ensureInitialized(clazz);
MethodHandles.Lookup lookup;
if (System.getSecurityManager() == null) {
lookup = MethodHandles.privateLookupIn(clazz, LOOKUP);
} else {
lookup = AccessController.doPrivileged(new PrivilegedExceptionAction<>() {
@Override
public MethodHandles.Lookup run() throws IllegalAccessException {
return MethodHandles.privateLookupIn(clazz, LOOKUP);
}
}, null, new ReflectPermission("suppressAccessChecks"));
}
lookup.ensureInitialized(clazz);
} catch (IllegalAccessException e) {
throw new InternalError(e);
} catch (PrivilegedActionException e) {
throw new InternalError(e.getCause());
}
}

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -30,40 +30,48 @@
*/

import java.io.*;
import java.lang.reflect.Method;
import java.nio.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import java.util.concurrent.*;
import java.util.zip.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class TestZipFile {

private static Random r = new Random();
private static int N = 50;
private static int NN = 10;
private static int ENUM = 10000;
private static int ESZ = 10000;
private static final Random r = new Random();
private static final int N = 50;
private static final int NN = 10;
private static final int ENUM = 10000;
private static final int ESZ = 10000;
private static ExecutorService executor = Executors.newFixedThreadPool(20);
private static Set<Path> paths = new HashSet<>();
private static final Set<Path> paths = new HashSet<>();
private static final boolean isWindows = System.getProperty("os.name")
.startsWith("Windows");

static void realMain (String[] args) throws Throwable {

try {
for (int i = 0; i < N; i++) {
test(r.nextInt(ENUM), r.nextInt(ESZ), false, true);
test(r.nextInt(ENUM), r.nextInt(ESZ), true, true);
}

executor.shutdown();
executor.awaitTermination(10, TimeUnit.MINUTES);
executor = Executors.newFixedThreadPool(20);
for (int i = 0; i < NN; i++) {
test(r.nextInt(ENUM), 100000 + r.nextInt(ESZ), false, true);
test(r.nextInt(ENUM), 100000 + r.nextInt(ESZ), true, true);
testCachedDelete();
testCachedOverwrite();
//test(r.nextInt(ENUM), r.nextInt(ESZ), false, true);
if(!isWindows) {
testCachedDelete();
testCachedOverwrite();
}
}

test(70000, 1000, false, true); // > 65536 entry number;
testDelete(); // OPEN_DELETE

@@ -77,7 +85,7 @@ static void realMain (String[] args) throws Throwable {
}

static void test(int numEntry, int szMax, boolean addPrefix, boolean cleanOld) {
String name = "zftest" + r.nextInt() + ".zip";
String name = "test-" + r.nextInt() + ".zip";
Zip zip = new Zip(name, numEntry, szMax, addPrefix, cleanOld);
for (int i = 0; i < NN; i++) {
executor.submit(() -> doTest(zip));
@@ -89,7 +97,7 @@ static void test(int numEntry, int szMax, boolean addPrefix, boolean cleanOld) {
// (2) test the ZipFile works correctly
// (3) check the zip is deleted after ZipFile gets closed
static void testDelete() throws Throwable {
String name = "zftest" + r.nextInt() + ".zip";
String name = "testDelete-" + r.nextInt() + ".zip";
Zip zip = new Zip(name, r.nextInt(ENUM), r.nextInt(ESZ), false, true);
try (ZipFile zf = new ZipFile(new File(zip.name),
ZipFile.OPEN_READ | ZipFile.OPEN_DELETE ))
@@ -108,7 +116,7 @@ static void testDelete() throws Throwable {
// (3) zip1 tests should fail, but no crash
// (4) zip2 tasks should all get zip2, then pass normal testing.
static void testCachedDelete() throws Throwable {
String name = "zftest" + r.nextInt() + ".zip";
String name = "testCachedDelete-" + r.nextInt() + ".zip";
Zip zip1 = new Zip(name, r.nextInt(ENUM), r.nextInt(ESZ), false, true);

try (ZipFile zf = new ZipFile(zip1.name)) {
@@ -135,7 +143,7 @@ static void testCachedDelete() throws Throwable {
// overwrite the "zip1" and create a new one to test. So the two zip files
// have the same fileKey, but probably different lastModified()
static void testCachedOverwrite() throws Throwable {
String name = "zftest" + r.nextInt() + ".zip";
String name = "testCachedOverWrite-" + r.nextInt() + ".zip";
Zip zip1 = new Zip(name, r.nextInt(ENUM), r.nextInt(ESZ), false, true);
try (ZipFile zf = new ZipFile(zip1.name)) {
for (int i = 0; i < NN; i++) {
@@ -153,8 +161,8 @@ static void testCachedOverwrite() throws Throwable {
// or deleted/rewritten, we only care if it crahes or not.
static void verifyNoCrash(Zip zip) throws RuntimeException {
try (ZipFile zf = new ZipFile(zip.name)) {
List<ZipEntry> zlist = new ArrayList(zip.entries.keySet());
String[] elist = zf.stream().map( e -> e.getName()).toArray(String[]::new);
List<ZipEntry> zlist = new ArrayList<>(zip.entries.keySet());
String[] elist = zf.stream().map(e -> e.getName()).toArray(String[]::new);
if (!Arrays.equals(elist,
zlist.stream().map( e -> e.getName()).toArray(String[]::new)))
{
@@ -219,12 +227,12 @@ static void doTest(Zip zip) throws RuntimeException {
static void doTest0(Zip zip, ZipFile zf) throws Throwable {
// (0) check zero-length entry name, no AIOOBE
try {
check(zf.getEntry("") == null);;
check(zf.getEntry("") == null);
} catch (Throwable t) {
unexpected(t);
}

List<ZipEntry> list = new ArrayList(zip.entries.keySet());
List<ZipEntry> list = new ArrayList<>(zip.entries.keySet());
// (1) check entry list, in expected order
if (!check(Arrays.equals(
list.stream().map( e -> e.getName()).toArray(String[]::new),
@@ -256,29 +264,11 @@ static void doTest0(Zip zip, ZipFile zf) throws Throwable {
}
}
}
// (3) check getMetaInfEntryNames
String[] metas = list.stream()
.map( e -> e.getName())
.filter( s -> s.startsWith("META-INF/"))
.sorted()
.toArray(String[]::new);
if (metas.length > 0) {
// meta-inf entries
Method getMetas = ZipFile.class.getDeclaredMethod("getMetaInfEntryNames");
getMetas.setAccessible(true);
String[] names = (String[])getMetas.invoke(zf);
if (names == null) {
fail("Failed to get metanames from " + zf);
} else {
Arrays.sort(names);
check(Arrays.equals(names, metas));
}
}
}

private static class Zip {
String name;
Map<ZipEntry, byte[]> entries;
final String name;
final Map<ZipEntry, byte[]> entries;
BasicFileAttributes attrs;
long lastModified;

@@ -292,7 +282,7 @@ private static class Zip {
}
paths.add(p);
} catch (Exception x) {
throw (RuntimeException)x;
throw new RuntimeException(x);
}

try (FileOutputStream fos = new FileOutputStream(name);
@@ -309,7 +299,7 @@ private static class Zip {
String ename = "entry-" + i + "-name-" + r.nextLong();
ZipEntry ze = new ZipEntry(ename);
int method = r.nextBoolean() ? ZipEntry.STORED : ZipEntry.DEFLATED;
writeEntry(zos, crc, ze, ZipEntry.STORED, szMax);
writeEntry(zos, crc, ze, method, szMax);
}
// add some manifest entries
for (int i = 0; i < r.nextInt(20); i++) {
@@ -318,13 +308,13 @@ private static class Zip {
writeEntry(zos, crc, ze, ZipEntry.STORED, szMax);
}
} catch (Exception x) {
throw (RuntimeException)x;
throw new RuntimeException(x);
}
try {
this.attrs = Files.readAttributes(Paths.get(name), BasicFileAttributes.class);
this.lastModified = new File(name).lastModified();
} catch (Exception x) {
throw (RuntimeException)x;
throw new RuntimeException(x);
}
}

@@ -358,11 +348,10 @@ private void writeEntry(ZipOutputStream zos, CRC32 crc,
static void fail() {failed++; Thread.dumpStack();}
static void fail(String msg) {System.out.println(msg); fail();}
static void unexpected(Throwable t) {failed++; t.printStackTrace();}
static void unexpected(Throwable t, String msg) {
System.out.println(msg); failed++; t.printStackTrace();}

static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}

public static void main(String[] args) throws Throwable {
public static void main(String[] args) {
try {realMain(args);} catch (Throwable t) {unexpected(t);}
System.out.println("\nPassed = " + passed + " failed = " + failed);
if (failed > 0) throw new AssertionError("Some tests failed");}

0 comments on commit 15999a9

Please sign in to comment.