1 /********************************************************************
3 * Copyright (c) 1998-2004, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
9 * Modification History:
11 * Date Name Description
12 * 02/22/2000 Madhu Creation
13 *******************************************************************************
16 #include "unicode/utypes.h"
17 #include "unicode/putil.h"
18 #include "unicode/udata.h"
19 #include "unicode/uchar.h"
20 #include "unicode/ucnv.h"
21 #include "unicode/ures.h"
22 #include "unicode/ustring.h"
29 #include <sys/types.h>
41 /* includes for TestSwapData() */
44 /* swapping implementations in common */
56 /* other definitions and prototypes */
58 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
60 static void TestUDataOpen(void);
61 static void TestUDataOpenChoiceDemo1(void);
62 static void TestUDataOpenChoiceDemo2(void);
63 static void TestUDataGetInfo(void);
64 static void TestUDataGetMemory(void);
65 static void TestUDataSetAppData(void);
66 static void TestErrorConditions(void);
67 static void TestAppData(void);
68 static void TestICUDataName(void);
69 static void TestSwapData(void);
71 void addUDataTest(TestNode
** root
);
74 addUDataTest(TestNode
** root
)
76 addTest(root
, &TestUDataOpen
, "udatatst/TestUDataOpen" );
77 addTest(root
, &TestUDataOpenChoiceDemo1
, "udatatst/TestUDataOpenChoiceDemo1");
78 addTest(root
, &TestUDataOpenChoiceDemo2
, "udatatst/TestUDataOpenChoiceDemo2");
79 addTest(root
, &TestUDataGetInfo
, "udatatst/TestUDataGetInfo" );
80 addTest(root
, &TestUDataGetMemory
, "udatatst/TestUDataGetMemory" );
81 addTest(root
, &TestUDataSetAppData
, "udatatst/TestUDataSetAppData" );
82 addTest(root
, &TestErrorConditions
, "udatatst/TestErrorConditions");
83 addTest(root
, &TestAppData
, "udatatst/TestAppData" );
84 addTest(root
, &TestICUDataName
, "udatatst/TestICUDataName" );
85 addTest(root
, &TestSwapData
, "udatatst/TestSwapData" );
89 static void lots_of_mallocs()
101 static void TestUDataOpen(){
103 UErrorCode status
=U_ZERO_ERROR
;
104 const char* memMap
[][2]={
109 {"ibm-37_P100-1995", "cnv"}
111 const char* name
= "test";
112 const char* type
= "icu";
113 const char dirSepString
[] = {U_FILE_SEP_CHAR
, 0};
114 const char pathSepString
[] = {U_PATH_SEP_CHAR
, 0};
117 char* path
=(char*)malloc(sizeof(char) * (strlen(ctest_dataOutDir())
118 + strlen(U_ICUDATA_NAME
)
119 + strlen("/build")+1 ) );
121 char *icuDataFilePath
= 0;
122 struct stat stat_buf
;
124 const char* testPath
=loadTestData(&status
);
126 /* lots_of_mallocs(); */
128 strcat(strcpy(path
, ctest_dataOutDir()), U_ICUDATA_NAME
);
130 log_verbose("Testing udata_open()\n");
131 result
=udata_open(testPath
, type
, name
, &status
);
132 if(U_FAILURE(status
)){
133 log_err("FAIL: udata_open() failed for path = %s, name=%s, type=%s, \n errorcode=%s\n", testPath
, name
, type
, myErrorName(status
));
135 log_verbose("PASS: udata_open worked\n");
139 /* If the ICU system common data file is present in this confiugration,
140 * verify that udata_open can explicitly fetch items from it.
141 * If packaging mode == dll, the file may not exist. So, if the file is
142 * missing, skip this test without error.
144 icuDataFilePath
= (char *)malloc(strlen(path
) + 10);
145 strcpy(icuDataFilePath
, path
);
146 strcat(icuDataFilePath
, ".dat");
147 /* lots_of_mallocs(); */
148 if (stat(icuDataFilePath
, &stat_buf
) == 0)
151 log_verbose("Testing udata_open() on %s\n", icuDataFilePath
);
152 for(i
=0; i
<sizeof(memMap
)/sizeof(memMap
[0]); i
++){
153 /* lots_of_mallocs(); */
155 result
=udata_open(path
, memMap
[i
][1], memMap
[i
][0], &status
);
156 if(U_FAILURE(status
)) {
157 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
));
159 log_verbose("PASS: udata_open worked for path = %s, name=%s, type=%s\n", path
, memMap
[i
][0], memMap
[i
][1]);
166 /* lots_of_mallocs(); */
167 log_verbose("Skipping tests of udata_open() on %s. File not present in this configuration.\n",
170 free(icuDataFilePath
);
171 icuDataFilePath
= NULL
;
172 /* lots_of_mallocs(); */
174 /* If the ICU individual files used to build the ICU system common data are
175 * present in this configuration,
176 * verify that udata_open can explicitly open them.
177 * These data files are present in the ICU data/build directory after a build
178 * completes. Tests are most commonly run with the data directory pointing
179 * back into this directory structure, but this is not required. Soooo, if
180 * the files are missing, skip this test without error.
182 /* lots_of_mallocs(); */
183 icuDataFilePath
= (char *)malloc(strlen(ctest_dataOutDir()) + 50);
184 strcpy(icuDataFilePath
, ctest_dataOutDir());
185 strcat(icuDataFilePath
, "build");
186 strcat(icuDataFilePath
, dirSepString
);
187 strcat(icuDataFilePath
, U_ICUDATA_NAME
);
188 strcat(icuDataFilePath
, "_");
189 strcat(icuDataFilePath
, "unorm.icu");
191 /* lots_of_mallocs(); */
192 /* if (stat(icuDataFilePath, &stat_buf) == 0)*/
195 log_verbose("%s exists, so..\n", icuDataFilePath
);
196 strcpy(icuDataFilePath
, ctest_dataOutDir());
197 strcat(icuDataFilePath
, "build");
198 strcat(icuDataFilePath
, dirSepString
);
199 strcat(icuDataFilePath
, U_ICUDATA_NAME
);
200 log_verbose("Testing udata_open() on %s\n", icuDataFilePath
);
201 for(i
=0; i
<sizeof(memMap
)/sizeof(memMap
[0]); i
++){
203 result
=udata_open(icuDataFilePath
, memMap
[i
][1], memMap
[i
][0], &status
);
204 if(U_FAILURE(status
)) {
205 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
));
207 log_verbose("PASS: udata_open worked for path = %s, name=%s, type=%s\n", icuDataFilePath
, memMap
[i
][0], memMap
[i
][1]);
214 log_verbose("Skipping tests of udata_open() on %s. File not present in this configuration.\n",
218 free(icuDataFilePath
);
219 icuDataFilePath
= NULL
;
222 * Test fallback file names for open of separate data files.
223 * With these params to udata_open:
224 * path = wherever/testdata
227 * these files will be tried first:
228 * wherever/testudata_nam.typ
230 * A test data file named testudata_nam.typ exists for the purpose of testing this.
232 log_verbose("Testing udata_open, with base_name.type style fallback to individual file.\n");
234 status
= U_ZERO_ERROR
;
235 result
= udata_open( testPath
, "typ", "nam", &status
);
236 if (status
!= U_ZERO_ERROR
) {
237 log_err("FAIL: udata_open( \"%s\", \"typ\", \"nam\") returned status %s\n", testPath
, u_errorName(status
));
240 free(icuDataFilePath
);
243 /* This type of path is deprecated */
245 * Another fallback test. Paths ending with a trailing directory separator
246 * take a slightly different code path, with the "base name" from the path
247 * being empty in the internal udata_open logic.
250 /* log_verbose("Testing udata_open, with path containing a trailing directory separator.\n"); */
251 /* icuDataFilePath = (char *)malloc(strlen(u_getDataDirectory()) + 50); */
252 /* strcpy(icuDataFilePath, testPath); */
253 /* status = U_ZERO_ERROR; */
254 /* result = udata_open( icuDataFilePath, "cnv", "test1", &status); */
255 /* if (status != U_ZERO_ERROR) { */
256 /* log_err("FAIL: udata_open( \"%s\", \"cnv\", \"test1\") returned status %s\n", icuDataFilePath, u_errorName(status)); */
258 /* udata_close(result); */
259 /* free(icuDataFilePath); */
262 log_verbose("Testing udata_open() with a non existing binary file\n");
263 result
=udata_open("testdata", "tst", "nonexist", &status
);
264 if(status
==U_FILE_ACCESS_ERROR
){
265 log_verbose("Opening udata_open with non-existing file handled correctly.\n");
268 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
));
269 if(U_SUCCESS(status
)) {
275 log_err("calling udata_open with non-existing file didn't return a null value\n");
277 log_verbose("calling udat_open with non-existing file returned null as expected\n");
281 * Try opening data with absurdly long path and name, to trigger buffer size
282 * overflow handling code.
285 char longTestPath
[1024]; /* Implementation goes to heap at length of 128. */
288 /* Try a very long nonexistent directory path.
289 * udata_open should still succeed. Opening with the path will fail,
290 * then fall back to skipping the directory portion of the path.
292 log_verbose("Testing udata_open() with really long names\n");
294 strcat(longTestPath
, "bogus_directory_name");
295 while (strlen(longTestPath
) < 500) {
296 strcat(longTestPath
, dirSepString
);
297 strcat(longTestPath
, "bogus_directory_name");
299 strcat(longTestPath
, pathSepString
);
300 strcat(longTestPath
, testPath
);
301 result
=udata_open(longTestPath
, type
, name
, &status
);
302 if(U_FAILURE(status
)){
303 log_err("FAIL: udata_open() failed for path = %s\n name=%s, type=%s, \n errorcode=%s\n",
304 longTestPath
, name
, type
, myErrorName(status
));
306 log_verbose("PASS: udata_open worked\n");
310 /* Try a very long name. Won't open, but shouldn't blow up.
313 while (strlen(longName
) < 500) {
314 strcat(longName
, name
);
315 strcat(longName
, "_");
317 strcat(longName
, dirSepString
);
318 strcat(longName
, name
);
320 result
=udata_open(longTestPath
, type
, longName
, &status
);
321 if (status
!= U_FILE_ACCESS_ERROR
) {
322 log_err("FAIL: udata_open() failed for path = %s\n name=%s, type=%s, \n errorcode=%s\n",
323 longTestPath
, longName
, type
, myErrorName(status
));
333 static void TestUDataSetAppData(){
334 /* UDataMemory *dataItem;*/
336 UErrorCode status
=U_ZERO_ERROR
;
337 int fileHandle
= 0; /* We are going to read the testdata.dat file */
344 /* Open the testdata.dat file, using normal */
345 const char* tdrelativepath
= loadTestData(&status
);
346 char* filePath
=(char*)malloc(sizeof(char) * (strlen(tdrelativepath
) + strlen(".dat") +1 +strlen(tdrelativepath
)) );
348 strcpy(filePath
, tdrelativepath
);
349 strcat(filePath
, ".dat");
351 log_verbose("Testing udata_setAppData() with %s\n", filePath
);
353 #if defined(WIN32) || defined(U_CYGWIN)
354 fileHandle
= open( filePath
, O_RDONLY
| O_BINARY
);
356 fileHandle
= open( filePath
, O_RDONLY
);
358 if( fileHandle
== -1 ) {
359 log_err("FAIL: TestUDataSetAppData() can not open(\"%s\", O_RDONLY)\n", filePath
);
360 goto cleanupAndReturn
;
364 *Find the size of testdata.dat, and read the whole thing into memory
366 if (fstat(fileHandle
, &statBuf
) == 0) {
367 fileSize
= statBuf
.st_size
;
370 log_err("FAIL: TestUDataSetAppData() can not find size of file \"%s\".\n", filePath
);
371 goto cleanupAndReturn
;
374 fileBuf
= (char *)ctst_malloc(fileSize
);
376 log_err("FAIL: TestUDataSetAppData() can not malloc(%d) for file \"%s\".\n", fileSize
, filePath
);
377 goto cleanupAndReturn
;
380 i
= read(fileHandle
, fileBuf
, fileSize
);
382 log_err("FAIL: TestUDataSetAppData() error reading file \"%s\" size=%d read=%d.\n", filePath
, fileSize
, i
);
383 goto cleanupAndReturn
;
387 * Got testdata.dat into memory, now we try setAppData using the memory image.
391 udata_setAppData("appData1", fileBuf
, &status
);
392 if (status
!= U_ZERO_ERROR
) {
393 log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData1\", fileBuf, status) "
394 " returned status of %s\n", u_errorName(status
));
395 goto cleanupAndReturn
;
398 udata_setAppData("appData2", fileBuf
, &status
);
399 if (status
!= U_ZERO_ERROR
) {
400 log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData2\", fileBuf, status) "
401 " returned status of %s\n", u_errorName(status
));
402 goto cleanupAndReturn
;
405 /* If we try to setAppData with the same name a second time, we should get a
406 * a using default warning.
408 udata_setAppData("appData2", fileBuf
, &status
);
409 if (status
!= U_USING_DEFAULT_WARNING
) {
410 log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData2\", fileBuf, status) "
411 " returned status of %s, expected U_USING_DEFAULT_WARNING.\n", u_errorName(status
));
415 /** It is no longer correct to use udata_setAppData to change the
416 package of a contained item.
418 dataItem = udata_open("appData1", "res", "te_IN", &status); **/
421 /* Note: fileBuf is not deleted because ICU retains a pointer to it
422 * forever (until ICU is shut down).
424 if (fileHandle
> 0) {
432 static UBool U_CALLCONV
433 isAcceptable1(void *context
,
434 const char *type
, const char *name
,
435 const UDataInfo
*pInfo
) {
437 if( pInfo
->size
>=20 &&
438 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
439 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
440 pInfo
->dataFormat
[0]==0x43 && /* dataFormat="CvAl" */
441 pInfo
->dataFormat
[1]==0x76 &&
442 pInfo
->dataFormat
[2]==0x41 &&
443 pInfo
->dataFormat
[3]==0x6c &&
444 pInfo
->formatVersion
[0]==3 )
446 log_verbose("The data from \"%s.%s\" IS acceptable using the verifing function isAcceptable1()\n", name
, type
);
449 log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable1():-\n"
451 "\tisBigEndian = %d\n"
452 "\tcharsetFamily = %d\n"
453 "\tformatVersion[0] = %d\n"
454 "\tdataVersion[0] = %d\n"
455 "\tdataFormat = %c%c%c%c\n",
456 name
, type
, pInfo
->size
, pInfo
->isBigEndian
, pInfo
->charsetFamily
, pInfo
->formatVersion
[0],
457 pInfo
->dataVersion
[0], pInfo
->dataFormat
[0], pInfo
->dataFormat
[1], pInfo
->dataFormat
[2],
458 pInfo
->dataFormat
[3]);
459 log_verbose("Call another verifing function to accept the data\n");
464 static UBool U_CALLCONV
465 isAcceptable2(void *context
,
466 const char *type
, const char *name
,
467 const UDataInfo
*pInfo
){
468 UVersionInfo unicodeVersion
;
470 u_getUnicodeVersion(unicodeVersion
);
472 if( pInfo
->size
>=20 &&
473 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
474 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
475 pInfo
->dataFormat
[0]==0x75 && /* dataFormat="unam" */
476 pInfo
->dataFormat
[1]==0x6e &&
477 pInfo
->dataFormat
[2]==0x61 &&
478 pInfo
->dataFormat
[3]==0x6d &&
479 pInfo
->formatVersion
[0]==1 &&
480 pInfo
->dataVersion
[0]==unicodeVersion
[0] )
482 log_verbose("The data from \"%s.%s\" IS acceptable using the verifing function isAcceptable2()\n", name
, type
);
485 log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable2()\n", name
, type
);
492 static UBool U_CALLCONV
493 isAcceptable3(void *context
,
494 const char *type
, const char *name
,
495 const UDataInfo
*pInfo
){
497 if( pInfo
->size
>=20 &&
498 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
499 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
500 pInfo
->dataFormat
[0]==0x54 && /* dataFormat="test" */
501 pInfo
->dataFormat
[1]==0x65 &&
502 pInfo
->dataFormat
[2]==0x73 &&
503 pInfo
->dataFormat
[3]==0x74 &&
504 pInfo
->formatVersion
[0]==1 &&
505 pInfo
->dataVersion
[0]==1 ) {
506 log_verbose("The data from \"%s.%s\" IS acceptable using the verifing function isAcceptable3()\n", name
, type
);
510 log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable3()\n", name
, type
);
517 static void TestUDataOpenChoiceDemo1() {
519 UErrorCode status
=U_ZERO_ERROR
;
526 const char* type
="icu";
527 const char* testPath
="testdata";
529 result
=udata_openChoice(NULL
, "icu", name
[0], isAcceptable1
, NULL
, &status
);
530 if(U_FAILURE(status
)){
531 log_err("FAIL: udata_openChoice() failed name=%s, type=%s, \n errorcode=%s\n", name
[0], type
, myErrorName(status
));
533 log_verbose("PASS: udata_openChoice worked\n");
537 result
=udata_openChoice(NULL
, type
, name
[1], isAcceptable1
, NULL
, &status
);
538 if(U_FAILURE(status
)){
540 result
=udata_openChoice(NULL
, type
, name
[1], isAcceptable2
, NULL
, &status
);
541 if(U_FAILURE(status
)){
542 log_err("FAIL: udata_openChoice() failed name=%s, type=%s, \n errorcode=%s\n", name
[1], type
, myErrorName(status
));
546 if(U_SUCCESS(status
)){
550 result
=udata_openChoice(testPath
, type
, name
[2], isAcceptable1
, NULL
, &status
);
551 if(U_FAILURE(status
)){
553 result
=udata_openChoice(testPath
, type
, name
[2], isAcceptable3
, NULL
, &status
);
554 if(U_FAILURE(status
)){
555 log_err("FAIL: udata_openChoice() failed path=%s name=%s, type=%s, \n errorcode=%s\n", testPath
, name
[2], type
, myErrorName(status
));
559 if(U_SUCCESS(status
)){
564 static UBool U_CALLCONV
565 isAcceptable(void *context
,
566 const char *type
, const char *name
,
567 const UDataInfo
*pInfo
){
568 if( pInfo
->size
>=20 &&
569 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
570 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
571 pInfo
->dataFormat
[0]==0x54 && /* dataFormat="test" */
572 pInfo
->dataFormat
[1]==0x65 &&
573 pInfo
->dataFormat
[2]==0x73 &&
574 pInfo
->dataFormat
[3]==0x74 &&
575 pInfo
->formatVersion
[0]==1 &&
576 pInfo
->dataVersion
[0]==1 &&
577 *((int*)context
) == 2 ) {
578 log_verbose("The data from\"%s.%s\" IS acceptable using the verifing function isAcceptable()\n", name
, type
);
582 log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable()\n", name
, type
);
588 /* This test checks to see if the isAcceptable function is being called correctly. */
590 static void TestUDataOpenChoiceDemo2() {
592 UErrorCode status
=U_ZERO_ERROR
;
596 const char* name
="test";
597 const char* type
="icu";
598 const char* path
= loadTestData(&status
);
600 result
=udata_openChoice(path
, type
, name
, isAcceptable
, &p
, &status
);
601 if(U_FAILURE(status
)){
602 log_err("failed to load data at p=%s t=%s n=%s, isAcceptable", path
, type
, name
);
604 if(U_SUCCESS(status
) ) {
610 result
=udata_openChoice(path
, type
, name
, isAcceptable
, &p
, &status
);
612 if(U_FAILURE(status
) && status
==U_INVALID_FORMAT_ERROR
){
613 log_verbose("Loads the data but rejects it as expected %s\n", myErrorName(status
));
618 log_err("FAIL: failed to either load the data or to reject the loaded data. ERROR=%s\n", myErrorName(status
) );
622 if(U_FAILURE(status
)) {
623 log_err("FAIL: failed to load the data and accept it. ERROR=%s\n", myErrorName(status
) );
626 log_verbose("Loads the data and accepts it for p==2 as expected\n");
634 static void TestUDataGetInfo() {
637 /* UDataInfo cf. udata.h */
638 static UDataInfo dataInfo
={
639 30, /*sizeof(UDataInfo),*/
647 {0x54, 0x65, 0x73, 0x74}, /* dataFormat="Test" */
648 {9, 0, 0, 0}, /* formatVersion */
649 {4, 0, 0, 0} /* dataVersion */
651 UErrorCode status
=U_ZERO_ERROR
;
652 const char* name
="cnvalias";
653 const char* name2
="test";
654 const char* type
="icu";
656 const char* testPath
=loadTestData(&status
);
658 log_verbose("Testing udata_getInfo() for cnvalias.icu\n");
659 result
=udata_open(NULL
, "icu", name
, &status
);
660 if(U_FAILURE(status
)){
661 log_err("FAIL: udata_open() failed for path = NULL, name=%s, type=%s, \n errorcode=%s\n", name
, type
, myErrorName(status
));
664 udata_getInfo(result
, &dataInfo
);
665 if(dataInfo
.size
==20 && dataInfo
.size
!=30 &&
666 dataInfo
.isBigEndian
==U_IS_BIG_ENDIAN
&&
667 dataInfo
.charsetFamily
==U_CHARSET_FAMILY
&&
668 dataInfo
.dataFormat
[0]==0x43 && dataInfo
.dataFormat
[0]!=0x54 && /* dataFormat="CvAl" and not "Test". The values are set for cnvalias.dat*/
669 dataInfo
.dataFormat
[1]==0x76 && dataInfo
.dataFormat
[1]!=0x65 &&
670 dataInfo
.dataFormat
[2]==0x41 && dataInfo
.dataFormat
[2]!=0x73 &&
671 dataInfo
.dataFormat
[3]==0x6c && dataInfo
.dataFormat
[3]!=0x74 &&
672 dataInfo
.formatVersion
[0]!=9 && /*formatVersion is also set to the one for cnvalias*/
673 dataInfo
.dataVersion
[0]!=4 && /*dataVersion*/
674 dataInfo
.dataVersion
[1]!=0 ){
675 log_verbose("PASS: udata_getInfo() filled in the right values\n");
677 log_err("FAIL: udata_getInfo() filled in the wrong values\n");
682 log_verbose("Testing udata_getInfo() for test.icu\n");
683 result
=udata_open(testPath
, type
, name2
, &status
);
684 if(U_FAILURE(status
)) {
685 log_err("FAIL: udata_open() failed for path=%s name2=%s, type=%s, \n errorcode=%s\n", testPath
, name2
, type
, myErrorName(status
));
688 udata_getInfo(result
, &dataInfo
);
689 if(dataInfo
.size
==20 &&
690 dataInfo
.isBigEndian
==U_IS_BIG_ENDIAN
&&
691 dataInfo
.charsetFamily
==U_CHARSET_FAMILY
&&
692 dataInfo
.dataFormat
[0]==0x54 && /* dataFormat="Test". The values are set for test.dat*/
693 dataInfo
.dataFormat
[1]==0x65 &&
694 dataInfo
.dataFormat
[2]==0x73 &&
695 dataInfo
.dataFormat
[3]==0x74 &&
696 dataInfo
.formatVersion
[0]==1 && /*formatVersion is also set to the one for test*/
697 dataInfo
.dataVersion
[0]==1 && /*dataVersion*/
698 dataInfo
.dataVersion
[1]==0 )
700 log_verbose("PASS: udata_getInfo() filled in the right values\n");
702 log_err("FAIL: udata_getInfo() filled in the wrong values\n");
707 static void TestUDataGetMemory() {
710 const int32_t *table
=NULL
;
711 uint16_t* intValue
=0;
712 UErrorCode status
=U_ZERO_ERROR
;
713 const char* name
="cnvalias";
716 const char* name2
="test";
718 const char* testPath
= loadTestData(&status
);
721 log_verbose("Testing udata_getMemory() for \"cnvalias.icu\"\n");
722 result
=udata_openChoice(NULL
, type
, name
, isAcceptable1
, NULL
, &status
);
723 if(U_FAILURE(status
)){
724 log_err("FAIL: udata_openChoice() failed for name=%s, type=%s, \n errorcode=%s\n", name
, type
, myErrorName(status
));
727 table
=(const int32_t *)udata_getMemory(result
);
729 /* The alias table may list more converters than what's actually available now. [grhoten] */
730 if(ucnv_countAvailable() > table
[1]) /*???*/
731 log_err("FAIL: udata_getMemory() failed ucnv_countAvailable returned = %d, expected = %d\n", ucnv_countAvailable(), table
[1+2*(*table
)]);
736 log_verbose("Testing udata_getMemory for \"test.icu\"()\n");
737 result
=udata_openChoice(testPath
, type
, name2
, isAcceptable3
, NULL
, &status
);
738 if(U_FAILURE(status
)){
739 log_err("FAIL: udata_openChoice() failed for path=%s name=%s, type=%s, \n errorcode=%s\n", testPath
, name2
, type
, myErrorName(status
));
742 intValue
=(uint16_t *)udata_getMemory(result
);
743 /*printf("%d ..... %s", *(intValue), intValue+1));*/
744 if( *intValue
!= 2000 || strcmp((char*)(intValue
+1), "YEAR") != 0 )
745 log_err("FAIL: udata_getMemory() failed: intValue :- Expected:2000 Got:%d \n\tstringValue:- Expected:YEAR Got:%s\n", *intValue
, (intValue
+1));
751 static void TestErrorConditions(){
753 UDataMemory
*result
=NULL
;
754 UErrorCode status
=U_ZERO_ERROR
;
755 uint16_t* intValue
=0;
756 static UDataInfo dataInfo
={
757 30, /*sizeof(UDataInfo),*/
765 {0x54, 0x65, 0x73, 0x74}, /* dataFormat="Test" */
766 {9, 0, 0, 0}, /* formatVersion */
767 {4, 0, 0, 0} /* dataVersion */
770 const char* name
= "test";
771 const char* type
="icu";
773 const char *testPath
= loadTestData(&status
);
775 status
= U_ILLEGAL_ARGUMENT_ERROR
;
776 /*Try udata_open with status != U_ZERO_ERROR*/
777 log_verbose("Testing udata_open() with status != U_ZERO_ERROR\n");
778 result
=udata_open(testPath
, type
, name
, &status
);
780 log_err("FAIL: udata_open() is supposed to fail for path = %s, name=%s, type=%s, \n errorcode !=U_ZERO_ERROR\n", testPath
, name
, type
);
784 log_verbose("PASS: udata_open with errorCode != U_ZERO_ERROR failed as expected\n");
787 /*Try udata_open with data name=NULL*/
788 log_verbose("Testing udata_open() with data name=NULL\n");
790 result
=udata_open(testPath
, type
, NULL
, &status
);
791 if(U_FAILURE(status
)){
792 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
|| result
!= NULL
){
793 log_err("FAIL: udata_open() with name=NULL should return NULL and errocode U_ILLEGAL_ARGUMENT_ERROR, GOT: errorcode=%s\n", myErrorName(status
));
795 log_verbose("PASS: udata_open with name=NULL failed as expected and errorcode = %s as expected\n", myErrorName(status
));
798 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
);
803 /*Try udata_openChoice with status != U_ZERO_ERROR*/
804 log_verbose("Testing udata_openChoice() with status != U_ZERO_ERROR\n");
805 status
=U_ILLEGAL_ARGUMENT_ERROR
;
806 result
=udata_openChoice(testPath
, type
, name
, isAcceptable3
, NULL
, &status
);
808 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
);
811 log_verbose("PASS: udata_openChoice() with errorCode != U_ZERO_ERROR failed as expected\n");
814 /*Try udata_open with data name=NULL*/
815 log_verbose("Testing udata_openChoice() with data name=NULL\n");
817 result
=udata_openChoice(testPath
, type
, NULL
, isAcceptable3
, NULL
, &status
);
818 if(U_FAILURE(status
)){
819 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
|| result
!= NULL
){
820 log_err("FAIL: udata_openChoice() with name=NULL should return NULL and errocode U_ILLEGAL_ARGUMENT_ERROR, GOT: errorcode=%s\n", myErrorName(status
));
822 log_verbose("PASS: udata_openChoice with name=NULL failed as expected and errorcode = %s as expected\n", myErrorName(status
));
825 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
);
829 /*Try udata_getMemory with UDataMemory=NULL*/
830 log_verbose("Testing udata_getMemory with UDataMemory=NULL\n");
831 intValue
=(uint16_t*)udata_getMemory(NULL
);
832 if(intValue
!= NULL
){
833 log_err("FAIL: udata_getMemory with UDataMemory = NULL is supposed to fail\n");
836 /*Try udata_getInfo with UDataMemory=NULL*/
838 udata_getInfo(NULL
, &dataInfo
);
839 if(dataInfo
.size
!= 0){
840 log_err("FAIL : udata_getInfo with UDataMemory = NULL us supposed to fail\n");
843 /*Try udata_openChoice with a non existing binary file*/
844 log_verbose("Testing udata_openChoice() with a non existing binary file\n");
845 result
=udata_openChoice(testPath
, "tst", "nonexist", isAcceptable3
, NULL
, &status
);
846 if(status
==U_FILE_ACCESS_ERROR
){
847 log_verbose("Opening udata_openChoice with non-existing file handled correctly.\n");
850 log_err("calling udata_open with non-existing file not handled correctly\n. Expected: U_FILE_ACCESS_ERROR, Got: %s\n", myErrorName(status
));
851 if(U_SUCCESS(status
)) {
857 log_err("calling udata_open with non-existing file didn't return a null value\n");
859 log_verbose("calling udat_open with non-existing file returned null as expected\n");
863 /* Test whether apps and ICU can each have their own root.res */
864 static void TestAppData()
866 UResourceBundle
*icu
, *app
;
867 UResourceBundle
*tmp
= NULL
;
868 UResourceBundle
*tmp2
= NULL
;
870 const UChar
*appString
;
871 const UChar
*icuString
;
875 UErrorCode status
= U_ZERO_ERROR
;
876 char testMsgBuf
[256];
878 const char* testPath
=loadTestData(&status
);
880 icu
= ures_open(NULL
, "root", &status
);
881 if(U_FAILURE(status
))
883 log_err("%s:%d: Couldn't open root ICU bundle- %s", __FILE__
, __LINE__
, u_errorName(status
));
886 /* log_info("Open icu root: %s size_%d\n", u_errorName(status), ures_getSize(icu)); */
887 status
= U_ZERO_ERROR
;
889 app
= ures_open(testPath
, "root", &status
);
890 if(U_FAILURE(status
))
892 log_err("%s:%d: Couldn't open app ICU bundle [%s]- %s", __FILE__
, __LINE__
, testPath
, u_errorName(status
));
895 /* log_info("Open app: %s, size %d\n", u_errorName(status), ures_getSize(app)); */
897 tmp
= ures_getByKey(icu
, "Version", tmp
, &status
);
898 if(U_FAILURE(status
))
900 log_err("%s:%d: Couldn't get Version string from ICU root bundle- %s", __FILE__
, __LINE__
, u_errorName(status
));
904 icuString
= ures_getString(tmp
, &len
, &status
);
905 if(U_FAILURE(status
))
907 log_err("%s:%d: Couldn't get string from Version string from ICU root bundle- %s", __FILE__
, __LINE__
, u_errorName(status
));
910 /* log_info("icuString=%p - %s\n", icuString, austrdup(icuString)); */
913 tmp2
= ures_getByKey(app
, "Version", tmp2
, &status
);
914 if(U_FAILURE(status
))
916 log_err("%s:%d: Couldn't get Version string from App root bundle- %s", __FILE__
, __LINE__
, u_errorName(status
));
920 appString
= ures_getString(tmp2
, &len
, &status
);
921 if(U_FAILURE(status
))
923 log_err("%s:%d: Couldn't get string from Version string from App root bundle- %s", __FILE__
, __LINE__
, u_errorName(status
));
927 /* log_info("appString=%p - %s\n", appString, austrdup(appString)); */
930 if(!u_strcmp(icuString
, appString
))
932 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
),
933 austrdup(appString
));
937 log_verbose("%s:%d: appstr=%s, icustr=%s\n", __FILE__
,
938 __LINE__
, u_austrcpy(testMsgBuf
, appString
), u_austrcpy(testMsgBuf
, icuString
));
947 static void TestICUDataName()
949 UVersionInfo icuVersion
;
950 char expectDataName
[20];
951 unsigned int expectLen
= 8;
955 /* Print out the version # we have .. */
956 log_verbose("utypes.h says U_ICUDATA_NAME = %s\n", U_ICUDATA_NAME
);
958 /* Build up the version # we expect to get */
959 u_getVersion(icuVersion
);
961 switch(U_CHARSET_FAMILY
)
964 switch(U_IS_BIG_ENDIAN
)
973 log_err("Expected 1 or 0 for U_IS_BIG_ENDIAN, got %d!\n", (int)U_IS_BIG_ENDIAN
);
977 case U_EBCDIC_FAMILY
:
982 sprintf(expectDataName
, "%s%d%d%c",
988 log_verbose("Expected: %s\n", expectDataName
);
989 if(uprv_strlen(expectDataName
) != expectLen
)
991 log_err("*Expected* length is wrong (test err?), should be %d is %d\n",
992 expectLen
, uprv_strlen(expectDataName
));
995 if(uprv_strlen(U_ICUDATA_NAME
) != expectLen
)
997 log_err("U_ICUDATA_NAME length should be %d is %d\n",
998 expectLen
, uprv_strlen(U_ICUDATA_NAME
));
1001 if(uprv_strcmp(U_ICUDATA_NAME
, expectDataName
))
1003 log_err("U_ICUDATA_NAME should be %s but is %s\n",
1004 expectDataName
, U_ICUDATA_NAME
);
1007 /* ICUDATA_NAME comes from the build system on *nix */
1009 if(uprv_strcmp(U_ICUDATA_NAME
, ICUDATA_NAME
))
1011 log_err("ICUDATA_NAME and U_ICUDATA_NAME don't match: "
1012 "ICUDATA_NAME=%s, U_ICUDATA_NAME=%s. Check configure.in, icudefs.mk.in, utypes.h...\n", ICUDATA_NAME
, U_ICUDATA_NAME
);
1016 log_verbose("ICUDATA_NAME=%s (from icudefs.mk), U_ICUDATA_NAME=%s (from utypes.h)\n", ICUDATA_NAME
, U_ICUDATA_NAME
);
1022 /* test data swapping ------------------------------------------------------- */
1024 /* test cases for maximum data swapping code coverage */
1025 static const struct {
1026 const char *name
, *type
;
1027 UDataSwapFn
*swapFn
;
1029 /* resource bundles */
1031 /* resource bundle with many data types */
1032 {"*testtypes", "res", ures_swap
},
1033 /* resource bundle with collation data */
1034 {"ja", "res", ures_swap
},
1035 /* resource bundle with options-only collation data */
1036 {"ru", "res", ures_swap
},
1037 {"el", "res", ures_swap
},
1039 {"root", "res", ures_swap
},
1041 /* ICU 2.6 resource bundle - data format 1.0, without indexes[] (little-endian ASCII) */
1042 {"*icu26_testtypes", "res", ures_swap
},
1043 /* same for big-endian EBCDIC */
1044 {"*icu26e_testtypes", "res", ures_swap
},
1046 #if !UCONFIG_NO_COLLATION
1047 /* standalone collation data files */
1048 {"ucadata", "icu", ucol_swap
},
1049 {"invuca", "icu", ucol_swapInverseUCA
},
1052 #if !UCONFIG_NO_LEGACY_CONVERSION
1053 /* conversion table files */
1055 /* SBCS conversion table file without extension */
1056 {"ibm-913_P100-2000", "cnv", ucnv_swap
},
1057 /* EBCDIC_STATEFUL conversion table file with extension */
1058 {"ibm-1390_P110-2003", "cnv", ucnv_swap
},
1059 /* DBCS extension-only conversion table file */
1060 {"ibm-16684_P110-2003", "cnv", ucnv_swap
},
1061 /* EUC-TW (3-byte) conversion table file without extension */
1062 {"ibm-964_P110-1999", "cnv", ucnv_swap
},
1063 /* GB 18030 (4-byte) conversion table file without extension */
1064 {"gb18030", "cnv", ucnv_swap
},
1065 /* MBCS conversion table file with extension */
1066 {"*test4x", "cnv", ucnv_swap
},
1069 {"cnvalias", "icu", ucnv_swapAliases
},
1072 #if !UCONFIG_NO_IDNA
1073 {"uidna", "spp", usprep_swap
},
1076 #if !UCONFIG_NO_BREAK_ITERATION
1077 {"char", "brk", ubrk_swap
},
1080 /* the last item should not be #if'ed so that it can reliably omit the last comma */
1082 /* Unicode properties */
1083 {"unames", "icu", uchar_swapNames
},
1084 {"pnames", "icu", upname_swap
},
1085 #if !UCONFIG_NO_NORMALIZATION
1086 {"unorm", "icu", unorm_swap
},
1088 {"uprops", "icu", uprops_swap
},
1089 {"ucase", "icu", ucase_swap
}
1092 #define SWAP_BUFFER_SIZE 1000000
1094 static void U_CALLCONV
1095 printError(void *context
, const char *fmt
, va_list args
) {
1096 vlog_info("[swap] ", fmt
, args
);
1100 TestSwapCase(UDataMemory
*pData
, const char *name
,
1101 UDataSwapFn
*swapFn
,
1102 uint8_t *buffer
, uint8_t *buffer2
) {
1104 const void *inData
, *inHeader
;
1105 int32_t length
, dataLength
, length2
, headerLength
;
1107 UErrorCode errorCode
;
1109 UBool inEndian
, oppositeEndian
;
1110 uint8_t inCharset
, oppositeCharset
;
1112 inData
=udata_getMemory(pData
);
1115 * get the data length if possible, to verify that swapping and preflighting
1116 * handles the entire data
1118 dataLength
=udata_getLength(pData
);
1121 * get the header and its length
1122 * all of the swap implementation functions require the header to be included
1124 inHeader
=udata_getRawMemory(pData
);
1125 headerLength
=(int32_t)((const char *)inData
-(const char *)inHeader
);
1127 /* first swap to opposite endianness but same charset family */
1128 errorCode
=U_ZERO_ERROR
;
1129 ds
=udata_openSwapperForInputData(inHeader
, headerLength
,
1130 !U_IS_BIG_ENDIAN
, U_CHARSET_FAMILY
, &errorCode
);
1131 if(U_FAILURE(errorCode
)) {
1132 log_err("udata_openSwapperForInputData(%s->!isBig+same charset) failed - %s\n",
1133 name
, u_errorName(errorCode
));
1137 inEndian
=ds
->inIsBigEndian
;
1138 inCharset
=ds
->inCharset
;
1140 oppositeEndian
=!inEndian
;
1141 oppositeCharset
= inCharset
==U_ASCII_FAMILY
? U_EBCDIC_FAMILY
: U_ASCII_FAMILY
;
1143 /* make this test work with data files that are built for a different platform */
1144 if(inEndian
!=U_IS_BIG_ENDIAN
|| inCharset
!=U_CHARSET_FAMILY
) {
1145 udata_closeSwapper(ds
);
1146 ds
=udata_openSwapper(inEndian
, inCharset
, oppositeEndian
, inCharset
, &errorCode
);
1147 if(U_FAILURE(errorCode
)) {
1148 log_err("udata_openSwapper(%s->!isBig+same charset) failed - %s\n",
1149 name
, u_errorName(errorCode
));
1154 ds
->printError
=printError
;
1156 /* preflight the length */
1157 length
=swapFn(ds
, inHeader
, -1, NULL
, &errorCode
);
1158 if(U_FAILURE(errorCode
)) {
1159 log_err("swapFn(preflight %s->!isBig+same charset) failed - %s\n",
1160 name
, u_errorName(errorCode
));
1161 udata_closeSwapper(ds
);
1165 /* compare the preflighted length against the data length */
1166 if(dataLength
>=0 && (length
+15)<(headerLength
+dataLength
)) {
1167 log_err("swapFn(preflight %s->!isBig+same charset) length too small: %d < data length %d\n",
1168 name
, length
, (headerLength
+dataLength
));
1169 udata_closeSwapper(ds
);
1173 /* swap, not in-place */
1174 length2
=swapFn(ds
, inHeader
, length
, buffer
, &errorCode
);
1175 udata_closeSwapper(ds
);
1176 if(U_FAILURE(errorCode
)) {
1177 log_err("swapFn(%s->!isBig+same charset) failed - %s\n",
1178 name
, u_errorName(errorCode
));
1182 /* compare the swap length against the preflighted length */
1183 if(length2
!=length
) {
1184 log_err("swapFn(%s->!isBig+same charset) length differs from preflighting: %d != preflighted %d\n",
1185 name
, length2
, length
);
1189 /* next swap to opposite charset family */
1190 ds
=udata_openSwapper(oppositeEndian
, inCharset
,
1191 oppositeEndian
, oppositeCharset
,
1193 if(U_FAILURE(errorCode
)) {
1194 log_err("udata_openSwapper(%s->!isBig+other charset) failed - %s\n",
1195 name
, u_errorName(errorCode
));
1198 ds
->printError
=printError
;
1201 length2
=swapFn(ds
, buffer
, length
, buffer
, &errorCode
);
1202 udata_closeSwapper(ds
);
1203 if(U_FAILURE(errorCode
)) {
1204 log_err("swapFn(%s->!isBig+other charset) failed - %s\n",
1205 name
, u_errorName(errorCode
));
1209 /* compare the swap length against the original length */
1210 if(length2
!=length
) {
1211 log_err("swapFn(%s->!isBig+other charset) length differs from original: %d != original %d\n",
1212 name
, length2
, length
);
1216 /* finally swap to original platform values */
1217 ds
=udata_openSwapper(oppositeEndian
, oppositeCharset
,
1218 inEndian
, inCharset
,
1220 if(U_FAILURE(errorCode
)) {
1221 log_err("udata_openSwapper(%s->back to original) failed - %s\n",
1222 name
, u_errorName(errorCode
));
1225 ds
->printError
=printError
;
1227 /* swap, not in-place */
1228 length2
=swapFn(ds
, buffer
, length
, buffer2
, &errorCode
);
1229 udata_closeSwapper(ds
);
1230 if(U_FAILURE(errorCode
)) {
1231 log_err("swapFn(%s->back to original) failed - %s\n",
1232 name
, u_errorName(errorCode
));
1236 /* compare the swap length against the original length */
1237 if(length2
!=length
) {
1238 log_err("swapFn(%s->back to original) length differs from original: %d != original %d\n",
1239 name
, length2
, length
);
1243 /* compare the final contents with the original */
1244 if(0!=uprv_memcmp(inHeader
, buffer2
, length
)) {
1245 const uint8_t *original
;
1249 log_err("swapFn(%s->back to original) contents differs from original\n",
1252 /* find the first difference */
1253 original
=(const uint8_t *)inHeader
;
1254 for(i
=0; i
<length
&& original
[i
]==buffer2
[i
]; ++i
) {}
1256 /* find the next byte that is the same */
1257 for(j
=i
+1; j
<length
&& original
[j
]!=buffer2
[j
]; ++j
) {}
1258 log_info(" difference at index %d=0x%x, until index %d=0x%x\n", i
, i
, j
, j
);
1260 /* round down to the last 4-boundary for better result output */
1262 log_info("showing bytes from index %d=0x%x (length %d=0x%x):\n", i
, i
, length
, length
);
1264 /* print 8 bytes but limit to the buffer contents */
1265 length2
=i
+sizeof(diff
);
1266 if(length2
>length
) {
1270 /* print the original bytes */
1271 uprv_memset(diff
, 0, sizeof(diff
));
1272 for(j
=i
; j
<length2
; ++j
) {
1273 diff
[j
-i
]=original
[j
];
1275 log_info(" original: %02x %02x %02x %02x %02x %02x %02x %02x\n",
1276 diff
[0], diff
[1], diff
[2], diff
[3], diff
[4], diff
[5], diff
[6], diff
[7]);
1278 /* print the swapped bytes */
1279 uprv_memset(diff
, 0, sizeof(diff
));
1280 for(j
=i
; j
<length2
; ++j
) {
1281 diff
[j
-i
]=buffer2
[j
];
1283 log_info(" swapped: %02x %02x %02x %02x %02x %02x %02x %02x\n",
1284 diff
[0], diff
[1], diff
[2], diff
[3], diff
[4], diff
[5], diff
[6], diff
[7]);
1293 const char *pkg
, *nm
;
1295 UErrorCode errorCode
;
1298 buffer
=(uint8_t *)uprv_malloc(2*SWAP_BUFFER_SIZE
);
1300 log_err("unable to allocate %d bytes\n", 2*SWAP_BUFFER_SIZE
);
1304 for(i
=0; i
<LENGTHOF(swapCases
); ++i
) {
1305 /* build the name for logging */
1306 errorCode
=U_ZERO_ERROR
;
1307 if(swapCases
[i
].name
[0]=='*') {
1308 pkg
=loadTestData(&errorCode
);
1309 nm
=swapCases
[i
].name
+1;
1310 uprv_strcpy(name
, "testdata");
1313 nm
=swapCases
[i
].name
;
1314 uprv_strcpy(name
, "NULL");
1316 uprv_strcat(name
, "/");
1317 uprv_strcat(name
, nm
);
1318 uprv_strcat(name
, ".");
1319 uprv_strcat(name
, swapCases
[i
].type
);
1321 pData
=udata_open(pkg
, swapCases
[i
].type
, nm
, &errorCode
);
1322 if(U_SUCCESS(errorCode
)) {
1323 TestSwapCase(pData
, name
, swapCases
[i
].swapFn
, buffer
, buffer
+SWAP_BUFFER_SIZE
);
1326 log_data_err("udata_open(%s) failed - %s\n",
1327 name
, u_errorName(errorCode
));