1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 1998-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
11 * Modification History:
13 * Date Name Description
14 * 02/22/2000 Madhu Creation
15 ******************************************************************************
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"
33 #include "toolutil.h" /* for uprv_fileExists() */
37 /* includes for TestSwapData() */
40 /* swapping implementations in common */
51 /* swapping implementation in i18n */
52 #include "uspoof_impl.h"
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
);
59 /* other definitions and prototypes */
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);
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);
80 void addUDataTest(TestNode
** root
);
83 addUDataTest(TestNode
** root
)
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" );
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" );
106 static void lots_of_mallocs()
118 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
119 static void TestUDataOpen(){
121 UErrorCode status
=U_ZERO_ERROR
;
122 const char* memMap
[][2]={
126 {"ibm-37_P100-1995", "cnv"}
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};
134 char* path
=(char*)malloc(sizeof(char) * (strlen(ctest_dataOutDir())
135 + strlen(U_ICUDATA_NAME
)
136 + strlen("/build/tmp/..")+1 ) );
138 char *icuDataFilePath
= 0;
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
));
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
));
153 log_verbose("PASS: udata_open worked\n");
158 strcat(strcpy(path
, ctest_dataOutDir()), U_ICUDATA_NAME
);
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.
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
))
172 log_verbose("Testing udata_open() on %s\n", icuDataFilePath
);
173 for(i
=0; i
<UPRV_LENGTHOF(memMap
); i
++){
174 /* lots_of_mallocs(); */
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
));
180 log_verbose("PASS: udata_open worked for path = %s, name=%s, type=%s\n", path
, memMap
[i
][0], memMap
[i
][1]);
187 /* lots_of_mallocs(); */
188 log_verbose("Skipping tests of udata_open() on %s. File not present in this configuration.\n",
191 uprv_free(icuDataFilePath
);
193 /* try again, adding /tmp */
195 strcpy(path
, ctest_dataOutDir());
197 strcat(path
, dirSepString
);
198 strcat(path
, U_ICUDATA_NAME
);
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.
205 icuDataFilePath
= (char *)malloc(strlen(path
) + 10);
206 strcpy(icuDataFilePath
, path
);
207 strcat(icuDataFilePath
, ".dat");
208 /* lots_of_mallocs(); */
209 if (uprv_fileExists(icuDataFilePath
))
212 log_verbose("Testing udata_open() on %s\n", icuDataFilePath
);
213 for(i
=0; i
<UPRV_LENGTHOF(memMap
); i
++){
214 /* lots_of_mallocs(); */
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
));
220 log_verbose("PASS: udata_open worked for path = %s, name=%s, type=%s\n", path
, memMap
[i
][0], memMap
[i
][1]);
227 /* lots_of_mallocs(); */
228 log_verbose("Skipping tests of udata_open() on %s. File not present in this configuration.\n",
233 free(icuDataFilePath
);
234 icuDataFilePath
= NULL
;
235 /* lots_of_mallocs(); */
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.
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");
254 /* lots_of_mallocs(); */
255 if (uprv_fileExists(icuDataFilePath
))
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
++){
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
));
270 log_verbose("PASS: udata_open worked for path = %s, name=%s, type=%s\n", icuDataFilePath
, memMap
[i
][0], memMap
[i
][1]);
277 log_verbose("Skipping tests of udata_open() on %s. File not present in this configuration.\n",
281 free(icuDataFilePath
);
282 icuDataFilePath
= NULL
;
285 * Test fallback file names for open of separate data files.
286 * With these params to udata_open:
287 * path = wherever/testdata
290 * these files will be tried first:
291 * wherever/testudata_nam.typ
293 * A test data file named testudata_nam.typ exists for the purpose of testing this.
295 log_verbose("Testing udata_open, with base_name.type style fallback to individual file.\n");
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
));
303 free(icuDataFilePath
);
306 /* This type of path is deprecated */
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.
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)); */
321 /* udata_close(result); */
322 /* free(icuDataFilePath); */
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");
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
)) {
338 log_err("calling udata_open with non-existing file didn't return a null value\n");
340 log_verbose("calling udat_open with non-existing file returned null as expected\n");
344 * Try opening data with absurdly long path and name, to trigger buffer size
345 * overflow handling code.
348 char longTestPath
[1024]; /* Implementation goes to heap at length of 128. */
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.
355 log_verbose("Testing udata_open() with really long names\n");
357 strcat(longTestPath
, "bogus_directory_name");
358 while (strlen(longTestPath
) < 500) {
359 strcat(longTestPath
, dirSepString
);
360 strcat(longTestPath
, "bogus_directory_name");
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
));
369 log_verbose("PASS: udata_open worked\n");
373 /* Try a very long name. Won't open, but shouldn't blow up.
376 while (strlen(longName
) < 500) {
377 strcat(longName
, name
);
378 strcat(longName
, "_");
380 strcat(longName
, dirSepString
);
381 strcat(longName
, name
);
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
));
397 uint8_t magic1
, magic2
;
400 uint32_t count
, reserved
;
403 const char *const name;
404 const void *const data;
407 int32_t fakeNameAndData
[4];
408 } ICU_COMMON_Data_Header
;
410 static const ICU_COMMON_Data_Header gEmptyHeader
= {
412 0xda, /* magic1, (see struct MappedData in udata.c) */
415 sizeof(UDataInfo
), /* size */
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 */
432 {0,0,0,0,0,0,0,0}, /* Padding[8] */
435 { /* TOC structure */
437 0 , 0 , 0, 0 /* name and data entries. Count says there are none, */
438 /* but put one in just in case. */
444 static void TestUDataSetAppData(){
445 /* UDataMemory *dataItem;*/
447 UErrorCode status
=U_ZERO_ERROR
;
450 * First we try some monkey business and try to do bad things.
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
));
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
);
465 * Got testdata.dat into memory, now we try setAppData using the memory image.
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
));
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
));
483 /* If we try to setAppData with the same name a second time, we should get a
484 * a using default warning.
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
));
493 /** It is no longer correct to use udata_setAppData to change the
494 package of a contained item.
496 dataItem = udata_open("appData1", "res", "te_IN", &status); **/
499 static char *safeGetICUDataDirectory() {
500 const char *dataDir
= u_getDataDirectory(); /* Returned string vanashes with u_cleanup */
502 if (dataDir
!= NULL
) {
503 retStr
= (char *)malloc(strlen(dataDir
)+1);
504 strcpy(retStr
, dataDir
);
509 static void TestUDataFileAccess(){
512 icuDataDir
= safeGetICUDataDirectory(); /* save icu data dir, so we can put it back
513 * after doing u_cleanup(). */
515 /** UDATA_NO_FILES, ICU does not access the file system for data loading. */
518 udata_setFileAccess(UDATA_NO_FILES
,&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
));
524 /** UDATA_ONLY_PACKAGES, ICU only loads data from packages, not from single files. */
527 udata_setFileAccess(UDATA_ONLY_PACKAGES
,&status
);
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. */
534 udata_setFileAccess(UDATA_PACKAGES_FIRST
,&status
);
537 /** UDATA_FILES_FIRST, ICU looks for data in single files first, then in packages. (default) */
540 udata_setFileAccess(UDATA_FILES_FIRST
,&status
);
543 /** An alias for the default access mode. */
546 udata_setFileAccess(UDATA_DEFAULT_ACCESS
,&status
);
547 u_setDataDirectory(icuDataDir
);
549 if(U_FAILURE(status
)){
550 log_err_status(status
, "%s\n", u_errorName(status
));
557 static UBool U_CALLCONV
558 isAcceptable1(void *context
,
559 const char *type
, const char *name
,
560 const UDataInfo
*pInfo
) {
561 (void)context
; // suppress compiler warnings about unused variable
563 if( pInfo
->size
>=20 &&
564 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
565 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
566 pInfo
->dataFormat
[0]==0x43 && /* dataFormat="CvAl" */
567 pInfo
->dataFormat
[1]==0x76 &&
568 pInfo
->dataFormat
[2]==0x41 &&
569 pInfo
->dataFormat
[3]==0x6c &&
570 pInfo
->formatVersion
[0]==3 )
572 log_verbose("The data from \"%s.%s\" IS acceptable using the verifing function isAcceptable1()\n", name
, type
);
575 log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable1():-\n"
577 "\tisBigEndian = %d\n"
578 "\tcharsetFamily = %d\n"
579 "\tformatVersion[0] = %d\n"
580 "\tdataVersion[0] = %d\n"
581 "\tdataFormat = %c%c%c%c\n",
582 name
, type
, pInfo
->size
, pInfo
->isBigEndian
, pInfo
->charsetFamily
, pInfo
->formatVersion
[0],
583 pInfo
->dataVersion
[0], pInfo
->dataFormat
[0], pInfo
->dataFormat
[1], pInfo
->dataFormat
[2],
584 pInfo
->dataFormat
[3]);
585 log_verbose("Call another verifing function to accept the data\n");
590 static UBool U_CALLCONV
591 isAcceptable2(void *context
,
592 const char *type
, const char *name
,
593 const UDataInfo
*pInfo
){
594 (void)context
; // suppress compiler warnings about unused variable
595 UVersionInfo unicodeVersion
;
597 u_getUnicodeVersion(unicodeVersion
);
599 if( pInfo
->size
>=20 &&
600 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
601 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
602 pInfo
->dataFormat
[0]==0x75 && /* dataFormat="unam" */
603 pInfo
->dataFormat
[1]==0x6e &&
604 pInfo
->dataFormat
[2]==0x61 &&
605 pInfo
->dataFormat
[3]==0x6d &&
606 pInfo
->formatVersion
[0]==1 &&
607 pInfo
->dataVersion
[0]==unicodeVersion
[0] )
609 log_verbose("The data from \"%s.%s\" IS acceptable using the verifing function isAcceptable2()\n", name
, type
);
612 log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable2()\n", name
, type
);
619 static UBool U_CALLCONV
620 isAcceptable3(void *context
,
621 const char *type
, const char *name
,
622 const UDataInfo
*pInfo
){
623 (void)context
; // suppress compiler warnings about unused variable
625 if( pInfo
->size
>=20 &&
626 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
627 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
628 pInfo
->dataFormat
[0]==0x54 && /* dataFormat="test" */
629 pInfo
->dataFormat
[1]==0x65 &&
630 pInfo
->dataFormat
[2]==0x73 &&
631 pInfo
->dataFormat
[3]==0x74 &&
632 pInfo
->formatVersion
[0]==1 &&
633 pInfo
->dataVersion
[0]==1 ) {
634 log_verbose("The data from \"%s.%s\" IS acceptable using the verifing function isAcceptable3()\n", name
, type
);
638 log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable3()\n", name
, type
);
645 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
646 static void TestUDataOpenChoiceDemo1() {
648 UErrorCode status
=U_ZERO_ERROR
;
656 const char* type
="icu";
657 const char* testPath
="testdata";
658 const char* fullTestDataPath
= loadTestData(&status
);
659 if(U_FAILURE(status
)) {
660 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
664 result
=udata_openChoice(NULL
, "icu", name
[0], isAcceptable1
, NULL
, &status
);
665 if(U_FAILURE(status
)){
666 log_data_err("FAIL: udata_openChoice() failed name=%s, type=%s, \n errorcode=%s\n", name
[0], type
, myErrorName(status
));
668 log_verbose("PASS: udata_openChoice worked\n");
673 result
=udata_openChoice(NULL
, type
, name
[1], isAcceptable1
, NULL
, &status
);
674 if(U_FAILURE(status
)){
676 result
=udata_openChoice(NULL
, type
, name
[1], isAcceptable2
, NULL
, &status
);
677 if(U_FAILURE(status
)){
678 log_data_err("FAIL: udata_openChoice() failed name=%s, type=%s, \n errorcode=%s\n", name
[1], type
, myErrorName(status
));
682 log_err("FAIL: udata_openChoice() unexpectedly passed. name=%s, type=%s, \n errorcode=%s\n", name
[1], type
, myErrorName(status
));
685 if(U_SUCCESS(status
)){
690 result
=udata_openChoice(testPath
, type
, name
[2], isAcceptable1
, NULL
, &status
);
691 if(U_FAILURE(status
)){
693 result
=udata_openChoice(testPath
, type
, name
[2], isAcceptable3
, NULL
, &status
);
694 if(U_FAILURE(status
)){
695 log_data_err("FAIL: udata_openChoice() failed path=%s name=%s, type=%s, \n errorcode=%s\n", testPath
, name
[2], type
, myErrorName(status
));
699 log_err("FAIL: udata_openChoice() unexpectedly passed. name=%s, type=%s, \n errorcode=%s\n", name
[2], type
, myErrorName(status
));
702 if(U_SUCCESS(status
)){
708 result
=udata_openChoice(fullTestDataPath
, type
, name
[3], isAcceptable1
, NULL
, &status
);
709 if(status
!= U_INVALID_FORMAT_ERROR
){
710 log_err("FAIL: udata_openChoice() did not fail as expected. name=%s, type=%s, \n errorcode=%s\n", name
[3], type
, myErrorName(status
));
713 status
=U_USELESS_COLLATOR_ERROR
;
714 result
=udata_openChoice(fullTestDataPath
, type
, name
[3], isAcceptable1
, NULL
, &status
);
715 if(status
!= U_USELESS_COLLATOR_ERROR
){
716 log_err("FAIL: udata_openChoice() did not fail as expected. name=%s, type=%s, \n errorcode=%s\n", name
[3], type
, myErrorName(status
));
720 static UBool U_CALLCONV
721 isAcceptable(void *context
,
722 const char *type
, const char *name
,
723 const UDataInfo
*pInfo
){
724 if( pInfo
->size
>=20 &&
725 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
726 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
727 pInfo
->dataFormat
[0]==0x54 && /* dataFormat="test" */
728 pInfo
->dataFormat
[1]==0x65 &&
729 pInfo
->dataFormat
[2]==0x73 &&
730 pInfo
->dataFormat
[3]==0x74 &&
731 pInfo
->formatVersion
[0]==1 &&
732 pInfo
->dataVersion
[0]==1 &&
733 *((int*)context
) == 2 ) {
734 log_verbose("The data from\"%s.%s\" IS acceptable using the verifing function isAcceptable()\n", name
, type
);
738 log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable()\n", name
, type
);
744 /* This test checks to see if the isAcceptable function is being called correctly. */
746 static void TestUDataOpenChoiceDemo2() {
748 UErrorCode status
=U_ZERO_ERROR
;
752 const char* name
="test";
753 const char* type
="icu";
754 const char* path
= loadTestData(&status
);
755 if(U_FAILURE(status
)) {
756 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
760 result
=udata_openChoice(path
, type
, name
, isAcceptable
, &p
, &status
);
761 if(U_FAILURE(status
)){
762 log_data_err("failed to load data at p=%s t=%s n=%s, isAcceptable", path
, type
, name
);
764 if(U_SUCCESS(status
) ) {
770 result
=udata_openChoice(path
, type
, name
, isAcceptable
, &p
, &status
);
772 if(U_FAILURE(status
) && status
==U_INVALID_FORMAT_ERROR
){
773 log_verbose("Loads the data but rejects it as expected %s\n", myErrorName(status
));
778 log_data_err("FAIL: failed to either load the data or to reject the loaded data. ERROR=%s\n", myErrorName(status
) );
782 if(U_FAILURE(status
)) {
783 log_data_err("FAIL: failed to load the data and accept it. ERROR=%s\n", myErrorName(status
) );
786 log_verbose("Loads the data and accepts it for p==2 as expected\n");
793 static void TestUDataGetInfo() {
796 /* UDataInfo cf. udata.h */
797 static UDataInfo dataInfo
={
798 30, /*sizeof(UDataInfo),*/
806 {0x54, 0x65, 0x73, 0x74}, /* dataFormat="Test" */
807 {9, 0, 0, 0}, /* formatVersion */
808 {4, 0, 0, 0} /* dataVersion */
810 UErrorCode status
=U_ZERO_ERROR
;
811 const char* name
="cnvalias";
812 const char* name2
="test";
813 const char* type
="icu";
815 const char* testPath
=loadTestData(&status
);
816 if(U_FAILURE(status
)) {
817 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
821 log_verbose("Testing udata_getInfo() for cnvalias.icu\n");
822 result
=udata_open(NULL
, "icu", name
, &status
);
823 if(U_FAILURE(status
)){
824 log_data_err("FAIL: udata_open() failed for path = NULL, name=%s, type=%s, \n errorcode=%s\n", name
, type
, myErrorName(status
));
827 udata_getInfo(result
, &dataInfo
);
828 if(dataInfo
.size
==20 && dataInfo
.size
!=30 &&
829 dataInfo
.isBigEndian
==U_IS_BIG_ENDIAN
&&
830 dataInfo
.charsetFamily
==U_CHARSET_FAMILY
&&
831 dataInfo
.dataFormat
[0]==0x43 && dataInfo
.dataFormat
[0]!=0x54 && /* dataFormat="CvAl" and not "Test". The values are set for cnvalias.dat*/
832 dataInfo
.dataFormat
[1]==0x76 && dataInfo
.dataFormat
[1]!=0x65 &&
833 dataInfo
.dataFormat
[2]==0x41 && dataInfo
.dataFormat
[2]!=0x73 &&
834 dataInfo
.dataFormat
[3]==0x6c && dataInfo
.dataFormat
[3]!=0x74 &&
835 dataInfo
.formatVersion
[0]!=9 && /*formatVersion is also set to the one for cnvalias*/
836 dataInfo
.dataVersion
[0]!=4 && /*dataVersion*/
837 dataInfo
.dataVersion
[1]!=0 ){
838 log_verbose("PASS: udata_getInfo() filled in the right values\n");
840 log_err("FAIL: udata_getInfo() filled in the wrong values\n");
845 log_verbose("Testing udata_getInfo() for test.icu\n");
846 result
=udata_open(testPath
, type
, name2
, &status
);
847 if(U_FAILURE(status
)) {
848 log_data_err("FAIL: udata_open() failed for path=%s name2=%s, type=%s, \n errorcode=%s\n", testPath
, name2
, type
, myErrorName(status
));
851 udata_getInfo(result
, &dataInfo
);
852 if(dataInfo
.size
==20 &&
853 dataInfo
.isBigEndian
==U_IS_BIG_ENDIAN
&&
854 dataInfo
.charsetFamily
==U_CHARSET_FAMILY
&&
855 dataInfo
.dataFormat
[0]==0x54 && /* dataFormat="Test". The values are set for test.dat*/
856 dataInfo
.dataFormat
[1]==0x65 &&
857 dataInfo
.dataFormat
[2]==0x73 &&
858 dataInfo
.dataFormat
[3]==0x74 &&
859 dataInfo
.formatVersion
[0]==1 && /*formatVersion is also set to the one for test*/
860 dataInfo
.dataVersion
[0]==1 && /*dataVersion*/
861 dataInfo
.dataVersion
[1]==0 )
863 log_verbose("PASS: udata_getInfo() filled in the right values\n");
865 log_err("FAIL: udata_getInfo() filled in the wrong values\n");
870 static void TestUDataGetMemory() {
873 const int32_t *table
=NULL
;
874 uint16_t* intValue
=0;
875 UErrorCode status
=U_ZERO_ERROR
;
876 const char* name
="cnvalias";
879 const char* name2
="test";
881 const char* testPath
= loadTestData(&status
);
882 if(U_FAILURE(status
)) {
883 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
888 log_verbose("Testing udata_getMemory() for \"cnvalias.icu\"\n");
889 result
=udata_openChoice(NULL
, type
, name
, isAcceptable1
, NULL
, &status
);
890 if(U_FAILURE(status
)){
891 log_data_err("FAIL: udata_openChoice() failed for name=%s, type=%s, \n errorcode=%s\n", name
, type
, myErrorName(status
));
894 table
=(const int32_t *)udata_getMemory(result
);
896 /* The alias table may list more converters than what's actually available now. [grhoten] */
897 if(ucnv_countAvailable() > table
[1]) /*???*/
898 log_err("FAIL: udata_getMemory() failed ucnv_countAvailable returned = %d, expected = %d\n", ucnv_countAvailable(), table
[1+2*(*table
)]);
903 log_verbose("Testing udata_getMemory for \"test.icu\"()\n");
904 result
=udata_openChoice(testPath
, type
, name2
, isAcceptable3
, NULL
, &status
);
905 if(U_FAILURE(status
)){
906 log_data_err("FAIL: udata_openChoice() failed for path=%s name=%s, type=%s, \n errorcode=%s\n", testPath
, name2
, type
, myErrorName(status
));
909 intValue
=(uint16_t *)udata_getMemory(result
);
910 /*printf("%d ..... %s", *(intValue), intValue+1));*/
911 if( *intValue
!= 2000 || strcmp((char*)(intValue
+1), "YEAR") != 0 )
912 log_err("FAIL: udata_getMemory() failed: intValue :- Expected:2000 Got:%d \n\tstringValue:- Expected:YEAR Got:%s\n", *intValue
, (intValue
+1));
918 static void TestErrorConditions(){
920 UDataMemory
*result
=NULL
;
921 UErrorCode status
=U_ZERO_ERROR
;
922 uint16_t* intValue
=0;
923 static UDataInfo dataInfo
={
924 30, /*sizeof(UDataInfo),*/
932 {0x54, 0x65, 0x73, 0x74}, /* dataFormat="Test" */
933 {9, 0, 0, 0}, /* formatVersion */
934 {4, 0, 0, 0} /* dataVersion */
937 const char* name
= "test";
938 const char* type
="icu";
940 const char *testPath
= loadTestData(&status
);
941 if(U_FAILURE(status
)) {
942 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
946 status
= U_ILLEGAL_ARGUMENT_ERROR
;
947 /*Try udata_open with status != U_ZERO_ERROR*/
948 log_verbose("Testing udata_open() with status != U_ZERO_ERROR\n");
949 result
=udata_open(testPath
, type
, name
, &status
);
951 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
);
955 log_verbose("PASS: udata_open with errorCode != U_ZERO_ERROR failed as expected\n");
958 /*Try udata_open with data name=NULL*/
959 log_verbose("Testing udata_open() with data name=NULL\n");
961 result
=udata_open(testPath
, type
, NULL
, &status
);
962 if(U_FAILURE(status
)){
963 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
|| result
!= NULL
){
964 log_err("FAIL: udata_open() with name=NULL should return NULL and errocode U_ILLEGAL_ARGUMENT_ERROR, GOT: errorcode=%s\n", myErrorName(status
));
966 log_verbose("PASS: udata_open with name=NULL failed as expected and errorcode = %s as expected\n", myErrorName(status
));
969 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
);
974 /*Try udata_openChoice with status != U_ZERO_ERROR*/
975 log_verbose("Testing udata_openChoice() with status != U_ZERO_ERROR\n");
976 status
=U_ILLEGAL_ARGUMENT_ERROR
;
977 result
=udata_openChoice(testPath
, type
, name
, isAcceptable3
, NULL
, &status
);
979 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
);
982 log_verbose("PASS: udata_openChoice() with errorCode != U_ZERO_ERROR failed as expected\n");
985 /*Try udata_open with data name=NULL*/
986 log_verbose("Testing udata_openChoice() with data name=NULL\n");
988 result
=udata_openChoice(testPath
, type
, NULL
, isAcceptable3
, NULL
, &status
);
989 if(U_FAILURE(status
)){
990 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
|| result
!= NULL
){
991 log_err("FAIL: udata_openChoice() with name=NULL should return NULL and errocode U_ILLEGAL_ARGUMENT_ERROR, GOT: errorcode=%s\n", myErrorName(status
));
993 log_verbose("PASS: udata_openChoice with name=NULL failed as expected and errorcode = %s as expected\n", myErrorName(status
));
996 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
);
1000 /*Try udata_getMemory with UDataMemory=NULL*/
1001 log_verbose("Testing udata_getMemory with UDataMemory=NULL\n");
1002 intValue
=(uint16_t*)udata_getMemory(NULL
);
1003 if(intValue
!= NULL
){
1004 log_err("FAIL: udata_getMemory with UDataMemory = NULL is supposed to fail\n");
1007 /*Try udata_getInfo with UDataMemory=NULL*/
1008 status
=U_ZERO_ERROR
;
1009 udata_getInfo(NULL
, &dataInfo
);
1010 if(dataInfo
.size
!= 0){
1011 log_err("FAIL : udata_getInfo with UDataMemory = NULL us supposed to fail\n");
1014 /*Try udata_openChoice with a non existing binary file*/
1015 log_verbose("Testing udata_openChoice() with a non existing binary file\n");
1016 result
=udata_openChoice(testPath
, "tst", "nonexist", isAcceptable3
, NULL
, &status
);
1017 if(status
==U_FILE_ACCESS_ERROR
){
1018 log_verbose("Opening udata_openChoice with non-existing file handled correctly.\n");
1019 status
=U_ZERO_ERROR
;
1021 log_err("calling udata_open with non-existing file not handled correctly\n. Expected: U_FILE_ACCESS_ERROR, Got: %s\n", myErrorName(status
));
1022 if(U_SUCCESS(status
)) {
1023 udata_close(result
);
1028 log_err("calling udata_open with non-existing file didn't return a null value\n");
1030 log_verbose("calling udat_open with non-existing file returned null as expected\n");
1034 /* Test whether apps and ICU can each have their own root.res */
1035 static void TestAppData()
1037 UResourceBundle
*icu
, *app
;
1038 UResourceBundle
*tmp
= NULL
;
1039 UResourceBundle
*tmp2
= NULL
;
1041 const UChar
*appString
;
1042 const UChar
*icuString
;
1046 UErrorCode status
= U_ZERO_ERROR
;
1047 char testMsgBuf
[256];
1049 const char* testPath
=loadTestData(&status
);
1050 if(U_FAILURE(status
)) {
1051 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
1055 icu
= ures_open(NULL
, "root", &status
);
1056 if(U_FAILURE(status
))
1058 log_data_err("%s:%d: Couldn't open root ICU bundle- %s", __FILE__
, __LINE__
, u_errorName(status
));
1061 /* log_info("Open icu root: %s size_%d\n", u_errorName(status), ures_getSize(icu)); */
1062 status
= U_ZERO_ERROR
;
1064 app
= ures_open(testPath
, "root", &status
);
1065 if(U_FAILURE(status
))
1067 log_data_err("%s:%d: Couldn't open app ICU bundle [%s]- %s", __FILE__
, __LINE__
, testPath
, u_errorName(status
));
1070 /* log_info("Open app: %s, size %d\n", u_errorName(status), ures_getSize(app)); */
1072 tmp
= ures_getByKey(icu
, "Version", tmp
, &status
);
1073 if(U_FAILURE(status
))
1075 log_err("%s:%d: Couldn't get Version string from ICU root bundle- %s", __FILE__
, __LINE__
, u_errorName(status
));
1079 icuString
= ures_getString(tmp
, &len
, &status
);
1080 if(U_FAILURE(status
))
1082 log_err("%s:%d: Couldn't get string from Version string from ICU root bundle- %s", __FILE__
, __LINE__
, u_errorName(status
));
1085 /* log_info("icuString=%p - %s\n", icuString, austrdup(icuString)); */
1088 tmp2
= ures_getByKey(app
, "Version", tmp2
, &status
);
1089 if(U_FAILURE(status
))
1091 log_err("%s:%d: Couldn't get Version string from App root bundle- %s", __FILE__
, __LINE__
, u_errorName(status
));
1095 appString
= ures_getString(tmp2
, &len
, &status
);
1096 if(U_FAILURE(status
))
1098 log_err("%s:%d: Couldn't get string from Version string from App root bundle- %s", __FILE__
, __LINE__
, u_errorName(status
));
1102 /* log_info("appString=%p - %s\n", appString, austrdup(appString)); */
1105 if(!u_strcmp(icuString
, appString
))
1107 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
),
1108 austrdup(appString
));
1112 log_verbose("%s:%d: appstr=%s, icustr=%s\n", __FILE__
,
1113 __LINE__
, u_austrcpy(testMsgBuf
, appString
), u_austrcpy(testMsgBuf
, icuString
));
1123 static void TestICUDataName()
1125 UVersionInfo icuVersion
;
1126 char expectDataName
[20];
1127 unsigned int expectLen
= 8;
1129 char typeChar
= '?';
1131 /* Print out the version # we have .. */
1132 log_verbose("utypes.h says U_ICUDATA_NAME = %s\n", U_ICUDATA_NAME
);
1134 /* Build up the version # we expect to get */
1135 u_getVersion(icuVersion
);
1137 switch(U_CHARSET_FAMILY
)
1139 case U_ASCII_FAMILY
:
1140 switch((int)U_IS_BIG_ENDIAN
)
1149 log_err("Expected 1 or 0 for U_IS_BIG_ENDIAN, got %d!\n", (int)U_IS_BIG_ENDIAN
);
1153 case U_EBCDIC_FAMILY
:
1158 /* Only major number is needed. */
1159 sprintf(expectDataName
, "%s%d%c",
1164 log_verbose("Expected: %s\n", expectDataName
);
1165 if(uprv_strlen(expectDataName
) != expectLen
)
1167 log_err("*Expected* length is wrong (test err?), should be %d is %d\n",
1168 expectLen
, uprv_strlen(expectDataName
));
1171 if(uprv_strlen(U_ICUDATA_NAME
) != expectLen
)
1173 log_err("U_ICUDATA_NAME length should be %d is %d\n",
1174 expectLen
, uprv_strlen(U_ICUDATA_NAME
));
1177 if(uprv_strcmp(U_ICUDATA_NAME
, expectDataName
))
1179 log_err("U_ICUDATA_NAME should be %s but is %s\n",
1180 expectDataName
, U_ICUDATA_NAME
);
1183 /* ICUDATA_NAME comes from the build system on *nix */
1185 if(uprv_strcmp(U_ICUDATA_NAME
, ICUDATA_NAME
))
1187 log_err("ICUDATA_NAME and U_ICUDATA_NAME don't match: "
1188 "ICUDATA_NAME=%s, U_ICUDATA_NAME=%s. Check configure.in, icudefs.mk.in, utypes.h...\n", ICUDATA_NAME
, U_ICUDATA_NAME
);
1192 log_verbose("ICUDATA_NAME=%s (from icudefs.mk), U_ICUDATA_NAME=%s (from utypes.h)\n", ICUDATA_NAME
, U_ICUDATA_NAME
);
1198 /* test data swapping ------------------------------------------------------- */
1200 #if U_PLATFORM == U_PF_OS400
1201 /* See comments in genccode.c on when this special implementation can be removed. */
1202 static const struct {
1205 } gOffsetTOCAppDataItem1
={ 0.0, /* alignment bytes */
1206 "\x00\x14" /* sizeof(UDataInfo) *//* MappedData { */
1209 "\x00\x14" /* sizeof(UDataInfo) *//* UDataInfo { */
1211 "\1" /* U_IS_BIG_ENDIAN */
1212 "\1" /* U_CHARSET_FAMILY */
1213 "\2" /* U_SIZEOF_WHAR_T */
1220 static const struct {
1225 } gOffsetTOCAppDataItem1
={
1226 0.0, /* alignment bytes */
1227 { sizeof(UDataInfo
), 0xda, 0x27 }, /* MappedData */
1237 {0x31, 0x31, 0x31, 0x31}, /* dataFormat="1111" */
1238 {0, 0, 0, 0}, /* formatVersion */
1239 {0, 0, 0, 0}}, /* dataVersion */
1244 static const UChar gOffsetTOCGarbage
[] = { /* "I have been very naughty!" */
1245 0x49, 0x20, 0x68, 0x61, 0x76, 0x65, 0x20, 0x62, 0x65, 0x65, 0x6E,
1246 0x20, 0x76, 0x65, 0x72, 0x79, 0x20, 0x6E, 0x61, 0x75, 0x67, 0x68, 0x74, 0x79, 0x21
1249 /* Original source: icu/source/tools/genccode */
1250 static const struct {
1251 uint16_t headerSize
;
1252 uint8_t magic1
, magic2
;
1255 uint32_t count
, reserved
;
1257 const char *const name
;
1258 const void *const data
;
1260 } gOffsetTOCAppData_dat
= {
1261 32, /* headerSize */
1262 0xda, /* magic1, (see struct MappedData in udata.c) */
1265 sizeof(UDataInfo
), /* size */
1271 { /* data format identifier */
1272 0x54, 0x6f, 0x43, 0x50}, /* "ToCP" */
1273 {1, 0, 0, 0}, /* format version major, minor, milli, micro */
1274 {0, 0, 0, 0} /* dataVersion */
1276 {0,0,0,0,0,0,0,0}, /* Padding[8] */
1279 { /* TOC structure */
1280 { "OffsetTOCAppData/a/b", &gOffsetTOCAppDataItem1
},
1281 { "OffsetTOCAppData/gOffsetTOCAppDataItem1", &gOffsetTOCAppDataItem1
},
1282 { "OffsetTOCAppData/gOffsetTOCGarbage", &gOffsetTOCGarbage
}
1286 /* Unfortunately, dictionaries are in a C++ header */
1287 U_CAPI
int32_t U_EXPORT2
1288 udict_swap(const UDataSwapper
*ds
, const void *inData
, int32_t length
, void *outData
, UErrorCode
*pErrorCode
);
1290 /* test cases for maximum data swapping code coverage */
1291 static const struct {
1292 const char *name
, *type
;
1293 UDataSwapFn
*swapFn
;
1295 /* resource bundles */
1297 /* resource bundle with many data types */
1298 {"*testtypes", "res", ures_swap
},
1299 /* resource bundle with collation data */
1300 {"ja", "res", ures_swap
},
1301 /* resource bundle with options-only collation data */
1302 {"ru", "res", ures_swap
},
1303 {"el", "res", ures_swap
},
1305 {"root", "res", ures_swap
},
1306 /* Test a 32-bit key table. This is large. */
1307 {"*testtable32", "res", ures_swap
},
1309 /* ICU 4.2 resource bundle - data format 1.2 (little-endian ASCII) */
1310 {"*old_l_testtypes", "res", ures_swap
},
1311 /* same for big-endian EBCDIC */
1312 {"*old_e_testtypes", "res", ures_swap
},
1314 #if !UCONFIG_NO_COLLATION
1315 /* standalone collation data files */
1316 {"ucadata", "icu", ucol_swap
},
1318 /* Starting with ICU 53, the "inverse UCA" data is integrated into ucadata.icu. */
1319 {"invuca", "icu", ucol_swapInverseUCA
},
1323 #if !UCONFIG_NO_LEGACY_CONVERSION
1324 /* conversion table files */
1326 /* SBCS conversion table file without extension */
1327 {"ibm-913_P100-2000", "cnv", ucnv_swap
},
1328 /* EBCDIC_STATEFUL conversion table file with extension */
1329 {"ibm-1390_P110-2003", "cnv", ucnv_swap
},
1330 /* DBCS extension-only conversion table file */
1331 {"ibm-16684_P110-2003", "cnv", ucnv_swap
},
1332 /* EUC-TW (3-byte) conversion table file without extension */
1333 {"ibm-964_P110-1999", "cnv", ucnv_swap
},
1334 /* GB 18030 (4-byte) conversion table file without extension */
1335 {"gb18030", "cnv", ucnv_swap
},
1336 /* MBCS conversion table file with extension */
1337 {"*test4x", "cnv", ucnv_swap
},
1339 * MBCS conversion table file without extension,
1340 * to test swapping and preflighting of UTF-8-friendly mbcsIndex[].
1342 {"jisx-212", "cnv", ucnv_swap
},
1345 #if !UCONFIG_NO_CONVERSION
1347 {"cnvalias", "icu", ucnv_swapAliases
},
1350 #if !UCONFIG_NO_IDNA
1351 {"rfc3491", "spp", usprep_swap
},
1354 #if !UCONFIG_NO_BREAK_ITERATION
1355 {"char", "brk", ubrk_swap
},
1356 {"thaidict", "dict",udict_swap
},
1361 * Starting with ICU 4.8, the Unicode property (value) aliases data
1362 * is hardcoded in the ICU4C common library.
1363 * The swapper was moved to the toolutil library for swapping for ICU4J.
1365 /* Unicode properties */
1366 {"pnames", "icu", upname_swap
},
1371 * Starting with ICU4C 3.4, the core Unicode properties files
1372 * (uprops.icu, ucase.icu, ubidi.icu, unorm.icu)
1373 * are hardcoded in the common DLL and therefore not included
1374 * in the data package any more.
1375 * Their swapping code is moved from the common DLL to the icuswap tool so that
1376 * we need not jump through hoops (like adding snapshots of these files
1377 * to testdata) for code coverage in tests.
1378 * See Jitterbug 4497.
1380 * ICU4C 4.4 adds normalization data files again, e.g., nfkc.nrm.
1382 {"uprops", "icu", uprops_swap
},
1383 {"ucase", "icu", ucase_swap
},
1384 {"ubidi", "icu", ubidi_swap
},
1386 #if !UCONFIG_NO_NORMALIZATION && !UCONFIG_ONLY_COLLATION
1387 {"nfkc", "nrm", unorm2_swap
},
1388 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
1389 {"confusables", "cfu", uspoof_swap
}, /* spoof data missing without regex */
1393 {"unames", "icu", uchar_swapNames
}
1394 /* the last item should not be #if'ed so that it can reliably omit the last comma */
1397 /* Large enough for the largest swappable data item. */
1398 #define SWAP_BUFFER_SIZE 1800000
1400 static void U_CALLCONV
1401 printError(void *context
, const char *fmt
, va_list args
) {
1402 (void)context
; // suppress compiler warnings about unused variable
1403 vlog_info("[swap] ", fmt
, args
);
1404 log_err("\n"); /* Register error */
1408 TestSwapCase(UDataMemory
*pData
, const char *name
,
1409 UDataSwapFn
*swapFn
,
1410 uint8_t *buffer
, uint8_t *buffer2
) {
1412 const void *inData
, *inHeader
;
1413 int32_t length
, dataLength
, length2
, headerLength
;
1415 UErrorCode errorCode
;
1416 UErrorCode badStatus
;
1418 UBool inEndian
, oppositeEndian
;
1419 uint8_t inCharset
, oppositeCharset
;
1421 /* First we check that swapFn handles failures as expected. */
1422 errorCode
= U_UNSUPPORTED_ERROR
;
1423 length
= swapFn(NULL
, NULL
, 0, buffer
, &errorCode
);
1424 if (length
!= 0 || errorCode
!= U_UNSUPPORTED_ERROR
) {
1425 log_err("%s() did not fail as expected - %s\n", name
, u_errorName(errorCode
));
1427 errorCode
= U_ZERO_ERROR
;
1428 length
= swapFn(NULL
, NULL
, 0, buffer
, &errorCode
);
1429 if (length
!= 0 || errorCode
!= U_ILLEGAL_ARGUMENT_ERROR
) {
1430 log_err("%s() did not fail as expected with bad arguments - %s\n", name
, u_errorName(errorCode
));
1434 /* Continue with the rest of the tests. */
1435 errorCode
= U_ZERO_ERROR
;
1436 inData
=udata_getMemory(pData
);
1439 * get the data length if possible, to verify that swapping and preflighting
1440 * handles the entire data
1442 dataLength
=udata_getLength(pData
);
1445 * get the header and its length
1446 * all of the swap implementation functions require the header to be included
1448 inHeader
=udata_getRawMemory(pData
);
1449 headerLength
=(int32_t)((const char *)inData
-(const char *)inHeader
);
1451 /* first swap to opposite endianness but same charset family */
1452 errorCode
=U_ZERO_ERROR
;
1453 ds
=udata_openSwapperForInputData(inHeader
, headerLength
,
1454 !U_IS_BIG_ENDIAN
, U_CHARSET_FAMILY
, &errorCode
);
1455 if(U_FAILURE(errorCode
)) {
1456 log_err("udata_openSwapperForInputData(%s->!isBig+same charset) failed - %s\n",
1457 name
, u_errorName(errorCode
));
1461 inEndian
=ds
->inIsBigEndian
;
1462 inCharset
=ds
->inCharset
;
1464 oppositeEndian
=!inEndian
;
1465 oppositeCharset
= inCharset
==U_ASCII_FAMILY
? U_EBCDIC_FAMILY
: U_ASCII_FAMILY
;
1467 /* make this test work with data files that are built for a different platform */
1468 if(inEndian
!=U_IS_BIG_ENDIAN
|| inCharset
!=U_CHARSET_FAMILY
) {
1469 udata_closeSwapper(ds
);
1470 ds
=udata_openSwapper(inEndian
, inCharset
, oppositeEndian
, inCharset
, &errorCode
);
1471 if(U_FAILURE(errorCode
)) {
1472 log_err("udata_openSwapper(%s->!isBig+same charset) failed - %s\n",
1473 name
, u_errorName(errorCode
));
1479 Check error checking of swappable data not specific to this swapper.
1480 This should always fail.
1482 badStatus
= U_ZERO_ERROR
;
1483 length
=swapFn(ds
, &gOffsetTOCAppData_dat
, -1, NULL
, &badStatus
);
1484 if(badStatus
!= U_UNSUPPORTED_ERROR
) {
1485 log_err("swapFn(%s->!isBig+same charset) unexpectedly succeeded on bad data - %s\n",
1486 name
, u_errorName(errorCode
));
1487 udata_closeSwapper(ds
);
1491 /* Now allow errors to be printed */
1492 ds
->printError
=printError
;
1494 /* preflight the length */
1495 length
=swapFn(ds
, inHeader
, -1, NULL
, &errorCode
);
1496 if(U_FAILURE(errorCode
)) {
1497 log_err("swapFn(preflight %s->!isBig+same charset) failed - %s\n",
1498 name
, u_errorName(errorCode
));
1499 udata_closeSwapper(ds
);
1503 /* compare the preflighted length against the data length */
1504 if(dataLength
>=0 && (length
+15)<(headerLength
+dataLength
)) {
1505 log_err("swapFn(preflight %s->!isBig+same charset) length too small: %d < data length %d\n",
1506 name
, length
, (headerLength
+dataLength
));
1507 udata_closeSwapper(ds
);
1511 /* swap, not in-place */
1512 length2
=swapFn(ds
, inHeader
, length
, buffer
, &errorCode
);
1513 udata_closeSwapper(ds
);
1514 if(U_FAILURE(errorCode
)) {
1515 log_err("swapFn(%s->!isBig+same charset) failed - %s\n",
1516 name
, u_errorName(errorCode
));
1520 /* compare the swap length against the preflighted length */
1521 if(length2
!=length
) {
1522 log_err("swapFn(%s->!isBig+same charset) length differs from preflighting: %d != preflighted %d\n",
1523 name
, length2
, length
);
1527 /* next swap to opposite charset family */
1528 ds
=udata_openSwapper(oppositeEndian
, inCharset
,
1529 oppositeEndian
, oppositeCharset
,
1531 if(U_FAILURE(errorCode
)) {
1532 log_err("udata_openSwapper(%s->!isBig+other charset) failed - %s\n",
1533 name
, u_errorName(errorCode
));
1536 ds
->printError
=printError
;
1539 length2
=swapFn(ds
, buffer
, length
, buffer
, &errorCode
);
1540 udata_closeSwapper(ds
);
1541 if(U_FAILURE(errorCode
)) {
1542 log_err("swapFn(%s->!isBig+other charset) failed - %s\n",
1543 name
, u_errorName(errorCode
));
1547 /* compare the swap length against the original length */
1548 if(length2
!=length
) {
1549 log_err("swapFn(%s->!isBig+other charset) length differs from original: %d != original %d\n",
1550 name
, length2
, length
);
1554 /* finally swap to original platform values */
1555 ds
=udata_openSwapper(oppositeEndian
, oppositeCharset
,
1556 inEndian
, inCharset
,
1558 if(U_FAILURE(errorCode
)) {
1559 log_err("udata_openSwapper(%s->back to original) failed - %s\n",
1560 name
, u_errorName(errorCode
));
1563 ds
->printError
=printError
;
1565 /* swap, not in-place */
1566 length2
=swapFn(ds
, buffer
, length
, buffer2
, &errorCode
);
1567 udata_closeSwapper(ds
);
1568 if(U_FAILURE(errorCode
)) {
1569 log_err("swapFn(%s->back to original) failed - %s\n",
1570 name
, u_errorName(errorCode
));
1574 /* compare the swap length against the original length */
1575 if(length2
!=length
) {
1576 log_err("swapFn(%s->back to original) length differs from original: %d != original %d\n",
1577 name
, length2
, length
);
1581 /* compare the final contents with the original */
1582 if(0!=uprv_memcmp(inHeader
, buffer2
, length
)) {
1583 const uint8_t *original
;
1587 log_err("swapFn(%s->back to original) contents differs from original\n",
1590 /* find the first difference */
1591 original
=(const uint8_t *)inHeader
;
1592 for(i
=0; i
<length
&& original
[i
]==buffer2
[i
]; ++i
) {}
1594 /* find the next byte that is the same */
1595 for(j
=i
+1; j
<length
&& original
[j
]!=buffer2
[j
]; ++j
) {}
1596 log_info(" difference at index %d=0x%x, until index %d=0x%x\n", i
, i
, j
, j
);
1598 /* round down to the last 4-boundary for better result output */
1600 log_info("showing bytes from index %d=0x%x (length %d=0x%x):\n", i
, i
, length
, length
);
1602 /* print 8 bytes but limit to the buffer contents */
1603 length2
=i
+sizeof(diff
);
1604 if(length2
>length
) {
1608 /* print the original bytes */
1609 uprv_memset(diff
, 0, sizeof(diff
));
1610 for(j
=i
; j
<length2
; ++j
) {
1611 diff
[j
-i
]=original
[j
];
1613 log_info(" original: %02x %02x %02x %02x %02x %02x %02x %02x\n",
1614 diff
[0], diff
[1], diff
[2], diff
[3], diff
[4], diff
[5], diff
[6], diff
[7]);
1616 /* print the swapped bytes */
1617 uprv_memset(diff
, 0, sizeof(diff
));
1618 for(j
=i
; j
<length2
; ++j
) {
1619 diff
[j
-i
]=buffer2
[j
];
1621 log_info(" swapped: %02x %02x %02x %02x %02x %02x %02x %02x\n",
1622 diff
[0], diff
[1], diff
[2], diff
[3], diff
[4], diff
[5], diff
[6], diff
[7]);
1626 static void U_CALLCONV
1627 printErrorToString(void *context
, const char *fmt
, va_list args
) {
1628 vsprintf((char *)context
, fmt
, args
);
1631 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
1638 const char *pkg
, *nm
, *testPath
;
1639 UErrorCode errorCode
= U_ZERO_ERROR
;
1642 buffer
=(uint8_t *)malloc(2*SWAP_BUFFER_SIZE
);
1644 log_err("unable to allocate %d bytes\n", 2*SWAP_BUFFER_SIZE
);
1648 testPath
=loadTestData(&errorCode
);
1649 if(U_FAILURE(errorCode
)) {
1650 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(errorCode
));
1653 /* Test that printError works as expected. */
1654 errorCode
=U_USELESS_COLLATOR_ERROR
;
1655 ds
=udata_openSwapper(U_IS_BIG_ENDIAN
, U_ASCII_FAMILY
,
1656 !U_IS_BIG_ENDIAN
, U_ASCII_FAMILY
,
1658 if (ds
!= NULL
|| errorCode
!= U_USELESS_COLLATOR_ERROR
) {
1659 log_err("udata_openSwapper should have returned NULL with bad argument\n", name
);
1661 errorCode
=U_ZERO_ERROR
;
1662 ds
=udata_openSwapper(U_IS_BIG_ENDIAN
, U_ASCII_FAMILY
,
1663 !U_IS_BIG_ENDIAN
, U_ASCII_FAMILY
,
1665 ds
->printError
=printErrorToString
;
1666 ds
->printErrorContext
=name
;
1667 udata_printError(ds
, "This %s a %s", "is", "test");
1668 udata_closeSwapper(ds
);
1669 if (strcmp(name
, "This is a test") != 0) {
1670 log_err("udata_printError can't properly print error messages. Got = %s\n", name
);
1672 errorCode
= U_USELESS_COLLATOR_ERROR
;
1673 ds
=udata_openSwapperForInputData(NULL
, 0,
1674 !U_IS_BIG_ENDIAN
, U_ASCII_FAMILY
,
1676 if (ds
!= NULL
|| errorCode
!= U_USELESS_COLLATOR_ERROR
) {
1677 log_err("udata_openSwapperForInputData should have returned NULL with bad argument\n", name
);
1679 errorCode
=U_ZERO_ERROR
;
1680 ds
=udata_openSwapperForInputData(NULL
, 0,
1681 !U_IS_BIG_ENDIAN
, U_ASCII_FAMILY
,
1683 if (ds
!= NULL
|| errorCode
!= U_ILLEGAL_ARGUMENT_ERROR
) {
1684 log_err("udata_openSwapperForInputData should have returned NULL with bad argument\n", name
);
1686 errorCode
=U_ZERO_ERROR
;
1687 memset(buffer
, 0, sizeof(2*SWAP_BUFFER_SIZE
));
1688 ds
=udata_openSwapperForInputData(buffer
, 2*SWAP_BUFFER_SIZE
,
1689 !U_IS_BIG_ENDIAN
, U_ASCII_FAMILY
,
1691 if (ds
!= NULL
|| errorCode
!= U_UNSUPPORTED_ERROR
) {
1692 log_err("udata_openSwapperForInputData should have returned NULL with bad argument\n", name
);
1694 errorCode
=U_ZERO_ERROR
;
1696 /* Test argument checking. ucol_swap is normally tested via ures_swap, and isn't normally called directly. */
1697 #if !UCONFIG_NO_COLLATION
1698 ucol_swap(NULL
, NULL
, -1, NULL
, &errorCode
);
1699 if (errorCode
!= U_ILLEGAL_ARGUMENT_ERROR
) {
1700 log_err("ucol_swap did not fail as expected\n", name
);
1702 errorCode
=U_ZERO_ERROR
;
1705 for(i
=0; i
<UPRV_LENGTHOF(swapCases
); ++i
) {
1706 /* build the name for logging */
1707 errorCode
=U_ZERO_ERROR
;
1708 if(swapCases
[i
].name
[0]=='*') {
1710 nm
=swapCases
[i
].name
+1;
1711 uprv_strcpy(name
, "testdata");
1712 } else if (uprv_strcmp(swapCases
[i
].type
, "brk")==0
1713 || uprv_strcmp(swapCases
[i
].type
, "dict")==0) {
1714 pkg
=U_ICUDATA_BRKITR
;
1715 nm
=swapCases
[i
].name
;
1716 uprv_strcpy(name
, U_ICUDATA_BRKITR
);
1717 #if !UCONFIG_NO_COLLATION
1718 } else if (uprv_strcmp(swapCases
[i
].name
, "ucadata")==0
1719 || uprv_strcmp(swapCases
[i
].name
, "invuca")==0) {
1721 nm
=swapCases
[i
].name
;
1722 uprv_strcpy(name
, U_ICUDATA_COLL
);
1723 #endif /* !UCONFIG_NO_COLLATION */
1726 nm
=swapCases
[i
].name
;
1727 uprv_strcpy(name
, "NULL");
1729 uprv_strcat(name
, "/");
1730 uprv_strcat(name
, nm
);
1731 uprv_strcat(name
, ".");
1732 uprv_strcat(name
, swapCases
[i
].type
);
1734 pData
=udata_open(pkg
, swapCases
[i
].type
, nm
, &errorCode
);
1736 if(U_SUCCESS(errorCode
)) {
1737 TestSwapCase(pData
, name
, swapCases
[i
].swapFn
, buffer
, buffer
+SWAP_BUFFER_SIZE
);
1740 log_data_err("udata_open(%s) failed - %s\n",
1741 name
, u_errorName(errorCode
));
1749 static void PointerTableOfContents() {
1750 UDataMemory
*dataItem
;
1751 UErrorCode status
=U_ZERO_ERROR
;
1754 * Got testdata.dat into memory, now we try setAppData using the memory image.
1757 status
=U_ZERO_ERROR
;
1758 udata_setAppData("OffsetTOCAppData", &gOffsetTOCAppData_dat
, &status
);
1759 if (status
!= U_ZERO_ERROR
) {
1760 log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData1\", fileBuf, status) \n"
1761 " returned status of %s\n", u_errorName(status
));
1765 dataItem
= udata_open("OffsetTOCAppData", "", "gOffsetTOCAppDataItem1", &status
);
1766 if (U_FAILURE(status
)) {
1767 log_err("FAIL: gOffsetTOCAppDataItem1 could not be opened. status = %s\n", u_errorName(status
));
1769 if (udata_getMemory(dataItem
) != NULL
) {
1770 log_verbose("FAIL: udata_getMemory(dataItem) passed\n");
1773 log_err("FAIL: udata_getMemory returned NULL\n", u_errorName(status
));
1775 udata_close(dataItem
);
1777 dataItem
= udata_open("OffsetTOCAppData-a", "", "b", &status
);
1778 if (U_FAILURE(status
)) {
1779 log_err("FAIL: gOffsetTOCAppDataItem1 in tree \"a\" could not be opened. status = %s\n", u_errorName(status
));
1781 if (udata_getMemory(dataItem
) != NULL
) {
1782 log_verbose("FAIL: udata_getMemory(dataItem) in tree \"a\" passed\n");
1785 log_err("FAIL: udata_getMemory returned NULL\n", u_errorName(status
));
1787 udata_close(dataItem
);
1789 dataItem
= udata_open("OffsetTOCAppData", "", "gOffsetTOCGarbage", &status
);
1790 if (U_SUCCESS(status
)) {
1791 log_err("FAIL: gOffsetTOCGarbage should not be opened. status = %s\n", u_errorName(status
));
1793 dataItem
= udata_open("OffsetTOCAppData", "", "gOffsetTOCNonExistent", &status
);
1794 if (U_SUCCESS(status
)) {
1795 log_err("FAIL: gOffsetTOCNonExistent should not be found. status = %s\n", u_errorName(status
));
1800 static void SetBadCommonData(void) {
1801 /* It's difficult to test that udata_setCommonData really works within the test framework.
1802 So we just test that foolish people can't do bad things. */
1804 char badBuffer
[sizeof(gOffsetTOCAppData_dat
)];
1806 memset(badBuffer
, 0, sizeof(badBuffer
));
1807 strcpy(badBuffer
, "Hello! I'm not good data.");
1809 /* Check that we don't do anything */
1810 status
= U_FILE_ACCESS_ERROR
;
1811 udata_setCommonData(&gOffsetTOCAppData_dat
, &status
);
1812 if (status
!= U_FILE_ACCESS_ERROR
) {
1813 log_err("FAIL: udata_setCommonData changed the failure code.\n");
1815 /* Check that we fail correctly */
1816 status
= U_ZERO_ERROR
;
1817 udata_setCommonData(NULL
, &status
);
1818 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
1819 log_err("FAIL: udata_setCommonData did not fail with bad arguments.\n");
1822 /* Check that we verify that the data isn't bad */
1823 status
= U_ZERO_ERROR
;
1824 udata_setAppData("invalid path", badBuffer
, &status
);
1825 if (status
!= U_INVALID_FORMAT_ERROR
) {
1826 log_err("FAIL: udata_setAppData doesn't verify data validity.\n");
1830 // Check the override loading of time zone .res files from a specified path
1832 // Hand testing notes:
1833 // 1. Run this test with the environment variable set. The following should induce faiures:
1834 // ICU_TIMEZONE_FILES_DIR=../testdata/out/build LD_LIBRARY_PATH=../../lib:../../stubdata:../../tools/ctestfw:$LD_LIBRARY_PATH ./cintltst /udatatst/TestTZDataDir
1835 // 2. Build ICU with with U_TIMEZONE_FILES_DIR defined. This should also induce failures.
1836 // CPPFLAGS=-DU_TIMEZONE_FILES_DIR\=`pwd`/test/testdata/out/testdata ./runConfigureICU Linux
1839 static void TestTZDataDir(void) {
1840 #if !UCONFIG_NO_FORMATTING
1841 UErrorCode status
= U_ZERO_ERROR
;
1842 const char *tzDataVersion
;
1843 const char *testDataPath
;
1845 // Verify that default ICU time zone data version is something newer than 2014a.
1846 tzDataVersion
= ucal_getTZDataVersion(&status
);
1847 // printf("tz data version is %s\n", tzDataVersion);
1848 if (U_FAILURE(status
)) {
1849 log_data_err("Failed call to ucal_getTZDataVersion - %s\n", u_errorName(status
));
1851 } else if (strcmp("2014a", tzDataVersion
) == 0) {
1852 log_err("File %s:%d - expected something newer than time zone data 2014a.\n", __FILE__
, __LINE__
, tzDataVersion
);
1855 testDataPath
= loadTestData(&status
);
1856 // The path produced by loadTestData() will look something like
1857 // whatever/.../testdata/out/testdata
1858 // The test data puts an old (2014a) version of the time zone data there.
1860 // Switch ICU to the testdata version of zoneinfo64.res, which is verison 2014a.
1862 u_setTimeZoneFilesDirectory(testDataPath
, &status
);
1863 tzDataVersion
= ucal_getTZDataVersion(&status
);
1864 if (strcmp("2014a", tzDataVersion
) != 0) {
1865 log_err("File %s:%d - expected \"2014a\"; actual \"%s\"\n", __FILE__
, __LINE__
, tzDataVersion
);
1868 ctest_resetICU(); // Return ICU to using its standard tz data.
1869 tzDataVersion
= ucal_getTZDataVersion(&status
);
1870 // printf("tz data version is %s\n", tzDataVersion);
1871 if (strcmp("2014a", tzDataVersion
) == 0) {
1872 log_err("File %s:%d - expected something newer than time zone data 2014a.\n", __FILE__
, __LINE__
, tzDataVersion
);