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