blob: 7fe9402c6ef00524928b73034508f0d383a59e27 [file] [log] [blame]
Neil Fuller0f6f3bd2018-07-13 20:02:58 +01001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net;
18
19import junit.framework.TestCase;
20
21import java.nio.charset.StandardCharsets;
22
23/**
24 * Tests for {@link UriCodec}
25 */
26public class UriCodecTest extends TestCase {
27
28 public void testDecode_emptyString_returnsEmptyString() {
29 assertEquals("", UriCodec.decode("",
30 false /* convertPlus */,
31 StandardCharsets.UTF_8,
32 true /* throwOnFailure */));
33 }
34
35 public void testDecode_wrongHexDigit_fails() {
36 try {
37 // %p in the end.
38 UriCodec.decode("ab%2f$%C4%82%25%e0%a1%80%p",
39 false /* convertPlus */,
40 StandardCharsets.UTF_8,
41 true /* throwOnFailure */);
42 fail("Expected URISyntaxException");
43 } catch (IllegalArgumentException expected) {
44 // Expected.
45 }
46 }
47
48 public void testDecode_secondHexDigitWrong_fails() {
49 try {
50 // %1p in the end.
51 UriCodec.decode("ab%2f$%c4%82%25%e0%a1%80%1p",
52 false /* convertPlus */,
53 StandardCharsets.UTF_8,
54 true /* throwOnFailure */);
55 fail("Expected URISyntaxException");
56 } catch (IllegalArgumentException expected) {
57 // Expected.
58 }
59 }
60
61 public void testDecode_endsWithPercent_fails() {
62 try {
63 // % in the end.
64 UriCodec.decode("ab%2f$%c4%82%25%e0%a1%80%",
65 false /* convertPlus */,
66 StandardCharsets.UTF_8,
67 true /* throwOnFailure */);
68 fail("Expected URISyntaxException");
69 } catch (IllegalArgumentException expected) {
70 // Expected.
71 }
72 }
73
74 public void testDecode_dontThrowException_appendsUnknownCharacter() {
75 assertEquals("ab/$\u0102%\u0840\ufffd",
76 UriCodec.decode("ab%2f$%c4%82%25%e0%a1%80%",
77 false /* convertPlus */,
78 StandardCharsets.UTF_8,
79 false /* throwOnFailure */));
80 }
81
82 public void testDecode_convertPlus() {
83 assertEquals("ab/$\u0102% \u0840",
84 UriCodec.decode("ab%2f$%c4%82%25+%e0%a1%80",
85 true /* convertPlus */,
86 StandardCharsets.UTF_8,
87 false /* throwOnFailure */));
88 }
89
90 // Last character needs decoding (make sure we are flushing the buffer with chars to decode).
91 public void testDecode_lastCharacter() {
92 assertEquals("ab/$\u0102%\u0840",
93 UriCodec.decode("ab%2f$%c4%82%25%e0%a1%80",
94 false /* convertPlus */,
95 StandardCharsets.UTF_8,
96 true /* throwOnFailure */));
97 }
98
99 // Check that a second row of encoded characters is decoded properly (internal buffers are
100 // reset properly).
101 public void testDecode_secondRowOfEncoded() {
102 assertEquals("ab/$\u0102%\u0840aa\u0840",
103 UriCodec.decode("ab%2f$%c4%82%25%e0%a1%80aa%e0%a1%80",
104 false /* convertPlus */,
105 StandardCharsets.UTF_8,
106 true /* throwOnFailure */));
107 }
108}