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
) {
562 if( pInfo
->size
>=20 &&
563 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
564 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
565 pInfo
->dataFormat
[0]==0x43 && /* dataFormat="CvAl" */
566 pInfo
->dataFormat
[1]==0x76 &&
567 pInfo
->dataFormat
[2]==0x41 &&
568 pInfo
->dataFormat
[3]==0x6c &&
569 pInfo
->formatVersion
[0]==3 )
571 log_verbose("The data from \"%s.%s\" IS acceptable using the verifing function isAcceptable1()\n", name
, type
);
574 log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable1():-\n"
576 "\tisBigEndian = %d\n"
577 "\tcharsetFamily = %d\n"
578 "\tformatVersion[0] = %d\n"
579 "\tdataVersion[0] = %d\n"
580 "\tdataFormat = %c%c%c%c\n",
581 name
, type
, pInfo
->size
, pInfo
->isBigEndian
, pInfo
->charsetFamily
, pInfo
->formatVersion
[0],
582 pInfo
->dataVersion
[0], pInfo
->dataFormat
[0], pInfo
->dataFormat
[1], pInfo
->dataFormat
[2],
583 pInfo
->dataFormat
[3]);
584 log_verbose("Call another verifing function to accept the data\n");
589 static UBool U_CALLCONV
590 isAcceptable2(void *context
,
591 const char *type
, const char *name
,
592 const UDataInfo
*pInfo
){
593 UVersionInfo unicodeVersion
;
595 u_getUnicodeVersion(unicodeVersion
);
597 if( pInfo
->size
>=20 &&
598 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
599 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
600 pInfo
->dataFormat
[0]==0x75 && /* dataFormat="unam" */
601 pInfo
->dataFormat
[1]==0x6e &&
602 pInfo
->dataFormat
[2]==0x61 &&
603 pInfo
->dataFormat
[3]==0x6d &&
604 pInfo
->formatVersion
[0]==1 &&
605 pInfo
->dataVersion
[0]==unicodeVersion
[0] )
607 log_verbose("The data from \"%s.%s\" IS acceptable using the verifing function isAcceptable2()\n", name
, type
);
610 log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable2()\n", name
, type
);
617 static UBool U_CALLCONV
618 isAcceptable3(void *context
,
619 const char *type
, const char *name
,
620 const UDataInfo
*pInfo
){
622 if( pInfo
->size
>=20 &&
623 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
624 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
625 pInfo
->dataFormat
[0]==0x54 && /* dataFormat="test" */
626 pInfo
->dataFormat
[1]==0x65 &&
627 pInfo
->dataFormat
[2]==0x73 &&
628 pInfo
->dataFormat
[3]==0x74 &&
629 pInfo
->formatVersion
[0]==1 &&
630 pInfo
->dataVersion
[0]==1 ) {
631 log_verbose("The data from \"%s.%s\" IS acceptable using the verifing function isAcceptable3()\n", name
, type
);
635 log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable3()\n", name
, type
);
642 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
643 static void TestUDataOpenChoiceDemo1() {
645 UErrorCode status
=U_ZERO_ERROR
;
653 const char* type
="icu";
654 const char* testPath
="testdata";
655 const char* fullTestDataPath
= loadTestData(&status
);
656 if(U_FAILURE(status
)) {
657 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
661 result
=udata_openChoice(NULL
, "icu", name
[0], isAcceptable1
, NULL
, &status
);
662 if(U_FAILURE(status
)){
663 log_data_err("FAIL: udata_openChoice() failed name=%s, type=%s, \n errorcode=%s\n", name
[0], type
, myErrorName(status
));
665 log_verbose("PASS: udata_openChoice worked\n");
670 result
=udata_openChoice(NULL
, type
, name
[1], isAcceptable1
, NULL
, &status
);
671 if(U_FAILURE(status
)){
673 result
=udata_openChoice(NULL
, type
, name
[1], isAcceptable2
, NULL
, &status
);
674 if(U_FAILURE(status
)){
675 log_data_err("FAIL: udata_openChoice() failed name=%s, type=%s, \n errorcode=%s\n", name
[1], type
, myErrorName(status
));
679 log_err("FAIL: udata_openChoice() unexpectedly passed. name=%s, type=%s, \n errorcode=%s\n", name
[1], type
, myErrorName(status
));
682 if(U_SUCCESS(status
)){
687 result
=udata_openChoice(testPath
, type
, name
[2], isAcceptable1
, NULL
, &status
);
688 if(U_FAILURE(status
)){
690 result
=udata_openChoice(testPath
, type
, name
[2], isAcceptable3
, NULL
, &status
);
691 if(U_FAILURE(status
)){
692 log_data_err("FAIL: udata_openChoice() failed path=%s name=%s, type=%s, \n errorcode=%s\n", testPath
, name
[2], type
, myErrorName(status
));
696 log_err("FAIL: udata_openChoice() unexpectedly passed. name=%s, type=%s, \n errorcode=%s\n", name
[2], type
, myErrorName(status
));
699 if(U_SUCCESS(status
)){
705 result
=udata_openChoice(fullTestDataPath
, type
, name
[3], isAcceptable1
, NULL
, &status
);
706 if(status
!= U_INVALID_FORMAT_ERROR
){
707 log_err("FAIL: udata_openChoice() did not fail as expected. name=%s, type=%s, \n errorcode=%s\n", name
[3], type
, myErrorName(status
));
710 status
=U_USELESS_COLLATOR_ERROR
;
711 result
=udata_openChoice(fullTestDataPath
, type
, name
[3], isAcceptable1
, NULL
, &status
);
712 if(status
!= U_USELESS_COLLATOR_ERROR
){
713 log_err("FAIL: udata_openChoice() did not fail as expected. name=%s, type=%s, \n errorcode=%s\n", name
[3], type
, myErrorName(status
));
717 static UBool U_CALLCONV
718 isAcceptable(void *context
,
719 const char *type
, const char *name
,
720 const UDataInfo
*pInfo
){
721 if( pInfo
->size
>=20 &&
722 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
723 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
724 pInfo
->dataFormat
[0]==0x54 && /* dataFormat="test" */
725 pInfo
->dataFormat
[1]==0x65 &&
726 pInfo
->dataFormat
[2]==0x73 &&
727 pInfo
->dataFormat
[3]==0x74 &&
728 pInfo
->formatVersion
[0]==1 &&
729 pInfo
->dataVersion
[0]==1 &&
730 *((int*)context
) == 2 ) {
731 log_verbose("The data from\"%s.%s\" IS acceptable using the verifing function isAcceptable()\n", name
, type
);
735 log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable()\n", name
, type
);
741 /* This test checks to see if the isAcceptable function is being called correctly. */
743 static void TestUDataOpenChoiceDemo2() {
745 UErrorCode status
=U_ZERO_ERROR
;
749 const char* name
="test";
750 const char* type
="icu";
751 const char* path
= loadTestData(&status
);
752 if(U_FAILURE(status
)) {
753 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
757 result
=udata_openChoice(path
, type
, name
, isAcceptable
, &p
, &status
);
758 if(U_FAILURE(status
)){
759 log_data_err("failed to load data at p=%s t=%s n=%s, isAcceptable", path
, type
, name
);
761 if(U_SUCCESS(status
) ) {
767 result
=udata_openChoice(path
, type
, name
, isAcceptable
, &p
, &status
);
769 if(U_FAILURE(status
) && status
==U_INVALID_FORMAT_ERROR
){
770 log_verbose("Loads the data but rejects it as expected %s\n", myErrorName(status
));
775 log_data_err("FAIL: failed to either load the data or to reject the loaded data. ERROR=%s\n", myErrorName(status
) );
779 if(U_FAILURE(status
)) {
780 log_data_err("FAIL: failed to load the data and accept it. ERROR=%s\n", myErrorName(status
) );
783 log_verbose("Loads the data and accepts it for p==2 as expected\n");
790 static void TestUDataGetInfo() {
793 /* UDataInfo cf. udata.h */
794 static UDataInfo dataInfo
={
795 30, /*sizeof(UDataInfo),*/
803 {0x54, 0x65, 0x73, 0x74}, /* dataFormat="Test" */
804 {9, 0, 0, 0}, /* formatVersion */
805 {4, 0, 0, 0} /* dataVersion */
807 UErrorCode status
=U_ZERO_ERROR
;
808 const char* name
="cnvalias";
809 const char* name2
="test";
810 const char* type
="icu";
812 const char* testPath
=loadTestData(&status
);
813 if(U_FAILURE(status
)) {
814 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
818 log_verbose("Testing udata_getInfo() for cnvalias.icu\n");
819 result
=udata_open(NULL
, "icu", name
, &status
);
820 if(U_FAILURE(status
)){
821 log_data_err("FAIL: udata_open() failed for path = NULL, name=%s, type=%s, \n errorcode=%s\n", name
, type
, myErrorName(status
));
824 udata_getInfo(result
, &dataInfo
);
825 if(dataInfo
.size
==20 && dataInfo
.size
!=30 &&
826 dataInfo
.isBigEndian
==U_IS_BIG_ENDIAN
&&
827 dataInfo
.charsetFamily
==U_CHARSET_FAMILY
&&
828 dataInfo
.dataFormat
[0]==0x43 && dataInfo
.dataFormat
[0]!=0x54 && /* dataFormat="CvAl" and not "Test". The values are set for cnvalias.dat*/
829 dataInfo
.dataFormat
[1]==0x76 && dataInfo
.dataFormat
[1]!=0x65 &&
830 dataInfo
.dataFormat
[2]==0x41 && dataInfo
.dataFormat
[2]!=0x73 &&
831 dataInfo
.dataFormat
[3]==0x6c && dataInfo
.dataFormat
[3]!=0x74 &&
832 dataInfo
.formatVersion
[0]!=9 && /*formatVersion is also set to the one for cnvalias*/
833 dataInfo
.dataVersion
[0]!=4 && /*dataVersion*/
834 dataInfo
.dataVersion
[1]!=0 ){
835 log_verbose("PASS: udata_getInfo() filled in the right values\n");
837 log_err("FAIL: udata_getInfo() filled in the wrong values\n");
842 log_verbose("Testing udata_getInfo() for test.icu\n");
843 result
=udata_open(testPath
, type
, name2
, &status
);
844 if(U_FAILURE(status
)) {
845 log_data_err("FAIL: udata_open() failed for path=%s name2=%s, type=%s, \n errorcode=%s\n", testPath
, name2
, type
, myErrorName(status
));
848 udata_getInfo(result
, &dataInfo
);
849 if(dataInfo
.size
==20 &&
850 dataInfo
.isBigEndian
==U_IS_BIG_ENDIAN
&&
851 dataInfo
.charsetFamily
==U_CHARSET_FAMILY
&&
852 dataInfo
.dataFormat
[0]==0x54 && /* dataFormat="Test". The values are set for test.dat*/
853 dataInfo
.dataFormat
[1]==0x65 &&
854 dataInfo
.dataFormat
[2]==0x73 &&
855 dataInfo
.dataFormat
[3]==0x74 &&
856 dataInfo
.formatVersion
[0]==1 && /*formatVersion is also set to the one for test*/
857 dataInfo
.dataVersion
[0]==1 && /*dataVersion*/
858 dataInfo
.dataVersion
[1]==0 )
860 log_verbose("PASS: udata_getInfo() filled in the right values\n");
862 log_err("FAIL: udata_getInfo() filled in the wrong values\n");
867 static void TestUDataGetMemory() {
870 const int32_t *table
=NULL
;
871 uint16_t* intValue
=0;
872 UErrorCode status
=U_ZERO_ERROR
;
873 const char* name
="cnvalias";
876 const char* name2
="test";
878 const char* testPath
= loadTestData(&status
);
879 if(U_FAILURE(status
)) {
880 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
885 log_verbose("Testing udata_getMemory() for \"cnvalias.icu\"\n");
886 result
=udata_openChoice(NULL
, type
, name
, isAcceptable1
, NULL
, &status
);
887 if(U_FAILURE(status
)){
888 log_data_err("FAIL: udata_openChoice() failed for name=%s, type=%s, \n errorcode=%s\n", name
, type
, myErrorName(status
));
891 table
=(const int32_t *)udata_getMemory(result
);
893 /* The alias table may list more converters than what's actually available now. [grhoten] */
894 if(ucnv_countAvailable() > table
[1]) /*???*/
895 log_err("FAIL: udata_getMemory() failed ucnv_countAvailable returned = %d, expected = %d\n", ucnv_countAvailable(), table
[1+2*(*table
)]);
900 log_verbose("Testing udata_getMemory for \"test.icu\"()\n");
901 result
=udata_openChoice(testPath
, type
, name2
, isAcceptable3
, NULL
, &status
);
902 if(U_FAILURE(status
)){
903 log_data_err("FAIL: udata_openChoice() failed for path=%s name=%s, type=%s, \n errorcode=%s\n", testPath
, name2
, type
, myErrorName(status
));
906 intValue
=(uint16_t *)udata_getMemory(result
);
907 /*printf("%d ..... %s", *(intValue), intValue+1));*/
908 if( *intValue
!= 2000 || strcmp((char*)(intValue
+1), "YEAR") != 0 )
909 log_err("FAIL: udata_getMemory() failed: intValue :- Expected:2000 Got:%d \n\tstringValue:- Expected:YEAR Got:%s\n", *intValue
, (intValue
+1));
915 static void TestErrorConditions(){
917 UDataMemory
*result
=NULL
;
918 UErrorCode status
=U_ZERO_ERROR
;
919 uint16_t* intValue
=0;
920 static UDataInfo dataInfo
={
921 30, /*sizeof(UDataInfo),*/
929 {0x54, 0x65, 0x73, 0x74}, /* dataFormat="Test" */
930 {9, 0, 0, 0}, /* formatVersion */
931 {4, 0, 0, 0} /* dataVersion */
934 const char* name
= "test";
935 const char* type
="icu";
937 const char *testPath
= loadTestData(&status
);
938 if(U_FAILURE(status
)) {
939 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
943 status
= U_ILLEGAL_ARGUMENT_ERROR
;
944 /*Try udata_open with status != U_ZERO_ERROR*/
945 log_verbose("Testing udata_open() with status != U_ZERO_ERROR\n");
946 result
=udata_open(testPath
, type
, name
, &status
);
948 log_data_err("FAIL: udata_open() is supposed to fail for path = %s, name=%s, type=%s, \n errorcode !=U_ZERO_ERROR\n", testPath
, name
, type
);
952 log_verbose("PASS: udata_open with errorCode != U_ZERO_ERROR failed as expected\n");
955 /*Try udata_open with data name=NULL*/
956 log_verbose("Testing udata_open() with data name=NULL\n");
958 result
=udata_open(testPath
, type
, NULL
, &status
);
959 if(U_FAILURE(status
)){
960 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
|| result
!= NULL
){
961 log_err("FAIL: udata_open() with name=NULL should return NULL and errocode U_ILLEGAL_ARGUMENT_ERROR, GOT: errorcode=%s\n", myErrorName(status
));
963 log_verbose("PASS: udata_open with name=NULL failed as expected and errorcode = %s as expected\n", myErrorName(status
));
966 log_err("FAIL: udata_open() with data name=NULL is supposed to fail for path = %s, name=NULL type=%s errorcode=U_ZERO_ERROR \n", testPath
, type
);
971 /*Try udata_openChoice with status != U_ZERO_ERROR*/
972 log_verbose("Testing udata_openChoice() with status != U_ZERO_ERROR\n");
973 status
=U_ILLEGAL_ARGUMENT_ERROR
;
974 result
=udata_openChoice(testPath
, type
, name
, isAcceptable3
, NULL
, &status
);
976 log_err("FAIL: udata_openChoice() is supposed to fail for path = %s, name=%s, type=%s, \n errorcode != U_ZERO_ERROR\n", testPath
, name
, type
);
979 log_verbose("PASS: udata_openChoice() with errorCode != U_ZERO_ERROR failed as expected\n");
982 /*Try udata_open with data name=NULL*/
983 log_verbose("Testing udata_openChoice() with data name=NULL\n");
985 result
=udata_openChoice(testPath
, type
, NULL
, isAcceptable3
, NULL
, &status
);
986 if(U_FAILURE(status
)){
987 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
|| result
!= NULL
){
988 log_err("FAIL: udata_openChoice() with name=NULL should return NULL and errocode U_ILLEGAL_ARGUMENT_ERROR, GOT: errorcode=%s\n", myErrorName(status
));
990 log_verbose("PASS: udata_openChoice with name=NULL failed as expected and errorcode = %s as expected\n", myErrorName(status
));
993 log_err("FAIL: udata_openChoice() with data name=NULL is supposed to fail for path = %s, name=NULL type=%s errorcode=U_ZERO_ERROR \n", testPath
, type
);
997 /*Try udata_getMemory with UDataMemory=NULL*/
998 log_verbose("Testing udata_getMemory with UDataMemory=NULL\n");
999 intValue
=(uint16_t*)udata_getMemory(NULL
);
1000 if(intValue
!= NULL
){
1001 log_err("FAIL: udata_getMemory with UDataMemory = NULL is supposed to fail\n");
1004 /*Try udata_getInfo with UDataMemory=NULL*/
1005 status
=U_ZERO_ERROR
;
1006 udata_getInfo(NULL
, &dataInfo
);
1007 if(dataInfo
.size
!= 0){
1008 log_err("FAIL : udata_getInfo with UDataMemory = NULL us supposed to fail\n");
1011 /*Try udata_openChoice with a non existing binary file*/
1012 log_verbose("Testing udata_openChoice() with a non existing binary file\n");
1013 result
=udata_openChoice(testPath
, "tst", "nonexist", isAcceptable3
, NULL
, &status
);
1014 if(status
==U_FILE_ACCESS_ERROR
){
1015 log_verbose("Opening udata_openChoice with non-existing file handled correctly.\n");
1016 status
=U_ZERO_ERROR
;
1018 log_err("calling udata_open with non-existing file not handled correctly\n. Expected: U_FILE_ACCESS_ERROR, Got: %s\n", myErrorName(status
));
1019 if(U_SUCCESS(status
)) {
1020 udata_close(result
);
1025 log_err("calling udata_open with non-existing file didn't return a null value\n");
1027 log_verbose("calling udat_open with non-existing file returned null as expected\n");
1031 /* Test whether apps and ICU can each have their own root.res */
1032 static void TestAppData()
1034 UResourceBundle
*icu
, *app
;
1035 UResourceBundle
*tmp
= NULL
;
1036 UResourceBundle
*tmp2
= NULL
;
1038 const UChar
*appString
;
1039 const UChar
*icuString
;
1043 UErrorCode status
= U_ZERO_ERROR
;
1044 char testMsgBuf
[256];
1046 const char* testPath
=loadTestData(&status
);
1047 if(U_FAILURE(status
)) {
1048 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
1052 icu
= ures_open(NULL
, "root", &status
);
1053 if(U_FAILURE(status
))
1055 log_data_err("%s:%d: Couldn't open root ICU bundle- %s", __FILE__
, __LINE__
, u_errorName(status
));
1058 /* log_info("Open icu root: %s size_%d\n", u_errorName(status), ures_getSize(icu)); */
1059 status
= U_ZERO_ERROR
;
1061 app
= ures_open(testPath
, "root", &status
);
1062 if(U_FAILURE(status
))
1064 log_data_err("%s:%d: Couldn't open app ICU bundle [%s]- %s", __FILE__
, __LINE__
, testPath
, u_errorName(status
));
1067 /* log_info("Open app: %s, size %d\n", u_errorName(status), ures_getSize(app)); */
1069 tmp
= ures_getByKey(icu
, "Version", tmp
, &status
);
1070 if(U_FAILURE(status
))
1072 log_err("%s:%d: Couldn't get Version string from ICU root bundle- %s", __FILE__
, __LINE__
, u_errorName(status
));
1076 icuString
= ures_getString(tmp
, &len
, &status
);
1077 if(U_FAILURE(status
))
1079 log_err("%s:%d: Couldn't get string from Version string from ICU root bundle- %s", __FILE__
, __LINE__
, u_errorName(status
));
1082 /* log_info("icuString=%p - %s\n", icuString, austrdup(icuString)); */
1085 tmp2
= ures_getByKey(app
, "Version", tmp2
, &status
);
1086 if(U_FAILURE(status
))
1088 log_err("%s:%d: Couldn't get Version string from App root bundle- %s", __FILE__
, __LINE__
, u_errorName(status
));
1092 appString
= ures_getString(tmp2
, &len
, &status
);
1093 if(U_FAILURE(status
))
1095 log_err("%s:%d: Couldn't get string from Version string from App root bundle- %s", __FILE__
, __LINE__
, u_errorName(status
));
1099 /* log_info("appString=%p - %s\n", appString, austrdup(appString)); */
1102 if(!u_strcmp(icuString
, appString
))
1104 log_err("%s:%d: Error! Expected ICU and App root version strings to be DIFFERENT but they are both %s and %s\n", __FILE__
, __LINE__
, austrdup(icuString
),
1105 austrdup(appString
));
1109 log_verbose("%s:%d: appstr=%s, icustr=%s\n", __FILE__
,
1110 __LINE__
, u_austrcpy(testMsgBuf
, appString
), u_austrcpy(testMsgBuf
, icuString
));
1120 static void TestICUDataName()
1122 UVersionInfo icuVersion
;
1123 char expectDataName
[20];
1124 unsigned int expectLen
= 8;
1126 char typeChar
= '?';
1128 /* Print out the version # we have .. */
1129 log_verbose("utypes.h says U_ICUDATA_NAME = %s\n", U_ICUDATA_NAME
);
1131 /* Build up the version # we expect to get */
1132 u_getVersion(icuVersion
);
1134 switch(U_CHARSET_FAMILY
)
1136 case U_ASCII_FAMILY
:
1137 switch((int)U_IS_BIG_ENDIAN
)
1146 log_err("Expected 1 or 0 for U_IS_BIG_ENDIAN, got %d!\n", (int)U_IS_BIG_ENDIAN
);
1150 case U_EBCDIC_FAMILY
:
1155 /* Only major number is needed. */
1156 sprintf(expectDataName
, "%s%d%c",
1161 log_verbose("Expected: %s\n", expectDataName
);
1162 if(uprv_strlen(expectDataName
) != expectLen
)
1164 log_err("*Expected* length is wrong (test err?), should be %d is %d\n",
1165 expectLen
, uprv_strlen(expectDataName
));
1168 if(uprv_strlen(U_ICUDATA_NAME
) != expectLen
)
1170 log_err("U_ICUDATA_NAME length should be %d is %d\n",
1171 expectLen
, uprv_strlen(U_ICUDATA_NAME
));
1174 if(uprv_strcmp(U_ICUDATA_NAME
, expectDataName
))
1176 log_err("U_ICUDATA_NAME should be %s but is %s\n",
1177 expectDataName
, U_ICUDATA_NAME
);
1180 /* ICUDATA_NAME comes from the build system on *nix */
1182 if(uprv_strcmp(U_ICUDATA_NAME
, ICUDATA_NAME
))
1184 log_err("ICUDATA_NAME and U_ICUDATA_NAME don't match: "
1185 "ICUDATA_NAME=%s, U_ICUDATA_NAME=%s. Check configure.in, icudefs.mk.in, utypes.h...\n", ICUDATA_NAME
, U_ICUDATA_NAME
);
1189 log_verbose("ICUDATA_NAME=%s (from icudefs.mk), U_ICUDATA_NAME=%s (from utypes.h)\n", ICUDATA_NAME
, U_ICUDATA_NAME
);
1195 /* test data swapping ------------------------------------------------------- */
1197 #if U_PLATFORM == U_PF_OS400
1198 /* See comments in genccode.c on when this special implementation can be removed. */
1199 static const struct {
1202 } gOffsetTOCAppDataItem1
={ 0.0, /* alignment bytes */
1203 "\x00\x14" /* sizeof(UDataInfo) *//* MappedData { */
1206 "\x00\x14" /* sizeof(UDataInfo) *//* UDataInfo { */
1208 "\1" /* U_IS_BIG_ENDIAN */
1209 "\1" /* U_CHARSET_FAMILY */
1210 "\2" /* U_SIZEOF_WHAR_T */
1217 static const struct {
1222 } gOffsetTOCAppDataItem1
={
1223 0.0, /* alignment bytes */
1224 { sizeof(UDataInfo
), 0xda, 0x27 }, /* MappedData */
1234 {0x31, 0x31, 0x31, 0x31}, /* dataFormat="1111" */
1235 {0, 0, 0, 0}, /* formatVersion */
1236 {0, 0, 0, 0}} /* dataVersion */
1240 static const UChar gOffsetTOCGarbage
[] = { /* "I have been very naughty!" */
1241 0x49, 0x20, 0x68, 0x61, 0x76, 0x65, 0x20, 0x62, 0x65, 0x65, 0x6E,
1242 0x20, 0x76, 0x65, 0x72, 0x79, 0x20, 0x6E, 0x61, 0x75, 0x67, 0x68, 0x74, 0x79, 0x21
1245 /* Original source: icu/source/tools/genccode */
1246 static const struct {
1247 uint16_t headerSize
;
1248 uint8_t magic1
, magic2
;
1251 uint32_t count
, reserved
;
1253 const char *const name
;
1254 const void *const data
;
1256 } gOffsetTOCAppData_dat
= {
1257 32, /* headerSize */
1258 0xda, /* magic1, (see struct MappedData in udata.c) */
1261 sizeof(UDataInfo
), /* size */
1267 { /* data format identifier */
1268 0x54, 0x6f, 0x43, 0x50}, /* "ToCP" */
1269 {1, 0, 0, 0}, /* format version major, minor, milli, micro */
1270 {0, 0, 0, 0} /* dataVersion */
1272 {0,0,0,0,0,0,0,0}, /* Padding[8] */
1275 { /* TOC structure */
1276 { "OffsetTOCAppData/a/b", &gOffsetTOCAppDataItem1
},
1277 { "OffsetTOCAppData/gOffsetTOCAppDataItem1", &gOffsetTOCAppDataItem1
},
1278 { "OffsetTOCAppData/gOffsetTOCGarbage", &gOffsetTOCGarbage
}
1282 /* Unfortunately, dictionaries are in a C++ header */
1283 U_CAPI
int32_t U_EXPORT2
1284 udict_swap(const UDataSwapper
*ds
, const void *inData
, int32_t length
, void *outData
, UErrorCode
*pErrorCode
);
1286 /* test cases for maximum data swapping code coverage */
1287 static const struct {
1288 const char *name
, *type
;
1289 UDataSwapFn
*swapFn
;
1291 /* resource bundles */
1293 /* resource bundle with many data types */
1294 {"*testtypes", "res", ures_swap
},
1295 /* resource bundle with collation data */
1296 {"ja", "res", ures_swap
},
1297 /* resource bundle with options-only collation data */
1298 {"ru", "res", ures_swap
},
1299 {"el", "res", ures_swap
},
1301 {"root", "res", ures_swap
},
1302 /* Test a 32-bit key table. This is large. */
1303 {"*testtable32", "res", ures_swap
},
1305 /* ICU 4.2 resource bundle - data format 1.2 (little-endian ASCII) */
1306 {"*old_l_testtypes", "res", ures_swap
},
1307 /* same for big-endian EBCDIC */
1308 {"*old_e_testtypes", "res", ures_swap
},
1310 #if !UCONFIG_NO_COLLATION
1311 /* standalone collation data files */
1312 {"ucadata", "icu", ucol_swap
},
1314 /* Starting with ICU 53, the "inverse UCA" data is integrated into ucadata.icu. */
1315 {"invuca", "icu", ucol_swapInverseUCA
},
1319 #if !UCONFIG_NO_LEGACY_CONVERSION
1320 /* conversion table files */
1322 /* SBCS conversion table file without extension */
1323 {"ibm-913_P100-2000", "cnv", ucnv_swap
},
1324 /* EBCDIC_STATEFUL conversion table file with extension */
1325 {"ibm-1390_P110-2003", "cnv", ucnv_swap
},
1326 /* DBCS extension-only conversion table file */
1327 {"ibm-16684_P110-2003", "cnv", ucnv_swap
},
1328 /* EUC-TW (3-byte) conversion table file without extension */
1329 {"ibm-964_P110-1999", "cnv", ucnv_swap
},
1330 /* GB 18030 (4-byte) conversion table file without extension */
1331 {"gb18030", "cnv", ucnv_swap
},
1332 /* MBCS conversion table file with extension */
1333 {"*test4x", "cnv", ucnv_swap
},
1335 * MBCS conversion table file without extension,
1336 * to test swapping and preflighting of UTF-8-friendly mbcsIndex[].
1338 {"jisx-212", "cnv", ucnv_swap
},
1341 #if !UCONFIG_NO_CONVERSION
1343 {"cnvalias", "icu", ucnv_swapAliases
},
1346 #if !UCONFIG_NO_IDNA
1347 {"rfc3491", "spp", usprep_swap
},
1350 #if !UCONFIG_NO_BREAK_ITERATION
1351 {"char", "brk", ubrk_swap
},
1352 {"thaidict", "dict",udict_swap
},
1357 * Starting with ICU 4.8, the Unicode property (value) aliases data
1358 * is hardcoded in the ICU4C common library.
1359 * The swapper was moved to the toolutil library for swapping for ICU4J.
1361 /* Unicode properties */
1362 {"pnames", "icu", upname_swap
},
1367 * Starting with ICU4C 3.4, the core Unicode properties files
1368 * (uprops.icu, ucase.icu, ubidi.icu, unorm.icu)
1369 * are hardcoded in the common DLL and therefore not included
1370 * in the data package any more.
1371 * Their swapping code is moved from the common DLL to the icuswap tool so that
1372 * we need not jump through hoops (like adding snapshots of these files
1373 * to testdata) for code coverage in tests.
1374 * See Jitterbug 4497.
1376 * ICU4C 4.4 adds normalization data files again, e.g., nfkc.nrm.
1378 {"uprops", "icu", uprops_swap
},
1379 {"ucase", "icu", ucase_swap
},
1380 {"ubidi", "icu", ubidi_swap
},
1382 #if !UCONFIG_NO_NORMALIZATION && !UCONFIG_ONLY_COLLATION
1383 {"nfkc", "nrm", unorm2_swap
},
1384 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
1385 {"confusables", "cfu", uspoof_swap
}, /* spoof data missing without regex */
1389 {"unames", "icu", uchar_swapNames
}
1390 /* the last item should not be #if'ed so that it can reliably omit the last comma */
1393 /* Large enough for the largest swappable data item. */
1394 #define SWAP_BUFFER_SIZE 1800000
1396 static void U_CALLCONV
1397 printError(void *context
, const char *fmt
, va_list args
) {
1398 vlog_info("[swap] ", fmt
, args
);
1399 log_err("\n"); /* Register error */
1403 TestSwapCase(UDataMemory
*pData
, const char *name
,
1404 UDataSwapFn
*swapFn
,
1405 uint8_t *buffer
, uint8_t *buffer2
) {
1407 const void *inData
, *inHeader
;
1408 int32_t length
, dataLength
, length2
, headerLength
;
1410 UErrorCode errorCode
;
1411 UErrorCode badStatus
;
1413 UBool inEndian
, oppositeEndian
;
1414 uint8_t inCharset
, oppositeCharset
;
1416 /* First we check that swapFn handles failures as expected. */
1417 errorCode
= U_UNSUPPORTED_ERROR
;
1418 length
= swapFn(NULL
, NULL
, 0, buffer
, &errorCode
);
1419 if (length
!= 0 || errorCode
!= U_UNSUPPORTED_ERROR
) {
1420 log_err("%s() did not fail as expected - %s\n", name
, u_errorName(errorCode
));
1422 errorCode
= U_ZERO_ERROR
;
1423 length
= swapFn(NULL
, NULL
, 0, buffer
, &errorCode
);
1424 if (length
!= 0 || errorCode
!= U_ILLEGAL_ARGUMENT_ERROR
) {
1425 log_err("%s() did not fail as expected with bad arguments - %s\n", name
, u_errorName(errorCode
));
1429 /* Continue with the rest of the tests. */
1430 errorCode
= U_ZERO_ERROR
;
1431 inData
=udata_getMemory(pData
);
1434 * get the data length if possible, to verify that swapping and preflighting
1435 * handles the entire data
1437 dataLength
=udata_getLength(pData
);
1440 * get the header and its length
1441 * all of the swap implementation functions require the header to be included
1443 inHeader
=udata_getRawMemory(pData
);
1444 headerLength
=(int32_t)((const char *)inData
-(const char *)inHeader
);
1446 /* first swap to opposite endianness but same charset family */
1447 errorCode
=U_ZERO_ERROR
;
1448 ds
=udata_openSwapperForInputData(inHeader
, headerLength
,
1449 !U_IS_BIG_ENDIAN
, U_CHARSET_FAMILY
, &errorCode
);
1450 if(U_FAILURE(errorCode
)) {
1451 log_err("udata_openSwapperForInputData(%s->!isBig+same charset) failed - %s\n",
1452 name
, u_errorName(errorCode
));
1456 inEndian
=ds
->inIsBigEndian
;
1457 inCharset
=ds
->inCharset
;
1459 oppositeEndian
=!inEndian
;
1460 oppositeCharset
= inCharset
==U_ASCII_FAMILY
? U_EBCDIC_FAMILY
: U_ASCII_FAMILY
;
1462 /* make this test work with data files that are built for a different platform */
1463 if(inEndian
!=U_IS_BIG_ENDIAN
|| inCharset
!=U_CHARSET_FAMILY
) {
1464 udata_closeSwapper(ds
);
1465 ds
=udata_openSwapper(inEndian
, inCharset
, oppositeEndian
, inCharset
, &errorCode
);
1466 if(U_FAILURE(errorCode
)) {
1467 log_err("udata_openSwapper(%s->!isBig+same charset) failed - %s\n",
1468 name
, u_errorName(errorCode
));
1474 Check error checking of swappable data not specific to this swapper.
1475 This should always fail.
1477 badStatus
= U_ZERO_ERROR
;
1478 length
=swapFn(ds
, &gOffsetTOCAppData_dat
, -1, NULL
, &badStatus
);
1479 if(badStatus
!= U_UNSUPPORTED_ERROR
) {
1480 log_err("swapFn(%s->!isBig+same charset) unexpectedly succeeded on bad data - %s\n",
1481 name
, u_errorName(errorCode
));
1482 udata_closeSwapper(ds
);
1486 /* Now allow errors to be printed */
1487 ds
->printError
=printError
;
1489 /* preflight the length */
1490 length
=swapFn(ds
, inHeader
, -1, NULL
, &errorCode
);
1491 if(U_FAILURE(errorCode
)) {
1492 log_err("swapFn(preflight %s->!isBig+same charset) failed - %s\n",
1493 name
, u_errorName(errorCode
));
1494 udata_closeSwapper(ds
);
1498 /* compare the preflighted length against the data length */
1499 if(dataLength
>=0 && (length
+15)<(headerLength
+dataLength
)) {
1500 log_err("swapFn(preflight %s->!isBig+same charset) length too small: %d < data length %d\n",
1501 name
, length
, (headerLength
+dataLength
));
1502 udata_closeSwapper(ds
);
1506 /* swap, not in-place */
1507 length2
=swapFn(ds
, inHeader
, length
, buffer
, &errorCode
);
1508 udata_closeSwapper(ds
);
1509 if(U_FAILURE(errorCode
)) {
1510 log_err("swapFn(%s->!isBig+same charset) failed - %s\n",
1511 name
, u_errorName(errorCode
));
1515 /* compare the swap length against the preflighted length */
1516 if(length2
!=length
) {
1517 log_err("swapFn(%s->!isBig+same charset) length differs from preflighting: %d != preflighted %d\n",
1518 name
, length2
, length
);
1522 /* next swap to opposite charset family */
1523 ds
=udata_openSwapper(oppositeEndian
, inCharset
,
1524 oppositeEndian
, oppositeCharset
,
1526 if(U_FAILURE(errorCode
)) {
1527 log_err("udata_openSwapper(%s->!isBig+other charset) failed - %s\n",
1528 name
, u_errorName(errorCode
));
1531 ds
->printError
=printError
;
1534 length2
=swapFn(ds
, buffer
, length
, buffer
, &errorCode
);
1535 udata_closeSwapper(ds
);
1536 if(U_FAILURE(errorCode
)) {
1537 log_err("swapFn(%s->!isBig+other charset) failed - %s\n",
1538 name
, u_errorName(errorCode
));
1542 /* compare the swap length against the original length */
1543 if(length2
!=length
) {
1544 log_err("swapFn(%s->!isBig+other charset) length differs from original: %d != original %d\n",
1545 name
, length2
, length
);
1549 /* finally swap to original platform values */
1550 ds
=udata_openSwapper(oppositeEndian
, oppositeCharset
,
1551 inEndian
, inCharset
,
1553 if(U_FAILURE(errorCode
)) {
1554 log_err("udata_openSwapper(%s->back to original) failed - %s\n",
1555 name
, u_errorName(errorCode
));
1558 ds
->printError
=printError
;
1560 /* swap, not in-place */
1561 length2
=swapFn(ds
, buffer
, length
, buffer2
, &errorCode
);
1562 udata_closeSwapper(ds
);
1563 if(U_FAILURE(errorCode
)) {
1564 log_err("swapFn(%s->back to original) failed - %s\n",
1565 name
, u_errorName(errorCode
));
1569 /* compare the swap length against the original length */
1570 if(length2
!=length
) {
1571 log_err("swapFn(%s->back to original) length differs from original: %d != original %d\n",
1572 name
, length2
, length
);
1576 /* compare the final contents with the original */
1577 if(0!=uprv_memcmp(inHeader
, buffer2
, length
)) {
1578 const uint8_t *original
;
1582 log_err("swapFn(%s->back to original) contents differs from original\n",
1585 /* find the first difference */
1586 original
=(const uint8_t *)inHeader
;
1587 for(i
=0; i
<length
&& original
[i
]==buffer2
[i
]; ++i
) {}
1589 /* find the next byte that is the same */
1590 for(j
=i
+1; j
<length
&& original
[j
]!=buffer2
[j
]; ++j
) {}
1591 log_info(" difference at index %d=0x%x, until index %d=0x%x\n", i
, i
, j
, j
);
1593 /* round down to the last 4-boundary for better result output */
1595 log_info("showing bytes from index %d=0x%x (length %d=0x%x):\n", i
, i
, length
, length
);
1597 /* print 8 bytes but limit to the buffer contents */
1598 length2
=i
+sizeof(diff
);
1599 if(length2
>length
) {
1603 /* print the original bytes */
1604 uprv_memset(diff
, 0, sizeof(diff
));
1605 for(j
=i
; j
<length2
; ++j
) {
1606 diff
[j
-i
]=original
[j
];
1608 log_info(" original: %02x %02x %02x %02x %02x %02x %02x %02x\n",
1609 diff
[0], diff
[1], diff
[2], diff
[3], diff
[4], diff
[5], diff
[6], diff
[7]);
1611 /* print the swapped bytes */
1612 uprv_memset(diff
, 0, sizeof(diff
));
1613 for(j
=i
; j
<length2
; ++j
) {
1614 diff
[j
-i
]=buffer2
[j
];
1616 log_info(" swapped: %02x %02x %02x %02x %02x %02x %02x %02x\n",
1617 diff
[0], diff
[1], diff
[2], diff
[3], diff
[4], diff
[5], diff
[6], diff
[7]);
1621 static void U_CALLCONV
1622 printErrorToString(void *context
, const char *fmt
, va_list args
) {
1623 vsprintf((char *)context
, fmt
, args
);
1626 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
1633 const char *pkg
, *nm
, *testPath
;
1634 UErrorCode errorCode
= U_ZERO_ERROR
;
1637 buffer
=(uint8_t *)malloc(2*SWAP_BUFFER_SIZE
);
1639 log_err("unable to allocate %d bytes\n", 2*SWAP_BUFFER_SIZE
);
1643 testPath
=loadTestData(&errorCode
);
1644 if(U_FAILURE(errorCode
)) {
1645 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(errorCode
));
1648 /* Test that printError works as expected. */
1649 errorCode
=U_USELESS_COLLATOR_ERROR
;
1650 ds
=udata_openSwapper(U_IS_BIG_ENDIAN
, U_ASCII_FAMILY
,
1651 !U_IS_BIG_ENDIAN
, U_ASCII_FAMILY
,
1653 if (ds
!= NULL
|| errorCode
!= U_USELESS_COLLATOR_ERROR
) {
1654 log_err("udata_openSwapper should have returned NULL with bad argument\n", name
);
1656 errorCode
=U_ZERO_ERROR
;
1657 ds
=udata_openSwapper(U_IS_BIG_ENDIAN
, U_ASCII_FAMILY
,
1658 !U_IS_BIG_ENDIAN
, U_ASCII_FAMILY
,
1660 ds
->printError
=printErrorToString
;
1661 ds
->printErrorContext
=name
;
1662 udata_printError(ds
, "This %s a %s", "is", "test");
1663 udata_closeSwapper(ds
);
1664 if (strcmp(name
, "This is a test") != 0) {
1665 log_err("udata_printError can't properly print error messages. Got = %s\n", name
);
1667 errorCode
= U_USELESS_COLLATOR_ERROR
;
1668 ds
=udata_openSwapperForInputData(NULL
, 0,
1669 !U_IS_BIG_ENDIAN
, U_ASCII_FAMILY
,
1671 if (ds
!= NULL
|| errorCode
!= U_USELESS_COLLATOR_ERROR
) {
1672 log_err("udata_openSwapperForInputData should have returned NULL with bad argument\n", name
);
1674 errorCode
=U_ZERO_ERROR
;
1675 ds
=udata_openSwapperForInputData(NULL
, 0,
1676 !U_IS_BIG_ENDIAN
, U_ASCII_FAMILY
,
1678 if (ds
!= NULL
|| errorCode
!= U_ILLEGAL_ARGUMENT_ERROR
) {
1679 log_err("udata_openSwapperForInputData should have returned NULL with bad argument\n", name
);
1681 errorCode
=U_ZERO_ERROR
;
1682 memset(buffer
, 0, sizeof(2*SWAP_BUFFER_SIZE
));
1683 ds
=udata_openSwapperForInputData(buffer
, 2*SWAP_BUFFER_SIZE
,
1684 !U_IS_BIG_ENDIAN
, U_ASCII_FAMILY
,
1686 if (ds
!= NULL
|| errorCode
!= U_UNSUPPORTED_ERROR
) {
1687 log_err("udata_openSwapperForInputData should have returned NULL with bad argument\n", name
);
1689 errorCode
=U_ZERO_ERROR
;
1691 /* Test argument checking. ucol_swap is normally tested via ures_swap, and isn't normally called directly. */
1692 #if !UCONFIG_NO_COLLATION
1693 ucol_swap(NULL
, NULL
, -1, NULL
, &errorCode
);
1694 if (errorCode
!= U_ILLEGAL_ARGUMENT_ERROR
) {
1695 log_err("ucol_swap did not fail as expected\n", name
);
1697 errorCode
=U_ZERO_ERROR
;
1700 for(i
=0; i
<UPRV_LENGTHOF(swapCases
); ++i
) {
1701 /* build the name for logging */
1702 errorCode
=U_ZERO_ERROR
;
1703 if(swapCases
[i
].name
[0]=='*') {
1705 nm
=swapCases
[i
].name
+1;
1706 uprv_strcpy(name
, "testdata");
1707 } else if (uprv_strcmp(swapCases
[i
].type
, "brk")==0
1708 || uprv_strcmp(swapCases
[i
].type
, "dict")==0) {
1709 pkg
=U_ICUDATA_BRKITR
;
1710 nm
=swapCases
[i
].name
;
1711 uprv_strcpy(name
, U_ICUDATA_BRKITR
);
1712 #if !UCONFIG_NO_COLLATION
1713 } else if (uprv_strcmp(swapCases
[i
].name
, "ucadata")==0
1714 || uprv_strcmp(swapCases
[i
].name
, "invuca")==0) {
1716 nm
=swapCases
[i
].name
;
1717 uprv_strcpy(name
, U_ICUDATA_COLL
);
1718 #endif /* !UCONFIG_NO_COLLATION */
1721 nm
=swapCases
[i
].name
;
1722 uprv_strcpy(name
, "NULL");
1724 uprv_strcat(name
, "/");
1725 uprv_strcat(name
, nm
);
1726 uprv_strcat(name
, ".");
1727 uprv_strcat(name
, swapCases
[i
].type
);
1729 pData
=udata_open(pkg
, swapCases
[i
].type
, nm
, &errorCode
);
1731 if(U_SUCCESS(errorCode
)) {
1732 TestSwapCase(pData
, name
, swapCases
[i
].swapFn
, buffer
, buffer
+SWAP_BUFFER_SIZE
);
1735 log_data_err("udata_open(%s) failed - %s\n",
1736 name
, u_errorName(errorCode
));
1744 static void PointerTableOfContents() {
1745 UDataMemory
*dataItem
;
1746 UErrorCode status
=U_ZERO_ERROR
;
1749 * Got testdata.dat into memory, now we try setAppData using the memory image.
1752 status
=U_ZERO_ERROR
;
1753 udata_setAppData("OffsetTOCAppData", &gOffsetTOCAppData_dat
, &status
);
1754 if (status
!= U_ZERO_ERROR
) {
1755 log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData1\", fileBuf, status) \n"
1756 " returned status of %s\n", u_errorName(status
));
1760 dataItem
= udata_open("OffsetTOCAppData", "", "gOffsetTOCAppDataItem1", &status
);
1761 if (U_FAILURE(status
)) {
1762 log_err("FAIL: gOffsetTOCAppDataItem1 could not be opened. status = %s\n", u_errorName(status
));
1764 if (udata_getMemory(dataItem
) != NULL
) {
1765 log_verbose("FAIL: udata_getMemory(dataItem) passed\n");
1768 log_err("FAIL: udata_getMemory returned NULL\n", u_errorName(status
));
1770 udata_close(dataItem
);
1772 dataItem
= udata_open("OffsetTOCAppData-a", "", "b", &status
);
1773 if (U_FAILURE(status
)) {
1774 log_err("FAIL: gOffsetTOCAppDataItem1 in tree \"a\" could not be opened. status = %s\n", u_errorName(status
));
1776 if (udata_getMemory(dataItem
) != NULL
) {
1777 log_verbose("FAIL: udata_getMemory(dataItem) in tree \"a\" passed\n");
1780 log_err("FAIL: udata_getMemory returned NULL\n", u_errorName(status
));
1782 udata_close(dataItem
);
1784 dataItem
= udata_open("OffsetTOCAppData", "", "gOffsetTOCGarbage", &status
);
1785 if (U_SUCCESS(status
)) {
1786 log_err("FAIL: gOffsetTOCGarbage should not be opened. status = %s\n", u_errorName(status
));
1788 dataItem
= udata_open("OffsetTOCAppData", "", "gOffsetTOCNonExistent", &status
);
1789 if (U_SUCCESS(status
)) {
1790 log_err("FAIL: gOffsetTOCNonExistent should not be found. status = %s\n", u_errorName(status
));
1795 static void SetBadCommonData(void) {
1796 /* It's difficult to test that udata_setCommonData really works within the test framework.
1797 So we just test that foolish people can't do bad things. */
1799 char badBuffer
[sizeof(gOffsetTOCAppData_dat
)];
1801 memset(badBuffer
, 0, sizeof(badBuffer
));
1802 strcpy(badBuffer
, "Hello! I'm not good data.");
1804 /* Check that we don't do anything */
1805 status
= U_FILE_ACCESS_ERROR
;
1806 udata_setCommonData(&gOffsetTOCAppData_dat
, &status
);
1807 if (status
!= U_FILE_ACCESS_ERROR
) {
1808 log_err("FAIL: udata_setCommonData changed the failure code.\n");
1810 /* Check that we fail correctly */
1811 status
= U_ZERO_ERROR
;
1812 udata_setCommonData(NULL
, &status
);
1813 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
1814 log_err("FAIL: udata_setCommonData did not fail with bad arguments.\n");
1817 /* Check that we verify that the data isn't bad */
1818 status
= U_ZERO_ERROR
;
1819 udata_setAppData("invalid path", badBuffer
, &status
);
1820 if (status
!= U_INVALID_FORMAT_ERROR
) {
1821 log_err("FAIL: udata_setAppData doesn't verify data validity.\n");
1825 // Check the override loading of time zone .res files from a specified path
1827 // Hand testing notes:
1828 // 1. Run this test with the environment variable set. The following should induce faiures:
1829 // ICU_TIMEZONE_FILES_DIR=../testdata/out/build LD_LIBRARY_PATH=../../lib:../../stubdata:../../tools/ctestfw:$LD_LIBRARY_PATH ./cintltst /udatatst/TestTZDataDir
1830 // 2. Build ICU with with U_TIMEZONE_FILES_DIR defined. This should also induce failures.
1831 // CPPFLAGS=-DU_TIMEZONE_FILES_DIR\=`pwd`/test/testdata/out/testdata ./runConfigureICU Linux
1834 static void TestTZDataDir(void) {
1835 #if !UCONFIG_NO_FORMATTING
1836 UErrorCode status
= U_ZERO_ERROR
;
1837 const char *tzDataVersion
;
1838 const char *testDataPath
;
1840 // Verify that default ICU time zone data version is something newer than 2014a.
1841 tzDataVersion
= ucal_getTZDataVersion(&status
);
1842 // printf("tz data version is %s\n", tzDataVersion);
1843 if (U_FAILURE(status
)) {
1844 log_data_err("Failed call to ucal_getTZDataVersion - %s\n", u_errorName(status
));
1846 } else if (strcmp("2014a", tzDataVersion
) == 0) {
1847 log_err("File %s:%d - expected something newer than time zone data 2014a.\n", __FILE__
, __LINE__
, tzDataVersion
);
1850 testDataPath
= loadTestData(&status
);
1851 // The path produced by loadTestData() will look something like
1852 // whatever/.../testdata/out/testdata
1853 // The test data puts an old (2014a) version of the time zone data there.
1855 // Switch ICU to the testdata version of zoneinfo64.res, which is verison 2014a.
1857 u_setTimeZoneFilesDirectory(testDataPath
, &status
);
1858 tzDataVersion
= ucal_getTZDataVersion(&status
);
1859 if (strcmp("2014a", tzDataVersion
) != 0) {
1860 log_err("File %s:%d - expected \"2014a\"; actual \"%s\"\n", __FILE__
, __LINE__
, tzDataVersion
);
1863 ctest_resetICU(); // Return ICU to using its standard tz data.
1864 tzDataVersion
= ucal_getTZDataVersion(&status
);
1865 // printf("tz data version is %s\n", tzDataVersion);
1866 if (strcmp("2014a", tzDataVersion
) == 0) {
1867 log_err("File %s:%d - expected something newer than time zone data 2014a.\n", __FILE__
, __LINE__
, tzDataVersion
);