1 /********************************************************************
3 * Copyright (c) 1998-2016, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
9 * Modification History:
11 * Date Name Description
12 * 02/22/2000 Madhu Creation
13 ******************************************************************************
16 #include "unicode/utypes.h"
17 #include "unicode/putil.h"
18 #include "unicode/udata.h"
19 #include "unicode/ucal.h"
20 #include "unicode/uchar.h"
21 #include "unicode/ucnv.h"
22 #include "unicode/ures.h"
23 #include "unicode/ustring.h"
24 #include "unicode/uclean.h"
31 #include "toolutil.h" /* for uprv_fileExists() */
35 /* includes for TestSwapData() */
38 /* swapping implementations in common */
49 /* swapping implementation in i18n */
50 #include "uspoof_impl.h"
52 U_CAPI
int32_t U_EXPORT2
53 unorm2_swap(const UDataSwapper
*ds
,
54 const void *inData
, int32_t length
, void *outData
,
55 UErrorCode
*pErrorCode
);
57 /* other definitions and prototypes */
59 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
60 static void TestUDataOpen(void);
61 static void TestUDataOpenChoiceDemo1(void);
62 static void TestUDataOpenChoiceDemo2(void);
63 static void TestUDataGetInfo(void);
64 static void TestUDataGetMemory(void);
65 static void TestErrorConditions(void);
66 static void TestAppData(void);
67 static void TestSwapData(void);
69 static void TestUDataSetAppData(void);
70 static void TestICUDataName(void);
71 static void PointerTableOfContents(void);
72 static void SetBadCommonData(void);
73 static void TestUDataFileAccess(void);
74 #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
75 static void TestTZDataDir(void);
78 void addUDataTest(TestNode
** root
);
81 addUDataTest(TestNode
** root
)
83 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
84 addTest(root
, &TestUDataOpen
, "udatatst/TestUDataOpen" );
85 addTest(root
, &TestUDataOpenChoiceDemo1
, "udatatst/TestUDataOpenChoiceDemo1");
86 addTest(root
, &TestUDataOpenChoiceDemo2
, "udatatst/TestUDataOpenChoiceDemo2");
87 addTest(root
, &TestUDataGetInfo
, "udatatst/TestUDataGetInfo" );
88 addTest(root
, &TestUDataGetMemory
, "udatatst/TestUDataGetMemory" );
89 addTest(root
, &TestErrorConditions
, "udatatst/TestErrorConditions");
90 addTest(root
, &TestAppData
, "udatatst/TestAppData" );
91 addTest(root
, &TestSwapData
, "udatatst/TestSwapData" );
93 addTest(root
, &TestUDataSetAppData
, "udatatst/TestUDataSetAppData" );
94 addTest(root
, &TestICUDataName
, "udatatst/TestICUDataName" );
95 addTest(root
, &PointerTableOfContents
, "udatatst/PointerTableOfContents" );
96 addTest(root
, &SetBadCommonData
, "udatatst/SetBadCommonData" );
97 addTest(root
, &TestUDataFileAccess
, "udatatst/TestUDataFileAccess" );
98 #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
99 addTest(root
, &TestTZDataDir
, "udatatst/TestTZDataDir" );
104 static void lots_of_mallocs()
116 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
117 static void TestUDataOpen(){
119 UErrorCode status
=U_ZERO_ERROR
;
120 const char* memMap
[][2]={
124 {"ibm-37_P100-1995", "cnv"}
126 const char* name
= "test";
127 const char* type
= "icu";
128 const char dirSepString
[] = {U_FILE_SEP_CHAR
, 0};
129 const char pathSepString
[] = {U_PATH_SEP_CHAR
, 0};
132 char* path
=(char*)malloc(sizeof(char) * (strlen(ctest_dataOutDir())
133 + strlen(U_ICUDATA_NAME
)
134 + strlen("/build/tmp/..")+1 ) );
136 char *icuDataFilePath
= 0;
138 const char* testPath
=loadTestData(&status
);
139 if(U_FAILURE(status
)) {
140 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
145 /* lots_of_mallocs(); */
146 log_verbose("Testing udata_open(%s)\n", testPath
);
147 result
=udata_open(testPath
, type
, name
, &status
);
148 if(U_FAILURE(status
)){
149 log_data_err("FAIL: udata_open() failed for path = %s, name=%s, type=%s, \n errorcode=%s\n", testPath
, name
, type
, myErrorName(status
));
151 log_verbose("PASS: udata_open worked\n");
156 strcat(strcpy(path
, ctest_dataOutDir()), U_ICUDATA_NAME
);
158 /* If the ICU system common data file is present in this confiugration,
159 * verify that udata_open can explicitly fetch items from it.
160 * If packaging mode == dll, the file may not exist. So, if the file is
161 * missing, skip this test without error.
163 icuDataFilePath
= (char *)uprv_malloc(strlen(path
) + 10);
164 strcpy(icuDataFilePath
, path
);
165 strcat(icuDataFilePath
, ".dat");
166 /* lots_of_mallocs(); */
167 if (uprv_fileExists(icuDataFilePath
))
170 log_verbose("Testing udata_open() on %s\n", icuDataFilePath
);
171 for(i
=0; i
<UPRV_LENGTHOF(memMap
); i
++){
172 /* lots_of_mallocs(); */
174 result
=udata_open(path
, memMap
[i
][1], memMap
[i
][0], &status
);
175 if(U_FAILURE(status
)) {
176 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
));
178 log_verbose("PASS: udata_open worked for path = %s, name=%s, type=%s\n", path
, memMap
[i
][0], memMap
[i
][1]);
185 /* lots_of_mallocs(); */
186 log_verbose("Skipping tests of udata_open() on %s. File not present in this configuration.\n",
189 uprv_free(icuDataFilePath
);
191 /* try again, adding /tmp */
193 strcpy(path
, ctest_dataOutDir());
195 strcat(path
, dirSepString
);
196 strcat(path
, U_ICUDATA_NAME
);
198 /* If the ICU system common data file is present in this confiugration,
199 * verify that udata_open can explicitly fetch items from it.
200 * If packaging mode == dll, the file may not exist. So, if the file is
201 * missing, skip this test without error.
203 icuDataFilePath
= (char *)malloc(strlen(path
) + 10);
204 strcpy(icuDataFilePath
, path
);
205 strcat(icuDataFilePath
, ".dat");
206 /* lots_of_mallocs(); */
207 if (uprv_fileExists(icuDataFilePath
))
210 log_verbose("Testing udata_open() on %s\n", icuDataFilePath
);
211 for(i
=0; i
<UPRV_LENGTHOF(memMap
); i
++){
212 /* lots_of_mallocs(); */
214 result
=udata_open(path
, memMap
[i
][1], memMap
[i
][0], &status
);
215 if(U_FAILURE(status
)) {
216 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
));
218 log_verbose("PASS: udata_open worked for path = %s, name=%s, type=%s\n", path
, memMap
[i
][0], memMap
[i
][1]);
225 /* lots_of_mallocs(); */
226 log_verbose("Skipping tests of udata_open() on %s. File not present in this configuration.\n",
231 free(icuDataFilePath
);
232 icuDataFilePath
= NULL
;
233 /* lots_of_mallocs(); */
235 /* If the ICU individual files used to build the ICU system common data are
236 * present in this configuration,
237 * verify that udata_open can explicitly open them.
238 * These data files are present in the ICU data/build directory after a build
239 * completes. Tests are most commonly run with the data directory pointing
240 * back into this directory structure, but this is not required. Soooo, if
241 * the files are missing, skip this test without error.
243 /* lots_of_mallocs(); */
244 icuDataFilePath
= (char *)malloc(strlen(ctest_dataOutDir()) + 50);
245 strcpy(icuDataFilePath
, ctest_dataOutDir());
246 strcat(icuDataFilePath
, "build");
247 strcat(icuDataFilePath
, dirSepString
);
248 strcat(icuDataFilePath
, U_ICUDATA_NAME
);
249 strcat(icuDataFilePath
, dirSepString
);
250 strcat(icuDataFilePath
, "cnvalias.icu");
252 /* lots_of_mallocs(); */
253 if (uprv_fileExists(icuDataFilePath
))
256 log_verbose("%s exists, so..\n", icuDataFilePath
);
257 strcpy(icuDataFilePath
, ctest_dataOutDir());
258 strcat(icuDataFilePath
, "build");
259 strcat(icuDataFilePath
, dirSepString
);
260 strcat(icuDataFilePath
, U_ICUDATA_NAME
);
261 log_verbose("Testing udata_open() on %s\n", icuDataFilePath
);
262 for(i
=0; i
<UPRV_LENGTHOF(memMap
); i
++){
264 result
=udata_open(icuDataFilePath
, memMap
[i
][1], memMap
[i
][0], &status
);
265 if(U_FAILURE(status
)) {
266 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
));
268 log_verbose("PASS: udata_open worked for path = %s, name=%s, type=%s\n", icuDataFilePath
, memMap
[i
][0], memMap
[i
][1]);
275 log_verbose("Skipping tests of udata_open() on %s. File not present in this configuration.\n",
279 free(icuDataFilePath
);
280 icuDataFilePath
= NULL
;
283 * Test fallback file names for open of separate data files.
284 * With these params to udata_open:
285 * path = wherever/testdata
288 * these files will be tried first:
289 * wherever/testudata_nam.typ
291 * A test data file named testudata_nam.typ exists for the purpose of testing this.
293 log_verbose("Testing udata_open, with base_name.type style fallback to individual file.\n");
295 status
= U_ZERO_ERROR
;
296 result
= udata_open( testPath
, "typ", "nam", &status
);
297 if (status
!= U_ZERO_ERROR
) {
298 log_data_err("FAIL: udata_open( \"%s\", \"typ\", \"nam\") returned status %s\n", testPath
, u_errorName(status
));
301 free(icuDataFilePath
);
304 /* This type of path is deprecated */
306 * Another fallback test. Paths ending with a trailing directory separator
307 * take a slightly different code path, with the "base name" from the path
308 * being empty in the internal udata_open logic.
311 /* log_verbose("Testing udata_open, with path containing a trailing directory separator.\n"); */
312 /* icuDataFilePath = (char *)malloc(strlen(u_getDataDirectory()) + 50); */
313 /* strcpy(icuDataFilePath, testPath); */
314 /* status = U_ZERO_ERROR; */
315 /* result = udata_open( icuDataFilePath, "cnv", "test1", &status); */
316 /* if (status != U_ZERO_ERROR) { */
317 /* log_err("FAIL: udata_open( \"%s\", \"cnv\", \"test1\") returned status %s\n", icuDataFilePath, u_errorName(status)); */
319 /* udata_close(result); */
320 /* free(icuDataFilePath); */
323 log_verbose("Testing udata_open() with a non existing binary file\n");
324 result
=udata_open("testdata", "tst", "nonexist", &status
);
325 if(status
==U_FILE_ACCESS_ERROR
){
326 log_verbose("Opening udata_open with non-existing file handled correctly.\n");
329 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
));
330 if(U_SUCCESS(status
)) {
336 log_err("calling udata_open with non-existing file didn't return a null value\n");
338 log_verbose("calling udat_open with non-existing file returned null as expected\n");
342 * Try opening data with absurdly long path and name, to trigger buffer size
343 * overflow handling code.
346 char longTestPath
[1024]; /* Implementation goes to heap at length of 128. */
349 /* Try a very long nonexistent directory path.
350 * udata_open should still succeed. Opening with the path will fail,
351 * then fall back to skipping the directory portion of the path.
353 log_verbose("Testing udata_open() with really long names\n");
355 strcat(longTestPath
, "bogus_directory_name");
356 while (strlen(longTestPath
) < 500) {
357 strcat(longTestPath
, dirSepString
);
358 strcat(longTestPath
, "bogus_directory_name");
360 strcat(longTestPath
, pathSepString
);
361 strcat(longTestPath
, testPath
);
362 result
=udata_open(longTestPath
, type
, name
, &status
);
363 if(U_FAILURE(status
)){
364 log_data_err("FAIL: udata_open() failed for path = %s\n name=%s, type=%s, \n errorcode=%s\n",
365 longTestPath
, name
, type
, myErrorName(status
));
367 log_verbose("PASS: udata_open worked\n");
371 /* Try a very long name. Won't open, but shouldn't blow up.
374 while (strlen(longName
) < 500) {
375 strcat(longName
, name
);
376 strcat(longName
, "_");
378 strcat(longName
, dirSepString
);
379 strcat(longName
, name
);
381 result
=udata_open(longTestPath
, type
, longName
, &status
);
382 if (status
!= U_FILE_ACCESS_ERROR
) {
383 log_data_err("FAIL: udata_open() failed for path = %s\n name=%s, type=%s, \n errorcode=%s\n",
384 longTestPath
, longName
, type
, myErrorName(status
));
395 uint8_t magic1
, magic2
;
398 uint32_t count
, reserved
;
401 const char *const name;
402 const void *const data;
405 int32_t fakeNameAndData
[4];
406 } ICU_COMMON_Data_Header
;
408 static const ICU_COMMON_Data_Header gEmptyHeader
= {
410 0xda, /* magic1, (see struct MappedData in udata.c) */
413 sizeof(UDataInfo
), /* size */
425 { /* data format identifier */
426 0x43, 0x6d, 0x6e, 0x44}, /* "CmnD" */
427 {1, 0, 0, 0}, /* format version major, minor, milli, micro */
428 {0, 0, 0, 0} /* dataVersion */
430 {0,0,0,0,0,0,0,0}, /* Padding[8] */
433 { /* TOC structure */
435 0 , 0 , 0, 0 /* name and data entries. Count says there are none, */
436 /* but put one in just in case. */
442 static void TestUDataSetAppData(){
443 /* UDataMemory *dataItem;*/
445 UErrorCode status
=U_ZERO_ERROR
;
448 * First we try some monkey business and try to do bad things.
452 udata_setAppData("appData1", NULL
, &status
);
453 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
454 log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData1\", NULL, status) should have failed."
455 " It returned status of %s\n", u_errorName(status
));
458 /* The following call should fail.
459 If the following works with a bad UErrorCode, then later calls to appData1 should fail. */
460 udata_setAppData("appData1", &gEmptyHeader
, &status
);
463 * Got testdata.dat into memory, now we try setAppData using the memory image.
467 udata_setAppData("appData1", &gEmptyHeader
, &status
);
468 if (status
!= U_ZERO_ERROR
) {
469 log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData1\", fileBuf, status) "
470 " returned status of %s\n", u_errorName(status
));
474 udata_setAppData("appData2", &gEmptyHeader
, &status
);
475 if (status
!= U_ZERO_ERROR
) {
476 log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData2\", fileBuf, status) "
477 " returned status of %s\n", u_errorName(status
));
481 /* If we try to setAppData with the same name a second time, we should get a
482 * a using default warning.
484 udata_setAppData("appData2", &gEmptyHeader
, &status
);
485 if (status
!= U_USING_DEFAULT_WARNING
) {
486 log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData2\", fileBuf, status) "
487 " returned status of %s, expected U_USING_DEFAULT_WARNING.\n", u_errorName(status
));
491 /** It is no longer correct to use udata_setAppData to change the
492 package of a contained item.
494 dataItem = udata_open("appData1", "res", "te_IN", &status); **/
497 static char *safeGetICUDataDirectory() {
498 const char *dataDir
= u_getDataDirectory(); /* Returned string vanashes with u_cleanup */
500 if (dataDir
!= NULL
) {
501 retStr
= (char *)malloc(strlen(dataDir
)+1);
502 strcpy(retStr
, dataDir
);
507 static void TestUDataFileAccess(){
510 icuDataDir
= safeGetICUDataDirectory(); /* save icu data dir, so we can put it back
511 * after doing u_cleanup(). */
513 /** UDATA_NO_FILES, ICU does not access the file system for data loading. */
516 udata_setFileAccess(UDATA_NO_FILES
,&status
);
518 if(U_FAILURE(status
) && *icuDataDir
== 0){
519 log_data_err("udata_setFileAccess(UDATA_NO_FILES) failed with ICU_DATA=\"\" err=%s\n", u_errorName(status
));
522 /** UDATA_ONLY_PACKAGES, ICU only loads data from packages, not from single files. */
525 udata_setFileAccess(UDATA_ONLY_PACKAGES
,&status
);
528 /** UDATA_PACKAGES_FIRST, ICU loads data from packages first, and only from single files
529 if the data cannot be found in a package. */
532 udata_setFileAccess(UDATA_PACKAGES_FIRST
,&status
);
535 /** UDATA_FILES_FIRST, ICU looks for data in single files first, then in packages. (default) */
538 udata_setFileAccess(UDATA_FILES_FIRST
,&status
);
541 /** An alias for the default access mode. */
544 udata_setFileAccess(UDATA_DEFAULT_ACCESS
,&status
);
545 u_setDataDirectory(icuDataDir
);
547 if(U_FAILURE(status
)){
548 log_err_status(status
, "%s\n", u_errorName(status
));
555 static UBool U_CALLCONV
556 isAcceptable1(void *context
,
557 const char *type
, const char *name
,
558 const UDataInfo
*pInfo
) {
560 if( pInfo
->size
>=20 &&
561 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
562 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
563 pInfo
->dataFormat
[0]==0x43 && /* dataFormat="CvAl" */
564 pInfo
->dataFormat
[1]==0x76 &&
565 pInfo
->dataFormat
[2]==0x41 &&
566 pInfo
->dataFormat
[3]==0x6c &&
567 pInfo
->formatVersion
[0]==3 )
569 log_verbose("The data from \"%s.%s\" IS acceptable using the verifing function isAcceptable1()\n", name
, type
);
572 log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable1():-\n"
574 "\tisBigEndian = %d\n"
575 "\tcharsetFamily = %d\n"
576 "\tformatVersion[0] = %d\n"
577 "\tdataVersion[0] = %d\n"
578 "\tdataFormat = %c%c%c%c\n",
579 name
, type
, pInfo
->size
, pInfo
->isBigEndian
, pInfo
->charsetFamily
, pInfo
->formatVersion
[0],
580 pInfo
->dataVersion
[0], pInfo
->dataFormat
[0], pInfo
->dataFormat
[1], pInfo
->dataFormat
[2],
581 pInfo
->dataFormat
[3]);
582 log_verbose("Call another verifing function to accept the data\n");
587 static UBool U_CALLCONV
588 isAcceptable2(void *context
,
589 const char *type
, const char *name
,
590 const UDataInfo
*pInfo
){
591 UVersionInfo unicodeVersion
;
593 u_getUnicodeVersion(unicodeVersion
);
595 if( pInfo
->size
>=20 &&
596 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
597 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
598 pInfo
->dataFormat
[0]==0x75 && /* dataFormat="unam" */
599 pInfo
->dataFormat
[1]==0x6e &&
600 pInfo
->dataFormat
[2]==0x61 &&
601 pInfo
->dataFormat
[3]==0x6d &&
602 pInfo
->formatVersion
[0]==1 &&
603 pInfo
->dataVersion
[0]==unicodeVersion
[0] )
605 log_verbose("The data from \"%s.%s\" IS acceptable using the verifing function isAcceptable2()\n", name
, type
);
608 log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable2()\n", name
, type
);
615 static UBool U_CALLCONV
616 isAcceptable3(void *context
,
617 const char *type
, const char *name
,
618 const UDataInfo
*pInfo
){
620 if( pInfo
->size
>=20 &&
621 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
622 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
623 pInfo
->dataFormat
[0]==0x54 && /* dataFormat="test" */
624 pInfo
->dataFormat
[1]==0x65 &&
625 pInfo
->dataFormat
[2]==0x73 &&
626 pInfo
->dataFormat
[3]==0x74 &&
627 pInfo
->formatVersion
[0]==1 &&
628 pInfo
->dataVersion
[0]==1 ) {
629 log_verbose("The data from \"%s.%s\" IS acceptable using the verifing function isAcceptable3()\n", name
, type
);
633 log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable3()\n", name
, type
);
640 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
641 static void TestUDataOpenChoiceDemo1() {
643 UErrorCode status
=U_ZERO_ERROR
;
651 const char* type
="icu";
652 const char* testPath
="testdata";
653 const char* fullTestDataPath
= loadTestData(&status
);
654 if(U_FAILURE(status
)) {
655 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
659 result
=udata_openChoice(NULL
, "icu", name
[0], isAcceptable1
, NULL
, &status
);
660 if(U_FAILURE(status
)){
661 log_data_err("FAIL: udata_openChoice() failed name=%s, type=%s, \n errorcode=%s\n", name
[0], type
, myErrorName(status
));
663 log_verbose("PASS: udata_openChoice worked\n");
668 result
=udata_openChoice(NULL
, type
, name
[1], isAcceptable1
, NULL
, &status
);
669 if(U_FAILURE(status
)){
671 result
=udata_openChoice(NULL
, type
, name
[1], isAcceptable2
, NULL
, &status
);
672 if(U_FAILURE(status
)){
673 log_data_err("FAIL: udata_openChoice() failed name=%s, type=%s, \n errorcode=%s\n", name
[1], type
, myErrorName(status
));
677 log_err("FAIL: udata_openChoice() unexpectedly passed. name=%s, type=%s, \n errorcode=%s\n", name
[1], type
, myErrorName(status
));
680 if(U_SUCCESS(status
)){
685 result
=udata_openChoice(testPath
, type
, name
[2], isAcceptable1
, NULL
, &status
);
686 if(U_FAILURE(status
)){
688 result
=udata_openChoice(testPath
, type
, name
[2], isAcceptable3
, NULL
, &status
);
689 if(U_FAILURE(status
)){
690 log_data_err("FAIL: udata_openChoice() failed path=%s name=%s, type=%s, \n errorcode=%s\n", testPath
, name
[2], type
, myErrorName(status
));
694 log_err("FAIL: udata_openChoice() unexpectedly passed. name=%s, type=%s, \n errorcode=%s\n", name
[2], type
, myErrorName(status
));
697 if(U_SUCCESS(status
)){
703 result
=udata_openChoice(fullTestDataPath
, type
, name
[3], isAcceptable1
, NULL
, &status
);
704 if(status
!= U_INVALID_FORMAT_ERROR
){
705 log_err("FAIL: udata_openChoice() did not fail as expected. name=%s, type=%s, \n errorcode=%s\n", name
[3], type
, myErrorName(status
));
708 status
=U_USELESS_COLLATOR_ERROR
;
709 result
=udata_openChoice(fullTestDataPath
, type
, name
[3], isAcceptable1
, NULL
, &status
);
710 if(status
!= U_USELESS_COLLATOR_ERROR
){
711 log_err("FAIL: udata_openChoice() did not fail as expected. name=%s, type=%s, \n errorcode=%s\n", name
[3], type
, myErrorName(status
));
715 static UBool U_CALLCONV
716 isAcceptable(void *context
,
717 const char *type
, const char *name
,
718 const UDataInfo
*pInfo
){
719 if( pInfo
->size
>=20 &&
720 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
721 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
722 pInfo
->dataFormat
[0]==0x54 && /* dataFormat="test" */
723 pInfo
->dataFormat
[1]==0x65 &&
724 pInfo
->dataFormat
[2]==0x73 &&
725 pInfo
->dataFormat
[3]==0x74 &&
726 pInfo
->formatVersion
[0]==1 &&
727 pInfo
->dataVersion
[0]==1 &&
728 *((int*)context
) == 2 ) {
729 log_verbose("The data from\"%s.%s\" IS acceptable using the verifing function isAcceptable()\n", name
, type
);
733 log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable()\n", name
, type
);
739 /* This test checks to see if the isAcceptable function is being called correctly. */
741 static void TestUDataOpenChoiceDemo2() {
743 UErrorCode status
=U_ZERO_ERROR
;
747 const char* name
="test";
748 const char* type
="icu";
749 const char* path
= loadTestData(&status
);
750 if(U_FAILURE(status
)) {
751 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
755 result
=udata_openChoice(path
, type
, name
, isAcceptable
, &p
, &status
);
756 if(U_FAILURE(status
)){
757 log_data_err("failed to load data at p=%s t=%s n=%s, isAcceptable", path
, type
, name
);
759 if(U_SUCCESS(status
) ) {
765 result
=udata_openChoice(path
, type
, name
, isAcceptable
, &p
, &status
);
767 if(U_FAILURE(status
) && status
==U_INVALID_FORMAT_ERROR
){
768 log_verbose("Loads the data but rejects it as expected %s\n", myErrorName(status
));
773 log_data_err("FAIL: failed to either load the data or to reject the loaded data. ERROR=%s\n", myErrorName(status
) );
777 if(U_FAILURE(status
)) {
778 log_data_err("FAIL: failed to load the data and accept it. ERROR=%s\n", myErrorName(status
) );
781 log_verbose("Loads the data and accepts it for p==2 as expected\n");
788 static void TestUDataGetInfo() {
791 /* UDataInfo cf. udata.h */
792 static UDataInfo dataInfo
={
793 30, /*sizeof(UDataInfo),*/
801 {0x54, 0x65, 0x73, 0x74}, /* dataFormat="Test" */
802 {9, 0, 0, 0}, /* formatVersion */
803 {4, 0, 0, 0} /* dataVersion */
805 UErrorCode status
=U_ZERO_ERROR
;
806 const char* name
="cnvalias";
807 const char* name2
="test";
808 const char* type
="icu";
810 const char* testPath
=loadTestData(&status
);
811 if(U_FAILURE(status
)) {
812 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
816 log_verbose("Testing udata_getInfo() for cnvalias.icu\n");
817 result
=udata_open(NULL
, "icu", name
, &status
);
818 if(U_FAILURE(status
)){
819 log_data_err("FAIL: udata_open() failed for path = NULL, name=%s, type=%s, \n errorcode=%s\n", name
, type
, myErrorName(status
));
822 udata_getInfo(result
, &dataInfo
);
823 if(dataInfo
.size
==20 && dataInfo
.size
!=30 &&
824 dataInfo
.isBigEndian
==U_IS_BIG_ENDIAN
&&
825 dataInfo
.charsetFamily
==U_CHARSET_FAMILY
&&
826 dataInfo
.dataFormat
[0]==0x43 && dataInfo
.dataFormat
[0]!=0x54 && /* dataFormat="CvAl" and not "Test". The values are set for cnvalias.dat*/
827 dataInfo
.dataFormat
[1]==0x76 && dataInfo
.dataFormat
[1]!=0x65 &&
828 dataInfo
.dataFormat
[2]==0x41 && dataInfo
.dataFormat
[2]!=0x73 &&
829 dataInfo
.dataFormat
[3]==0x6c && dataInfo
.dataFormat
[3]!=0x74 &&
830 dataInfo
.formatVersion
[0]!=9 && /*formatVersion is also set to the one for cnvalias*/
831 dataInfo
.dataVersion
[0]!=4 && /*dataVersion*/
832 dataInfo
.dataVersion
[1]!=0 ){
833 log_verbose("PASS: udata_getInfo() filled in the right values\n");
835 log_err("FAIL: udata_getInfo() filled in the wrong values\n");
840 log_verbose("Testing udata_getInfo() for test.icu\n");
841 result
=udata_open(testPath
, type
, name2
, &status
);
842 if(U_FAILURE(status
)) {
843 log_data_err("FAIL: udata_open() failed for path=%s name2=%s, type=%s, \n errorcode=%s\n", testPath
, name2
, type
, myErrorName(status
));
846 udata_getInfo(result
, &dataInfo
);
847 if(dataInfo
.size
==20 &&
848 dataInfo
.isBigEndian
==U_IS_BIG_ENDIAN
&&
849 dataInfo
.charsetFamily
==U_CHARSET_FAMILY
&&
850 dataInfo
.dataFormat
[0]==0x54 && /* dataFormat="Test". The values are set for test.dat*/
851 dataInfo
.dataFormat
[1]==0x65 &&
852 dataInfo
.dataFormat
[2]==0x73 &&
853 dataInfo
.dataFormat
[3]==0x74 &&
854 dataInfo
.formatVersion
[0]==1 && /*formatVersion is also set to the one for test*/
855 dataInfo
.dataVersion
[0]==1 && /*dataVersion*/
856 dataInfo
.dataVersion
[1]==0 )
858 log_verbose("PASS: udata_getInfo() filled in the right values\n");
860 log_err("FAIL: udata_getInfo() filled in the wrong values\n");
865 static void TestUDataGetMemory() {
868 const int32_t *table
=NULL
;
869 uint16_t* intValue
=0;
870 UErrorCode status
=U_ZERO_ERROR
;
871 const char* name
="cnvalias";
874 const char* name2
="test";
876 const char* testPath
= loadTestData(&status
);
877 if(U_FAILURE(status
)) {
878 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
883 log_verbose("Testing udata_getMemory() for \"cnvalias.icu\"\n");
884 result
=udata_openChoice(NULL
, type
, name
, isAcceptable1
, NULL
, &status
);
885 if(U_FAILURE(status
)){
886 log_data_err("FAIL: udata_openChoice() failed for name=%s, type=%s, \n errorcode=%s\n", name
, type
, myErrorName(status
));
889 table
=(const int32_t *)udata_getMemory(result
);
891 /* The alias table may list more converters than what's actually available now. [grhoten] */
892 if(ucnv_countAvailable() > table
[1]) /*???*/
893 log_err("FAIL: udata_getMemory() failed ucnv_countAvailable returned = %d, expected = %d\n", ucnv_countAvailable(), table
[1+2*(*table
)]);
898 log_verbose("Testing udata_getMemory for \"test.icu\"()\n");
899 result
=udata_openChoice(testPath
, type
, name2
, isAcceptable3
, NULL
, &status
);
900 if(U_FAILURE(status
)){
901 log_data_err("FAIL: udata_openChoice() failed for path=%s name=%s, type=%s, \n errorcode=%s\n", testPath
, name2
, type
, myErrorName(status
));
904 intValue
=(uint16_t *)udata_getMemory(result
);
905 /*printf("%d ..... %s", *(intValue), intValue+1));*/
906 if( *intValue
!= 2000 || strcmp((char*)(intValue
+1), "YEAR") != 0 )
907 log_err("FAIL: udata_getMemory() failed: intValue :- Expected:2000 Got:%d \n\tstringValue:- Expected:YEAR Got:%s\n", *intValue
, (intValue
+1));
913 static void TestErrorConditions(){
915 UDataMemory
*result
=NULL
;
916 UErrorCode status
=U_ZERO_ERROR
;
917 uint16_t* intValue
=0;
918 static UDataInfo dataInfo
={
919 30, /*sizeof(UDataInfo),*/
927 {0x54, 0x65, 0x73, 0x74}, /* dataFormat="Test" */
928 {9, 0, 0, 0}, /* formatVersion */
929 {4, 0, 0, 0} /* dataVersion */
932 const char* name
= "test";
933 const char* type
="icu";
935 const char *testPath
= loadTestData(&status
);
936 if(U_FAILURE(status
)) {
937 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
941 status
= U_ILLEGAL_ARGUMENT_ERROR
;
942 /*Try udata_open with status != U_ZERO_ERROR*/
943 log_verbose("Testing udata_open() with status != U_ZERO_ERROR\n");
944 result
=udata_open(testPath
, type
, name
, &status
);
946 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
);
950 log_verbose("PASS: udata_open with errorCode != U_ZERO_ERROR failed as expected\n");
953 /*Try udata_open with data name=NULL*/
954 log_verbose("Testing udata_open() with data name=NULL\n");
956 result
=udata_open(testPath
, type
, NULL
, &status
);
957 if(U_FAILURE(status
)){
958 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
|| result
!= NULL
){
959 log_err("FAIL: udata_open() with name=NULL should return NULL and errocode U_ILLEGAL_ARGUMENT_ERROR, GOT: errorcode=%s\n", myErrorName(status
));
961 log_verbose("PASS: udata_open with name=NULL failed as expected and errorcode = %s as expected\n", myErrorName(status
));
964 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
);
969 /*Try udata_openChoice with status != U_ZERO_ERROR*/
970 log_verbose("Testing udata_openChoice() with status != U_ZERO_ERROR\n");
971 status
=U_ILLEGAL_ARGUMENT_ERROR
;
972 result
=udata_openChoice(testPath
, type
, name
, isAcceptable3
, NULL
, &status
);
974 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 log_verbose("PASS: udata_openChoice() with errorCode != U_ZERO_ERROR failed as expected\n");
980 /*Try udata_open with data name=NULL*/
981 log_verbose("Testing udata_openChoice() with data name=NULL\n");
983 result
=udata_openChoice(testPath
, type
, NULL
, isAcceptable3
, NULL
, &status
);
984 if(U_FAILURE(status
)){
985 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
|| result
!= NULL
){
986 log_err("FAIL: udata_openChoice() with name=NULL should return NULL and errocode U_ILLEGAL_ARGUMENT_ERROR, GOT: errorcode=%s\n", myErrorName(status
));
988 log_verbose("PASS: udata_openChoice with name=NULL failed as expected and errorcode = %s as expected\n", myErrorName(status
));
991 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
);
995 /*Try udata_getMemory with UDataMemory=NULL*/
996 log_verbose("Testing udata_getMemory with UDataMemory=NULL\n");
997 intValue
=(uint16_t*)udata_getMemory(NULL
);
998 if(intValue
!= NULL
){
999 log_err("FAIL: udata_getMemory with UDataMemory = NULL is supposed to fail\n");
1002 /*Try udata_getInfo with UDataMemory=NULL*/
1003 status
=U_ZERO_ERROR
;
1004 udata_getInfo(NULL
, &dataInfo
);
1005 if(dataInfo
.size
!= 0){
1006 log_err("FAIL : udata_getInfo with UDataMemory = NULL us supposed to fail\n");
1009 /*Try udata_openChoice with a non existing binary file*/
1010 log_verbose("Testing udata_openChoice() with a non existing binary file\n");
1011 result
=udata_openChoice(testPath
, "tst", "nonexist", isAcceptable3
, NULL
, &status
);
1012 if(status
==U_FILE_ACCESS_ERROR
){
1013 log_verbose("Opening udata_openChoice with non-existing file handled correctly.\n");
1014 status
=U_ZERO_ERROR
;
1016 log_err("calling udata_open with non-existing file not handled correctly\n. Expected: U_FILE_ACCESS_ERROR, Got: %s\n", myErrorName(status
));
1017 if(U_SUCCESS(status
)) {
1018 udata_close(result
);
1023 log_err("calling udata_open with non-existing file didn't return a null value\n");
1025 log_verbose("calling udat_open with non-existing file returned null as expected\n");
1029 /* Test whether apps and ICU can each have their own root.res */
1030 static void TestAppData()
1032 UResourceBundle
*icu
, *app
;
1033 UResourceBundle
*tmp
= NULL
;
1034 UResourceBundle
*tmp2
= NULL
;
1036 const UChar
*appString
;
1037 const UChar
*icuString
;
1041 UErrorCode status
= U_ZERO_ERROR
;
1042 char testMsgBuf
[256];
1044 const char* testPath
=loadTestData(&status
);
1045 if(U_FAILURE(status
)) {
1046 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
1050 icu
= ures_open(NULL
, "root", &status
);
1051 if(U_FAILURE(status
))
1053 log_data_err("%s:%d: Couldn't open root ICU bundle- %s", __FILE__
, __LINE__
, u_errorName(status
));
1056 /* log_info("Open icu root: %s size_%d\n", u_errorName(status), ures_getSize(icu)); */
1057 status
= U_ZERO_ERROR
;
1059 app
= ures_open(testPath
, "root", &status
);
1060 if(U_FAILURE(status
))
1062 log_data_err("%s:%d: Couldn't open app ICU bundle [%s]- %s", __FILE__
, __LINE__
, testPath
, u_errorName(status
));
1065 /* log_info("Open app: %s, size %d\n", u_errorName(status), ures_getSize(app)); */
1067 tmp
= ures_getByKey(icu
, "Version", tmp
, &status
);
1068 if(U_FAILURE(status
))
1070 log_err("%s:%d: Couldn't get Version string from ICU root bundle- %s", __FILE__
, __LINE__
, u_errorName(status
));
1074 icuString
= ures_getString(tmp
, &len
, &status
);
1075 if(U_FAILURE(status
))
1077 log_err("%s:%d: Couldn't get string from Version string from ICU root bundle- %s", __FILE__
, __LINE__
, u_errorName(status
));
1080 /* log_info("icuString=%p - %s\n", icuString, austrdup(icuString)); */
1083 tmp2
= ures_getByKey(app
, "Version", tmp2
, &status
);
1084 if(U_FAILURE(status
))
1086 log_err("%s:%d: Couldn't get Version string from App root bundle- %s", __FILE__
, __LINE__
, u_errorName(status
));
1090 appString
= ures_getString(tmp2
, &len
, &status
);
1091 if(U_FAILURE(status
))
1093 log_err("%s:%d: Couldn't get string from Version string from App root bundle- %s", __FILE__
, __LINE__
, u_errorName(status
));
1097 /* log_info("appString=%p - %s\n", appString, austrdup(appString)); */
1100 if(!u_strcmp(icuString
, appString
))
1102 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
),
1103 austrdup(appString
));
1107 log_verbose("%s:%d: appstr=%s, icustr=%s\n", __FILE__
,
1108 __LINE__
, u_austrcpy(testMsgBuf
, appString
), u_austrcpy(testMsgBuf
, icuString
));
1118 static void TestICUDataName()
1120 UVersionInfo icuVersion
;
1121 char expectDataName
[20];
1122 unsigned int expectLen
= 8;
1124 char typeChar
= '?';
1126 /* Print out the version # we have .. */
1127 log_verbose("utypes.h says U_ICUDATA_NAME = %s\n", U_ICUDATA_NAME
);
1129 /* Build up the version # we expect to get */
1130 u_getVersion(icuVersion
);
1132 switch(U_CHARSET_FAMILY
)
1134 case U_ASCII_FAMILY
:
1135 switch((int)U_IS_BIG_ENDIAN
)
1144 log_err("Expected 1 or 0 for U_IS_BIG_ENDIAN, got %d!\n", (int)U_IS_BIG_ENDIAN
);
1148 case U_EBCDIC_FAMILY
:
1153 /* Only major number is needed. */
1154 sprintf(expectDataName
, "%s%d%c",
1159 log_verbose("Expected: %s\n", expectDataName
);
1160 if(uprv_strlen(expectDataName
) != expectLen
)
1162 log_err("*Expected* length is wrong (test err?), should be %d is %d\n",
1163 expectLen
, uprv_strlen(expectDataName
));
1166 if(uprv_strlen(U_ICUDATA_NAME
) != expectLen
)
1168 log_err("U_ICUDATA_NAME length should be %d is %d\n",
1169 expectLen
, uprv_strlen(U_ICUDATA_NAME
));
1172 if(uprv_strcmp(U_ICUDATA_NAME
, expectDataName
))
1174 log_err("U_ICUDATA_NAME should be %s but is %s\n",
1175 expectDataName
, U_ICUDATA_NAME
);
1178 /* ICUDATA_NAME comes from the build system on *nix */
1180 if(uprv_strcmp(U_ICUDATA_NAME
, ICUDATA_NAME
))
1182 log_err("ICUDATA_NAME and U_ICUDATA_NAME don't match: "
1183 "ICUDATA_NAME=%s, U_ICUDATA_NAME=%s. Check configure.in, icudefs.mk.in, utypes.h...\n", ICUDATA_NAME
, U_ICUDATA_NAME
);
1187 log_verbose("ICUDATA_NAME=%s (from icudefs.mk), U_ICUDATA_NAME=%s (from utypes.h)\n", ICUDATA_NAME
, U_ICUDATA_NAME
);
1193 /* test data swapping ------------------------------------------------------- */
1195 #if U_PLATFORM == U_PF_OS400
1196 /* See comments in genccode.c on when this special implementation can be removed. */
1197 static const struct {
1200 } gOffsetTOCAppDataItem1
={ 0.0, /* alignment bytes */
1201 "\x00\x14" /* sizeof(UDataInfo) *//* MappedData { */
1204 "\x00\x14" /* sizeof(UDataInfo) *//* UDataInfo { */
1206 "\1" /* U_IS_BIG_ENDIAN */
1207 "\1" /* U_CHARSET_FAMILY */
1208 "\2" /* U_SIZEOF_WHAR_T */
1215 static const struct {
1220 } gOffsetTOCAppDataItem1
={
1221 0.0, /* alignment bytes */
1222 { sizeof(UDataInfo
), 0xda, 0x27 }, /* MappedData */
1232 {0x31, 0x31, 0x31, 0x31}, /* dataFormat="1111" */
1233 {0, 0, 0, 0}, /* formatVersion */
1234 {0, 0, 0, 0}} /* dataVersion */
1238 static const UChar gOffsetTOCGarbage
[] = { /* "I have been very naughty!" */
1239 0x49, 0x20, 0x68, 0x61, 0x76, 0x65, 0x20, 0x62, 0x65, 0x65, 0x6E,
1240 0x20, 0x76, 0x65, 0x72, 0x79, 0x20, 0x6E, 0x61, 0x75, 0x67, 0x68, 0x74, 0x79, 0x21
1243 /* Original source: icu/source/tools/genccode */
1244 static const struct {
1245 uint16_t headerSize
;
1246 uint8_t magic1
, magic2
;
1249 uint32_t count
, reserved
;
1251 const char *const name
;
1252 const void *const data
;
1254 } gOffsetTOCAppData_dat
= {
1255 32, /* headerSize */
1256 0xda, /* magic1, (see struct MappedData in udata.c) */
1259 sizeof(UDataInfo
), /* size */
1265 { /* data format identifier */
1266 0x54, 0x6f, 0x43, 0x50}, /* "ToCP" */
1267 {1, 0, 0, 0}, /* format version major, minor, milli, micro */
1268 {0, 0, 0, 0} /* dataVersion */
1270 {0,0,0,0,0,0,0,0}, /* Padding[8] */
1273 { /* TOC structure */
1274 { "OffsetTOCAppData/a/b", &gOffsetTOCAppDataItem1
},
1275 { "OffsetTOCAppData/gOffsetTOCAppDataItem1", &gOffsetTOCAppDataItem1
},
1276 { "OffsetTOCAppData/gOffsetTOCGarbage", &gOffsetTOCGarbage
}
1280 /* Unfortunately, dictionaries are in a C++ header */
1281 U_CAPI
int32_t U_EXPORT2
1282 udict_swap(const UDataSwapper
*ds
, const void *inData
, int32_t length
, void *outData
, UErrorCode
*pErrorCode
);
1284 /* test cases for maximum data swapping code coverage */
1285 static const struct {
1286 const char *name
, *type
;
1287 UDataSwapFn
*swapFn
;
1289 /* resource bundles */
1291 /* resource bundle with many data types */
1292 {"*testtypes", "res", ures_swap
},
1293 /* resource bundle with collation data */
1294 {"ja", "res", ures_swap
},
1295 /* resource bundle with options-only collation data */
1296 {"ru", "res", ures_swap
},
1297 {"el", "res", ures_swap
},
1299 {"root", "res", ures_swap
},
1300 /* Test a 32-bit key table. This is large. */
1301 {"*testtable32", "res", ures_swap
},
1303 /* ICU 4.2 resource bundle - data format 1.2 (little-endian ASCII) */
1304 {"*old_l_testtypes", "res", ures_swap
},
1305 /* same for big-endian EBCDIC */
1306 {"*old_e_testtypes", "res", ures_swap
},
1308 #if !UCONFIG_NO_COLLATION
1309 /* standalone collation data files */
1310 {"ucadata", "icu", ucol_swap
},
1312 /* Starting with ICU 53, the "inverse UCA" data is integrated into ucadata.icu. */
1313 {"invuca", "icu", ucol_swapInverseUCA
},
1317 #if !UCONFIG_NO_LEGACY_CONVERSION
1318 /* conversion table files */
1320 /* SBCS conversion table file without extension */
1321 {"ibm-913_P100-2000", "cnv", ucnv_swap
},
1322 /* EBCDIC_STATEFUL conversion table file with extension */
1323 {"ibm-1390_P110-2003", "cnv", ucnv_swap
},
1324 /* DBCS extension-only conversion table file */
1325 {"ibm-16684_P110-2003", "cnv", ucnv_swap
},
1326 /* EUC-TW (3-byte) conversion table file without extension */
1327 {"ibm-964_P110-1999", "cnv", ucnv_swap
},
1328 /* GB 18030 (4-byte) conversion table file without extension */
1329 {"gb18030", "cnv", ucnv_swap
},
1330 /* MBCS conversion table file with extension */
1331 {"*test4x", "cnv", ucnv_swap
},
1333 * MBCS conversion table file without extension,
1334 * to test swapping and preflighting of UTF-8-friendly mbcsIndex[].
1336 {"jisx-212", "cnv", ucnv_swap
},
1339 #if !UCONFIG_NO_CONVERSION
1341 {"cnvalias", "icu", ucnv_swapAliases
},
1344 #if !UCONFIG_NO_IDNA
1345 {"rfc3491", "spp", usprep_swap
},
1348 #if !UCONFIG_NO_BREAK_ITERATION
1349 {"char", "brk", ubrk_swap
},
1350 {"thaidict", "dict",udict_swap
},
1355 * Starting with ICU 4.8, the Unicode property (value) aliases data
1356 * is hardcoded in the ICU4C common library.
1357 * The swapper was moved to the toolutil library for swapping for ICU4J.
1359 /* Unicode properties */
1360 {"pnames", "icu", upname_swap
},
1365 * Starting with ICU4C 3.4, the core Unicode properties files
1366 * (uprops.icu, ucase.icu, ubidi.icu, unorm.icu)
1367 * are hardcoded in the common DLL and therefore not included
1368 * in the data package any more.
1369 * Their swapping code is moved from the common DLL to the icuswap tool so that
1370 * we need not jump through hoops (like adding snapshots of these files
1371 * to testdata) for code coverage in tests.
1372 * See Jitterbug 4497.
1374 * ICU4C 4.4 adds normalization data files again, e.g., nfkc.nrm.
1376 {"uprops", "icu", uprops_swap
},
1377 {"ucase", "icu", ucase_swap
},
1378 {"ubidi", "icu", ubidi_swap
},
1380 #if !UCONFIG_NO_NORMALIZATION && !UCONFIG_ONLY_COLLATION
1381 {"nfkc", "nrm", unorm2_swap
},
1382 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
1383 {"confusables", "cfu", uspoof_swap
}, /* spoof data missing without regex */
1387 {"unames", "icu", uchar_swapNames
}
1388 /* the last item should not be #if'ed so that it can reliably omit the last comma */
1391 /* Large enough for the largest swappable data item. */
1392 #define SWAP_BUFFER_SIZE 1800000
1394 static void U_CALLCONV
1395 printError(void *context
, const char *fmt
, va_list args
) {
1396 vlog_info("[swap] ", fmt
, args
);
1397 log_err("\n"); /* Register error */
1401 TestSwapCase(UDataMemory
*pData
, const char *name
,
1402 UDataSwapFn
*swapFn
,
1403 uint8_t *buffer
, uint8_t *buffer2
) {
1405 const void *inData
, *inHeader
;
1406 int32_t length
, dataLength
, length2
, headerLength
;
1408 UErrorCode errorCode
;
1409 UErrorCode badStatus
;
1411 UBool inEndian
, oppositeEndian
;
1412 uint8_t inCharset
, oppositeCharset
;
1414 /* First we check that swapFn handles failures as expected. */
1415 errorCode
= U_UNSUPPORTED_ERROR
;
1416 length
= swapFn(NULL
, NULL
, 0, buffer
, &errorCode
);
1417 if (length
!= 0 || errorCode
!= U_UNSUPPORTED_ERROR
) {
1418 log_err("%s() did not fail as expected - %s\n", name
, u_errorName(errorCode
));
1420 errorCode
= U_ZERO_ERROR
;
1421 length
= swapFn(NULL
, NULL
, 0, buffer
, &errorCode
);
1422 if (length
!= 0 || errorCode
!= U_ILLEGAL_ARGUMENT_ERROR
) {
1423 log_err("%s() did not fail as expected with bad arguments - %s\n", name
, u_errorName(errorCode
));
1427 /* Continue with the rest of the tests. */
1428 errorCode
= U_ZERO_ERROR
;
1429 inData
=udata_getMemory(pData
);
1432 * get the data length if possible, to verify that swapping and preflighting
1433 * handles the entire data
1435 dataLength
=udata_getLength(pData
);
1438 * get the header and its length
1439 * all of the swap implementation functions require the header to be included
1441 inHeader
=udata_getRawMemory(pData
);
1442 headerLength
=(int32_t)((const char *)inData
-(const char *)inHeader
);
1444 /* first swap to opposite endianness but same charset family */
1445 errorCode
=U_ZERO_ERROR
;
1446 ds
=udata_openSwapperForInputData(inHeader
, headerLength
,
1447 !U_IS_BIG_ENDIAN
, U_CHARSET_FAMILY
, &errorCode
);
1448 if(U_FAILURE(errorCode
)) {
1449 log_err("udata_openSwapperForInputData(%s->!isBig+same charset) failed - %s\n",
1450 name
, u_errorName(errorCode
));
1454 inEndian
=ds
->inIsBigEndian
;
1455 inCharset
=ds
->inCharset
;
1457 oppositeEndian
=!inEndian
;
1458 oppositeCharset
= inCharset
==U_ASCII_FAMILY
? U_EBCDIC_FAMILY
: U_ASCII_FAMILY
;
1460 /* make this test work with data files that are built for a different platform */
1461 if(inEndian
!=U_IS_BIG_ENDIAN
|| inCharset
!=U_CHARSET_FAMILY
) {
1462 udata_closeSwapper(ds
);
1463 ds
=udata_openSwapper(inEndian
, inCharset
, oppositeEndian
, inCharset
, &errorCode
);
1464 if(U_FAILURE(errorCode
)) {
1465 log_err("udata_openSwapper(%s->!isBig+same charset) failed - %s\n",
1466 name
, u_errorName(errorCode
));
1472 Check error checking of swappable data not specific to this swapper.
1473 This should always fail.
1475 badStatus
= U_ZERO_ERROR
;
1476 length
=swapFn(ds
, &gOffsetTOCAppData_dat
, -1, NULL
, &badStatus
);
1477 if(badStatus
!= U_UNSUPPORTED_ERROR
) {
1478 log_err("swapFn(%s->!isBig+same charset) unexpectedly succeeded on bad data - %s\n",
1479 name
, u_errorName(errorCode
));
1480 udata_closeSwapper(ds
);
1484 /* Now allow errors to be printed */
1485 ds
->printError
=printError
;
1487 /* preflight the length */
1488 length
=swapFn(ds
, inHeader
, -1, NULL
, &errorCode
);
1489 if(U_FAILURE(errorCode
)) {
1490 log_err("swapFn(preflight %s->!isBig+same charset) failed - %s\n",
1491 name
, u_errorName(errorCode
));
1492 udata_closeSwapper(ds
);
1496 /* compare the preflighted length against the data length */
1497 if(dataLength
>=0 && (length
+15)<(headerLength
+dataLength
)) {
1498 log_err("swapFn(preflight %s->!isBig+same charset) length too small: %d < data length %d\n",
1499 name
, length
, (headerLength
+dataLength
));
1500 udata_closeSwapper(ds
);
1504 /* swap, not in-place */
1505 length2
=swapFn(ds
, inHeader
, length
, buffer
, &errorCode
);
1506 udata_closeSwapper(ds
);
1507 if(U_FAILURE(errorCode
)) {
1508 log_err("swapFn(%s->!isBig+same charset) failed - %s\n",
1509 name
, u_errorName(errorCode
));
1513 /* compare the swap length against the preflighted length */
1514 if(length2
!=length
) {
1515 log_err("swapFn(%s->!isBig+same charset) length differs from preflighting: %d != preflighted %d\n",
1516 name
, length2
, length
);
1520 /* next swap to opposite charset family */
1521 ds
=udata_openSwapper(oppositeEndian
, inCharset
,
1522 oppositeEndian
, oppositeCharset
,
1524 if(U_FAILURE(errorCode
)) {
1525 log_err("udata_openSwapper(%s->!isBig+other charset) failed - %s\n",
1526 name
, u_errorName(errorCode
));
1529 ds
->printError
=printError
;
1532 length2
=swapFn(ds
, buffer
, length
, buffer
, &errorCode
);
1533 udata_closeSwapper(ds
);
1534 if(U_FAILURE(errorCode
)) {
1535 log_err("swapFn(%s->!isBig+other charset) failed - %s\n",
1536 name
, u_errorName(errorCode
));
1540 /* compare the swap length against the original length */
1541 if(length2
!=length
) {
1542 log_err("swapFn(%s->!isBig+other charset) length differs from original: %d != original %d\n",
1543 name
, length2
, length
);
1547 /* finally swap to original platform values */
1548 ds
=udata_openSwapper(oppositeEndian
, oppositeCharset
,
1549 inEndian
, inCharset
,
1551 if(U_FAILURE(errorCode
)) {
1552 log_err("udata_openSwapper(%s->back to original) failed - %s\n",
1553 name
, u_errorName(errorCode
));
1556 ds
->printError
=printError
;
1558 /* swap, not in-place */
1559 length2
=swapFn(ds
, buffer
, length
, buffer2
, &errorCode
);
1560 udata_closeSwapper(ds
);
1561 if(U_FAILURE(errorCode
)) {
1562 log_err("swapFn(%s->back to original) failed - %s\n",
1563 name
, u_errorName(errorCode
));
1567 /* compare the swap length against the original length */
1568 if(length2
!=length
) {
1569 log_err("swapFn(%s->back to original) length differs from original: %d != original %d\n",
1570 name
, length2
, length
);
1574 /* compare the final contents with the original */
1575 if(0!=uprv_memcmp(inHeader
, buffer2
, length
)) {
1576 const uint8_t *original
;
1580 log_err("swapFn(%s->back to original) contents differs from original\n",
1583 /* find the first difference */
1584 original
=(const uint8_t *)inHeader
;
1585 for(i
=0; i
<length
&& original
[i
]==buffer2
[i
]; ++i
) {}
1587 /* find the next byte that is the same */
1588 for(j
=i
+1; j
<length
&& original
[j
]!=buffer2
[j
]; ++j
) {}
1589 log_info(" difference at index %d=0x%x, until index %d=0x%x\n", i
, i
, j
, j
);
1591 /* round down to the last 4-boundary for better result output */
1593 log_info("showing bytes from index %d=0x%x (length %d=0x%x):\n", i
, i
, length
, length
);
1595 /* print 8 bytes but limit to the buffer contents */
1596 length2
=i
+sizeof(diff
);
1597 if(length2
>length
) {
1601 /* print the original bytes */
1602 uprv_memset(diff
, 0, sizeof(diff
));
1603 for(j
=i
; j
<length2
; ++j
) {
1604 diff
[j
-i
]=original
[j
];
1606 log_info(" original: %02x %02x %02x %02x %02x %02x %02x %02x\n",
1607 diff
[0], diff
[1], diff
[2], diff
[3], diff
[4], diff
[5], diff
[6], diff
[7]);
1609 /* print the swapped bytes */
1610 uprv_memset(diff
, 0, sizeof(diff
));
1611 for(j
=i
; j
<length2
; ++j
) {
1612 diff
[j
-i
]=buffer2
[j
];
1614 log_info(" swapped: %02x %02x %02x %02x %02x %02x %02x %02x\n",
1615 diff
[0], diff
[1], diff
[2], diff
[3], diff
[4], diff
[5], diff
[6], diff
[7]);
1619 static void U_CALLCONV
1620 printErrorToString(void *context
, const char *fmt
, va_list args
) {
1621 vsprintf((char *)context
, fmt
, args
);
1624 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
1631 const char *pkg
, *nm
, *testPath
;
1632 UErrorCode errorCode
= U_ZERO_ERROR
;
1635 buffer
=(uint8_t *)malloc(2*SWAP_BUFFER_SIZE
);
1637 log_err("unable to allocate %d bytes\n", 2*SWAP_BUFFER_SIZE
);
1641 testPath
=loadTestData(&errorCode
);
1642 if(U_FAILURE(errorCode
)) {
1643 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(errorCode
));
1646 /* Test that printError works as expected. */
1647 errorCode
=U_USELESS_COLLATOR_ERROR
;
1648 ds
=udata_openSwapper(U_IS_BIG_ENDIAN
, U_ASCII_FAMILY
,
1649 !U_IS_BIG_ENDIAN
, U_ASCII_FAMILY
,
1651 if (ds
!= NULL
|| errorCode
!= U_USELESS_COLLATOR_ERROR
) {
1652 log_err("udata_openSwapper should have returned NULL with bad argument\n", name
);
1654 errorCode
=U_ZERO_ERROR
;
1655 ds
=udata_openSwapper(U_IS_BIG_ENDIAN
, U_ASCII_FAMILY
,
1656 !U_IS_BIG_ENDIAN
, U_ASCII_FAMILY
,
1658 ds
->printError
=printErrorToString
;
1659 ds
->printErrorContext
=name
;
1660 udata_printError(ds
, "This %s a %s", "is", "test");
1661 udata_closeSwapper(ds
);
1662 if (strcmp(name
, "This is a test") != 0) {
1663 log_err("udata_printError can't properly print error messages. Got = %s\n", name
);
1665 errorCode
= U_USELESS_COLLATOR_ERROR
;
1666 ds
=udata_openSwapperForInputData(NULL
, 0,
1667 !U_IS_BIG_ENDIAN
, U_ASCII_FAMILY
,
1669 if (ds
!= NULL
|| errorCode
!= U_USELESS_COLLATOR_ERROR
) {
1670 log_err("udata_openSwapperForInputData should have returned NULL with bad argument\n", name
);
1672 errorCode
=U_ZERO_ERROR
;
1673 ds
=udata_openSwapperForInputData(NULL
, 0,
1674 !U_IS_BIG_ENDIAN
, U_ASCII_FAMILY
,
1676 if (ds
!= NULL
|| errorCode
!= U_ILLEGAL_ARGUMENT_ERROR
) {
1677 log_err("udata_openSwapperForInputData should have returned NULL with bad argument\n", name
);
1679 errorCode
=U_ZERO_ERROR
;
1680 memset(buffer
, 0, sizeof(2*SWAP_BUFFER_SIZE
));
1681 ds
=udata_openSwapperForInputData(buffer
, 2*SWAP_BUFFER_SIZE
,
1682 !U_IS_BIG_ENDIAN
, U_ASCII_FAMILY
,
1684 if (ds
!= NULL
|| errorCode
!= U_UNSUPPORTED_ERROR
) {
1685 log_err("udata_openSwapperForInputData should have returned NULL with bad argument\n", name
);
1687 errorCode
=U_ZERO_ERROR
;
1689 /* Test argument checking. ucol_swap is normally tested via ures_swap, and isn't normally called directly. */
1690 #if !UCONFIG_NO_COLLATION
1691 ucol_swap(NULL
, NULL
, -1, NULL
, &errorCode
);
1692 if (errorCode
!= U_ILLEGAL_ARGUMENT_ERROR
) {
1693 log_err("ucol_swap did not fail as expected\n", name
);
1695 errorCode
=U_ZERO_ERROR
;
1698 for(i
=0; i
<UPRV_LENGTHOF(swapCases
); ++i
) {
1699 /* build the name for logging */
1700 errorCode
=U_ZERO_ERROR
;
1701 if(swapCases
[i
].name
[0]=='*') {
1703 nm
=swapCases
[i
].name
+1;
1704 uprv_strcpy(name
, "testdata");
1705 } else if (uprv_strcmp(swapCases
[i
].type
, "brk")==0
1706 || uprv_strcmp(swapCases
[i
].type
, "dict")==0) {
1707 pkg
=U_ICUDATA_BRKITR
;
1708 nm
=swapCases
[i
].name
;
1709 uprv_strcpy(name
, U_ICUDATA_BRKITR
);
1710 #if !UCONFIG_NO_COLLATION
1711 } else if (uprv_strcmp(swapCases
[i
].name
, "ucadata")==0
1712 || uprv_strcmp(swapCases
[i
].name
, "invuca")==0) {
1714 nm
=swapCases
[i
].name
;
1715 uprv_strcpy(name
, U_ICUDATA_COLL
);
1716 #endif /* !UCONFIG_NO_COLLATION */
1719 nm
=swapCases
[i
].name
;
1720 uprv_strcpy(name
, "NULL");
1722 uprv_strcat(name
, "/");
1723 uprv_strcat(name
, nm
);
1724 uprv_strcat(name
, ".");
1725 uprv_strcat(name
, swapCases
[i
].type
);
1727 pData
=udata_open(pkg
, swapCases
[i
].type
, nm
, &errorCode
);
1729 if(U_SUCCESS(errorCode
)) {
1730 TestSwapCase(pData
, name
, swapCases
[i
].swapFn
, buffer
, buffer
+SWAP_BUFFER_SIZE
);
1733 log_data_err("udata_open(%s) failed - %s\n",
1734 name
, u_errorName(errorCode
));
1742 static void PointerTableOfContents() {
1743 UDataMemory
*dataItem
;
1744 UErrorCode status
=U_ZERO_ERROR
;
1747 * Got testdata.dat into memory, now we try setAppData using the memory image.
1750 status
=U_ZERO_ERROR
;
1751 udata_setAppData("OffsetTOCAppData", &gOffsetTOCAppData_dat
, &status
);
1752 if (status
!= U_ZERO_ERROR
) {
1753 log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData1\", fileBuf, status) \n"
1754 " returned status of %s\n", u_errorName(status
));
1758 dataItem
= udata_open("OffsetTOCAppData", "", "gOffsetTOCAppDataItem1", &status
);
1759 if (U_FAILURE(status
)) {
1760 log_err("FAIL: gOffsetTOCAppDataItem1 could not be opened. status = %s\n", u_errorName(status
));
1762 if (udata_getMemory(dataItem
) != NULL
) {
1763 log_verbose("FAIL: udata_getMemory(dataItem) passed\n");
1766 log_err("FAIL: udata_getMemory returned NULL\n", u_errorName(status
));
1768 udata_close(dataItem
);
1770 dataItem
= udata_open("OffsetTOCAppData-a", "", "b", &status
);
1771 if (U_FAILURE(status
)) {
1772 log_err("FAIL: gOffsetTOCAppDataItem1 in tree \"a\" could not be opened. status = %s\n", u_errorName(status
));
1774 if (udata_getMemory(dataItem
) != NULL
) {
1775 log_verbose("FAIL: udata_getMemory(dataItem) in tree \"a\" passed\n");
1778 log_err("FAIL: udata_getMemory returned NULL\n", u_errorName(status
));
1780 udata_close(dataItem
);
1782 dataItem
= udata_open("OffsetTOCAppData", "", "gOffsetTOCGarbage", &status
);
1783 if (U_SUCCESS(status
)) {
1784 log_err("FAIL: gOffsetTOCGarbage should not be opened. status = %s\n", u_errorName(status
));
1786 dataItem
= udata_open("OffsetTOCAppData", "", "gOffsetTOCNonExistent", &status
);
1787 if (U_SUCCESS(status
)) {
1788 log_err("FAIL: gOffsetTOCNonExistent should not be found. status = %s\n", u_errorName(status
));
1793 static void SetBadCommonData(void) {
1794 /* It's difficult to test that udata_setCommonData really works within the test framework.
1795 So we just test that foolish people can't do bad things. */
1797 char badBuffer
[sizeof(gOffsetTOCAppData_dat
)];
1799 memset(badBuffer
, 0, sizeof(badBuffer
));
1800 strcpy(badBuffer
, "Hello! I'm not good data.");
1802 /* Check that we don't do anything */
1803 status
= U_FILE_ACCESS_ERROR
;
1804 udata_setCommonData(&gOffsetTOCAppData_dat
, &status
);
1805 if (status
!= U_FILE_ACCESS_ERROR
) {
1806 log_err("FAIL: udata_setCommonData changed the failure code.\n");
1808 /* Check that we fail correctly */
1809 status
= U_ZERO_ERROR
;
1810 udata_setCommonData(NULL
, &status
);
1811 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
1812 log_err("FAIL: udata_setCommonData did not fail with bad arguments.\n");
1815 /* Check that we verify that the data isn't bad */
1816 status
= U_ZERO_ERROR
;
1817 udata_setAppData("invalid path", badBuffer
, &status
);
1818 if (status
!= U_INVALID_FORMAT_ERROR
) {
1819 log_err("FAIL: udata_setAppData doesn't verify data validity.\n");
1823 // Check the override loading of time zone .res files from a specified path
1825 // Hand testing notes:
1826 // 1. Run this test with the environment variable set. The following should induce faiures:
1827 // ICU_TIMEZONE_FILES_DIR=../testdata/out/build LD_LIBRARY_PATH=../../lib:../../stubdata:../../tools/ctestfw:$LD_LIBRARY_PATH ./cintltst /udatatst/TestTZDataDir
1828 // 2. Build ICU with with U_TIMEZONE_FILES_DIR defined. This should also induce failures.
1829 // CPPFLAGS=-DU_TIMEZONE_FILES_DIR\=`pwd`/test/testdata/out/testdata ./runConfigureICU Linux
1832 static void TestTZDataDir(void) {
1833 #if !UCONFIG_NO_FORMATTING
1834 UErrorCode status
= U_ZERO_ERROR
;
1835 const char *tzDataVersion
;
1836 const char *testDataPath
;
1838 // Verify that default ICU time zone data version is something newer than 2014a.
1839 tzDataVersion
= ucal_getTZDataVersion(&status
);
1840 // printf("tz data version is %s\n", tzDataVersion);
1841 if (U_FAILURE(status
)) {
1842 log_data_err("Failed call to ucal_getTZDataVersion - %s\n", u_errorName(status
));
1844 } else if (strcmp("2014a", tzDataVersion
) == 0) {
1845 log_err("File %s:%d - expected something newer than time zone data 2014a.\n", __FILE__
, __LINE__
, tzDataVersion
);
1848 testDataPath
= loadTestData(&status
);
1849 // The path produced by loadTestData() will look something like
1850 // whatever/.../testdata/out/testdata
1851 // The test data puts an old (2014a) version of the time zone data there.
1853 // Switch ICU to the testdata version of zoneinfo64.res, which is verison 2014a.
1855 u_setTimeZoneFilesDirectory(testDataPath
, &status
);
1856 tzDataVersion
= ucal_getTZDataVersion(&status
);
1857 if (strcmp("2014a", tzDataVersion
) != 0) {
1858 log_err("File %s:%d - expected \"2014a\"; actual \"%s\"\n", __FILE__
, __LINE__
, tzDataVersion
);
1861 ctest_resetICU(); // Return ICU to using its standard tz data.
1862 tzDataVersion
= ucal_getTZDataVersion(&status
);
1863 // printf("tz data version is %s\n", tzDataVersion);
1864 if (strcmp("2014a", tzDataVersion
) == 0) {
1865 log_err("File %s:%d - expected something newer than time zone data 2014a.\n", __FILE__
, __LINE__
, tzDataVersion
);