Home | History | Annotate | Download | only in stringprep
      1 /*
      2  *******************************************************************************
      3  * Copyright (C) 2003-2009, International Business Machines Corporation and    *
      4  * others. All Rights Reserved.                                                *
      5  *******************************************************************************
      6 */
      7 package com.ibm.icu.dev.test.stringprep;
      8 
      9 import java.util.Locale;
     10 
     11 import com.ibm.icu.dev.test.TestFmwk;
     12 import com.ibm.icu.text.StringPrep;
     13 import com.ibm.icu.text.StringPrepParseException;
     14 
     15 /**
     16  * @author ram
     17  *
     18  */
     19 public class TestStringPrep extends TestFmwk {
     20     public static void main(String[] args) throws Exception {
     21         new TestStringPrep().run(args);
     22     }
     23     /*
     24        There are several special identifiers ("who") which need to be
     25        understood universally, rather than in the context of a particular
     26        DNS domain.  Some of these identifiers cannot be understood when an
     27        NFS client accesses the server, but have meaning when a local process
     28        accesses the file.  The ability to display and modify these
     29        permissions is permitted over NFS, even if none of the access methods
     30        on the server understands the identifiers.
     31 
     32         Who                    Description
     33        _______________________________________________________________
     34 
     35        "OWNER"                The owner of the file.
     36        "GROUP"                The group associated with the file.
     37        "EVERYONE"             The world.
     38        "INTERACTIVE"          Accessed from an interactive terminal.
     39        "NETWORK"              Accessed via the network.
     40        "DIALUP"               Accessed as a dialup user to the server.
     41        "BATCH"                Accessed from a batch job.
     42        "ANONYMOUS"            Accessed without any authentication.
     43        "AUTHENTICATED"        Any authenticated user (opposite of
     44                               ANONYMOUS)
     45        "SERVICE"              Access from a system service.
     46 
     47        To avoid conflict, these special identifiers are distinguish by an
     48        appended "@" and should appear in the form "xxxx@" (note: no domain
     49        name after the "@").  For example: ANONYMOUS@.
     50     */
     51     private String[] mixed_prep_data ={
     52         "OWNER@",
     53         "GROUP@",
     54         "EVERYONE@",
     55         "INTERACTIVE@",
     56         "NETWORK@",
     57         "DIALUP@",
     58         "BATCH@",
     59         "ANONYMOUS@",
     60         "AUTHENTICATED@",
     61         "\u0930\u094D\u092E\u094D\u0915\u094D\u0937\u0947\u0924\u094D (at) slip129-37-118-146.nc.us.ibm.net",
     62         "\u0936\u094d\u0930\u0940\u092e\u0926\u094d (at) saratoga.pe.utexas.edu",
     63         "\u092d\u0917\u0935\u0926\u094d\u0917\u0940\u0924\u093e (at) dial-120-45.ots.utexas.edu",
     64         "\u0905\u0927\u094d\u092f\u093e\u092f (at) woo-085.dorms.waller.net",
     65         "\u0905\u0930\u094d\u091c\u0941\u0928 (at) hd30-049.hil.compuserve.com",
     66         "\u0935\u093f\u0937\u093e\u0926 (at) pem203-31.pe.ttu.edu",
     67         "\u092f\u094b\u0917 (at) 56K-227.MaxTNT3.pdq.net",
     68         "\u0927\u0943\u0924\u0930\u093e\u0937\u094d\u091f\u094d\u0930 (at) dial-36-2.ots.utexas.edu",
     69         "\u0909\u0935\u093E\u091A\u0943 (at) slip129-37-23-152.ga.us.ibm.net",
     70         "\u0927\u0930\u094d\u092e\u0915\u094d\u0937\u0947\u0924\u094d\u0930\u0947 (at) ts45ip119.cadvision.com",
     71         "\u0915\u0941\u0930\u0941\u0915\u094d\u0937\u0947\u0924\u094d\u0930\u0947 (at) sdn-ts-004txaustP05.dialsprint.net",
     72         "\u0938\u092e\u0935\u0947\u0924\u093e (at) bar-tnt1s66.erols.com",
     73         "\u092f\u0941\u092f\u0941\u0924\u094d\u0938\u0935\u0903 (at) 101.st-louis-15.mo.dial-access.att.net",
     74         "\u092e\u093e\u092e\u0915\u093e\u0903 (at) h92-245.Arco.COM",
     75         "\u092a\u093e\u0923\u094d\u0921\u0935\u093e\u0936\u094d\u091a\u0948\u0935 (at) dial-13-2.ots.utexas.edu",
     76         "\u0915\u093f\u092e\u0915\u0941\u0930\u094d\u0935\u0924 (at) net-redynet29.datamarkets.com.ar",
     77         "\u0938\u0902\u091c\u0935 (at) ccs-shiva28.reacciun.net.ve",
     78         "\u0c30\u0c18\u0c41\u0c30\u0c3e\u0c2e\u0c4d (at) 7.houston-11.tx.dial-access.att.net",
     79         "\u0c35\u0c3f\u0c36\u0c4d\u0c35\u0c28\u0c3e\u0c27 (at) ingw129-37-120-26.mo.us.ibm.net",
     80         "\u0c06\u0c28\u0c02\u0c26\u0c4d (at) dialup6.austintx.com",
     81         "\u0C35\u0C26\u0C4D\u0C26\u0C3F\u0C30\u0C3E\u0C1C\u0C41 (at) dns2.tpao.gov.tr",
     82         "\u0c30\u0c3e\u0c1c\u0c40\u0c35\u0c4d (at) slip129-37-119-194.nc.us.ibm.net",
     83         "\u0c15\u0c36\u0c30\u0c2c\u0c3e\u0c26 (at) cs7.dillons.co.uk.203.119.193.in-addr.arpa",
     84         "\u0c38\u0c02\u0c1c\u0c40\u0c35\u0c4d (at) swprd1.innovplace.saskatoon.sk.ca",
     85         "\u0c15\u0c36\u0c30\u0c2c\u0c3e\u0c26 (at) bikini.bologna.maraut.it",
     86         "\u0c38\u0c02\u0c1c\u0c40\u0c2c\u0c4d (at) node91.subnet159-198-79.baxter.com",
     87         "\u0c38\u0c46\u0c28\u0c4d\u0c17\u0c41\u0c2a\u0c4d\u0c24 (at) cust19.max5.new-york.ny.ms.uu.net",
     88         "\u0c05\u0c2e\u0c30\u0c47\u0c02\u0c26\u0c4d\u0c30 (at) balexander.slip.andrew.cmu.edu",
     89         "\u0c39\u0c28\u0c41\u0c2e\u0c3e\u0c28\u0c41\u0c32 (at) pool029.max2.denver.co.dynip.alter.net",
     90         "\u0c30\u0c35\u0c3f (at) cust49.max9.new-york.ny.ms.uu.net",
     91         "\u0c15\u0c41\u0c2e\u0c3e\u0c30\u0c4d (at) s61.abq-dialin2.hollyberry.com",
     92         "\u0c35\u0c3f\u0c36\u0c4d\u0c35\u0c28\u0c3e\u0c27@\u0917\u0928\u0947\u0936.sanjose.ibm.com",
     93         "\u0c06\u0c26\u0c3f\u0c24\u0c4d\u0c2f@www.\u00E0\u00B3\u00AF.com",
     94         "\u0C15\u0C02\u0C26\u0C4D\u0C30\u0C47\u0C17\u0C41\u0c32@www.\u00C2\u00A4.com",
     95         "\u0c36\u0c4d\u0c30\u0c40\u0C27\u0C30\u0C4D@www.\u00C2\u00A3.com",
     96         "\u0c15\u0c02\u0c1f\u0c2e\u0c36\u0c46\u0c1f\u0c4d\u0c1f\u0c3f@\u0025",
     97         "\u0c2e\u0c3e\u0c27\u0c35\u0c4d@\u005C\u005C",
     98         "\u0c26\u0c46\u0c36\u0c46\u0c1f\u0c4d\u0c1f\u0c3f@www.\u0021.com",
     99         "test@www.\u0024.com",
    100         "help@\u00C3\u00BC.com",
    101     };
    102     public void TestNFS4MixedPrep(){
    103         for(int i=0; i< mixed_prep_data.length; i++){
    104             try{
    105                 String src = mixed_prep_data[i];
    106                 byte[] dest = NFS4StringPrep.mixed_prepare(src.getBytes("UTF-8"));
    107                 String destString = new String(dest, "UTF-8");
    108                 int destIndex = destString.indexOf('@');
    109                 if(destIndex < 0){
    110                     errln("Delimiter @ disappeared from the output!");
    111                 }
    112             }catch(Exception e){
    113                 errln("mixed_prepare for string: " + mixed_prep_data[i] +" failed with " + e.toString());
    114             }
    115         }
    116         /* test the error condition */
    117         {
    118             String src = "OWNER (at) oss.software.ibm.com";
    119             try{
    120                 byte[] dest = NFS4StringPrep.mixed_prepare(src.getBytes("UTF-8"));
    121                 if(dest!=null){
    122                     errln("Did not get the expected exception");
    123                 }
    124             }catch(Exception e){
    125                 logln("mixed_prepare for string: " + src +" passed with " + e.toString());
    126             }
    127 
    128          }
    129     }
    130     public void TestCISPrep(){
    131 
    132         for(int i=0;i< (TestData.conformanceTestCases.length);i++){
    133             TestData.ConformanceTestCase testCase = TestData.conformanceTestCases[i];
    134             String src = testCase.input;
    135             Exception expected = testCase.expected;
    136             String expectedDest = testCase.output;
    137             try{
    138                 byte[] dest =NFS4StringPrep.cis_prepare(src.getBytes("UTF-8"));
    139                 String destString = new String(dest, "UTF-8");
    140                 if(!expectedDest.equalsIgnoreCase(destString)){
    141                       errln("Did not get the expected output for nfs4_cis_prep at index " + i);
    142                 }
    143             }catch(Exception e){
    144                 if(!expected.equals(e)){
    145                     errln("Did not get the expected exception");
    146                 }
    147             }
    148 
    149         }
    150     }
    151 
    152     public void TestCSPrep(){
    153 
    154         // Checking for bidi is turned off
    155         String src = "\uC138\uACC4\uC758\uBAA8\uB4E0\uC0AC\uB78C\uB4E4\uC774\u0644\u064A\u0647\uD55C\uAD6D\uC5B4\uB97C\uC774\uD574\uD55C\uB2E4\uBA74";
    156         try{
    157             NFS4StringPrep.cs_prepare(src.getBytes("UTF-8"), false);
    158         }catch(Exception e){
    159             errln("Got unexpected exception: " + e.toString());
    160         }
    161 
    162         // normalization is turned off
    163         try{
    164             src = "www.\u00E0\u00B3\u00AF.com";
    165             byte[] dest = NFS4StringPrep.cs_prepare(src.getBytes("UTF-8"), false);
    166             String destStr = new String(dest, "UTF-8");
    167             if(!src.equals(destStr)){
    168                 errln("Did not get expected output. Expected: "+ prettify(src)+
    169                       " Got: " + prettify(destStr));
    170             }
    171         }catch(Exception e){
    172             errln("Got unexpected exception: " + e.toString());
    173         }
    174 
    175         // test case insensitive string
    176         try{
    177             src = "THISISATEST";
    178             byte[] dest = NFS4StringPrep.cs_prepare(src.getBytes("UTF-8"), false);
    179             String destStr = new String(dest, "UTF-8");
    180             if(!src.toLowerCase().equals(destStr)){
    181                 errln("Did not get expected output. Expected: "+ prettify(src)+
    182                       " Got: " + prettify(destStr));
    183             }
    184         }catch(Exception e){
    185             errln("Got unexpected exception: " + e.toString());
    186         }
    187         // test case sensitive string
    188         try{
    189             src = "THISISATEST";
    190             byte[] dest = NFS4StringPrep.cs_prepare(src.getBytes("UTF-8"), true);
    191             String destStr = new String(dest, "UTF-8");
    192             if(!src.equals(destStr)){
    193                 errln("Did not get expected output. Expected: "+ prettify(src)+
    194                       " Got: " + prettify(destStr));
    195             }
    196         }catch(Exception e){
    197             errln("Got unexpected exception: " + e.toString());
    198         }
    199     }
    200 
    201     public void TestCoverage(){
    202         if (new StringPrepParseException("coverage", 0, "", 0,0) == null){
    203             errln("Construct StringPrepParseException(String, int, String, int, int)");
    204         }
    205     }
    206 
    207     /* Tests the method public static StringPrep getInstance(int profile) */
    208     public void TestGetInstance(){
    209         // Tests when "if (profile < 0 || profile > MAX_PROFILE)" is true
    210         int[] neg_num_cases = {-100,-50,-10,-5,-2,-1};
    211         for(int i=0; i<neg_num_cases.length; i++){
    212             try{
    213                 StringPrep.getInstance(neg_num_cases[i]);
    214                 errln("StringPrep.getInstance(int) expected an exception for " +
    215                         "an invalid parameter of " + neg_num_cases[i]);
    216             } catch(Exception e){
    217             }
    218         }
    219 
    220         int[] max_profile_cases = {StringPrep.RFC4518_LDAP_CI+1, StringPrep.RFC4518_LDAP_CI+2, StringPrep.RFC4518_LDAP_CI+5, StringPrep.RFC4518_LDAP_CI+10};
    221         for(int i=0; i<max_profile_cases.length; i++){
    222             try{
    223                 StringPrep.getInstance(max_profile_cases[i]);
    224                 errln("StringPrep.getInstance(int) expected an exception for " +
    225                         "an invalid parameter of " + max_profile_cases[i]);
    226             } catch(Exception e){
    227             }
    228         }
    229 
    230         // Tests when "if (instance == null)", "if (stream != null)", "if (instance != null)", and "if (ref != null)" is true
    231         int[] cases = {0, 1, StringPrep.RFC4518_LDAP_CI};
    232         for(int i=0; i<cases.length; i++){
    233             try{
    234                 StringPrep.getInstance(cases[i]);
    235             } catch(Exception e){
    236                 errln("StringPrep.getInstance(int) did not expected an exception for " +
    237                         "an valid parameter of " + cases[i]);
    238             }
    239         }
    240     }
    241 
    242     /* Test the method public String prepare(String src, int options) */
    243     public void TestPrepare() {
    244         StringPrep sp = StringPrep.getInstance(0);
    245         try {
    246             if (!(sp.prepare("dummy", 0)).equals("dummy")) {
    247                 errln("StringPrep.prepare(String,int) was suppose to return " + "'dummy'");
    248             }
    249         } catch (Exception e) {
    250             errln("StringPrep.prepare(String,int) was not suppose to return " + "an exception.");
    251         }
    252     }
    253 
    254     /*
    255      * Tests the constructor public StringPrepParseException(String message, int error, String rules, int pos, int
    256      * lineNumber)
    257      */
    258     public void TestStringPrepParseException() {
    259         Locale locales[] = {Locale.US, Locale.FRENCH, Locale.SIMPLIFIED_CHINESE};
    260         String rules = "This is a very odd little set of rules, just for testing, you know...";
    261         StringPrepParseException exceptions[] = new StringPrepParseException[locales.length];
    262 
    263         for (int i = 0; i < locales.length; i += 1) {
    264             exceptions[i] = new StringPrepParseException(locales[i].toString(), i, rules, i, i);
    265         }
    266     }
    267 
    268     /* Tests the method public boolean equals(Object other) for StringPrepParseException */
    269     public void TestStringPrepParseExceptionEquals(){
    270         StringPrepParseException sppe = new StringPrepParseException("dummy",0,"dummy",0,0);
    271         StringPrepParseException sppe_clone = new StringPrepParseException("dummy",0,"dummy",0,0);
    272         StringPrepParseException sppe1 = new StringPrepParseException("dummy1",1,"dummy1",0,0);
    273 
    274         // Tests when "if(!(other instanceof StringPrepParseException))" is true
    275         if(sppe.equals(0)){
    276             errln("StringPrepParseException.equals(Object) is suppose to return false when " +
    277                     "passing integer '0'");
    278         }
    279         if(sppe.equals(0.0)){
    280             errln("StringPrepParseException.equals(Object) is suppose to return false when " +
    281                     "passing float/double '0.0'");
    282         }
    283         if(sppe.equals("0")){
    284             errln("StringPrepParseException.equals(Object) is suppose to return false when " +
    285                     "passing string '0'");
    286         }
    287 
    288         // Tests when "if(!(other instanceof StringPrepParseException))" is true
    289         if(!sppe.equals(sppe)){
    290             errln("StringPrepParseException.equals(Object) is suppose to return true when " +
    291             "comparing to the same object");
    292         }
    293         if(!sppe.equals(sppe_clone)){
    294             errln("StringPrepParseException.equals(Object) is suppose to return true when " +
    295             "comparing to the same initiated object");
    296         }
    297         if(sppe.equals(sppe1)){
    298             errln("StringPrepParseException.equals(Object) is suppose to return false when " +
    299             "comparing to another object that isn't the same");
    300         }
    301     }
    302 
    303     /* Tests the method public int getError() */
    304     public void TestGetError(){
    305         for(int i=0; i < 5; i++){
    306             StringPrepParseException sppe = new StringPrepParseException("dummy",i,"dummy",0,0);
    307             if(sppe.getError() != i){
    308                 errln("StringPrepParseExcpetion.getError() was suppose to return " + i + " but got " + sppe.getError());
    309             }
    310         }
    311     }
    312 
    313     /* Tests the private void setPreContext(char[] str, int pos) */
    314     public void TestSetPreContext(){
    315         String WordAtLeast16Characters = "abcdefghijklmnopqrstuvwxyz";
    316         for(int i=0; i < 5; i++){
    317             try{
    318                 @SuppressWarnings("unused")
    319                 StringPrepParseException sppe = new StringPrepParseException("dummy",i,WordAtLeast16Characters,0,0);
    320                 sppe = new StringPrepParseException(WordAtLeast16Characters,i,"dummy",0,0);
    321             } catch(Exception e){
    322                 errln("StringPrepParseException.setPreContext was not suppose to return an exception");
    323             }
    324         }
    325     }
    326 }
    327