0001: /*
0002: *******************************************************************************
0003: * Copyright (C) 2003-2006, International Business Machines Corporation and *
0004: * others. All Rights Reserved. *
0005: *******************************************************************************
0006: */
0007: package com.ibm.icu.dev.test.stringprep;
0008:
0009: import java.io.InputStream;
0010: import java.util.Random;
0011:
0012: import com.ibm.icu.dev.test.TestFmwk;
0013: import com.ibm.icu.text.IDNA;
0014: import com.ibm.icu.text.StringPrepParseException;
0015: import com.ibm.icu.text.StringPrep;
0016: import com.ibm.icu.text.UCharacterIterator;
0017: import com.ibm.icu.text.UTF16;
0018: import com.ibm.icu.impl.ICUData;
0019: import com.ibm.icu.impl.ICUResourceBundle;
0020: import com.ibm.icu.impl.Utility;
0021:
0022: /**
0023: * @author ram
0024: */
0025: public class TestIDNA extends TestFmwk {
0026: public static void main(String[] args) throws Exception {
0027: new TestIDNA().run(args);
0028: }
0029:
0030: private StringPrepParseException unassignedException = new StringPrepParseException(
0031: "", StringPrepParseException.UNASSIGNED_ERROR);
0032:
0033: public void TestToUnicode() throws Exception {
0034: for (int i = 0; i < TestData.asciiIn.length; i++) {
0035: // test StringBuffer toUnicode
0036: doTestToUnicode(TestData.asciiIn[i], new String(
0037: TestData.unicodeIn[i]), IDNA.DEFAULT, null);
0038: doTestToUnicode(TestData.asciiIn[i], new String(
0039: TestData.unicodeIn[i]), IDNA.ALLOW_UNASSIGNED, null);
0040: doTestToUnicode(TestData.asciiIn[i], new String(
0041: TestData.unicodeIn[i]), IDNA.USE_STD3_RULES, null);
0042: doTestToUnicode(TestData.asciiIn[i], new String(
0043: TestData.unicodeIn[i]), IDNA.USE_STD3_RULES
0044: | IDNA.ALLOW_UNASSIGNED, null);
0045:
0046: }
0047: }
0048:
0049: public void TestToASCII() throws Exception {
0050: for (int i = 0; i < TestData.asciiIn.length; i++) {
0051: // test StringBuffer toUnicode
0052: doTestToASCII(new String(TestData.unicodeIn[i]),
0053: TestData.asciiIn[i], IDNA.DEFAULT, null);
0054: doTestToASCII(new String(TestData.unicodeIn[i]),
0055: TestData.asciiIn[i], IDNA.ALLOW_UNASSIGNED, null);
0056: doTestToUnicode(TestData.asciiIn[i], new String(
0057: TestData.unicodeIn[i]), IDNA.USE_STD3_RULES, null);
0058: doTestToUnicode(TestData.asciiIn[i], new String(
0059: TestData.unicodeIn[i]), IDNA.USE_STD3_RULES
0060: | IDNA.ALLOW_UNASSIGNED, null);
0061:
0062: }
0063: }
0064:
0065: public void TestIDNToASCII() throws Exception {
0066: for (int i = 0; i < TestData.domainNames.length; i++) {
0067: doTestIDNToASCII(TestData.domainNames[i],
0068: TestData.domainNames[i], IDNA.DEFAULT, null);
0069: doTestIDNToASCII(TestData.domainNames[i],
0070: TestData.domainNames[i], IDNA.ALLOW_UNASSIGNED,
0071: null);
0072: doTestIDNToASCII(TestData.domainNames[i],
0073: TestData.domainNames[i], IDNA.USE_STD3_RULES, null);
0074: doTestIDNToASCII(TestData.domainNames[i],
0075: TestData.domainNames[i], IDNA.ALLOW_UNASSIGNED
0076: | IDNA.USE_STD3_RULES, null);
0077: }
0078:
0079: for (int i = 0; i < TestData.domainNames1Uni.length; i++) {
0080: doTestIDNToASCII(TestData.domainNames1Uni[i],
0081: TestData.domainNamesToASCIIOut[i], IDNA.DEFAULT,
0082: null);
0083: doTestIDNToASCII(TestData.domainNames1Uni[i],
0084: TestData.domainNamesToASCIIOut[i],
0085: IDNA.ALLOW_UNASSIGNED, null);
0086: }
0087: }
0088:
0089: public void TestIDNToUnicode() throws Exception {
0090: for (int i = 0; i < TestData.domainNames.length; i++) {
0091: doTestIDNToUnicode(TestData.domainNames[i],
0092: TestData.domainNames[i], IDNA.DEFAULT, null);
0093: doTestIDNToUnicode(TestData.domainNames[i],
0094: TestData.domainNames[i], IDNA.ALLOW_UNASSIGNED,
0095: null);
0096: doTestIDNToUnicode(TestData.domainNames[i],
0097: TestData.domainNames[i], IDNA.USE_STD3_RULES, null);
0098: doTestIDNToUnicode(TestData.domainNames[i],
0099: TestData.domainNames[i], IDNA.ALLOW_UNASSIGNED
0100: | IDNA.USE_STD3_RULES, null);
0101: }
0102: for (int i = 0; i < TestData.domainNamesToASCIIOut.length; i++) {
0103: doTestIDNToUnicode(TestData.domainNamesToASCIIOut[i],
0104: TestData.domainNamesToUnicodeOut[i], IDNA.DEFAULT,
0105: null);
0106: doTestIDNToUnicode(TestData.domainNamesToASCIIOut[i],
0107: TestData.domainNamesToUnicodeOut[i],
0108: IDNA.ALLOW_UNASSIGNED, null);
0109: }
0110: }
0111:
0112: private void doTestToUnicode(String src, String expected,
0113: int options, Object expectedException) throws Exception {
0114: StringBuffer inBuf = new StringBuffer(src);
0115: UCharacterIterator inIter = UCharacterIterator.getInstance(src);
0116: try {
0117:
0118: StringBuffer out = IDNA.convertToUnicode(src, options);
0119: if (expected != null && out != null
0120: && !out.toString().equals(expected)) {
0121: errln("convertToUnicode did not return expected result with options : "
0122: + options
0123: + " Expected: "
0124: + prettify(expected)
0125: + " Got: " + prettify(out));
0126: }
0127: if (expectedException != null
0128: && !unassignedException.equals(expectedException)) {
0129: errln("convertToUnicode did not get the expected exception. The operation succeeded!");
0130: }
0131: } catch (StringPrepParseException ex) {
0132: if (expectedException == null
0133: || !ex.equals(expectedException)) {
0134: errln("convertToUnicode did not get the expected exception for source: "
0135: + prettify(src) + " Got: " + ex.toString());
0136: }
0137: }
0138: try {
0139:
0140: StringBuffer out = IDNA.convertToUnicode(inBuf, options);
0141: if (expected != null && out != null
0142: && !out.toString().equals(expected)) {
0143: errln("convertToUnicode did not return expected result with options : "
0144: + options
0145: + " Expected: "
0146: + prettify(expected)
0147: + " Got: " + out);
0148: }
0149: if (expectedException != null
0150: && !unassignedException.equals(expectedException)) {
0151: errln("convertToUnicode did not get the expected exception. The operation succeeded!");
0152: }
0153: } catch (StringPrepParseException ex) {
0154: if (expectedException == null
0155: || !ex.equals(expectedException)) {
0156: errln("convertToUnicode did not get the expected exception for source: "
0157: + prettify(src) + " Got: " + ex.toString());
0158: }
0159: }
0160:
0161: try {
0162: StringBuffer out = IDNA.convertToUnicode(inIter, options);
0163: if (expected != null && out != null
0164: && !out.toString().equals(expected)) {
0165: errln("convertToUnicode did not return expected result with options : "
0166: + options
0167: + " Expected: "
0168: + prettify(expected)
0169: + " Got: " + prettify(out));
0170: }
0171: if (expectedException != null
0172: && !unassignedException.equals(expectedException)) {
0173: errln("Did not get the expected exception. The operation succeeded!");
0174: }
0175: } catch (StringPrepParseException ex) {
0176: if (expectedException == null
0177: || !ex.equals(expectedException)) {
0178: errln("Did not get the expected exception for source: "
0179: + prettify(src) + " Got: " + ex.toString());
0180: }
0181: }
0182: }
0183:
0184: private void doTestIDNToUnicode(String src, String expected,
0185: int options, Object expectedException) throws Exception {
0186: StringBuffer inBuf = new StringBuffer(src);
0187: UCharacterIterator inIter = UCharacterIterator.getInstance(src);
0188: try {
0189:
0190: StringBuffer out = IDNA.convertIDNToUnicode(src, options);
0191: if (expected != null && out != null
0192: && !out.toString().equals(expected)) {
0193: errln("convertToUnicode did not return expected result with options : "
0194: + options
0195: + " Expected: "
0196: + prettify(expected)
0197: + " Got: " + prettify(out));
0198: }
0199: if (expectedException != null
0200: && !unassignedException.equals(expectedException)) {
0201: errln("convertToUnicode did not get the expected exception. The operation succeeded!");
0202: }
0203: } catch (StringPrepParseException ex) {
0204: if (expectedException == null
0205: || !expectedException.equals(ex)) {
0206: errln("convertToUnicode did not get the expected exception for source: "
0207: + src + " Got: " + ex.toString());
0208: }
0209: }
0210: try {
0211: StringBuffer out = IDNA.convertIDNToUnicode(inBuf, options);
0212: if (expected != null && out != null
0213: && !out.toString().equals(expected)) {
0214: errln("convertToUnicode did not return expected result with options : "
0215: + options
0216: + " Expected: "
0217: + prettify(expected)
0218: + " Got: " + out);
0219: }
0220: if (expectedException != null
0221: && !unassignedException.equals(expectedException)) {
0222: errln("convertToUnicode did not get the expected exception. The operation succeeded!");
0223: }
0224: } catch (StringPrepParseException ex) {
0225: if (expectedException == null
0226: || !expectedException.equals(ex)) {
0227: errln("convertToUnicode did not get the expected exception for source: "
0228: + src + " Got: " + ex.toString());
0229: }
0230: }
0231:
0232: try {
0233: StringBuffer out = IDNA
0234: .convertIDNToUnicode(inIter, options);
0235: if (expected != null && out != null
0236: && !out.toString().equals(expected)) {
0237: errln("convertToUnicode did not return expected result with options : "
0238: + options
0239: + " Expected: "
0240: + prettify(expected)
0241: + " Got: " + prettify(out));
0242: }
0243: if (expectedException != null
0244: && !unassignedException.equals(expectedException)) {
0245: errln("Did not get the expected exception. The operation succeeded!");
0246: }
0247: } catch (StringPrepParseException ex) {
0248: if (expectedException == null
0249: || !expectedException.equals(ex)) {
0250: errln("Did not get the expected exception for source: "
0251: + src + " Got: " + ex.toString());
0252: }
0253: }
0254: }
0255:
0256: private void doTestToASCII(String src, String expected,
0257: int options, Object expectedException) throws Exception {
0258: StringBuffer inBuf = new StringBuffer(src);
0259: UCharacterIterator inIter = UCharacterIterator.getInstance(src);
0260: try {
0261:
0262: StringBuffer out = IDNA.convertToASCII(src, options);
0263: if (!unassignedException.equals(expectedException)
0264: && expected != null && out != null
0265: && expected != null && out != null
0266: && !out.toString().equals(expected.toLowerCase())) {
0267: errln("convertToASCII did not return expected result with options : "
0268: + options
0269: + " Expected: "
0270: + expected
0271: + " Got: "
0272: + out);
0273: }
0274: if (expectedException != null
0275: && !unassignedException.equals(expectedException)) {
0276: errln("convertToASCII did not get the expected exception. The operation succeeded!");
0277: }
0278: } catch (StringPrepParseException ex) {
0279: if (expectedException == null
0280: || !expectedException.equals(ex)) {
0281: errln("convertToASCII did not get the expected exception for source: "
0282: + src
0283: + "\n Got: "
0284: + ex.toString()
0285: + "\n Expected: " + ex.toString());
0286: }
0287: }
0288:
0289: try {
0290: StringBuffer out = IDNA.convertToASCII(inBuf, options);
0291: if (!unassignedException.equals(expectedException)
0292: && expected != null && out != null
0293: && expected != null && out != null
0294: && !out.toString().equals(expected.toLowerCase())) {
0295: errln("convertToASCII did not return expected result with options : "
0296: + options
0297: + " Expected: "
0298: + expected
0299: + " Got: "
0300: + out);
0301: }
0302: if (expectedException != null
0303: && !unassignedException.equals(expectedException)) {
0304: errln("convertToASCII did not get the expected exception. The operation succeeded!");
0305: }
0306: } catch (StringPrepParseException ex) {
0307: if (expectedException == null
0308: || !expectedException.equals(ex)) {
0309: errln("convertToASCII did not get the expected exception for source: "
0310: + src + " Got: " + ex.toString());
0311: }
0312: }
0313:
0314: try {
0315: StringBuffer out = IDNA.convertToASCII(inIter, options);
0316: if (!unassignedException.equals(expectedException)
0317: && expected != null && out != null
0318: && expected != null && out != null
0319: && !out.toString().equals(expected.toLowerCase())) {
0320: errln("convertToASCII did not return expected result with options : "
0321: + options
0322: + " Expected: "
0323: + expected
0324: + " Got: "
0325: + out);
0326: }
0327: if (expectedException != null
0328: && !unassignedException.equals(expectedException)) {
0329: errln("convertToASCII did not get the expected exception. The operation succeeded!");
0330: }
0331: } catch (StringPrepParseException ex) {
0332: if (expectedException == null
0333: || !expectedException.equals(ex)) {
0334: errln("convertToASCII did not get the expected exception for source: "
0335: + src + " Got: " + ex.toString());
0336: }
0337: }
0338: }
0339:
0340: private void doTestIDNToASCII(String src, String expected,
0341: int options, Object expectedException) throws Exception {
0342: StringBuffer inBuf = new StringBuffer(src);
0343: UCharacterIterator inIter = UCharacterIterator.getInstance(src);
0344: try {
0345:
0346: StringBuffer out = IDNA.convertIDNToASCII(src, options);
0347: if (expected != null && out != null
0348: && !out.toString().equals(expected)) {
0349: errln("convertToIDNASCII did not return expected result with options : "
0350: + options
0351: + " Expected: "
0352: + expected
0353: + " Got: "
0354: + out);
0355: }
0356: if (expectedException != null
0357: && !unassignedException.equals(expectedException)) {
0358: errln("convertToIDNASCII did not get the expected exception. The operation succeeded!");
0359: }
0360: } catch (StringPrepParseException ex) {
0361: if (expectedException == null
0362: || !ex.equals(expectedException)) {
0363: errln("convertToIDNASCII did not get the expected exception for source: "
0364: + src + " Got: " + ex.toString());
0365: }
0366: }
0367: try {
0368: StringBuffer out = IDNA.convertIDNToASCII(inBuf, options);
0369: if (expected != null && out != null
0370: && !out.toString().equals(expected)) {
0371: errln("convertToIDNASCII did not return expected result with options : "
0372: + options
0373: + " Expected: "
0374: + expected
0375: + " Got: "
0376: + out);
0377: }
0378: if (expectedException != null
0379: && !unassignedException.equals(expectedException)) {
0380: errln("convertToIDNASCII did not get the expected exception. The operation succeeded!");
0381: }
0382: } catch (StringPrepParseException ex) {
0383: if (expectedException == null
0384: || !ex.equals(expectedException)) {
0385: errln("convertToIDNASCII did not get the expected exception for source: "
0386: + src + " Got: " + ex.toString());
0387: }
0388: }
0389:
0390: try {
0391: StringBuffer out = IDNA.convertIDNToASCII(inIter, options);
0392: if (expected != null && out != null
0393: && !out.toString().equals(expected)) {
0394: errln("convertIDNToASCII did not return expected result with options : "
0395: + options
0396: + " Expected: "
0397: + expected
0398: + " Got: "
0399: + out);
0400: }
0401:
0402: if (expectedException != null
0403: && !unassignedException.equals(expectedException)) {
0404: errln("convertIDNToASCII did not get the expected exception. The operation succeeded!");
0405: }
0406: } catch (StringPrepParseException ex) {
0407: if (expectedException == null
0408: || !ex.equals(expectedException)) {
0409: errln("convertIDNToASCII did not get the expected exception for source: "
0410: + src + " Got: " + ex.toString());
0411: }
0412: }
0413: }
0414:
0415: public void TestConformance() throws Exception {
0416: for (int i = 0; i < TestData.conformanceTestCases.length; i++) {
0417:
0418: TestData.ConformanceTestCase testCase = TestData.conformanceTestCases[i];
0419: if (testCase.expected != null) {
0420: //Test toASCII
0421: doTestToASCII(testCase.input, testCase.output,
0422: IDNA.DEFAULT, testCase.expected);
0423: doTestToASCII(testCase.input, testCase.output,
0424: IDNA.ALLOW_UNASSIGNED, testCase.expected);
0425: }
0426: //Test toUnicode
0427: //doTestToUnicode(testCase.input,testCase.output,IDNA.DEFAULT,testCase.expected);
0428: }
0429: }
0430:
0431: public void TestNamePrepConformance() throws Exception {
0432: InputStream stream = ICUData
0433: .getRequiredStream(ICUResourceBundle.ICU_BUNDLE
0434: + "/uidna.spp");
0435: StringPrep namePrep = new StringPrep(stream);
0436: for (int i = 0; i < TestData.conformanceTestCases.length; i++) {
0437: TestData.ConformanceTestCase testCase = TestData.conformanceTestCases[i];
0438: UCharacterIterator iter = UCharacterIterator
0439: .getInstance(testCase.input);
0440: try {
0441: StringBuffer output = namePrep.prepare(iter,
0442: StringPrep.DEFAULT);
0443: if (testCase.output != null && output != null
0444: && !testCase.output.equals(output.toString())) {
0445: errln("Did not get the expected output. Expected: "
0446: + prettify(testCase.output) + " Got: "
0447: + prettify(output));
0448: }
0449: if (testCase.expected != null
0450: && !unassignedException
0451: .equals(testCase.expected)) {
0452: errln("Did not get the expected exception. The operation succeeded!");
0453: }
0454: } catch (StringPrepParseException ex) {
0455: if (testCase.expected == null
0456: || !ex.equals(testCase.expected)) {
0457: errln("Did not get the expected exception for source: "
0458: + testCase.input
0459: + " Got: "
0460: + ex.toString());
0461: }
0462: }
0463:
0464: try {
0465: iter.setToStart();
0466: StringBuffer output = namePrep.prepare(iter,
0467: StringPrep.ALLOW_UNASSIGNED);
0468: if (testCase.output != null && output != null
0469: && !testCase.output.equals(output.toString())) {
0470: errln("Did not get the expected output. Expected: "
0471: + prettify(testCase.output) + " Got: "
0472: + prettify(output));
0473: }
0474: if (testCase.expected != null
0475: && !unassignedException
0476: .equals(testCase.expected)) {
0477: errln("Did not get the expected exception. The operation succeeded!");
0478: }
0479: } catch (StringPrepParseException ex) {
0480: if (testCase.expected == null
0481: || !ex.equals(testCase.expected)) {
0482: errln("Did not get the expected exception for source: "
0483: + testCase.input
0484: + " Got: "
0485: + ex.toString());
0486: }
0487: }
0488: }
0489:
0490: }
0491:
0492: public void TestErrorCases() throws Exception {
0493: for (int i = 0; i < TestData.errorCases.length; i++) {
0494: TestData.ErrorCase errCase = TestData.errorCases[i];
0495: if (errCase.testLabel == true) {
0496: // Test ToASCII
0497: doTestToASCII(new String(errCase.unicode),
0498: errCase.ascii, IDNA.DEFAULT, errCase.expected);
0499: doTestToASCII(new String(errCase.unicode),
0500: errCase.ascii, IDNA.ALLOW_UNASSIGNED,
0501: errCase.expected);
0502: if (errCase.useSTD3ASCIIRules) {
0503: doTestToASCII(new String(errCase.unicode),
0504: errCase.ascii, IDNA.USE_STD3_RULES,
0505: errCase.expected);
0506: }
0507: }
0508: if (errCase.useSTD3ASCIIRules != true) {
0509:
0510: // Test IDNToASCII
0511: doTestIDNToASCII(new String(errCase.unicode),
0512: errCase.ascii, IDNA.DEFAULT, errCase.expected);
0513: doTestIDNToASCII(new String(errCase.unicode),
0514: errCase.ascii, IDNA.ALLOW_UNASSIGNED,
0515: errCase.expected);
0516:
0517: } else {
0518: doTestIDNToASCII(new String(errCase.unicode),
0519: errCase.ascii, IDNA.USE_STD3_RULES,
0520: errCase.expected);
0521: }
0522:
0523: //TestToUnicode
0524: if (false && errCase.testToUnicode == true) {
0525: if (errCase.useSTD3ASCIIRules != true) {
0526: // Test IDNToUnicode
0527: doTestIDNToUnicode(errCase.ascii, new String(
0528: errCase.unicode), IDNA.DEFAULT,
0529: errCase.expected);
0530: doTestIDNToUnicode(errCase.ascii, new String(
0531: errCase.unicode), IDNA.ALLOW_UNASSIGNED,
0532: errCase.expected);
0533:
0534: } else {
0535: doTestIDNToUnicode(errCase.ascii, new String(
0536: errCase.unicode), IDNA.USE_STD3_RULES,
0537: errCase.expected);
0538: }
0539: }
0540: }
0541: }
0542:
0543: private void doTestCompare(String s1, String s2, boolean isEqual) {
0544: try {
0545: int retVal = IDNA.compare(s1, s2, IDNA.DEFAULT);
0546: if (isEqual == true && retVal != 0) {
0547: errln("Did not get the expected result for s1: "
0548: + prettify(s1) + " s2: " + prettify(s2));
0549: }
0550: retVal = IDNA.compare(new StringBuffer(s1),
0551: new StringBuffer(s2), IDNA.DEFAULT);
0552: if (isEqual == true && retVal != 0) {
0553: errln("Did not get the expected result for s1: "
0554: + prettify(s1) + " s2: " + prettify(s2));
0555: }
0556: retVal = IDNA.compare(UCharacterIterator.getInstance(s1),
0557: UCharacterIterator.getInstance(s2), IDNA.DEFAULT);
0558: if (isEqual == true && retVal != 0) {
0559: errln("Did not get the expected result for s1: "
0560: + prettify(s1) + " s2: " + prettify(s2));
0561: }
0562: } catch (Exception e) {
0563: e.printStackTrace();
0564: errln("Unexpected exception thrown by IDNA.compare");
0565: }
0566:
0567: try {
0568: int retVal = IDNA.compare(s1, s2, IDNA.ALLOW_UNASSIGNED);
0569: if (isEqual == true && retVal != 0) {
0570: errln("Did not get the expected result for s1: "
0571: + prettify(s1) + " s2: " + prettify(s2));
0572: }
0573: retVal = IDNA.compare(new StringBuffer(s1),
0574: new StringBuffer(s2), IDNA.ALLOW_UNASSIGNED);
0575: if (isEqual == true && retVal != 0) {
0576: errln("Did not get the expected result for s1: "
0577: + prettify(s1) + " s2: " + prettify(s2));
0578: }
0579: retVal = IDNA.compare(UCharacterIterator.getInstance(s1),
0580: UCharacterIterator.getInstance(s2),
0581: IDNA.ALLOW_UNASSIGNED);
0582: if (isEqual == true && retVal != 0) {
0583: errln("Did not get the expected result for s1: "
0584: + prettify(s1) + " s2: " + prettify(s2));
0585: }
0586: } catch (Exception e) {
0587: errln("Unexpected exception thrown by IDNA.compare");
0588: }
0589: }
0590:
0591: public void TestCompare() throws Exception {
0592: String www = "www.";
0593: String com = ".com";
0594: StringBuffer source = new StringBuffer(www);
0595: StringBuffer uni0 = new StringBuffer(www);
0596: StringBuffer uni1 = new StringBuffer(www);
0597: StringBuffer ascii0 = new StringBuffer(www);
0598: StringBuffer ascii1 = new StringBuffer(www);
0599:
0600: uni0.append(TestData.unicodeIn[0]);
0601: uni0.append(com);
0602:
0603: uni1.append(TestData.unicodeIn[1]);
0604: uni1.append(com);
0605:
0606: ascii0.append(TestData.asciiIn[0]);
0607: ascii0.append(com);
0608:
0609: ascii1.append(TestData.asciiIn[1]);
0610: ascii1.append(com);
0611:
0612: for (int i = 0; i < TestData.unicodeIn.length; i++) {
0613:
0614: // for every entry in unicodeIn array
0615: // prepend www. and append .com
0616: source.setLength(4);
0617: source.append(TestData.unicodeIn[i]);
0618: source.append(com);
0619:
0620: // a) compare it with itself
0621: doTestCompare(source.toString(), source.toString(), true);
0622:
0623: // b) compare it with asciiIn equivalent
0624: doTestCompare(source.toString(), www + TestData.asciiIn[i]
0625: + com, true);
0626:
0627: // c) compare it with unicodeIn not equivalent
0628: if (i == 0) {
0629: doTestCompare(source.toString(), uni1.toString(), false);
0630: } else {
0631: doTestCompare(source.toString(), uni0.toString(), false);
0632: }
0633: // d) compare it with asciiIn not equivalent
0634: if (i == 0) {
0635: doTestCompare(source.toString(), ascii1.toString(),
0636: false);
0637: } else {
0638: doTestCompare(source.toString(), ascii0.toString(),
0639: false);
0640: }
0641:
0642: }
0643: }
0644:
0645: // test and ascertain
0646: // func(func(func(src))) == func(src)
0647: public void doTestChainingToASCII(String source) throws Exception {
0648: StringBuffer expected;
0649: StringBuffer chained;
0650:
0651: // test convertIDNToASCII
0652: expected = IDNA.convertIDNToASCII(source, IDNA.DEFAULT);
0653: chained = expected;
0654: for (int i = 0; i < 4; i++) {
0655: chained = IDNA.convertIDNToASCII(chained, IDNA.DEFAULT);
0656: }
0657: if (!expected.toString().equals(chained.toString())) {
0658: errln("Chaining test failed for convertIDNToASCII");
0659: }
0660: // test convertIDNToA
0661: expected = IDNA.convertToASCII(source, IDNA.DEFAULT);
0662: chained = expected;
0663: for (int i = 0; i < 4; i++) {
0664: chained = IDNA.convertToASCII(chained, IDNA.DEFAULT);
0665: }
0666: if (!expected.toString().equals(chained.toString())) {
0667: errln("Chaining test failed for convertToASCII");
0668: }
0669: }
0670:
0671: // test and ascertain
0672: // func(func(func(src))) == func(src)
0673: public void doTestChainingToUnicode(String source) throws Exception {
0674: StringBuffer expected;
0675: StringBuffer chained;
0676:
0677: // test convertIDNToUnicode
0678: expected = IDNA.convertIDNToUnicode(source, IDNA.DEFAULT);
0679: chained = expected;
0680: for (int i = 0; i < 4; i++) {
0681: chained = IDNA.convertIDNToUnicode(chained, IDNA.DEFAULT);
0682: }
0683: if (!expected.toString().equals(chained.toString())) {
0684: errln("Chaining test failed for convertIDNToUnicode");
0685: }
0686: // test convertIDNToA
0687: expected = IDNA.convertToUnicode(source, IDNA.DEFAULT);
0688: chained = expected;
0689: for (int i = 0; i < 4; i++) {
0690: chained = IDNA.convertToUnicode(chained, IDNA.DEFAULT);
0691: }
0692: if (!expected.toString().equals(chained.toString())) {
0693: errln("Chaining test failed for convertToUnicode");
0694: }
0695: }
0696:
0697: public void TestChaining() throws Exception {
0698: for (int i = 0; i < TestData.asciiIn.length; i++) {
0699: doTestChainingToUnicode(TestData.asciiIn[i]);
0700: }
0701: for (int i = 0; i < TestData.unicodeIn.length; i++) {
0702: doTestChainingToASCII(new String(TestData.unicodeIn[i]));
0703: }
0704: }
0705:
0706: /* IDNA RFC Says:
0707: A label is an individual part of a domain name. Labels are usually
0708: shown separated by dots; for example, the domain name
0709: "www.example.com" is composed of three labels: "www", "example", and
0710: "com". (The zero-length root label described in [STD13], which can
0711: be explicit as in "www.example.com." or implicit as in
0712: "www.example.com", is not considered a label in this specification.)
0713: */
0714: public void TestRootLabelSeparator() throws Exception {
0715: String www = "www.";
0716: String com = ".com."; //root label separator
0717: StringBuffer source = new StringBuffer(www);
0718: StringBuffer uni0 = new StringBuffer(www);
0719: StringBuffer uni1 = new StringBuffer(www);
0720: StringBuffer ascii0 = new StringBuffer(www);
0721: StringBuffer ascii1 = new StringBuffer(www);
0722:
0723: uni0.append(TestData.unicodeIn[0]);
0724: uni0.append(com);
0725:
0726: uni1.append(TestData.unicodeIn[1]);
0727: uni1.append(com);
0728:
0729: ascii0.append(TestData.asciiIn[0]);
0730: ascii0.append(com);
0731:
0732: ascii1.append(TestData.asciiIn[1]);
0733: ascii1.append(com);
0734:
0735: for (int i = 0; i < TestData.unicodeIn.length; i++) {
0736:
0737: // for every entry in unicodeIn array
0738: // prepend www. and append .com
0739: source.setLength(4);
0740: source.append(TestData.unicodeIn[i]);
0741: source.append(com);
0742:
0743: // a) compare it with itself
0744: doTestCompare(source.toString(), source.toString(), true);
0745:
0746: // b) compare it with asciiIn equivalent
0747: doTestCompare(source.toString(), www + TestData.asciiIn[i]
0748: + com, true);
0749:
0750: // c) compare it with unicodeIn not equivalent
0751: if (i == 0) {
0752: doTestCompare(source.toString(), uni1.toString(), false);
0753: } else {
0754: doTestCompare(source.toString(), uni0.toString(), false);
0755: }
0756: // d) compare it with asciiIn not equivalent
0757: if (i == 0) {
0758: doTestCompare(source.toString(), ascii1.toString(),
0759: false);
0760: } else {
0761: doTestCompare(source.toString(), ascii0.toString(),
0762: false);
0763: }
0764:
0765: }
0766:
0767: }
0768:
0769: private static final int loopCount = 100;
0770: private static final int maxCharCount = 15;
0771: // private static final int maxCodePoint = 0x10ffff;
0772: private Random random = null;
0773:
0774: /**
0775: * Return a random integer i where 0 <= i < n.
0776: * A special function that gets random codepoints from planes 0,1,2 and 14
0777: */
0778: private int rand_uni() {
0779: int retVal = (int) (random.nextLong() & 0x3FFFF);
0780: if (retVal >= 0x30000) {
0781: retVal += 0xB0000;
0782: }
0783: return retVal;
0784: }
0785:
0786: private int randi(int n) {
0787: return (random.nextInt(0x7fff) % (n + 1));
0788: }
0789:
0790: private StringBuffer getTestSource(StringBuffer fillIn) {
0791: // use uniform seed value from the framework
0792: if (random == null) {
0793: random = createRandom();
0794: }
0795: int i = 0;
0796: int charCount = (randi(maxCharCount) + 1);
0797: while (i < charCount) {
0798: int codepoint = rand_uni();
0799: if (codepoint == 0x0000) {
0800: continue;
0801: }
0802: UTF16.append(fillIn, codepoint);
0803: i++;
0804: }
0805: return fillIn;
0806:
0807: }
0808:
0809: public void MonkeyTest() throws Exception {
0810: StringBuffer source = new StringBuffer();
0811: /* do the monkey test */
0812: for (int i = 0; i < loopCount; i++) {
0813: source.setLength(0);
0814: getTestSource(source);
0815: doTestCompareReferenceImpl(source);
0816: }
0817:
0818: // test string with embedded null
0819: source.append("\\u0000\\u2109\\u3E1B\\U000E65CA\\U0001CAC5");
0820:
0821: source = new StringBuffer(Utility.unescape(source.toString()));
0822: doTestCompareReferenceImpl(source);
0823:
0824: //StringBuffer src = new StringBuffer(Utility.unescape("\\uDEE8\\U000E228C\\U0002EE8E\\U000E6350\\U00024DD9\u4049\\U000E0DE4\\U000E448C\\U0001869B\\U000E3380\\U00016A8E\\U000172D5\\U0001C408\\U000E9FB5"));
0825: //doTestCompareReferenceImpl(src);
0826:
0827: //test deletion of code points
0828: source = new StringBuffer(
0829: Utility
0830: .unescape("\\u043f\\u00AD\\u034f\\u043e\\u0447\\u0435\\u043c\\u0443\\u0436\\u0435\\u043e\\u043d\\u0438\\u043d\\u0435\\u0433\\u043e\\u0432\\u043e\\u0440\\u044f\\u0442\\u043f\\u043e\\u0440\\u0443\\u0441\\u0441\\u043a\\u0438"));
0831: StringBuffer expected = new StringBuffer(
0832: "xn--b1abfaaepdrnnbgefbadotcwatmq2g4l");
0833: doTestCompareReferenceImpl(source);
0834: doTestToASCII(source.toString(), expected.toString(),
0835: IDNA.DEFAULT, null);
0836: }
0837:
0838: private void doTestCompareReferenceImpl(StringBuffer src)
0839: throws Exception {
0840:
0841: StringBuffer label = src;
0842:
0843: StringPrepParseException expected = null;
0844: StringBuffer ascii = null;
0845: int options = IDNA.DEFAULT;
0846: logln("Comparing idnaref_toASCII with uidna_toASCII for input: "
0847: + prettify(label));
0848: try {
0849: ascii = IDNAReference.convertToASCII(label, options);
0850: } catch (StringPrepParseException e) {
0851: expected = e;
0852: if (e.equals(unassignedException)) {
0853: options = IDNA.ALLOW_UNASSIGNED;
0854: expected = null;
0855: try {
0856: ascii = IDNAReference
0857: .convertToASCII(label, options);
0858: } catch (StringPrepParseException ex) {
0859: expected = ex;
0860: }
0861: }
0862: }
0863:
0864: doTestToASCII(label.toString(), (ascii == null) ? null : ascii
0865: .toString(), options, expected);
0866:
0867: logln("Comparing idnaref_toUnicode with uidna_toUnicode for input: "
0868: + prettify(label));
0869: StringBuffer uni = null;
0870:
0871: if (expected == null) {
0872: options = IDNA.DEFAULT;
0873: try {
0874: uni = IDNAReference.convertToUnicode(ascii, options);
0875: } catch (StringPrepParseException e) {
0876: expected = e;
0877: if (expected.equals(unassignedException)) {
0878: options = IDNA.ALLOW_UNASSIGNED;
0879: expected = null;
0880: try {
0881: uni = IDNAReference.convertToUnicode(ascii,
0882: options);
0883: } catch (StringPrepParseException ex) {
0884: expected = ex;
0885: }
0886: }
0887: }
0888: doTestToUnicode(ascii.toString(), (uni == null) ? null
0889: : uni.toString(), options, expected);
0890: }
0891:
0892: }
0893:
0894: public void TestCompareRefImpl() throws Exception {
0895:
0896: for (int i = 0x40000; i < 0x10ffff; i++) {
0897: StringBuffer src = new StringBuffer();
0898:
0899: if (isQuick() == true && i > 0x1FFFF) {
0900: return;
0901: }
0902: if (i >= 0x30000 && i <= 0xf0000) {
0903: i += 0xB0000;
0904: }
0905: UTF16.append(src, i);
0906: doTestCompareReferenceImpl(src);
0907:
0908: }
0909: }
0910:
0911: public void TestUnicode32Norm() {
0912: /*
0913: * test Unicode 3.2 normalization, before Public Review Issue #29
0914: * see cnormtst.c TestComposition()
0915: */
0916: final String strings[] = { "\u1100\u0300\u1161\u0327",
0917: "\u0b47\u0300\u0b3e\u0327" };
0918:
0919: String ascii = null, unicode = null;
0920: StringPrepParseException ex2;
0921: int i;
0922:
0923: for (i = 0; i < strings.length; ++i) {
0924: ex2 = null;
0925: try {
0926: ascii = IDNA.convertToASCII(strings[i], 0).toString();
0927: unicode = IDNA.convertToUnicode(ascii, 0).toString();
0928: } catch (StringPrepParseException ex) {
0929: errln("string " + i + " gets exception "
0930: + ex.toString());
0931: }
0932:
0933: if (unicode == null || unicode.compareTo(ascii) != 0) {
0934: String uc = unicode == null ? "(null)" : unicode;
0935:
0936: errln("string " + i + " yields " + uc + " instead of "
0937: + ascii);
0938: }
0939: }
0940: }
0941:
0942: public void TestJB4490() {
0943: String[] in = new String[] { "\u00F5\u00dE\u00dF\u00dD",
0944: "\uFB00\uFB01" };
0945: for (int i = 0; i < in.length; i++) {
0946: try {
0947: String ascii = IDNA.convertToASCII(in[i], IDNA.DEFAULT)
0948: .toString();
0949: try {
0950: String unicode = IDNA.convertToUnicode(ascii,
0951: IDNA.DEFAULT).toString();
0952: } catch (StringPrepParseException ex) {
0953: errln("Unexpected exception for convertToUnicode: "
0954: + ex.getMessage());
0955: }
0956: } catch (StringPrepParseException ex) {
0957: errln("Unexpected exception for convertToASCII: "
0958: + ex.getMessage());
0959: }
0960: }
0961: }
0962:
0963: public void TestJB4475() {
0964: String[] in = new String[] { "TEST", "test" };
0965: for (int i = 0; i < in.length; i++) {
0966:
0967: try {
0968: String ascii = IDNA.convertToASCII(in[i], IDNA.DEFAULT)
0969: .toString();
0970: if (!ascii.equals(in[i])) {
0971: errln("Did not get the expected string for convertToASCII. Expected: "
0972: + in[i] + " Got: " + ascii);
0973: }
0974: } catch (StringPrepParseException ex) {
0975: errln("Unexpected exception: " + ex.getMessage());
0976: }
0977: }
0978:
0979: }
0980:
0981: public void TestJB5273() {
0982: String INVALID_DOMAIN_NAME = "xn--m\u00FCller.de";
0983:
0984: try {
0985: IDNA.convertIDNToUnicode(INVALID_DOMAIN_NAME, IDNA.DEFAULT);
0986: IDNA.convertIDNToUnicode(INVALID_DOMAIN_NAME,
0987: IDNA.USE_STD3_RULES);
0988:
0989: } catch (StringPrepParseException ex) {
0990: errln("Unexpected exceptoin: " + ex.getMessage());
0991: } catch (ArrayIndexOutOfBoundsException ex) {
0992: errln("Got an ArrayIndexOutOfBoundsException calling convertIDNToUnicode(\""
0993: + INVALID_DOMAIN_NAME + "\")");
0994: }
0995:
0996: }
0997:
0998: public void TestDebug() {
0999: try {
1000: String src = "\u00ED4dn";
1001: String uni = IDNA.convertToUnicode(src, IDNA.DEFAULT)
1002: .toString();
1003: if (!uni.equals(src)) {
1004: errln("Did not get the expected result. Expected: "
1005: + prettify(src) + " Got: " + uni);
1006: }
1007: } catch (StringPrepParseException ex) {
1008: logln("Unexpected exception: " + ex.getMessage());
1009: }
1010: try {
1011: String ascii = IDNA.convertToASCII("\u00AD", IDNA.DEFAULT)
1012: .toString();
1013: if (ascii != null) {
1014: errln("Did not get the expected exception");
1015: }
1016: } catch (StringPrepParseException ex) {
1017: logln("Got the expected exception: " + ex.getMessage());
1018: }
1019: }
1020: }
|