]>
Commit | Line | Data |
---|---|---|
1 | // © 2016 and later: Unicode, Inc. and others. | |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
3 | /******************************************************************** | |
4 | * COPYRIGHT: | |
5 | * Copyright (c) 1998-2016, International Business Machines Corporation and | |
6 | * others. All Rights Reserved. | |
7 | ********************************************************************/ | |
8 | /* | |
9 | * File udatatst.c | |
10 | * | |
11 | * Modification History: | |
12 | * | |
13 | * Date Name Description | |
14 | * 02/22/2000 Madhu Creation | |
15 | ****************************************************************************** | |
16 | */ | |
17 | ||
18 | #include "unicode/utypes.h" | |
19 | #include "unicode/putil.h" | |
20 | #include "unicode/udata.h" | |
21 | #include "unicode/ucal.h" | |
22 | #include "unicode/uchar.h" | |
23 | #include "unicode/ucnv.h" | |
24 | #include "unicode/ures.h" | |
25 | #include "unicode/ustring.h" | |
26 | #include "unicode/uclean.h" | |
27 | #include "cmemory.h" | |
28 | #include "cstring.h" | |
29 | #include "filestrm.h" | |
30 | #include "udatamem.h" | |
31 | #include "cintltst.h" | |
32 | #include "ubrkimpl.h" | |
33 | #include "toolutil.h" /* for uprv_fileExists() */ | |
34 | #include <stdlib.h> | |
35 | #include <stdio.h> | |
36 | ||
37 | /* includes for TestSwapData() */ | |
38 | #include "udataswp.h" | |
39 | ||
40 | /* swapping implementations in common */ | |
41 | #include "uresdata.h" | |
42 | #include "ucnv_io.h" | |
43 | #include "uprops.h" | |
44 | #include "ucase.h" | |
45 | #include "ucol_imp.h" | |
46 | #include "ucol_swp.h" | |
47 | #include "ucnv_bld.h" | |
48 | #include "sprpimpl.h" | |
49 | #include "rbbidata.h" | |
50 | ||
51 | /* swapping implementation in i18n */ | |
52 | #include "uspoof_impl.h" | |
53 | ||
54 | U_CAPI int32_t U_EXPORT2 | |
55 | unorm2_swap(const UDataSwapper *ds, | |
56 | const void *inData, int32_t length, void *outData, | |
57 | UErrorCode *pErrorCode); | |
58 | ||
59 | /* other definitions and prototypes */ | |
60 | ||
61 | #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION | |
62 | static void TestUDataOpen(void); | |
63 | static void TestUDataOpenChoiceDemo1(void); | |
64 | static void TestUDataOpenChoiceDemo2(void); | |
65 | static void TestUDataGetInfo(void); | |
66 | static void TestUDataGetMemory(void); | |
67 | static void TestErrorConditions(void); | |
68 | static void TestAppData(void); | |
69 | static void TestSwapData(void); | |
70 | #endif | |
71 | static void TestUDataSetAppData(void); | |
72 | static void TestICUDataName(void); | |
73 | static void PointerTableOfContents(void); | |
74 | static void SetBadCommonData(void); | |
75 | static void TestUDataFileAccess(void); | |
76 | #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION | |
77 | static void TestTZDataDir(void); | |
78 | #endif | |
79 | ||
80 | void addUDataTest(TestNode** root); | |
81 | ||
82 | void | |
83 | addUDataTest(TestNode** root) | |
84 | { | |
85 | #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION | |
86 | addTest(root, &TestUDataOpen, "udatatst/TestUDataOpen" ); | |
87 | addTest(root, &TestUDataOpenChoiceDemo1, "udatatst/TestUDataOpenChoiceDemo1"); | |
88 | addTest(root, &TestUDataOpenChoiceDemo2, "udatatst/TestUDataOpenChoiceDemo2"); | |
89 | addTest(root, &TestUDataGetInfo, "udatatst/TestUDataGetInfo" ); | |
90 | addTest(root, &TestUDataGetMemory, "udatatst/TestUDataGetMemory" ); | |
91 | addTest(root, &TestErrorConditions, "udatatst/TestErrorConditions"); | |
92 | addTest(root, &TestAppData, "udatatst/TestAppData" ); | |
93 | addTest(root, &TestSwapData, "udatatst/TestSwapData" ); | |
94 | #endif | |
95 | addTest(root, &TestUDataSetAppData, "udatatst/TestUDataSetAppData" ); | |
96 | addTest(root, &TestICUDataName, "udatatst/TestICUDataName" ); | |
97 | addTest(root, &PointerTableOfContents, "udatatst/PointerTableOfContents" ); | |
98 | addTest(root, &SetBadCommonData, "udatatst/SetBadCommonData" ); | |
99 | addTest(root, &TestUDataFileAccess, "udatatst/TestUDataFileAccess" ); | |
100 | #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION | |
101 | addTest(root, &TestTZDataDir, "udatatst/TestTZDataDir" ); | |
102 | #endif | |
103 | } | |
104 | ||
105 | #if 0 | |
106 | static void lots_of_mallocs() | |
107 | { | |
108 | int q; | |
109 | for(q=1;q<100;q++) | |
110 | { | |
111 | free(malloc(q)); | |
112 | malloc(q*2); | |
113 | } | |
114 | ||
115 | } | |
116 | #endif | |
117 | ||
118 | #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION | |
119 | static void TestUDataOpen(){ | |
120 | UDataMemory *result; | |
121 | UErrorCode status=U_ZERO_ERROR; | |
122 | const char* memMap[][2]={ | |
123 | {"root", "res"}, | |
124 | {"cnvalias", "icu"}, | |
125 | {"unames", "icu"}, | |
126 | {"ibm-37_P100-1995", "cnv"} | |
127 | }; | |
128 | const char* name = "test"; | |
129 | const char* type = "icu"; | |
130 | const char dirSepString[] = {U_FILE_SEP_CHAR, 0}; | |
131 | const char pathSepString[] = {U_PATH_SEP_CHAR, 0}; | |
132 | ||
133 | ||
134 | char* path=(char*)malloc(sizeof(char) * (strlen(ctest_dataOutDir()) | |
135 | + strlen(U_ICUDATA_NAME) | |
136 | + strlen("/build/tmp/..")+1 ) ); | |
137 | ||
138 | char *icuDataFilePath = 0; | |
139 | ||
140 | const char* testPath=loadTestData(&status); | |
141 | if(U_FAILURE(status)) { | |
142 | log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status)); | |
143 | free(path); | |
144 | return; | |
145 | } | |
146 | ||
147 | /* lots_of_mallocs(); */ | |
148 | log_verbose("Testing udata_open(%s)\n", testPath); | |
149 | result=udata_open(testPath, type, name, &status); | |
150 | if(U_FAILURE(status)){ | |
151 | log_data_err("FAIL: udata_open() failed for path = %s, name=%s, type=%s, \n errorcode=%s\n", testPath, name, type, myErrorName(status)); | |
152 | } else { | |
153 | log_verbose("PASS: udata_open worked\n"); | |
154 | udata_close(result); | |
155 | } | |
156 | ||
157 | { | |
158 | strcat(strcpy(path, ctest_dataOutDir()), U_ICUDATA_NAME); | |
159 | ||
160 | /* If the ICU system common data file is present in this confiugration, | |
161 | * verify that udata_open can explicitly fetch items from it. | |
162 | * If packaging mode == dll, the file may not exist. So, if the file is | |
163 | * missing, skip this test without error. | |
164 | */ | |
165 | icuDataFilePath = (char *)uprv_malloc(strlen(path) + 10); | |
166 | strcpy(icuDataFilePath, path); | |
167 | strcat(icuDataFilePath, ".dat"); | |
168 | /* lots_of_mallocs(); */ | |
169 | if (uprv_fileExists(icuDataFilePath)) | |
170 | { | |
171 | int i; | |
172 | log_verbose("Testing udata_open() on %s\n", icuDataFilePath); | |
173 | for(i=0; i<UPRV_LENGTHOF(memMap); i++){ | |
174 | /* lots_of_mallocs(); */ | |
175 | status=U_ZERO_ERROR; | |
176 | result=udata_open(path, memMap[i][1], memMap[i][0], &status); | |
177 | if(U_FAILURE(status)) { | |
178 | log_data_err("FAIL: udata_open() failed for path = %s, name=%s, type=%s, \n errorcode=%s\n", path, memMap[i][0], memMap[i][1], myErrorName(status)); | |
179 | } else { | |
180 | log_verbose("PASS: udata_open worked for path = %s, name=%s, type=%s\n", path, memMap[i][0], memMap[i][1]); | |
181 | udata_close(result); | |
182 | } | |
183 | } | |
184 | } | |
185 | else | |
186 | { | |
187 | /* lots_of_mallocs(); */ | |
188 | log_verbose("Skipping tests of udata_open() on %s. File not present in this configuration.\n", | |
189 | icuDataFilePath); | |
190 | } | |
191 | uprv_free(icuDataFilePath); | |
192 | } | |
193 | /* try again, adding /tmp */ | |
194 | { | |
195 | strcpy(path, ctest_dataOutDir()); | |
196 | strcat(path, "tmp"); | |
197 | strcat(path, dirSepString); | |
198 | strcat(path, U_ICUDATA_NAME); | |
199 | ||
200 | /* If the ICU system common data file is present in this confiugration, | |
201 | * verify that udata_open can explicitly fetch items from it. | |
202 | * If packaging mode == dll, the file may not exist. So, if the file is | |
203 | * missing, skip this test without error. | |
204 | */ | |
205 | icuDataFilePath = (char *)malloc(strlen(path) + 10); | |
206 | strcpy(icuDataFilePath, path); | |
207 | strcat(icuDataFilePath, ".dat"); | |
208 | /* lots_of_mallocs(); */ | |
209 | if (uprv_fileExists(icuDataFilePath)) | |
210 | { | |
211 | int i; | |
212 | log_verbose("Testing udata_open() on %s\n", icuDataFilePath); | |
213 | for(i=0; i<UPRV_LENGTHOF(memMap); i++){ | |
214 | /* lots_of_mallocs(); */ | |
215 | status=U_ZERO_ERROR; | |
216 | result=udata_open(path, memMap[i][1], memMap[i][0], &status); | |
217 | if(U_FAILURE(status)) { | |
218 | log_data_err("FAIL: udata_open() failed for path = %s, name=%s, type=%s, \n errorcode=%s\n", path, memMap[i][0], memMap[i][1], myErrorName(status)); | |
219 | } else { | |
220 | log_verbose("PASS: udata_open worked for path = %s, name=%s, type=%s\n", path, memMap[i][0], memMap[i][1]); | |
221 | udata_close(result); | |
222 | } | |
223 | } | |
224 | } | |
225 | else | |
226 | { | |
227 | /* lots_of_mallocs(); */ | |
228 | log_verbose("Skipping tests of udata_open() on %s. File not present in this configuration.\n", | |
229 | icuDataFilePath); | |
230 | } | |
231 | } | |
232 | ||
233 | free(icuDataFilePath); | |
234 | icuDataFilePath = NULL; | |
235 | /* lots_of_mallocs(); */ | |
236 | ||
237 | /* If the ICU individual files used to build the ICU system common data are | |
238 | * present in this configuration, | |
239 | * verify that udata_open can explicitly open them. | |
240 | * These data files are present in the ICU data/build directory after a build | |
241 | * completes. Tests are most commonly run with the data directory pointing | |
242 | * back into this directory structure, but this is not required. Soooo, if | |
243 | * the files are missing, skip this test without error. | |
244 | */ | |
245 | /* lots_of_mallocs(); */ | |
246 | icuDataFilePath = (char *)malloc(strlen(ctest_dataOutDir()) + 50); | |
247 | strcpy(icuDataFilePath, ctest_dataOutDir()); | |
248 | strcat(icuDataFilePath, "build"); | |
249 | strcat(icuDataFilePath, dirSepString); | |
250 | strcat(icuDataFilePath, U_ICUDATA_NAME); | |
251 | strcat(icuDataFilePath, dirSepString); | |
252 | strcat(icuDataFilePath, "cnvalias.icu"); | |
253 | ||
254 | /* lots_of_mallocs(); */ | |
255 | if (uprv_fileExists(icuDataFilePath)) | |
256 | { | |
257 | int i; | |
258 | log_verbose("%s exists, so..\n", icuDataFilePath); | |
259 | strcpy(icuDataFilePath, ctest_dataOutDir()); | |
260 | strcat(icuDataFilePath, "build"); | |
261 | strcat(icuDataFilePath, dirSepString); | |
262 | strcat(icuDataFilePath, U_ICUDATA_NAME); | |
263 | log_verbose("Testing udata_open() on %s\n", icuDataFilePath); | |
264 | for(i=0; i<UPRV_LENGTHOF(memMap); i++){ | |
265 | status=U_ZERO_ERROR; | |
266 | result=udata_open(icuDataFilePath, memMap[i][1], memMap[i][0], &status); | |
267 | if(U_FAILURE(status)) { | |
268 | log_data_err("FAIL: udata_open() failed for path = %s, name=%s, type=%s, \n errorcode=%s\n", icuDataFilePath, memMap[i][0], memMap[i][1], myErrorName(status)); | |
269 | } else { | |
270 | log_verbose("PASS: udata_open worked for path = %s, name=%s, type=%s\n", icuDataFilePath, memMap[i][0], memMap[i][1]); | |
271 | udata_close(result); | |
272 | } | |
273 | } | |
274 | } | |
275 | else | |
276 | { | |
277 | log_verbose("Skipping tests of udata_open() on %s. File not present in this configuration.\n", | |
278 | icuDataFilePath); | |
279 | } | |
280 | ||
281 | free(icuDataFilePath); | |
282 | icuDataFilePath = NULL; | |
283 | ||
284 | /* | |
285 | * Test fallback file names for open of separate data files. | |
286 | * With these params to udata_open: | |
287 | * path = wherever/testdata | |
288 | * type = typ | |
289 | * name = nam | |
290 | * these files will be tried first: | |
291 | * wherever/testudata_nam.typ | |
292 | * testudata_nam.typ | |
293 | * A test data file named testudata_nam.typ exists for the purpose of testing this. | |
294 | */ | |
295 | log_verbose("Testing udata_open, with base_name.type style fallback to individual file.\n"); | |
296 | ||
297 | status = U_ZERO_ERROR; | |
298 | result = udata_open( testPath, "typ", "nam", &status); | |
299 | if (status != U_ZERO_ERROR) { | |
300 | log_data_err("FAIL: udata_open( \"%s\", \"typ\", \"nam\") returned status %s\n", testPath, u_errorName(status)); | |
301 | } | |
302 | udata_close(result); | |
303 | free(icuDataFilePath); | |
304 | ||
305 | ||
306 | /* This type of path is deprecated */ | |
307 | /* | |
308 | * Another fallback test. Paths ending with a trailing directory separator | |
309 | * take a slightly different code path, with the "base name" from the path | |
310 | * being empty in the internal udata_open logic. | |
311 | */ | |
312 | ||
313 | /* log_verbose("Testing udata_open, with path containing a trailing directory separator.\n"); */ | |
314 | /* icuDataFilePath = (char *)malloc(strlen(u_getDataDirectory()) + 50); */ | |
315 | /* strcpy(icuDataFilePath, testPath); */ | |
316 | /* status = U_ZERO_ERROR; */ | |
317 | /* result = udata_open( icuDataFilePath, "cnv", "test1", &status); */ | |
318 | /* if (status != U_ZERO_ERROR) { */ | |
319 | /* log_err("FAIL: udata_open( \"%s\", \"cnv\", \"test1\") returned status %s\n", icuDataFilePath, u_errorName(status)); */ | |
320 | /* } */ | |
321 | /* udata_close(result); */ | |
322 | /* free(icuDataFilePath); */ | |
323 | ||
324 | ||
325 | log_verbose("Testing udata_open() with a non existing binary file\n"); | |
326 | result=udata_open("testdata", "tst", "nonexist", &status); | |
327 | if(status==U_FILE_ACCESS_ERROR){ | |
328 | log_verbose("Opening udata_open with non-existing file handled correctly.\n"); | |
329 | status=U_ZERO_ERROR; | |
330 | } else { | |
331 | log_err("calling udata_open with non-existing file [testdata | nonexist.tst] not handled correctly\n. Expected: U_FILE_ACCESS_ERROR, Got: %s\n", myErrorName(status)); | |
332 | if(U_SUCCESS(status)) { | |
333 | udata_close(result); | |
334 | } | |
335 | } | |
336 | ||
337 | if(result != NULL){ | |
338 | log_err("calling udata_open with non-existing file didn't return a null value\n"); | |
339 | } else { | |
340 | log_verbose("calling udat_open with non-existing file returned null as expected\n"); | |
341 | } | |
342 | ||
343 | /* | |
344 | * Try opening data with absurdly long path and name, to trigger buffer size | |
345 | * overflow handling code. | |
346 | */ | |
347 | { | |
348 | char longTestPath[1024]; /* Implementation goes to heap at length of 128. */ | |
349 | char longName[1024]; | |
350 | ||
351 | /* Try a very long nonexistent directory path. | |
352 | * udata_open should still succeed. Opening with the path will fail, | |
353 | * then fall back to skipping the directory portion of the path. | |
354 | */ | |
355 | log_verbose("Testing udata_open() with really long names\n"); | |
356 | longTestPath[0] = 0; | |
357 | strcat(longTestPath, "bogus_directory_name"); | |
358 | while (strlen(longTestPath) < 500) { | |
359 | strcat(longTestPath, dirSepString); | |
360 | strcat(longTestPath, "bogus_directory_name"); | |
361 | } | |
362 | strcat(longTestPath, pathSepString); | |
363 | strcat(longTestPath, testPath); | |
364 | result=udata_open(longTestPath, type, name, &status); | |
365 | if(U_FAILURE(status)){ | |
366 | log_data_err("FAIL: udata_open() failed for path = %s\n name=%s, type=%s, \n errorcode=%s\n", | |
367 | longTestPath, name, type, myErrorName(status)); | |
368 | } else { | |
369 | log_verbose("PASS: udata_open worked\n"); | |
370 | udata_close(result); | |
371 | } | |
372 | ||
373 | /* Try a very long name. Won't open, but shouldn't blow up. | |
374 | */ | |
375 | longName[0] = 0; | |
376 | while (strlen(longName) < 500) { | |
377 | strcat(longName, name); | |
378 | strcat(longName, "_"); | |
379 | } | |
380 | strcat(longName, dirSepString); | |
381 | strcat(longName, name); | |
382 | ||
383 | result=udata_open(longTestPath, type, longName, &status); | |
384 | if (status != U_FILE_ACCESS_ERROR) { | |
385 | log_data_err("FAIL: udata_open() failed for path = %s\n name=%s, type=%s, \n errorcode=%s\n", | |
386 | longTestPath, longName, type, myErrorName(status)); | |
387 | } | |
388 | udata_close(result); | |
389 | } | |
390 | ||
391 | free(path); | |
392 | } | |
393 | #endif | |
394 | ||
395 | typedef struct { | |
396 | uint16_t headerSize; | |
397 | uint8_t magic1, magic2; | |
398 | UDataInfo info; | |
399 | char padding[8]; | |
400 | uint32_t count, reserved; | |
401 | /* | |
402 | const struct { | |
403 | const char *const name; | |
404 | const void *const data; | |
405 | } toc[1]; | |
406 | */ | |
407 | int32_t fakeNameAndData[4]; | |
408 | } ICU_COMMON_Data_Header; | |
409 | ||
410 | static const ICU_COMMON_Data_Header gEmptyHeader = { | |
411 | 32, /* headerSize */ | |
412 | 0xda, /* magic1, (see struct MappedData in udata.c) */ | |
413 | 0x27, /* magic2 */ | |
414 | { /*UDataInfo */ | |
415 | sizeof(UDataInfo), /* size */ | |
416 | 0, /* reserved */ | |
417 | ||
418 | #if U_IS_BIG_ENDIAN | |
419 | 1, | |
420 | #else | |
421 | 0, | |
422 | #endif | |
423 | ||
424 | U_CHARSET_FAMILY, | |
425 | sizeof(UChar), | |
426 | 0, /* reserved */ | |
427 | { /* data format identifier */ | |
428 | 0x43, 0x6d, 0x6e, 0x44}, /* "CmnD" */ | |
429 | {1, 0, 0, 0}, /* format version major, minor, milli, micro */ | |
430 | {0, 0, 0, 0} /* dataVersion */ | |
431 | }, | |
432 | {0,0,0,0,0,0,0,0}, /* Padding[8] */ | |
433 | 0, /* count */ | |
434 | 0, /* Reserved */ | |
435 | { /* TOC structure */ | |
436 | /* { */ | |
437 | 0 , 0 , 0, 0 /* name and data entries. Count says there are none, */ | |
438 | /* but put one in just in case. */ | |
439 | /* } */ | |
440 | } | |
441 | }; | |
442 | ||
443 | ||
444 | static void TestUDataSetAppData(){ | |
445 | /* UDataMemory *dataItem;*/ | |
446 | ||
447 | UErrorCode status=U_ZERO_ERROR; | |
448 | ||
449 | /* | |
450 | * First we try some monkey business and try to do bad things. | |
451 | */ | |
452 | ||
453 | status=U_ZERO_ERROR; | |
454 | udata_setAppData("appData1", NULL, &status); | |
455 | if (status != U_ILLEGAL_ARGUMENT_ERROR) { | |
456 | log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData1\", NULL, status) should have failed." | |
457 | " It returned status of %s\n", u_errorName(status)); | |
458 | return; | |
459 | } | |
460 | /* The following call should fail. | |
461 | If the following works with a bad UErrorCode, then later calls to appData1 should fail. */ | |
462 | udata_setAppData("appData1", &gEmptyHeader, &status); | |
463 | ||
464 | /* | |
465 | * Got testdata.dat into memory, now we try setAppData using the memory image. | |
466 | */ | |
467 | ||
468 | status=U_ZERO_ERROR; | |
469 | udata_setAppData("appData1", &gEmptyHeader, &status); | |
470 | if (status != U_ZERO_ERROR) { | |
471 | log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData1\", fileBuf, status) " | |
472 | " returned status of %s\n", u_errorName(status)); | |
473 | return; | |
474 | } | |
475 | ||
476 | udata_setAppData("appData2", &gEmptyHeader, &status); | |
477 | if (status != U_ZERO_ERROR) { | |
478 | log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData2\", fileBuf, status) " | |
479 | " returned status of %s\n", u_errorName(status)); | |
480 | return; | |
481 | } | |
482 | ||
483 | /* If we try to setAppData with the same name a second time, we should get a | |
484 | * a using default warning. | |
485 | */ | |
486 | udata_setAppData("appData2", &gEmptyHeader, &status); | |
487 | if (status != U_USING_DEFAULT_WARNING) { | |
488 | log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData2\", fileBuf, status) " | |
489 | " returned status of %s, expected U_USING_DEFAULT_WARNING.\n", u_errorName(status)); | |
490 | } | |
491 | ||
492 | ||
493 | /** It is no longer correct to use udata_setAppData to change the | |
494 | package of a contained item. | |
495 | ||
496 | dataItem = udata_open("appData1", "res", "te_IN", &status); **/ | |
497 | } | |
498 | ||
499 | static char *safeGetICUDataDirectory() { | |
500 | const char *dataDir = u_getDataDirectory(); /* Returned string vanashes with u_cleanup */ | |
501 | char *retStr = NULL; | |
502 | if (dataDir != NULL) { | |
503 | retStr = (char *)malloc(strlen(dataDir)+1); | |
504 | strcpy(retStr, dataDir); | |
505 | } | |
506 | return retStr; | |
507 | } | |
508 | ||
509 | static void TestUDataFileAccess(){ | |
510 | UErrorCode status; | |
511 | char *icuDataDir; | |
512 | icuDataDir = safeGetICUDataDirectory(); /* save icu data dir, so we can put it back | |
513 | * after doing u_cleanup(). */ | |
514 | ||
515 | /** UDATA_NO_FILES, ICU does not access the file system for data loading. */ | |
516 | status=U_ZERO_ERROR; | |
517 | u_cleanup(); | |
518 | udata_setFileAccess(UDATA_NO_FILES,&status); | |
519 | u_init(&status); | |
520 | if(U_FAILURE(status) && *icuDataDir == 0){ | |
521 | log_data_err("udata_setFileAccess(UDATA_NO_FILES) failed with ICU_DATA=\"\" err=%s\n", u_errorName(status)); | |
522 | } | |
523 | ||
524 | /** UDATA_ONLY_PACKAGES, ICU only loads data from packages, not from single files. */ | |
525 | status=U_ZERO_ERROR; | |
526 | u_cleanup(); | |
527 | udata_setFileAccess(UDATA_ONLY_PACKAGES,&status); | |
528 | u_init(&status); | |
529 | ||
530 | /** UDATA_PACKAGES_FIRST, ICU loads data from packages first, and only from single files | |
531 | if the data cannot be found in a package. */ | |
532 | status=U_ZERO_ERROR; | |
533 | u_cleanup(); | |
534 | udata_setFileAccess(UDATA_PACKAGES_FIRST,&status); | |
535 | u_init(&status); | |
536 | ||
537 | /** UDATA_FILES_FIRST, ICU looks for data in single files first, then in packages. (default) */ | |
538 | status=U_ZERO_ERROR; | |
539 | u_cleanup(); | |
540 | udata_setFileAccess(UDATA_FILES_FIRST,&status); | |
541 | u_init(&status); | |
542 | ||
543 | /** An alias for the default access mode. */ | |
544 | status=U_ZERO_ERROR; | |
545 | u_cleanup(); | |
546 | udata_setFileAccess(UDATA_DEFAULT_ACCESS,&status); | |
547 | u_setDataDirectory(icuDataDir); | |
548 | u_init(&status); | |
549 | if(U_FAILURE(status)){ | |
550 | log_err_status(status, "%s\n", u_errorName(status)); | |
551 | } | |
552 | free(icuDataDir); | |
553 | ctest_resetICU(); | |
554 | } | |
555 | ||
556 | ||
557 | static UBool U_CALLCONV | |
558 | isAcceptable1(void *context, | |
559 | const char *type, const char *name, | |
560 | const UDataInfo *pInfo) { | |
561 | ||
562 | if( pInfo->size>=20 && | |
563 | pInfo->isBigEndian==U_IS_BIG_ENDIAN && | |
564 | pInfo->charsetFamily==U_CHARSET_FAMILY && | |
565 | pInfo->dataFormat[0]==0x43 && /* dataFormat="CvAl" */ | |
566 | pInfo->dataFormat[1]==0x76 && | |
567 | pInfo->dataFormat[2]==0x41 && | |
568 | pInfo->dataFormat[3]==0x6c && | |
569 | pInfo->formatVersion[0]==3 ) | |
570 | { | |
571 | log_verbose("The data from \"%s.%s\" IS acceptable using the verifing function isAcceptable1()\n", name, type); | |
572 | return TRUE; | |
573 | } else { | |
574 | log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable1():-\n" | |
575 | "\tsize = %d\n" | |
576 | "\tisBigEndian = %d\n" | |
577 | "\tcharsetFamily = %d\n" | |
578 | "\tformatVersion[0] = %d\n" | |
579 | "\tdataVersion[0] = %d\n" | |
580 | "\tdataFormat = %c%c%c%c\n", | |
581 | name, type, pInfo->size, pInfo->isBigEndian, pInfo->charsetFamily, pInfo->formatVersion[0], | |
582 | pInfo->dataVersion[0], pInfo->dataFormat[0], pInfo->dataFormat[1], pInfo->dataFormat[2], | |
583 | pInfo->dataFormat[3]); | |
584 | log_verbose("Call another verifing function to accept the data\n"); | |
585 | return FALSE; | |
586 | } | |
587 | } | |
588 | ||
589 | static UBool U_CALLCONV | |
590 | isAcceptable2(void *context, | |
591 | const char *type, const char *name, | |
592 | const UDataInfo *pInfo){ | |
593 | UVersionInfo unicodeVersion; | |
594 | ||
595 | u_getUnicodeVersion(unicodeVersion); | |
596 | ||
597 | if( pInfo->size>=20 && | |
598 | pInfo->isBigEndian==U_IS_BIG_ENDIAN && | |
599 | pInfo->charsetFamily==U_CHARSET_FAMILY && | |
600 | pInfo->dataFormat[0]==0x75 && /* dataFormat="unam" */ | |
601 | pInfo->dataFormat[1]==0x6e && | |
602 | pInfo->dataFormat[2]==0x61 && | |
603 | pInfo->dataFormat[3]==0x6d && | |
604 | pInfo->formatVersion[0]==1 && | |
605 | pInfo->dataVersion[0]==unicodeVersion[0] ) | |
606 | { | |
607 | log_verbose("The data from \"%s.%s\" IS acceptable using the verifing function isAcceptable2()\n", name, type); | |
608 | return TRUE; | |
609 | } else { | |
610 | log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable2()\n", name, type); | |
611 | ||
612 | return FALSE; | |
613 | } | |
614 | ||
615 | ||
616 | } | |
617 | static UBool U_CALLCONV | |
618 | isAcceptable3(void *context, | |
619 | const char *type, const char *name, | |
620 | const UDataInfo *pInfo){ | |
621 | ||
622 | if( pInfo->size>=20 && | |
623 | pInfo->isBigEndian==U_IS_BIG_ENDIAN && | |
624 | pInfo->charsetFamily==U_CHARSET_FAMILY && | |
625 | pInfo->dataFormat[0]==0x54 && /* dataFormat="test" */ | |
626 | pInfo->dataFormat[1]==0x65 && | |
627 | pInfo->dataFormat[2]==0x73 && | |
628 | pInfo->dataFormat[3]==0x74 && | |
629 | pInfo->formatVersion[0]==1 && | |
630 | pInfo->dataVersion[0]==1 ) { | |
631 | log_verbose("The data from \"%s.%s\" IS acceptable using the verifing function isAcceptable3()\n", name, type); | |
632 | ||
633 | return TRUE; | |
634 | } else { | |
635 | log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable3()\n", name, type); | |
636 | return FALSE; | |
637 | } | |
638 | ||
639 | ||
640 | } | |
641 | ||
642 | #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION | |
643 | static void TestUDataOpenChoiceDemo1() { | |
644 | UDataMemory *result; | |
645 | UErrorCode status=U_ZERO_ERROR; | |
646 | ||
647 | const char* name[]={ | |
648 | "cnvalias", | |
649 | "unames", | |
650 | "test", | |
651 | "nam" | |
652 | }; | |
653 | const char* type="icu"; | |
654 | const char* testPath="testdata"; | |
655 | const char* fullTestDataPath = loadTestData(&status); | |
656 | if(U_FAILURE(status)) { | |
657 | log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status)); | |
658 | return; | |
659 | } | |
660 | ||
661 | result=udata_openChoice(NULL, "icu", name[0], isAcceptable1, NULL, &status); | |
662 | if(U_FAILURE(status)){ | |
663 | log_data_err("FAIL: udata_openChoice() failed name=%s, type=%s, \n errorcode=%s\n", name[0], type, myErrorName(status)); | |
664 | } else { | |
665 | log_verbose("PASS: udata_openChoice worked\n"); | |
666 | udata_close(result); | |
667 | } | |
668 | ||
669 | status=U_ZERO_ERROR; | |
670 | result=udata_openChoice(NULL, type, name[1], isAcceptable1, NULL, &status); | |
671 | if(U_FAILURE(status)){ | |
672 | status=U_ZERO_ERROR; | |
673 | result=udata_openChoice(NULL, type, name[1], isAcceptable2, NULL, &status); | |
674 | if(U_FAILURE(status)){ | |
675 | log_data_err("FAIL: udata_openChoice() failed name=%s, type=%s, \n errorcode=%s\n", name[1], type, myErrorName(status)); | |
676 | } | |
677 | } | |
678 | else { | |
679 | log_err("FAIL: udata_openChoice() unexpectedly passed. name=%s, type=%s, \n errorcode=%s\n", name[1], type, myErrorName(status)); | |
680 | } | |
681 | ||
682 | if(U_SUCCESS(status)){ | |
683 | udata_close(result); | |
684 | } | |
685 | ||
686 | status=U_ZERO_ERROR; | |
687 | result=udata_openChoice(testPath, type, name[2], isAcceptable1, NULL, &status); | |
688 | if(U_FAILURE(status)){ | |
689 | status=U_ZERO_ERROR; | |
690 | result=udata_openChoice(testPath, type, name[2], isAcceptable3, NULL, &status); | |
691 | if(U_FAILURE(status)){ | |
692 | log_data_err("FAIL: udata_openChoice() failed path=%s name=%s, type=%s, \n errorcode=%s\n", testPath, name[2], type, myErrorName(status)); | |
693 | } | |
694 | } | |
695 | else { | |
696 | log_err("FAIL: udata_openChoice() unexpectedly passed. name=%s, type=%s, \n errorcode=%s\n", name[2], type, myErrorName(status)); | |
697 | } | |
698 | ||
699 | if(U_SUCCESS(status)){ | |
700 | udata_close(result); | |
701 | } | |
702 | ||
703 | status=U_ZERO_ERROR; | |
704 | type="typ"; | |
705 | result=udata_openChoice(fullTestDataPath, type, name[3], isAcceptable1, NULL, &status); | |
706 | if(status != U_INVALID_FORMAT_ERROR){ | |
707 | log_err("FAIL: udata_openChoice() did not fail as expected. name=%s, type=%s, \n errorcode=%s\n", name[3], type, myErrorName(status)); | |
708 | } | |
709 | ||
710 | status=U_USELESS_COLLATOR_ERROR; | |
711 | result=udata_openChoice(fullTestDataPath, type, name[3], isAcceptable1, NULL, &status); | |
712 | if(status != U_USELESS_COLLATOR_ERROR){ | |
713 | log_err("FAIL: udata_openChoice() did not fail as expected. name=%s, type=%s, \n errorcode=%s\n", name[3], type, myErrorName(status)); | |
714 | } | |
715 | } | |
716 | ||
717 | static UBool U_CALLCONV | |
718 | isAcceptable(void *context, | |
719 | const char *type, const char *name, | |
720 | const UDataInfo *pInfo){ | |
721 | if( pInfo->size>=20 && | |
722 | pInfo->isBigEndian==U_IS_BIG_ENDIAN && | |
723 | pInfo->charsetFamily==U_CHARSET_FAMILY && | |
724 | pInfo->dataFormat[0]==0x54 && /* dataFormat="test" */ | |
725 | pInfo->dataFormat[1]==0x65 && | |
726 | pInfo->dataFormat[2]==0x73 && | |
727 | pInfo->dataFormat[3]==0x74 && | |
728 | pInfo->formatVersion[0]==1 && | |
729 | pInfo->dataVersion[0]==1 && | |
730 | *((int*)context) == 2 ) { | |
731 | log_verbose("The data from\"%s.%s\" IS acceptable using the verifing function isAcceptable()\n", name, type); | |
732 | ||
733 | return TRUE; | |
734 | } else { | |
735 | log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable()\n", name, type); | |
736 | return FALSE; | |
737 | } | |
738 | } | |
739 | ||
740 | ||
741 | /* This test checks to see if the isAcceptable function is being called correctly. */ | |
742 | ||
743 | static void TestUDataOpenChoiceDemo2() { | |
744 | UDataMemory *result; | |
745 | UErrorCode status=U_ZERO_ERROR; | |
746 | int i; | |
747 | int p=2; | |
748 | ||
749 | const char* name="test"; | |
750 | const char* type="icu"; | |
751 | const char* path = loadTestData(&status); | |
752 | if(U_FAILURE(status)) { | |
753 | log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status)); | |
754 | return; | |
755 | } | |
756 | ||
757 | result=udata_openChoice(path, type, name, isAcceptable, &p, &status); | |
758 | if(U_FAILURE(status)){ | |
759 | log_data_err("failed to load data at p=%s t=%s n=%s, isAcceptable", path, type, name); | |
760 | } | |
761 | if(U_SUCCESS(status) ) { | |
762 | udata_close(result); | |
763 | } | |
764 | ||
765 | p=0; | |
766 | for(i=0;i<2; i++){ | |
767 | result=udata_openChoice(path, type, name, isAcceptable, &p, &status); | |
768 | if(p<2) { | |
769 | if(U_FAILURE(status) && status==U_INVALID_FORMAT_ERROR){ | |
770 | log_verbose("Loads the data but rejects it as expected %s\n", myErrorName(status)); | |
771 | status=U_ZERO_ERROR; | |
772 | p++; | |
773 | } | |
774 | else { | |
775 | log_data_err("FAIL: failed to either load the data or to reject the loaded data. ERROR=%s\n", myErrorName(status) ); | |
776 | } | |
777 | } | |
778 | else if(p == 2) { | |
779 | if(U_FAILURE(status)) { | |
780 | log_data_err("FAIL: failed to load the data and accept it. ERROR=%s\n", myErrorName(status) ); | |
781 | } | |
782 | else { | |
783 | log_verbose("Loads the data and accepts it for p==2 as expected\n"); | |
784 | udata_close(result); | |
785 | } | |
786 | } | |
787 | } | |
788 | } | |
789 | ||
790 | static void TestUDataGetInfo() { | |
791 | ||
792 | UDataMemory *result; | |
793 | /* UDataInfo cf. udata.h */ | |
794 | static UDataInfo dataInfo={ | |
795 | 30, /*sizeof(UDataInfo),*/ | |
796 | 0, | |
797 | ||
798 | U_IS_BIG_ENDIAN, | |
799 | U_CHARSET_FAMILY, | |
800 | sizeof(UChar), | |
801 | 0, | |
802 | ||
803 | {0x54, 0x65, 0x73, 0x74}, /* dataFormat="Test" */ | |
804 | {9, 0, 0, 0}, /* formatVersion */ | |
805 | {4, 0, 0, 0} /* dataVersion */ | |
806 | }; | |
807 | UErrorCode status=U_ZERO_ERROR; | |
808 | const char* name="cnvalias"; | |
809 | const char* name2="test"; | |
810 | const char* type="icu"; | |
811 | ||
812 | const char* testPath=loadTestData(&status); | |
813 | if(U_FAILURE(status)) { | |
814 | log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status)); | |
815 | return; | |
816 | } | |
817 | ||
818 | log_verbose("Testing udata_getInfo() for cnvalias.icu\n"); | |
819 | result=udata_open(NULL, "icu", name, &status); | |
820 | if(U_FAILURE(status)){ | |
821 | log_data_err("FAIL: udata_open() failed for path = NULL, name=%s, type=%s, \n errorcode=%s\n", name, type, myErrorName(status)); | |
822 | return; | |
823 | } | |
824 | udata_getInfo(result, &dataInfo); | |
825 | if(dataInfo.size==20 && dataInfo.size!=30 && | |
826 | dataInfo.isBigEndian==U_IS_BIG_ENDIAN && | |
827 | dataInfo.charsetFamily==U_CHARSET_FAMILY && | |
828 | dataInfo.dataFormat[0]==0x43 && dataInfo.dataFormat[0]!=0x54 && /* dataFormat="CvAl" and not "Test". The values are set for cnvalias.dat*/ | |
829 | dataInfo.dataFormat[1]==0x76 && dataInfo.dataFormat[1]!=0x65 && | |
830 | dataInfo.dataFormat[2]==0x41 && dataInfo.dataFormat[2]!=0x73 && | |
831 | dataInfo.dataFormat[3]==0x6c && dataInfo.dataFormat[3]!=0x74 && | |
832 | dataInfo.formatVersion[0]!=9 && /*formatVersion is also set to the one for cnvalias*/ | |
833 | dataInfo.dataVersion[0]!=4 && /*dataVersion*/ | |
834 | dataInfo.dataVersion[1]!=0 ){ | |
835 | log_verbose("PASS: udata_getInfo() filled in the right values\n"); | |
836 | } else { | |
837 | log_err("FAIL: udata_getInfo() filled in the wrong values\n"); | |
838 | } | |
839 | udata_close(result); | |
840 | ||
841 | ||
842 | log_verbose("Testing udata_getInfo() for test.icu\n"); | |
843 | result=udata_open(testPath, type, name2, &status); | |
844 | if(U_FAILURE(status)) { | |
845 | log_data_err("FAIL: udata_open() failed for path=%s name2=%s, type=%s, \n errorcode=%s\n", testPath, name2, type, myErrorName(status)); | |
846 | return; | |
847 | } | |
848 | udata_getInfo(result, &dataInfo); | |
849 | if(dataInfo.size==20 && | |
850 | dataInfo.isBigEndian==U_IS_BIG_ENDIAN && | |
851 | dataInfo.charsetFamily==U_CHARSET_FAMILY && | |
852 | dataInfo.dataFormat[0]==0x54 && /* dataFormat="Test". The values are set for test.dat*/ | |
853 | dataInfo.dataFormat[1]==0x65 && | |
854 | dataInfo.dataFormat[2]==0x73 && | |
855 | dataInfo.dataFormat[3]==0x74 && | |
856 | dataInfo.formatVersion[0]==1 && /*formatVersion is also set to the one for test*/ | |
857 | dataInfo.dataVersion[0]==1 && /*dataVersion*/ | |
858 | dataInfo.dataVersion[1]==0 ) | |
859 | { | |
860 | log_verbose("PASS: udata_getInfo() filled in the right values\n"); | |
861 | } else { | |
862 | log_err("FAIL: udata_getInfo() filled in the wrong values\n"); | |
863 | } | |
864 | udata_close(result); | |
865 | } | |
866 | ||
867 | static void TestUDataGetMemory() { | |
868 | ||
869 | UDataMemory *result; | |
870 | const int32_t *table=NULL; | |
871 | uint16_t* intValue=0; | |
872 | UErrorCode status=U_ZERO_ERROR; | |
873 | const char* name="cnvalias"; | |
874 | const char* type; | |
875 | ||
876 | const char* name2="test"; | |
877 | ||
878 | const char* testPath = loadTestData(&status); | |
879 | if(U_FAILURE(status)) { | |
880 | log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status)); | |
881 | return; | |
882 | } | |
883 | ||
884 | type="icu"; | |
885 | log_verbose("Testing udata_getMemory() for \"cnvalias.icu\"\n"); | |
886 | result=udata_openChoice(NULL, type, name, isAcceptable1, NULL, &status); | |
887 | if(U_FAILURE(status)){ | |
888 | log_data_err("FAIL: udata_openChoice() failed for name=%s, type=%s, \n errorcode=%s\n", name, type, myErrorName(status)); | |
889 | return; | |
890 | } | |
891 | table=(const int32_t *)udata_getMemory(result); | |
892 | ||
893 | /* The alias table may list more converters than what's actually available now. [grhoten] */ | |
894 | if(ucnv_countAvailable() > table[1]) /*???*/ | |
895 | log_err("FAIL: udata_getMemory() failed ucnv_countAvailable returned = %d, expected = %d\n", ucnv_countAvailable(), table[1+2*(*table)]); | |
896 | ||
897 | udata_close(result); | |
898 | ||
899 | type="icu"; | |
900 | log_verbose("Testing udata_getMemory for \"test.icu\"()\n"); | |
901 | result=udata_openChoice(testPath, type, name2, isAcceptable3, NULL, &status); | |
902 | if(U_FAILURE(status)){ | |
903 | log_data_err("FAIL: udata_openChoice() failed for path=%s name=%s, type=%s, \n errorcode=%s\n", testPath, name2, type, myErrorName(status)); | |
904 | return; | |
905 | } | |
906 | intValue=(uint16_t *)udata_getMemory(result); | |
907 | /*printf("%d ..... %s", *(intValue), intValue+1));*/ | |
908 | if( *intValue != 2000 || strcmp((char*)(intValue+1), "YEAR") != 0 ) | |
909 | log_err("FAIL: udata_getMemory() failed: intValue :- Expected:2000 Got:%d \n\tstringValue:- Expected:YEAR Got:%s\n", *intValue, (intValue+1)); | |
910 | ||
911 | udata_close(result); | |
912 | ||
913 | } | |
914 | ||
915 | static void TestErrorConditions(){ | |
916 | ||
917 | UDataMemory *result=NULL; | |
918 | UErrorCode status=U_ZERO_ERROR; | |
919 | uint16_t* intValue=0; | |
920 | static UDataInfo dataInfo={ | |
921 | 30, /*sizeof(UDataInfo),*/ | |
922 | 0, | |
923 | ||
924 | U_IS_BIG_ENDIAN, | |
925 | U_CHARSET_FAMILY, | |
926 | sizeof(UChar), | |
927 | 0, | |
928 | ||
929 | {0x54, 0x65, 0x73, 0x74}, /* dataFormat="Test" */ | |
930 | {9, 0, 0, 0}, /* formatVersion */ | |
931 | {4, 0, 0, 0} /* dataVersion */ | |
932 | }; | |
933 | ||
934 | const char* name = "test"; | |
935 | const char* type="icu"; | |
936 | ||
937 | const char *testPath = loadTestData(&status); | |
938 | if(U_FAILURE(status)) { | |
939 | log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status)); | |
940 | return; | |
941 | } | |
942 | ||
943 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
944 | /*Try udata_open with status != U_ZERO_ERROR*/ | |
945 | log_verbose("Testing udata_open() with status != U_ZERO_ERROR\n"); | |
946 | result=udata_open(testPath, type, name, &status); | |
947 | if(result != NULL){ | |
948 | log_data_err("FAIL: udata_open() is supposed to fail for path = %s, name=%s, type=%s, \n errorcode !=U_ZERO_ERROR\n", testPath, name, type); | |
949 | udata_close(result); | |
950 | ||
951 | } else { | |
952 | log_verbose("PASS: udata_open with errorCode != U_ZERO_ERROR failed as expected\n"); | |
953 | } | |
954 | ||
955 | /*Try udata_open with data name=NULL*/ | |
956 | log_verbose("Testing udata_open() with data name=NULL\n"); | |
957 | status=U_ZERO_ERROR; | |
958 | result=udata_open(testPath, type, NULL, &status); | |
959 | if(U_FAILURE(status)){ | |
960 | if(status != U_ILLEGAL_ARGUMENT_ERROR || result != NULL){ | |
961 | log_err("FAIL: udata_open() with name=NULL should return NULL and errocode U_ILLEGAL_ARGUMENT_ERROR, GOT: errorcode=%s\n", myErrorName(status)); | |
962 | }else{ | |
963 | log_verbose("PASS: udata_open with name=NULL failed as expected and errorcode = %s as expected\n", myErrorName(status)); | |
964 | } | |
965 | }else{ | |
966 | log_err("FAIL: udata_open() with data name=NULL is supposed to fail for path = %s, name=NULL type=%s errorcode=U_ZERO_ERROR \n", testPath, type); | |
967 | udata_close(result); | |
968 | } | |
969 | ||
970 | ||
971 | /*Try udata_openChoice with status != U_ZERO_ERROR*/ | |
972 | log_verbose("Testing udata_openChoice() with status != U_ZERO_ERROR\n"); | |
973 | status=U_ILLEGAL_ARGUMENT_ERROR; | |
974 | result=udata_openChoice(testPath, type, name, isAcceptable3, NULL, &status); | |
975 | if(result != NULL){ | |
976 | log_err("FAIL: udata_openChoice() is supposed to fail for path = %s, name=%s, type=%s, \n errorcode != U_ZERO_ERROR\n", testPath, name, type); | |
977 | udata_close(result); | |
978 | } else { | |
979 | log_verbose("PASS: udata_openChoice() with errorCode != U_ZERO_ERROR failed as expected\n"); | |
980 | } | |
981 | ||
982 | /*Try udata_open with data name=NULL*/ | |
983 | log_verbose("Testing udata_openChoice() with data name=NULL\n"); | |
984 | status=U_ZERO_ERROR; | |
985 | result=udata_openChoice(testPath, type, NULL, isAcceptable3, NULL, &status); | |
986 | if(U_FAILURE(status)){ | |
987 | if(status != U_ILLEGAL_ARGUMENT_ERROR || result != NULL){ | |
988 | log_err("FAIL: udata_openChoice() with name=NULL should return NULL and errocode U_ILLEGAL_ARGUMENT_ERROR, GOT: errorcode=%s\n", myErrorName(status)); | |
989 | }else{ | |
990 | log_verbose("PASS: udata_openChoice with name=NULL failed as expected and errorcode = %s as expected\n", myErrorName(status)); | |
991 | } | |
992 | }else{ | |
993 | log_err("FAIL: udata_openChoice() with data name=NULL is supposed to fail for path = %s, name=NULL type=%s errorcode=U_ZERO_ERROR \n", testPath, type); | |
994 | udata_close(result); | |
995 | } | |
996 | ||
997 | /*Try udata_getMemory with UDataMemory=NULL*/ | |
998 | log_verbose("Testing udata_getMemory with UDataMemory=NULL\n"); | |
999 | intValue=(uint16_t*)udata_getMemory(NULL); | |
1000 | if(intValue != NULL){ | |
1001 | log_err("FAIL: udata_getMemory with UDataMemory = NULL is supposed to fail\n"); | |
1002 | } | |
1003 | ||
1004 | /*Try udata_getInfo with UDataMemory=NULL*/ | |
1005 | status=U_ZERO_ERROR; | |
1006 | udata_getInfo(NULL, &dataInfo); | |
1007 | if(dataInfo.size != 0){ | |
1008 | log_err("FAIL : udata_getInfo with UDataMemory = NULL us supposed to fail\n"); | |
1009 | } | |
1010 | ||
1011 | /*Try udata_openChoice with a non existing binary file*/ | |
1012 | log_verbose("Testing udata_openChoice() with a non existing binary file\n"); | |
1013 | result=udata_openChoice(testPath, "tst", "nonexist", isAcceptable3, NULL, &status); | |
1014 | if(status==U_FILE_ACCESS_ERROR){ | |
1015 | log_verbose("Opening udata_openChoice with non-existing file handled correctly.\n"); | |
1016 | status=U_ZERO_ERROR; | |
1017 | } else { | |
1018 | log_err("calling udata_open with non-existing file not handled correctly\n. Expected: U_FILE_ACCESS_ERROR, Got: %s\n", myErrorName(status)); | |
1019 | if(U_SUCCESS(status)) { | |
1020 | udata_close(result); | |
1021 | } | |
1022 | } | |
1023 | ||
1024 | if(result != NULL){ | |
1025 | log_err("calling udata_open with non-existing file didn't return a null value\n"); | |
1026 | } else { | |
1027 | log_verbose("calling udat_open with non-existing file returned null as expected\n"); | |
1028 | } | |
1029 | } | |
1030 | ||
1031 | /* Test whether apps and ICU can each have their own root.res */ | |
1032 | static void TestAppData() | |
1033 | { | |
1034 | UResourceBundle *icu, *app; | |
1035 | UResourceBundle *tmp = NULL; | |
1036 | UResourceBundle *tmp2 = NULL; | |
1037 | ||
1038 | const UChar *appString; | |
1039 | const UChar *icuString; | |
1040 | ||
1041 | int32_t len; | |
1042 | ||
1043 | UErrorCode status = U_ZERO_ERROR; | |
1044 | char testMsgBuf[256]; | |
1045 | ||
1046 | const char* testPath=loadTestData(&status); | |
1047 | if(U_FAILURE(status)) { | |
1048 | log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status)); | |
1049 | return; | |
1050 | } | |
1051 | ||
1052 | icu = ures_open(NULL, "root", &status); | |
1053 | if(U_FAILURE(status)) | |
1054 | { | |
1055 | log_data_err("%s:%d: Couldn't open root ICU bundle- %s", __FILE__, __LINE__, u_errorName(status)); | |
1056 | return; | |
1057 | } | |
1058 | /* log_info("Open icu root: %s size_%d\n", u_errorName(status), ures_getSize(icu)); */ | |
1059 | status = U_ZERO_ERROR; | |
1060 | ||
1061 | app = ures_open(testPath, "root", &status); | |
1062 | if(U_FAILURE(status)) | |
1063 | { | |
1064 | log_data_err("%s:%d: Couldn't open app ICU bundle [%s]- %s", __FILE__, __LINE__, testPath, u_errorName(status)); | |
1065 | return; | |
1066 | } | |
1067 | /* log_info("Open app: %s, size %d\n", u_errorName(status), ures_getSize(app)); */ | |
1068 | ||
1069 | tmp = ures_getByKey(icu, "Version", tmp, &status); | |
1070 | if(U_FAILURE(status)) | |
1071 | { | |
1072 | log_err("%s:%d: Couldn't get Version string from ICU root bundle- %s", __FILE__, __LINE__, u_errorName(status)); | |
1073 | return; | |
1074 | } | |
1075 | ||
1076 | icuString = ures_getString(tmp, &len, &status); | |
1077 | if(U_FAILURE(status)) | |
1078 | { | |
1079 | log_err("%s:%d: Couldn't get string from Version string from ICU root bundle- %s", __FILE__, __LINE__, u_errorName(status)); | |
1080 | return; | |
1081 | } | |
1082 | /* log_info("icuString=%p - %s\n", icuString, austrdup(icuString)); */ | |
1083 | ||
1084 | ||
1085 | tmp2 = ures_getByKey(app, "Version", tmp2, &status); | |
1086 | if(U_FAILURE(status)) | |
1087 | { | |
1088 | log_err("%s:%d: Couldn't get Version string from App root bundle- %s", __FILE__, __LINE__, u_errorName(status)); | |
1089 | return; | |
1090 | } | |
1091 | ||
1092 | appString = ures_getString(tmp2, &len, &status); | |
1093 | if(U_FAILURE(status)) | |
1094 | { | |
1095 | log_err("%s:%d: Couldn't get string from Version string from App root bundle- %s", __FILE__, __LINE__, u_errorName(status)); | |
1096 | return; | |
1097 | } | |
1098 | ||
1099 | /* log_info("appString=%p - %s\n", appString, austrdup(appString)); */ | |
1100 | ||
1101 | ||
1102 | if(!u_strcmp(icuString, appString)) | |
1103 | { | |
1104 | log_err("%s:%d: Error! Expected ICU and App root version strings to be DIFFERENT but they are both %s and %s\n", __FILE__, __LINE__, austrdup(icuString), | |
1105 | austrdup(appString)); | |
1106 | } | |
1107 | else | |
1108 | { | |
1109 | log_verbose("%s:%d: appstr=%s, icustr=%s\n", __FILE__, | |
1110 | __LINE__, u_austrcpy(testMsgBuf, appString), u_austrcpy(testMsgBuf, icuString)); | |
1111 | } | |
1112 | ||
1113 | ures_close(tmp); | |
1114 | ures_close(tmp2); | |
1115 | ures_close(icu); | |
1116 | ures_close(app); | |
1117 | } | |
1118 | #endif | |
1119 | ||
1120 | static void TestICUDataName() | |
1121 | { | |
1122 | UVersionInfo icuVersion; | |
1123 | char expectDataName[20]; | |
1124 | unsigned int expectLen = 8; | |
1125 | ||
1126 | char typeChar = '?'; | |
1127 | ||
1128 | /* Print out the version # we have .. */ | |
1129 | log_verbose("utypes.h says U_ICUDATA_NAME = %s\n", U_ICUDATA_NAME); | |
1130 | ||
1131 | /* Build up the version # we expect to get */ | |
1132 | u_getVersion(icuVersion); | |
1133 | ||
1134 | switch(U_CHARSET_FAMILY) | |
1135 | { | |
1136 | case U_ASCII_FAMILY: | |
1137 | switch((int)U_IS_BIG_ENDIAN) | |
1138 | { | |
1139 | case 1: | |
1140 | typeChar = 'b'; | |
1141 | break; | |
1142 | case 0: | |
1143 | typeChar = 'l'; | |
1144 | break; | |
1145 | default: | |
1146 | log_err("Expected 1 or 0 for U_IS_BIG_ENDIAN, got %d!\n", (int)U_IS_BIG_ENDIAN); | |
1147 | /* return; */ | |
1148 | } | |
1149 | break; | |
1150 | case U_EBCDIC_FAMILY: | |
1151 | typeChar = 'e'; | |
1152 | break; | |
1153 | } | |
1154 | ||
1155 | /* Only major number is needed. */ | |
1156 | sprintf(expectDataName, "%s%d%c", | |
1157 | "icudt", | |
1158 | (int)icuVersion[0], | |
1159 | typeChar); | |
1160 | ||
1161 | log_verbose("Expected: %s\n", expectDataName); | |
1162 | if(uprv_strlen(expectDataName) != expectLen) | |
1163 | { | |
1164 | log_err("*Expected* length is wrong (test err?), should be %d is %d\n", | |
1165 | expectLen, uprv_strlen(expectDataName)); | |
1166 | } | |
1167 | ||
1168 | if(uprv_strlen(U_ICUDATA_NAME) != expectLen) | |
1169 | { | |
1170 | log_err("U_ICUDATA_NAME length should be %d is %d\n", | |
1171 | expectLen, uprv_strlen(U_ICUDATA_NAME)); | |
1172 | } | |
1173 | ||
1174 | if(uprv_strcmp(U_ICUDATA_NAME, expectDataName)) | |
1175 | { | |
1176 | log_err("U_ICUDATA_NAME should be %s but is %s\n", | |
1177 | expectDataName, U_ICUDATA_NAME); | |
1178 | } | |
1179 | ||
1180 | /* ICUDATA_NAME comes from the build system on *nix */ | |
1181 | #ifdef ICUDATA_NAME | |
1182 | if(uprv_strcmp(U_ICUDATA_NAME, ICUDATA_NAME)) | |
1183 | { | |
1184 | log_err("ICUDATA_NAME and U_ICUDATA_NAME don't match: " | |
1185 | "ICUDATA_NAME=%s, U_ICUDATA_NAME=%s. Check configure.in, icudefs.mk.in, utypes.h...\n", ICUDATA_NAME, U_ICUDATA_NAME); | |
1186 | } | |
1187 | else | |
1188 | { | |
1189 | log_verbose("ICUDATA_NAME=%s (from icudefs.mk), U_ICUDATA_NAME=%s (from utypes.h)\n", ICUDATA_NAME, U_ICUDATA_NAME); | |
1190 | } | |
1191 | #endif | |
1192 | ||
1193 | } | |
1194 | ||
1195 | /* test data swapping ------------------------------------------------------- */ | |
1196 | ||
1197 | #if U_PLATFORM == U_PF_OS400 | |
1198 | /* See comments in genccode.c on when this special implementation can be removed. */ | |
1199 | static const struct { | |
1200 | double bogus; | |
1201 | const char *bytes; | |
1202 | } gOffsetTOCAppDataItem1={ 0.0, /* alignment bytes */ | |
1203 | "\x00\x14" /* sizeof(UDataInfo) *//* MappedData { */ | |
1204 | "\xda" | |
1205 | "\x27" /* } */ | |
1206 | "\x00\x14" /* sizeof(UDataInfo) *//* UDataInfo { */ | |
1207 | "\0\0" | |
1208 | "\1" /* U_IS_BIG_ENDIAN */ | |
1209 | "\1" /* U_CHARSET_FAMILY */ | |
1210 | "\2" /* U_SIZEOF_WHAR_T */ | |
1211 | "\0" | |
1212 | "\x31\x31\x31\x31" | |
1213 | "\0\0\0\0" | |
1214 | "\0\0\0\0" /* } */ | |
1215 | }; | |
1216 | #else | |
1217 | static const struct { | |
1218 | double bogus; | |
1219 | MappedData bytes1; | |
1220 | UDataInfo bytes2; | |
1221 | uint8_t bytes3; | |
1222 | } gOffsetTOCAppDataItem1={ | |
1223 | 0.0, /* alignment bytes */ | |
1224 | { sizeof(UDataInfo), 0xda, 0x27 }, /* MappedData */ | |
1225 | ||
1226 | {sizeof(UDataInfo), | |
1227 | 0, | |
1228 | ||
1229 | U_IS_BIG_ENDIAN, | |
1230 | U_CHARSET_FAMILY, | |
1231 | sizeof(UChar), | |
1232 | 0, | |
1233 | ||
1234 | {0x31, 0x31, 0x31, 0x31}, /* dataFormat="1111" */ | |
1235 | {0, 0, 0, 0}, /* formatVersion */ | |
1236 | {0, 0, 0, 0}} /* dataVersion */ | |
1237 | }; | |
1238 | #endif | |
1239 | ||
1240 | static const UChar gOffsetTOCGarbage[] = { /* "I have been very naughty!" */ | |
1241 | 0x49, 0x20, 0x68, 0x61, 0x76, 0x65, 0x20, 0x62, 0x65, 0x65, 0x6E, | |
1242 | 0x20, 0x76, 0x65, 0x72, 0x79, 0x20, 0x6E, 0x61, 0x75, 0x67, 0x68, 0x74, 0x79, 0x21 | |
1243 | }; | |
1244 | ||
1245 | /* Original source: icu/source/tools/genccode */ | |
1246 | static const struct { | |
1247 | uint16_t headerSize; | |
1248 | uint8_t magic1, magic2; | |
1249 | UDataInfo info; | |
1250 | char padding[8]; | |
1251 | uint32_t count, reserved; | |
1252 | const struct { | |
1253 | const char *const name; | |
1254 | const void *const data; | |
1255 | } toc[3]; | |
1256 | } gOffsetTOCAppData_dat = { | |
1257 | 32, /* headerSize */ | |
1258 | 0xda, /* magic1, (see struct MappedData in udata.c) */ | |
1259 | 0x27, /* magic2 */ | |
1260 | { /*UDataInfo */ | |
1261 | sizeof(UDataInfo), /* size */ | |
1262 | 0, /* reserved */ | |
1263 | U_IS_BIG_ENDIAN, | |
1264 | U_CHARSET_FAMILY, | |
1265 | sizeof(UChar), | |
1266 | 0, /* reserved */ | |
1267 | { /* data format identifier */ | |
1268 | 0x54, 0x6f, 0x43, 0x50}, /* "ToCP" */ | |
1269 | {1, 0, 0, 0}, /* format version major, minor, milli, micro */ | |
1270 | {0, 0, 0, 0} /* dataVersion */ | |
1271 | }, | |
1272 | {0,0,0,0,0,0,0,0}, /* Padding[8] */ | |
1273 | 3, /* count */ | |
1274 | 0, /* Reserved */ | |
1275 | { /* TOC structure */ | |
1276 | { "OffsetTOCAppData/a/b", &gOffsetTOCAppDataItem1 }, | |
1277 | { "OffsetTOCAppData/gOffsetTOCAppDataItem1", &gOffsetTOCAppDataItem1 }, | |
1278 | { "OffsetTOCAppData/gOffsetTOCGarbage", &gOffsetTOCGarbage } | |
1279 | } | |
1280 | }; | |
1281 | ||
1282 | /* Unfortunately, dictionaries are in a C++ header */ | |
1283 | U_CAPI int32_t U_EXPORT2 | |
1284 | udict_swap(const UDataSwapper *ds, const void *inData, int32_t length, void *outData, UErrorCode *pErrorCode); | |
1285 | ||
1286 | /* test cases for maximum data swapping code coverage */ | |
1287 | static const struct { | |
1288 | const char *name, *type; | |
1289 | UDataSwapFn *swapFn; | |
1290 | } swapCases[]={ | |
1291 | /* resource bundles */ | |
1292 | ||
1293 | /* resource bundle with many data types */ | |
1294 | {"*testtypes", "res", ures_swap}, | |
1295 | /* resource bundle with collation data */ | |
1296 | {"ja", "res", ures_swap}, | |
1297 | /* resource bundle with options-only collation data */ | |
1298 | {"ru", "res", ures_swap}, | |
1299 | {"el", "res", ures_swap}, | |
1300 | /* ICU's root */ | |
1301 | {"root", "res", ures_swap}, | |
1302 | /* Test a 32-bit key table. This is large. */ | |
1303 | {"*testtable32", "res", ures_swap}, | |
1304 | ||
1305 | /* ICU 4.2 resource bundle - data format 1.2 (little-endian ASCII) */ | |
1306 | {"*old_l_testtypes", "res", ures_swap}, | |
1307 | /* same for big-endian EBCDIC */ | |
1308 | {"*old_e_testtypes", "res", ures_swap}, | |
1309 | ||
1310 | #if !UCONFIG_NO_COLLATION | |
1311 | /* standalone collation data files */ | |
1312 | {"ucadata", "icu", ucol_swap}, | |
1313 | #if 0 | |
1314 | /* Starting with ICU 53, the "inverse UCA" data is integrated into ucadata.icu. */ | |
1315 | {"invuca", "icu", ucol_swapInverseUCA}, | |
1316 | #endif | |
1317 | #endif | |
1318 | ||
1319 | #if !UCONFIG_NO_LEGACY_CONVERSION | |
1320 | /* conversion table files */ | |
1321 | ||
1322 | /* SBCS conversion table file without extension */ | |
1323 | {"ibm-913_P100-2000", "cnv", ucnv_swap}, | |
1324 | /* EBCDIC_STATEFUL conversion table file with extension */ | |
1325 | {"ibm-1390_P110-2003", "cnv", ucnv_swap}, | |
1326 | /* DBCS extension-only conversion table file */ | |
1327 | {"ibm-16684_P110-2003", "cnv", ucnv_swap}, | |
1328 | /* EUC-TW (3-byte) conversion table file without extension */ | |
1329 | {"ibm-964_P110-1999", "cnv", ucnv_swap}, | |
1330 | /* GB 18030 (4-byte) conversion table file without extension */ | |
1331 | {"gb18030", "cnv", ucnv_swap}, | |
1332 | /* MBCS conversion table file with extension */ | |
1333 | {"*test4x", "cnv", ucnv_swap}, | |
1334 | /* | |
1335 | * MBCS conversion table file without extension, | |
1336 | * to test swapping and preflighting of UTF-8-friendly mbcsIndex[]. | |
1337 | */ | |
1338 | {"jisx-212", "cnv", ucnv_swap}, | |
1339 | #endif | |
1340 | ||
1341 | #if !UCONFIG_NO_CONVERSION | |
1342 | /* alias table */ | |
1343 | {"cnvalias", "icu", ucnv_swapAliases}, | |
1344 | #endif | |
1345 | ||
1346 | #if !UCONFIG_NO_IDNA | |
1347 | {"rfc3491", "spp", usprep_swap}, | |
1348 | #endif | |
1349 | ||
1350 | #if !UCONFIG_NO_BREAK_ITERATION | |
1351 | {"char", "brk", ubrk_swap}, | |
1352 | {"thaidict", "dict",udict_swap}, | |
1353 | #endif | |
1354 | ||
1355 | #if 0 | |
1356 | /* | |
1357 | * Starting with ICU 4.8, the Unicode property (value) aliases data | |
1358 | * is hardcoded in the ICU4C common library. | |
1359 | * The swapper was moved to the toolutil library for swapping for ICU4J. | |
1360 | */ | |
1361 | /* Unicode properties */ | |
1362 | {"pnames", "icu", upname_swap}, | |
1363 | #endif | |
1364 | ||
1365 | #if 0 | |
1366 | /* | |
1367 | * Starting with ICU4C 3.4, the core Unicode properties files | |
1368 | * (uprops.icu, ucase.icu, ubidi.icu, unorm.icu) | |
1369 | * are hardcoded in the common DLL and therefore not included | |
1370 | * in the data package any more. | |
1371 | * Their swapping code is moved from the common DLL to the icuswap tool so that | |
1372 | * we need not jump through hoops (like adding snapshots of these files | |
1373 | * to testdata) for code coverage in tests. | |
1374 | * See Jitterbug 4497. | |
1375 | * | |
1376 | * ICU4C 4.4 adds normalization data files again, e.g., nfkc.nrm. | |
1377 | */ | |
1378 | {"uprops", "icu", uprops_swap}, | |
1379 | {"ucase", "icu", ucase_swap}, | |
1380 | {"ubidi", "icu", ubidi_swap}, | |
1381 | #endif | |
1382 | #if !UCONFIG_NO_NORMALIZATION && !UCONFIG_ONLY_COLLATION | |
1383 | {"nfkc", "nrm", unorm2_swap}, | |
1384 | #if !UCONFIG_NO_REGULAR_EXPRESSIONS | |
1385 | {"confusables", "cfu", uspoof_swap}, /* spoof data missing without regex */ | |
1386 | #endif | |
1387 | ||
1388 | #endif | |
1389 | {"unames", "icu", uchar_swapNames} | |
1390 | /* the last item should not be #if'ed so that it can reliably omit the last comma */ | |
1391 | }; | |
1392 | ||
1393 | /* Large enough for the largest swappable data item. */ | |
1394 | #define SWAP_BUFFER_SIZE 1800000 | |
1395 | ||
1396 | static void U_CALLCONV | |
1397 | printError(void *context, const char *fmt, va_list args) { | |
1398 | vlog_info("[swap] ", fmt, args); | |
1399 | log_err("\n"); /* Register error */ | |
1400 | } | |
1401 | ||
1402 | static void | |
1403 | TestSwapCase(UDataMemory *pData, const char *name, | |
1404 | UDataSwapFn *swapFn, | |
1405 | uint8_t *buffer, uint8_t *buffer2) { | |
1406 | UDataSwapper *ds; | |
1407 | const void *inData, *inHeader; | |
1408 | int32_t length, dataLength, length2, headerLength; | |
1409 | ||
1410 | UErrorCode errorCode; | |
1411 | UErrorCode badStatus; | |
1412 | ||
1413 | UBool inEndian, oppositeEndian; | |
1414 | uint8_t inCharset, oppositeCharset; | |
1415 | ||
1416 | /* First we check that swapFn handles failures as expected. */ | |
1417 | errorCode = U_UNSUPPORTED_ERROR; | |
1418 | length = swapFn(NULL, NULL, 0, buffer, &errorCode); | |
1419 | if (length != 0 || errorCode != U_UNSUPPORTED_ERROR) { | |
1420 | log_err("%s() did not fail as expected - %s\n", name, u_errorName(errorCode)); | |
1421 | } | |
1422 | errorCode = U_ZERO_ERROR; | |
1423 | length = swapFn(NULL, NULL, 0, buffer, &errorCode); | |
1424 | if (length != 0 || errorCode != U_ILLEGAL_ARGUMENT_ERROR) { | |
1425 | log_err("%s() did not fail as expected with bad arguments - %s\n", name, u_errorName(errorCode)); | |
1426 | } | |
1427 | ||
1428 | ||
1429 | /* Continue with the rest of the tests. */ | |
1430 | errorCode = U_ZERO_ERROR; | |
1431 | inData=udata_getMemory(pData); | |
1432 | ||
1433 | /* | |
1434 | * get the data length if possible, to verify that swapping and preflighting | |
1435 | * handles the entire data | |
1436 | */ | |
1437 | dataLength=udata_getLength(pData); | |
1438 | ||
1439 | /* | |
1440 | * get the header and its length | |
1441 | * all of the swap implementation functions require the header to be included | |
1442 | */ | |
1443 | inHeader=udata_getRawMemory(pData); | |
1444 | headerLength=(int32_t)((const char *)inData-(const char *)inHeader); | |
1445 | ||
1446 | /* first swap to opposite endianness but same charset family */ | |
1447 | errorCode=U_ZERO_ERROR; | |
1448 | ds=udata_openSwapperForInputData(inHeader, headerLength, | |
1449 | !U_IS_BIG_ENDIAN, U_CHARSET_FAMILY, &errorCode); | |
1450 | if(U_FAILURE(errorCode)) { | |
1451 | log_err("udata_openSwapperForInputData(%s->!isBig+same charset) failed - %s\n", | |
1452 | name, u_errorName(errorCode)); | |
1453 | return; | |
1454 | } | |
1455 | ||
1456 | inEndian=ds->inIsBigEndian; | |
1457 | inCharset=ds->inCharset; | |
1458 | ||
1459 | oppositeEndian=!inEndian; | |
1460 | oppositeCharset= inCharset==U_ASCII_FAMILY ? U_EBCDIC_FAMILY : U_ASCII_FAMILY; | |
1461 | ||
1462 | /* make this test work with data files that are built for a different platform */ | |
1463 | if(inEndian!=U_IS_BIG_ENDIAN || inCharset!=U_CHARSET_FAMILY) { | |
1464 | udata_closeSwapper(ds); | |
1465 | ds=udata_openSwapper(inEndian, inCharset, oppositeEndian, inCharset, &errorCode); | |
1466 | if(U_FAILURE(errorCode)) { | |
1467 | log_err("udata_openSwapper(%s->!isBig+same charset) failed - %s\n", | |
1468 | name, u_errorName(errorCode)); | |
1469 | return; | |
1470 | } | |
1471 | } | |
1472 | ||
1473 | /* | |
1474 | Check error checking of swappable data not specific to this swapper. | |
1475 | This should always fail. | |
1476 | */ | |
1477 | badStatus = U_ZERO_ERROR; | |
1478 | length=swapFn(ds, &gOffsetTOCAppData_dat, -1, NULL, &badStatus); | |
1479 | if(badStatus != U_UNSUPPORTED_ERROR) { | |
1480 | log_err("swapFn(%s->!isBig+same charset) unexpectedly succeeded on bad data - %s\n", | |
1481 | name, u_errorName(errorCode)); | |
1482 | udata_closeSwapper(ds); | |
1483 | return; | |
1484 | } | |
1485 | ||
1486 | /* Now allow errors to be printed */ | |
1487 | ds->printError=printError; | |
1488 | ||
1489 | /* preflight the length */ | |
1490 | length=swapFn(ds, inHeader, -1, NULL, &errorCode); | |
1491 | if(U_FAILURE(errorCode)) { | |
1492 | log_err("swapFn(preflight %s->!isBig+same charset) failed - %s\n", | |
1493 | name, u_errorName(errorCode)); | |
1494 | udata_closeSwapper(ds); | |
1495 | return; | |
1496 | } | |
1497 | ||
1498 | /* compare the preflighted length against the data length */ | |
1499 | if(dataLength>=0 && (length+15)<(headerLength+dataLength)) { | |
1500 | log_err("swapFn(preflight %s->!isBig+same charset) length too small: %d < data length %d\n", | |
1501 | name, length, (headerLength+dataLength)); | |
1502 | udata_closeSwapper(ds); | |
1503 | return; | |
1504 | } | |
1505 | ||
1506 | /* swap, not in-place */ | |
1507 | length2=swapFn(ds, inHeader, length, buffer, &errorCode); | |
1508 | udata_closeSwapper(ds); | |
1509 | if(U_FAILURE(errorCode)) { | |
1510 | log_err("swapFn(%s->!isBig+same charset) failed - %s\n", | |
1511 | name, u_errorName(errorCode)); | |
1512 | return; | |
1513 | } | |
1514 | ||
1515 | /* compare the swap length against the preflighted length */ | |
1516 | if(length2!=length) { | |
1517 | log_err("swapFn(%s->!isBig+same charset) length differs from preflighting: %d != preflighted %d\n", | |
1518 | name, length2, length); | |
1519 | return; | |
1520 | } | |
1521 | ||
1522 | /* next swap to opposite charset family */ | |
1523 | ds=udata_openSwapper(oppositeEndian, inCharset, | |
1524 | oppositeEndian, oppositeCharset, | |
1525 | &errorCode); | |
1526 | if(U_FAILURE(errorCode)) { | |
1527 | log_err("udata_openSwapper(%s->!isBig+other charset) failed - %s\n", | |
1528 | name, u_errorName(errorCode)); | |
1529 | return; | |
1530 | } | |
1531 | ds->printError=printError; | |
1532 | ||
1533 | /* swap in-place */ | |
1534 | length2=swapFn(ds, buffer, length, buffer, &errorCode); | |
1535 | udata_closeSwapper(ds); | |
1536 | if(U_FAILURE(errorCode)) { | |
1537 | log_err("swapFn(%s->!isBig+other charset) failed - %s\n", | |
1538 | name, u_errorName(errorCode)); | |
1539 | return; | |
1540 | } | |
1541 | ||
1542 | /* compare the swap length against the original length */ | |
1543 | if(length2!=length) { | |
1544 | log_err("swapFn(%s->!isBig+other charset) length differs from original: %d != original %d\n", | |
1545 | name, length2, length); | |
1546 | return; | |
1547 | } | |
1548 | ||
1549 | /* finally swap to original platform values */ | |
1550 | ds=udata_openSwapper(oppositeEndian, oppositeCharset, | |
1551 | inEndian, inCharset, | |
1552 | &errorCode); | |
1553 | if(U_FAILURE(errorCode)) { | |
1554 | log_err("udata_openSwapper(%s->back to original) failed - %s\n", | |
1555 | name, u_errorName(errorCode)); | |
1556 | return; | |
1557 | } | |
1558 | ds->printError=printError; | |
1559 | ||
1560 | /* swap, not in-place */ | |
1561 | length2=swapFn(ds, buffer, length, buffer2, &errorCode); | |
1562 | udata_closeSwapper(ds); | |
1563 | if(U_FAILURE(errorCode)) { | |
1564 | log_err("swapFn(%s->back to original) failed - %s\n", | |
1565 | name, u_errorName(errorCode)); | |
1566 | return; | |
1567 | } | |
1568 | ||
1569 | /* compare the swap length against the original length */ | |
1570 | if(length2!=length) { | |
1571 | log_err("swapFn(%s->back to original) length differs from original: %d != original %d\n", | |
1572 | name, length2, length); | |
1573 | return; | |
1574 | } | |
1575 | ||
1576 | /* compare the final contents with the original */ | |
1577 | if(0!=uprv_memcmp(inHeader, buffer2, length)) { | |
1578 | const uint8_t *original; | |
1579 | uint8_t diff[8]; | |
1580 | int32_t i, j; | |
1581 | ||
1582 | log_err("swapFn(%s->back to original) contents differs from original\n", | |
1583 | name); | |
1584 | ||
1585 | /* find the first difference */ | |
1586 | original=(const uint8_t *)inHeader; | |
1587 | for(i=0; i<length && original[i]==buffer2[i]; ++i) {} | |
1588 | ||
1589 | /* find the next byte that is the same */ | |
1590 | for(j=i+1; j<length && original[j]!=buffer2[j]; ++j) {} | |
1591 | log_info(" difference at index %d=0x%x, until index %d=0x%x\n", i, i, j, j); | |
1592 | ||
1593 | /* round down to the last 4-boundary for better result output */ | |
1594 | i&=~3; | |
1595 | log_info("showing bytes from index %d=0x%x (length %d=0x%x):\n", i, i, length, length); | |
1596 | ||
1597 | /* print 8 bytes but limit to the buffer contents */ | |
1598 | length2=i+sizeof(diff); | |
1599 | if(length2>length) { | |
1600 | length2=length; | |
1601 | } | |
1602 | ||
1603 | /* print the original bytes */ | |
1604 | uprv_memset(diff, 0, sizeof(diff)); | |
1605 | for(j=i; j<length2; ++j) { | |
1606 | diff[j-i]=original[j]; | |
1607 | } | |
1608 | log_info(" original: %02x %02x %02x %02x %02x %02x %02x %02x\n", | |
1609 | diff[0], diff[1], diff[2], diff[3], diff[4], diff[5], diff[6], diff[7]); | |
1610 | ||
1611 | /* print the swapped bytes */ | |
1612 | uprv_memset(diff, 0, sizeof(diff)); | |
1613 | for(j=i; j<length2; ++j) { | |
1614 | diff[j-i]=buffer2[j]; | |
1615 | } | |
1616 | log_info(" swapped: %02x %02x %02x %02x %02x %02x %02x %02x\n", | |
1617 | diff[0], diff[1], diff[2], diff[3], diff[4], diff[5], diff[6], diff[7]); | |
1618 | } | |
1619 | } | |
1620 | ||
1621 | static void U_CALLCONV | |
1622 | printErrorToString(void *context, const char *fmt, va_list args) { | |
1623 | vsprintf((char *)context, fmt, args); | |
1624 | } | |
1625 | ||
1626 | #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION | |
1627 | static void | |
1628 | TestSwapData() { | |
1629 | char name[100]; | |
1630 | UDataSwapper *ds; | |
1631 | UDataMemory *pData; | |
1632 | uint8_t *buffer; | |
1633 | const char *pkg, *nm, *testPath; | |
1634 | UErrorCode errorCode = U_ZERO_ERROR; | |
1635 | int32_t i; | |
1636 | ||
1637 | buffer=(uint8_t *)malloc(2*SWAP_BUFFER_SIZE); | |
1638 | if(buffer==NULL) { | |
1639 | log_err("unable to allocate %d bytes\n", 2*SWAP_BUFFER_SIZE); | |
1640 | return; | |
1641 | } | |
1642 | ||
1643 | testPath=loadTestData(&errorCode); | |
1644 | if(U_FAILURE(errorCode)) { | |
1645 | log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(errorCode)); | |
1646 | } | |
1647 | ||
1648 | /* Test that printError works as expected. */ | |
1649 | errorCode=U_USELESS_COLLATOR_ERROR; | |
1650 | ds=udata_openSwapper(U_IS_BIG_ENDIAN, U_ASCII_FAMILY, | |
1651 | !U_IS_BIG_ENDIAN, U_ASCII_FAMILY, | |
1652 | &errorCode); | |
1653 | if (ds != NULL || errorCode != U_USELESS_COLLATOR_ERROR) { | |
1654 | log_err("udata_openSwapper should have returned NULL with bad argument\n", name); | |
1655 | } | |
1656 | errorCode=U_ZERO_ERROR; | |
1657 | ds=udata_openSwapper(U_IS_BIG_ENDIAN, U_ASCII_FAMILY, | |
1658 | !U_IS_BIG_ENDIAN, U_ASCII_FAMILY, | |
1659 | &errorCode); | |
1660 | ds->printError=printErrorToString; | |
1661 | ds->printErrorContext=name; | |
1662 | udata_printError(ds, "This %s a %s", "is", "test"); | |
1663 | udata_closeSwapper(ds); | |
1664 | if (strcmp(name, "This is a test") != 0) { | |
1665 | log_err("udata_printError can't properly print error messages. Got = %s\n", name); | |
1666 | } | |
1667 | errorCode = U_USELESS_COLLATOR_ERROR; | |
1668 | ds=udata_openSwapperForInputData(NULL, 0, | |
1669 | !U_IS_BIG_ENDIAN, U_ASCII_FAMILY, | |
1670 | &errorCode); | |
1671 | if (ds != NULL || errorCode != U_USELESS_COLLATOR_ERROR) { | |
1672 | log_err("udata_openSwapperForInputData should have returned NULL with bad argument\n", name); | |
1673 | } | |
1674 | errorCode=U_ZERO_ERROR; | |
1675 | ds=udata_openSwapperForInputData(NULL, 0, | |
1676 | !U_IS_BIG_ENDIAN, U_ASCII_FAMILY, | |
1677 | &errorCode); | |
1678 | if (ds != NULL || errorCode != U_ILLEGAL_ARGUMENT_ERROR) { | |
1679 | log_err("udata_openSwapperForInputData should have returned NULL with bad argument\n", name); | |
1680 | } | |
1681 | errorCode=U_ZERO_ERROR; | |
1682 | memset(buffer, 0, sizeof(2*SWAP_BUFFER_SIZE)); | |
1683 | ds=udata_openSwapperForInputData(buffer, 2*SWAP_BUFFER_SIZE, | |
1684 | !U_IS_BIG_ENDIAN, U_ASCII_FAMILY, | |
1685 | &errorCode); | |
1686 | if (ds != NULL || errorCode != U_UNSUPPORTED_ERROR) { | |
1687 | log_err("udata_openSwapperForInputData should have returned NULL with bad argument\n", name); | |
1688 | } | |
1689 | errorCode=U_ZERO_ERROR; | |
1690 | ||
1691 | /* Test argument checking. ucol_swap is normally tested via ures_swap, and isn't normally called directly. */ | |
1692 | #if !UCONFIG_NO_COLLATION | |
1693 | ucol_swap(NULL, NULL, -1, NULL, &errorCode); | |
1694 | if (errorCode != U_ILLEGAL_ARGUMENT_ERROR) { | |
1695 | log_err("ucol_swap did not fail as expected\n", name); | |
1696 | } | |
1697 | errorCode=U_ZERO_ERROR; | |
1698 | #endif | |
1699 | ||
1700 | for(i=0; i<UPRV_LENGTHOF(swapCases); ++i) { | |
1701 | /* build the name for logging */ | |
1702 | errorCode=U_ZERO_ERROR; | |
1703 | if(swapCases[i].name[0]=='*') { | |
1704 | pkg=testPath; | |
1705 | nm=swapCases[i].name+1; | |
1706 | uprv_strcpy(name, "testdata"); | |
1707 | } else if (uprv_strcmp(swapCases[i].type, "brk")==0 | |
1708 | || uprv_strcmp(swapCases[i].type, "dict")==0) { | |
1709 | pkg=U_ICUDATA_BRKITR; | |
1710 | nm=swapCases[i].name; | |
1711 | uprv_strcpy(name, U_ICUDATA_BRKITR); | |
1712 | #if !UCONFIG_NO_COLLATION | |
1713 | } else if (uprv_strcmp(swapCases[i].name, "ucadata")==0 | |
1714 | || uprv_strcmp(swapCases[i].name, "invuca")==0) { | |
1715 | pkg=U_ICUDATA_COLL; | |
1716 | nm=swapCases[i].name; | |
1717 | uprv_strcpy(name, U_ICUDATA_COLL); | |
1718 | #endif /* !UCONFIG_NO_COLLATION */ | |
1719 | } else { | |
1720 | pkg=NULL; | |
1721 | nm=swapCases[i].name; | |
1722 | uprv_strcpy(name, "NULL"); | |
1723 | } | |
1724 | uprv_strcat(name, "/"); | |
1725 | uprv_strcat(name, nm); | |
1726 | uprv_strcat(name, "."); | |
1727 | uprv_strcat(name, swapCases[i].type); | |
1728 | ||
1729 | pData=udata_open(pkg, swapCases[i].type, nm, &errorCode); | |
1730 | ||
1731 | if(U_SUCCESS(errorCode)) { | |
1732 | TestSwapCase(pData, name, swapCases[i].swapFn, buffer, buffer+SWAP_BUFFER_SIZE); | |
1733 | udata_close(pData); | |
1734 | } else { | |
1735 | log_data_err("udata_open(%s) failed - %s\n", | |
1736 | name, u_errorName(errorCode)); | |
1737 | } | |
1738 | } | |
1739 | ||
1740 | free(buffer); | |
1741 | } | |
1742 | #endif | |
1743 | ||
1744 | static void PointerTableOfContents() { | |
1745 | UDataMemory *dataItem; | |
1746 | UErrorCode status=U_ZERO_ERROR; | |
1747 | ||
1748 | /* | |
1749 | * Got testdata.dat into memory, now we try setAppData using the memory image. | |
1750 | */ | |
1751 | ||
1752 | status=U_ZERO_ERROR; | |
1753 | udata_setAppData("OffsetTOCAppData", &gOffsetTOCAppData_dat, &status); | |
1754 | if (status != U_ZERO_ERROR) { | |
1755 | log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData1\", fileBuf, status) \n" | |
1756 | " returned status of %s\n", u_errorName(status)); | |
1757 | return; | |
1758 | } | |
1759 | ||
1760 | dataItem = udata_open("OffsetTOCAppData", "", "gOffsetTOCAppDataItem1", &status); | |
1761 | if (U_FAILURE(status)) { | |
1762 | log_err("FAIL: gOffsetTOCAppDataItem1 could not be opened. status = %s\n", u_errorName(status)); | |
1763 | } | |
1764 | if (udata_getMemory(dataItem) != NULL) { | |
1765 | log_verbose("FAIL: udata_getMemory(dataItem) passed\n"); | |
1766 | } | |
1767 | else { | |
1768 | log_err("FAIL: udata_getMemory returned NULL\n", u_errorName(status)); | |
1769 | } | |
1770 | udata_close(dataItem); | |
1771 | ||
1772 | dataItem = udata_open("OffsetTOCAppData-a", "", "b", &status); | |
1773 | if (U_FAILURE(status)) { | |
1774 | log_err("FAIL: gOffsetTOCAppDataItem1 in tree \"a\" could not be opened. status = %s\n", u_errorName(status)); | |
1775 | } | |
1776 | if (udata_getMemory(dataItem) != NULL) { | |
1777 | log_verbose("FAIL: udata_getMemory(dataItem) in tree \"a\" passed\n"); | |
1778 | } | |
1779 | else { | |
1780 | log_err("FAIL: udata_getMemory returned NULL\n", u_errorName(status)); | |
1781 | } | |
1782 | udata_close(dataItem); | |
1783 | ||
1784 | dataItem = udata_open("OffsetTOCAppData", "", "gOffsetTOCGarbage", &status); | |
1785 | if (U_SUCCESS(status)) { | |
1786 | log_err("FAIL: gOffsetTOCGarbage should not be opened. status = %s\n", u_errorName(status)); | |
1787 | } | |
1788 | dataItem = udata_open("OffsetTOCAppData", "", "gOffsetTOCNonExistent", &status); | |
1789 | if (U_SUCCESS(status)) { | |
1790 | log_err("FAIL: gOffsetTOCNonExistent should not be found. status = %s\n", u_errorName(status)); | |
1791 | } | |
1792 | ||
1793 | } | |
1794 | ||
1795 | static void SetBadCommonData(void) { | |
1796 | /* It's difficult to test that udata_setCommonData really works within the test framework. | |
1797 | So we just test that foolish people can't do bad things. */ | |
1798 | UErrorCode status; | |
1799 | char badBuffer[sizeof(gOffsetTOCAppData_dat)]; | |
1800 | ||
1801 | memset(badBuffer, 0, sizeof(badBuffer)); | |
1802 | strcpy(badBuffer, "Hello! I'm not good data."); | |
1803 | ||
1804 | /* Check that we don't do anything */ | |
1805 | status = U_FILE_ACCESS_ERROR; | |
1806 | udata_setCommonData(&gOffsetTOCAppData_dat, &status); | |
1807 | if (status != U_FILE_ACCESS_ERROR) { | |
1808 | log_err("FAIL: udata_setCommonData changed the failure code.\n"); | |
1809 | } | |
1810 | /* Check that we fail correctly */ | |
1811 | status = U_ZERO_ERROR; | |
1812 | udata_setCommonData(NULL, &status); | |
1813 | if (status != U_ILLEGAL_ARGUMENT_ERROR) { | |
1814 | log_err("FAIL: udata_setCommonData did not fail with bad arguments.\n"); | |
1815 | } | |
1816 | ||
1817 | /* Check that we verify that the data isn't bad */ | |
1818 | status = U_ZERO_ERROR; | |
1819 | udata_setAppData("invalid path", badBuffer, &status); | |
1820 | if (status != U_INVALID_FORMAT_ERROR) { | |
1821 | log_err("FAIL: udata_setAppData doesn't verify data validity.\n"); | |
1822 | } | |
1823 | } | |
1824 | ||
1825 | // Check the override loading of time zone .res files from a specified path | |
1826 | // | |
1827 | // Hand testing notes: | |
1828 | // 1. Run this test with the environment variable set. The following should induce faiures: | |
1829 | // ICU_TIMEZONE_FILES_DIR=../testdata/out/build LD_LIBRARY_PATH=../../lib:../../stubdata:../../tools/ctestfw:$LD_LIBRARY_PATH ./cintltst /udatatst/TestTZDataDir | |
1830 | // 2. Build ICU with with U_TIMEZONE_FILES_DIR defined. This should also induce failures. | |
1831 | // CPPFLAGS=-DU_TIMEZONE_FILES_DIR\=`pwd`/test/testdata/out/testdata ./runConfigureICU Linux | |
1832 | // make check | |
1833 | ||
1834 | static void TestTZDataDir(void) { | |
1835 | #if !UCONFIG_NO_FORMATTING | |
1836 | UErrorCode status = U_ZERO_ERROR; | |
1837 | const char *tzDataVersion; | |
1838 | const char *testDataPath; | |
1839 | ||
1840 | // Verify that default ICU time zone data version is something newer than 2014a. | |
1841 | tzDataVersion = ucal_getTZDataVersion(&status); | |
1842 | // printf("tz data version is %s\n", tzDataVersion); | |
1843 | if (U_FAILURE(status)) { | |
1844 | log_data_err("Failed call to ucal_getTZDataVersion - %s\n", u_errorName(status)); | |
1845 | return; | |
1846 | } else if (strcmp("2014a", tzDataVersion) == 0) { | |
1847 | log_err("File %s:%d - expected something newer than time zone data 2014a.\n", __FILE__, __LINE__, tzDataVersion); | |
1848 | } | |
1849 | ||
1850 | testDataPath = loadTestData(&status); | |
1851 | // The path produced by loadTestData() will look something like | |
1852 | // whatever/.../testdata/out/testdata | |
1853 | // The test data puts an old (2014a) version of the time zone data there. | |
1854 | ||
1855 | // Switch ICU to the testdata version of zoneinfo64.res, which is verison 2014a. | |
1856 | ctest_resetICU(); | |
1857 | u_setTimeZoneFilesDirectory(testDataPath, &status); | |
1858 | tzDataVersion = ucal_getTZDataVersion(&status); | |
1859 | if (strcmp("2014a", tzDataVersion) != 0) { | |
1860 | log_err("File %s:%d - expected \"2014a\"; actual \"%s\"\n", __FILE__, __LINE__, tzDataVersion); | |
1861 | } | |
1862 | ||
1863 | ctest_resetICU(); // Return ICU to using its standard tz data. | |
1864 | tzDataVersion = ucal_getTZDataVersion(&status); | |
1865 | // printf("tz data version is %s\n", tzDataVersion); | |
1866 | if (strcmp("2014a", tzDataVersion) == 0) { | |
1867 | log_err("File %s:%d - expected something newer than time zone data 2014a.\n", __FILE__, __LINE__, tzDataVersion); | |
1868 | } | |
1869 | #endif | |
1870 | } |