Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/idn/TestStringPrep.java
38840 views
/*1* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Unit test for sun.net.idn.Punycode26* @bug 473717027* @library .28* @compile -XDignore.symbol.file TestStringPrep.java NFS4StringPrep.java29* TestData.java30* @run main/othervm -ea TestStringPrep31* @author Edward Wang32*/33/*34*******************************************************************************35* Copyright (C) 2003-2004, International Business Machines Corporation and *36* others. All Rights Reserved. *37*******************************************************************************38*/3940import java.text.ParseException;41import java.io.InputStream;4243import sun.net.idn.StringPrep;44import sun.text.normalizer.UCharacterIterator;4546public class TestStringPrep {47public static void main(String[] args) throws Exception {48TestNFS4MixedPrep();49TestCISPrep();50TestCSPrep();51TestNamePrepConformance();52}53/*54There are several special identifiers ("who") which need to be55understood universally, rather than in the context of a particular56DNS domain. Some of these identifiers cannot be understood when an57NFS client accesses the server, but have meaning when a local process58accesses the file. The ability to display and modify these59permissions is permitted over NFS, even if none of the access methods60on the server understands the identifiers.6162Who Description63_______________________________________________________________6465"OWNER" The owner of the file.66"GROUP" The group associated with the file.67"EVERYONE" The world.68"INTERACTIVE" Accessed from an interactive terminal.69"NETWORK" Accessed via the network.70"DIALUP" Accessed as a dialup user to the server.71"BATCH" Accessed from a batch job.72"ANONYMOUS" Accessed without any authentication.73"AUTHENTICATED" Any authenticated user (opposite of74ANONYMOUS)75"SERVICE" Access from a system service.7677To avoid conflict, these special identifiers are distinguish by an78appended "@" and should appear in the form "xxxx@" (note: no domain79name after the "@"). For example: ANONYMOUS@.80*/81private static String[] mixed_prep_data ={82"OWNER@",83"GROUP@",84"EVERYONE@",85"INTERACTIVE@",86"NETWORK@",87"DIALUP@",88"BATCH@",89"ANONYMOUS@",90"AUTHENTICATED@",91"\u0930\u094D\u092E\u094D\u0915\u094D\u0937\u0947\u0924\[email protected]",92"\u0936\u094d\u0930\u0940\u092e\u0926\[email protected]",93"\u092d\u0917\u0935\u0926\u094d\u0917\u0940\u0924\[email protected]",94"\u0905\u0927\u094d\u092f\u093e\[email protected]",95"\u0905\u0930\u094d\u091c\u0941\[email protected]",96"\u0935\u093f\u0937\u093e\[email protected]",97"\u092f\u094b\[email protected]",98"\u0927\u0943\u0924\u0930\u093e\u0937\u094d\u091f\u094d\[email protected]",99"\u0909\u0935\u093E\u091A\[email protected]",100"\u0927\u0930\u094d\u092e\u0915\u094d\u0937\u0947\u0924\u094d\u0930\[email protected]",101"\u0915\u0941\u0930\u0941\u0915\u094d\u0937\u0947\u0924\u094d\u0930\[email protected]",102"\u0938\u092e\u0935\u0947\u0924\[email protected]",103"\u092f\u0941\u092f\u0941\u0924\u094d\u0938\u0935\[email protected]",104"\u092e\u093e\u092e\u0915\u093e\[email protected]",105"\u092a\u093e\u0923\u094d\u0921\u0935\u093e\u0936\u094d\u091a\u0948\[email protected]",106"\u0915\u093f\u092e\u0915\u0941\u0930\u094d\u0935\[email protected]",107"\u0938\u0902\u091c\[email protected]",108"\u0c30\u0c18\u0c41\u0c30\u0c3e\u0c2e\[email protected]",109"\u0c35\u0c3f\u0c36\u0c4d\u0c35\u0c28\u0c3e\[email protected]",110"\u0c06\u0c28\u0c02\u0c26\[email protected]",111"\u0C35\u0C26\u0C4D\u0C26\u0C3F\u0C30\u0C3E\u0C1C\[email protected]",112"\u0c30\u0c3e\u0c1c\u0c40\u0c35\[email protected]",113"\u0c15\u0c36\u0c30\u0c2c\u0c3e\[email protected]",114"\u0c38\u0c02\u0c1c\u0c40\u0c35\[email protected]",115"\u0c15\u0c36\u0c30\u0c2c\u0c3e\[email protected]",116"\u0c38\u0c02\u0c1c\u0c40\u0c2c\[email protected]",117"\u0c38\u0c46\u0c28\u0c4d\u0c17\u0c41\u0c2a\u0c4d\[email protected]",118"\u0c05\u0c2e\u0c30\u0c47\u0c02\u0c26\u0c4d\[email protected]",119"\u0c39\u0c28\u0c41\u0c2e\u0c3e\u0c28\u0c41\[email protected]",120"\u0c30\u0c35\[email protected]",121"\u0c15\u0c41\u0c2e\u0c3e\u0c30\[email protected]",122"\u0c35\u0c3f\u0c36\u0c4d\u0c35\u0c28\u0c3e\u0c27@\u0917\u0928\u0947\u0936.sanjose.ibm.com",123"\u0c06\u0c26\u0c3f\u0c24\u0c4d\u0c2f@www.\u00E0\u00B3\u00AF.com",124"\u0C15\u0C02\u0C26\u0C4D\u0C30\u0C47\u0C17\u0C41\u0c32@www.\u00C2\u00A4.com",125"\u0c36\u0c4d\u0c30\u0c40\u0C27\u0C30\u0C4D@www.\u00C2\u00A3.com",126"\u0c15\u0c02\u0c1f\u0c2e\u0c36\u0c46\u0c1f\u0c4d\u0c1f\u0c3f@\u0025",127"\u0c2e\u0c3e\u0c27\u0c35\u0c4d@\u005C\u005C",128"\u0c26\u0c46\u0c36\u0c46\u0c1f\u0c4d\u0c1f\u0c3f@www.\u0021.com",129"test@www.\u0024.com",130"help@\u00C3\u00BC.com",131};132public static void TestNFS4MixedPrep(){133for(int i=0; i< mixed_prep_data.length; i++){134try{135String src = mixed_prep_data[i];136byte[] dest = NFS4StringPrep.mixed_prepare(src.getBytes("UTF-8"));137String destString = new String(dest, "UTF-8");138int destIndex = destString.indexOf('@');139if(destIndex < 0){140fail("Delimiter @ disappeared from the output!");141}142}catch(Exception e){143fail("mixed_prepare for string: " + mixed_prep_data[i] +" failed with " + e.toString());144}145}146/* test the error condition */147{148String src = "[email protected]";149try{150byte[] dest = NFS4StringPrep.mixed_prepare(src.getBytes("UTF-8"));151if(dest!=null){152fail("Did not get the expected exception");153}154}catch(Exception e){155System.out.println("mixed_prepare for string: " + src +" passed with " + e.toString());156}157158}159}160public static void TestCISPrep(){161162for(int i=0;i< (TestData.conformanceTestCases.length);i++){163TestData.ConformanceTestCase testCase = TestData.conformanceTestCases[i];164String src = testCase.input;165Exception expected = testCase.expected;166String expectedDest = testCase.output;167try{168byte[] dest =NFS4StringPrep.cis_prepare(src.getBytes("UTF-8"));169String destString = new String(dest, "UTF-8");170if(!expectedDest.equalsIgnoreCase(destString)){171fail("Did not get the expected output for nfs4_cis_prep at index " + i);172}173}catch(Exception e){174if(!((expected instanceof ParseException) && (e instanceof ParseException))) {175fail("Did not get the expected exception");176}177}178179}180}181182public static void TestCSPrep(){183184// Checking for bidi is turned off185String src = "\uC138\uACC4\uC758\uBAA8\uB4E0\uC0AC\uB78C\uB4E4\uC774\u0644\u064A\u0647\uD55C\uAD6D\uC5B4\uB97C\uC774\uD574\uD55C\uB2E4\uBA74";186try{187NFS4StringPrep.cs_prepare(src.getBytes("UTF-8"), false);188}catch(Exception e){189fail("Got unexpected exception: " + e.toString());190}191192// normalization is turned off193try{194src = "www.\u00E0\u00B3\u00AF.com";195byte[] dest = NFS4StringPrep.cs_prepare(src.getBytes("UTF-8"), false);196String destStr = new String(dest, "UTF-8");197if(!src.equals(destStr)){198fail("Did not get expected output. Expected: "+ prettify(src)+199" Got: " + prettify(destStr));200}201}catch(Exception e){202fail("Got unexpected exception: " + e.toString());203}204205// test case insensitive string206try{207src = "THISISATEST";208byte[] dest = NFS4StringPrep.cs_prepare(src.getBytes("UTF-8"), false);209String destStr = new String(dest, "UTF-8");210if(!src.toLowerCase().equals(destStr)){211fail("Did not get expected output. Expected: "+ prettify(src)+212" Got: " + prettify(destStr));213}214}catch(Exception e){215fail("Got unexpected exception: " + e.toString());216}217// test case sensitive string218try{219src = "THISISATEST";220byte[] dest = NFS4StringPrep.cs_prepare(src.getBytes("UTF-8"), true);221String destStr = new String(dest, "UTF-8");222if(!src.equals(destStr)){223fail("Did not get expected output. Expected: "+ prettify(src)+224" Got: " + prettify(destStr));225}226}catch(Exception e){227fail("Got unexpected exception: " + e.toString());228}229}230231public static void TestNamePrepConformance() throws Exception {232InputStream stream = StringPrep.class.getResourceAsStream("uidna.spp");233StringPrep namePrep = new StringPrep(stream);234stream.close();235int i;236for(i=0; i<TestData.conformanceTestCases.length;i++){237TestData.ConformanceTestCase testCase = TestData.conformanceTestCases[i];238try{239UCharacterIterator iter = UCharacterIterator.getInstance(testCase.input);240StringBuffer output = namePrep.prepare(iter, testCase.flags);241if(testCase.output !=null && output!=null && !testCase.output.equals(output.toString())){242fail("Did not get the expected output. Expected: " + prettify(testCase.output)+243" Got: "+ prettify(output.toString()) );244}245} catch(ParseException ex) {246if (testCase.expected == null) {247fail("get the unexpected exception for source: " +testCase.input +" Got: "+ ex.toString());248}249}250}251System.out.println("Nameprep test count: " + i);252}253254private static void fail(String msg) {255throw new RuntimeException(msg);256}257258private static String prettify(String s) {259StringBuffer result = new StringBuffer();260for (int i = 0; i < s.length(); ++i) {261char ch =s.charAt(i);262if(ch > 0x7f){263result.append("\\u");264result.append(hex(ch));265}else{266result.append(ch);267}268269}270return result.toString();271}272273private static String hex(char ch) {274StringBuffer result = new StringBuffer();275String foo = Integer.toString(ch,16).toUpperCase();276for (int i = foo.length(); i < 4; ++i) {277result.append('0');278}279return result + foo;280}281}282283284