Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,42 +1,29 @@
package datadog.trace.instrumentation.java.io

import datadog.trace.agent.test.InstrumentationSpecification
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.Shared
import spock.lang.TempDir

abstract class BaseIoCallSiteTest extends InstrumentationSpecification {
import java.nio.file.Files
import java.nio.file.Path

@Rule
TemporaryFolder temporaryFolder = new TemporaryFolder(parentFolder())
abstract class BaseIoCallSiteTest extends InstrumentationSpecification {
@Shared
@TempDir
Path temporaryFolder

@Override
protected void configurePreAgent() {
injectSysConfig("dd.iast.enabled", "true")
}

protected File newFile(final String name) {
return temporaryFolder.newFile(name)
Path p = temporaryFolder.resolve(name)
Files.createDirectories(p.getParent())
return Files.createFile(p).toFile()
}

protected File getRootFolder() {
protected Path getRootFolder() {
return temporaryFolder.getRoot()
}

/**
* We cannot use @TempDir from spock due to dependencies, this method tries to write to the build folder to prevent
* permissions with /tmp
*/
private static File parentFolder() {
def folder = new File(BaseIoCallSiteTest.getResource('.').toURI())
while (folder.name != 'build') {
folder = folder.parentFile
}
folder = new File(folder, 'tmp')
if (!folder.exists()) {
if (!folder.mkdirs()) {
throw new RuntimeException('Cannot create folder ' + folder)
}
}
return folder
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class FileInputStreamCallSiteTest extends BaseIoRaspCallSiteTest {
setup:
PathTraversalModule iastModule = Mock(PathTraversalModule)
InstrumentationBridge.registerIastModule(iastModule)
final path = newFile('test.txt').toString()
final path = newFile('test_iast.txt').toString()

when:
TestFileInputStreamSuite.newFileInputStream(path)
Expand All @@ -24,7 +24,7 @@ class FileInputStreamCallSiteTest extends BaseIoRaspCallSiteTest {
setup:
final helper = Mock(FileLoadedRaspHelper)
FileLoadedRaspHelper.INSTANCE = helper
final path = newFile('test.txt').toString()
final path = newFile('test_rasp.txt').toString()

when:
TestFileInputStreamSuite.newFileInputStream(path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class FileOutputStreamCallSiteTest extends BaseIoRaspCallSiteTest {
setup:
PathTraversalModule iastModule = Mock(PathTraversalModule)
InstrumentationBridge.registerIastModule(iastModule)
final path = newFile('test.txt').toString()
final path = newFile('test_iast_1.txt').toString()

when:
TestFileOutputStreamSuite.newFileOutputStream(path)
Expand All @@ -26,7 +26,7 @@ class FileOutputStreamCallSiteTest extends BaseIoRaspCallSiteTest {
setup:
PathTraversalModule iastModule = Mock(PathTraversalModule)
InstrumentationBridge.registerIastModule(iastModule)
final path = newFile('test.txt').toString()
final path = newFile('test_iast_2.txt').toString()

when:
TestFileOutputStreamSuite.newFileOutputStream(path, false)
Expand All @@ -39,7 +39,7 @@ class FileOutputStreamCallSiteTest extends BaseIoRaspCallSiteTest {
setup:
final helper = Mock(FileLoadedRaspHelper)
FileLoadedRaspHelper.INSTANCE = helper
final path = newFile('test.txt').toString()
final path = newFile('test_rasp_1.txt').toString()

when:
TestFileOutputStreamSuite.newFileOutputStream(path)
Expand All @@ -52,7 +52,7 @@ class FileOutputStreamCallSiteTest extends BaseIoRaspCallSiteTest {
setup:
final helper = Mock(FileLoadedRaspHelper)
FileLoadedRaspHelper.INSTANCE = helper
final path = newFile('test.txt').toString()
final path = newFile('test_rasp_2.txt').toString()

when:
TestFileOutputStreamSuite.newFileOutputStream(path, false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class PathCallSiteTest extends BaseIoRaspCallSiteTest {
setup:
PathTraversalModule iastModule = Mock(PathTraversalModule)
InstrumentationBridge.registerIastModule(iastModule)
final path = 'test.txt'
final path = 'test_iast.txt'

when:
TestPathSuite.resolve(getRootFolder().toPath(), path)
TestPathSuite.resolve(getRootFolder(), path)

then:
1 * iastModule.onPathTraversal(path)
Expand All @@ -24,8 +24,8 @@ class PathCallSiteTest extends BaseIoRaspCallSiteTest {
setup:
PathTraversalModule iastModule = Mock(PathTraversalModule)
InstrumentationBridge.registerIastModule(iastModule)
final sibling = newFile('test1.txt').toPath()
final path = 'test2.txt'
final sibling = newFile('test_iast_1.txt').toPath()
final path = 'test_iast_2.txt'

when:
TestPathSuite.resolveSibling(sibling, path)
Expand All @@ -38,10 +38,10 @@ class PathCallSiteTest extends BaseIoRaspCallSiteTest {
setup:
final helper = Mock(FileLoadedRaspHelper)
FileLoadedRaspHelper.INSTANCE = helper
final path = 'test.txt'
final path = 'test_rasp.txt'

when:
TestPathSuite.resolve(getRootFolder().toPath(), path)
TestPathSuite.resolve(getRootFolder(), path)

then:
1 * helper.beforeFileLoaded(path)
Expand All @@ -51,8 +51,8 @@ class PathCallSiteTest extends BaseIoRaspCallSiteTest {
setup:
final helper = Mock(FileLoadedRaspHelper)
FileLoadedRaspHelper.INSTANCE = helper
final sibling = newFile('test1.txt').toPath()
final path = 'test2.txt'
final sibling = newFile('test_rasp_1.txt').toPath()
final path = 'test_rasp_2.txt'

when:
TestPathSuite.resolveSibling(sibling, path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.File;
import java.io.PrintStream;
import java.nio.file.Path;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
Expand All @@ -12,16 +13,15 @@
import org.apache.maven.eventspy.EventSpy;
import org.apache.maven.execution.ExecutionEvent;
import org.codehaus.plexus.PlexusContainer;
import org.junit.ClassRule;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.io.TempDir;

public abstract class AbstractMavenTest {

@ClassRule public static TemporaryFolder WORKING_DIRECTORY = new TemporaryFolder();
@TempDir static Path WORKING_DIRECTORY;

protected AbstractMavenTest() {
System.setProperty(
"maven.multiModuleProjectDirectory", WORKING_DIRECTORY.getRoot().getAbsolutePath());
"maven.multiModuleProjectDirectory",
WORKING_DIRECTORY.getRoot().toAbsolutePath().toString());
}

protected void executeMaven(
Expand Down Expand Up @@ -58,7 +58,8 @@ protected void customizeContainer(PlexusContainer container) {
arguments[2] = goal;
System.arraycopy(additionalArgs, 0, arguments, 3, additionalArgs.length);

mavenCli.doMain(arguments, WORKING_DIRECTORY.getRoot().getAbsolutePath(), stdOut, stderr);
mavenCli.doMain(
arguments, WORKING_DIRECTORY.getRoot().toAbsolutePath().toString(), stdOut, stderr);

Exception error = spy.handlerError.get();
if (error != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ private static File createToolchainsFile()
Map<String, String> replacements =
Collections.singletonMap("my_jdk_home_path", toolchainJdkHome.getAbsolutePath());

File toolchainsFile = new File(WORKING_DIRECTORY.getRoot(), "toolchains.xml");
File toolchainsFile = WORKING_DIRECTORY.getRoot().resolve("toolchains.xml").toFile();
try (FileWriter toolchainsFileWriter = new FileWriter(toolchainsFile)) {
Template coveragesTemplate = FREEMARKER.getTemplate("sampleToolchains.ftl");
coveragesTemplate.process(replacements, toolchainsFileWriter);
Expand Down