[ASTERIXDB-2004][TEST] Prevent Tests from writing outside target
- user model changes: no
- storage format changes: no
- interface changes: no
details:
- Some tests access data and queries outside the module. When that
happens, the base path can contain ../ which can lead to results
being written outside target. This change removes all ../ from
the path of the actual results.
Change-Id: If100c33780fa436ddb2a8e64f3901251156f5524
Reviewed-on: https://asterix-gerrit.ics.uci.edu/1905
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
BAD: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Till Westmann <tillw@apache.org>
diff --git a/asterixdb/asterix-test-framework/src/main/java/org/apache/asterix/testframework/context/TestCaseContext.java b/asterixdb/asterix-test-framework/src/main/java/org/apache/asterix/testframework/context/TestCaseContext.java
index ff914b5..7a34648 100644
--- a/asterixdb/asterix-test-framework/src/main/java/org/apache/asterix/testframework/context/TestCaseContext.java
+++ b/asterixdb/asterix-test-framework/src/main/java/org/apache/asterix/testframework/context/TestCaseContext.java
@@ -186,9 +186,20 @@
public File getActualResultFile(CompilationUnit cUnit, File expectedFile, File actualResultsBase) {
File path = actualResultsBase;
- path = new File(path, testSuite.getResultOffsetPath());
- path = new File(path, testCase.getFilePath());
- return new File(path, cUnit.getOutputDir().getValue() + File.separator + expectedFile.getName());
+ String resultOffsetPath = removeUpward(testSuite.getResultOffsetPath());
+ path = new File(path, resultOffsetPath);
+ String testCaseFilePath = removeUpward(testCase.getFilePath());
+ String expectedFilePath = removeUpward(expectedFile.getName());
+ path = new File(path, testCaseFilePath);
+ return new File(path, cUnit.getOutputDir().getValue() + File.separator + expectedFilePath);
+ }
+
+ private String removeUpward(String filePath) {
+ String evil = ".." + File.separatorChar;
+ while (filePath.contains(evil)) {
+ filePath = filePath.replace(evil, ""); // NOSONAR
+ }
+ return filePath;
}
@Override
@@ -224,8 +235,8 @@
File tsFile = new File(tsRoot, tsXMLFilePath);
TestSuiteParser tsp = new TestSuiteParser();
TestSuite ts = tsp.parse(tsFile);
- List<TestCaseContext> tccs = new ArrayList<TestCaseContext>();
- List<TestGroup> tgPath = new ArrayList<TestGroup>();
+ List<TestCaseContext> tccs = new ArrayList<>();
+ List<TestGroup> tgPath = new ArrayList<>();
addContexts(tsRoot, ts, tgPath, ts.getTestGroup(), tccs);
return tccs;
}