2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @summary Unit test for sun.net.idn.Punycode
|
|
27 |
* @bug 4737170
|
|
28 |
* @compile -XDignore.symbol.file PunycodeTest.java
|
|
29 |
* @run main/othervm -ea PunycodeTest
|
|
30 |
* @author Edward Wang
|
|
31 |
*/
|
|
32 |
|
|
33 |
import java.util.Scanner;
|
|
34 |
import java.text.ParseException;
|
|
35 |
import sun.net.idn.Punycode;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* unit test for Punycode that is also originated from the sample code
|
|
39 |
* provided in rfc3492.txt
|
|
40 |
*/
|
|
41 |
public class PunycodeTest {
|
|
42 |
|
|
43 |
/* For testing, we'll just set some compile-time limits rather than */
|
|
44 |
/* use malloc(), and set a compile-time option rather than using a */
|
|
45 |
/* command-line option. */
|
|
46 |
|
|
47 |
static final int unicode_max_length = 256;
|
|
48 |
static final int ace_max_length = 256;
|
|
49 |
|
|
50 |
static final String too_big =
|
|
51 |
"input or output is too large, recompile with larger limits\n";
|
|
52 |
static final String invalid_input = "invalid input\n";
|
|
53 |
static final String overflow = "arithmetic overflow\n";
|
|
54 |
static final String io_error = "I/O error\n";
|
|
55 |
|
|
56 |
/* The following string is used to convert printable */
|
|
57 |
/* characters between ASCII and the native charset: */
|
|
58 |
|
|
59 |
static void fail(String msg, String input) {
|
|
60 |
System.out.println(msg+" input: "+input);
|
|
61 |
throw new RuntimeException(msg+" input: "+input);
|
|
62 |
}
|
|
63 |
|
|
64 |
|
|
65 |
public int testCount = 0;
|
|
66 |
|
|
67 |
private int input_length, j;
|
|
68 |
private int output_length[] = new int[1];
|
|
69 |
private boolean case_flags[] = new boolean[unicode_max_length];
|
|
70 |
|
|
71 |
public String testEncoding(String inputS) {
|
|
72 |
char input[] = new char[unicode_max_length];
|
|
73 |
int codept = 0;
|
|
74 |
char uplus[] = new char[2];
|
|
75 |
StringBuffer output;
|
|
76 |
int c;
|
|
77 |
|
|
78 |
/* Read the input code points: */
|
|
79 |
|
|
80 |
input_length = 0;
|
|
81 |
|
|
82 |
Scanner sc = new Scanner(inputS);
|
|
83 |
|
|
84 |
while (sc.hasNext()) { // need to stop at end of line
|
|
85 |
try {
|
|
86 |
String next = sc.next();
|
|
87 |
uplus[0] = next.charAt(0);
|
|
88 |
uplus[1] = next.charAt(1);
|
|
89 |
codept = Integer.parseInt(next.substring(2), 16);
|
|
90 |
} catch (Exception ex) {
|
|
91 |
fail(invalid_input, inputS);
|
|
92 |
}
|
|
93 |
|
|
94 |
if (uplus[1] != '+' || codept > Integer.MAX_VALUE) {
|
|
95 |
fail(invalid_input, inputS);
|
|
96 |
}
|
|
97 |
|
|
98 |
if (input_length == unicode_max_length) fail(too_big, inputS);
|
|
99 |
|
|
100 |
if (uplus[0] == 'u') case_flags[input_length] = false;
|
|
101 |
else if (uplus[0] == 'U') case_flags[input_length] = true;
|
|
102 |
else fail(invalid_input, inputS);
|
|
103 |
|
|
104 |
input[input_length++] = (char)codept;
|
|
105 |
}
|
|
106 |
|
|
107 |
/* Encode: */
|
|
108 |
|
|
109 |
output_length[0] = ace_max_length;
|
|
110 |
try {
|
|
111 |
output = Punycode.encode((new StringBuffer()).append(input, 0, input_length), case_flags);
|
|
112 |
} catch (Exception e) {
|
|
113 |
fail(invalid_input, inputS);
|
|
114 |
// never reach here, just to make compiler happy
|
|
115 |
return null;
|
|
116 |
}
|
|
117 |
|
|
118 |
testCount++;
|
|
119 |
return output.toString();
|
|
120 |
}
|
|
121 |
|
|
122 |
public String testDecoding(String inputS) {
|
|
123 |
char input[] = new char[0];
|
|
124 |
int pp;
|
|
125 |
StringBuffer output;
|
|
126 |
|
|
127 |
/* Read the Punycode input string and convert to ASCII: */
|
|
128 |
|
|
129 |
if (inputS.length() <= ace_max_length+2) {
|
|
130 |
input = inputS.toCharArray();
|
|
131 |
} else {
|
|
132 |
fail(invalid_input, inputS);
|
|
133 |
}
|
|
134 |
input_length = input.length;
|
|
135 |
|
|
136 |
/* Decode: */
|
|
137 |
|
|
138 |
output_length[0] = unicode_max_length;
|
|
139 |
try {
|
|
140 |
output = Punycode.decode((new StringBuffer()).append(input, 0, input_length), case_flags);
|
|
141 |
} catch (Exception e) {
|
|
142 |
fail(invalid_input, inputS);
|
|
143 |
// never reach here, just to make compiler happy
|
|
144 |
return null;
|
|
145 |
}
|
|
146 |
|
|
147 |
/* Output the result: */
|
|
148 |
StringBuffer result = new StringBuffer();
|
|
149 |
for (j = 0; j < output.length(); ++j) {
|
|
150 |
result.append(String.format("%s+%04X ",
|
|
151 |
case_flags[j] ? "U" : "u",
|
|
152 |
(int)output.charAt(j) ));
|
|
153 |
}
|
|
154 |
|
|
155 |
testCount++;
|
|
156 |
return result.substring(0, result.length() - 1);
|
|
157 |
}
|
|
158 |
|
|
159 |
// test data from rfc3492
|
|
160 |
static String[][] testdata = {
|
|
161 |
{"(A) Arabic (Egyptian):",
|
|
162 |
"u+0644 u+064A u+0647 u+0645 u+0627 u+0628 u+062A u+0643 u+0644 "+
|
|
163 |
"u+0645 u+0648 u+0634 u+0639 u+0631 u+0628 u+064A u+061F",
|
|
164 |
"egbpdaj6bu4bxfgehfvwxn"},
|
|
165 |
{"(B) Chinese (simplified):",
|
|
166 |
"u+4ED6 u+4EEC u+4E3A u+4EC0 u+4E48 u+4E0D u+8BF4 u+4E2D u+6587",
|
|
167 |
"ihqwcrb4cv8a8dqg056pqjye"},
|
|
168 |
{"(C) Chinese (traditional):",
|
|
169 |
"u+4ED6 u+5011 u+7232 u+4EC0 u+9EBD u+4E0D u+8AAA u+4E2D u+6587",
|
|
170 |
"ihqwctvzc91f659drss3x8bo0yb"},
|
|
171 |
{"(D) Czech: Pro<ccaron>prost<ecaron>nemluv<iacute><ccaron>esky",
|
|
172 |
"U+0050 u+0072 u+006F u+010D u+0070 u+0072 u+006F u+0073 u+0074 "+
|
|
173 |
"u+011B u+006E u+0065 u+006D u+006C u+0075 u+0076 u+00ED u+010D "+
|
|
174 |
"u+0065 u+0073 u+006B u+0079",
|
|
175 |
"Proprostnemluvesky-uyb24dma41a"},
|
|
176 |
{"(E) Hebrew:",
|
|
177 |
"u+05DC u+05DE u+05D4 u+05D4 u+05DD u+05E4 u+05E9 u+05D5 u+05D8 "+
|
|
178 |
"u+05DC u+05D0 u+05DE u+05D3 u+05D1 u+05E8 u+05D9 u+05DD u+05E2 "+
|
|
179 |
"u+05D1 u+05E8 u+05D9 u+05EA",
|
|
180 |
"4dbcagdahymbxekheh6e0a7fei0b"},
|
|
181 |
{"(F) Hindi (Devanagari):",
|
|
182 |
"u+092F u+0939 u+0932 u+094B u+0917 u+0939 u+093F u+0928 u+094D "+
|
|
183 |
"u+0926 u+0940 u+0915 u+094D u+092F u+094B u+0902 u+0928 u+0939 "+
|
|
184 |
"u+0940 u+0902 u+092C u+094B u+0932 u+0938 u+0915 u+0924 u+0947 "+
|
|
185 |
"u+0939 u+0948 u+0902",
|
|
186 |
"i1baa7eci9glrd9b2ae1bj0hfcgg6iyaf8o0a1dig0cd"},
|
|
187 |
{"(G) Japanese (kanji and hiragana):",
|
|
188 |
"u+306A u+305C u+307F u+3093 u+306A u+65E5 u+672C u+8A9E u+3092 "+
|
|
189 |
"u+8A71 u+3057 u+3066 u+304F u+308C u+306A u+3044 u+306E u+304B",
|
|
190 |
"n8jok5ay5dzabd5bym9f0cm5685rrjetr6pdxa"},
|
|
191 |
{"(H) Korean (Hangul syllables):",
|
|
192 |
"u+C138 u+ACC4 u+C758 u+BAA8 u+B4E0 u+C0AC u+B78C u+B4E4 u+C774 "+
|
|
193 |
"u+D55C u+AD6D u+C5B4 u+B97C u+C774 u+D574 u+D55C u+B2E4 u+BA74 "+
|
|
194 |
"u+C5BC u+B9C8 u+B098 u+C88B u+C744 u+AE4C",
|
|
195 |
"989aomsvi5e83db1d2a355cv1e0vak1dwrv93d5xbh15a0dt30a5j"+
|
|
196 |
"psd879ccm6fea98c"},
|
|
197 |
{"(I) Russian (Cyrillic):",
|
|
198 |
"U+043F u+043E u+0447 u+0435 u+043C u+0443 u+0436 u+0435 u+043E "+
|
|
199 |
"u+043D u+0438 u+043D u+0435 u+0433 u+043E u+0432 u+043E u+0440 "+
|
|
200 |
"u+044F u+0442 u+043F u+043E u+0440 u+0443 u+0441 u+0441 u+043A "+
|
|
201 |
"u+0438",
|
|
202 |
"b1abfaaepdrnnbgefbaDotcwatmq2g4l"},
|
|
203 |
{"(J) Spanish: Porqu<eacute>nopuedensimplementehablarenEspa<ntilde>ol",
|
|
204 |
"U+0050 u+006F u+0072 u+0071 u+0075 u+00E9 u+006E u+006F u+0070 "+
|
|
205 |
"u+0075 u+0065 u+0064 u+0065 u+006E u+0073 u+0069 u+006D u+0070 "+
|
|
206 |
"u+006C u+0065 u+006D u+0065 u+006E u+0074 u+0065 u+0068 u+0061 "+
|
|
207 |
"u+0062 u+006C u+0061 u+0072 u+0065 u+006E U+0045 u+0073 u+0070 "+
|
|
208 |
"u+0061 u+00F1 u+006F u+006C",
|
|
209 |
"PorqunopuedensimplementehablarenEspaol-fmd56a"},
|
|
210 |
{"(K) Vietnamese:"+
|
|
211 |
"T<adotbelow>isaoh<odotbelow>kh<ocirc>ngth<ecirchookabove>ch"+
|
|
212 |
"<ihookabove>n<oacute>iti<ecircacute>ngVi<ecircdotbelow>t",
|
|
213 |
"U+0054 u+1EA1 u+0069 u+0073 u+0061 u+006F u+0068 u+1ECD u+006B "+
|
|
214 |
"u+0068 u+00F4 u+006E u+0067 u+0074 u+0068 u+1EC3 u+0063 u+0068 "+
|
|
215 |
"u+1EC9 u+006E u+00F3 u+0069 u+0074 u+0069 u+1EBF u+006E u+0067 "+
|
|
216 |
"U+0056 u+0069 u+1EC7 u+0074",
|
|
217 |
"TisaohkhngthchnitingVit-kjcr8268qyxafd2f1b9g"},
|
|
218 |
{"(L) 3<nen>B<gumi><kinpachi><sensei>",
|
|
219 |
"u+0033 u+5E74 U+0042 u+7D44 u+91D1 u+516B u+5148 u+751F",
|
|
220 |
"3B-ww4c5e180e575a65lsy2b"},
|
|
221 |
{"(M) <amuro><namie>-with-SUPER-MONKEYS",
|
|
222 |
"u+5B89 u+5BA4 u+5948 u+7F8E u+6075 u+002D u+0077 u+0069 u+0074 "+
|
|
223 |
"u+0068 u+002D U+0053 U+0055 U+0050 U+0045 U+0052 u+002D U+004D "+
|
|
224 |
"U+004F U+004E U+004B U+0045 U+0059 U+0053",
|
|
225 |
"-with-SUPER-MONKEYS-pc58ag80a8qai00g7n9n"},
|
|
226 |
{"(N) Hello-Another-Way-<sorezore><no><basho>",
|
|
227 |
"U+0048 u+0065 u+006C u+006C u+006F u+002D U+0041 u+006E u+006F "+
|
|
228 |
"u+0074 u+0068 u+0065 u+0072 u+002D U+0057 u+0061 u+0079 u+002D "+
|
|
229 |
"u+305D u+308C u+305E u+308C u+306E u+5834 u+6240",
|
|
230 |
"Hello-Another-Way--fc4qua05auwb3674vfr0b"},
|
|
231 |
{"(O) <hitotsu><yane><no><shita>2",
|
|
232 |
"u+3072 u+3068 u+3064 u+5C4B u+6839 u+306E u+4E0B u+0032",
|
|
233 |
"2-u9tlzr9756bt3uc0v"},
|
|
234 |
{"(P) Maji<de>Koi<suru>5<byou><mae>",
|
|
235 |
"U+004D u+0061 u+006A u+0069 u+3067 U+004B u+006F u+0069 u+3059 "+
|
|
236 |
"u+308B u+0035 u+79D2 u+524D",
|
|
237 |
"MajiKoi5-783gue6qz075azm5e"},
|
|
238 |
{"(Q) <pafii>de<runba>",
|
|
239 |
"u+30D1 u+30D5 u+30A3 u+30FC u+0064 u+0065 u+30EB u+30F3 u+30D0",
|
|
240 |
"de-jg4avhby1noc0d"},
|
|
241 |
{"(R) <sono><supiido><de>",
|
|
242 |
"u+305D u+306E u+30B9 u+30D4 u+30FC u+30C9 u+3067",
|
|
243 |
"d9juau41awczczp"},
|
|
244 |
{"(S) -> $1.00 <-",
|
|
245 |
"u+002D u+003E u+0020 u+0024 u+0031 u+002E u+0030 u+0030 u+0020 "+
|
|
246 |
"u+003C u+002D",
|
|
247 |
"-> $1.00 <--"},
|
|
248 |
};
|
|
249 |
|
|
250 |
public static void main(String[] argv) throws Exception {
|
|
251 |
PunycodeTest mytest = new PunycodeTest();
|
|
252 |
for (int i = 0; i < testdata.length; i++) {
|
|
253 |
String encodeResult = mytest.testEncoding(testdata[i][1]);
|
|
254 |
String decodeResult = mytest.testDecoding(testdata[i][2]);
|
|
255 |
|
|
256 |
checkResult(encodeResult, testdata[i][2]);
|
|
257 |
checkResult(decodeResult, testdata[i][1]);
|
|
258 |
}
|
|
259 |
|
|
260 |
System.out.println("Test cases: " + mytest.testCount);
|
|
261 |
}
|
|
262 |
|
|
263 |
public static void checkResult(String actual, String expected) {
|
|
264 |
if (!actual.equals(expected)) {
|
|
265 |
System.out.printf("\n%15s: %s\n", "FAILED", actual);
|
|
266 |
System.out.printf("%15s: %s\n\n", "should be", expected);
|
|
267 |
throw new RuntimeException("Punycode test failed.");
|
|
268 |
} else {
|
|
269 |
System.out.printf("%15s: %s\n", "SUCCEEDED", actual);
|
|
270 |
}
|
|
271 |
}
|
|
272 |
|
|
273 |
}
|