Fredrik Roubert | 0596fae | 2017-04-18 21:34:02 +0200 | [diff] [blame] | 1 | // © 2016 and later: Unicode, Inc. and others. |
Fredrik Roubert | 64339d3 | 2016-10-21 19:43:16 +0200 | [diff] [blame] | 2 | // License & terms of use: http://www.unicode.org/copyright.html |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 3 | /******************************************************************** |
| 4 | * COPYRIGHT: |
Fredrik Roubert | 8de051c | 2016-03-10 13:13:27 +0100 | [diff] [blame] | 5 | * Copyright (c) 1998-2016, International Business Machines Corporation and |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 6 | * others. All Rights Reserved. |
| 7 | ********************************************************************/ |
| 8 | /* |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 9 | * File udatatst.c |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 10 | * |
| 11 | * Modification History: |
| 12 | * |
| 13 | * Date Name Description |
| 14 | * 02/22/2000 Madhu Creation |
| 15 | ****************************************************************************** |
| 16 | */ |
| 17 | |
| 18 | #include "unicode/utypes.h" |
| 19 | #include "unicode/putil.h" |
| 20 | #include "unicode/udata.h" |
ccornelius | f9878a2 | 2014-11-20 18:09:39 -0800 | [diff] [blame] | 21 | #include "unicode/ucal.h" |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 22 | #include "unicode/uchar.h" |
| 23 | #include "unicode/ucnv.h" |
| 24 | #include "unicode/ures.h" |
| 25 | #include "unicode/ustring.h" |
| 26 | #include "unicode/uclean.h" |
| 27 | #include "cmemory.h" |
| 28 | #include "cstring.h" |
| 29 | #include "filestrm.h" |
| 30 | #include "udatamem.h" |
| 31 | #include "cintltst.h" |
| 32 | #include "ubrkimpl.h" |
ccornelius | 59d709d | 2014-02-20 10:29:46 -0800 | [diff] [blame] | 33 | #include "toolutil.h" /* for uprv_fileExists() */ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 34 | #include <stdlib.h> |
| 35 | #include <stdio.h> |
| 36 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 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" |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 45 | #include "ucol_imp.h" |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 46 | #include "ucol_swp.h" |
| 47 | #include "ucnv_bld.h" |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 48 | #include "sprpimpl.h" |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 49 | #include "rbbidata.h" |
| 50 | |
claireho | 27f6547 | 2011-06-09 11:11:49 -0700 | [diff] [blame] | 51 | /* swapping implementation in i18n */ |
| 52 | #include "uspoof_impl.h" |
| 53 | |
claireho | 50294ea | 2010-05-03 15:44:48 -0700 | [diff] [blame] | 54 | U_CAPI int32_t U_EXPORT2 |
| 55 | unorm2_swap(const UDataSwapper *ds, |
| 56 | const void *inData, int32_t length, void *outData, |
| 57 | UErrorCode *pErrorCode); |
| 58 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 59 | /* other definitions and prototypes */ |
| 60 | |
Craig Cornelius | 54dcd9b | 2013-02-15 14:03:14 -0800 | [diff] [blame] | 61 | #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 62 | static void TestUDataOpen(void); |
| 63 | static void TestUDataOpenChoiceDemo1(void); |
| 64 | static void TestUDataOpenChoiceDemo2(void); |
| 65 | static void TestUDataGetInfo(void); |
| 66 | static void TestUDataGetMemory(void); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 67 | static void TestErrorConditions(void); |
| 68 | static void TestAppData(void); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 69 | static void TestSwapData(void); |
Craig Cornelius | 54dcd9b | 2013-02-15 14:03:14 -0800 | [diff] [blame] | 70 | #endif |
| 71 | static void TestUDataSetAppData(void); |
| 72 | static void TestICUDataName(void); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 73 | static void PointerTableOfContents(void); |
| 74 | static void SetBadCommonData(void); |
| 75 | static void TestUDataFileAccess(void); |
ccornelius | f9878a2 | 2014-11-20 18:09:39 -0800 | [diff] [blame] | 76 | #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION |
| 77 | static void TestTZDataDir(void); |
| 78 | #endif |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 79 | |
| 80 | void addUDataTest(TestNode** root); |
| 81 | |
| 82 | void |
| 83 | addUDataTest(TestNode** root) |
| 84 | { |
claireho | 50294ea | 2010-05-03 15:44:48 -0700 | [diff] [blame] | 85 | #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 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" ); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 91 | addTest(root, &TestErrorConditions, "udatatst/TestErrorConditions"); |
| 92 | addTest(root, &TestAppData, "udatatst/TestAppData" ); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 93 | addTest(root, &TestSwapData, "udatatst/TestSwapData" ); |
claireho | 50294ea | 2010-05-03 15:44:48 -0700 | [diff] [blame] | 94 | #endif |
| 95 | addTest(root, &TestUDataSetAppData, "udatatst/TestUDataSetAppData" ); |
| 96 | addTest(root, &TestICUDataName, "udatatst/TestICUDataName" ); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 97 | addTest(root, &PointerTableOfContents, "udatatst/PointerTableOfContents" ); |
| 98 | addTest(root, &SetBadCommonData, "udatatst/SetBadCommonData" ); |
| 99 | addTest(root, &TestUDataFileAccess, "udatatst/TestUDataFileAccess" ); |
ccornelius | f9878a2 | 2014-11-20 18:09:39 -0800 | [diff] [blame] | 100 | #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION |
| 101 | addTest(root, &TestTZDataDir, "udatatst/TestTZDataDir" ); |
| 102 | #endif |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | #if 0 |
| 106 | static 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 | |
Craig Cornelius | 54dcd9b | 2013-02-15 14:03:14 -0800 | [diff] [blame] | 118 | #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 119 | static void TestUDataOpen(){ |
| 120 | UDataMemory *result; |
| 121 | UErrorCode status=U_ZERO_ERROR; |
| 122 | const char* memMap[][2]={ |
| 123 | {"root", "res"}, |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 124 | {"cnvalias", "icu"}, |
| 125 | {"unames", "icu"}, |
| 126 | {"ibm-37_P100-1995", "cnv"} |
| 127 | }; |
| 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 | |
| 133 | |
| 134 | char* path=(char*)malloc(sizeof(char) * (strlen(ctest_dataOutDir()) |
| 135 | + strlen(U_ICUDATA_NAME) |
ccornelius | 59d709d | 2014-02-20 10:29:46 -0800 | [diff] [blame] | 136 | + strlen("/build/tmp/..")+1 ) ); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 137 | |
| 138 | char *icuDataFilePath = 0; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 139 | |
| 140 | const char* testPath=loadTestData(&status); |
| 141 | if(U_FAILURE(status)) { |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 142 | log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status)); |
claireho | 50294ea | 2010-05-03 15:44:48 -0700 | [diff] [blame] | 143 | free(path); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 144 | return; |
| 145 | } |
| 146 | |
| 147 | /* lots_of_mallocs(); */ |
ccornelius | 59d709d | 2014-02-20 10:29:46 -0800 | [diff] [blame] | 148 | log_verbose("Testing udata_open(%s)\n", testPath); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 149 | result=udata_open(testPath, type, name, &status); |
| 150 | if(U_FAILURE(status)){ |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 151 | log_data_err("FAIL: udata_open() failed for path = %s, name=%s, type=%s, \n errorcode=%s\n", testPath, name, type, myErrorName(status)); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 152 | } else { |
| 153 | log_verbose("PASS: udata_open worked\n"); |
| 154 | udata_close(result); |
| 155 | } |
| 156 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 157 | { |
ccornelius | 59d709d | 2014-02-20 10:29:46 -0800 | [diff] [blame] | 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); |
Fredrik Roubert | 8de051c | 2016-03-10 13:13:27 +0100 | [diff] [blame] | 173 | for(i=0; i<UPRV_LENGTHOF(memMap); i++){ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 174 | /* lots_of_mallocs(); */ |
| 175 | status=U_ZERO_ERROR; |
| 176 | result=udata_open(path, memMap[i][1], memMap[i][0], &status); |
| 177 | if(U_FAILURE(status)) { |
| 178 | log_data_err("FAIL: udata_open() failed for path = %s, name=%s, type=%s, \n errorcode=%s\n", path, memMap[i][0], memMap[i][1], myErrorName(status)); |
| 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 | } |
ccornelius | 59d709d | 2014-02-20 10:29:46 -0800 | [diff] [blame] | 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); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 192 | } |
ccornelius | 59d709d | 2014-02-20 10:29:46 -0800 | [diff] [blame] | 193 | /* try again, adding /tmp */ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 194 | { |
ccornelius | 59d709d | 2014-02-20 10:29:46 -0800 | [diff] [blame] | 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); |
Fredrik Roubert | 8de051c | 2016-03-10 13:13:27 +0100 | [diff] [blame] | 213 | for(i=0; i<UPRV_LENGTHOF(memMap); i++){ |
ccornelius | 59d709d | 2014-02-20 10:29:46 -0800 | [diff] [blame] | 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 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 231 | } |
ccornelius | 59d709d | 2014-02-20 10:29:46 -0800 | [diff] [blame] | 232 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 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); |
ccornelius | 59d709d | 2014-02-20 10:29:46 -0800 | [diff] [blame] | 251 | strcat(icuDataFilePath, dirSepString); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 252 | strcat(icuDataFilePath, "cnvalias.icu"); |
| 253 | |
| 254 | /* lots_of_mallocs(); */ |
ccornelius | 59d709d | 2014-02-20 10:29:46 -0800 | [diff] [blame] | 255 | if (uprv_fileExists(icuDataFilePath)) |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 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); |
Fredrik Roubert | 8de051c | 2016-03-10 13:13:27 +0100 | [diff] [blame] | 264 | for(i=0; i<UPRV_LENGTHOF(memMap); i++){ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 265 | status=U_ZERO_ERROR; |
| 266 | result=udata_open(icuDataFilePath, memMap[i][1], memMap[i][0], &status); |
| 267 | if(U_FAILURE(status)) { |
| 268 | log_data_err("FAIL: udata_open() failed for path = %s, name=%s, type=%s, \n errorcode=%s\n", icuDataFilePath, memMap[i][0], memMap[i][1], myErrorName(status)); |
| 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 | } |
| 275 | else |
| 276 | { |
| 277 | log_verbose("Skipping tests of udata_open() on %s. File not present in this configuration.\n", |
| 278 | icuDataFilePath); |
| 279 | } |
| 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) { |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 300 | log_data_err("FAIL: udata_open( \"%s\", \"typ\", \"nam\") returned status %s\n", testPath, u_errorName(status)); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 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 | |
| 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)){ |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 366 | log_data_err("FAIL: udata_open() failed for path = %s\n name=%s, type=%s, \n errorcode=%s\n", |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 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) { |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 385 | log_data_err("FAIL: udata_open() failed for path = %s\n name=%s, type=%s, \n errorcode=%s\n", |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 386 | longTestPath, longName, type, myErrorName(status)); |
| 387 | } |
| 388 | udata_close(result); |
| 389 | } |
| 390 | |
| 391 | free(path); |
| 392 | } |
Craig Cornelius | 54dcd9b | 2013-02-15 14:03:14 -0800 | [diff] [blame] | 393 | #endif |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 394 | |
| 395 | typedef 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; |
| 409 | |
| 410 | static 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 */ |
| 417 | |
| 418 | #if U_IS_BIG_ENDIAN |
| 419 | 1, |
| 420 | #else |
| 421 | 0, |
| 422 | #endif |
| 423 | |
| 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 | /* } */ |
| 440 | } |
| 441 | }; |
| 442 | |
| 443 | |
| 444 | static void TestUDataSetAppData(){ |
| 445 | /* UDataMemory *dataItem;*/ |
| 446 | |
| 447 | UErrorCode status=U_ZERO_ERROR; |
| 448 | |
| 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)); |
| 458 | return; |
| 459 | } |
| 460 | /* The following call should fail. |
| 461 | If the following works with a bad UErrorCode, then later calls to appData1 should fail. */ |
| 462 | udata_setAppData("appData1", &gEmptyHeader, &status); |
| 463 | |
| 464 | /* |
| 465 | * Got testdata.dat into memory, now we try setAppData using the memory image. |
| 466 | */ |
| 467 | |
| 468 | status=U_ZERO_ERROR; |
| 469 | udata_setAppData("appData1", &gEmptyHeader, &status); |
| 470 | if (status != U_ZERO_ERROR) { |
| 471 | log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData1\", fileBuf, status) " |
| 472 | " returned status of %s\n", u_errorName(status)); |
| 473 | return; |
| 474 | } |
| 475 | |
| 476 | udata_setAppData("appData2", &gEmptyHeader, &status); |
| 477 | if (status != U_ZERO_ERROR) { |
| 478 | log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData2\", fileBuf, status) " |
| 479 | " returned status of %s\n", u_errorName(status)); |
| 480 | return; |
| 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 | */ |
| 486 | udata_setAppData("appData2", &gEmptyHeader, &status); |
| 487 | if (status != U_USING_DEFAULT_WARNING) { |
| 488 | log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData2\", fileBuf, status) " |
| 489 | " returned status of %s, expected U_USING_DEFAULT_WARNING.\n", u_errorName(status)); |
| 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); **/ |
| 497 | } |
| 498 | |
| 499 | static 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 | |
| 509 | static 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)){ |
Jean-Baptiste Queru | 6d5deb1 | 2009-07-20 15:14:11 -0700 | [diff] [blame] | 550 | log_err_status(status, "%s\n", u_errorName(status)); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 551 | } |
| 552 | free(icuDataDir); |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 553 | ctest_resetICU(); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | |
| 557 | static UBool U_CALLCONV |
| 558 | isAcceptable1(void *context, |
| 559 | const char *type, const char *name, |
| 560 | const UDataInfo *pInfo) { |
Nikita Iashchenko | 4c0e286 | 2019-11-05 16:38:00 +0000 | [diff] [blame] | 561 | (void)context; // suppress compiler warnings about unused variable |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 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 | |
| 590 | static UBool U_CALLCONV |
| 591 | isAcceptable2(void *context, |
| 592 | const char *type, const char *name, |
| 593 | const UDataInfo *pInfo){ |
Nikita Iashchenko | 4c0e286 | 2019-11-05 16:38:00 +0000 | [diff] [blame] | 594 | (void)context; // suppress compiler warnings about unused variable |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 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 | } |
| 619 | static UBool U_CALLCONV |
| 620 | isAcceptable3(void *context, |
| 621 | const char *type, const char *name, |
| 622 | const UDataInfo *pInfo){ |
Nikita Iashchenko | 4c0e286 | 2019-11-05 16:38:00 +0000 | [diff] [blame] | 623 | (void)context; // suppress compiler warnings about unused variable |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 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 | |
Craig Cornelius | 54dcd9b | 2013-02-15 14:03:14 -0800 | [diff] [blame] | 645 | #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 646 | static void TestUDataOpenChoiceDemo1() { |
| 647 | UDataMemory *result; |
| 648 | UErrorCode status=U_ZERO_ERROR; |
| 649 | |
| 650 | const char* name[]={ |
| 651 | "cnvalias", |
| 652 | "unames", |
| 653 | "test", |
| 654 | "nam" |
| 655 | }; |
| 656 | const char* type="icu"; |
| 657 | const char* testPath="testdata"; |
| 658 | const char* fullTestDataPath = loadTestData(&status); |
| 659 | if(U_FAILURE(status)) { |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 660 | log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status)); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 661 | return; |
| 662 | } |
| 663 | |
| 664 | result=udata_openChoice(NULL, "icu", name[0], isAcceptable1, NULL, &status); |
| 665 | if(U_FAILURE(status)){ |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 666 | log_data_err("FAIL: udata_openChoice() failed name=%s, type=%s, \n errorcode=%s\n", name[0], type, myErrorName(status)); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 667 | } else { |
| 668 | log_verbose("PASS: udata_openChoice worked\n"); |
| 669 | udata_close(result); |
| 670 | } |
| 671 | |
| 672 | status=U_ZERO_ERROR; |
| 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)){ |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 678 | log_data_err("FAIL: udata_openChoice() failed name=%s, type=%s, \n errorcode=%s\n", name[1], type, myErrorName(status)); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | else { |
| 682 | log_err("FAIL: udata_openChoice() unexpectedly passed. name=%s, type=%s, \n errorcode=%s\n", name[1], type, myErrorName(status)); |
| 683 | } |
| 684 | |
| 685 | if(U_SUCCESS(status)){ |
| 686 | udata_close(result); |
| 687 | } |
| 688 | |
| 689 | status=U_ZERO_ERROR; |
| 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)){ |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 695 | log_data_err("FAIL: udata_openChoice() failed path=%s name=%s, type=%s, \n errorcode=%s\n", testPath, name[2], type, myErrorName(status)); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | else { |
| 699 | log_err("FAIL: udata_openChoice() unexpectedly passed. name=%s, type=%s, \n errorcode=%s\n", name[2], type, myErrorName(status)); |
| 700 | } |
| 701 | |
| 702 | if(U_SUCCESS(status)){ |
| 703 | udata_close(result); |
| 704 | } |
| 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){ |
Victor Chang | 34bb603 | 2021-03-12 17:59:42 +0000 | [diff] [blame^] | 710 | // Android-changed: Android does not try to load ICU data from other files. See gDataFileAccess = UDATA_NO_FILES in udata.cpp. |
| 711 | // log_err("FAIL: udata_openChoice() did not fail as expected. name=%s, type=%s, \n errorcode=%s\n", name[3], type, myErrorName(status)); |
| 712 | log_data_err("FAIL: udata_openChoice() did not fail as expected. name=%s, type=%s, \n errorcode=%s\n", name[3], type, myErrorName(status)); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | status=U_USELESS_COLLATOR_ERROR; |
| 716 | result=udata_openChoice(fullTestDataPath, type, name[3], isAcceptable1, NULL, &status); |
| 717 | if(status != U_USELESS_COLLATOR_ERROR){ |
Victor Chang | 34bb603 | 2021-03-12 17:59:42 +0000 | [diff] [blame^] | 718 | // Android-changed: Android does not try to load ICU data from other files. See gDataFileAccess = UDATA_NO_FILES in udata.cpp. |
| 719 | // log_err("FAIL: udata_openChoice() did not fail as expected. name=%s, type=%s, \n errorcode=%s\n", name[3], type, myErrorName(status)); |
| 720 | log_data_err("FAIL: udata_openChoice() did not fail as expected. name=%s, type=%s, \n errorcode=%s\n", name[3], type, myErrorName(status)); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
| 724 | static UBool U_CALLCONV |
| 725 | isAcceptable(void *context, |
| 726 | const char *type, const char *name, |
| 727 | const UDataInfo *pInfo){ |
| 728 | if( pInfo->size>=20 && |
| 729 | pInfo->isBigEndian==U_IS_BIG_ENDIAN && |
| 730 | pInfo->charsetFamily==U_CHARSET_FAMILY && |
| 731 | pInfo->dataFormat[0]==0x54 && /* dataFormat="test" */ |
| 732 | pInfo->dataFormat[1]==0x65 && |
| 733 | pInfo->dataFormat[2]==0x73 && |
| 734 | pInfo->dataFormat[3]==0x74 && |
| 735 | pInfo->formatVersion[0]==1 && |
| 736 | pInfo->dataVersion[0]==1 && |
| 737 | *((int*)context) == 2 ) { |
| 738 | log_verbose("The data from\"%s.%s\" IS acceptable using the verifing function isAcceptable()\n", name, type); |
| 739 | |
| 740 | return TRUE; |
| 741 | } else { |
| 742 | log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable()\n", name, type); |
| 743 | return FALSE; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | |
| 748 | /* This test checks to see if the isAcceptable function is being called correctly. */ |
| 749 | |
| 750 | static void TestUDataOpenChoiceDemo2() { |
| 751 | UDataMemory *result; |
| 752 | UErrorCode status=U_ZERO_ERROR; |
| 753 | int i; |
| 754 | int p=2; |
| 755 | |
| 756 | const char* name="test"; |
| 757 | const char* type="icu"; |
| 758 | const char* path = loadTestData(&status); |
| 759 | if(U_FAILURE(status)) { |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 760 | log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status)); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 761 | return; |
| 762 | } |
| 763 | |
| 764 | result=udata_openChoice(path, type, name, isAcceptable, &p, &status); |
| 765 | if(U_FAILURE(status)){ |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 766 | log_data_err("failed to load data at p=%s t=%s n=%s, isAcceptable", path, type, name); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 767 | } |
| 768 | if(U_SUCCESS(status) ) { |
| 769 | udata_close(result); |
| 770 | } |
| 771 | |
| 772 | p=0; |
| 773 | for(i=0;i<2; i++){ |
| 774 | result=udata_openChoice(path, type, name, isAcceptable, &p, &status); |
| 775 | if(p<2) { |
| 776 | if(U_FAILURE(status) && status==U_INVALID_FORMAT_ERROR){ |
| 777 | log_verbose("Loads the data but rejects it as expected %s\n", myErrorName(status)); |
| 778 | status=U_ZERO_ERROR; |
| 779 | p++; |
| 780 | } |
| 781 | else { |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 782 | log_data_err("FAIL: failed to either load the data or to reject the loaded data. ERROR=%s\n", myErrorName(status) ); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | else if(p == 2) { |
| 786 | if(U_FAILURE(status)) { |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 787 | log_data_err("FAIL: failed to load the data and accept it. ERROR=%s\n", myErrorName(status) ); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 788 | } |
| 789 | else { |
| 790 | log_verbose("Loads the data and accepts it for p==2 as expected\n"); |
| 791 | udata_close(result); |
| 792 | } |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 797 | static void TestUDataGetInfo() { |
| 798 | |
| 799 | UDataMemory *result; |
| 800 | /* UDataInfo cf. udata.h */ |
| 801 | static UDataInfo dataInfo={ |
| 802 | 30, /*sizeof(UDataInfo),*/ |
| 803 | 0, |
| 804 | |
| 805 | U_IS_BIG_ENDIAN, |
| 806 | U_CHARSET_FAMILY, |
| 807 | sizeof(UChar), |
| 808 | 0, |
| 809 | |
| 810 | {0x54, 0x65, 0x73, 0x74}, /* dataFormat="Test" */ |
| 811 | {9, 0, 0, 0}, /* formatVersion */ |
| 812 | {4, 0, 0, 0} /* dataVersion */ |
| 813 | }; |
| 814 | UErrorCode status=U_ZERO_ERROR; |
| 815 | const char* name="cnvalias"; |
| 816 | const char* name2="test"; |
| 817 | const char* type="icu"; |
| 818 | |
| 819 | const char* testPath=loadTestData(&status); |
| 820 | if(U_FAILURE(status)) { |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 821 | log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status)); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 822 | return; |
| 823 | } |
| 824 | |
| 825 | log_verbose("Testing udata_getInfo() for cnvalias.icu\n"); |
| 826 | result=udata_open(NULL, "icu", name, &status); |
| 827 | if(U_FAILURE(status)){ |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 828 | log_data_err("FAIL: udata_open() failed for path = NULL, name=%s, type=%s, \n errorcode=%s\n", name, type, myErrorName(status)); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 829 | return; |
| 830 | } |
| 831 | udata_getInfo(result, &dataInfo); |
| 832 | if(dataInfo.size==20 && dataInfo.size!=30 && |
| 833 | dataInfo.isBigEndian==U_IS_BIG_ENDIAN && |
| 834 | dataInfo.charsetFamily==U_CHARSET_FAMILY && |
| 835 | dataInfo.dataFormat[0]==0x43 && dataInfo.dataFormat[0]!=0x54 && /* dataFormat="CvAl" and not "Test". The values are set for cnvalias.dat*/ |
| 836 | dataInfo.dataFormat[1]==0x76 && dataInfo.dataFormat[1]!=0x65 && |
| 837 | dataInfo.dataFormat[2]==0x41 && dataInfo.dataFormat[2]!=0x73 && |
| 838 | dataInfo.dataFormat[3]==0x6c && dataInfo.dataFormat[3]!=0x74 && |
| 839 | dataInfo.formatVersion[0]!=9 && /*formatVersion is also set to the one for cnvalias*/ |
| 840 | dataInfo.dataVersion[0]!=4 && /*dataVersion*/ |
| 841 | dataInfo.dataVersion[1]!=0 ){ |
| 842 | log_verbose("PASS: udata_getInfo() filled in the right values\n"); |
| 843 | } else { |
| 844 | log_err("FAIL: udata_getInfo() filled in the wrong values\n"); |
| 845 | } |
| 846 | udata_close(result); |
| 847 | |
| 848 | |
| 849 | log_verbose("Testing udata_getInfo() for test.icu\n"); |
| 850 | result=udata_open(testPath, type, name2, &status); |
| 851 | if(U_FAILURE(status)) { |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 852 | log_data_err("FAIL: udata_open() failed for path=%s name2=%s, type=%s, \n errorcode=%s\n", testPath, name2, type, myErrorName(status)); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 853 | return; |
| 854 | } |
| 855 | udata_getInfo(result, &dataInfo); |
| 856 | if(dataInfo.size==20 && |
| 857 | dataInfo.isBigEndian==U_IS_BIG_ENDIAN && |
| 858 | dataInfo.charsetFamily==U_CHARSET_FAMILY && |
| 859 | dataInfo.dataFormat[0]==0x54 && /* dataFormat="Test". The values are set for test.dat*/ |
| 860 | dataInfo.dataFormat[1]==0x65 && |
| 861 | dataInfo.dataFormat[2]==0x73 && |
| 862 | dataInfo.dataFormat[3]==0x74 && |
| 863 | dataInfo.formatVersion[0]==1 && /*formatVersion is also set to the one for test*/ |
| 864 | dataInfo.dataVersion[0]==1 && /*dataVersion*/ |
| 865 | dataInfo.dataVersion[1]==0 ) |
| 866 | { |
| 867 | log_verbose("PASS: udata_getInfo() filled in the right values\n"); |
| 868 | } else { |
| 869 | log_err("FAIL: udata_getInfo() filled in the wrong values\n"); |
| 870 | } |
| 871 | udata_close(result); |
| 872 | } |
| 873 | |
| 874 | static void TestUDataGetMemory() { |
| 875 | |
| 876 | UDataMemory *result; |
| 877 | const int32_t *table=NULL; |
| 878 | uint16_t* intValue=0; |
| 879 | UErrorCode status=U_ZERO_ERROR; |
| 880 | const char* name="cnvalias"; |
| 881 | const char* type; |
| 882 | |
| 883 | const char* name2="test"; |
| 884 | |
| 885 | const char* testPath = loadTestData(&status); |
| 886 | if(U_FAILURE(status)) { |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 887 | log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status)); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 888 | return; |
| 889 | } |
| 890 | |
| 891 | type="icu"; |
| 892 | log_verbose("Testing udata_getMemory() for \"cnvalias.icu\"\n"); |
| 893 | result=udata_openChoice(NULL, type, name, isAcceptable1, NULL, &status); |
| 894 | if(U_FAILURE(status)){ |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 895 | log_data_err("FAIL: udata_openChoice() failed for name=%s, type=%s, \n errorcode=%s\n", name, type, myErrorName(status)); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 896 | return; |
| 897 | } |
| 898 | table=(const int32_t *)udata_getMemory(result); |
| 899 | |
| 900 | /* The alias table may list more converters than what's actually available now. [grhoten] */ |
| 901 | if(ucnv_countAvailable() > table[1]) /*???*/ |
| 902 | log_err("FAIL: udata_getMemory() failed ucnv_countAvailable returned = %d, expected = %d\n", ucnv_countAvailable(), table[1+2*(*table)]); |
| 903 | |
| 904 | udata_close(result); |
| 905 | |
| 906 | type="icu"; |
| 907 | log_verbose("Testing udata_getMemory for \"test.icu\"()\n"); |
| 908 | result=udata_openChoice(testPath, type, name2, isAcceptable3, NULL, &status); |
| 909 | if(U_FAILURE(status)){ |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 910 | log_data_err("FAIL: udata_openChoice() failed for path=%s name=%s, type=%s, \n errorcode=%s\n", testPath, name2, type, myErrorName(status)); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 911 | return; |
| 912 | } |
| 913 | intValue=(uint16_t *)udata_getMemory(result); |
| 914 | /*printf("%d ..... %s", *(intValue), intValue+1));*/ |
| 915 | if( *intValue != 2000 || strcmp((char*)(intValue+1), "YEAR") != 0 ) |
| 916 | log_err("FAIL: udata_getMemory() failed: intValue :- Expected:2000 Got:%d \n\tstringValue:- Expected:YEAR Got:%s\n", *intValue, (intValue+1)); |
| 917 | |
| 918 | udata_close(result); |
| 919 | |
| 920 | } |
| 921 | |
| 922 | static void TestErrorConditions(){ |
| 923 | |
| 924 | UDataMemory *result=NULL; |
| 925 | UErrorCode status=U_ZERO_ERROR; |
| 926 | uint16_t* intValue=0; |
| 927 | static UDataInfo dataInfo={ |
| 928 | 30, /*sizeof(UDataInfo),*/ |
| 929 | 0, |
| 930 | |
| 931 | U_IS_BIG_ENDIAN, |
| 932 | U_CHARSET_FAMILY, |
| 933 | sizeof(UChar), |
| 934 | 0, |
| 935 | |
| 936 | {0x54, 0x65, 0x73, 0x74}, /* dataFormat="Test" */ |
| 937 | {9, 0, 0, 0}, /* formatVersion */ |
| 938 | {4, 0, 0, 0} /* dataVersion */ |
| 939 | }; |
| 940 | |
| 941 | const char* name = "test"; |
| 942 | const char* type="icu"; |
| 943 | |
| 944 | const char *testPath = loadTestData(&status); |
| 945 | if(U_FAILURE(status)) { |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 946 | log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status)); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 947 | return; |
| 948 | } |
| 949 | |
| 950 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 951 | /*Try udata_open with status != U_ZERO_ERROR*/ |
| 952 | log_verbose("Testing udata_open() with status != U_ZERO_ERROR\n"); |
| 953 | result=udata_open(testPath, type, name, &status); |
| 954 | if(result != NULL){ |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 955 | 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); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 956 | udata_close(result); |
| 957 | |
| 958 | } else { |
| 959 | log_verbose("PASS: udata_open with errorCode != U_ZERO_ERROR failed as expected\n"); |
| 960 | } |
| 961 | |
| 962 | /*Try udata_open with data name=NULL*/ |
| 963 | log_verbose("Testing udata_open() with data name=NULL\n"); |
| 964 | status=U_ZERO_ERROR; |
| 965 | result=udata_open(testPath, type, NULL, &status); |
| 966 | if(U_FAILURE(status)){ |
| 967 | if(status != U_ILLEGAL_ARGUMENT_ERROR || result != NULL){ |
| 968 | log_err("FAIL: udata_open() with name=NULL should return NULL and errocode U_ILLEGAL_ARGUMENT_ERROR, GOT: errorcode=%s\n", myErrorName(status)); |
| 969 | }else{ |
| 970 | log_verbose("PASS: udata_open with name=NULL failed as expected and errorcode = %s as expected\n", myErrorName(status)); |
| 971 | } |
| 972 | }else{ |
| 973 | log_err("FAIL: udata_open() with data name=NULL is supposed to fail for path = %s, name=NULL type=%s errorcode=U_ZERO_ERROR \n", testPath, type); |
| 974 | udata_close(result); |
| 975 | } |
| 976 | |
| 977 | |
| 978 | /*Try udata_openChoice with status != U_ZERO_ERROR*/ |
| 979 | log_verbose("Testing udata_openChoice() with status != U_ZERO_ERROR\n"); |
| 980 | status=U_ILLEGAL_ARGUMENT_ERROR; |
| 981 | result=udata_openChoice(testPath, type, name, isAcceptable3, NULL, &status); |
| 982 | if(result != NULL){ |
| 983 | 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); |
| 984 | udata_close(result); |
| 985 | } else { |
| 986 | log_verbose("PASS: udata_openChoice() with errorCode != U_ZERO_ERROR failed as expected\n"); |
| 987 | } |
| 988 | |
| 989 | /*Try udata_open with data name=NULL*/ |
| 990 | log_verbose("Testing udata_openChoice() with data name=NULL\n"); |
| 991 | status=U_ZERO_ERROR; |
| 992 | result=udata_openChoice(testPath, type, NULL, isAcceptable3, NULL, &status); |
| 993 | if(U_FAILURE(status)){ |
| 994 | if(status != U_ILLEGAL_ARGUMENT_ERROR || result != NULL){ |
| 995 | log_err("FAIL: udata_openChoice() with name=NULL should return NULL and errocode U_ILLEGAL_ARGUMENT_ERROR, GOT: errorcode=%s\n", myErrorName(status)); |
| 996 | }else{ |
| 997 | log_verbose("PASS: udata_openChoice with name=NULL failed as expected and errorcode = %s as expected\n", myErrorName(status)); |
| 998 | } |
| 999 | }else{ |
| 1000 | 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); |
| 1001 | udata_close(result); |
| 1002 | } |
| 1003 | |
| 1004 | /*Try udata_getMemory with UDataMemory=NULL*/ |
| 1005 | log_verbose("Testing udata_getMemory with UDataMemory=NULL\n"); |
| 1006 | intValue=(uint16_t*)udata_getMemory(NULL); |
| 1007 | if(intValue != NULL){ |
| 1008 | log_err("FAIL: udata_getMemory with UDataMemory = NULL is supposed to fail\n"); |
| 1009 | } |
| 1010 | |
| 1011 | /*Try udata_getInfo with UDataMemory=NULL*/ |
| 1012 | status=U_ZERO_ERROR; |
| 1013 | udata_getInfo(NULL, &dataInfo); |
| 1014 | if(dataInfo.size != 0){ |
| 1015 | log_err("FAIL : udata_getInfo with UDataMemory = NULL us supposed to fail\n"); |
| 1016 | } |
| 1017 | |
| 1018 | /*Try udata_openChoice with a non existing binary file*/ |
| 1019 | log_verbose("Testing udata_openChoice() with a non existing binary file\n"); |
| 1020 | result=udata_openChoice(testPath, "tst", "nonexist", isAcceptable3, NULL, &status); |
| 1021 | if(status==U_FILE_ACCESS_ERROR){ |
| 1022 | log_verbose("Opening udata_openChoice with non-existing file handled correctly.\n"); |
| 1023 | status=U_ZERO_ERROR; |
| 1024 | } else { |
| 1025 | log_err("calling udata_open with non-existing file not handled correctly\n. Expected: U_FILE_ACCESS_ERROR, Got: %s\n", myErrorName(status)); |
| 1026 | if(U_SUCCESS(status)) { |
| 1027 | udata_close(result); |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | if(result != NULL){ |
| 1032 | log_err("calling udata_open with non-existing file didn't return a null value\n"); |
| 1033 | } else { |
| 1034 | log_verbose("calling udat_open with non-existing file returned null as expected\n"); |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | /* Test whether apps and ICU can each have their own root.res */ |
| 1039 | static void TestAppData() |
| 1040 | { |
| 1041 | UResourceBundle *icu, *app; |
| 1042 | UResourceBundle *tmp = NULL; |
| 1043 | UResourceBundle *tmp2 = NULL; |
| 1044 | |
| 1045 | const UChar *appString; |
| 1046 | const UChar *icuString; |
| 1047 | |
| 1048 | int32_t len; |
| 1049 | |
| 1050 | UErrorCode status = U_ZERO_ERROR; |
| 1051 | char testMsgBuf[256]; |
| 1052 | |
| 1053 | const char* testPath=loadTestData(&status); |
| 1054 | if(U_FAILURE(status)) { |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 1055 | log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status)); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1056 | return; |
| 1057 | } |
| 1058 | |
| 1059 | icu = ures_open(NULL, "root", &status); |
| 1060 | if(U_FAILURE(status)) |
| 1061 | { |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 1062 | log_data_err("%s:%d: Couldn't open root ICU bundle- %s", __FILE__, __LINE__, u_errorName(status)); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1063 | return; |
| 1064 | } |
| 1065 | /* log_info("Open icu root: %s size_%d\n", u_errorName(status), ures_getSize(icu)); */ |
| 1066 | status = U_ZERO_ERROR; |
| 1067 | |
| 1068 | app = ures_open(testPath, "root", &status); |
| 1069 | if(U_FAILURE(status)) |
| 1070 | { |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 1071 | log_data_err("%s:%d: Couldn't open app ICU bundle [%s]- %s", __FILE__, __LINE__, testPath, u_errorName(status)); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1072 | return; |
| 1073 | } |
| 1074 | /* log_info("Open app: %s, size %d\n", u_errorName(status), ures_getSize(app)); */ |
| 1075 | |
| 1076 | tmp = ures_getByKey(icu, "Version", tmp, &status); |
| 1077 | if(U_FAILURE(status)) |
| 1078 | { |
| 1079 | log_err("%s:%d: Couldn't get Version string from ICU root bundle- %s", __FILE__, __LINE__, u_errorName(status)); |
| 1080 | return; |
| 1081 | } |
| 1082 | |
| 1083 | icuString = ures_getString(tmp, &len, &status); |
| 1084 | if(U_FAILURE(status)) |
| 1085 | { |
| 1086 | log_err("%s:%d: Couldn't get string from Version string from ICU root bundle- %s", __FILE__, __LINE__, u_errorName(status)); |
| 1087 | return; |
| 1088 | } |
| 1089 | /* log_info("icuString=%p - %s\n", icuString, austrdup(icuString)); */ |
| 1090 | |
| 1091 | |
| 1092 | tmp2 = ures_getByKey(app, "Version", tmp2, &status); |
| 1093 | if(U_FAILURE(status)) |
| 1094 | { |
| 1095 | log_err("%s:%d: Couldn't get Version string from App root bundle- %s", __FILE__, __LINE__, u_errorName(status)); |
| 1096 | return; |
| 1097 | } |
| 1098 | |
| 1099 | appString = ures_getString(tmp2, &len, &status); |
| 1100 | if(U_FAILURE(status)) |
| 1101 | { |
| 1102 | log_err("%s:%d: Couldn't get string from Version string from App root bundle- %s", __FILE__, __LINE__, u_errorName(status)); |
| 1103 | return; |
| 1104 | } |
| 1105 | |
| 1106 | /* log_info("appString=%p - %s\n", appString, austrdup(appString)); */ |
| 1107 | |
| 1108 | |
| 1109 | if(!u_strcmp(icuString, appString)) |
| 1110 | { |
| 1111 | 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), |
| 1112 | austrdup(appString)); |
| 1113 | } |
| 1114 | else |
| 1115 | { |
| 1116 | log_verbose("%s:%d: appstr=%s, icustr=%s\n", __FILE__, |
| 1117 | __LINE__, u_austrcpy(testMsgBuf, appString), u_austrcpy(testMsgBuf, icuString)); |
| 1118 | } |
| 1119 | |
| 1120 | ures_close(tmp); |
| 1121 | ures_close(tmp2); |
| 1122 | ures_close(icu); |
| 1123 | ures_close(app); |
| 1124 | } |
Craig Cornelius | 54dcd9b | 2013-02-15 14:03:14 -0800 | [diff] [blame] | 1125 | #endif |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1126 | |
| 1127 | static void TestICUDataName() |
| 1128 | { |
| 1129 | UVersionInfo icuVersion; |
| 1130 | char expectDataName[20]; |
| 1131 | unsigned int expectLen = 8; |
| 1132 | |
| 1133 | char typeChar = '?'; |
| 1134 | |
| 1135 | /* Print out the version # we have .. */ |
| 1136 | log_verbose("utypes.h says U_ICUDATA_NAME = %s\n", U_ICUDATA_NAME); |
| 1137 | |
| 1138 | /* Build up the version # we expect to get */ |
| 1139 | u_getVersion(icuVersion); |
| 1140 | |
| 1141 | switch(U_CHARSET_FAMILY) |
| 1142 | { |
| 1143 | case U_ASCII_FAMILY: |
ccornelius | 59d709d | 2014-02-20 10:29:46 -0800 | [diff] [blame] | 1144 | switch((int)U_IS_BIG_ENDIAN) |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1145 | { |
| 1146 | case 1: |
| 1147 | typeChar = 'b'; |
| 1148 | break; |
| 1149 | case 0: |
| 1150 | typeChar = 'l'; |
| 1151 | break; |
| 1152 | default: |
| 1153 | log_err("Expected 1 or 0 for U_IS_BIG_ENDIAN, got %d!\n", (int)U_IS_BIG_ENDIAN); |
| 1154 | /* return; */ |
| 1155 | } |
| 1156 | break; |
| 1157 | case U_EBCDIC_FAMILY: |
| 1158 | typeChar = 'e'; |
| 1159 | break; |
| 1160 | } |
| 1161 | |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame] | 1162 | /* Only major number is needed. */ |
| 1163 | sprintf(expectDataName, "%s%d%c", |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1164 | "icudt", |
| 1165 | (int)icuVersion[0], |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1166 | typeChar); |
| 1167 | |
| 1168 | log_verbose("Expected: %s\n", expectDataName); |
| 1169 | if(uprv_strlen(expectDataName) != expectLen) |
| 1170 | { |
| 1171 | log_err("*Expected* length is wrong (test err?), should be %d is %d\n", |
| 1172 | expectLen, uprv_strlen(expectDataName)); |
| 1173 | } |
| 1174 | |
| 1175 | if(uprv_strlen(U_ICUDATA_NAME) != expectLen) |
| 1176 | { |
| 1177 | log_err("U_ICUDATA_NAME length should be %d is %d\n", |
| 1178 | expectLen, uprv_strlen(U_ICUDATA_NAME)); |
| 1179 | } |
| 1180 | |
| 1181 | if(uprv_strcmp(U_ICUDATA_NAME, expectDataName)) |
| 1182 | { |
| 1183 | log_err("U_ICUDATA_NAME should be %s but is %s\n", |
| 1184 | expectDataName, U_ICUDATA_NAME); |
| 1185 | } |
| 1186 | |
| 1187 | /* ICUDATA_NAME comes from the build system on *nix */ |
| 1188 | #ifdef ICUDATA_NAME |
| 1189 | if(uprv_strcmp(U_ICUDATA_NAME, ICUDATA_NAME)) |
| 1190 | { |
| 1191 | log_err("ICUDATA_NAME and U_ICUDATA_NAME don't match: " |
| 1192 | "ICUDATA_NAME=%s, U_ICUDATA_NAME=%s. Check configure.in, icudefs.mk.in, utypes.h...\n", ICUDATA_NAME, U_ICUDATA_NAME); |
| 1193 | } |
| 1194 | else |
| 1195 | { |
| 1196 | log_verbose("ICUDATA_NAME=%s (from icudefs.mk), U_ICUDATA_NAME=%s (from utypes.h)\n", ICUDATA_NAME, U_ICUDATA_NAME); |
| 1197 | } |
| 1198 | #endif |
| 1199 | |
| 1200 | } |
| 1201 | |
| 1202 | /* test data swapping ------------------------------------------------------- */ |
| 1203 | |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame] | 1204 | #if U_PLATFORM == U_PF_OS400 |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1205 | /* See comments in genccode.c on when this special implementation can be removed. */ |
| 1206 | static const struct { |
| 1207 | double bogus; |
| 1208 | const char *bytes; |
| 1209 | } gOffsetTOCAppDataItem1={ 0.0, /* alignment bytes */ |
| 1210 | "\x00\x14" /* sizeof(UDataInfo) *//* MappedData { */ |
| 1211 | "\xda" |
| 1212 | "\x27" /* } */ |
| 1213 | "\x00\x14" /* sizeof(UDataInfo) *//* UDataInfo { */ |
| 1214 | "\0\0" |
| 1215 | "\1" /* U_IS_BIG_ENDIAN */ |
| 1216 | "\1" /* U_CHARSET_FAMILY */ |
| 1217 | "\2" /* U_SIZEOF_WHAR_T */ |
| 1218 | "\0" |
| 1219 | "\x31\x31\x31\x31" |
| 1220 | "\0\0\0\0" |
| 1221 | "\0\0\0\0" /* } */ |
| 1222 | }; |
| 1223 | #else |
| 1224 | static const struct { |
| 1225 | double bogus; |
| 1226 | MappedData bytes1; |
| 1227 | UDataInfo bytes2; |
| 1228 | uint8_t bytes3; |
| 1229 | } gOffsetTOCAppDataItem1={ |
| 1230 | 0.0, /* alignment bytes */ |
| 1231 | { sizeof(UDataInfo), 0xda, 0x27 }, /* MappedData */ |
| 1232 | |
| 1233 | {sizeof(UDataInfo), |
| 1234 | 0, |
| 1235 | |
| 1236 | U_IS_BIG_ENDIAN, |
| 1237 | U_CHARSET_FAMILY, |
| 1238 | sizeof(UChar), |
| 1239 | 0, |
| 1240 | |
| 1241 | {0x31, 0x31, 0x31, 0x31}, /* dataFormat="1111" */ |
| 1242 | {0, 0, 0, 0}, /* formatVersion */ |
Nikita Iashchenko | 4c0e286 | 2019-11-05 16:38:00 +0000 | [diff] [blame] | 1243 | {0, 0, 0, 0}}, /* dataVersion */ |
| 1244 | 0 |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1245 | }; |
| 1246 | #endif |
| 1247 | |
| 1248 | static const UChar gOffsetTOCGarbage[] = { /* "I have been very naughty!" */ |
| 1249 | 0x49, 0x20, 0x68, 0x61, 0x76, 0x65, 0x20, 0x62, 0x65, 0x65, 0x6E, |
| 1250 | 0x20, 0x76, 0x65, 0x72, 0x79, 0x20, 0x6E, 0x61, 0x75, 0x67, 0x68, 0x74, 0x79, 0x21 |
| 1251 | }; |
| 1252 | |
| 1253 | /* Original source: icu/source/tools/genccode */ |
| 1254 | static const struct { |
| 1255 | uint16_t headerSize; |
| 1256 | uint8_t magic1, magic2; |
| 1257 | UDataInfo info; |
| 1258 | char padding[8]; |
| 1259 | uint32_t count, reserved; |
| 1260 | const struct { |
| 1261 | const char *const name; |
| 1262 | const void *const data; |
| 1263 | } toc[3]; |
| 1264 | } gOffsetTOCAppData_dat = { |
| 1265 | 32, /* headerSize */ |
| 1266 | 0xda, /* magic1, (see struct MappedData in udata.c) */ |
| 1267 | 0x27, /* magic2 */ |
| 1268 | { /*UDataInfo */ |
| 1269 | sizeof(UDataInfo), /* size */ |
| 1270 | 0, /* reserved */ |
| 1271 | U_IS_BIG_ENDIAN, |
| 1272 | U_CHARSET_FAMILY, |
| 1273 | sizeof(UChar), |
| 1274 | 0, /* reserved */ |
| 1275 | { /* data format identifier */ |
| 1276 | 0x54, 0x6f, 0x43, 0x50}, /* "ToCP" */ |
| 1277 | {1, 0, 0, 0}, /* format version major, minor, milli, micro */ |
| 1278 | {0, 0, 0, 0} /* dataVersion */ |
| 1279 | }, |
| 1280 | {0,0,0,0,0,0,0,0}, /* Padding[8] */ |
| 1281 | 3, /* count */ |
| 1282 | 0, /* Reserved */ |
| 1283 | { /* TOC structure */ |
| 1284 | { "OffsetTOCAppData/a/b", &gOffsetTOCAppDataItem1 }, |
| 1285 | { "OffsetTOCAppData/gOffsetTOCAppDataItem1", &gOffsetTOCAppDataItem1 }, |
| 1286 | { "OffsetTOCAppData/gOffsetTOCGarbage", &gOffsetTOCGarbage } |
| 1287 | } |
| 1288 | }; |
| 1289 | |
Craig Cornelius | 54dcd9b | 2013-02-15 14:03:14 -0800 | [diff] [blame] | 1290 | /* Unfortunately, dictionaries are in a C++ header */ |
| 1291 | U_CAPI int32_t U_EXPORT2 |
| 1292 | udict_swap(const UDataSwapper *ds, const void *inData, int32_t length, void *outData, UErrorCode *pErrorCode); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1293 | |
| 1294 | /* test cases for maximum data swapping code coverage */ |
| 1295 | static const struct { |
| 1296 | const char *name, *type; |
| 1297 | UDataSwapFn *swapFn; |
| 1298 | } swapCases[]={ |
| 1299 | /* resource bundles */ |
| 1300 | |
| 1301 | /* resource bundle with many data types */ |
| 1302 | {"*testtypes", "res", ures_swap}, |
| 1303 | /* resource bundle with collation data */ |
| 1304 | {"ja", "res", ures_swap}, |
| 1305 | /* resource bundle with options-only collation data */ |
| 1306 | {"ru", "res", ures_swap}, |
| 1307 | {"el", "res", ures_swap}, |
| 1308 | /* ICU's root */ |
| 1309 | {"root", "res", ures_swap}, |
| 1310 | /* Test a 32-bit key table. This is large. */ |
| 1311 | {"*testtable32", "res", ures_swap}, |
| 1312 | |
claireho | 50294ea | 2010-05-03 15:44:48 -0700 | [diff] [blame] | 1313 | /* ICU 4.2 resource bundle - data format 1.2 (little-endian ASCII) */ |
| 1314 | {"*old_l_testtypes", "res", ures_swap}, |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1315 | /* same for big-endian EBCDIC */ |
claireho | 50294ea | 2010-05-03 15:44:48 -0700 | [diff] [blame] | 1316 | {"*old_e_testtypes", "res", ures_swap}, |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1317 | |
| 1318 | #if !UCONFIG_NO_COLLATION |
| 1319 | /* standalone collation data files */ |
| 1320 | {"ucadata", "icu", ucol_swap}, |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 1321 | #if 0 |
| 1322 | /* Starting with ICU 53, the "inverse UCA" data is integrated into ucadata.icu. */ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1323 | {"invuca", "icu", ucol_swapInverseUCA}, |
| 1324 | #endif |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 1325 | #endif |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1326 | |
| 1327 | #if !UCONFIG_NO_LEGACY_CONVERSION |
| 1328 | /* conversion table files */ |
| 1329 | |
| 1330 | /* SBCS conversion table file without extension */ |
| 1331 | {"ibm-913_P100-2000", "cnv", ucnv_swap}, |
| 1332 | /* EBCDIC_STATEFUL conversion table file with extension */ |
| 1333 | {"ibm-1390_P110-2003", "cnv", ucnv_swap}, |
| 1334 | /* DBCS extension-only conversion table file */ |
| 1335 | {"ibm-16684_P110-2003", "cnv", ucnv_swap}, |
| 1336 | /* EUC-TW (3-byte) conversion table file without extension */ |
| 1337 | {"ibm-964_P110-1999", "cnv", ucnv_swap}, |
| 1338 | /* GB 18030 (4-byte) conversion table file without extension */ |
| 1339 | {"gb18030", "cnv", ucnv_swap}, |
| 1340 | /* MBCS conversion table file with extension */ |
| 1341 | {"*test4x", "cnv", ucnv_swap}, |
| 1342 | /* |
| 1343 | * MBCS conversion table file without extension, |
| 1344 | * to test swapping and preflighting of UTF-8-friendly mbcsIndex[]. |
| 1345 | */ |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 1346 | {"jisx-212", "cnv", ucnv_swap}, |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1347 | #endif |
| 1348 | |
| 1349 | #if !UCONFIG_NO_CONVERSION |
| 1350 | /* alias table */ |
| 1351 | {"cnvalias", "icu", ucnv_swapAliases}, |
| 1352 | #endif |
| 1353 | |
| 1354 | #if !UCONFIG_NO_IDNA |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1355 | {"rfc3491", "spp", usprep_swap}, |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1356 | #endif |
| 1357 | |
| 1358 | #if !UCONFIG_NO_BREAK_ITERATION |
| 1359 | {"char", "brk", ubrk_swap}, |
Craig Cornelius | 54dcd9b | 2013-02-15 14:03:14 -0800 | [diff] [blame] | 1360 | {"thaidict", "dict",udict_swap}, |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1361 | #endif |
| 1362 | |
claireho | b26ce3a | 2012-01-10 17:54:41 -0800 | [diff] [blame] | 1363 | #if 0 |
| 1364 | /* |
| 1365 | * Starting with ICU 4.8, the Unicode property (value) aliases data |
| 1366 | * is hardcoded in the ICU4C common library. |
| 1367 | * The swapper was moved to the toolutil library for swapping for ICU4J. |
| 1368 | */ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1369 | /* Unicode properties */ |
| 1370 | {"pnames", "icu", upname_swap}, |
claireho | b26ce3a | 2012-01-10 17:54:41 -0800 | [diff] [blame] | 1371 | #endif |
| 1372 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1373 | #if 0 |
| 1374 | /* |
| 1375 | * Starting with ICU4C 3.4, the core Unicode properties files |
| 1376 | * (uprops.icu, ucase.icu, ubidi.icu, unorm.icu) |
| 1377 | * are hardcoded in the common DLL and therefore not included |
| 1378 | * in the data package any more. |
| 1379 | * Their swapping code is moved from the common DLL to the icuswap tool so that |
| 1380 | * we need not jump through hoops (like adding snapshots of these files |
| 1381 | * to testdata) for code coverage in tests. |
| 1382 | * See Jitterbug 4497. |
claireho | 50294ea | 2010-05-03 15:44:48 -0700 | [diff] [blame] | 1383 | * |
ccornelius | f9878a2 | 2014-11-20 18:09:39 -0800 | [diff] [blame] | 1384 | * ICU4C 4.4 adds normalization data files again, e.g., nfkc.nrm. |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1385 | */ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1386 | {"uprops", "icu", uprops_swap}, |
| 1387 | {"ucase", "icu", ucase_swap}, |
| 1388 | {"ubidi", "icu", ubidi_swap}, |
| 1389 | #endif |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame] | 1390 | #if !UCONFIG_NO_NORMALIZATION && !UCONFIG_ONLY_COLLATION |
ccornelius | f9878a2 | 2014-11-20 18:09:39 -0800 | [diff] [blame] | 1391 | {"nfkc", "nrm", unorm2_swap}, |
| 1392 | #if !UCONFIG_NO_REGULAR_EXPRESSIONS |
| 1393 | {"confusables", "cfu", uspoof_swap}, /* spoof data missing without regex */ |
| 1394 | #endif |
| 1395 | |
claireho | 50294ea | 2010-05-03 15:44:48 -0700 | [diff] [blame] | 1396 | #endif |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1397 | {"unames", "icu", uchar_swapNames} |
claireho | b26ce3a | 2012-01-10 17:54:41 -0800 | [diff] [blame] | 1398 | /* the last item should not be #if'ed so that it can reliably omit the last comma */ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1399 | }; |
| 1400 | |
| 1401 | /* Large enough for the largest swappable data item. */ |
| 1402 | #define SWAP_BUFFER_SIZE 1800000 |
| 1403 | |
| 1404 | static void U_CALLCONV |
| 1405 | printError(void *context, const char *fmt, va_list args) { |
Nikita Iashchenko | 4c0e286 | 2019-11-05 16:38:00 +0000 | [diff] [blame] | 1406 | (void)context; // suppress compiler warnings about unused variable |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1407 | vlog_info("[swap] ", fmt, args); |
| 1408 | log_err("\n"); /* Register error */ |
| 1409 | } |
| 1410 | |
| 1411 | static void |
| 1412 | TestSwapCase(UDataMemory *pData, const char *name, |
| 1413 | UDataSwapFn *swapFn, |
| 1414 | uint8_t *buffer, uint8_t *buffer2) { |
| 1415 | UDataSwapper *ds; |
| 1416 | const void *inData, *inHeader; |
| 1417 | int32_t length, dataLength, length2, headerLength; |
| 1418 | |
| 1419 | UErrorCode errorCode; |
| 1420 | UErrorCode badStatus; |
| 1421 | |
| 1422 | UBool inEndian, oppositeEndian; |
| 1423 | uint8_t inCharset, oppositeCharset; |
| 1424 | |
| 1425 | /* First we check that swapFn handles failures as expected. */ |
| 1426 | errorCode = U_UNSUPPORTED_ERROR; |
| 1427 | length = swapFn(NULL, NULL, 0, buffer, &errorCode); |
| 1428 | if (length != 0 || errorCode != U_UNSUPPORTED_ERROR) { |
| 1429 | log_err("%s() did not fail as expected - %s\n", name, u_errorName(errorCode)); |
| 1430 | } |
| 1431 | errorCode = U_ZERO_ERROR; |
| 1432 | length = swapFn(NULL, NULL, 0, buffer, &errorCode); |
| 1433 | if (length != 0 || errorCode != U_ILLEGAL_ARGUMENT_ERROR) { |
| 1434 | log_err("%s() did not fail as expected with bad arguments - %s\n", name, u_errorName(errorCode)); |
| 1435 | } |
| 1436 | |
| 1437 | |
| 1438 | /* Continue with the rest of the tests. */ |
| 1439 | errorCode = U_ZERO_ERROR; |
| 1440 | inData=udata_getMemory(pData); |
| 1441 | |
| 1442 | /* |
| 1443 | * get the data length if possible, to verify that swapping and preflighting |
| 1444 | * handles the entire data |
| 1445 | */ |
| 1446 | dataLength=udata_getLength(pData); |
| 1447 | |
| 1448 | /* |
| 1449 | * get the header and its length |
| 1450 | * all of the swap implementation functions require the header to be included |
| 1451 | */ |
| 1452 | inHeader=udata_getRawMemory(pData); |
| 1453 | headerLength=(int32_t)((const char *)inData-(const char *)inHeader); |
| 1454 | |
| 1455 | /* first swap to opposite endianness but same charset family */ |
| 1456 | errorCode=U_ZERO_ERROR; |
| 1457 | ds=udata_openSwapperForInputData(inHeader, headerLength, |
| 1458 | !U_IS_BIG_ENDIAN, U_CHARSET_FAMILY, &errorCode); |
| 1459 | if(U_FAILURE(errorCode)) { |
| 1460 | log_err("udata_openSwapperForInputData(%s->!isBig+same charset) failed - %s\n", |
| 1461 | name, u_errorName(errorCode)); |
| 1462 | return; |
| 1463 | } |
| 1464 | |
| 1465 | inEndian=ds->inIsBigEndian; |
| 1466 | inCharset=ds->inCharset; |
| 1467 | |
| 1468 | oppositeEndian=!inEndian; |
| 1469 | oppositeCharset= inCharset==U_ASCII_FAMILY ? U_EBCDIC_FAMILY : U_ASCII_FAMILY; |
| 1470 | |
| 1471 | /* make this test work with data files that are built for a different platform */ |
| 1472 | if(inEndian!=U_IS_BIG_ENDIAN || inCharset!=U_CHARSET_FAMILY) { |
| 1473 | udata_closeSwapper(ds); |
| 1474 | ds=udata_openSwapper(inEndian, inCharset, oppositeEndian, inCharset, &errorCode); |
| 1475 | if(U_FAILURE(errorCode)) { |
| 1476 | log_err("udata_openSwapper(%s->!isBig+same charset) failed - %s\n", |
| 1477 | name, u_errorName(errorCode)); |
| 1478 | return; |
| 1479 | } |
| 1480 | } |
| 1481 | |
| 1482 | /* |
| 1483 | Check error checking of swappable data not specific to this swapper. |
| 1484 | This should always fail. |
| 1485 | */ |
| 1486 | badStatus = U_ZERO_ERROR; |
| 1487 | length=swapFn(ds, &gOffsetTOCAppData_dat, -1, NULL, &badStatus); |
| 1488 | if(badStatus != U_UNSUPPORTED_ERROR) { |
| 1489 | log_err("swapFn(%s->!isBig+same charset) unexpectedly succeeded on bad data - %s\n", |
| 1490 | name, u_errorName(errorCode)); |
| 1491 | udata_closeSwapper(ds); |
| 1492 | return; |
| 1493 | } |
| 1494 | |
| 1495 | /* Now allow errors to be printed */ |
| 1496 | ds->printError=printError; |
| 1497 | |
| 1498 | /* preflight the length */ |
| 1499 | length=swapFn(ds, inHeader, -1, NULL, &errorCode); |
| 1500 | if(U_FAILURE(errorCode)) { |
| 1501 | log_err("swapFn(preflight %s->!isBig+same charset) failed - %s\n", |
| 1502 | name, u_errorName(errorCode)); |
| 1503 | udata_closeSwapper(ds); |
| 1504 | return; |
| 1505 | } |
| 1506 | |
| 1507 | /* compare the preflighted length against the data length */ |
| 1508 | if(dataLength>=0 && (length+15)<(headerLength+dataLength)) { |
| 1509 | log_err("swapFn(preflight %s->!isBig+same charset) length too small: %d < data length %d\n", |
| 1510 | name, length, (headerLength+dataLength)); |
| 1511 | udata_closeSwapper(ds); |
| 1512 | return; |
| 1513 | } |
| 1514 | |
| 1515 | /* swap, not in-place */ |
| 1516 | length2=swapFn(ds, inHeader, length, buffer, &errorCode); |
| 1517 | udata_closeSwapper(ds); |
| 1518 | if(U_FAILURE(errorCode)) { |
| 1519 | log_err("swapFn(%s->!isBig+same charset) failed - %s\n", |
| 1520 | name, u_errorName(errorCode)); |
| 1521 | return; |
| 1522 | } |
| 1523 | |
| 1524 | /* compare the swap length against the preflighted length */ |
| 1525 | if(length2!=length) { |
| 1526 | log_err("swapFn(%s->!isBig+same charset) length differs from preflighting: %d != preflighted %d\n", |
| 1527 | name, length2, length); |
| 1528 | return; |
| 1529 | } |
| 1530 | |
| 1531 | /* next swap to opposite charset family */ |
| 1532 | ds=udata_openSwapper(oppositeEndian, inCharset, |
| 1533 | oppositeEndian, oppositeCharset, |
| 1534 | &errorCode); |
| 1535 | if(U_FAILURE(errorCode)) { |
| 1536 | log_err("udata_openSwapper(%s->!isBig+other charset) failed - %s\n", |
| 1537 | name, u_errorName(errorCode)); |
| 1538 | return; |
| 1539 | } |
| 1540 | ds->printError=printError; |
| 1541 | |
| 1542 | /* swap in-place */ |
| 1543 | length2=swapFn(ds, buffer, length, buffer, &errorCode); |
| 1544 | udata_closeSwapper(ds); |
| 1545 | if(U_FAILURE(errorCode)) { |
| 1546 | log_err("swapFn(%s->!isBig+other charset) failed - %s\n", |
| 1547 | name, u_errorName(errorCode)); |
| 1548 | return; |
| 1549 | } |
| 1550 | |
| 1551 | /* compare the swap length against the original length */ |
| 1552 | if(length2!=length) { |
| 1553 | log_err("swapFn(%s->!isBig+other charset) length differs from original: %d != original %d\n", |
| 1554 | name, length2, length); |
| 1555 | return; |
| 1556 | } |
| 1557 | |
| 1558 | /* finally swap to original platform values */ |
| 1559 | ds=udata_openSwapper(oppositeEndian, oppositeCharset, |
| 1560 | inEndian, inCharset, |
| 1561 | &errorCode); |
| 1562 | if(U_FAILURE(errorCode)) { |
| 1563 | log_err("udata_openSwapper(%s->back to original) failed - %s\n", |
| 1564 | name, u_errorName(errorCode)); |
| 1565 | return; |
| 1566 | } |
| 1567 | ds->printError=printError; |
| 1568 | |
| 1569 | /* swap, not in-place */ |
| 1570 | length2=swapFn(ds, buffer, length, buffer2, &errorCode); |
| 1571 | udata_closeSwapper(ds); |
| 1572 | if(U_FAILURE(errorCode)) { |
| 1573 | log_err("swapFn(%s->back to original) failed - %s\n", |
| 1574 | name, u_errorName(errorCode)); |
| 1575 | return; |
| 1576 | } |
| 1577 | |
| 1578 | /* compare the swap length against the original length */ |
| 1579 | if(length2!=length) { |
| 1580 | log_err("swapFn(%s->back to original) length differs from original: %d != original %d\n", |
| 1581 | name, length2, length); |
| 1582 | return; |
| 1583 | } |
| 1584 | |
| 1585 | /* compare the final contents with the original */ |
| 1586 | if(0!=uprv_memcmp(inHeader, buffer2, length)) { |
| 1587 | const uint8_t *original; |
| 1588 | uint8_t diff[8]; |
| 1589 | int32_t i, j; |
| 1590 | |
| 1591 | log_err("swapFn(%s->back to original) contents differs from original\n", |
| 1592 | name); |
| 1593 | |
| 1594 | /* find the first difference */ |
| 1595 | original=(const uint8_t *)inHeader; |
| 1596 | for(i=0; i<length && original[i]==buffer2[i]; ++i) {} |
| 1597 | |
| 1598 | /* find the next byte that is the same */ |
| 1599 | for(j=i+1; j<length && original[j]!=buffer2[j]; ++j) {} |
| 1600 | log_info(" difference at index %d=0x%x, until index %d=0x%x\n", i, i, j, j); |
| 1601 | |
| 1602 | /* round down to the last 4-boundary for better result output */ |
| 1603 | i&=~3; |
| 1604 | log_info("showing bytes from index %d=0x%x (length %d=0x%x):\n", i, i, length, length); |
| 1605 | |
| 1606 | /* print 8 bytes but limit to the buffer contents */ |
| 1607 | length2=i+sizeof(diff); |
| 1608 | if(length2>length) { |
| 1609 | length2=length; |
| 1610 | } |
| 1611 | |
| 1612 | /* print the original bytes */ |
| 1613 | uprv_memset(diff, 0, sizeof(diff)); |
| 1614 | for(j=i; j<length2; ++j) { |
| 1615 | diff[j-i]=original[j]; |
| 1616 | } |
| 1617 | log_info(" original: %02x %02x %02x %02x %02x %02x %02x %02x\n", |
| 1618 | diff[0], diff[1], diff[2], diff[3], diff[4], diff[5], diff[6], diff[7]); |
| 1619 | |
| 1620 | /* print the swapped bytes */ |
| 1621 | uprv_memset(diff, 0, sizeof(diff)); |
| 1622 | for(j=i; j<length2; ++j) { |
| 1623 | diff[j-i]=buffer2[j]; |
| 1624 | } |
| 1625 | log_info(" swapped: %02x %02x %02x %02x %02x %02x %02x %02x\n", |
| 1626 | diff[0], diff[1], diff[2], diff[3], diff[4], diff[5], diff[6], diff[7]); |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | static void U_CALLCONV |
| 1631 | printErrorToString(void *context, const char *fmt, va_list args) { |
| 1632 | vsprintf((char *)context, fmt, args); |
| 1633 | } |
| 1634 | |
Craig Cornelius | 54dcd9b | 2013-02-15 14:03:14 -0800 | [diff] [blame] | 1635 | #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1636 | static void |
| 1637 | TestSwapData() { |
| 1638 | char name[100]; |
| 1639 | UDataSwapper *ds; |
| 1640 | UDataMemory *pData; |
| 1641 | uint8_t *buffer; |
| 1642 | const char *pkg, *nm, *testPath; |
| 1643 | UErrorCode errorCode = U_ZERO_ERROR; |
| 1644 | int32_t i; |
| 1645 | |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 1646 | buffer=(uint8_t *)malloc(2*SWAP_BUFFER_SIZE); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1647 | if(buffer==NULL) { |
| 1648 | log_err("unable to allocate %d bytes\n", 2*SWAP_BUFFER_SIZE); |
| 1649 | return; |
| 1650 | } |
| 1651 | |
| 1652 | testPath=loadTestData(&errorCode); |
| 1653 | if(U_FAILURE(errorCode)) { |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 1654 | log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(errorCode)); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | /* Test that printError works as expected. */ |
| 1658 | errorCode=U_USELESS_COLLATOR_ERROR; |
| 1659 | ds=udata_openSwapper(U_IS_BIG_ENDIAN, U_ASCII_FAMILY, |
| 1660 | !U_IS_BIG_ENDIAN, U_ASCII_FAMILY, |
| 1661 | &errorCode); |
| 1662 | if (ds != NULL || errorCode != U_USELESS_COLLATOR_ERROR) { |
| 1663 | log_err("udata_openSwapper should have returned NULL with bad argument\n", name); |
| 1664 | } |
| 1665 | errorCode=U_ZERO_ERROR; |
| 1666 | ds=udata_openSwapper(U_IS_BIG_ENDIAN, U_ASCII_FAMILY, |
| 1667 | !U_IS_BIG_ENDIAN, U_ASCII_FAMILY, |
| 1668 | &errorCode); |
| 1669 | ds->printError=printErrorToString; |
| 1670 | ds->printErrorContext=name; |
| 1671 | udata_printError(ds, "This %s a %s", "is", "test"); |
| 1672 | udata_closeSwapper(ds); |
| 1673 | if (strcmp(name, "This is a test") != 0) { |
| 1674 | log_err("udata_printError can't properly print error messages. Got = %s\n", name); |
| 1675 | } |
| 1676 | errorCode = U_USELESS_COLLATOR_ERROR; |
| 1677 | ds=udata_openSwapperForInputData(NULL, 0, |
| 1678 | !U_IS_BIG_ENDIAN, U_ASCII_FAMILY, |
| 1679 | &errorCode); |
| 1680 | if (ds != NULL || errorCode != U_USELESS_COLLATOR_ERROR) { |
| 1681 | log_err("udata_openSwapperForInputData should have returned NULL with bad argument\n", name); |
| 1682 | } |
| 1683 | errorCode=U_ZERO_ERROR; |
| 1684 | ds=udata_openSwapperForInputData(NULL, 0, |
| 1685 | !U_IS_BIG_ENDIAN, U_ASCII_FAMILY, |
| 1686 | &errorCode); |
| 1687 | if (ds != NULL || errorCode != U_ILLEGAL_ARGUMENT_ERROR) { |
| 1688 | log_err("udata_openSwapperForInputData should have returned NULL with bad argument\n", name); |
| 1689 | } |
| 1690 | errorCode=U_ZERO_ERROR; |
| 1691 | memset(buffer, 0, sizeof(2*SWAP_BUFFER_SIZE)); |
| 1692 | ds=udata_openSwapperForInputData(buffer, 2*SWAP_BUFFER_SIZE, |
| 1693 | !U_IS_BIG_ENDIAN, U_ASCII_FAMILY, |
| 1694 | &errorCode); |
| 1695 | if (ds != NULL || errorCode != U_UNSUPPORTED_ERROR) { |
| 1696 | log_err("udata_openSwapperForInputData should have returned NULL with bad argument\n", name); |
| 1697 | } |
| 1698 | errorCode=U_ZERO_ERROR; |
| 1699 | |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 1700 | /* Test argument checking. ucol_swap is normally tested via ures_swap, and isn't normally called directly. */ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1701 | #if !UCONFIG_NO_COLLATION |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 1702 | ucol_swap(NULL, NULL, -1, NULL, &errorCode); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1703 | if (errorCode != U_ILLEGAL_ARGUMENT_ERROR) { |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 1704 | log_err("ucol_swap did not fail as expected\n", name); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1705 | } |
| 1706 | errorCode=U_ZERO_ERROR; |
| 1707 | #endif |
| 1708 | |
ccornelius | f9878a2 | 2014-11-20 18:09:39 -0800 | [diff] [blame] | 1709 | for(i=0; i<UPRV_LENGTHOF(swapCases); ++i) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1710 | /* build the name for logging */ |
| 1711 | errorCode=U_ZERO_ERROR; |
| 1712 | if(swapCases[i].name[0]=='*') { |
| 1713 | pkg=testPath; |
| 1714 | nm=swapCases[i].name+1; |
| 1715 | uprv_strcpy(name, "testdata"); |
| 1716 | } else if (uprv_strcmp(swapCases[i].type, "brk")==0 |
Craig Cornelius | 54dcd9b | 2013-02-15 14:03:14 -0800 | [diff] [blame] | 1717 | || uprv_strcmp(swapCases[i].type, "dict")==0) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1718 | pkg=U_ICUDATA_BRKITR; |
| 1719 | nm=swapCases[i].name; |
| 1720 | uprv_strcpy(name, U_ICUDATA_BRKITR); |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 1721 | #if !UCONFIG_NO_COLLATION |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1722 | } else if (uprv_strcmp(swapCases[i].name, "ucadata")==0 |
| 1723 | || uprv_strcmp(swapCases[i].name, "invuca")==0) { |
| 1724 | pkg=U_ICUDATA_COLL; |
| 1725 | nm=swapCases[i].name; |
| 1726 | uprv_strcpy(name, U_ICUDATA_COLL); |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 1727 | #endif /* !UCONFIG_NO_COLLATION */ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1728 | } else { |
| 1729 | pkg=NULL; |
| 1730 | nm=swapCases[i].name; |
| 1731 | uprv_strcpy(name, "NULL"); |
| 1732 | } |
| 1733 | uprv_strcat(name, "/"); |
| 1734 | uprv_strcat(name, nm); |
| 1735 | uprv_strcat(name, "."); |
| 1736 | uprv_strcat(name, swapCases[i].type); |
| 1737 | |
| 1738 | pData=udata_open(pkg, swapCases[i].type, nm, &errorCode); |
claireho | b26ce3a | 2012-01-10 17:54:41 -0800 | [diff] [blame] | 1739 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1740 | if(U_SUCCESS(errorCode)) { |
| 1741 | TestSwapCase(pData, name, swapCases[i].swapFn, buffer, buffer+SWAP_BUFFER_SIZE); |
| 1742 | udata_close(pData); |
| 1743 | } else { |
| 1744 | log_data_err("udata_open(%s) failed - %s\n", |
| 1745 | name, u_errorName(errorCode)); |
| 1746 | } |
| 1747 | } |
| 1748 | |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 1749 | free(buffer); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1750 | } |
Craig Cornelius | 54dcd9b | 2013-02-15 14:03:14 -0800 | [diff] [blame] | 1751 | #endif |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1752 | |
| 1753 | static void PointerTableOfContents() { |
| 1754 | UDataMemory *dataItem; |
| 1755 | UErrorCode status=U_ZERO_ERROR; |
| 1756 | |
| 1757 | /* |
| 1758 | * Got testdata.dat into memory, now we try setAppData using the memory image. |
| 1759 | */ |
| 1760 | |
| 1761 | status=U_ZERO_ERROR; |
| 1762 | udata_setAppData("OffsetTOCAppData", &gOffsetTOCAppData_dat, &status); |
| 1763 | if (status != U_ZERO_ERROR) { |
| 1764 | log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData1\", fileBuf, status) \n" |
| 1765 | " returned status of %s\n", u_errorName(status)); |
| 1766 | return; |
| 1767 | } |
| 1768 | |
| 1769 | dataItem = udata_open("OffsetTOCAppData", "", "gOffsetTOCAppDataItem1", &status); |
| 1770 | if (U_FAILURE(status)) { |
| 1771 | log_err("FAIL: gOffsetTOCAppDataItem1 could not be opened. status = %s\n", u_errorName(status)); |
| 1772 | } |
| 1773 | if (udata_getMemory(dataItem) != NULL) { |
| 1774 | log_verbose("FAIL: udata_getMemory(dataItem) passed\n"); |
| 1775 | } |
| 1776 | else { |
| 1777 | log_err("FAIL: udata_getMemory returned NULL\n", u_errorName(status)); |
| 1778 | } |
| 1779 | udata_close(dataItem); |
| 1780 | |
| 1781 | dataItem = udata_open("OffsetTOCAppData-a", "", "b", &status); |
| 1782 | if (U_FAILURE(status)) { |
| 1783 | log_err("FAIL: gOffsetTOCAppDataItem1 in tree \"a\" could not be opened. status = %s\n", u_errorName(status)); |
| 1784 | } |
| 1785 | if (udata_getMemory(dataItem) != NULL) { |
| 1786 | log_verbose("FAIL: udata_getMemory(dataItem) in tree \"a\" passed\n"); |
| 1787 | } |
| 1788 | else { |
| 1789 | log_err("FAIL: udata_getMemory returned NULL\n", u_errorName(status)); |
| 1790 | } |
| 1791 | udata_close(dataItem); |
| 1792 | |
| 1793 | dataItem = udata_open("OffsetTOCAppData", "", "gOffsetTOCGarbage", &status); |
| 1794 | if (U_SUCCESS(status)) { |
| 1795 | log_err("FAIL: gOffsetTOCGarbage should not be opened. status = %s\n", u_errorName(status)); |
| 1796 | } |
| 1797 | dataItem = udata_open("OffsetTOCAppData", "", "gOffsetTOCNonExistent", &status); |
| 1798 | if (U_SUCCESS(status)) { |
| 1799 | log_err("FAIL: gOffsetTOCNonExistent should not be found. status = %s\n", u_errorName(status)); |
| 1800 | } |
| 1801 | |
| 1802 | } |
| 1803 | |
| 1804 | static void SetBadCommonData(void) { |
| 1805 | /* It's difficult to test that udata_setCommonData really works within the test framework. |
| 1806 | So we just test that foolish people can't do bad things. */ |
| 1807 | UErrorCode status; |
| 1808 | char badBuffer[sizeof(gOffsetTOCAppData_dat)]; |
| 1809 | |
| 1810 | memset(badBuffer, 0, sizeof(badBuffer)); |
| 1811 | strcpy(badBuffer, "Hello! I'm not good data."); |
| 1812 | |
| 1813 | /* Check that we don't do anything */ |
| 1814 | status = U_FILE_ACCESS_ERROR; |
| 1815 | udata_setCommonData(&gOffsetTOCAppData_dat, &status); |
| 1816 | if (status != U_FILE_ACCESS_ERROR) { |
| 1817 | log_err("FAIL: udata_setCommonData changed the failure code.\n"); |
| 1818 | } |
| 1819 | /* Check that we fail correctly */ |
| 1820 | status = U_ZERO_ERROR; |
| 1821 | udata_setCommonData(NULL, &status); |
| 1822 | if (status != U_ILLEGAL_ARGUMENT_ERROR) { |
| 1823 | log_err("FAIL: udata_setCommonData did not fail with bad arguments.\n"); |
| 1824 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1825 | |
| 1826 | /* Check that we verify that the data isn't bad */ |
| 1827 | status = U_ZERO_ERROR; |
| 1828 | udata_setAppData("invalid path", badBuffer, &status); |
| 1829 | if (status != U_INVALID_FORMAT_ERROR) { |
| 1830 | log_err("FAIL: udata_setAppData doesn't verify data validity.\n"); |
| 1831 | } |
| 1832 | } |
| 1833 | |
ccornelius | f9878a2 | 2014-11-20 18:09:39 -0800 | [diff] [blame] | 1834 | // Check the override loading of time zone .res files from a specified path |
| 1835 | // |
| 1836 | // Hand testing notes: |
| 1837 | // 1. Run this test with the environment variable set. The following should induce faiures: |
| 1838 | // ICU_TIMEZONE_FILES_DIR=../testdata/out/build LD_LIBRARY_PATH=../../lib:../../stubdata:../../tools/ctestfw:$LD_LIBRARY_PATH ./cintltst /udatatst/TestTZDataDir |
| 1839 | // 2. Build ICU with with U_TIMEZONE_FILES_DIR defined. This should also induce failures. |
| 1840 | // CPPFLAGS=-DU_TIMEZONE_FILES_DIR\=`pwd`/test/testdata/out/testdata ./runConfigureICU Linux |
| 1841 | // make check |
| 1842 | |
| 1843 | static void TestTZDataDir(void) { |
| 1844 | #if !UCONFIG_NO_FORMATTING |
| 1845 | UErrorCode status = U_ZERO_ERROR; |
| 1846 | const char *tzDataVersion; |
| 1847 | const char *testDataPath; |
| 1848 | |
| 1849 | // Verify that default ICU time zone data version is something newer than 2014a. |
| 1850 | tzDataVersion = ucal_getTZDataVersion(&status); |
| 1851 | // printf("tz data version is %s\n", tzDataVersion); |
| 1852 | if (U_FAILURE(status)) { |
| 1853 | log_data_err("Failed call to ucal_getTZDataVersion - %s\n", u_errorName(status)); |
| 1854 | return; |
| 1855 | } else if (strcmp("2014a", tzDataVersion) == 0) { |
| 1856 | log_err("File %s:%d - expected something newer than time zone data 2014a.\n", __FILE__, __LINE__, tzDataVersion); |
| 1857 | } |
| 1858 | |
| 1859 | testDataPath = loadTestData(&status); |
| 1860 | // The path produced by loadTestData() will look something like |
| 1861 | // whatever/.../testdata/out/testdata |
| 1862 | // The test data puts an old (2014a) version of the time zone data there. |
| 1863 | |
| 1864 | // Switch ICU to the testdata version of zoneinfo64.res, which is verison 2014a. |
| 1865 | ctest_resetICU(); |
| 1866 | u_setTimeZoneFilesDirectory(testDataPath, &status); |
| 1867 | tzDataVersion = ucal_getTZDataVersion(&status); |
| 1868 | if (strcmp("2014a", tzDataVersion) != 0) { |
| 1869 | log_err("File %s:%d - expected \"2014a\"; actual \"%s\"\n", __FILE__, __LINE__, tzDataVersion); |
| 1870 | } |
| 1871 | |
| 1872 | ctest_resetICU(); // Return ICU to using its standard tz data. |
| 1873 | tzDataVersion = ucal_getTZDataVersion(&status); |
| 1874 | // printf("tz data version is %s\n", tzDataVersion); |
| 1875 | if (strcmp("2014a", tzDataVersion) == 0) { |
| 1876 | log_err("File %s:%d - expected something newer than time zone data 2014a.\n", __FILE__, __LINE__, tzDataVersion); |
| 1877 | } |
| 1878 | #endif |
| 1879 | } |