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
|
/**
* Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
*
* Author: Oliver Hitz
*
* This file is part of GNU Libidn.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
import gnu.inet.encoding.IDNA;
import gnu.inet.encoding.IDNAException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.StringTokenizer;
public class TestIDNA
{
final static int STATE_SCAN = 0;
final static int STATE_INPUT = 1;
public static void usage()
{
System.err.println("Usage: "+TestIDNA.class.toString()+" [-a|-u string] [-t]");
System.err.println(" -a string: apply toASCII(string)");
System.err.println(" -u string: apply toUnicode(string)");
System.err.println(" -t: automatic test using draft-josefsson-idn-test-vectors.html");
System.exit(1);
}
public static void main(String[] args)
throws Exception
{
String[] tests = new String[] {
"domain\u3002invalid",
"domain\uFF0Einvalid",
"domain\uFF61invalid",
};
for ( int i = 0; i < tests.length ; i++ ) {
assert IDNA.toASCII( tests[i] ).equals( "domain.invalid" );
}
if (args.length == 2) {
if (args[0].equals("-u")) {
try {
System.out.println("Input: "+args[1]);
System.out.println("Output: "+IDNA.toASCII(args[1]));
} catch (IDNAException e) {
System.out.println(e);
}
} else if (args[0].equals("-a")) {
System.out.println("Input: "+args[1]);
System.out.println("Output: "+IDNA.toUnicode(args[1]));
} else {
usage();
}
} else if (args.length == 1 && args[0].equals("-t")) {
File f = new File("draft-josefsson-idn-test-vectors.html");
if (!f.exists()) {
System.err.println("Unable to find draft-josefsson-idn-test-vectors.html.");
System.err.println("Please download the latest version of this file from:");
System.err.println("http://www.gnu.org/software/libidn/");
System.exit(1);
}
BufferedReader r = new BufferedReader(new FileReader(f));
int state = STATE_SCAN;
StringBuffer input = new StringBuffer();
String out;
while (true) {
String l = r.readLine();
if (null == l) {
break;
}
switch (state) {
case STATE_SCAN:
if (l.startsWith("input (length ")) {
state = STATE_INPUT;
input = new StringBuffer();
}
break;
case STATE_INPUT:
if (l.equals("")) {
// Empty line (before "out:")
} else if (l.startsWith("out: ")) {
out = l.substring(5).trim();
try {
String ascii = IDNA.toASCII(input.toString());
if (ascii.equals(out)) {
// Ok
} else {
System.err.println("Error detected:");
System.err.println(" Input: "+input);
System.err.println(" toASCII returned: "+ascii);
System.err.println(" expected result: "+out);
System.exit(1);
}
} catch (IDNAException e) {
System.out.println(" exception thrown ("+e+")");
}
state = STATE_SCAN;
} else {
StringTokenizer tok = new StringTokenizer(l.trim(), " ");
while (tok.hasMoreTokens()) {
String t = tok.nextToken();
if (t.startsWith("U+")) {
char u = (char) Integer.parseInt(t.substring(2, 6), 16);
input.append(u);
} else {
System.err.println("Unknown token: "+t);
}
}
}
break;
}
}
System.out.println("No errors detected!");
} else {
usage();
}
}
}
|