Skip to content

Commit

Permalink
[1.6.1] Fixed JavaAgent and added unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
3arthqu4ke committed Jan 12, 2023
1 parent 4163e45 commit 1417136
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
22 changes: 19 additions & 3 deletions headlessmc-lwjgl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,28 @@ jar {
archivesBaseName = 'headlessmc-lwjgl'
//noinspection GroovyAssignabilityCheck
manifest {
// TODO: test tweaker?
attributes 'TweakClass': 'me.earth.headlessmc.lwjgl.launchwrapper.LwjglTweaker'
// TODO: test java agent
attributes 'Premain-Class': 'me.earth.headlessmc.lwjgl.agent.LwjglAgent'
attributes 'Agent-Class': 'me.earth.headlessmc.lwjgl.agent.LwjglAgent'
attributes 'Can-Redefine-Classes': true
attributes 'Can-Retransform-Classes': true
}
}
}

task testAgent(type: Test, dependsOn: jar) {
group = 'verification'
useJUnitPlatform()
testLogging {
events "failed"
exceptionFormat "full"
}

jvmArgs = [
"-javaagent:" + projectDir + "/build/libs/headlessmc-lwjgl.jar",
'-Dhmc.lwjgl.agenttest=true'
]

filter {
includeTestsMatching 'me.earth.headlessmc.lwjgl.agent.AgentTest'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public byte[] transform(ClassLoader loader, String className,
ProtectionDomain protectionDomain,
byte[] classfileBuffer)
throws IllegalClassFormatException {
if (className.contains("lwjgl")) {
if (className != null && className.startsWith("org/lwjgl")) {
ClassNode node = AsmUtil.read(classfileBuffer);
transformer.transform(node);
// TODO: make writer.getClassLoader() return the given loader?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package me.earth.headlessmc.lwjgl.agent;

import me.earth.headlessmc.lwjgl.testlaunchwrapper.LaunchWrapperTarget;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.lwjgl.AbstractLwjglClass;

import java.lang.reflect.Modifier;

import static org.junit.jupiter.api.Assertions.assertFalse;

public class AgentTest {
@Test
public void testWithAgent() {
Assumptions.assumeTrue(Boolean.parseBoolean(
System.getProperty("hmc.lwjgl.agenttest")));

LaunchWrapperTarget.testLwjglClasses();
assertFalse(Modifier.isAbstract(
AbstractLwjglClass.class.getModifiers()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public static void main(String[] args) {
assertEquals(Launch.classLoader,
LwjglInterface.class.getClassLoader());

testLwjglClasses();
System.setProperty(LaunchWrapperTest.PASSED, "true");
}

public static void testLwjglClasses() {
val lwjgl = Lwjgl.factoryMethod("test");
assertNotNull(lwjgl);
LwjglInstrumentationTest.testRedirections(lwjgl, lwjgl.getClass());
Expand All @@ -39,8 +44,6 @@ public static void main(String[] args) {
val iLwjgl = LwjglInterface.factoryMethod("test");
assertNotNull(iLwjgl);
LwjglInstrumentationTest.testRedirections(iLwjgl, LwjglInterface.class);

System.setProperty(LaunchWrapperTest.PASSED, "true");
}

}

0 comments on commit 1417136

Please sign in to comment.