• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4  *******************************************************************************
5  *
6  *   Copyright (C) 2003-2016, International Business Machines
7  *   Corporation and others.  All Rights Reserved.
8  *
9  *******************************************************************************
10  *   file name:  spreptst.c
11  *   encoding:   UTF-8
12  *   tab size:   8 (not used)
13  *   indentation:4
14  *
15  *   created on: 2003jul11
16  *   created by: Ram Viswanadha
17  */
18 #include <stdbool.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include "unicode/utypes.h"
22 
23 #if !UCONFIG_NO_IDNA
24 
25 #include "unicode/ustring.h"
26 #include "unicode/usprep.h"
27 #include "cstring.h"
28 #include "cintltst.h"
29 #include "cmemory.h"
30 #include "nfsprep.h"
31 
32 void addUStringPrepTest(TestNode** root);
33 void doStringPrepTest(const char* binFileName, const char* txtFileName,
34                  int32_t options, UErrorCode* errorCode);
35 
36 static void Test_nfs4_cs_prep_data(void);
37 static void Test_nfs4_cis_prep_data(void);
38 static void Test_nfs4_mixed_prep_data(void);
39 static void Test_nfs4_cs_prep(void);
40 static void Test_nfs4_cis_prep(void);
41 static void Test_nfs4_mixed_prep(void);
42 static void TestBEAMWarning(void);
43 static void TestCoverage(void);
44 static void TestStringPrepProfiles(void);
45 
46 UStringPrepProfileType getTypeFromProfileName(const char* profileName);
47 
48 void
addUStringPrepTest(TestNode ** root)49 addUStringPrepTest(TestNode** root)
50 {
51 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
52    addTest(root, &Test_nfs4_cs_prep_data,    "spreptst/Test_nfs4_cs_prep_data");
53    addTest(root, &Test_nfs4_cis_prep_data,   "spreptst/Test_nfs4_cis_prep_data");
54    addTest(root, &Test_nfs4_mixed_prep_data, "spreptst/Test_nfs4_mixed_prep_data");
55    addTest(root, &Test_nfs4_cs_prep,         "spreptst/Test_nfs4_cs_prep");
56    addTest(root, &Test_nfs4_cis_prep,        "spreptst/Test_nfs4_cis_prep");
57    addTest(root, &Test_nfs4_mixed_prep,      "spreptst/Test_nfs4_mixed_prep");
58    addTest(root, &TestBEAMWarning,           "spreptst/TestBEAMWarning");
59 #endif
60    addTest(root, &TestCoverage,              "spreptst/TestCoverage");
61    addTest(root, &TestStringPrepProfiles,              "spreptst/TestStringPrepProfiles");
62 }
63 
64 static void
Test_nfs4_cs_prep_data(void)65 Test_nfs4_cs_prep_data(void){
66     UErrorCode errorCode = U_ZERO_ERROR;
67     loadTestData(&errorCode);
68     if(U_FAILURE(errorCode)) {
69         log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(errorCode));
70         return;
71     }
72 
73     log_verbose("Testing nfs4_cs_prep_ci.txt\n");
74     doStringPrepTest("nfscsi","nfs4_cs_prep_ci.txt", USPREP_DEFAULT, &errorCode);
75 
76     log_verbose("Testing nfs4_cs_prep_cs.txt\n");
77     errorCode = U_ZERO_ERROR;
78     doStringPrepTest("nfscss","nfs4_cs_prep_cs.txt", USPREP_DEFAULT, &errorCode);
79 
80 
81 }
82 static void
Test_nfs4_cis_prep_data(void)83 Test_nfs4_cis_prep_data(void){
84     UErrorCode errorCode = U_ZERO_ERROR;
85     log_verbose("Testing nfs4_cis_prep.txt\n");
86     doStringPrepTest("nfscis","nfs4_cis_prep.txt", USPREP_DEFAULT, &errorCode);
87 }
88 static void
Test_nfs4_mixed_prep_data(void)89 Test_nfs4_mixed_prep_data(void){
90     UErrorCode errorCode = U_ZERO_ERROR;
91     loadTestData(&errorCode);
92     if(U_FAILURE(errorCode)) {
93         log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(errorCode));
94         return;
95     }
96 
97     log_verbose("Testing nfs4_mixed_prep_s.txt\n");
98     doStringPrepTest("nfsmxs","nfs4_mixed_prep_s.txt", USPREP_DEFAULT, &errorCode);
99 
100     errorCode = U_ZERO_ERROR;
101     log_verbose("Testing nfs4_mixed_prep_p.txt\n");
102     doStringPrepTest("nfsmxp","nfs4_mixed_prep_p.txt", USPREP_DEFAULT, &errorCode);
103 
104 }
105 
106 static const struct ConformanceTestCases
107    {
108      const char *comment;
109      const char *in;
110      const char *out;
111      const char *profile;
112      UErrorCode expectedStatus;
113    }
114    conformanceTestCases[] =
115    {
116 
117      {/*0*/
118        "Case folding ASCII U+0043 U+0041 U+0046 U+0045",
119        "\x43\x41\x46\x45", "\x63\x61\x66\x65",
120        "nfs4_cis_prep",
121        U_ZERO_ERROR
122 
123      },
124      {/*1*/
125        "Case folding 8bit U+00DF (german sharp s)",
126        "\xC3\x9F", "\x73\x73",
127        "nfs4_cis_prep",
128        U_ZERO_ERROR
129      },
130      {/*2*/
131        "Non-ASCII multibyte space character U+1680",
132        "\xE1\x9A\x80", NULL,
133        "nfs4_cis_prep",
134        U_STRINGPREP_PROHIBITED_ERROR
135      },
136      {/*3*/
137        "Non-ASCII 8bit control character U+0085",
138        "\xC2\x85", NULL,
139        "nfs4_cis_prep",
140        U_STRINGPREP_PROHIBITED_ERROR
141      },
142      {/*4*/
143        "Non-ASCII multibyte control character U+180E",
144        "\xE1\xA0\x8E", NULL,
145        "nfs4_cis_prep",
146        U_STRINGPREP_PROHIBITED_ERROR
147      },
148      {/*5*/
149        "Non-ASCII control character U+1D175",
150        "\xF0\x9D\x85\xB5", NULL,
151        "nfs4_cis_prep",
152        U_STRINGPREP_PROHIBITED_ERROR
153      },
154      {/*6*/
155        "Plane 0 private use character U+F123",
156        "\xEF\x84\xA3", NULL,
157        "nfs4_cis_prep",
158        U_STRINGPREP_PROHIBITED_ERROR
159      },
160      {/*7*/
161        "Plane 15 private use character U+F1234",
162        "\xF3\xB1\x88\xB4", NULL,
163        "nfs4_cis_prep",
164        U_STRINGPREP_PROHIBITED_ERROR
165      },
166      {/*8*/
167        "Plane 16 private use character U+10F234",
168        "\xF4\x8F\x88\xB4", NULL,
169        "nfs4_cis_prep",
170        U_STRINGPREP_PROHIBITED_ERROR
171      },
172      {/*9*/
173        "Non-character code point U+8FFFE",
174        "\xF2\x8F\xBF\xBE", NULL,
175        "nfs4_cis_prep",
176        U_STRINGPREP_PROHIBITED_ERROR
177      },
178      {/*10*/
179        "Non-character code point U+10FFFF",
180        "\xF4\x8F\xBF\xBF", NULL,
181        "nfs4_cis_prep",
182        U_STRINGPREP_PROHIBITED_ERROR
183      },
184  /*
185      {
186        "Surrogate code U+DF42",
187        "\xED\xBD\x82", NULL, "nfs4_cis_prep", UIDNA_DEFAULT,
188        U_STRINGPREP_PROHIBITED_ERROR
189      },
190 */
191      {/*11*/
192        "Non-plain text character U+FFFD",
193        "\xEF\xBF\xBD", NULL,
194        "nfs4_cis_prep",
195        U_STRINGPREP_PROHIBITED_ERROR
196      },
197      {/*12*/
198        "Ideographic description character U+2FF5",
199        "\xE2\xBF\xB5", NULL,
200        "nfs4_cis_prep",
201        U_STRINGPREP_PROHIBITED_ERROR
202      },
203      {/*13*/
204        "Display property character U+0341",
205        "\xCD\x81", "\xCC\x81",
206        "nfs4_cis_prep", U_ZERO_ERROR
207 
208      },
209 
210      {/*14*/
211        "Left-to-right mark U+200E",
212        "\xE2\x80\x8E", "\xCC\x81",
213        "nfs4_cis_prep",
214        U_STRINGPREP_PROHIBITED_ERROR
215      },
216      {/*15*/
217 
218        "Deprecated U+202A",
219        "\xE2\x80\xAA", "\xCC\x81",
220        "nfs4_cis_prep",
221        U_STRINGPREP_PROHIBITED_ERROR
222      },
223      {/*16*/
224        "Language tagging character U+E0001",
225        "\xF3\xA0\x80\x81", "\xCC\x81",
226        "nfs4_cis_prep",
227        U_STRINGPREP_PROHIBITED_ERROR
228      },
229      {/*17*/
230        "Language tagging character U+E0042",
231        "\xF3\xA0\x81\x82", NULL,
232        "nfs4_cis_prep",
233        U_STRINGPREP_PROHIBITED_ERROR
234      },
235      {/*18*/
236        "Bidi: RandALCat character U+05BE and LCat characters",
237        "\x66\x6F\x6F\xD6\xBE\x62\x61\x72", NULL,
238        "nfs4_cis_prep",
239        U_STRINGPREP_CHECK_BIDI_ERROR
240      },
241      {/*19*/
242        "Bidi: RandALCat character U+FD50 and LCat characters",
243        "\x66\x6F\x6F\xEF\xB5\x90\x62\x61\x72", NULL,
244        "nfs4_cis_prep",
245        U_STRINGPREP_CHECK_BIDI_ERROR
246      },
247      {/*20*/
248        "Bidi: RandALCat character U+FB38 and LCat characters",
249        "\x66\x6F\x6F\xEF\xB9\xB6\x62\x61\x72", "\x66\x6F\x6F\x20\xd9\x8e\x62\x61\x72",
250        "nfs4_cis_prep",
251        U_ZERO_ERROR
252      },
253      {/*21*/
254        "Bidi: RandALCat without trailing RandALCat U+0627 U+0031",
255        "\xD8\xA7\x31", NULL,
256        "nfs4_cis_prep",
257        U_STRINGPREP_CHECK_BIDI_ERROR
258      },
259      {/*22*/
260        "Bidi: RandALCat character U+0627 U+0031 U+0628",
261        "\xD8\xA7\x31\xD8\xA8", "\xD8\xA7\x31\xD8\xA8",
262        "nfs4_cis_prep",
263        U_ZERO_ERROR
264      },
265      {/*23*/
266        "Unassigned code point U+E0002",
267        "\xF3\xA0\x80\x82", NULL,
268        "nfs4_cis_prep",
269        U_STRINGPREP_UNASSIGNED_ERROR
270      },
271 
272 /*  // Invalid UTF-8
273      {
274        "Larger test (shrinking)",
275        "X\xC2\xAD\xC3\xDF\xC4\xB0\xE2\x84\xA1\x6a\xcc\x8c\xc2\xa0\xc2"
276        "\xaa\xce\xb0\xe2\x80\x80", "xssi\xcc\x87""tel\xc7\xb0 a\xce\xb0 ",
277        "nfs4_cis_prep",
278         U_ZERO_ERROR
279      },
280     {
281 
282        "Larger test (expanding)",
283        "X\xC3\xDF\xe3\x8c\x96\xC4\xB0\xE2\x84\xA1\xE2\x92\x9F\xE3\x8c\x80",
284        "xss\xe3\x82\xad\xe3\x83\xad\xe3\x83\xa1\xe3\x83\xbc\xe3\x83\x88"
285        "\xe3\x83\xab""i\xcc\x87""tel\x28""d\x29\xe3\x82\xa2\xe3\x83\x91"
286        "\xe3\x83\xbc\xe3\x83\x88"
287        "nfs4_cis_prep",
288         U_ZERO_ERROR
289      },
290   */
291 };
292 
293 #define MAX_BUFFER_SIZE  1000
294 
295 static int32_t
unescapeData(const char * src,int32_t srcLen,char * dest,int32_t destCapacity,UErrorCode * status)296 unescapeData(const char* src, int32_t srcLen,
297              char* dest, int32_t destCapacity,
298              UErrorCode* status){
299     (void)srcLen; // suppress compiler warnings about unused variable
300 
301     UChar b1Stack[MAX_BUFFER_SIZE];
302     int32_t b1Capacity = MAX_BUFFER_SIZE,
303             b1Len      = 0,
304             destLen    = 0;
305 
306     UChar* b1 = b1Stack;
307 
308     b1Len = u_unescape(src,b1,b1Capacity);
309 
310     u_strToUTF8(dest, destCapacity, &destLen, b1, b1Len, status);
311 
312     return destLen;
313 }
314 
315 
Test_nfs4_cis_prep(void)316 static void Test_nfs4_cis_prep(void){
317     int32_t i=0;
318     UErrorCode loadStatus = U_ZERO_ERROR;
319     loadTestData(&loadStatus);
320     if (U_FAILURE(loadStatus)) {
321         log_data_err("Test could not initialize. Got %s\n", u_errorName(loadStatus));
322         return;
323     }
324 
325     for(i=0;i< UPRV_LENGTHOF(conformanceTestCases);i++){
326         const char* src = conformanceTestCases[i].in;
327         UErrorCode status = U_ZERO_ERROR;
328         UParseError parseError;
329         UErrorCode expectedStatus = conformanceTestCases[i].expectedStatus;
330         const char* expectedDest = conformanceTestCases[i].out;
331         char* dest = NULL;
332         int32_t destLen = 0;
333 
334         destLen = nfs4_cis_prepare(src , (int32_t)strlen(src), dest, destLen, &parseError, &status);
335         if(status == U_BUFFER_OVERFLOW_ERROR){
336             status = U_ZERO_ERROR;
337             dest = (char*) malloc(++destLen);
338             destLen = nfs4_cis_prepare( src , (int32_t)strlen(src), dest, destLen, &parseError, &status);
339         }
340 
341         if(expectedStatus != status){
342             log_data_err("Did not get the expected status for nfs4_cis_prep at index %i. Expected: %s Got: %s - (Are you missing data?)\n",i, u_errorName(expectedStatus), u_errorName(status));
343         }
344         if(U_SUCCESS(status) && (strcmp(expectedDest,dest) !=0)){
345               log_err("Did not get the expected output for nfs4_cis_prep at index %i.\n", i);
346         }
347         free(dest);
348     }
349 }
350 
351 
352 
353 /*
354    There are several special identifiers ("who") which need to be
355    understood universally, rather than in the context of a particular
356    DNS domain.  Some of these identifiers cannot be understood when an
357    NFS client accesses the server, but have meaning when a local process
358    accesses the file.  The ability to display and modify these
359    permissions is permitted over NFS, even if none of the access methods
360    on the server understands the identifiers.
361 
362     Who                    Description
363    _______________________________________________________________
364 
365    "OWNER"                The owner of the file.
366    "GROUP"                The group associated with the file.
367    "EVERYONE"             The world.
368    "INTERACTIVE"          Accessed from an interactive terminal.
369    "NETWORK"              Accessed via the network.
370    "DIALUP"               Accessed as a dialup user to the server.
371    "BATCH"                Accessed from a batch job.
372    "ANONYMOUS"            Accessed without any authentication.
373    "AUTHENTICATED"        Any authenticated user (opposite of
374                           ANONYMOUS)
375    "SERVICE"              Access from a system service.
376 
377    To avoid conflict, these special identifiers are distinguish by an
378    appended "@" and should appear in the form "xxxx@" (note: no domain
379    name after the "@").  For example: ANONYMOUS@.
380 */
381 static const char* mixed_prep_data[] ={
382     "OWNER@",
383     "GROUP@",
384     "EVERYONE@",
385     "INTERACTIVE@",
386     "NETWORK@",
387     "DIALUP@",
388     "BATCH@",
389     "ANONYMOUS@",
390     "AUTHENTICATED@",
391     "\\u0930\\u094D\\u092E\\u094D\\u0915\\u094D\\u0937\\u0947\\u0924\\u094D@slip129-37-118-146.nc.us.ibm.net",
392     "\\u0936\\u094d\\u0930\\u0940\\u092e\\u0926\\u094d@saratoga.pe.utexas.edu",
393     "\\u092d\\u0917\\u0935\\u0926\\u094d\\u0917\\u0940\\u0924\\u093e@dial-120-45.ots.utexas.edu",
394     "\\u0905\\u0927\\u094d\\u092f\\u093e\\u092f@woo-085.dorms.waller.net",
395     "\\u0905\\u0930\\u094d\\u091c\\u0941\\u0928@hd30-049.hil.compuserve.com",
396     "\\u0935\\u093f\\u0937\\u093e\\u0926@pem203-31.pe.ttu.edu",
397     "\\u092f\\u094b\\u0917@56K-227.MaxTNT3.pdq.net",
398     "\\u0927\\u0943\\u0924\\u0930\\u093e\\u0937\\u094d\\u091f\\u094d\\u0930@dial-36-2.ots.utexas.edu",
399     "\\u0909\\u0935\\u093E\\u091A\\u0943@slip129-37-23-152.ga.us.ibm.net",
400     "\\u0927\\u0930\\u094d\\u092e\\u0915\\u094d\\u0937\\u0947\\u0924\\u094d\\u0930\\u0947@ts45ip119.cadvision.com",
401     "\\u0915\\u0941\\u0930\\u0941\\u0915\\u094d\\u0937\\u0947\\u0924\\u094d\\u0930\\u0947@sdn-ts-004txaustP05.dialsprint.net",
402     "\\u0938\\u092e\\u0935\\u0947\\u0924\\u093e@bar-tnt1s66.erols.com",
403     "\\u092f\\u0941\\u092f\\u0941\\u0924\\u094d\\u0938\\u0935\\u0903@101.st-louis-15.mo.dial-access.att.net",
404     "\\u092e\\u093e\\u092e\\u0915\\u093e\\u0903@h92-245.Arco.COM",
405     "\\u092a\\u093e\\u0923\\u094d\\u0921\\u0935\\u093e\\u0936\\u094d\\u091a\\u0948\\u0935@dial-13-2.ots.utexas.edu",
406     "\\u0915\\u093f\\u092e\\u0915\\u0941\\u0930\\u094d\\u0935\\u0924@net-redynet29.datamarkets.com.ar",
407     "\\u0938\\u0902\\u091c\\u0935@ccs-shiva28.reacciun.net.ve",
408     "\\u0c30\\u0c18\\u0c41\\u0c30\\u0c3e\\u0c2e\\u0c4d@7.houston-11.tx.dial-access.att.net",
409     "\\u0c35\\u0c3f\\u0c36\\u0c4d\\u0c35\\u0c28\\u0c3e\\u0c27@ingw129-37-120-26.mo.us.ibm.net",
410     "\\u0c06\\u0c28\\u0c02\\u0c26\\u0c4d@dialup6.austintx.com",
411     "\\u0C35\\u0C26\\u0C4D\\u0C26\\u0C3F\\u0C30\\u0C3E\\u0C1C\\u0C41@dns2.tpao.gov.tr",
412     "\\u0c30\\u0c3e\\u0c1c\\u0c40\\u0c35\\u0c4d@slip129-37-119-194.nc.us.ibm.net",
413     "\\u0c15\\u0c36\\u0c30\\u0c2c\\u0c3e\\u0c26@cs7.dillons.co.uk.203.119.193.in-addr.arpa",
414     "\\u0c38\\u0c02\\u0c1c\\u0c40\\u0c35\\u0c4d@swprd1.innovplace.saskatoon.sk.ca",
415     "\\u0c15\\u0c36\\u0c30\\u0c2c\\u0c3e\\u0c26@bikini.bologna.maraut.it",
416     "\\u0c38\\u0c02\\u0c1c\\u0c40\\u0c2c\\u0c4d@node91.subnet159-198-79.baxter.com",
417     "\\u0c38\\u0c46\\u0c28\\u0c4d\\u0c17\\u0c41\\u0c2a\\u0c4d\\u0c24@cust19.max5.new-york.ny.ms.uu.net",
418     "\\u0c05\\u0c2e\\u0c30\\u0c47\\u0c02\\u0c26\\u0c4d\\u0c30@balexander.slip.andrew.cmu.edu",
419     "\\u0c39\\u0c28\\u0c41\\u0c2e\\u0c3e\\u0c28\\u0c41\\u0c32@pool029.max2.denver.co.dynip.alter.net",
420     "\\u0c30\\u0c35\\u0c3f@cust49.max9.new-york.ny.ms.uu.net",
421     "\\u0c15\\u0c41\\u0c2e\\u0c3e\\u0c30\\u0c4d@s61.abq-dialin2.hollyberry.com",
422     "\\u0c35\\u0c3f\\u0c36\\u0c4d\\u0c35\\u0c28\\u0c3e\\u0c27@\\u0917\\u0928\\u0947\\u0936.sanjose.ibm.com",
423     "\\u0c06\\u0c26\\u0c3f\\u0c24\\u0c4d\\u0c2f@www.\\u00E0\\u00B3\\u00AF.com",
424     "\\u0C15\\u0C02\\u0C26\\u0C4D\\u0C30\\u0C47\\u0C17\\u0C41\\u0c32@www.\\u00C2\\u00A4.com",
425     "\\u0c36\\u0c4d\\u0c30\\u0c40\\u0C27\\u0C30\\u0C4D@www.\\u00C2\\u00A3.com",
426     "\\u0c15\\u0c02\\u0c1f\\u0c2e\\u0c36\\u0c46\\u0c1f\\u0c4d\\u0c1f\\u0c3f@\\u0025",
427     "\\u0c2e\\u0c3e\\u0c27\\u0c35\\u0c4d@\\u005C\\u005C",
428     "\\u0c26\\u0c46\\u0c36\\u0c46\\u0c1f\\u0c4d\\u0c1f\\u0c3f@www.\\u0021.com",
429     "test@www.\\u0024.com",
430     "help@\\u00C3\\u00BC.com",
431 
432 };
433 
434 
435 static void
Test_nfs4_mixed_prep(void)436 Test_nfs4_mixed_prep(void){
437     UErrorCode loadStatus = U_ZERO_ERROR;
438     loadTestData(&loadStatus);
439     if (U_FAILURE(loadStatus)) {
440         log_data_err("Test could not initialize. Got %s\n", u_errorName(loadStatus));
441         return;
442     }
443 
444     {
445         int32_t i=0;
446         char src[MAX_BUFFER_SIZE];
447         int32_t srcLen;
448 
449         for(i=0; i< UPRV_LENGTHOF(mixed_prep_data); i++){
450             int32_t destLen=0;
451             char* dest = NULL;
452             UErrorCode status = U_ZERO_ERROR;
453             UParseError parseError;
454             srcLen = unescapeData(mixed_prep_data[i], (int32_t)strlen(mixed_prep_data[i]), src, MAX_BUFFER_SIZE, &status);
455             if(U_FAILURE(status)){
456                 log_err("Conversion of data at index %i failed. Error: %s\n", i, u_errorName(status));
457                 continue;
458             }
459             destLen = nfs4_mixed_prepare(src, srcLen, NULL, 0, &parseError, &status);
460             if(status == U_BUFFER_OVERFLOW_ERROR){
461                 status = U_ZERO_ERROR;
462                 dest = (char*)malloc(++destLen);
463                 destLen = nfs4_mixed_prepare(src, srcLen, dest, destLen, &parseError, &status);
464             }
465             free(dest);
466             if(U_FAILURE(status)){
467                 log_data_err("Preparation of string at index %i failed. Error: %s - (Are you missing data?)\n", i, u_errorName(status));
468                 continue;
469             }
470         }
471     }
472     /* test the error condition */
473     {
474         const char* source = "OWNER@oss.software.ibm.com";
475         char dest[MAX_BUFFER_SIZE];
476         char src[MAX_BUFFER_SIZE] = {0};
477         UErrorCode status = U_ZERO_ERROR;
478         UParseError parseError;
479 
480         int32_t srcLen = unescapeData(source, (int32_t)strlen(source), src, MAX_BUFFER_SIZE, &status);
481 
482         nfs4_mixed_prepare(src, srcLen, dest, MAX_BUFFER_SIZE, &parseError, &status);
483 
484         if(status != U_PARSE_ERROR){
485             log_err("Did not get the expected error.Expected: %s Got: %s\n", u_errorName(U_PARSE_ERROR), u_errorName(status));
486         }
487     }
488 
489 
490 }
491 
492 static void
Test_nfs4_cs_prep(void)493 Test_nfs4_cs_prep(void){
494     UErrorCode errorCode = U_ZERO_ERROR;
495     loadTestData(&errorCode);
496     if(U_FAILURE(errorCode)) {
497         log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(errorCode));
498         return;
499     }
500 
501     {
502         /* BiDi checking is turned off */
503         const char *source = "\\uC138\\uACC4\\uC758\\uBAA8\\uB4E0\\uC0AC\\uB78C\\uB4E4\\uC774\\u0644\\u064A\\u0647\\uD55C\\uAD6D\\uC5B4\\uB97C\\uC774\\uD574\\uD55C\\uB2E4\\uBA74";
504         UErrorCode status = U_ZERO_ERROR;
505         char src[MAX_BUFFER_SIZE]={'\0'};
506         UParseError parseError;
507         int32_t srcLen = unescapeData(source, (int32_t)strlen(source), src, MAX_BUFFER_SIZE, &status);
508         if(U_SUCCESS(status)){
509             char dest[MAX_BUFFER_SIZE] = {'\0'};
510             int32_t destLen = nfs4_cs_prepare(src, srcLen, dest, MAX_BUFFER_SIZE, false, &parseError, &status);
511             if(U_FAILURE(status)){
512                 log_err("StringPrep failed for case: BiDi Checking Turned OFF with error: %s\n", u_errorName(status));
513             }
514             if(strcmp(dest,src)!=0){
515                 log_err("Did not get the expected output for case: BiDi Checking Turned OFF\n");
516             }
517             if(destLen != srcLen){
518                 log_err("Did not get the expected length for the output for case: BiDi Checking Turned OFF. Expected: %i Got: %i\n", srcLen, destLen);
519             }
520         }else{
521             log_err("Conversion failed for case: BiDi Checking Turned OFF with error: %s\n", u_errorName(status));
522         }
523     }
524     {
525         /* Normalization turned off */
526         const char *source = "www.\\u00E0\\u00B3\\u00AF.com";
527         UErrorCode status = U_ZERO_ERROR;
528         char src[MAX_BUFFER_SIZE]={'\0'};
529         UParseError parseError;
530         int32_t srcLen = unescapeData(source, (int32_t)strlen(source), src, MAX_BUFFER_SIZE, &status);
531         if(U_SUCCESS(status)){
532             char dest[MAX_BUFFER_SIZE] = {'\0'};
533             int32_t destLen = nfs4_cs_prepare(src, srcLen, dest, MAX_BUFFER_SIZE, false, &parseError, &status);
534             if(U_FAILURE(status)){
535                 log_err("StringPrep failed for case: Normalization Turned OFF with error: %s\n", u_errorName(status));
536             }
537             if(strcmp(dest,src)!=0){
538                 log_err("Did not get the expected output for case: Normalization Turned OFF\n");
539             }
540             if(destLen != srcLen){
541                 log_err("Did not get the expected length for the output for case: Normalization Turned OFF. Expected: %i Got: %i\n", srcLen, destLen);
542             }
543         }else{
544             log_err("Conversion failed for case: Normalization Turned OFF with error: %s\n", u_errorName(status));
545         }
546     }
547     {
548         /* case mapping turned off */
549         const char *source = "THISISATEST";
550         UErrorCode status = U_ZERO_ERROR;
551         char src[MAX_BUFFER_SIZE]={'\0'};
552         UParseError parseError;
553         int32_t srcLen = unescapeData(source, (int32_t)strlen(source), src, MAX_BUFFER_SIZE, &status);
554         if(U_SUCCESS(status)){
555             char dest[MAX_BUFFER_SIZE] = {'\0'};
556             int32_t destLen = nfs4_cs_prepare(src, srcLen, dest, MAX_BUFFER_SIZE, true, &parseError, &status);
557             if(U_FAILURE(status)){
558                 log_err("StringPrep failed for case: Case Mapping Turned OFF with error: %s\n", u_errorName(status));
559             }
560             if(strcmp(dest,src)!=0){
561                 log_err("Did not get the expected output for case: Case Mapping Turned OFF\n");
562             }
563             if(destLen != srcLen){
564                 log_err("Did not get the expected length for the output for case: Case Mapping Turned OFF. Expected: %i Got: %i\n", srcLen, destLen);
565             }
566         }else{
567             log_err("Conversion failed for case: Case Mapping Turned OFF with error: %s\n", u_errorName(status));
568         }
569     }
570     {
571         /* case mapping turned on */
572         const char *source = "THISISATEST";
573         const char *expected = "thisisatest";
574         UErrorCode status = U_ZERO_ERROR;
575         char src[MAX_BUFFER_SIZE]={'\0'};
576         char exp[MAX_BUFFER_SIZE]={'\0'};
577         UParseError parseError;
578         int32_t srcLen = unescapeData(source, (int32_t)strlen(source), src, MAX_BUFFER_SIZE, &status);
579         int32_t expLen = unescapeData(expected, (int32_t)strlen(expected), exp, MAX_BUFFER_SIZE, &status);
580         if(U_SUCCESS(status)){
581             char dest[MAX_BUFFER_SIZE] = {'\0'};
582             int32_t destLen = nfs4_cs_prepare(src, srcLen, dest, MAX_BUFFER_SIZE, false, &parseError, &status);
583             if(U_FAILURE(status)){
584                 log_err("StringPrep failed for case: Case Mapping Turned On with error: %s\n", u_errorName(status));
585             }
586             if(strcmp(exp, dest)!=0){
587                 log_err("Did not get the expected output for case: Case Mapping Turned On!\n");
588             }
589             if(destLen != expLen){
590                 log_err("Did not get the expected length for the outputfor case: Case Mapping Turned On. Expected: %i Got: %i\n", strlen(expected), destLen);
591             }
592         }else{
593             log_err("Conversion failed for case: Case Mapping Turned ON with error: %s\n", u_errorName(status));
594         }
595     }
596 }
597 
598 
599 
TestBEAMWarning()600 static void TestBEAMWarning(){
601     UErrorCode status = U_ZERO_ERROR;
602     UParseError parseError;
603     UStringPrepProfile* profile = NULL;
604     /* get the test data path */
605     const char *testdatapath = NULL;
606     UChar src =0x0000;
607     testdatapath = loadTestData(&status);
608     if(U_FAILURE(status)) {
609         log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status));
610         return;
611     }
612     /* open the profile */
613     profile = usprep_open(testdatapath, "nfscis",  &status);
614     usprep_prepare(profile,&src , 0, NULL, 0, USPREP_DEFAULT, &parseError, &status);
615 
616     usprep_close(profile);
617 }
618 
TestCoverage(void)619 static void TestCoverage(void) {
620     UErrorCode status = U_USELESS_COLLATOR_ERROR;
621     UParseError parseError;
622 
623     usprep_open(NULL, NULL, &status);
624     if (status != U_USELESS_COLLATOR_ERROR) {
625         log_err("usprep_open didn't react correctly to a bad UErrorCode\n");
626     }
627     usprep_prepare(NULL, NULL, 0, NULL, 0, USPREP_DEFAULT, &parseError, &status);
628     if (status != U_USELESS_COLLATOR_ERROR) {
629         log_err("usprep_prepare didn't react correctly to a bad UErrorCode\n");
630     }
631     status = U_ZERO_ERROR;
632     usprep_prepare(NULL, NULL, 0, NULL, 0, USPREP_DEFAULT, &parseError, &status);
633     if (status != U_ILLEGAL_ARGUMENT_ERROR) {
634         log_err("usprep_prepare didn't check its arguments\n");
635     }
636 
637     /* Don't crash */
638     usprep_close(NULL);
639 }
640 
641 /**** Profile Test ****/
642 
643 #define SPREP_PROFILE_TEST_MAX_LENGTH 64
644 /* The format of the test cases should be the following:
645 * {
646 *     Profile name
647 *     src string1
648 *     expected result1
649 *     src string2
650 *     expected result2
651 *     ...
652 * }
653 *
654 * *Note: For expected failures add FAIL to beginning of the source string and for expected result use "FAIL".
655 */
656 static const char *profile_test_case[] = {
657 /**** RFC4013_SASLPREP ****/
658     "RFC4013_SASLPREP",
659     "user:\\u00A0\\u0AC6\\u1680\\u00ADpassword1",
660     "user: \\u0AC6 password1",
661 
662 /**** RFC4011_MIB ****/
663     "RFC4011_MIB",
664     "Policy\\u034F\\u200DBase\\u0020d\\u1806\\u200C",
665     "PolicyBase d",
666 
667 /**** RFC4505_TRACE ****/
668     "RFC4505_TRACE",
669     "Anony\\u0020\\u00A0mous\\u3000\\u0B9D\\u034F\\u00AD",
670     "Anony\\u0020\\u00A0mous\\u3000\\u0B9D\\u034F\\u00AD",
671 
672 /**** RFC4518_LDAP ****/
673     "RFC4518_LDAP",
674     "Ldap\\uFB01\\u00ADTest\\u0020\\u00A0\\u2062ing",
675     "LdapfiTest  ing",
676 
677 /**** RFC4518_LDAP_CI ****/
678     "RFC4518_LDAP_CI",
679     "Ldap\\uFB01\\u00ADTest\\u0020\\u00A0\\u2062ing12345",
680     "ldapfitest  ing12345",
681 
682 /**** RFC3920_RESOURCEPREP ****/
683     "RFC3920_RESOURCEPREP",
684     "ServerXM\\u2060\\uFE00\\uFE09PP s p ",
685     "ServerXMPP s p ",
686 
687 /**** RFC3920_NODEPREP ****/
688     "RFC3920_NODEPREP",
689     "Server\\u200DXMPPGreEK\\u03D0",
690     "serverxmppgreek\\u03B2",
691 
692 /**** RFC3722_ISCI ****/
693     "RFC3722_ISCSI",
694     "InternetSmallComputer\\uFB01\\u0032\\u2075Interface",
695     "internetsmallcomputerfi25interface",
696     "FAILThisShouldFailBecauseOfThis\\u002F",
697     "FAIL",
698 
699 /**** RFC3530_NFS4_CS_PREP ****/
700     "RFC3530_NFS4_CS_PREP",
701     "\\u00ADUser\\u2060Name@ \\u06DDDOMAIN.com",
702     "UserName@ \\u06DDDOMAIN.com",
703 
704 /**** RFC3530_NFS4_CS_PREP_CI ****/
705     "RFC3530_NFS4_CS_PREP_CI",
706     "\\u00ADUser\\u2060Name@ \\u06DDDOMAIN.com",
707     "username@ \\u06DDdomain.com",
708 
709 /**** RFC3530_NFS4_CIS_PREP ****/
710     "RFC3530_NFS4_CIS_PREP",
711     "AA\\u200C\\u200D @@DomAin.org",
712     "aa @@domain.org",
713 
714 /**** RFC3530_NFS4_MIXED_PREP_PREFIX ****/
715     "RFC3530_NFS4_MIXED_PREP_PREFIX",
716     "PrefixUser \\u007F\\uFB01End",
717     "PrefixUser \\u007FfiEnd",
718 
719 /**** RFC3530_NFS4_MIXED_PREP_SUFFIX ****/
720     "RFC3530_NFS4_MIXED_PREP_SUFFIX",
721     "SuffixDomain \\u007F\\uFB01EnD",
722     "suffixdomain \\u007Ffiend",
723 };
724 
getTypeFromProfileName(const char * profileName)725 UStringPrepProfileType getTypeFromProfileName(const char* profileName) {
726     if (uprv_strcmp(profileName, "RFC4013_SASLPREP") == 0) {
727         return USPREP_RFC4013_SASLPREP;
728     } else if (uprv_strcmp(profileName, "RFC4011_MIB") == 0) {
729         return USPREP_RFC4011_MIB;
730     } else if (uprv_strcmp(profileName, "RFC4505_TRACE") == 0) {
731         return USPREP_RFC4505_TRACE;
732     } else if (uprv_strcmp(profileName, "RFC4518_LDAP") == 0) {
733         return USPREP_RFC4518_LDAP;
734     } else if (uprv_strcmp(profileName, "RFC4518_LDAP_CI") == 0) {
735         return USPREP_RFC4518_LDAP_CI;
736     } else if (uprv_strcmp(profileName, "RFC3920_RESOURCEPREP") == 0) {
737         return USPREP_RFC3920_RESOURCEPREP;
738     } else if (uprv_strcmp(profileName, "RFC3920_NODEPREP") == 0) {
739         return USPREP_RFC3920_NODEPREP;
740     } else if (uprv_strcmp(profileName, "RFC3722_ISCSI") == 0) {
741         return USPREP_RFC3722_ISCSI;
742     } else if (uprv_strcmp(profileName, "RFC3530_NFS4_CS_PREP") == 0) {
743         return USPREP_RFC3530_NFS4_CS_PREP;
744     } else if (uprv_strcmp(profileName, "RFC3530_NFS4_CS_PREP_CI") == 0) {
745         return USPREP_RFC3530_NFS4_CS_PREP_CI;
746     } else if (uprv_strcmp(profileName, "RFC3530_NFS4_CIS_PREP") == 0) {
747         return USPREP_RFC3530_NFS4_CIS_PREP;
748     } else if (uprv_strcmp(profileName, "RFC3530_NFS4_MIXED_PREP_PREFIX") == 0) {
749         return USPREP_RFC3530_NFS4_MIXED_PREP_PREFIX;
750     } else if (uprv_strcmp(profileName, "RFC3530_NFS4_MIXED_PREP_SUFFIX") == 0) {
751         return USPREP_RFC3530_NFS4_MIXED_PREP_SUFFIX;
752     }
753     /* Should not happen. */
754     return USPREP_RFC3491_NAMEPREP;
755 }
TestStringPrepProfiles(void)756 static void TestStringPrepProfiles(void) {
757     UErrorCode status = U_ZERO_ERROR;
758     const char *profileName = NULL;
759     UChar src[SPREP_PROFILE_TEST_MAX_LENGTH];
760     UChar expected[SPREP_PROFILE_TEST_MAX_LENGTH];
761     UChar result[SPREP_PROFILE_TEST_MAX_LENGTH];
762     int32_t srcLength, resultLength, expectedLength;
763     int32_t i, testNum = 0;
764     UStringPrepProfile *sprep = NULL;
765 
766     for (i = 0; i < UPRV_LENGTHOF(profile_test_case); i++) {
767         if (uprv_strstr(profile_test_case[i], "RFC")) {
768             if (sprep != NULL) {
769                 usprep_close(sprep);
770                 sprep = NULL;
771             }
772             profileName = profile_test_case[i];
773             sprep = usprep_openByType(getTypeFromProfileName(profileName), &status);
774             if (U_FAILURE(status)) {
775                 log_data_err("Unable to open String Prep Profile with: %s\n", profileName);
776                 break;
777             }
778 
779             testNum = 0;
780             continue;
781         }
782         srcLength = resultLength = expectedLength = SPREP_PROFILE_TEST_MAX_LENGTH;
783 
784         testNum++;
785 
786         srcLength = u_unescape(profile_test_case[i], src, srcLength);
787         expectedLength = u_unescape(profile_test_case[++i], expected, expectedLength);
788 
789         resultLength = usprep_prepare(sprep, src, srcLength, result, resultLength, USPREP_ALLOW_UNASSIGNED, NULL, &status);
790         if (U_FAILURE(status)) {
791             if (uprv_strstr(profile_test_case[i], "FAIL") == NULL) {
792                 log_err("Error occurred on test[%d] for profile: %s\n", testNum, profileName);
793             } else {
794                 /* Error is expected so reset the status. */
795                 status = U_ZERO_ERROR;
796             }
797         } else {
798             if (uprv_strstr(profile_test_case[i], "FAIL") != NULL) {
799                 log_err("Error expected on test[%d] for profile: %s\n", testNum, profileName);
800             }
801 
802             if (resultLength != expectedLength || u_strcmp(result, expected) != 0) {
803                 log_err("Results do not match expected on test[%d] for profile: %s\n", testNum, profileName);
804             }
805         }
806     }
807 
808     if (sprep != NULL) {
809         usprep_close(sprep);
810     }
811 }
812 
813 #endif
814 
815 /*
816  * Hey, Emacs, please set the following:
817  *
818  * Local Variables:
819  * indent-tabs-mode: nil
820  * End:
821  *
822  */
823