1 /*
2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package test.java.net.URLDecoder;
24 
25 import java.net.URLDecoder;
26 import java.net.URLEncoder;
27 import java.nio.charset.StandardCharsets;
28 import org.testng.Assert;
29 import org.testng.annotations.DataProvider;
30 import org.testng.annotations.Test;
31 
32 /**
33  * @test
34  * @bug 8183743
35  * @summary Test to verify the new overload method with Charset functions the
36  * same as the existing method that takes a charset name.
37  * @run testng EncodingTest
38  */
39 public class EncodingTest {
40     public static enum ParameterType {
41         STRING,
42         CHARSET
43     }
44 
45     @DataProvider(name = "illegalArgument")
getParameters()46     public Object[][] getParameters() {
47         return new Object[][]{
48                 {ParameterType.STRING},
49                 {ParameterType.CHARSET}
50         };
51     }
52 
53     @DataProvider(name = "decode")
getDecodeParameters()54     public Object[][] getDecodeParameters() {
55         return new Object[][]{
56                 {"The string \u00FC@foo-bar"},
57                 // the string from javadoc example
58 
59                 {""}, // an empty string
60 
61                 {"x"}, // a string of length 1
62 
63                 {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.*"},
64                 // the string of characters should remain the same
65 
66                 {charactersRange('\u0000', '\u007F')},
67                 // a string of characters from 0 to 127
68 
69                 {charactersRange('\u0080', '\u00FF')},
70                 // a string of characters from 128 to 255
71 
72                 {"\u0100 \u0101 \u0555 \u07FD \u07FF"},
73                 // a string of Unicode values can be expressed as 2 bytes
74 
75                 {"\u8000 \u8001 \uA000 \uFFFD \uFFFF"}, // a string of Unicode values can be expressed as 3 bytes
76         };
77     }
78 
79     /**
80      * Verifies that IAE is thrown when decoding an invalid string using the
81      * existing method or the new overload method.
82      *
83      * @param type the type of the argument, e.g. a String charset name or
84      * charset
85      */
86     @Test(dataProvider = "illegalArgument", expectedExceptions = IllegalArgumentException.class)
testIllegalArgument(ParameterType type)87     public void testIllegalArgument(ParameterType type) throws Exception {
88         String encoded = URLEncoder.encode("http://www.xyz.com/find?key=\u0100\u0101",
89                 StandardCharsets.UTF_8.name());
90         String illegal = "%" + encoded;
91         String returned;
92         if (type == ParameterType.STRING) {
93             returned = URLDecoder.decode(illegal, StandardCharsets.UTF_8.name());
94         } else {
95             returned = URLDecoder.decode(illegal, StandardCharsets.UTF_8);
96         }
97     }
98 
99     /**
100      * Verifies that the returned values of decoding with the existing
101      * and the overload methods match.
102      *
103      * @param s the string to be encoded and then decoded with both existing
104      * and the overload methods.
105      * @throws Exception
106      */
107     @Test(dataProvider = "decode")
decode(String s)108     public void decode(String s) throws Exception {
109         String encoded = URLEncoder.encode(s, StandardCharsets.UTF_8.name());
110         String returned1 = URLDecoder.decode(encoded, StandardCharsets.UTF_8.name());
111         String returned2 = URLDecoder.decode(encoded, StandardCharsets.UTF_8);
112         Assert.assertEquals(returned1, returned2);
113     }
114 
charactersRange(char c1, char c2)115     String charactersRange(char c1, char c2) {
116         StringBuilder sb = new StringBuilder(c2 - c1);
117         for (char c = c1; c < c2; c++) {
118             sb.append(c);
119         }
120 
121         return sb.toString();
122     }
123 }