0N/A/*
2362N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @summary Unit test for sun.net.idn.Punycode
0N/A * @bug 4737170
0N/A * @compile -XDignore.symbol.file PunycodeTest.java
0N/A * @run main/othervm -ea PunycodeTest
0N/A * @author Edward Wang
0N/A */
0N/A
0N/Aimport java.util.Scanner;
0N/Aimport java.text.ParseException;
0N/Aimport sun.net.idn.Punycode;
0N/A
0N/A/**
0N/A * unit test for Punycode that is also originated from the sample code
0N/A * provided in rfc3492.txt
0N/A */
0N/Apublic class PunycodeTest {
0N/A
0N/A /* For testing, we'll just set some compile-time limits rather than */
0N/A /* use malloc(), and set a compile-time option rather than using a */
0N/A /* command-line option. */
0N/A
0N/A static final int unicode_max_length = 256;
0N/A static final int ace_max_length = 256;
0N/A
0N/A static final String too_big =
0N/A "input or output is too large, recompile with larger limits\n";
0N/A static final String invalid_input = "invalid input\n";
0N/A static final String overflow = "arithmetic overflow\n";
0N/A static final String io_error = "I/O error\n";
0N/A
0N/A /* The following string is used to convert printable */
0N/A /* characters between ASCII and the native charset: */
0N/A
0N/A static void fail(String msg, String input) {
0N/A System.out.println(msg+" input: "+input);
0N/A throw new RuntimeException(msg+" input: "+input);
0N/A }
0N/A
0N/A
0N/A public int testCount = 0;
0N/A
0N/A private int input_length, j;
0N/A private int output_length[] = new int[1];
0N/A private boolean case_flags[] = new boolean[unicode_max_length];
0N/A
0N/A public String testEncoding(String inputS) {
0N/A char input[] = new char[unicode_max_length];
0N/A int codept = 0;
0N/A char uplus[] = new char[2];
0N/A StringBuffer output;
0N/A int c;
0N/A
0N/A /* Read the input code points: */
0N/A
0N/A input_length = 0;
0N/A
0N/A Scanner sc = new Scanner(inputS);
0N/A
0N/A while (sc.hasNext()) { // need to stop at end of line
0N/A try {
0N/A String next = sc.next();
0N/A uplus[0] = next.charAt(0);
0N/A uplus[1] = next.charAt(1);
0N/A codept = Integer.parseInt(next.substring(2), 16);
0N/A } catch (Exception ex) {
0N/A fail(invalid_input, inputS);
0N/A }
0N/A
0N/A if (uplus[1] != '+' || codept > Integer.MAX_VALUE) {
0N/A fail(invalid_input, inputS);
0N/A }
0N/A
0N/A if (input_length == unicode_max_length) fail(too_big, inputS);
0N/A
0N/A if (uplus[0] == 'u') case_flags[input_length] = false;
0N/A else if (uplus[0] == 'U') case_flags[input_length] = true;
0N/A else fail(invalid_input, inputS);
0N/A
0N/A input[input_length++] = (char)codept;
0N/A }
0N/A
0N/A /* Encode: */
0N/A
0N/A output_length[0] = ace_max_length;
0N/A try {
0N/A output = Punycode.encode((new StringBuffer()).append(input, 0, input_length), case_flags);
0N/A } catch (Exception e) {
0N/A fail(invalid_input, inputS);
0N/A // never reach here, just to make compiler happy
0N/A return null;
0N/A }
0N/A
0N/A testCount++;
0N/A return output.toString();
0N/A }
0N/A
0N/A public String testDecoding(String inputS) {
0N/A char input[] = new char[0];
0N/A int pp;
0N/A StringBuffer output;
0N/A
0N/A /* Read the Punycode input string and convert to ASCII: */
0N/A
0N/A if (inputS.length() <= ace_max_length+2) {
0N/A input = inputS.toCharArray();
0N/A } else {
0N/A fail(invalid_input, inputS);
0N/A }
0N/A input_length = input.length;
0N/A
0N/A /* Decode: */
0N/A
0N/A output_length[0] = unicode_max_length;
0N/A try {
0N/A output = Punycode.decode((new StringBuffer()).append(input, 0, input_length), case_flags);
0N/A } catch (Exception e) {
0N/A fail(invalid_input, inputS);
0N/A // never reach here, just to make compiler happy
0N/A return null;
0N/A }
0N/A
0N/A /* Output the result: */
0N/A StringBuffer result = new StringBuffer();
0N/A for (j = 0; j < output.length(); ++j) {
0N/A result.append(String.format("%s+%04X ",
0N/A case_flags[j] ? "U" : "u",
0N/A (int)output.charAt(j) ));
0N/A }
0N/A
0N/A testCount++;
0N/A return result.substring(0, result.length() - 1);
0N/A }
0N/A
0N/A // test data from rfc3492
0N/A static String[][] testdata = {
0N/A {"(A) Arabic (Egyptian):",
0N/A "u+0644 u+064A u+0647 u+0645 u+0627 u+0628 u+062A u+0643 u+0644 "+
0N/A "u+0645 u+0648 u+0634 u+0639 u+0631 u+0628 u+064A u+061F",
0N/A "egbpdaj6bu4bxfgehfvwxn"},
0N/A {"(B) Chinese (simplified):",
0N/A "u+4ED6 u+4EEC u+4E3A u+4EC0 u+4E48 u+4E0D u+8BF4 u+4E2D u+6587",
0N/A "ihqwcrb4cv8a8dqg056pqjye"},
0N/A {"(C) Chinese (traditional):",
0N/A "u+4ED6 u+5011 u+7232 u+4EC0 u+9EBD u+4E0D u+8AAA u+4E2D u+6587",
0N/A "ihqwctvzc91f659drss3x8bo0yb"},
0N/A {"(D) Czech: Pro<ccaron>prost<ecaron>nemluv<iacute><ccaron>esky",
0N/A "U+0050 u+0072 u+006F u+010D u+0070 u+0072 u+006F u+0073 u+0074 "+
0N/A "u+011B u+006E u+0065 u+006D u+006C u+0075 u+0076 u+00ED u+010D "+
0N/A "u+0065 u+0073 u+006B u+0079",
0N/A "Proprostnemluvesky-uyb24dma41a"},
0N/A {"(E) Hebrew:",
0N/A "u+05DC u+05DE u+05D4 u+05D4 u+05DD u+05E4 u+05E9 u+05D5 u+05D8 "+
0N/A "u+05DC u+05D0 u+05DE u+05D3 u+05D1 u+05E8 u+05D9 u+05DD u+05E2 "+
0N/A "u+05D1 u+05E8 u+05D9 u+05EA",
0N/A "4dbcagdahymbxekheh6e0a7fei0b"},
0N/A {"(F) Hindi (Devanagari):",
0N/A "u+092F u+0939 u+0932 u+094B u+0917 u+0939 u+093F u+0928 u+094D "+
0N/A "u+0926 u+0940 u+0915 u+094D u+092F u+094B u+0902 u+0928 u+0939 "+
0N/A "u+0940 u+0902 u+092C u+094B u+0932 u+0938 u+0915 u+0924 u+0947 "+
0N/A "u+0939 u+0948 u+0902",
0N/A "i1baa7eci9glrd9b2ae1bj0hfcgg6iyaf8o0a1dig0cd"},
0N/A {"(G) Japanese (kanji and hiragana):",
0N/A "u+306A u+305C u+307F u+3093 u+306A u+65E5 u+672C u+8A9E u+3092 "+
0N/A "u+8A71 u+3057 u+3066 u+304F u+308C u+306A u+3044 u+306E u+304B",
0N/A "n8jok5ay5dzabd5bym9f0cm5685rrjetr6pdxa"},
0N/A {"(H) Korean (Hangul syllables):",
0N/A "u+C138 u+ACC4 u+C758 u+BAA8 u+B4E0 u+C0AC u+B78C u+B4E4 u+C774 "+
0N/A "u+D55C u+AD6D u+C5B4 u+B97C u+C774 u+D574 u+D55C u+B2E4 u+BA74 "+
0N/A "u+C5BC u+B9C8 u+B098 u+C88B u+C744 u+AE4C",
0N/A "989aomsvi5e83db1d2a355cv1e0vak1dwrv93d5xbh15a0dt30a5j"+
0N/A "psd879ccm6fea98c"},
0N/A {"(I) Russian (Cyrillic):",
0N/A "U+043F u+043E u+0447 u+0435 u+043C u+0443 u+0436 u+0435 u+043E "+
0N/A "u+043D u+0438 u+043D u+0435 u+0433 u+043E u+0432 u+043E u+0440 "+
0N/A "u+044F u+0442 u+043F u+043E u+0440 u+0443 u+0441 u+0441 u+043A "+
0N/A "u+0438",
0N/A "b1abfaaepdrnnbgefbaDotcwatmq2g4l"},
0N/A {"(J) Spanish: Porqu<eacute>nopuedensimplementehablarenEspa<ntilde>ol",
0N/A "U+0050 u+006F u+0072 u+0071 u+0075 u+00E9 u+006E u+006F u+0070 "+
0N/A "u+0075 u+0065 u+0064 u+0065 u+006E u+0073 u+0069 u+006D u+0070 "+
0N/A "u+006C u+0065 u+006D u+0065 u+006E u+0074 u+0065 u+0068 u+0061 "+
0N/A "u+0062 u+006C u+0061 u+0072 u+0065 u+006E U+0045 u+0073 u+0070 "+
0N/A "u+0061 u+00F1 u+006F u+006C",
0N/A "PorqunopuedensimplementehablarenEspaol-fmd56a"},
0N/A {"(K) Vietnamese:"+
0N/A "T<adotbelow>isaoh<odotbelow>kh<ocirc>ngth<ecirchookabove>ch"+
0N/A "<ihookabove>n<oacute>iti<ecircacute>ngVi<ecircdotbelow>t",
0N/A "U+0054 u+1EA1 u+0069 u+0073 u+0061 u+006F u+0068 u+1ECD u+006B "+
0N/A "u+0068 u+00F4 u+006E u+0067 u+0074 u+0068 u+1EC3 u+0063 u+0068 "+
0N/A "u+1EC9 u+006E u+00F3 u+0069 u+0074 u+0069 u+1EBF u+006E u+0067 "+
0N/A "U+0056 u+0069 u+1EC7 u+0074",
0N/A "TisaohkhngthchnitingVit-kjcr8268qyxafd2f1b9g"},
0N/A {"(L) 3<nen>B<gumi><kinpachi><sensei>",
0N/A "u+0033 u+5E74 U+0042 u+7D44 u+91D1 u+516B u+5148 u+751F",
0N/A "3B-ww4c5e180e575a65lsy2b"},
0N/A {"(M) <amuro><namie>-with-SUPER-MONKEYS",
0N/A "u+5B89 u+5BA4 u+5948 u+7F8E u+6075 u+002D u+0077 u+0069 u+0074 "+
0N/A "u+0068 u+002D U+0053 U+0055 U+0050 U+0045 U+0052 u+002D U+004D "+
0N/A "U+004F U+004E U+004B U+0045 U+0059 U+0053",
0N/A "-with-SUPER-MONKEYS-pc58ag80a8qai00g7n9n"},
0N/A {"(N) Hello-Another-Way-<sorezore><no><basho>",
0N/A "U+0048 u+0065 u+006C u+006C u+006F u+002D U+0041 u+006E u+006F "+
0N/A "u+0074 u+0068 u+0065 u+0072 u+002D U+0057 u+0061 u+0079 u+002D "+
0N/A "u+305D u+308C u+305E u+308C u+306E u+5834 u+6240",
0N/A "Hello-Another-Way--fc4qua05auwb3674vfr0b"},
0N/A {"(O) <hitotsu><yane><no><shita>2",
0N/A "u+3072 u+3068 u+3064 u+5C4B u+6839 u+306E u+4E0B u+0032",
0N/A "2-u9tlzr9756bt3uc0v"},
0N/A {"(P) Maji<de>Koi<suru>5<byou><mae>",
0N/A "U+004D u+0061 u+006A u+0069 u+3067 U+004B u+006F u+0069 u+3059 "+
0N/A "u+308B u+0035 u+79D2 u+524D",
0N/A "MajiKoi5-783gue6qz075azm5e"},
0N/A {"(Q) <pafii>de<runba>",
0N/A "u+30D1 u+30D5 u+30A3 u+30FC u+0064 u+0065 u+30EB u+30F3 u+30D0",
0N/A "de-jg4avhby1noc0d"},
0N/A {"(R) <sono><supiido><de>",
0N/A "u+305D u+306E u+30B9 u+30D4 u+30FC u+30C9 u+3067",
0N/A "d9juau41awczczp"},
0N/A {"(S) -> $1.00 <-",
0N/A "u+002D u+003E u+0020 u+0024 u+0031 u+002E u+0030 u+0030 u+0020 "+
0N/A "u+003C u+002D",
0N/A "-> $1.00 <--"},
0N/A };
0N/A
0N/A public static void main(String[] argv) throws Exception {
0N/A PunycodeTest mytest = new PunycodeTest();
0N/A for (int i = 0; i < testdata.length; i++) {
0N/A String encodeResult = mytest.testEncoding(testdata[i][1]);
0N/A String decodeResult = mytest.testDecoding(testdata[i][2]);
0N/A
0N/A checkResult(encodeResult, testdata[i][2]);
0N/A checkResult(decodeResult, testdata[i][1]);
0N/A }
0N/A
0N/A System.out.println("Test cases: " + mytest.testCount);
0N/A }
0N/A
0N/A public static void checkResult(String actual, String expected) {
0N/A if (!actual.equals(expected)) {
0N/A System.out.printf("\n%15s: %s\n", "FAILED", actual);
0N/A System.out.printf("%15s: %s\n\n", "should be", expected);
0N/A throw new RuntimeException("Punycode test failed.");
0N/A } else {
0N/A System.out.printf("%15s: %s\n", "SUCCEEDED", actual);
0N/A }
0N/A }
0N/A
0N/A}