1 /*
   2  * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @summary Unit test for sun.net.idn.Punycode
  27  * @bug 4737170
  28  * @library .
  29  * @compile -XDignore.symbol.file TestStringPrep.java NFS4StringPrep.java
  30  *     TestData.java
  31  * @run main/othervm -ea TestStringPrep
  32  * @author Edward Wang
  33  */
  34 /*
  35  *******************************************************************************
  36  * Copyright (C) 2003-2004, International Business Machines Corporation and    *
  37  * others. All Rights Reserved.                                                *
  38  *******************************************************************************
  39 */
  40 
  41 import java.text.ParseException;
  42 import java.io.InputStream;
  43 
  44 import sun.net.idn.StringPrep;
  45 import sun.text.normalizer.UCharacterIterator;
  46 
  47 public class TestStringPrep {
  48     public static void main(String[] args) throws Exception {
  49         TestNFS4MixedPrep();
  50         TestCISPrep();
  51         TestCSPrep();
  52         TestNamePrepConformance();
  53     }
  54     /*
  55        There are several special identifiers ("who") which need to be
  56        understood universally, rather than in the context of a particular
  57        DNS domain.  Some of these identifiers cannot be understood when an
  58        NFS client accesses the server, but have meaning when a local process
  59        accesses the file.  The ability to display and modify these
  60        permissions is permitted over NFS, even if none of the access methods
  61        on the server understands the identifiers.
  62 
  63         Who                    Description
  64        _______________________________________________________________
  65 
  66        "OWNER"                The owner of the file.
  67        "GROUP"                The group associated with the file.
  68        "EVERYONE"             The world.
  69        "INTERACTIVE"          Accessed from an interactive terminal.
  70        "NETWORK"              Accessed via the network.
  71        "DIALUP"               Accessed as a dialup user to the server.
  72        "BATCH"                Accessed from a batch job.
  73        "ANONYMOUS"            Accessed without any authentication.
  74        "AUTHENTICATED"        Any authenticated user (opposite of
  75                               ANONYMOUS)
  76        "SERVICE"              Access from a system service.
  77 
  78        To avoid conflict, these special identifiers are distinguish by an
  79        appended "@" and should appear in the form "xxxx@" (note: no domain
  80        name after the "@").  For example: ANONYMOUS@.
  81     */
  82     private static String[] mixed_prep_data ={
  83         "OWNER@",
  84         "GROUP@",
  85         "EVERYONE@",
  86         "INTERACTIVE@",
  87         "NETWORK@",
  88         "DIALUP@",
  89         "BATCH@",
  90         "ANONYMOUS@",
  91         "AUTHENTICATED@",
  92         "\u0930\u094D\u092E\u094D\u0915\u094D\u0937\u0947\u0924\u094D@slip129-37-118-146.nc.us.ibm.net",
  93         "\u0936\u094d\u0930\u0940\u092e\u0926\u094d@saratoga.pe.utexas.edu",
  94         "\u092d\u0917\u0935\u0926\u094d\u0917\u0940\u0924\u093e@dial-120-45.ots.utexas.edu",
  95         "\u0905\u0927\u094d\u092f\u093e\u092f@woo-085.dorms.waller.net",
  96         "\u0905\u0930\u094d\u091c\u0941\u0928@hd30-049.hil.compuserve.com",
  97         "\u0935\u093f\u0937\u093e\u0926@pem203-31.pe.ttu.edu",
  98         "\u092f\u094b\u0917@56K-227.MaxTNT3.pdq.net",
  99         "\u0927\u0943\u0924\u0930\u093e\u0937\u094d\u091f\u094d\u0930@dial-36-2.ots.utexas.edu",
 100         "\u0909\u0935\u093E\u091A\u0943@slip129-37-23-152.ga.us.ibm.net",
 101         "\u0927\u0930\u094d\u092e\u0915\u094d\u0937\u0947\u0924\u094d\u0930\u0947@ts45ip119.cadvision.com",
 102         "\u0915\u0941\u0930\u0941\u0915\u094d\u0937\u0947\u0924\u094d\u0930\u0947@sdn-ts-004txaustP05.dialsprint.net",
 103         "\u0938\u092e\u0935\u0947\u0924\u093e@bar-tnt1s66.erols.com",
 104         "\u092f\u0941\u092f\u0941\u0924\u094d\u0938\u0935\u0903@101.st-louis-15.mo.dial-access.att.net",
 105         "\u092e\u093e\u092e\u0915\u093e\u0903@h92-245.Arco.COM",
 106         "\u092a\u093e\u0923\u094d\u0921\u0935\u093e\u0936\u094d\u091a\u0948\u0935@dial-13-2.ots.utexas.edu",
 107         "\u0915\u093f\u092e\u0915\u0941\u0930\u094d\u0935\u0924@net-redynet29.datamarkets.com.ar",
 108         "\u0938\u0902\u091c\u0935@ccs-shiva28.reacciun.net.ve",
 109         "\u0c30\u0c18\u0c41\u0c30\u0c3e\u0c2e\u0c4d@7.houston-11.tx.dial-access.att.net",
 110         "\u0c35\u0c3f\u0c36\u0c4d\u0c35\u0c28\u0c3e\u0c27@ingw129-37-120-26.mo.us.ibm.net",
 111         "\u0c06\u0c28\u0c02\u0c26\u0c4d@dialup6.austintx.com",
 112         "\u0C35\u0C26\u0C4D\u0C26\u0C3F\u0C30\u0C3E\u0C1C\u0C41@dns2.tpao.gov.tr",
 113         "\u0c30\u0c3e\u0c1c\u0c40\u0c35\u0c4d@slip129-37-119-194.nc.us.ibm.net",
 114         "\u0c15\u0c36\u0c30\u0c2c\u0c3e\u0c26@cs7.dillons.co.uk.203.119.193.in-addr.arpa",
 115         "\u0c38\u0c02\u0c1c\u0c40\u0c35\u0c4d@swprd1.innovplace.saskatoon.sk.ca",
 116         "\u0c15\u0c36\u0c30\u0c2c\u0c3e\u0c26@bikini.bologna.maraut.it",
 117         "\u0c38\u0c02\u0c1c\u0c40\u0c2c\u0c4d@node91.subnet159-198-79.baxter.com",
 118         "\u0c38\u0c46\u0c28\u0c4d\u0c17\u0c41\u0c2a\u0c4d\u0c24@cust19.max5.new-york.ny.ms.uu.net",
 119         "\u0c05\u0c2e\u0c30\u0c47\u0c02\u0c26\u0c4d\u0c30@balexander.slip.andrew.cmu.edu",
 120         "\u0c39\u0c28\u0c41\u0c2e\u0c3e\u0c28\u0c41\u0c32@pool029.max2.denver.co.dynip.alter.net",
 121         "\u0c30\u0c35\u0c3f@cust49.max9.new-york.ny.ms.uu.net",
 122         "\u0c15\u0c41\u0c2e\u0c3e\u0c30\u0c4d@s61.abq-dialin2.hollyberry.com",
 123         "\u0c35\u0c3f\u0c36\u0c4d\u0c35\u0c28\u0c3e\u0c27@\u0917\u0928\u0947\u0936.sanjose.ibm.com",
 124         "\u0c06\u0c26\u0c3f\u0c24\u0c4d\u0c2f@www.\u00E0\u00B3\u00AF.com",
 125         "\u0C15\u0C02\u0C26\u0C4D\u0C30\u0C47\u0C17\u0C41\u0c32@www.\u00C2\u00A4.com",
 126         "\u0c36\u0c4d\u0c30\u0c40\u0C27\u0C30\u0C4D@www.\u00C2\u00A3.com",
 127         "\u0c15\u0c02\u0c1f\u0c2e\u0c36\u0c46\u0c1f\u0c4d\u0c1f\u0c3f@\u0025",
 128         "\u0c2e\u0c3e\u0c27\u0c35\u0c4d@\u005C\u005C",
 129         "\u0c26\u0c46\u0c36\u0c46\u0c1f\u0c4d\u0c1f\u0c3f@www.\u0021.com",
 130         "test@www.\u0024.com",
 131         "help@\u00C3\u00BC.com",
 132     };
 133     public static void TestNFS4MixedPrep(){
 134         for(int i=0; i< mixed_prep_data.length; i++){
 135             try{
 136                 String src = mixed_prep_data[i];
 137                 byte[] dest = NFS4StringPrep.mixed_prepare(src.getBytes("UTF-8"));
 138                 String destString = new String(dest, "UTF-8");
 139                 int destIndex = destString.indexOf('@');
 140                 if(destIndex < 0){
 141                     fail("Delimiter @ disappeared from the output!");
 142                 }
 143             }catch(Exception e){
 144                 fail("mixed_prepare for string: " + mixed_prep_data[i] +" failed with " + e.toString());
 145             }
 146         }
 147         /* test the error condition */
 148         {
 149             String src = "OWNER@oss.software.ibm.com";
 150             try{
 151                 byte[] dest = NFS4StringPrep.mixed_prepare(src.getBytes("UTF-8"));
 152                 if(dest!=null){
 153                     fail("Did not get the expected exception");
 154                 }
 155             }catch(Exception e){
 156                 System.out.println("mixed_prepare for string: " + src +" passed with " + e.toString());
 157             }
 158 
 159          }
 160     }
 161     public static void TestCISPrep(){
 162 
 163         for(int i=0;i< (TestData.conformanceTestCases.length);i++){
 164             TestData.ConformanceTestCase testCase = TestData.conformanceTestCases[i];
 165             String src = testCase.input;
 166             Exception expected = testCase.expected;
 167             String expectedDest = testCase.output;
 168             try{
 169                 byte[] dest =NFS4StringPrep.cis_prepare(src.getBytes("UTF-8"));
 170                 String destString = new String(dest, "UTF-8");
 171                 if(!expectedDest.equalsIgnoreCase(destString)){
 172                       fail("Did not get the expected output for nfs4_cis_prep at index " + i);
 173                 }
 174             }catch(Exception e){
 175                 if(!((expected instanceof ParseException) && (e instanceof ParseException))) {
 176                     fail("Did not get the expected exception");
 177                 }
 178             }
 179 
 180         }
 181     }
 182 
 183     public static void TestCSPrep(){
 184 
 185         // Checking for bidi is turned off
 186         String src = "\uC138\uACC4\uC758\uBAA8\uB4E0\uC0AC\uB78C\uB4E4\uC774\u0644\u064A\u0647\uD55C\uAD6D\uC5B4\uB97C\uC774\uD574\uD55C\uB2E4\uBA74";
 187         try{
 188             NFS4StringPrep.cs_prepare(src.getBytes("UTF-8"), false);
 189         }catch(Exception e){
 190             fail("Got unexpected exception: " + e.toString());
 191         }
 192 
 193         // normalization is turned off
 194         try{
 195             src = "www.\u00E0\u00B3\u00AF.com";
 196             byte[] dest = NFS4StringPrep.cs_prepare(src.getBytes("UTF-8"), false);
 197             String destStr = new String(dest, "UTF-8");
 198             if(!src.equals(destStr)){
 199                 fail("Did not get expected output. Expected: "+ prettify(src)+
 200                       " Got: " + prettify(destStr));
 201             }
 202         }catch(Exception e){
 203             fail("Got unexpected exception: " + e.toString());
 204         }
 205 
 206         // test case insensitive string
 207         try{
 208             src = "THISISATEST";
 209             byte[] dest = NFS4StringPrep.cs_prepare(src.getBytes("UTF-8"), false);
 210             String destStr = new String(dest, "UTF-8");
 211             if(!src.toLowerCase().equals(destStr)){
 212                 fail("Did not get expected output. Expected: "+ prettify(src)+
 213                       " Got: " + prettify(destStr));
 214             }
 215         }catch(Exception e){
 216             fail("Got unexpected exception: " + e.toString());
 217         }
 218         // test case sensitive string
 219         try{
 220             src = "THISISATEST";
 221             byte[] dest = NFS4StringPrep.cs_prepare(src.getBytes("UTF-8"), true);
 222             String destStr = new String(dest, "UTF-8");
 223             if(!src.equals(destStr)){
 224                 fail("Did not get expected output. Expected: "+ prettify(src)+
 225                       " Got: " + prettify(destStr));
 226             }
 227         }catch(Exception e){
 228             fail("Got unexpected exception: " + e.toString());
 229         }
 230     }
 231 
 232     public static void TestNamePrepConformance() throws Exception {
 233         InputStream stream = StringPrep.class.getResourceAsStream("uidna.spp");
 234         StringPrep namePrep = new StringPrep(stream);
 235         stream.close();
 236         int i;
 237         for(i=0; i<TestData.conformanceTestCases.length;i++){
 238             TestData.ConformanceTestCase testCase = TestData.conformanceTestCases[i];
 239             try{
 240                 UCharacterIterator iter = UCharacterIterator.getInstance(testCase.input);
 241                 StringBuffer output = namePrep.prepare(iter, testCase.flags);
 242                 if(testCase.output !=null && output!=null && !testCase.output.equals(output.toString())){
 243                     fail("Did not get the expected output. Expected: " + prettify(testCase.output)+
 244                             " Got: "+ prettify(output.toString()) );
 245                 }
 246             } catch(ParseException ex) {
 247                 if (testCase.expected == null) {
 248                     fail("get the unexpected exception for source: " +testCase.input +" Got:  "+ ex.toString());
 249                 }
 250             }
 251         }
 252         System.out.println("Nameprep test count: " + i);
 253     }
 254 
 255     private static void fail(String msg) {
 256         throw new RuntimeException(msg);
 257     }
 258 
 259     private static String prettify(String s) {
 260         StringBuffer result = new StringBuffer();
 261         for (int i = 0; i < s.length(); ++i) {
 262             char ch =s.charAt(i);
 263             if(ch > 0x7f){
 264                 result.append("\\u");
 265                 result.append(hex(ch));
 266             }else{
 267                 result.append(ch);
 268             }
 269 
 270         }
 271         return result.toString();
 272     }
 273 
 274     private static String hex(char ch) {
 275         StringBuffer result = new StringBuffer();
 276         String foo = Integer.toString(ch,16).toUpperCase();
 277         for (int i = foo.length(); i < 4; ++i) {
 278             result.append('0');
 279         }
 280         return result + foo;
 281     }
 282 }