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