Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /** |
michael@0 | 7 | * This binary tests the updater's ReadStrings ini parser and should run in a |
michael@0 | 8 | * directory with a Unicode character to test bug 473417. |
michael@0 | 9 | */ |
michael@0 | 10 | #ifdef XP_WIN |
michael@0 | 11 | #include <windows.h> |
michael@0 | 12 | #define NS_main wmain |
michael@0 | 13 | #define NS_tstrrchr wcsrchr |
michael@0 | 14 | #define NS_T(str) L ## str |
michael@0 | 15 | #define PATH_SEPARATOR_CHAR L'\\' |
michael@0 | 16 | // On Windows, argv[0] can also have forward slashes instead |
michael@0 | 17 | #define ALT_PATH_SEPARATOR_CHAR L'/' |
michael@0 | 18 | #else |
michael@0 | 19 | #include <unistd.h> |
michael@0 | 20 | #define NS_main main |
michael@0 | 21 | #define NS_tstrrchr strrchr |
michael@0 | 22 | #define NS_T(str) str |
michael@0 | 23 | #define PATH_SEPARATOR_CHAR '/' |
michael@0 | 24 | #endif |
michael@0 | 25 | |
michael@0 | 26 | #include <stdio.h> |
michael@0 | 27 | #include <stdarg.h> |
michael@0 | 28 | #include <string.h> |
michael@0 | 29 | |
michael@0 | 30 | #include "updater/resource.h" |
michael@0 | 31 | #include "updater/progressui.h" |
michael@0 | 32 | #include "common/readstrings.h" |
michael@0 | 33 | #include "common/errors.h" |
michael@0 | 34 | #include "mozilla/ArrayUtils.h" |
michael@0 | 35 | |
michael@0 | 36 | #ifndef MAXPATHLEN |
michael@0 | 37 | # ifdef PATH_MAX |
michael@0 | 38 | # define MAXPATHLEN PATH_MAX |
michael@0 | 39 | # elif defined(MAX_PATH) |
michael@0 | 40 | # define MAXPATHLEN MAX_PATH |
michael@0 | 41 | # elif defined(_MAX_PATH) |
michael@0 | 42 | # define MAXPATHLEN _MAX_PATH |
michael@0 | 43 | # elif defined(CCHMAXPATH) |
michael@0 | 44 | # define MAXPATHLEN CCHMAXPATH |
michael@0 | 45 | # else |
michael@0 | 46 | # define MAXPATHLEN 1024 |
michael@0 | 47 | # endif |
michael@0 | 48 | #endif |
michael@0 | 49 | |
michael@0 | 50 | #define TEST_NAME "Updater ReadStrings" |
michael@0 | 51 | |
michael@0 | 52 | using namespace mozilla; |
michael@0 | 53 | |
michael@0 | 54 | static int gFailCount = 0; |
michael@0 | 55 | |
michael@0 | 56 | /** |
michael@0 | 57 | * Prints the given failure message and arguments using printf, prepending |
michael@0 | 58 | * "TEST-UNEXPECTED-FAIL " for the benefit of the test harness and |
michael@0 | 59 | * appending "\n" to eliminate having to type it at each call site. |
michael@0 | 60 | */ |
michael@0 | 61 | void fail(const char* msg, ...) |
michael@0 | 62 | { |
michael@0 | 63 | va_list ap; |
michael@0 | 64 | |
michael@0 | 65 | printf("TEST-UNEXPECTED-FAIL | "); |
michael@0 | 66 | |
michael@0 | 67 | va_start(ap, msg); |
michael@0 | 68 | vprintf(msg, ap); |
michael@0 | 69 | va_end(ap); |
michael@0 | 70 | |
michael@0 | 71 | putchar('\n'); |
michael@0 | 72 | ++gFailCount; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | int NS_main(int argc, NS_tchar **argv) |
michael@0 | 76 | { |
michael@0 | 77 | printf("Running TestAUSReadStrings tests\n"); |
michael@0 | 78 | |
michael@0 | 79 | int rv = 0; |
michael@0 | 80 | int retval; |
michael@0 | 81 | NS_tchar inifile[MAXPATHLEN]; |
michael@0 | 82 | StringTable testStrings; |
michael@0 | 83 | |
michael@0 | 84 | NS_tchar *slash = NS_tstrrchr(argv[0], PATH_SEPARATOR_CHAR); |
michael@0 | 85 | #ifdef ALT_PATH_SEPARATOR_CHAR |
michael@0 | 86 | NS_tchar *altslash = NS_tstrrchr(argv[0], ALT_PATH_SEPARATOR_CHAR); |
michael@0 | 87 | slash = (slash > altslash) ? slash : altslash; |
michael@0 | 88 | #endif // ALT_PATH_SEPARATOR_CHAR |
michael@0 | 89 | |
michael@0 | 90 | if (!slash) { |
michael@0 | 91 | fail("%s | unable to find platform specific path separator (check 1)", TEST_NAME); |
michael@0 | 92 | return 20; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | *(++slash) = '\0'; |
michael@0 | 96 | // Test success when the ini file exists with both Title and Info in the |
michael@0 | 97 | // Strings section and the values for Title and Info. |
michael@0 | 98 | NS_tsnprintf(inifile, ArrayLength(inifile), NS_T("%sTestAUSReadStrings1.ini"), argv[0]); |
michael@0 | 99 | retval = ReadStrings(inifile, &testStrings); |
michael@0 | 100 | if (retval == OK) { |
michael@0 | 101 | if (strcmp(testStrings.title, "Title Test - \xD0\x98\xD1\x81\xD0\xBF\xD1\x8B" \ |
michael@0 | 102 | "\xD1\x82\xD0\xB0\xD0\xBD\xD0\xB8\xD0\xB5 " \ |
michael@0 | 103 | "\xCE\x94\xCE\xBF\xCE\xBA\xCE\xB9\xCE\xBC\xCE\xAE " \ |
michael@0 | 104 | "\xE3\x83\x86\xE3\x82\xB9\xE3\x83\x88 " \ |
michael@0 | 105 | "\xE6\xB8\xAC\xE8\xA9\xA6 " \ |
michael@0 | 106 | "\xE6\xB5\x8B\xE8\xAF\x95") != 0) { |
michael@0 | 107 | rv = 21; |
michael@0 | 108 | fail("%s | Title ini value incorrect (check 3)", TEST_NAME); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | if (strcmp(testStrings.info, "Info Test - \xD0\x98\xD1\x81\xD0\xBF\xD1\x8B" \ |
michael@0 | 112 | "\xD1\x82\xD0\xB0\xD0\xBD\xD0\xB8\xD0\xB5 " \ |
michael@0 | 113 | "\xCE\x94\xCE\xBF\xCE\xBA\xCE\xB9\xCE\xBC\xCE\xAE " \ |
michael@0 | 114 | "\xE3\x83\x86\xE3\x82\xB9\xE3\x83\x88 " \ |
michael@0 | 115 | "\xE6\xB8\xAC\xE8\xA9\xA6 " \ |
michael@0 | 116 | "\xE6\xB5\x8B\xE8\xAF\x95\xE2\x80\xA6") != 0) { |
michael@0 | 117 | rv = 22; |
michael@0 | 118 | fail("%s | Info ini value incorrect (check 4)", TEST_NAME); |
michael@0 | 119 | } |
michael@0 | 120 | } |
michael@0 | 121 | else { |
michael@0 | 122 | fail("%s | ReadStrings returned %i (check 2)", TEST_NAME, retval); |
michael@0 | 123 | rv = 23; |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | // Test failure when the ini file exists without Title and with Info in the |
michael@0 | 127 | // Strings section. |
michael@0 | 128 | NS_tsnprintf(inifile, ArrayLength(inifile), NS_T("%sTestAUSReadStrings2.ini"), argv[0]); |
michael@0 | 129 | retval = ReadStrings(inifile, &testStrings); |
michael@0 | 130 | if (retval != PARSE_ERROR) { |
michael@0 | 131 | rv = 24; |
michael@0 | 132 | fail("%s | ReadStrings returned %i (check 5)", TEST_NAME, retval); |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | // Test failure when the ini file exists with Title and without Info in the |
michael@0 | 136 | // Strings section. |
michael@0 | 137 | NS_tsnprintf(inifile, ArrayLength(inifile), NS_T("%sTestAUSReadStrings3.ini"), argv[0]); |
michael@0 | 138 | retval = ReadStrings(inifile, &testStrings); |
michael@0 | 139 | if (retval != PARSE_ERROR) { |
michael@0 | 140 | rv = 25; |
michael@0 | 141 | fail("%s | ReadStrings returned %i (check 6)", TEST_NAME, retval); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | // Test failure when the ini file doesn't exist |
michael@0 | 145 | NS_tsnprintf(inifile, ArrayLength(inifile), NS_T("%sTestAUSReadStringsBogus.ini"), argv[0]); |
michael@0 | 146 | retval = ReadStrings(inifile, &testStrings); |
michael@0 | 147 | if (retval != READ_ERROR) { |
michael@0 | 148 | rv = 26; |
michael@0 | 149 | fail("%s | ini file doesn't exist (check 7)", TEST_NAME); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | // Test reading a non-default section name |
michael@0 | 153 | NS_tsnprintf(inifile, ArrayLength(inifile), NS_T("%sTestAUSReadStrings3.ini"), argv[0]); |
michael@0 | 154 | retval = ReadStrings(inifile, "Title\0", 1, &testStrings.title, "BogusSection2"); |
michael@0 | 155 | if (retval == OK) { |
michael@0 | 156 | if (strcmp(testStrings.title, "Bogus Title") != 0) { |
michael@0 | 157 | rv = 27; |
michael@0 | 158 | fail("%s | Title ini value incorrect (check 9)", TEST_NAME); |
michael@0 | 159 | } |
michael@0 | 160 | } |
michael@0 | 161 | else { |
michael@0 | 162 | fail("%s | ReadStrings returned %i (check 8)", TEST_NAME, retval); |
michael@0 | 163 | rv = 28; |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | |
michael@0 | 167 | if (rv == 0) { |
michael@0 | 168 | printf("TEST-PASS | %s | all checks passed\n", TEST_NAME); |
michael@0 | 169 | } else { |
michael@0 | 170 | fail("%s | %i out of 9 checks failed", TEST_NAME, gFailCount); |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | return rv; |
michael@0 | 174 | } |