File: TestUDecoder.java

package info (click to toggle)
tomcat9 9.0.115-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 48,140 kB
  • sloc: java: 383,595; xml: 71,225; jsp: 4,682; sh: 1,228; perl: 324; makefile: 18; ansic: 14
file content (470 lines) | stat: -rw-r--r-- 15,215 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.tomcat.util.buf;

import java.io.CharConversionException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.junit.Assert;
import org.junit.Test;

public class TestUDecoder {

    @Test(expected = IllegalArgumentException.class)
    public void testURLDecodeStringInvalid01() {
        // %n rather than %nn should throw an IAE according to the Javadoc
        UDecoder.URLDecode("%5xxxxx", StandardCharsets.UTF_8);
    }


    @Test(expected = IllegalArgumentException.class)
    public void testURLDecodeStringInvalid02() {
        // Edge case trying to trigger ArrayIndexOutOfBoundsException
        UDecoder.URLDecode("%5", StandardCharsets.UTF_8);
    }


    @Test
    public void testURLDecodeStringValidIso88591Start() {
        String result = UDecoder.URLDecode("%41xxxx", StandardCharsets.ISO_8859_1);
        Assert.assertEquals("Axxxx", result);
    }


    @Test
    public void testURLDecodeStringValidIso88591Middle() {
        String result = UDecoder.URLDecode("xx%41xx", StandardCharsets.ISO_8859_1);
        Assert.assertEquals("xxAxx", result);
    }


    @Test
    public void testURLDecodeStringValidIso88591End() {
        String result = UDecoder.URLDecode("xxxx%41", StandardCharsets.ISO_8859_1);
        Assert.assertEquals("xxxxA", result);
    }


    @Test
    public void testURLDecodeStringValidUtf8Start() {
        String result = UDecoder.URLDecode("%c3%aaxxxx", StandardCharsets.UTF_8);
        Assert.assertEquals("\u00eaxxxx", result);
    }


    @Test
    public void testURLDecodeStringValidUtf8Middle() {
        String result = UDecoder.URLDecode("xx%c3%aaxx", StandardCharsets.UTF_8);
        Assert.assertEquals("xx\u00eaxx", result);
    }


    @Test
    public void testURLDecodeStringValidUtf8End() {
        String result = UDecoder.URLDecode("xxxx%c3%aa", StandardCharsets.UTF_8);
        Assert.assertEquals("xxxx\u00ea", result);
    }


    @Test
    public void testURLDecodeStringNonAsciiValidNone() {
        String result = UDecoder.URLDecode("\u00eaxxxx", StandardCharsets.UTF_8);
        Assert.assertEquals("\u00eaxxxx", result);
    }


    @Test
    public void testURLDecodeStringNonAsciiValidUtf8() {
        String result = UDecoder.URLDecode("\u00ea%c3%aa", StandardCharsets.UTF_8);
        Assert.assertEquals("\u00ea\u00ea", result);
    }


    @Test
    public void testURLDecodeStringSolidus01() throws IOException {
        doTestSolidus("xxxxxx", "xxxxxx");
    }


    @Test
    public void testURLDecodeStringSolidus02() throws IOException {
        doTestSolidus("%20xxxx", " xxxx");
    }


    @Test
    public void testURLDecodeStringSolidus03() throws IOException {
        doTestSolidus("xx%20xx", "xx xx");
    }


    @Test
    public void testURLDecodeStringSolidus04() throws IOException {
        doTestSolidus("xxxx%20", "xxxx ");
    }


    @Test(expected = CharConversionException.class)
    public void testURLDecodeStringSolidus05a() throws IOException {
        doTestSolidus("%2fxxxx", EncodedSolidusHandling.REJECT);
    }


    @Test
    public void testURLDecodeStringSolidus05b() throws IOException {
        String result = doTestSolidus("%2fxxxx", EncodedSolidusHandling.PASS_THROUGH);
        Assert.assertEquals("%2fxxxx", result);
    }


    @Test
    public void testURLDecodeStringSolidus05c() throws IOException {
        String result = doTestSolidus("%2fxxxx", EncodedSolidusHandling.DECODE);
        Assert.assertEquals("/xxxx", result);
    }


    @Test(expected = CharConversionException.class)
    public void testURLDecodeStringSolidus06a() throws IOException {
        doTestSolidus("%2fxx%20xx", EncodedSolidusHandling.REJECT);
    }


    @Test
    public void testURLDecodeStringSolidus06b() throws IOException {
        String result = doTestSolidus("%2fxx%20xx", EncodedSolidusHandling.PASS_THROUGH);
        Assert.assertEquals("%2fxx xx", result);
    }


    @Test
    public void testURLDecodeStringSolidus06c() throws IOException {
        String result = doTestSolidus("%2fxx%20xx", EncodedSolidusHandling.DECODE);
        Assert.assertEquals("/xx xx", result);
    }


    @Test(expected = CharConversionException.class)
    public void testURLDecodeStringSolidus07a() throws IOException {
        doTestSolidus("xx%2f%20xx", EncodedSolidusHandling.REJECT);
    }


    @Test
    public void testURLDecodeStringSolidus07b() throws IOException {
        String result = doTestSolidus("xx%2f%20xx", EncodedSolidusHandling.PASS_THROUGH);
        Assert.assertEquals("xx%2f xx", result);
    }


    @Test
    public void testURLDecodeStringSolidus07c() throws IOException {
        String result = doTestSolidus("xx%2f%20xx", EncodedSolidusHandling.DECODE);
        Assert.assertEquals("xx/ xx", result);
    }


    @Test(expected = CharConversionException.class)
    public void testURLDecodeStringSolidus08a() throws IOException {
        doTestSolidus("xx%20%2fxx", EncodedSolidusHandling.REJECT);
    }


    @Test
    public void testURLDecodeStringSolidus08b() throws IOException {
        String result = doTestSolidus("xx%20%2fxx", EncodedSolidusHandling.PASS_THROUGH);
        Assert.assertEquals("xx %2fxx", result);
    }


    @Test
    public void testURLDecodeStringSolidus08c() throws IOException {
        String result = doTestSolidus("xx%20%2fxx", EncodedSolidusHandling.DECODE);
        Assert.assertEquals("xx /xx", result);
    }


    @Test(expected = CharConversionException.class)
    public void testURLDecodeStringSolidus09a() throws IOException {
        doTestSolidus("xx%20xx%2f", EncodedSolidusHandling.REJECT);
    }


    @Test
    public void testURLDecodeStringSolidus09b() throws IOException {
        String result = doTestSolidus("xx%20xx%2f", EncodedSolidusHandling.PASS_THROUGH);
        Assert.assertEquals("xx xx%2f", result);
    }


    @Test
    public void testURLDecodeStringSolidus09c() throws IOException {
        String result = doTestSolidus("xx%20xx%2f", EncodedSolidusHandling.DECODE);
        Assert.assertEquals("xx xx/", result);
    }


    @Test
    public void testURLDecodeStringSolidus10a() throws IOException {
        String result = doTestSolidus("xx%25xx", EncodedSolidusHandling.REJECT);
        Assert.assertEquals("xx%xx", result);
    }


    @Test
    public void testURLDecodeStringSolidus10b() throws IOException {
        String result = doTestSolidus("xx%25xx", EncodedSolidusHandling.PASS_THROUGH);
        Assert.assertEquals("xx%25xx", result);
    }


    @Test
    public void testURLDecodeStringSolidus10c() throws IOException {
        String result = doTestSolidus("xx%25xx", EncodedSolidusHandling.DECODE);
        Assert.assertEquals("xx%xx", result);
    }


    @Test(expected = CharConversionException.class)
    public void testURLDecodeStringSolidus11a() throws IOException {
        String result = doTestSolidus("xx%2f%25xx", EncodedSolidusHandling.REJECT);
        Assert.assertEquals("xx%xx", result);
    }


    @Test
    public void testURLDecodeStringSolidus11b() throws IOException {
        String result = doTestSolidus("xx%2f%25xx", EncodedSolidusHandling.PASS_THROUGH);
        Assert.assertEquals("xx%2f%25xx", result);
    }


    @Test
    public void testURLDecodeStringSolidus11c() throws IOException {
        String result = doTestSolidus("xx%2f%25xx", EncodedSolidusHandling.DECODE);
        Assert.assertEquals("xx/%xx", result);
    }


    private void doTestSolidus(String input, String expected) throws IOException {
        for (EncodedSolidusHandling solidusHandling : EncodedSolidusHandling.values()) {
            String result = doTestSolidus(input, solidusHandling);
            Assert.assertEquals(expected, result);
        }
    }


    private String doTestSolidus(String input, EncodedSolidusHandling solidusHandling) throws IOException {
        byte[] b = input.getBytes(StandardCharsets.UTF_8);
        ByteChunk bc = new ByteChunk(16);
        bc.setBytes(b, 0,  b.length);
        bc.setCharset(StandardCharsets.UTF_8);

        UDecoder udecoder = new UDecoder();
        udecoder.convert(bc, solidusHandling, EncodedSolidusHandling.DECODE);

        return bc.toString();
    }


    @Test
    public void testURLDecodeStringReverseSolidus01() throws IOException {
        doTestReverseSolidus("xxxxxx", "xxxxxx");
    }


    @Test
    public void testURLDecodeStringReverseSolidus02() throws IOException {
        doTestReverseSolidus("%20xxxx", " xxxx");
    }


    @Test
    public void testURLDecodeStringReverseSolidus03() throws IOException {
        doTestReverseSolidus("xx%20xx", "xx xx");
    }


    @Test
    public void testURLDecodeStringReverseSolidus04() throws IOException {
        doTestReverseSolidus("xxxx%20", "xxxx ");
    }


    @Test(expected = CharConversionException.class)
    public void testURLDecodeStringReverseSolidus05a() throws IOException {
        doTestReverseSolidus("%5cxxxx", EncodedSolidusHandling.REJECT);
    }


    @Test
    public void testURLDecodeStringReverseSolidus05b() throws IOException {
        String result = doTestReverseSolidus("%5cxxxx", EncodedSolidusHandling.PASS_THROUGH);
        Assert.assertEquals("%5cxxxx", result);
    }


    @Test
    public void testURLDecodeStringReverseSolidus05c() throws IOException {
        String result = doTestReverseSolidus("%5cxxxx", EncodedSolidusHandling.DECODE);
        Assert.assertEquals("\\xxxx", result);
    }


    @Test(expected = CharConversionException.class)
    public void testURLDecodeStringReverseSolidus06a() throws IOException {
        doTestReverseSolidus("%5cxx%20xx", EncodedSolidusHandling.REJECT);
    }


    @Test
    public void testURLDecodeStringReverseSolidus06b() throws IOException {
        String result = doTestReverseSolidus("%5cxx%20xx", EncodedSolidusHandling.PASS_THROUGH);
        Assert.assertEquals("%5cxx xx", result);
    }


    @Test
    public void testURLDecodeStringReverseSolidus06c() throws IOException {
        String result = doTestReverseSolidus("%5cxx%20xx", EncodedSolidusHandling.DECODE);
        Assert.assertEquals("\\xx xx", result);
    }


    @Test(expected = CharConversionException.class)
    public void testURLDecodeStringReverseSolidus07a() throws IOException {
        doTestReverseSolidus("xx%5c%20xx", EncodedSolidusHandling.REJECT);
    }


    @Test
    public void testURLDecodeStringReverseSolidus07b() throws IOException {
        String result = doTestReverseSolidus("xx%5c%20xx", EncodedSolidusHandling.PASS_THROUGH);
        Assert.assertEquals("xx%5c xx", result);
    }


    @Test
    public void testURLDecodeStringReverseSolidus07c() throws IOException {
        String result = doTestReverseSolidus("xx%5c%20xx", EncodedSolidusHandling.DECODE);
        Assert.assertEquals("xx\\ xx", result);
    }


    @Test(expected = CharConversionException.class)
    public void testURLDecodeStringReverseSolidus08a() throws IOException {
        doTestReverseSolidus("xx%20%5cxx", EncodedSolidusHandling.REJECT);
    }


    @Test
    public void testURLDecodeStringReverseSolidus08b() throws IOException {
        String result = doTestReverseSolidus("xx%20%5cxx", EncodedSolidusHandling.PASS_THROUGH);
        Assert.assertEquals("xx %5cxx", result);
    }


    @Test
    public void testURLDecodeStringReverseSolidus08c() throws IOException {
        String result = doTestReverseSolidus("xx%20%5cxx", EncodedSolidusHandling.DECODE);
        Assert.assertEquals("xx \\xx", result);
    }


    @Test(expected = CharConversionException.class)
    public void testURLDecodeStringReverseSolidus09a() throws IOException {
        doTestReverseSolidus("xx%20xx%5c", EncodedSolidusHandling.REJECT);
    }


    @Test
    public void testURLDecodeStringReverseSolidus09b() throws IOException {
        String result = doTestReverseSolidus("xx%20xx%5c", EncodedSolidusHandling.PASS_THROUGH);
        Assert.assertEquals("xx xx%5c", result);
    }


    @Test
    public void testURLDecodeStringReverseSolidus09c() throws IOException {
        String result = doTestReverseSolidus("xx%20xx%5c", EncodedSolidusHandling.DECODE);
        Assert.assertEquals("xx xx\\", result);
    }


    @Test
    public void testURLDecodeStringReverseSolidus10a() throws IOException {
        String result = doTestReverseSolidus("xx%25xx", EncodedSolidusHandling.REJECT);
        Assert.assertEquals("xx%xx", result);
    }


    @Test
    public void testURLDecodeStringReverseSolidus10b() throws IOException {
        String result = doTestReverseSolidus("xx%25xx", EncodedSolidusHandling.PASS_THROUGH);
        Assert.assertEquals("xx%25xx", result);
    }


    @Test
    public void testURLDecodeStringReverseSolidus10c() throws IOException {
        String result = doTestReverseSolidus("xx%25xx", EncodedSolidusHandling.DECODE);
        Assert.assertEquals("xx%xx", result);
    }


    @Test(expected = CharConversionException.class)
    public void testURLDecodeStringReverseSolidus11a() throws IOException {
        String result = doTestReverseSolidus("xx%5c%25xx", EncodedSolidusHandling.REJECT);
        Assert.assertEquals("xx%xx", result);
    }


    @Test
    public void testURLDecodeStringReverseSolidus11b() throws IOException {
        String result = doTestReverseSolidus("xx%5c%25xx", EncodedSolidusHandling.PASS_THROUGH);
        Assert.assertEquals("xx%5c%25xx", result);
    }


    @Test
    public void testURLDecodeStringReverseSolidus11c() throws IOException {
        String result = doTestReverseSolidus("xx%5c%25xx", EncodedSolidusHandling.DECODE);
        Assert.assertEquals("xx\\%xx", result);
    }


    private void doTestReverseSolidus(String input, String expected) throws IOException {
        for (EncodedSolidusHandling solidusHandling : EncodedSolidusHandling.values()) {
            String result = doTestReverseSolidus(input, solidusHandling);
            Assert.assertEquals(expected, result);
        }
    }


    private String doTestReverseSolidus(String input, EncodedSolidusHandling reverseSolidusHandling) throws IOException {
        byte[] b = input.getBytes(StandardCharsets.UTF_8);
        ByteChunk bc = new ByteChunk(16);
        bc.setBytes(b, 0,  b.length);
        bc.setCharset(StandardCharsets.UTF_8);

        UDecoder udecoder = new UDecoder();
        udecoder.convert(bc, EncodedSolidusHandling.REJECT, reverseSolidusHandling);

        return bc.toString();
    }
}