Skip to content

Commit

Permalink
Improve testing unit
Browse files Browse the repository at this point in the history
  • Loading branch information
Karlatemp committed Jun 6, 2021
1 parent a2705b8 commit 9a98b8c
Show file tree
Hide file tree
Showing 10 changed files with 536 additions and 162 deletions.
6 changes: 4 additions & 2 deletions impl/testunit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def registerTestUnitWith = { vmpath ->
def type = new File(vmpath).getName()
def unitx = "runTestUnit" + type.capitalize()
def unitw = unitx
while (tasks.findByName(unitx) != null ) {
while (tasks.findByName(unitx) != null) {
unitw = unitx + unitCounter
unitCounter ++
unitCounter++
}
return unitw
}
Expand All @@ -57,6 +57,8 @@ def registerTestUnitWith = { vmpath ->

exec.classpath = sourceSets.jdk9.runtimeClasspath + project(":impl.loader").tasks.getByPath("jar").outputs.files
main = 'runtest.RunTestUnit'
exec.environment('taskUnitName', exec.name)
exec.environment('writeToFile', rootProject.hasProperty("writeToFile"))
if (vmpath != null) {
exec.executable(vmpath + "/bin/java")
exec.doFirst {
Expand Down
12 changes: 0 additions & 12 deletions impl/testunit/src/jdk9/java/runtest/J9TestUnit.java

This file was deleted.

9 changes: 4 additions & 5 deletions impl/testunit/src/jdk9/java/runtest/MAT.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
import java.util.UUID;

public class MAT {
@TestTask
public static void run() {
System.out.println("==== Module Access Test ====");
ModuleAccess access = Root.getModuleAccess();
Unsafe usf = Unsafe.getUnsafe();


Assertions.assertTrue(access.isSupport());
Assertions.assertNotNull(access.getModule(ModuleAccess.class));
Assertions.assertNotNull(access.getModule(ModuleAccessTest.class));
ClassLoader cl = ModuleAccessTest.class.getClassLoader();
Assertions.assertNotNull(access.getModule(MAT.class));
ClassLoader cl = MAT.class.getClassLoader();
{

String ppk = "io.kjg.ppkv." + UUID.randomUUID();
Expand All @@ -33,7 +33,7 @@ public static void run() {

Object unnamedModule = access.defineModule(cl, moduleBuilder.build(), null);
System.out.println("UN: " + unnamedModule);
Assertions.assertNotSame(unnamedModule, access.getModule(ModuleAccessTest.class));
Assertions.assertNotSame(unnamedModule, access.getModule(MAT.class));
ClassWriter cw = new ClassWriter(0);
cw.visit(Opcodes.V1_8, 0, (ppk + ".RX").replace('.', '/'), null, "java/lang/Object", null);
byte[] b = cw.toByteArray();
Expand All @@ -42,6 +42,5 @@ public static void run() {
Assertions.assertSame(access.getModule(c), unnamedModule);
}

System.out.println("============================");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.*;
import runtest.TestTask;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandleInfo;
Expand Down Expand Up @@ -40,6 +41,9 @@ public static boolean hasAnnotation(MethodNode node, Class<?> type) {
return hasAnnotation(node, type.getName().replace('.', '/'));
}

@TestTask(
hiddenOutput = true
)
public static void run() throws Throwable {
System.out.println("Analysing Unsafe BinaryCompatibility");
List<MethodNode> methods = new ArrayList<>();
Expand Down
14 changes: 0 additions & 14 deletions impl/testunit/src/main/java/runtest/ModuleAccessTest.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ private static void privateMet() {

private static final Object finalField = null;

@TestTask
public static void run() throws Throwable {
MethodHandles.Lookup trusted = Root.getTrusted(RootMethodHandleLookupTest.class);
System.out.println("Access: " + trusted.lookupModes());
Expand Down
136 changes: 7 additions & 129 deletions impl/testunit/src/main/java/runtest/RunTestUnit.java
Original file line number Diff line number Diff line change
@@ -1,136 +1,14 @@
package runtest;

import io.github.karlatemp.unsafeaccessor.BinaryCompatibilityAnalysis;
import io.github.karlatemp.unsafeaccessor.Root;
import io.github.karlatemp.unsafeaccessor.Unsafe;
import org.junit.jupiter.api.Assertions;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;

import java.lang.invoke.MethodHandle;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;

public class RunTestUnit {
@SuppressWarnings("deprecation")
public static void main(String[] args) throws Throwable {
Unsafe usf = Unsafe.getUnsafe();
System.out.println("Usf = " + usf);
System.out.println(Unsafe.class.getResource("Unsafe.class"));
System.out.println(Unsafe.getUnsafe());

// region ECHO
RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean();
System.out.println("== SPEC ==");
System.out.println(rt.getSpecName());
System.out.println(rt.getSpecVendor());
System.out.println(rt.getSpecVersion());
System.out.println("== VM ==");
System.out.println(rt.getVmName());
System.out.println(rt.getVmVendor());
System.out.println(rt.getVmVersion());
System.out.println("=========");
// endregion

// region check Root.getTrusted()
trustCheck:
{
String toString = Root.getTrusted().toString();
if (toString.equals("/trusted")) {
break trustCheck;
}

// AdoptOpenJDK - jdk 11.0.9.11 openj9
if (Root.getTrusted().lookupClass() == MethodHandle.class) {
if (Root.getTrusted().lookupModes() == 0x40) {
if (toString.equals("java.lang.invoke.MethodHandle")) {
break trustCheck;
}
}
}

Assertions.assertEquals("/trusted", Root.getTrusted().toString());
}
RootMethodHandleLookupTest.run();
// endregion


System.out.println("IsJava9 = " + Unsafe.getUnsafe().isJava9());

// region check set accessible
{
System.out.println("Checking AccessibleObject.setAccessible");
Method addURL = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
System.out.println(addURL.isAccessible());
Assertions.assertFalse(addURL.isAccessible());
System.out.println(Root.openAccess(addURL));
System.out.println(addURL.isAccessible());
Assertions.assertTrue(addURL.isAccessible());
}
// endregion
public static String taskUnitName = System.getenv("taskUnitName");

// region check Unsafe.defineClass
{
System.out.println("Checking class defining");
ClassWriter writer1 = new ClassWriter(0),
writer2 = new ClassWriter(0),
writer3 = new ClassWriter(0),
writer4 = new ClassWriter(0);
writer1.visit(Opcodes.V1_8, 0, "testSwe/WEASawx", null, "java/lang/Object", null);
writer2.visit(Opcodes.V1_8, 0, "testSwe/WEAS687", null, "java/lang/Object", null);
writer3.visit(Opcodes.V1_8, 0, "testSwe/AAXWXu", null, "java/lang/Object", null);
writer4.visit(Opcodes.V1_8, 0, "testSwe/AWZXaex", null, "java/lang/Object", null);

byte[] code1 = writer1.toByteArray();
byte[] code2 = writer2.toByteArray();
byte[] code3 = writer3.toByteArray();
byte[] code4 = writer4.toByteArray();

ClassLoader loader = RunTestUnit.class.getClassLoader();

Class<?> class1 = Unsafe.getUnsafe().defineClass(null, code1, 0, code1.length, loader, null);
Class<?> class2 = Unsafe.getUnsafe().defineClass0(null, code2, 0, code2.length, loader, null);
Class<?> class3 = Unsafe.getUnsafe().defineClass0(null, code3, 0, code3.length, null, null);
Class<?> class4 = Unsafe.getUnsafe().defineAnonymousClass(class1, code4, null);

Assertions.assertEquals("testSwe.WEASawx", class1.getName());
Assertions.assertEquals("testSwe.WEAS687", class2.getName());
Assertions.assertEquals("testSwe.AAXWXu", class3.getName());
System.out.println("AnonymousClass: " + class4);
Assertions.assertTrue(class4.getName().contains("/"), class4.getName());

Assertions.assertSame(loader, class1.getClassLoader());
Assertions.assertSame(loader, class2.getClassLoader());
Assertions.assertSame(null, class3.getClassLoader());
Assertions.assertSame(loader, class4.getClassLoader());
}
// endregion

TestUnsafe.runTest();
ModuleAccessTest.run();

BinaryCompatibilityAnalysis.run();

{
Class<?> c = null;
try {
c = Class.forName("runtest.J9TestUnit");
} catch (ClassFormatError ignore) {
}
if (c != null) {
c.getDeclaredMethod("invoke").invoke(null);
}
}
{
try {
Class.forName("runtest.loct.Loct")
.getDeclaredMethod("invoke")
.invoke(null);
} catch (ClassNotFoundException ignore) {
}
}
public static void main(String[] args) throws Throwable {
List<String> classes = new ArrayList<>(TestTasks.findTestClasses());
classes.sort(String::compareTo);
TestTasks.runTests(classes);
}
}
14 changes: 14 additions & 0 deletions impl/testunit/src/main/java/runtest/TestTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package runtest;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface TestTask {
boolean hiddenOutput() default false;

String name() default "";
}
Loading

0 comments on commit 9a98b8c

Please sign in to comment.