michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * This binary tests the updater's ReadStrings ini parser and should run in a michael@0: * directory with a Unicode character to test bug 473417. michael@0: */ michael@0: #ifdef XP_WIN michael@0: #include michael@0: #define NS_main wmain michael@0: #define NS_tstrrchr wcsrchr michael@0: #define NS_T(str) L ## str michael@0: #define PATH_SEPARATOR_CHAR L'\\' michael@0: // On Windows, argv[0] can also have forward slashes instead michael@0: #define ALT_PATH_SEPARATOR_CHAR L'/' michael@0: #else michael@0: #include michael@0: #define NS_main main michael@0: #define NS_tstrrchr strrchr michael@0: #define NS_T(str) str michael@0: #define PATH_SEPARATOR_CHAR '/' michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "updater/resource.h" michael@0: #include "updater/progressui.h" michael@0: #include "common/readstrings.h" michael@0: #include "common/errors.h" michael@0: #include "mozilla/ArrayUtils.h" michael@0: michael@0: #ifndef MAXPATHLEN michael@0: # ifdef PATH_MAX michael@0: # define MAXPATHLEN PATH_MAX michael@0: # elif defined(MAX_PATH) michael@0: # define MAXPATHLEN MAX_PATH michael@0: # elif defined(_MAX_PATH) michael@0: # define MAXPATHLEN _MAX_PATH michael@0: # elif defined(CCHMAXPATH) michael@0: # define MAXPATHLEN CCHMAXPATH michael@0: # else michael@0: # define MAXPATHLEN 1024 michael@0: # endif michael@0: #endif michael@0: michael@0: #define TEST_NAME "Updater ReadStrings" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: static int gFailCount = 0; michael@0: michael@0: /** michael@0: * Prints the given failure message and arguments using printf, prepending michael@0: * "TEST-UNEXPECTED-FAIL " for the benefit of the test harness and michael@0: * appending "\n" to eliminate having to type it at each call site. michael@0: */ michael@0: void fail(const char* msg, ...) michael@0: { michael@0: va_list ap; michael@0: michael@0: printf("TEST-UNEXPECTED-FAIL | "); michael@0: michael@0: va_start(ap, msg); michael@0: vprintf(msg, ap); michael@0: va_end(ap); michael@0: michael@0: putchar('\n'); michael@0: ++gFailCount; michael@0: } michael@0: michael@0: int NS_main(int argc, NS_tchar **argv) michael@0: { michael@0: printf("Running TestAUSReadStrings tests\n"); michael@0: michael@0: int rv = 0; michael@0: int retval; michael@0: NS_tchar inifile[MAXPATHLEN]; michael@0: StringTable testStrings; michael@0: michael@0: NS_tchar *slash = NS_tstrrchr(argv[0], PATH_SEPARATOR_CHAR); michael@0: #ifdef ALT_PATH_SEPARATOR_CHAR michael@0: NS_tchar *altslash = NS_tstrrchr(argv[0], ALT_PATH_SEPARATOR_CHAR); michael@0: slash = (slash > altslash) ? slash : altslash; michael@0: #endif // ALT_PATH_SEPARATOR_CHAR michael@0: michael@0: if (!slash) { michael@0: fail("%s | unable to find platform specific path separator (check 1)", TEST_NAME); michael@0: return 20; michael@0: } michael@0: michael@0: *(++slash) = '\0'; michael@0: // Test success when the ini file exists with both Title and Info in the michael@0: // Strings section and the values for Title and Info. michael@0: NS_tsnprintf(inifile, ArrayLength(inifile), NS_T("%sTestAUSReadStrings1.ini"), argv[0]); michael@0: retval = ReadStrings(inifile, &testStrings); michael@0: if (retval == OK) { michael@0: if (strcmp(testStrings.title, "Title Test - \xD0\x98\xD1\x81\xD0\xBF\xD1\x8B" \ michael@0: "\xD1\x82\xD0\xB0\xD0\xBD\xD0\xB8\xD0\xB5 " \ michael@0: "\xCE\x94\xCE\xBF\xCE\xBA\xCE\xB9\xCE\xBC\xCE\xAE " \ michael@0: "\xE3\x83\x86\xE3\x82\xB9\xE3\x83\x88 " \ michael@0: "\xE6\xB8\xAC\xE8\xA9\xA6 " \ michael@0: "\xE6\xB5\x8B\xE8\xAF\x95") != 0) { michael@0: rv = 21; michael@0: fail("%s | Title ini value incorrect (check 3)", TEST_NAME); michael@0: } michael@0: michael@0: if (strcmp(testStrings.info, "Info Test - \xD0\x98\xD1\x81\xD0\xBF\xD1\x8B" \ michael@0: "\xD1\x82\xD0\xB0\xD0\xBD\xD0\xB8\xD0\xB5 " \ michael@0: "\xCE\x94\xCE\xBF\xCE\xBA\xCE\xB9\xCE\xBC\xCE\xAE " \ michael@0: "\xE3\x83\x86\xE3\x82\xB9\xE3\x83\x88 " \ michael@0: "\xE6\xB8\xAC\xE8\xA9\xA6 " \ michael@0: "\xE6\xB5\x8B\xE8\xAF\x95\xE2\x80\xA6") != 0) { michael@0: rv = 22; michael@0: fail("%s | Info ini value incorrect (check 4)", TEST_NAME); michael@0: } michael@0: } michael@0: else { michael@0: fail("%s | ReadStrings returned %i (check 2)", TEST_NAME, retval); michael@0: rv = 23; michael@0: } michael@0: michael@0: // Test failure when the ini file exists without Title and with Info in the michael@0: // Strings section. michael@0: NS_tsnprintf(inifile, ArrayLength(inifile), NS_T("%sTestAUSReadStrings2.ini"), argv[0]); michael@0: retval = ReadStrings(inifile, &testStrings); michael@0: if (retval != PARSE_ERROR) { michael@0: rv = 24; michael@0: fail("%s | ReadStrings returned %i (check 5)", TEST_NAME, retval); michael@0: } michael@0: michael@0: // Test failure when the ini file exists with Title and without Info in the michael@0: // Strings section. michael@0: NS_tsnprintf(inifile, ArrayLength(inifile), NS_T("%sTestAUSReadStrings3.ini"), argv[0]); michael@0: retval = ReadStrings(inifile, &testStrings); michael@0: if (retval != PARSE_ERROR) { michael@0: rv = 25; michael@0: fail("%s | ReadStrings returned %i (check 6)", TEST_NAME, retval); michael@0: } michael@0: michael@0: // Test failure when the ini file doesn't exist michael@0: NS_tsnprintf(inifile, ArrayLength(inifile), NS_T("%sTestAUSReadStringsBogus.ini"), argv[0]); michael@0: retval = ReadStrings(inifile, &testStrings); michael@0: if (retval != READ_ERROR) { michael@0: rv = 26; michael@0: fail("%s | ini file doesn't exist (check 7)", TEST_NAME); michael@0: } michael@0: michael@0: // Test reading a non-default section name michael@0: NS_tsnprintf(inifile, ArrayLength(inifile), NS_T("%sTestAUSReadStrings3.ini"), argv[0]); michael@0: retval = ReadStrings(inifile, "Title\0", 1, &testStrings.title, "BogusSection2"); michael@0: if (retval == OK) { michael@0: if (strcmp(testStrings.title, "Bogus Title") != 0) { michael@0: rv = 27; michael@0: fail("%s | Title ini value incorrect (check 9)", TEST_NAME); michael@0: } michael@0: } michael@0: else { michael@0: fail("%s | ReadStrings returned %i (check 8)", TEST_NAME, retval); michael@0: rv = 28; michael@0: } michael@0: michael@0: michael@0: if (rv == 0) { michael@0: printf("TEST-PASS | %s | all checks passed\n", TEST_NAME); michael@0: } else { michael@0: fail("%s | %i out of 9 checks failed", TEST_NAME, gFailCount); michael@0: } michael@0: michael@0: return rv; michael@0: }