Skip to content

Commit c4d77b1

Browse files
PercentCodecCharacterTest cleanup
Splitting up the PushbackSequence test for better readability. Adding documentation. Whitespace cleanup.
1 parent c3870f6 commit c4d77b1

File tree

1 file changed

+58
-47
lines changed

1 file changed

+58
-47
lines changed

src/test/java/org/owasp/esapi/codecs/PercentCodecCharacterTest.java

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,98 +11,109 @@
1111
import org.owasp.esapi.codecs.PushbackString;
1212

1313
/**
14-
* FIXME: Document intent of class. General Function, purpose of creation, intended feature, etc.
15-
* Why do people care this exists?
16-
* @author Jeremiah
17-
* @since Jan 20, 2018
18-
*
14+
* Codec validation focused on the PercentCodec Character-based api.
15+
*
1916
*/
2017
public class PercentCodecCharacterTest extends AbstractCodecCharacterTest {
2118
private static final char[] PERCENT_CODEC_IMMUNE;
2219

2320
static {
2421
/*
25-
* The percent codec contains a unique immune character set which include letters and numbers that will not be transformed.
26-
*
22+
* The percent codec contains a unique immune character set which include letters and numbers that will not be
23+
* transformed.
2724
* It is being replicated here to allow the test to reasonably expect the correct state back.
2825
*/
2926
List<Character> immune = new ArrayList<>();
3027
// 65 - 90 (capital letters) 97 - 122 lower case 48 - 57 digits
31-
//numbers
32-
for (int index = 48 ; index < 58; index ++) {
33-
immune.add((char)index);
28+
// numbers
29+
for (int index = 48; index < 58; index++) {
30+
immune.add((char) index);
3431
}
35-
//letters
36-
for (int index = 65 ; index < 91; index ++) {
37-
Character capsChar = (char)index;
32+
// letters
33+
for (int index = 65; index < 91; index++) {
34+
Character capsChar = (char) index;
3835
immune.add(capsChar);
39-
immune.add(Character.toLowerCase(capsChar));
36+
immune.add(Character.toLowerCase(capsChar));
4037
}
41-
38+
4239
PERCENT_CODEC_IMMUNE = new char[immune.size()];
4340
for (int index = 0; index < immune.size(); index++) {
4441
PERCENT_CODEC_IMMUNE[index] = immune.get(index).charValue();
4542
}
4643
}
4744

48-
@Parameters(name="{0}")
45+
@Parameters(name = "{0}")
4946
public static Collection<Object[]> buildTests() {
5047
Collection<Object[]> tests = new ArrayList<>();
51-
48+
5249
Collection<CodecCharacterTestTuple> tuples = new ArrayList<>();
53-
tuples.add(newTuple("%3C",Character.valueOf('<')));
54-
55-
tuples.add(newTuple("%C4%80",Character.valueOf((char)0x100)));
56-
tuples.add(newTuple("%00",Character.MIN_VALUE));
57-
tuples.add(newTuple("%3D",'='));
58-
tuples.add(newTuple("%26",'&'));
59-
50+
tuples.add(newTuple("%3C", Character.valueOf('<')));
51+
52+
tuples.add(newTuple("%C4%80", Character.valueOf((char) 0x100)));
53+
tuples.add(newTuple("%00", Character.MIN_VALUE));
54+
tuples.add(newTuple("%3D", '='));
55+
tuples.add(newTuple("%26", '&'));
56+
6057
for (char c : PERCENT_CODEC_IMMUNE) {
6158
tuples.add(newTuple(Character.toString(c), c));
6259
}
63-
60+
6461
for (CodecCharacterTestTuple tuple : tuples) {
65-
tests.add(new Object[]{tuple});
62+
tests.add(new Object[] { tuple });
6663
}
67-
64+
6865
return tests;
6966
}
70-
71-
72-
67+
7368
private static CodecCharacterTestTuple newTuple(String encodedInput, Character decoded) {
7469
CodecCharacterTestTuple tuple = new CodecCharacterTestTuple();
7570
tuple.codec = new PercentCodec();
7671
tuple.encodeImmune = PERCENT_CODEC_IMMUNE;
7772
tuple.decodedValue = decoded;
7873
tuple.input = encodedInput;
79-
74+
8075
return tuple;
8176
}
82-
/**
83-
* @param tuple
84-
*/
77+
8578
public PercentCodecCharacterTest(CodecCharacterTestTuple tuple) {
8679
super(tuple);
8780
}
88-
89-
81+
9082
@Override
9183
@Test
9284
public void testDecodePushbackSequence() {
93-
//If the input is not decoded, then null should be returned and the pushback string index should be unchanged.
94-
//If the input is decode then the decoded value should be returned, and the pushback string index should have progressed forward.
95-
96-
PushbackString pbs = new PushbackString(input);
97-
int startIndex = pbs.index();
98-
boolean shouldDecode = input.startsWith("%");
99-
if (shouldDecode) {
100-
Assert.assertEquals(decodedValue, codec.decodeCharacter(pbs));
101-
Assert.assertTrue(startIndex < pbs.index());
85+
// check duplicated from PushbackSequence handling in PercentCodec.
86+
boolean inputIsEncoded = input.startsWith("%");
87+
88+
if (inputIsEncoded) {
89+
assertInputIsDecodedToValue();
10290
} else {
103-
Assert.assertEquals(null, codec.decodeCharacter(pbs));
104-
Assert.assertEquals(startIndex, pbs.index());
91+
assertInputIsDecodedToNull();
10592
}
10693
}
10794

95+
/**
96+
* tests that when Input is decoded through a PushbackString that the decodedValue reference is returned and that
97+
* the PushbackString index has incremented.
98+
*/
99+
@SuppressWarnings("unchecked")
100+
private void assertInputIsDecodedToValue() {
101+
PushbackString pbs = new PushbackString(input);
102+
int startIndex = pbs.index();
103+
Assert.assertEquals(decodedValue, codec.decodeCharacter(pbs));
104+
Assert.assertTrue(startIndex < pbs.index());
105+
}
106+
107+
/**
108+
* tests that when Input is decoded through a PushbackString that null is returned and that the PushbackString index
109+
* remains unchanged.
110+
*/
111+
@SuppressWarnings("unchecked")
112+
private void assertInputIsDecodedToNull() {
113+
PushbackString pbs = new PushbackString(input);
114+
int startIndex = pbs.index();
115+
Assert.assertEquals(null, codec.decodeCharacter(pbs));
116+
Assert.assertEquals(startIndex, pbs.index());
117+
}
118+
108119
}

0 commit comments

Comments
 (0)