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