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