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