-
Notifications
You must be signed in to change notification settings - Fork 1k
Added unit test for DefineClassInstrumentation #12483
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 1 commit
Commits
Show all changes
3 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
107 changes: 107 additions & 0 deletions
107
...emetry/javaagent/instrumentation/internal/classloader/DefineClassInstrumentationTest.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,107 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.internal.classloader; | ||
|
|
||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.ArgumentMatchers.same; | ||
| import static org.mockito.Mockito.verify; | ||
|
|
||
| import io.opentelemetry.javaagent.bootstrap.DefineClassHelper; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.lang.reflect.Field; | ||
| import java.security.ProtectionDomain; | ||
| import org.apache.commons.compress.utils.IOUtils; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
| import org.mockito.Mockito; | ||
|
|
||
| class DefineClassInstrumentationTest { | ||
|
|
||
| public static class DefiningClassLoader extends ClassLoader { | ||
|
|
||
| public DefiningClassLoader() { | ||
| super(null); | ||
| } | ||
|
|
||
| // Suppressing warnings to force testing of deprecated method | ||
| @SuppressWarnings("deprecation") | ||
| public Class<?> doDefineClass(byte[] b, int off, int len) { | ||
| return defineClass(b, off, len); | ||
| } | ||
|
|
||
| public Class<?> doDefineClass(String name, byte[] b, int off, int len) { | ||
| return defineClass(name, b, off, len); | ||
| } | ||
|
|
||
| public Class<?> doDefineClass( | ||
| String name, byte[] b, int off, int len, ProtectionDomain protectionDomain) { | ||
| return defineClass(name, b, off, len, protectionDomain); | ||
| } | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(ints = {3, 4, 5}) | ||
| @SuppressWarnings("DirectInvocationOnMock") | ||
| void ensureDefineClassInstrumented(int argCount) throws IOException { | ||
| String className = Dummy.class.getName(); | ||
| String resource = className.replace('.', '/') + ".class"; | ||
| ByteArrayOutputStream byteCodeStream = new ByteArrayOutputStream(); | ||
| try (InputStream classfile = getClass().getResourceAsStream("/" + resource)) { | ||
| IOUtils.copy(classfile, byteCodeStream, 1024); | ||
| } | ||
| byte[] bytecode = byteCodeStream.toByteArray(); | ||
|
|
||
| DefiningClassLoader cl = new DefiningClassLoader(); | ||
|
|
||
| DefineClassHelper.Handler mockHandler = Mockito.mock(DefineClassHelper.Handler.class); | ||
| // We need to initialize mockHandler by invoking it early, otherwise this leads to problems | ||
| mockHandler.beforeDefineClass(cl, className, bytecode, 0, bytecode.length); | ||
| Mockito.reset(mockHandler); | ||
|
|
||
| DefineClassHelper.Handler originalHandler = forceSetDefineClassHelper(mockHandler); | ||
| String expectedClassName; | ||
| try { | ||
| switch (argCount) { | ||
| case 3: | ||
| expectedClassName = null; | ||
| cl.doDefineClass(bytecode, 0, bytecode.length); | ||
| break; | ||
| case 4: | ||
| expectedClassName = className; | ||
| cl.doDefineClass(className, bytecode, 0, bytecode.length); | ||
| break; | ||
| case 5: | ||
| expectedClassName = className; | ||
| cl.doDefineClass(className, bytecode, 0, bytecode.length, null); | ||
| break; | ||
| default: | ||
| throw new IllegalStateException(); | ||
| } | ||
| } finally { | ||
| forceSetDefineClassHelper(originalHandler); | ||
| } | ||
|
|
||
| verify(mockHandler) | ||
| .beforeDefineClass( | ||
| same(cl), eq(expectedClassName), eq(bytecode), eq(0), eq(bytecode.length)); | ||
| verify(mockHandler).afterDefineClass(eq(null)); | ||
| } | ||
|
|
||
| private static DefineClassHelper.Handler forceSetDefineClassHelper( | ||
| DefineClassHelper.Handler newHandler) { | ||
| try { | ||
| Field handler = DefineClassHelper.class.getDeclaredField("handler"); | ||
| handler.setAccessible(true); | ||
| DefineClassHelper.Handler previous = (DefineClassHelper.Handler) handler.get(null); | ||
| handler.set(null, newHandler); | ||
| return previous; | ||
| } catch (Exception e) { | ||
| throw new IllegalStateException(e); | ||
| } | ||
| } | ||
| } | ||
8 changes: 8 additions & 0 deletions
8
.../src/test/java/io/opentelemetry/javaagent/instrumentation/internal/classloader/Dummy.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,8 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.internal.classloader; | ||
|
|
||
| class Dummy {} |
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.