/* ctest_setICU_DATA - if the ICU_DATA environment variable is not already * set, try to deduce the directory in which ICU was built, * and set ICU_DATA to "icu/source/data" in that location. * The intent is to allow the tests to have a good chance * of running without requiring that the user manually set * ICU_DATA. Common data isn't a problem, since it is * picked up via a static (build time) reference, but the * tests dynamically load some data. */ static void ctest_setICU_DATA() { /* No location for the data dir was identifiable. * Add other fallbacks for the test data location here if the need arises */ if (getenv("ICU_DATA") == NULL) { /* If ICU_DATA isn't set, set it to the usual location */ u_setDataDirectory(ctest_dataOutDir()); } }
const char* loadTestData(UErrorCode* err){ if( _testDataPath == NULL){ const char* directory=NULL; UResourceBundle* test =NULL; char* tdpath=NULL; const char* tdrelativepath; #if defined (U_TOPBUILDDIR) tdrelativepath = "test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING"out"U_FILE_SEP_STRING; directory = U_TOPBUILDDIR; #else tdrelativepath = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING"test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING"out"U_FILE_SEP_STRING; directory= ctest_dataOutDir(); #endif tdpath = (char*) ctst_malloc(sizeof(char) *(( strlen(directory) * strlen(tdrelativepath)) + 10)); /* u_getDataDirectory shoul return \source\data ... set the * directory to ..\source\data\..\test\testdata\out\testdata * * Fallback: When Memory mapped file is built * ..\source\data\out\..\..\test\testdata\out\testdata */ strcpy(tdpath, directory); strcat(tdpath, tdrelativepath); strcat(tdpath,"testdata"); test=ures_open(tdpath, "testtypes", err); /* Fall back did not succeed either so return */ if(U_FAILURE(*err)){ *err = U_FILE_ACCESS_ERROR; log_data_err("Could not load testtypes.res in testdata bundle with path %s - %s\n", tdpath, u_errorName(*err)); return ""; } ures_close(test); _testDataPath = tdpath; return _testDataPath; } return _testDataPath; }
static void TestUDataOpen(){ UDataMemory *result; UErrorCode status=U_ZERO_ERROR; const char* memMap[][2]={ {"root", "res"}, {"unorm", "icu"}, {"cnvalias", "icu"}, {"unames", "icu"}, {"ibm-37_P100-1995", "cnv"} }; const char* name = "test"; const char* type = "icu"; const char dirSepString[] = {U_FILE_SEP_CHAR, 0}; const char pathSepString[] = {U_PATH_SEP_CHAR, 0}; char* path=(char*)malloc(sizeof(char) * (strlen(ctest_dataOutDir()) + strlen(U_ICUDATA_NAME) + strlen("/build")+1 ) ); char *icuDataFilePath = 0; struct stat stat_buf; const char* testPath=loadTestData(&status); /* lots_of_mallocs(); */ strcat(strcpy(path, ctest_dataOutDir()), U_ICUDATA_NAME); log_verbose("Testing udata_open()\n"); result=udata_open(testPath, type, name, &status); if(U_FAILURE(status)){ log_err("FAIL: udata_open() failed for path = %s, name=%s, type=%s, \n errorcode=%s\n", testPath, name, type, myErrorName(status)); } else { log_verbose("PASS: udata_open worked\n"); udata_close(result); } /* If the ICU system common data file is present in this confiugration, * verify that udata_open can explicitly fetch items from it. * If packaging mode == dll, the file may not exist. So, if the file is * missing, skip this test without error. */ icuDataFilePath = (char *)malloc(strlen(path) + 10); strcpy(icuDataFilePath, path); strcat(icuDataFilePath, ".dat"); /* lots_of_mallocs(); */ if (stat(icuDataFilePath, &stat_buf) == 0) { int i; log_verbose("Testing udata_open() on %s\n", icuDataFilePath); for(i=0; i<sizeof(memMap)/sizeof(memMap[0]); i++){ /* lots_of_mallocs(); */ status=U_ZERO_ERROR; result=udata_open(path, memMap[i][1], memMap[i][0], &status); if(U_FAILURE(status)) { 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)); } else { log_verbose("PASS: udata_open worked for path = %s, name=%s, type=%s\n", path, memMap[i][0], memMap[i][1]); udata_close(result); } } } else { /* lots_of_mallocs(); */ log_verbose("Skipping tests of udata_open() on %s. File not present in this configuration.\n", icuDataFilePath); } free(icuDataFilePath); icuDataFilePath = NULL; /* lots_of_mallocs(); */ /* If the ICU individual files used to build the ICU system common data are * present in this configuration, * verify that udata_open can explicitly open them. * These data files are present in the ICU data/build directory after a build * completes. Tests are most commonly run with the data directory pointing * back into this directory structure, but this is not required. Soooo, if * the files are missing, skip this test without error. */ /* lots_of_mallocs(); */ icuDataFilePath = (char *)malloc(strlen(ctest_dataOutDir()) + 50); strcpy(icuDataFilePath, ctest_dataOutDir()); strcat(icuDataFilePath, "build"); strcat(icuDataFilePath, dirSepString); strcat(icuDataFilePath, U_ICUDATA_NAME); strcat(icuDataFilePath, "_"); strcat(icuDataFilePath, "unorm.icu"); /* lots_of_mallocs(); */ /* if (stat(icuDataFilePath, &stat_buf) == 0)*/ { int i; log_verbose("%s exists, so..\n", icuDataFilePath); strcpy(icuDataFilePath, ctest_dataOutDir()); strcat(icuDataFilePath, "build"); strcat(icuDataFilePath, dirSepString); strcat(icuDataFilePath, U_ICUDATA_NAME); log_verbose("Testing udata_open() on %s\n", icuDataFilePath); for(i=0; i<sizeof(memMap)/sizeof(memMap[0]); i++){ status=U_ZERO_ERROR; result=udata_open(icuDataFilePath, memMap[i][1], memMap[i][0], &status); if(U_FAILURE(status)) { 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)); } else { log_verbose("PASS: udata_open worked for path = %s, name=%s, type=%s\n", icuDataFilePath, memMap[i][0], memMap[i][1]); udata_close(result); } } } /* else { log_verbose("Skipping tests of udata_open() on %s. File not present in this configuration.\n", icuDataFilePath); }*/ free(icuDataFilePath); icuDataFilePath = NULL; /* * Test fallback file names for open of separate data files. * With these params to udata_open: * path = wherever/testdata * type = typ * name = nam * these files will be tried first: * wherever/testudata_nam.typ * testudata_nam.typ * A test data file named testudata_nam.typ exists for the purpose of testing this. */ log_verbose("Testing udata_open, with base_name.type style fallback to individual file.\n"); status = U_ZERO_ERROR; result = udata_open( testPath, "typ", "nam", &status); if (status != U_ZERO_ERROR) { log_err("FAIL: udata_open( \"%s\", \"typ\", \"nam\") returned status %s\n", testPath, u_errorName(status)); } udata_close(result); free(icuDataFilePath); /* This type of path is deprecated */ /* * Another fallback test. Paths ending with a trailing directory separator * take a slightly different code path, with the "base name" from the path * being empty in the internal udata_open logic. */ /* log_verbose("Testing udata_open, with path containing a trailing directory separator.\n"); */ /* icuDataFilePath = (char *)malloc(strlen(u_getDataDirectory()) + 50); */ /* strcpy(icuDataFilePath, testPath); */ /* status = U_ZERO_ERROR; */ /* result = udata_open( icuDataFilePath, "cnv", "test1", &status); */ /* if (status != U_ZERO_ERROR) { */ /* log_err("FAIL: udata_open( \"%s\", \"cnv\", \"test1\") returned status %s\n", icuDataFilePath, u_errorName(status)); */ /* } */ /* udata_close(result); */ /* free(icuDataFilePath); */ log_verbose("Testing udata_open() with a non existing binary file\n"); result=udata_open("testdata", "tst", "nonexist", &status); if(status==U_FILE_ACCESS_ERROR){ log_verbose("Opening udata_open with non-existing file handled correctly.\n"); status=U_ZERO_ERROR; } else { 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)); if(U_SUCCESS(status)) { udata_close(result); } } if(result != NULL){ log_err("calling udata_open with non-existing file didn't return a null value\n"); } else { log_verbose("calling udat_open with non-existing file returned null as expected\n"); } /* * Try opening data with absurdly long path and name, to trigger buffer size * overflow handling code. */ { char longTestPath[1024]; /* Implementation goes to heap at length of 128. */ char longName[1024]; /* Try a very long nonexistent directory path. * udata_open should still succeed. Opening with the path will fail, * then fall back to skipping the directory portion of the path. */ log_verbose("Testing udata_open() with really long names\n"); longTestPath[0] = 0; strcat(longTestPath, "bogus_directory_name"); while (strlen(longTestPath) < 500) { strcat(longTestPath, dirSepString); strcat(longTestPath, "bogus_directory_name"); } strcat(longTestPath, pathSepString); strcat(longTestPath, testPath); result=udata_open(longTestPath, type, name, &status); if(U_FAILURE(status)){ log_err("FAIL: udata_open() failed for path = %s\n name=%s, type=%s, \n errorcode=%s\n", longTestPath, name, type, myErrorName(status)); } else { log_verbose("PASS: udata_open worked\n"); udata_close(result); } /* Try a very long name. Won't open, but shouldn't blow up. */ longName[0] = 0; while (strlen(longName) < 500) { strcat(longName, name); strcat(longName, "_"); } strcat(longName, dirSepString); strcat(longName, name); result=udata_open(longTestPath, type, longName, &status); if (status != U_FILE_ACCESS_ERROR) { log_err("FAIL: udata_open() failed for path = %s\n name=%s, type=%s, \n errorcode=%s\n", longTestPath, longName, type, myErrorName(status)); } udata_close(result); } free(path); }