Repository navigation
Add functional class injection tests using startup instrumentation #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| plugins { | ||
| id("java-common") | ||
| } | ||
|
|
||
| val testAgent = configurations.create("testAgent") { | ||
| isCanBeConsumed = false | ||
| isTransitive = false | ||
| } | ||
|
|
||
| dependencies { | ||
| testImplementation(project(":testing")) | ||
| testAgent(project(":testing")) | ||
| } | ||
|
|
||
| class TestAgentArguments(@get:Classpath val agentClasspath: FileCollection) : CommandLineArgumentProvider { | ||
| override fun asArguments() = listOf("-javaagent:${agentClasspath.singleFile.absolutePath}") | ||
| } | ||
|
|
||
| // Supply instrumentation to every test JVM, including the additional Java version tasks. | ||
| tasks.withType<Test>().configureEach { | ||
| jvmArgumentProviders.add(TestAgentArguments(testAgent)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| plugins { | ||
| id("java-common") | ||
| id("instrument-glue") | ||
| id("instrument-testing") | ||
| } | ||
|
|
||
| dependencies { | ||
|
|
||
139 changes: 139 additions & 0 deletions
139
class-inject/src/test/java/datadog/instrument/classinject/ClassInjectorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| package datadog.instrument.classinject; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.objectweb.asm.Opcodes.*; | ||
|
|
||
| import datadog.instrument.testing.TestAgent; | ||
| import java.security.ProtectionDomain; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.objectweb.asm.ClassWriter; | ||
| import org.objectweb.asm.MethodVisitor; | ||
|
|
||
| class ClassInjectorTest { | ||
|
|
||
| @BeforeAll | ||
|
mcculls marked this conversation as resolved.
|
||
| static void enableClassInjection() { | ||
| ClassInjector.enableClassInjection(TestAgent.getInstrumentation()); | ||
| } | ||
|
|
||
| @Test | ||
| void injectsBootClass() throws Exception { | ||
| String name = "datadog.injected.BootSample"; | ||
| Class<?> injectedClass = ClassInjector.injectBootClass(name, sampleClass(name)); | ||
| assertThat(injectedClass.getClassLoader()).isNull(); | ||
| assertThat(Class.forName(name, false, null)).isSameAs(injectedClass); | ||
| assertThat(injectedClass.getMethod("value").invoke(null)).isEqualTo(42); | ||
|
|
||
| // injecting an already defined class returns the existing class | ||
| assertThat(ClassInjector.injectBootClass(name, sampleClass(name))).isSameAs(injectedClass); | ||
| } | ||
|
|
||
| @Test | ||
| void injectsClassIntoGivenLoader() throws Exception { | ||
| ClassLoader loader = newCL(); | ||
| String name = "datadog.injected.LoaderSample"; | ||
| Class<?> injectedClass = ClassInjector.injectClass(name, sampleClass(name), loader); | ||
| assertThat(injectedClass.getClassLoader()).isSameAs(loader); | ||
| assertThat(loader.loadClass(name)).isSameAs(injectedClass); | ||
| assertThat(injectedClass.getMethod("value").invoke(null)).isEqualTo(42); | ||
|
|
||
| // injecting an already defined class returns the existing class | ||
| assertThat(ClassInjector.injectClass(name, sampleClass(name), loader)).isSameAs(injectedClass); | ||
| } | ||
|
|
||
| @Test | ||
| void injectsClassWithGivenProtectionDomain() throws Exception { | ||
|
mcculls marked this conversation as resolved.
|
||
| ClassLoader loader = newCL(); | ||
| ProtectionDomain domain = new ProtectionDomain(null, null, loader, null); | ||
| String name = "datadog.injected.DomainSample"; | ||
| Class<?> injectedClass = ClassInjector.injectClass(name, sampleClass(name), domain); | ||
| assertThat(injectedClass.getClassLoader()).isSameAs(loader); | ||
| assertThat(injectedClass.getProtectionDomain()).isSameAs(domain); | ||
| assertThat(injectedClass.getMethod("value").invoke(null)).isEqualTo(42); | ||
|
|
||
| assertThat(ClassInjector.injectClass(name, sampleClass(name), domain)).isSameAs(injectedClass); | ||
| } | ||
|
|
||
| @Test | ||
| void injectsBootClassWithGivenProtectionDomain() throws Exception { | ||
| ProtectionDomain domain = new ProtectionDomain(null, null, null, null); | ||
| String name = "datadog.injected.BootDomainSample"; | ||
| Class<?> injectedClass = ClassInjector.injectClass(name, sampleClass(name), domain); | ||
| assertThat(injectedClass.getClassLoader()).isNull(); | ||
| assertThat(Class.forName(name, false, null)).isSameAs(injectedClass); | ||
| assertThat(injectedClass.getProtectionDomain()).isSameAs(domain); | ||
| assertThat(injectedClass.getMethod("value").invoke(null)).isEqualTo(42); | ||
|
|
||
| assertThat(ClassInjector.injectClass(name, sampleClass(name), domain)).isSameAs(injectedClass); | ||
| } | ||
|
|
||
| @Test | ||
| void injectsBootClassesThatReferenceEachOther() throws Exception { | ||
| String caller = "datadog.injected.BootCallerSample"; | ||
| String callee = "datadog.injected.BootCalleeSample"; | ||
| Map<String, byte[]> bytecode = new LinkedHashMap<>(); | ||
| bytecode.put(caller, delegatingClass(caller, callee)); | ||
| bytecode.put(callee, sampleClass(callee)); | ||
|
|
||
| List<Class<?>> injectedClasses = ClassInjector.injectBootClasses(bytecode); | ||
| assertThat(injectedClasses).extracting(Class::getName).containsExactly(caller, callee); | ||
| assertThat(injectedClasses).allSatisfy(c -> assertThat(c.getClassLoader()).isNull()); | ||
| assertThat(Class.forName(caller, false, null)).isSameAs(injectedClasses.get(0)); | ||
| assertThat(Class.forName(callee, false, null)).isSameAs(injectedClasses.get(1)); | ||
| assertThat(injectedClasses.get(0).getMethod("value").invoke(null)).isEqualTo(42); | ||
|
|
||
| assertThat(ClassInjector.injectBootClasses(bytecode)) | ||
| .containsExactlyElementsOf(injectedClasses); | ||
| } | ||
|
|
||
| @Test | ||
| void injectsClassesThatReferenceEachOther() throws Exception { | ||
| ClassLoader loader = newCL(); | ||
| String caller = "datadog.injected.CallerSample"; | ||
| String callee = "datadog.injected.CalleeSample"; | ||
| Map<String, byte[]> bytecode = new LinkedHashMap<>(); | ||
| bytecode.put(caller, delegatingClass(caller, callee)); | ||
| bytecode.put(callee, sampleClass(callee)); | ||
|
|
||
| List<Class<?>> injectedClasses = ClassInjector.injectClasses(bytecode, loader); | ||
| assertThat(injectedClasses).extracting(Class::getName).containsExactly(caller, callee); | ||
| assertThat(injectedClasses).allSatisfy(c -> assertThat(c.getClassLoader()).isSameAs(loader)); | ||
| assertThat(injectedClasses.get(0).getMethod("value").invoke(null)).isEqualTo(42); | ||
| } | ||
|
|
||
| private static ClassLoader newCL() { | ||
| return new ClassLoader(null) {}; | ||
| } | ||
|
|
||
| /** Generates a class with a static {@code value()} method that returns 42. */ | ||
| private static byte[] sampleClass(String name) { | ||
| ClassWriter writer = new ClassWriter(0); | ||
| writer.visit(V1_8, ACC_PUBLIC, name.replace('.', '/'), null, "java/lang/Object", null); | ||
| MethodVisitor method = writer.visitMethod(ACC_PUBLIC | ACC_STATIC, "value", "()I", null, null); | ||
| method.visitCode(); | ||
| method.visitIntInsn(BIPUSH, 42); | ||
| method.visitInsn(IRETURN); | ||
| method.visitMaxs(1, 0); | ||
| method.visitEnd(); | ||
| writer.visitEnd(); | ||
| return writer.toByteArray(); | ||
| } | ||
|
|
||
| /** Generates a class with a static {@code value()} method that delegates to the target. */ | ||
| private static byte[] delegatingClass(String name, String target) { | ||
| ClassWriter writer = new ClassWriter(0); | ||
| writer.visit(V1_8, ACC_PUBLIC, name.replace('.', '/'), null, "java/lang/Object", null); | ||
| MethodVisitor method = writer.visitMethod(ACC_PUBLIC | ACC_STATIC, "value", "()I", null, null); | ||
| method.visitCode(); | ||
| method.visitMethodInsn(INVOKESTATIC, target.replace('.', '/'), "value", "()I", false); | ||
| method.visitInsn(IRETURN); | ||
| method.visitMaxs(1, 0); | ||
| method.visitEnd(); | ||
| writer.visitEnd(); | ||
| return writer.toByteArray(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,12 @@ | ||
| plugins { | ||
| id("java-common") | ||
| } | ||
|
|
||
| // Allow test JVMs to obtain instrumentation without dynamic agent attachment. | ||
| tasks.jar { | ||
| manifest { | ||
| attributes( | ||
| "Premain-Class" to "datadog.instrument.testing.TestAgent", | ||
| "Can-Retransform-Classes" to "true") | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
testing/src/main/java/datadog/instrument/testing/TestAgent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
| * Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. | ||
| * This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| * Copyright 2026-Present Datadog, Inc. | ||
| */ | ||
|
|
||
| package datadog.instrument.testing; | ||
|
|
||
| import java.lang.instrument.Instrumentation; | ||
|
|
||
| /** Supplies instrumentation to tests when the testing jar is used as a startup agent. */ | ||
| public final class TestAgent { | ||
| private static volatile Instrumentation instrumentation; | ||
|
|
||
| private TestAgent() {} | ||
|
|
||
| /** Called by the JVM when the testing jar is supplied with {@code -javaagent}. */ | ||
| public static void premain(String arguments, Instrumentation inst) { | ||
| instrumentation = inst; | ||
| } | ||
|
|
||
| /** Returns the instrumentation supplied at JVM startup. */ | ||
| public static Instrumentation getInstrumentation() { | ||
| if (instrumentation == null) { | ||
| throw new IllegalStateException("The testing jar must be supplied with -javaagent"); | ||
| } | ||
| return instrumentation; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.