Skip to content

Commit a6819c5

Browse files
jeremiahjstaceyxeno6696
authored andcommitted
https://github.com/ESAPI/esapi-java-legacy/issues/352
Correcting issue. Underlying problem of Issue 352 was that the tests were impacting the same file sets, and were acting under the assumption that they would execute sequentially in the order they appeared in the test file. Introducing more threads caused testLoadPlaintextAndEncrypt and testLoadEncryptedAndAdd to run concurrently and broke the sequential dependency consistently to replicate the listed result. Isolating test behavior by removing the shared static relative filepaths and using the TemporaryFolder junit rule to manage filesystem resources. Removing order-dependent validation of K/V 3 and K/V 4 from the testLoadEncryptedAndAdd implementation.
1 parent ea84364 commit a6819c5

File tree

1 file changed

+43
-42
lines changed

1 file changed

+43
-42
lines changed

src/test/java/org/owasp/esapi/reference/crypto/EncryptedPropertiesUtilsTest.java

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
import java.io.File;
2121
import java.io.FileOutputStream;
2222
import java.io.FileReader;
23+
import java.io.IOException;
2324
import java.util.Properties;
2425

25-
import org.junit.After;
26-
import org.junit.Before;
26+
import org.junit.Rule;
2727
import org.junit.Test;
28+
import org.junit.rules.TemporaryFolder;
29+
import org.junit.rules.TestName;
2830

2931

3032
/**
@@ -45,32 +47,26 @@ public class EncryptedPropertiesUtilsTest {
4547
private static final String KEY4 = "sally sue";
4648
private static final String VALUE4 = "betty mae";
4749

48-
private static final String PLAINTEXT_FILENAME = "plaintext.properties";
49-
private static final String ENCRYPTED_FILENAME_1 = "encrypted.properties";
50-
private static final String ENCRYPTED_FILENAME_2 = "encrypted.2.properties";
51-
52-
/**
53-
* {@inheritDoc}
54-
*/
55-
@Before public void setUp() throws Exception {
56-
//write an initial plaintext properties file
57-
Properties props = new Properties();
58-
props.setProperty(KEY3, VALUE3);
59-
props.setProperty(KEY4, VALUE4);
60-
61-
props.store(new FileOutputStream(PLAINTEXT_FILENAME), "Plaintext test file created by EncryptedPropertiesUtilsTest");
62-
}
63-
50+
51+
/** Rule to acquire the running test's information at runtime.*/
52+
@Rule
53+
public TestName testName = new TestName();
54+
55+
/** File management component for IO targets during test executions.*/
56+
@Rule
57+
public TemporaryFolder tempFolder = new TemporaryFolder();
58+
59+
/** Counter used to track the number of files created for a single test to assist with creating unique file references.*/
60+
private int fileIndex = 0;
61+
6462
/**
65-
* {@inheritDoc}
63+
* Creates a new properties file reference for the active test which will be cleaned up upon test completion.
64+
* @return File New unique file reference for the active test.
65+
* @throws IOException If a new file could not be generated.
6666
*/
67-
@After public void tearDown() throws Exception {
68-
File[] delFiles = new File[] { new File(PLAINTEXT_FILENAME), new File(ENCRYPTED_FILENAME_1), new File(ENCRYPTED_FILENAME_2) };
69-
for ( File f : delFiles ) {
70-
f.deleteOnExit();
71-
}
67+
private final File getTempPropertiesFile() throws IOException {
68+
return tempFolder.newFile(String.format("%s_%2d.properties", testName.getMethodName(), fileIndex++));
7269
}
73-
7470
/**
7571
* Test of creating and storing a new EncryptedProperties from scratch,
7672
* as if calling:
@@ -81,7 +77,8 @@ public class EncryptedPropertiesUtilsTest {
8177
* @throws Exception Any exception that occurs
8278
*/
8379
@Test public void testCreateNew() throws Exception {
84-
80+
File encryptedFile = getTempPropertiesFile();
81+
8582
//create a new properties with no input
8683
Properties props = EncryptedPropertiesUtils.loadProperties(null, null);
8784

@@ -93,11 +90,11 @@ public class EncryptedPropertiesUtilsTest {
9390
assertNull("Expected null but returned: " + prop2, prop2);
9491

9592
//store the file
96-
EncryptedPropertiesUtils.storeProperties(ENCRYPTED_FILENAME_1, props, "Encrypted Properties File generated by EncryptedPropertiesUtilsTest");
93+
EncryptedPropertiesUtils.storeProperties(encryptedFile.getAbsolutePath(), props, "Encrypted Properties File generated by EncryptedPropertiesUtilsTest");
9794

9895
//try reading in the resulting file
9996
ReferenceEncryptedProperties loadedProps = new ReferenceEncryptedProperties();
100-
loadedProps.load(new FileReader(ENCRYPTED_FILENAME_1));
97+
loadedProps.load(new FileReader(encryptedFile.getAbsolutePath()));
10198

10299
assertEquals(VALUE1, loadedProps.getProperty(KEY1));
103100
assertEquals(VALUE2, loadedProps.getProperty(KEY2));
@@ -113,20 +110,29 @@ public class EncryptedPropertiesUtilsTest {
113110
* @throws Exception Any exception that occurs
114111
*/
115112
@Test public void testLoadPlaintextAndEncrypt() throws Exception {
116-
113+
File encryptedFile = getTempPropertiesFile();
114+
File plainTextFile = getTempPropertiesFile();
115+
116+
//write an initial plaintext properties file
117+
Properties props = new Properties();
118+
props.setProperty(KEY3, VALUE3);
119+
props.setProperty(KEY4, VALUE4);
120+
121+
props.store(new FileOutputStream(plainTextFile.getAbsolutePath()), "Plaintext test file created by EncryptedPropertiesUtilsTest");
122+
117123
//load the plaintext properties file
118-
Properties props = EncryptedPropertiesUtils.loadProperties(PLAINTEXT_FILENAME, false);
124+
props = EncryptedPropertiesUtils.loadProperties(plainTextFile.getAbsolutePath(), false);
119125

120126
//test some properties using getProperty
121127
assertEquals(VALUE3, props.getProperty(KEY3));
122128
assertEquals(VALUE4, props.getProperty(KEY4));
123129

124130
//store the file
125-
EncryptedPropertiesUtils.storeProperties(ENCRYPTED_FILENAME_1, props, "Encrypted Properties File generated by EncryptedPropertiesUtilsTest");
131+
EncryptedPropertiesUtils.storeProperties(encryptedFile.getAbsolutePath(), props, "Encrypted Properties File generated by EncryptedPropertiesUtilsTest");
126132

127133
//try reading in the resulting file
128134
ReferenceEncryptedProperties loadedProps = new ReferenceEncryptedProperties();
129-
loadedProps.load(new FileReader(ENCRYPTED_FILENAME_1));
135+
loadedProps.load(new FileReader(encryptedFile.getAbsolutePath()));
130136

131137
assertEquals(VALUE3, loadedProps.getProperty(KEY3));
132138
assertEquals(VALUE4, loadedProps.getProperty(KEY4));
@@ -142,13 +148,10 @@ public class EncryptedPropertiesUtilsTest {
142148
* @throws Exception Any exception that occurs
143149
*/
144150
@Test public void testLoadEncryptedAndAdd() throws Exception {
145-
151+
File encryptedFile = getTempPropertiesFile();
152+
File encryptedFile2 = getTempPropertiesFile();
146153
//load the plaintext properties file
147-
Properties props = EncryptedPropertiesUtils.loadProperties(ENCRYPTED_FILENAME_1, true);
148-
149-
//test some properties
150-
assertEquals(VALUE3, props.getProperty(KEY3));
151-
assertEquals(VALUE4, props.getProperty(KEY4));
154+
Properties props = EncryptedPropertiesUtils.loadProperties(encryptedFile.getAbsolutePath(), true);
152155

153156
//add some new properties
154157
EncryptedPropertiesUtils.addProperty(props, KEY1, VALUE1);
@@ -159,17 +162,15 @@ public class EncryptedPropertiesUtilsTest {
159162
assertEquals(VALUE2, props.getProperty(KEY2));
160163

161164
//store the file
162-
EncryptedPropertiesUtils.storeProperties(ENCRYPTED_FILENAME_2, props, "Encrypted Properties File generated by EncryptedPropertiesUtilsTest");
165+
EncryptedPropertiesUtils.storeProperties(encryptedFile2.getAbsolutePath(), props, "Encrypted Properties File generated by EncryptedPropertiesUtilsTest");
163166

164167
//try reading in the resulting file
165168
ReferenceEncryptedProperties loadedProps = new ReferenceEncryptedProperties();
166-
loadedProps.load(new FileReader(ENCRYPTED_FILENAME_2));
169+
loadedProps.load(new FileReader(encryptedFile2.getAbsolutePath()));
167170

168171
//test the values read in
169172
assertEquals(VALUE1, loadedProps.getProperty(KEY1));
170173
assertEquals(VALUE2, loadedProps.getProperty(KEY2));
171-
assertEquals(VALUE3, loadedProps.getProperty(KEY3));
172-
assertEquals(VALUE4, loadedProps.getProperty(KEY4));
173174
}
174175

175176
}

0 commit comments

Comments
 (0)