|
11 | 11 | import org.owasp.esapi.codecs.PushbackString;
|
12 | 12 |
|
13 | 13 | /**
|
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 | + * |
19 | 16 | */
|
20 | 17 | public class PercentCodecCharacterTest extends AbstractCodecCharacterTest {
|
21 | 18 | private static final char[] PERCENT_CODEC_IMMUNE;
|
22 | 19 |
|
23 | 20 | static {
|
24 | 21 | /*
|
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. |
27 | 24 | * It is being replicated here to allow the test to reasonably expect the correct state back.
|
28 | 25 | */
|
29 | 26 | List<Character> immune = new ArrayList<>();
|
30 | 27 | // 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); |
34 | 31 | }
|
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; |
38 | 35 | immune.add(capsChar);
|
39 |
| - immune.add(Character.toLowerCase(capsChar)); |
| 36 | + immune.add(Character.toLowerCase(capsChar)); |
40 | 37 | }
|
41 |
| - |
| 38 | + |
42 | 39 | PERCENT_CODEC_IMMUNE = new char[immune.size()];
|
43 | 40 | for (int index = 0; index < immune.size(); index++) {
|
44 | 41 | PERCENT_CODEC_IMMUNE[index] = immune.get(index).charValue();
|
45 | 42 | }
|
46 | 43 | }
|
47 | 44 |
|
48 |
| - @Parameters(name="{0}") |
| 45 | + @Parameters(name = "{0}") |
49 | 46 | public static Collection<Object[]> buildTests() {
|
50 | 47 | Collection<Object[]> tests = new ArrayList<>();
|
51 |
| - |
| 48 | + |
52 | 49 | 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 | + |
60 | 57 | for (char c : PERCENT_CODEC_IMMUNE) {
|
61 | 58 | tuples.add(newTuple(Character.toString(c), c));
|
62 | 59 | }
|
63 |
| - |
| 60 | + |
64 | 61 | for (CodecCharacterTestTuple tuple : tuples) {
|
65 |
| - tests.add(new Object[]{tuple}); |
| 62 | + tests.add(new Object[] { tuple }); |
66 | 63 | }
|
67 |
| - |
| 64 | + |
68 | 65 | return tests;
|
69 | 66 | }
|
70 |
| - |
71 |
| - |
72 |
| - |
| 67 | + |
73 | 68 | private static CodecCharacterTestTuple newTuple(String encodedInput, Character decoded) {
|
74 | 69 | CodecCharacterTestTuple tuple = new CodecCharacterTestTuple();
|
75 | 70 | tuple.codec = new PercentCodec();
|
76 | 71 | tuple.encodeImmune = PERCENT_CODEC_IMMUNE;
|
77 | 72 | tuple.decodedValue = decoded;
|
78 | 73 | tuple.input = encodedInput;
|
79 |
| - |
| 74 | + |
80 | 75 | return tuple;
|
81 | 76 | }
|
82 |
| - /** |
83 |
| - * @param tuple |
84 |
| - */ |
| 77 | + |
85 | 78 | public PercentCodecCharacterTest(CodecCharacterTestTuple tuple) {
|
86 | 79 | super(tuple);
|
87 | 80 | }
|
88 |
| - |
89 |
| - |
| 81 | + |
90 | 82 | @Override
|
91 | 83 | @Test
|
92 | 84 | 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(); |
102 | 90 | } else {
|
103 |
| - Assert.assertEquals(null, codec.decodeCharacter(pbs)); |
104 |
| - Assert.assertEquals(startIndex, pbs.index()); |
| 91 | + assertInputIsDecodedToNull(); |
105 | 92 | }
|
106 | 93 | }
|
107 | 94 |
|
| 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 | + |
108 | 119 | }
|
0 commit comments