Path: blob/master/test/jdk/sun/net/idn/TestStringPrep.java
41149 views
/*1* Copyright (c) 2005, 2020, 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 jdk.internal.icu.text.StringPrep26* @bug 4737170 8060097 817427027* @modules java.base/jdk.internal.icu.text28* java.base/sun.net.idn:open29* @library .30* @compile -XDignore.symbol.file TestStringPrep.java NFS4StringPrep.java31* TestData.java32* @run main/othervm -ea TestStringPrep33* @author Edward Wang34*/35/*36*******************************************************************************37* Copyright (C) 2003-2004, International Business Machines Corporation and *38* others. All Rights Reserved. *39*******************************************************************************40*/4142import java.text.ParseException;43import java.io.InputStream;44import java.util.Locale;4546import jdk.internal.icu.text.StringPrep;47import jdk.internal.icu.text.UCharacterIterator;4849public class TestStringPrep {50public static void main(String[] args) throws Exception {51TestNFS4MixedPrep();52TestCISPrep();53TestCSPrep();54TestNamePrepConformance();55}56/*57There are several special identifiers ("who") which need to be58understood universally, rather than in the context of a particular59DNS domain. Some of these identifiers cannot be understood when an60NFS client accesses the server, but have meaning when a local process61accesses the file. The ability to display and modify these62permissions is permitted over NFS, even if none of the access methods63on the server understands the identifiers.6465Who Description66_______________________________________________________________6768"OWNER" The owner of the file.69"GROUP" The group associated with the file.70"EVERYONE" The world.71"INTERACTIVE" Accessed from an interactive terminal.72"NETWORK" Accessed via the network.73"DIALUP" Accessed as a dialup user to the server.74"BATCH" Accessed from a batch job.75"ANONYMOUS" Accessed without any authentication.76"AUTHENTICATED" Any authenticated user (opposite of77ANONYMOUS)78"SERVICE" Access from a system service.7980To avoid conflict, these special identifiers are distinguish by an81appended "@" and should appear in the form "xxxx@" (note: no domain82name after the "@"). For example: ANONYMOUS@.83*/84private static String[] mixed_prep_data ={85"OWNER@",86"GROUP@",87"EVERYONE@",88"INTERACTIVE@",89"NETWORK@",90"DIALUP@",91"BATCH@",92"ANONYMOUS@",93"AUTHENTICATED@",94"\u0930\u094D\u092E\u094D\u0915\u094D\u0937\u0947\u0924\[email protected]",95"\u0936\u094d\u0930\u0940\u092e\u0926\[email protected]",96"\u092d\u0917\u0935\u0926\u094d\u0917\u0940\u0924\[email protected]",97"\u0905\u0927\u094d\u092f\u093e\[email protected]",98"\u0905\u0930\u094d\u091c\u0941\[email protected]",99"\u0935\u093f\u0937\u093e\[email protected]",100"\u092f\u094b\[email protected]",101"\u0927\u0943\u0924\u0930\u093e\u0937\u094d\u091f\u094d\[email protected]",102"\u0909\u0935\u093E\u091A\[email protected]",103"\u0927\u0930\u094d\u092e\u0915\u094d\u0937\u0947\u0924\u094d\u0930\[email protected]",104"\u0915\u0941\u0930\u0941\u0915\u094d\u0937\u0947\u0924\u094d\u0930\[email protected]",105"\u0938\u092e\u0935\u0947\u0924\[email protected]",106"\u092f\u0941\u092f\u0941\u0924\u094d\u0938\u0935\[email protected]",107"\u092e\u093e\u092e\u0915\u093e\[email protected]",108"\u092a\u093e\u0923\u094d\u0921\u0935\u093e\u0936\u094d\u091a\u0948\[email protected]",109"\u0915\u093f\u092e\u0915\u0941\u0930\u094d\u0935\[email protected]",110"\u0938\u0902\u091c\[email protected]",111"\u0c30\u0c18\u0c41\u0c30\u0c3e\u0c2e\[email protected]",112"\u0c35\u0c3f\u0c36\u0c4d\u0c35\u0c28\u0c3e\[email protected]",113"\u0c06\u0c28\u0c02\u0c26\[email protected]",114"\u0C35\u0C26\u0C4D\u0C26\u0C3F\u0C30\u0C3E\u0C1C\[email protected]",115"\u0c30\u0c3e\u0c1c\u0c40\u0c35\[email protected]",116"\u0c15\u0c36\u0c30\u0c2c\u0c3e\[email protected]",117"\u0c38\u0c02\u0c1c\u0c40\u0c35\[email protected]",118"\u0c15\u0c36\u0c30\u0c2c\u0c3e\[email protected]",119"\u0c38\u0c02\u0c1c\u0c40\u0c2c\[email protected]",120"\u0c38\u0c46\u0c28\u0c4d\u0c17\u0c41\u0c2a\u0c4d\[email protected]",121"\u0c05\u0c2e\u0c30\u0c47\u0c02\u0c26\u0c4d\[email protected]",122"\u0c39\u0c28\u0c41\u0c2e\u0c3e\u0c28\u0c41\[email protected]",123"\u0c30\u0c35\[email protected]",124"\u0c15\u0c41\u0c2e\u0c3e\u0c30\[email protected]",125"\u0c35\u0c3f\u0c36\u0c4d\u0c35\u0c28\u0c3e\u0c27@\u0917\u0928\u0947\u0936.sanjose.ibm.com",126"\u0c06\u0c26\u0c3f\u0c24\u0c4d\u0c2f@www.\u00E0\u00B3\u00AF.com",127"\u0C15\u0C02\u0C26\u0C4D\u0C30\u0C47\u0C17\u0C41\u0c32@www.\u00C2\u00A4.com",128"\u0c36\u0c4d\u0c30\u0c40\u0C27\u0C30\u0C4D@www.\u00C2\u00A3.com",129"\u0c15\u0c02\u0c1f\u0c2e\u0c36\u0c46\u0c1f\u0c4d\u0c1f\u0c3f@\u0025",130"\u0c2e\u0c3e\u0c27\u0c35\u0c4d@\u005C\u005C",131"\u0c26\u0c46\u0c36\u0c46\u0c1f\u0c4d\u0c1f\u0c3f@www.\u0021.com",132"test@www.\u0024.com",133"help@\u00C3\u00BC.com",134};135public static void TestNFS4MixedPrep(){136for(int i=0; i< mixed_prep_data.length; i++){137try{138String src = mixed_prep_data[i];139byte[] dest = NFS4StringPrep.mixed_prepare(src.getBytes("UTF-8"));140String destString = new String(dest, "UTF-8");141int destIndex = destString.indexOf('@');142if(destIndex < 0){143fail("Delimiter @ disappeared from the output!");144}145}catch(Exception e){146fail("mixed_prepare for string: " + mixed_prep_data[i] +" failed with " + e.toString());147}148}149/* test the error condition */150{151String src = "[email protected]";152try{153byte[] dest = NFS4StringPrep.mixed_prepare(src.getBytes("UTF-8"));154if(dest!=null){155fail("Did not get the expected exception");156}157}catch(Exception e){158System.out.println("mixed_prepare for string: " + src +" passed with " + e.toString());159}160161}162}163public static void TestCISPrep(){164165for(int i=0;i< (TestData.conformanceTestCases.length);i++){166TestData.ConformanceTestCase testCase = TestData.conformanceTestCases[i];167String src = testCase.input;168Exception expected = testCase.expected;169String expectedDest = testCase.output;170try{171byte[] dest =NFS4StringPrep.cis_prepare(src.getBytes("UTF-8"));172String destString = new String(dest, "UTF-8");173if(!expectedDest.equalsIgnoreCase(destString)){174fail("Did not get the expected output for nfs4_cis_prep at index " + i);175}176}catch(Exception e){177if(!((expected instanceof ParseException) && (e instanceof ParseException))) {178fail("Did not get the expected exception");179}180}181182}183}184185public static void TestCSPrep(){186187// Checking for bidi is turned off188String src = "\uC138\uACC4\uC758\uBAA8\uB4E0\uC0AC\uB78C\uB4E4\uC774\u0644\u064A\u0647\uD55C\uAD6D\uC5B4\uB97C\uC774\uD574\uD55C\uB2E4\uBA74";189try{190NFS4StringPrep.cs_prepare(src.getBytes("UTF-8"), false);191}catch(Exception e){192fail("Got unexpected exception: " + e.toString());193}194195// normalization is turned off196try{197src = "www.\u00E0\u00B3\u00AF.com";198byte[] dest = NFS4StringPrep.cs_prepare(src.getBytes("UTF-8"), false);199String destStr = new String(dest, "UTF-8");200if(!src.equals(destStr)){201fail("Did not get expected output. Expected: "+ prettify(src)+202" Got: " + prettify(destStr));203}204}catch(Exception e){205fail("Got unexpected exception: " + e.toString());206}207208// test case insensitive string209try{210src = "THISISATEST";211byte[] dest = NFS4StringPrep.cs_prepare(src.getBytes("UTF-8"), false);212String destStr = new String(dest, "UTF-8");213if(!src.toLowerCase(Locale.ROOT).equals(destStr)){214fail("Did not get expected output. Expected: "+ prettify(src)+215" Got: " + prettify(destStr));216}217}catch(Exception e){218fail("Got unexpected exception: " + e.toString());219}220// test case sensitive string221try{222src = "THISISATEST";223byte[] dest = NFS4StringPrep.cs_prepare(src.getBytes("UTF-8"), true);224String destStr = new String(dest, "UTF-8");225if(!src.equals(destStr)){226fail("Did not get expected output. Expected: "+ prettify(src)+227" Got: " + prettify(destStr));228}229}catch(Exception e){230fail("Got unexpected exception: " + e.toString());231}232}233234public static void TestNamePrepConformance() throws Exception {235InputStream stream = StringPrep.class.getModule()236.getResourceAsStream("sun/net/idn/uidna.spp");237StringPrep namePrep = new StringPrep(stream);238stream.close();239int i;240for(i=0; i<TestData.conformanceTestCases.length;i++){241TestData.ConformanceTestCase testCase = TestData.conformanceTestCases[i];242try{243UCharacterIterator iter = UCharacterIterator.getInstance(testCase.input);244StringBuffer output = namePrep.prepare(iter, testCase.flags);245if(testCase.output !=null && output!=null && !testCase.output.equals(output.toString())){246fail("Did not get the expected output. Expected: " + prettify(testCase.output)+247" Got: "+ prettify(output.toString()) );248}249} catch(ParseException ex) {250if (testCase.expected == null) {251fail("get the unexpected exception for source: " +testCase.input +" Got: "+ ex.toString());252}253}254}255System.out.println("Nameprep test count: " + i);256}257258private static void fail(String msg) {259throw new RuntimeException(msg);260}261262private static String prettify(String s) {263StringBuffer result = new StringBuffer();264for (int i = 0; i < s.length(); ++i) {265char ch =s.charAt(i);266if(ch > 0x7f){267result.append("\\u");268result.append(hex(ch));269}else{270result.append(ch);271}272273}274return result.toString();275}276277private static String hex(char ch) {278StringBuffer result = new StringBuffer();279String foo = Integer.toString(ch,16).toUpperCase(Locale.ROOT);280for (int i = foo.length(); i < 4; ++i) {281result.append('0');282}283return result + foo;284}285}286287288