Skip to content

Commit a0c65d4

Browse files
ValidationTest temporary file handling update
Replacing OS-specific path references with a use of the TemporaryFolder junit rule.
1 parent cc0e4fd commit a0c65d4

File tree

1 file changed

+11
-36
lines changed

1 file changed

+11
-36
lines changed

src/test/java/org/owasp/esapi/reference/ValidatorTest.java

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import static org.junit.Assert.assertEquals;
1919
import static org.junit.Assert.assertFalse;
2020
import static org.junit.Assert.assertNull;
21-
import static org.junit.Assert.assertThrows;
2221
import static org.junit.Assert.assertTrue;
2322
import static org.junit.Assert.fail;
2423

@@ -36,7 +35,9 @@
3635

3736
import javax.servlet.http.Cookie;
3837

38+
import org.junit.Rule;
3939
import org.junit.Test;
40+
import org.junit.rules.TemporaryFolder;
4041
import org.owasp.esapi.ESAPI;
4142
import org.owasp.esapi.Encoder;
4243
import org.owasp.esapi.EncoderConstants;
@@ -63,6 +64,9 @@ public class ValidatorTest {
6364

6465
private static final String PREFERRED_ENCODING = "UTF-8";
6566

67+
@Rule
68+
public TemporaryFolder tempFolder = new TemporaryFolder();
69+
6670
@Test
6771
public void testAddRule() {
6872
Validator validator = ESAPI.validator();
@@ -388,24 +392,6 @@ public void testIsValidDirectoryPath() throws IOException {
388392
}
389393
}
390394

391-
private static void mkdir(String dirname) throws IOException {
392-
File file = new File( dirname );
393-
394-
if ( file.exists() && file.isDirectory() ) {
395-
return;
396-
} else if ( file.exists() ) {
397-
throw new IOException("Filename " + dirname + " already exists, but is not a directory.");
398-
}
399-
400-
file.deleteOnExit(); // Mark the directory that we create below to be deleted when the JVM exits.
401-
402-
boolean flag = file.mkdir();
403-
404-
if ( !flag ) throw new IOException("Failed to create directory: " + dirname);
405-
406-
return;
407-
}
408-
409395
// GitHub issue # xxxx - GHSL-2022-008
410396
@Test
411397
public void testIsValidDirectoryPathGHSL_POC() throws IOException {
@@ -415,29 +401,18 @@ public void testIsValidDirectoryPathGHSL_POC() throws IOException {
415401

416402
Validator instance = ESAPI.validator();
417403
ValidationErrorList errors = new ValidationErrorList();
418-
419-
String input = null;
420-
File parent = null;
421-
422-
boolean isWindows = (System.getProperty("os.name").indexOf("Windows") != -1) ? true : false;
423-
if (isWindows) {
424-
input = "C:/temp/esapi-test2";
425-
parent = new File("C:/temp/esapi-test/"); // Note the trailing '/'.
426-
} else {
427-
input = "/tmp/esapi-test2";
428-
parent = new File("/tmp/esapi-test/"); // Note the trailing '/'.
429-
}
430-
431-
// Create the 2 directories and set them to be deleted when the JVM exists.
432-
mkdir( input );
433-
mkdir( parent.getCanonicalPath() );
404+
405+
String invalidPath = tempFolder.newFolder("esapi-test2").getAbsolutePath();
406+
File parent = tempFolder.newFolder("sibling-of-esapi-test2");
407+
String validPath = tempFolder.newFolder("sibling-of-esapi-test2", "child").getAbsolutePath();
434408

435409
// Before the fix, this incorrectly would return 'true' even though
436410
// 'esapi-test2' directory clearly was not within the 'esapi-test'
437411
// directory.
438412
//
439-
assertFalse( instance.isValidDirectoryPath("GHSL-2022-008", input, parent, false, errors) );
413+
assertFalse( instance.isValidDirectoryPath("GHSL-2022-008", invalidPath, parent, false, errors) );
440414
assertEquals( 1, errors.size() );
415+
assertTrue (instance.isValidDirectoryPath("GHSL-2022-008", validPath, parent, false, new ValidationErrorList()));
441416
}
442417

443418

0 commit comments

Comments
 (0)