-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathJarScannerTest.java
More file actions
80 lines (73 loc) · 3.48 KB
/
JarScannerTest.java
File metadata and controls
80 lines (73 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.datadog.debugger.symbol;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.security.cert.Certificate;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
class JarScannerTest {
@Test
public void extractJarPathFromJar()
throws ClassNotFoundException, URISyntaxException, MalformedURLException {
final String CLASS_NAME = "com.datadog.debugger.symbol.SymbolExtraction01";
URL jarFileUrl = getClass().getResource("/debugger-symbol.jar");
URL jarUrl = new URL("jar:file:" + jarFileUrl.getFile() + "!/");
URLClassLoader urlClassLoader = new URLClassLoader(new URL[] {jarUrl}, null);
Class<?> testClass = urlClassLoader.loadClass(CLASS_NAME);
assertEquals(
jarFileUrl.getFile(), JarScanner.extractJarPath(testClass, SymDBReport.NO_OP).toString());
assertEquals(
jarFileUrl.getFile(),
JarScanner.extractJarPath(testClass.getProtectionDomain(), null).toString());
}
@Test
public void extractJarPathFromFile() throws ClassNotFoundException, URISyntaxException {
final String CLASS_NAME = "com.datadog.debugger.symbol.SymbolExtraction01";
URL jarFileUrl = getClass().getResource("/debugger-symbol.jar");
URLClassLoader urlClassLoader = new URLClassLoader(new URL[] {jarFileUrl}, null);
Class<?> testClass = urlClassLoader.loadClass(CLASS_NAME);
assertEquals(
jarFileUrl.getFile(), JarScanner.extractJarPath(testClass, SymDBReport.NO_OP).toString());
}
@Test
public void extractJarPathFromNestedJar() throws URISyntaxException {
URL jarFileUrl = getClass().getResource("/debugger-symbol.jar");
URL mockLocation = mock(URL.class);
when(mockLocation.toString())
.thenReturn("jar:nested:" + jarFileUrl.getFile() + "/!BOOT-INF/classes/!");
CodeSource codeSource = new CodeSource(mockLocation, (Certificate[]) null);
ProtectionDomain protectionDomain = new ProtectionDomain(codeSource, null);
assertEquals(
jarFileUrl.getFile(), JarScanner.extractJarPath(protectionDomain, null).toString());
}
@Test
@EnabledOnOs(OS.WINDOWS)
public void extractJarPathFromFileOnWindows() throws URISyntaxException {
URL mockLocation = mock(URL.class);
when(mockLocation.toString()).thenReturn("file:/C:/apps/server/classes/");
CodeSource codeSource = new CodeSource(mockLocation, (Certificate[]) null);
ProtectionDomain protectionDomain = new ProtectionDomain(codeSource, null);
Path result = JarScanner.extractJarPath(protectionDomain, SymDBReport.NO_OP);
assertNotNull(result);
assertTrue(result.toString().contains("server"));
}
@Test
@EnabledOnOs(OS.WINDOWS)
public void extractJarPathFromJarOnWindows() throws URISyntaxException {
URL mockLocation = mock(URL.class);
when(mockLocation.toString()).thenReturn("jar:file:/C:/libs/app.jar!/com/example/");
CodeSource codeSource = new CodeSource(mockLocation, (Certificate[]) null);
ProtectionDomain protectionDomain = new ProtectionDomain(codeSource, null);
Path result = JarScanner.extractJarPath(protectionDomain, SymDBReport.NO_OP);
assertNotNull(result);
assertTrue(result.toString().contains("app.jar"));
}
}