Skip to content

Commit c3870f6

Browse files
Pattern Validation Test Cleanup
Adding documentation to the AbstractPatternTest to help with usability. Extracting message constant to a class var in EsapiWhitelistValidaitonPatternTests for improved readability.
1 parent 9d3d93a commit c3870f6

File tree

2 files changed

+43
-38
lines changed

2 files changed

+43
-38
lines changed

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,50 @@
77
import org.junit.runner.RunWith;
88
import org.junit.runners.Parameterized;
99

10-
1110
/**
12-
* FIXME: Document intent of class. General Function, purpose of creation, intended feature, etc.
13-
* Why do people care this exists?
14-
* @author Jeremiah
15-
* @since Jan 20, 2018
16-
*
11+
* Abstract parameterized test case meant to assist with verifying regular expressions in test scope.
12+
* <br/>
13+
* Sub-classes are expected to provide instances of {@link PatternTestTuple} to this instance.
14+
* <br/>
15+
* For better test naming output specify {@link PatternTestTuple#description} and use {@code} @Parameters (name="{0}")},
16+
* where '0' is the index that the PatternTestTuple reference appears in the constructor.
1717
*/
18-
@RunWith (Parameterized.class)
18+
@RunWith(Parameterized.class)
1919
public abstract class AbstractPatternTest {
20-
20+
21+
/**
22+
* Test tuple for Pattern validation.
23+
*/
2124
protected static class PatternTestTuple {
25+
/** String value to be tested against the compiled regex reference. */
2226
String input;
27+
/** Regular expression string that will be compiled and be passed the input. */
2328
String regex;
29+
/** Test Expectation whether input should match the compiled regex. */
2430
boolean shouldMatch;
31+
/** Optional field to override the toString value of this tuple. */
2532
String description;
26-
/** {@inheritDoc}*/
33+
34+
/** {@inheritDoc} */
2735
@Override
2836
public String toString() {
29-
return description != null ? description : regex;
37+
return description != null ? description : regex;
3038
}
3139
}
3240

3341
private String input;
3442
private Pattern pattern;
3543
private boolean shouldMatch;
36-
37-
44+
3845
public AbstractPatternTest(PatternTestTuple tuple) {
3946
this.input = tuple.input;
4047
this.pattern = Pattern.compile(tuple.regex);
4148
this.shouldMatch = tuple.shouldMatch;
4249
}
43-
50+
4451
@Test
4552
public void checkPatternMatches() {
4653
Assert.assertEquals(shouldMatch, pattern.matcher(input).matches());
4754
}
48-
55+
4956
}

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

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,38 @@
1010
/**
1111
* Extension of the AbstractPatternTest which focuses on asserting that the default whitelist regex values applied in
1212
* the validation process are performing the intended function in the environment.
13-
*
1413
* <br/>
15-
*
1614
* If the regex values in this test are found to not match the running environment configurations, then the tests will
1715
* be skipped.
1816
*
1917
* @author Jeremiah
2018
* @since Jan 20, 2018
2119
*/
2220
public class EsapiWhitelistValidationPatternTester extends AbstractPatternTest {
23-
//See ESAPI.properties
24-
private static final String HTTP_QUERY_STRING_PROP_NAME="HTTPQueryString";
25-
private static final String HTTP_QUERY_STRING_REGEX="^([a-zA-Z0-9_\\-]{1,32}=[\\p{L}\\p{N}.\\-/+=_ !$*?@%]*&?)*$";
21+
// See ESAPI.properties
22+
private static final String HTTP_QUERY_STRING_PROP_NAME = "HTTPQueryString";
23+
private static final String HTTP_QUERY_STRING_REGEX = "^([a-zA-Z0-9_\\-]{1,32}=[\\p{L}\\p{N}.\\-/+=_ !$*?@%]*&?)*$";
24+
25+
private static final String CONFIGURATION_PATTERN_MISMATCH_MESSAGE = "The regular expression specified does not match the configuration settings.\n"
26+
+ "If the value was changed from the ESAPI default, it is recommended to copy "
27+
+ "this class into your project, update the regex being tested, and update all "
28+
+ "associated input expectations for your unique environment.";
2629

2730
@Parameters(name = "{0}-{1}")
2831
public static Collection<Object[]> createDefaultPatternTests() {
2932
Collection<Object[]> parameters = new ArrayList<>();
30-
31-
for(PatternTestTuple tuple : buildHttpQueryStringTests()) {
33+
34+
for (PatternTestTuple tuple : buildHttpQueryStringTests()) {
3235
parameters.add(new Object[] { HTTP_QUERY_STRING_PROP_NAME, tuple });
3336
}
3437

35-
3638
return parameters;
3739
}
38-
40+
3941
private static Collection<PatternTestTuple> buildHttpQueryStringTests() {
40-
Collection <PatternTestTuple> httpQueryStringTests = new ArrayList<>();
41-
42-
//MATCHING CASES
42+
Collection<PatternTestTuple> httpQueryStringTests = new ArrayList<>();
43+
44+
// MATCHING CASES
4345
PatternTestTuple tuple = newHttpQueryStringTuple("Default Case", "b", true);
4446
httpQueryStringTests.add(tuple);
4547
tuple = newHttpQueryStringTuple("Percent Encoded Value", "%62", true);
@@ -48,27 +50,27 @@ private static Collection<PatternTestTuple> buildHttpQueryStringTests() {
4850
httpQueryStringTests.add(tuple);
4951
tuple = newHttpQueryStringTuple("Double Equals", "=", true);
5052
httpQueryStringTests.add(tuple);
51-
52-
//NON-MATCHING CASES
53+
54+
// NON-MATCHING CASES
5355
tuple = newHttpQueryStringTuple("Ampersand In Value", "&b", false);
5456
httpQueryStringTests.add(tuple);
55-
tuple = newHttpQueryStringTuple("Null Character", ""+Character.MIN_VALUE, false);
57+
tuple = newHttpQueryStringTuple("Null Character", "" + Character.MIN_VALUE, false);
5658
httpQueryStringTests.add(tuple);
5759
tuple = newHttpQueryStringTuple("Encoded Null Character", "\u0000", false);
5860
httpQueryStringTests.add(tuple);
59-
61+
6062
return httpQueryStringTests;
6163
}
6264

6365
private static PatternTestTuple newHttpQueryStringTuple(String description, String value, boolean shouldPass) {
6466
PatternTestTuple tuple = new PatternTestTuple();
65-
tuple.input = "a="+value;
67+
tuple.input = "a=" + value;
6668
tuple.shouldMatch = shouldPass;
6769
tuple.regex = HTTP_QUERY_STRING_REGEX;
6870
tuple.description = description;
6971
return tuple;
7072
}
71-
73+
7274
public EsapiWhitelistValidationPatternTester(String property, PatternTestTuple tuple) {
7375
super(tuple);
7476
/*
@@ -80,12 +82,8 @@ public EsapiWhitelistValidationPatternTester(String property, PatternTestTuple t
8082
* behavior.
8183
*/
8284
DefaultSecurityConfiguration configuration = new DefaultSecurityConfiguration();
83-
Assume.assumeTrue(
84-
"The regular expression specified does not match the configuration settings.\n"
85-
+ "If the value was changed from the ESAPI default, it is recommended to copy "
86-
+ "this class into your project, update the regex being tested, and update all "
87-
+ "associated input expectations for your unique environment.",
88-
configuration.getValidationPattern(property).toString().equals(tuple.regex));
85+
Assume.assumeTrue(CONFIGURATION_PATTERN_MISMATCH_MESSAGE, configuration.getValidationPattern(property)
86+
.toString().equals(tuple.regex));
8987
}
9088

9189
}

0 commit comments

Comments
 (0)