1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.codec.net;
19
20 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertNull;
23 import static org.junit.jupiter.api.Assertions.assertThrows;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25
26 import java.nio.charset.StandardCharsets;
27 import java.util.Arrays;
28
29 import org.apache.commons.codec.DecoderException;
30 import org.apache.commons.codec.EncoderException;
31 import org.junit.jupiter.api.Disabled;
32 import org.junit.jupiter.api.Test;
33
34
35
36
37 public class PercentCodecTest {
38
39 @Test
40 public void testBasicEncodeDecode() throws Exception {
41 final PercentCodec percentCodec = new PercentCodec();
42 final String input = "abcdABCD";
43 final byte[] encoded = percentCodec.encode(input.getBytes(StandardCharsets.UTF_8));
44 final String encodedS = new String(encoded, StandardCharsets.UTF_8);
45 final byte[] decoded = percentCodec.decode(encoded);
46 final String decodedS = new String(decoded, StandardCharsets.UTF_8);
47 assertEquals(input, encodedS, "Basic PercentCodec encoding test");
48 assertEquals(input, decodedS, "Basic PercentCodec decoding test");
49 }
50
51 @Test
52 @Disabled
53 public void testBasicSpace() throws Exception {
54 final PercentCodec percentCodec = new PercentCodec();
55 final String input = " ";
56 final byte[] encoded = percentCodec.encode(input.getBytes(StandardCharsets.UTF_8));
57 assertArrayEquals("%20".getBytes(StandardCharsets.UTF_8), encoded);
58 }
59
60 @Test
61 public void testConfigurablePercentEncoder() throws Exception {
62 final String input = "abc123_-.*\u03B1\u03B2";
63 final PercentCodec percentCodec = new PercentCodec("abcdef".getBytes(StandardCharsets.UTF_8), false);
64 final byte[] encoded = percentCodec.encode(input.getBytes(StandardCharsets.UTF_8));
65 final String encodedS = new String(encoded, StandardCharsets.UTF_8);
66 assertEquals("%61%62%63123_-.*%CE%B1%CE%B2", encodedS, "Configurable PercentCodec encoding test");
67 final byte[] decoded = percentCodec.decode(encoded);
68 assertEquals(new String(decoded, StandardCharsets.UTF_8), input, "Configurable PercentCodec decoding test");
69 }
70
71 @Test
72 public void testDecodeInvalidEncodedResultDecoding() throws Exception {
73 final String inputS = "\u03B1\u03B2";
74 final PercentCodec percentCodec = new PercentCodec();
75 final byte[] encoded = percentCodec.encode(inputS.getBytes(StandardCharsets.UTF_8));
76 try {
77 percentCodec.decode(Arrays.copyOf(encoded, encoded.length - 1));
78 } catch (final Exception e) {
79 assertTrue(DecoderException.class.isInstance(e) && ArrayIndexOutOfBoundsException.class.isInstance(e.getCause()));
80 }
81 }
82
83 @Test
84 public void testDecodeNullObject() throws Exception {
85 final PercentCodec percentCodec = new PercentCodec();
86 assertNull(percentCodec.decode((Object) null));
87 }
88
89 @Test
90 public void testDecodeUnsupportedObject() {
91 final PercentCodec percentCodec = new PercentCodec();
92 assertThrows(DecoderException.class, () -> percentCodec.decode("test"));
93 }
94
95 @Test
96 public void testEncodeNullObject() throws Exception {
97 final PercentCodec percentCodec = new PercentCodec();
98 assertNull(percentCodec.encode((Object) null));
99 }
100
101 @Test
102 public void testEncodeUnsupportedObject() {
103 final PercentCodec percentCodec = new PercentCodec();
104 assertThrows(EncoderException.class, () -> percentCodec.encode("test"));
105 }
106
107 @Test
108 public void testInvalidByte() throws Exception {
109 final byte[] invalid = { (byte) -1, (byte) 'A' };
110 assertThrows(IllegalArgumentException.class, () -> new PercentCodec(invalid, true));
111 }
112
113 @Test
114 public void testPercentEncoderDecoderWithNullOrEmptyInput() throws Exception {
115 final PercentCodec percentCodec = new PercentCodec(null, true);
116 assertNull(percentCodec.encode(null), "Null input value encoding test");
117 assertNull(percentCodec.decode(null), "Null input value decoding test");
118 final byte[] emptyInput = "".getBytes(StandardCharsets.UTF_8);
119 assertEquals(percentCodec.encode(emptyInput), emptyInput, "Empty input value encoding test");
120 assertArrayEquals(percentCodec.decode(emptyInput), emptyInput, "Empty input value decoding test");
121 }
122
123 @Test
124 public void testPercentEncoderDecoderWithPlusForSpace() throws Exception {
125 final String input = "a b c d";
126 final PercentCodec percentCodec = new PercentCodec(null, true);
127 final byte[] encoded = percentCodec.encode(input.getBytes(StandardCharsets.UTF_8));
128 final String encodedS = new String(encoded, StandardCharsets.UTF_8);
129 assertEquals("a+b+c+d", encodedS, "PercentCodec plus for space encoding test");
130 final byte[] decode = percentCodec.decode(encoded);
131 assertEquals(new String(decode, StandardCharsets.UTF_8), input, "PercentCodec plus for space decoding test");
132 }
133
134 @Test
135 public void testSafeCharEncodeDecodeObject() throws Exception {
136 final PercentCodec percentCodec = new PercentCodec(null, true);
137 final String input = "abc123_-.*";
138 final Object encoded = percentCodec.encode((Object) input.getBytes(StandardCharsets.UTF_8));
139 final String encodedS = new String((byte[]) encoded, StandardCharsets.UTF_8);
140 final Object decoded = percentCodec.decode(encoded);
141 final String decodedS = new String((byte[]) decoded, StandardCharsets.UTF_8);
142 assertEquals(input, encodedS, "Basic PercentCodec safe char encoding test");
143 assertEquals(input, decodedS, "Basic PercentCodec safe char decoding test");
144 }
145
146 @Test
147 public void testUnsafeCharEncodeDecode() throws Exception {
148 final PercentCodec percentCodec = new PercentCodec();
149 final String input = "\u03B1\u03B2\u03B3\u03B4\u03B5\u03B6% ";
150 final byte[] encoded = percentCodec.encode(input.getBytes(StandardCharsets.UTF_8));
151 final String encodedS = new String(encoded, StandardCharsets.UTF_8);
152 final byte[] decoded = percentCodec.decode(encoded);
153 final String decodedS = new String(decoded, StandardCharsets.UTF_8);
154 assertEquals("%CE%B1%CE%B2%CE%B3%CE%B4%CE%B5%CE%B6%25 ", encodedS, "Basic PercentCodec unsafe char encoding test");
155 assertEquals(input, decodedS, "Basic PercentCodec unsafe char decoding test");
156 }
157
158 }