1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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/. */
5 
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 PATH_SEPARATOR_CHAR L'\\'
14 // On Windows, argv[0] can also have forward slashes instead
15 #  define ALT_PATH_SEPARATOR_CHAR L'/'
16 #else
17 #  include <unistd.h>
18 #  define NS_main main
19 #  define PATH_SEPARATOR_CHAR '/'
20 #endif
21 
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <string.h>
25 
26 #include "updater/resource.h"
27 #include "updater/progressui.h"
28 #include "common/readstrings.h"
29 #include "common/updatererrors.h"
30 #include "common/updatedefines.h"
31 #include "mozilla/ArrayUtils.h"
32 
33 #ifndef MAXPATHLEN
34 #  ifdef PATH_MAX
35 #    define MAXPATHLEN PATH_MAX
36 #  elif defined(MAX_PATH)
37 #    define MAXPATHLEN MAX_PATH
38 #  elif defined(_MAX_PATH)
39 #    define MAXPATHLEN _MAX_PATH
40 #  elif defined(CCHMAXPATH)
41 #    define MAXPATHLEN CCHMAXPATH
42 #  else
43 #    define MAXPATHLEN 1024
44 #  endif
45 #endif
46 
47 #define TEST_NAME "Updater ReadStrings"
48 
49 using namespace mozilla;
50 
51 static int gFailCount = 0;
52 
53 /**
54  * Prints the given failure message and arguments using printf, prepending
55  * "TEST-UNEXPECTED-FAIL " for the benefit of the test harness and
56  * appending "\n" to eliminate having to type it at each call site.
57  */
fail(const char * msg,...)58 void fail(const char* msg, ...) {
59   va_list ap;
60 
61   printf("TEST-UNEXPECTED-FAIL | ");
62 
63   va_start(ap, msg);
64   vprintf(msg, ap);
65   va_end(ap);
66 
67   putchar('\n');
68   ++gFailCount;
69 }
70 
NS_main(int argc,NS_tchar ** argv)71 int NS_main(int argc, NS_tchar** argv) {
72   printf("Running TestAUSReadStrings tests\n");
73 
74   int rv = 0;
75   int retval;
76   NS_tchar inifile[MAXPATHLEN];
77   StringTable testStrings;
78 
79   NS_tchar* slash = NS_tstrrchr(argv[0], PATH_SEPARATOR_CHAR);
80 #ifdef ALT_PATH_SEPARATOR_CHAR
81   NS_tchar* altslash = NS_tstrrchr(argv[0], ALT_PATH_SEPARATOR_CHAR);
82   slash = (slash > altslash) ? slash : altslash;
83 #endif  // ALT_PATH_SEPARATOR_CHAR
84 
85   if (!slash) {
86     fail("%s | unable to find platform specific path separator (check 1)",
87          TEST_NAME);
88     return 20;
89   }
90 
91   *(++slash) = '\0';
92   // Test success when the ini file exists with both Title and Info in the
93   // Strings section and the values for Title and Info.
94   NS_tsnprintf(inifile, ArrayLength(inifile), NS_T("%sTestAUSReadStrings1.ini"),
95                argv[0]);
96   retval = ReadStrings(inifile, &testStrings);
97   if (retval == OK) {
98     if (strcmp(testStrings.title.get(),
99                "Title Test - \xD0\x98\xD1\x81\xD0\xBF\xD1\x8B"
100                "\xD1\x82\xD0\xB0\xD0\xBD\xD0\xB8\xD0\xB5 "
101                "\xCE\x94\xCE\xBF\xCE\xBA\xCE\xB9\xCE\xBC\xCE\xAE "
102                "\xE3\x83\x86\xE3\x82\xB9\xE3\x83\x88 "
103                "\xE6\xB8\xAC\xE8\xA9\xA6 "
104                "\xE6\xB5\x8B\xE8\xAF\x95") != 0) {
105       rv = 21;
106       fail("%s | Title ini value incorrect (check 3)", TEST_NAME);
107     }
108 
109     if (strcmp(testStrings.info.get(),
110                "Info Test - \xD0\x98\xD1\x81\xD0\xBF\xD1\x8B"
111                "\xD1\x82\xD0\xB0\xD0\xBD\xD0\xB8\xD0\xB5 "
112                "\xCE\x94\xCE\xBF\xCE\xBA\xCE\xB9\xCE\xBC\xCE\xAE "
113                "\xE3\x83\x86\xE3\x82\xB9\xE3\x83\x88 "
114                "\xE6\xB8\xAC\xE8\xA9\xA6 "
115                "\xE6\xB5\x8B\xE8\xAF\x95\xE2\x80\xA6") != 0) {
116       rv = 22;
117       fail("%s | Info ini value incorrect (check 4)", TEST_NAME);
118     }
119   } else {
120     fail("%s | ReadStrings returned %i (check 2)", TEST_NAME, retval);
121     rv = 23;
122   }
123 
124   // Test failure when the ini file exists without Title and with Info in the
125   // Strings section.
126   NS_tsnprintf(inifile, ArrayLength(inifile), NS_T("%sTestAUSReadStrings2.ini"),
127                argv[0]);
128   retval = ReadStrings(inifile, &testStrings);
129   if (retval != PARSE_ERROR) {
130     rv = 24;
131     fail("%s | ReadStrings returned %i (check 5)", TEST_NAME, retval);
132   }
133 
134   // Test failure when the ini file exists with Title and without Info in the
135   // Strings section.
136   NS_tsnprintf(inifile, ArrayLength(inifile), NS_T("%sTestAUSReadStrings3.ini"),
137                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   }
143 
144   // Test failure when the ini file doesn't exist
145   NS_tsnprintf(inifile, ArrayLength(inifile),
146                NS_T("%sTestAUSReadStringsBogus.ini"), argv[0]);
147   retval = ReadStrings(inifile, &testStrings);
148   if (retval != READ_ERROR) {
149     rv = 26;
150     fail("%s | ini file doesn't exist (check 7)", TEST_NAME);
151   }
152 
153   // Test reading a non-default section name
154   NS_tsnprintf(inifile, ArrayLength(inifile), NS_T("%sTestAUSReadStrings3.ini"),
155                argv[0]);
156   retval =
157       ReadStrings(inifile, "Title\0", 1, &testStrings.title, "BogusSection2");
158   if (retval == OK) {
159     if (strcmp(testStrings.title.get(), "Bogus Title") != 0) {
160       rv = 27;
161       fail("%s | Title ini value incorrect (check 9)", TEST_NAME);
162     }
163   } else {
164     fail("%s | ReadStrings returned %i (check 8)", TEST_NAME, retval);
165     rv = 28;
166   }
167 
168   // Test reading an exceedingly long string
169   NS_tsnprintf(inifile, ArrayLength(inifile), NS_T("%sTestAUSReadStrings4.ini"),
170                argv[0]);
171   retval = ReadStrings(inifile, "LongValue\0", 1, &testStrings.title);
172   const char* expectedValue =
173       "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam id "
174       "ipsum condimentum, faucibus ante porta, vehicula metus. Nunc nec luctus "
175       "lorem. Nunc mattis viverra nisl, eu ornare dui feugiat id. Aenean "
176       "commodo ligula porttitor elit aliquam, ut luctus nunc aliquam. In eu "
177       "eros at nunc pulvinar porta. Praesent porta felis vitae massa "
178       "sollicitudin, a vestibulum dolor rutrum. Aenean finibus, felis ac "
179       "dictum hendrerit, ligula arcu semper enim, rhoncus consequat arcu orci "
180       "nec est. Sed auctor hendrerit rhoncus. Maecenas dignissim lorem et "
181       "tellus maximus, sit amet pretium urna imperdiet. Duis ut libero "
182       "volutpat, rhoncus mi non, placerat lacus. Nunc id tortor in quam "
183       "lacinia luctus. Nam eu maximus ipsum, eu bibendum enim. Ut iaculis "
184       "maximus ipsum in condimentum. Aliquam tellus nulla, congue quis pretium "
185       "a, posuere quis ligula. Donec vel quam ipsum. Pellentesque congue urna "
186       "eget porttitor pulvinar. Proin non risus lacus. Vestibulum molestie et "
187       "ligula sit amet pellentesque. Phasellus luctus auctor lorem, vel "
188       "dapibus ante iaculis sed. Cras ligula ex, vehicula a dui vel, posuere "
189       "fermentum elit. Vestibulum et nisi at libero maximus interdum a non ex. "
190       "Ut ut leo in metus convallis porta a et libero. Pellentesque fringilla "
191       "dolor sit amet eleifend fermentum. Quisque blandit dolor facilisis "
192       "purus vulputate sodales eget ac arcu. Nulla pulvinar feugiat accumsan. "
193       "Phasellus auctor nisl eget diam auctor, sit amet imperdiet mauris "
194       "condimentum. In a risus ut felis lobortis facilisis.";
195   if (retval == OK) {
196     if (strcmp(testStrings.title.get(), expectedValue) != 0) {
197       rv = 29;
198       fail("%s | LongValue ini value incorrect (check 10)", TEST_NAME);
199     }
200   } else {
201     fail("%s | ReadStrings returned %i (check 11)", TEST_NAME, retval);
202     rv = 30;
203   }
204 
205   if (rv == 0) {
206     printf("TEST-PASS | %s | all checks passed\n", TEST_NAME);
207   } else {
208     fail("%s | %i out of 9 checks failed", TEST_NAME, gFailCount);
209   }
210 
211   return rv;
212 }
213