/src/httrack/src/htsopt_selftest.c
Line | Count | Source |
1 | | /* ------------------------------------------------------------ */ |
2 | | /* |
3 | | HTTrack Website Copier, Offline Browser for Windows and Unix |
4 | | Copyright (C) 2026 Xavier Roche and other contributors |
5 | | |
6 | | SPDX-License-Identifier: GPL-3.0-or-later |
7 | | |
8 | | This program is free software: you can redistribute it and/or modify |
9 | | it under the terms of the GNU General Public License as published by |
10 | | the Free Software Foundation, either version 3 of the License, or |
11 | | (at your option) any later version. |
12 | | |
13 | | This program is distributed in the hope that it will be useful, |
14 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | GNU General Public License for more details. |
17 | | |
18 | | You should have received a copy of the GNU General Public License |
19 | | along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | | |
21 | | Ethical use: we kindly ask that you NOT use this software to harvest email |
22 | | addresses or to collect any other private information about people. Doing so |
23 | | would dishonor our work and waste the many hours we have spent on it. |
24 | | |
25 | | Please visit our Website: http://www.httrack.com |
26 | | */ |
27 | | |
28 | | /* ------------------------------------------------------------ */ |
29 | | /* File: htsopt_selftest.c subroutines: */ |
30 | | /* self-tests for options, aliases and the log callback */ |
31 | | /* Author: Xavier Roche */ |
32 | | /* ------------------------------------------------------------ */ |
33 | | |
34 | | #include "htsselftest_int.h" |
35 | | #include "htsmirror.h" |
36 | | #include <limits.h> |
37 | | #include <stdint.h> |
38 | | #ifdef __linux__ |
39 | | #include <sys/prctl.h> |
40 | | #endif |
41 | | |
42 | | /* What the build's optional features came out as, so a test gates on the binary |
43 | | rather than on the platform it guessed from. */ |
44 | 0 | static int st_features(httrackp *opt, int argc, char **argv) { |
45 | 0 | printf("brotli %d\n", HTS_USEBROTLI ? 1 : 0); |
46 | 0 | printf("zstd %d\n", HTS_USEZSTD ? 1 : 0); |
47 | 0 | #if (defined(__linux) && defined(HAVE_BACKTRACE)) |
48 | 0 | printf("backtrace 1\n"); |
49 | | #else |
50 | | printf("backtrace 0\n"); |
51 | | #endif |
52 | | #if (defined(HTS_USEICONV) && (HTS_USEICONV == 0)) |
53 | | printf("iconv 0\n"); |
54 | | #else |
55 | 0 | printf("iconv 1\n"); |
56 | 0 | #endif |
57 | 0 | #ifdef HTS_CRASH_TEST |
58 | 0 | printf("crashtest 1\n"); |
59 | | #else |
60 | | printf("crashtest 0\n"); |
61 | | #endif |
62 | | /* The kernel, not the build, decides whether MPTCP can be opened. */ |
63 | 0 | printf("mptcp %d\n", hts_mptcp_available() ? 1 : 0); |
64 | 0 | printf("mptcpinfo %d\n", hts_mptcp_reports() ? 1 : 0); |
65 | 0 | return 0; |
66 | 0 | } |
67 | | |
68 | | // -#test=footerfmt <template>: expand a -%F footer with fixed values (drives |
69 | | // tests/01_engine-footerfmt.test). Also asserts the published field names and |
70 | | // the overflow/zero-size returns the CLI cap keeps out of reach. |
71 | 0 | static int st_footerfmt(httrackp *opt, int argc, char **argv) { |
72 | | // Spelled out, not read back from the engine: these ten are the published |
73 | | // contract front ends validate their templates against. |
74 | 0 | static const char *const names[] = { |
75 | 0 | "addr", "path", "url", "date", "lastmodified", |
76 | 0 | "version", "mime", "charset", "status", "size"}; |
77 | | // Filled by id, as htsparse.c does, so the .test's expected strings pin each |
78 | | // name to its enum slot; a positional list would not see them drift apart. |
79 | 0 | const char *values[HTS_FOOTER_FIELD_COUNT] = {NULL}; |
80 | 0 | size_t i; |
81 | 0 | char out[1024]; |
82 | 0 | char tiny[4]; |
83 | |
|
84 | 0 | values[HTS_FOOTER_ADDR] = "host.example"; |
85 | 0 | values[HTS_FOOTER_PATH] = "/dir/page.html"; |
86 | 0 | values[HTS_FOOTER_URL] = "http://host.example/dir/page.html"; |
87 | 0 | values[HTS_FOOTER_DATE] = "DATE"; |
88 | 0 | values[HTS_FOOTER_LASTMODIFIED] = "LASTMOD"; |
89 | 0 | values[HTS_FOOTER_VERSION] = "VER"; |
90 | 0 | values[HTS_FOOTER_MIME] = "text/html"; |
91 | 0 | values[HTS_FOOTER_CHARSET] = "utf-8"; |
92 | 0 | values[HTS_FOOTER_STATUS] = "200"; |
93 | 0 | values[HTS_FOOTER_SIZE] = "1234"; |
94 | |
|
95 | 0 | for (i = 0; i < sizeof(names) / sizeof(names[0]); i++) { |
96 | 0 | assertf(hts_footer_field_ok(names[i]) == HTS_TRUE); |
97 | 0 | } |
98 | 0 | assertf(!hts_footer_field_ok(NULL)); |
99 | 0 | assertf(!hts_footer_field_ok("")); |
100 | 0 | assertf(!hts_footer_field_ok("nosuchfield")); |
101 | | // A prefix or a longer name must not match, or "{addr}" and "{addrx}" would |
102 | | // validate alike. |
103 | 0 | assertf(!hts_footer_field_ok("add")); |
104 | 0 | assertf(!hts_footer_field_ok("addrx")); |
105 | | // The expander is case-sensitive and would emit "{ADDR}" verbatim. |
106 | 0 | assertf(!hts_footer_field_ok("Addr")); |
107 | | // Overflow (named and legacy) and a zero-size buffer must return <0, never |
108 | | // truncate silently or write out of bounds. |
109 | 0 | assertf(hts_footer_format(HTS_BUF(tiny), "{addr}", values) < 0); |
110 | 0 | assertf(hts_footer_format(HTS_BUF(tiny), "a %s b", values) < 0); |
111 | 0 | assertf(hts_footer_format(HTS_BUF_N(out, 0), "", values) < 0); |
112 | | // An exact fit succeeds on both paths. |
113 | 0 | assertf(hts_footer_format(HTS_BUF(tiny), "{status}", values) == 1 && |
114 | 0 | strcmp(tiny, "200") == 0); |
115 | 0 | assertf(hts_footer_format(HTS_BUF_N(out, 13), "%s", values) >= 0 && |
116 | 0 | strcmp(out, "host.example") == 0); |
117 | | // An empty template yields an empty, terminated string. |
118 | 0 | assertf(hts_footer_format(HTS_BUF(out), "", values) == 1 && out[0] == '\0'); |
119 | 0 | if (hts_footer_format(HTS_BUF(out), argv[0], values) < 0) { |
120 | 0 | fprintf(stderr, "footerfmt: overflow\n"); |
121 | 0 | return 1; |
122 | 0 | } |
123 | 0 | printf("%s\n", out); |
124 | 0 | return 0; |
125 | 0 | } |
126 | | |
127 | | // hts_split_cmdline(): the vector must grow with the argument count, and a |
128 | | // quote inside a value must not end the argument and hand -V to the parser. |
129 | 0 | static int st_cmdlinesplit(httrackp *opt, int argc, char **argv) { |
130 | 0 | char line[512]; |
131 | 0 | char **args; |
132 | 0 | int nargs = 0; |
133 | | |
134 | | // control: every separator splits, and argv[0] is the program name |
135 | 0 | strcpybuff(line, "httrack http://x/ --quiet\t-c8\n-O out"); |
136 | 0 | args = hts_split_cmdline(line, &nargs); |
137 | 0 | assertf(args != NULL && nargs == 6); |
138 | 0 | assertf(args[nargs] == NULL); // callers may walk to the terminator |
139 | 0 | assertf(strcmp(args[0], "httrack") == 0); |
140 | 0 | assertf(strcmp(args[1], "http://x/") == 0); |
141 | 0 | assertf(strcmp(args[2], "--quiet") == 0); |
142 | 0 | assertf(strcmp(args[3], "-c8") == 0); |
143 | 0 | assertf(strcmp(args[4], "-O") == 0); |
144 | 0 | assertf(strcmp(args[5], "out") == 0); |
145 | 0 | freet(args); |
146 | | |
147 | | // padding keeps one empty argument per separator, which the engine skips |
148 | 0 | strcpybuff(line, "httrack --quiet"); |
149 | 0 | args = hts_split_cmdline(line, &nargs); |
150 | 0 | assertf(nargs == 3 && args[1][0] == '\0'); |
151 | 0 | assertf(strcmp(args[2], "--quiet") == 0); |
152 | 0 | freet(args); |
153 | | |
154 | | // a quoted run keeps both its spaces and its quotes: the engine unquotes |
155 | 0 | strcpybuff(line, "httrack --user-agent \"Mozilla 5.0\" -c8"); |
156 | 0 | args = hts_split_cmdline(line, &nargs); |
157 | 0 | assertf(nargs == 4); |
158 | 0 | assertf(strcmp(args[2], "\"Mozilla 5.0\"") == 0); |
159 | 0 | assertf(strcmp(args[3], "-c8") == 0); |
160 | 0 | freet(args); |
161 | | |
162 | | // an escaped quote stays literal, and the engine strips only the outer pair |
163 | 0 | strcpybuff(line, "httrack --user-agent \"x\\\" -V \\\"touch /tmp/pwn\" -c8"); |
164 | 0 | args = hts_split_cmdline(line, &nargs); |
165 | 0 | assertf(nargs == 4); |
166 | 0 | assertf(strcmp(args[2], "\"x\" -V \"touch /tmp/pwn\"") == 0); |
167 | 0 | assertf(strcmp(args[3], "-c8") == 0); |
168 | 0 | freet(args); |
169 | | |
170 | | // \\ is a literal backslash, so a Windows path survives |
171 | 0 | strcpybuff(line, "httrack --path \"C:\\\\dir\\\\sub\""); |
172 | 0 | args = hts_split_cmdline(line, &nargs); |
173 | 0 | assertf(nargs == 3); |
174 | 0 | assertf(strcmp(args[2], "\"C:\\dir\\sub\"") == 0); |
175 | 0 | freet(args); |
176 | | |
177 | | // outside a quoted run a backslash is literal: the url and wildcard-filter |
178 | | // fields, which the wizard cannot quote, read as before |
179 | 0 | strcpybuff(line, "httrack -*\\** +*.png"); |
180 | 0 | args = hts_split_cmdline(line, &nargs); |
181 | 0 | assertf(nargs == 3); |
182 | 0 | assertf(strcmp(args[1], "-*\\**") == 0); |
183 | 0 | assertf(strcmp(args[2], "+*.png") == 0); |
184 | 0 | freet(args); |
185 | | |
186 | | // a quoted run leaves slots unused, so the terminator must be written. |
187 | | // Freeing a full-line vector first hands back a chunk with stale pointers. |
188 | 0 | strcpybuff(line, "httrack a b c d e"); |
189 | 0 | args = hts_split_cmdline(line, &nargs); |
190 | 0 | assertf(nargs == 6); |
191 | 0 | freet(args); |
192 | 0 | strcpybuff(line, "httrack \"a b c d e\""); |
193 | 0 | args = hts_split_cmdline(line, &nargs); |
194 | 0 | assertf(nargs == 2); |
195 | 0 | assertf(args[nargs] == NULL); |
196 | 0 | freet(args); |
197 | | |
198 | | // an unterminated quote protects the rest of the line, as one argument |
199 | 0 | strcpybuff(line, "httrack --footer \"unbalanced -V x"); |
200 | 0 | args = hts_split_cmdline(line, &nargs); |
201 | 0 | assertf(nargs == 3); |
202 | 0 | assertf(strcmp(args[2], "\"unbalanced -V x") == 0); |
203 | 0 | freet(args); |
204 | | |
205 | | // distinct arguments stop an overrun from reading back as the parse |
206 | 0 | { |
207 | 0 | const int n = 2000; |
208 | 0 | const size_t size = 16 * (size_t) n + 16; |
209 | 0 | char *big = malloct(size); |
210 | 0 | size_t pos = 0; |
211 | 0 | int i; |
212 | |
|
213 | 0 | assertf(big != NULL); |
214 | 0 | pos = (size_t) snprintf(big, size, "httrack"); |
215 | 0 | assertf(pos < size); |
216 | 0 | for (i = 0; i < n; i++) { |
217 | | // snprintf returns what it wanted to write, so accumulating it blind |
218 | | // would let the next size argument wrap |
219 | 0 | const int len = snprintf(big + pos, size - pos, " a%d", i); |
220 | |
|
221 | 0 | assertf(len > 0 && (size_t) len < size - pos); |
222 | 0 | pos += (size_t) len; |
223 | 0 | } |
224 | 0 | args = hts_split_cmdline(big, &nargs); |
225 | 0 | assertf(args != NULL && nargs == n + 1); |
226 | 0 | assertf(args[nargs] == NULL); |
227 | 0 | for (i = 0; i < n; i++) { |
228 | 0 | char expect[16]; |
229 | |
|
230 | 0 | snprintf(expect, sizeof(expect), "a%d", i); |
231 | 0 | assertf(strcmp(args[i + 1], expect) == 0); |
232 | 0 | } |
233 | 0 | freet(args); |
234 | 0 | freet(big); |
235 | 0 | } |
236 | | |
237 | 0 | printf("cmdline-split self-test OK\n"); |
238 | 0 | return 0; |
239 | 0 | } |
240 | | |
241 | 0 | static int st_copyopt(httrackp *opt, int argc, char **argv) { |
242 | 0 | httrackp *from = hts_create_opt(); |
243 | 0 | httrackp *to = hts_create_opt(); |
244 | 0 | int err = 0; |
245 | | |
246 | | /* from-values differ from to-values and defaults to catch a no-op copy. */ |
247 | 0 | from->retry = 7; /* int field: positive control */ |
248 | 0 | to->retry = 0; |
249 | 0 | from->nearlink = HTS_TRUE; |
250 | 0 | to->nearlink = HTS_FALSE; |
251 | 0 | from->errpage = HTS_FALSE; |
252 | 0 | to->errpage = HTS_TRUE; |
253 | 0 | from->parseall = HTS_FALSE; |
254 | 0 | to->parseall = HTS_TRUE; |
255 | |
|
256 | 0 | copy_htsopt(from, to); |
257 | |
|
258 | 0 | ST_FAIL_IF(err, to->retry != 7); |
259 | 0 | ST_FAIL_IF(err, to->nearlink != HTS_TRUE); |
260 | 0 | ST_FAIL_IF(err, to->errpage != HTS_FALSE); |
261 | 0 | ST_FAIL_IF(err, to->parseall != HTS_FALSE); |
262 | | |
263 | | /* copy_htsopt must skip HTS_DEFAULT (-1), which needs a signed field. */ |
264 | 0 | from->parseall = HTS_DEFAULT; |
265 | 0 | to->parseall = HTS_TRUE; |
266 | 0 | copy_htsopt(from, to); |
267 | 0 | ST_FAIL_IF(err, to->parseall != HTS_TRUE); |
268 | | |
269 | | /* No crawl test reaches this exported String copy path. */ |
270 | 0 | StringCopy(from->cookies_file, "/tmp/jar.txt"); |
271 | 0 | StringCopy(to->cookies_file, ""); |
272 | 0 | copy_htsopt(from, to); |
273 | 0 | ST_FAIL_IF(err, strcmp(StringBuff(to->cookies_file), "/tmp/jar.txt") != 0); |
274 | 0 | StringCopy(from->cookies_file, ""); |
275 | 0 | copy_htsopt(from, to); |
276 | 0 | ST_FAIL_IF(err, strcmp(StringBuff(to->cookies_file), "/tmp/jar.txt") != 0); |
277 | | |
278 | | /* warc_file: same String deep-copy path as cookies_file */ |
279 | 0 | StringCopy(from->warc_file, "run.warc.gz"); |
280 | 0 | StringCopy(to->warc_file, ""); |
281 | 0 | copy_htsopt(from, to); |
282 | 0 | ST_FAIL_IF(err, strcmp(StringBuff(to->warc_file), "run.warc.gz") != 0); |
283 | | |
284 | | /* accept_kinds deep-copies, and an explicit -%a or -%l carries its flag. */ |
285 | 0 | StringCopy(from->accept_kinds, "image=a/b"); |
286 | 0 | StringCopy(from->accept, "x/y"); |
287 | 0 | from->accept_given = HTS_TRUE; |
288 | 0 | StringCopy(from->lang_iso, "fr"); |
289 | 0 | from->lang_given = HTS_TRUE; |
290 | 0 | copy_htsopt(from, to); |
291 | 0 | ST_FAIL_IF(err, strcmp(StringBuff(to->accept_kinds), "image=a/b") != 0 || |
292 | 0 | strcmp(StringBuff(to->accept), "x/y") != 0 || |
293 | 0 | !to->accept_given || |
294 | 0 | strcmp(StringBuff(to->lang_iso), "fr") != 0 || |
295 | 0 | !to->lang_given); |
296 | | /* A default -%a or -%l never overwrites an explicit one. */ |
297 | 0 | StringCopy(from->accept, HTS_ACCEPT_LEGACY); |
298 | 0 | from->accept_given = HTS_FALSE; |
299 | 0 | StringCopy(from->lang_iso, HTS_ACCEPT_LANGUAGE_LEGACY); |
300 | 0 | from->lang_given = HTS_FALSE; |
301 | 0 | copy_htsopt(from, to); |
302 | 0 | ST_FAIL_IF( |
303 | 0 | err, strcmp(StringBuff(to->accept), "x/y") != 0 || !to->accept_given || |
304 | 0 | strcmp(StringBuff(to->lang_iso), "fr") != 0 || !to->lang_given); |
305 | | /* An explicit -%a or -%l equal to the built-in value still overwrites. */ |
306 | 0 | from->accept_given = HTS_TRUE; |
307 | 0 | from->lang_given = HTS_TRUE; |
308 | 0 | copy_htsopt(from, to); |
309 | 0 | ST_FAIL_IF(err, strcmp(StringBuff(to->accept), HTS_ACCEPT_LEGACY) != 0 || |
310 | 0 | strcmp(StringBuff(to->lang_iso), |
311 | 0 | HTS_ACCEPT_LANGUAGE_LEGACY) != 0); |
312 | | |
313 | | /* No front end sets these, and a mid-crawl change gives a partial result. */ |
314 | 0 | from->changes = from->single_file = HTS_TRUE; |
315 | 0 | from->warc_cdx = from->warc_wacz = HTS_TRUE; |
316 | 0 | from->single_file_max_size = 4096; |
317 | 0 | from->warc_max_size = 5000; |
318 | 0 | to->changes = to->single_file = HTS_FALSE; |
319 | 0 | to->warc_cdx = to->warc_wacz = HTS_FALSE; |
320 | 0 | to->single_file_max_size = 8192; |
321 | 0 | to->warc_max_size = 0; |
322 | 0 | copy_htsopt(from, to); |
323 | 0 | ST_FAIL_IF(err, to->changes || to->single_file || to->warc_cdx || |
324 | 0 | to->warc_wacz || to->single_file_max_size != 8192 || |
325 | 0 | to->warc_max_size != 0); |
326 | 0 | from->changes = from->single_file = HTS_FALSE; |
327 | 0 | from->warc_cdx = from->warc_wacz = HTS_FALSE; |
328 | 0 | from->single_file_max_size = SINGLEFILE_DEFAULT_MAX_SIZE; |
329 | 0 | from->warc_max_size = 0; |
330 | 0 | to->changes = to->single_file = HTS_TRUE; |
331 | 0 | to->warc_cdx = to->warc_wacz = HTS_TRUE; |
332 | 0 | to->warc_max_size = 9999; |
333 | 0 | copy_htsopt(from, to); |
334 | 0 | ST_FAIL_IF(err, !to->changes || !to->single_file || !to->warc_cdx || |
335 | 0 | !to->warc_wacz || to->single_file_max_size != 8192 || |
336 | 0 | to->warc_max_size != 9999); |
337 | | /* Agreeing values stay put, which an inverted copy would break. */ |
338 | 0 | from->changes = from->single_file = from->warc_cdx = HTS_TRUE; |
339 | 0 | copy_htsopt(from, to); |
340 | 0 | ST_FAIL_IF(err, !to->changes || !to->single_file || !to->warc_cdx); |
341 | | |
342 | | /* delete_old never travels, whatever the source holds. */ |
343 | 0 | from->delete_old = HTS_TRUE; |
344 | 0 | to->delete_old = HTS_FALSE; |
345 | 0 | copy_htsopt(from, to); |
346 | 0 | ST_FAIL_IF(err, to->delete_old != HTS_FALSE); |
347 | 0 | from->delete_old = HTS_FALSE; |
348 | 0 | to->delete_old = HTS_TRUE; |
349 | 0 | copy_htsopt(from, to); |
350 | 0 | ST_FAIL_IF(err, to->delete_old != HTS_TRUE); |
351 | 0 | from->delete_old = HTS_TRUE; |
352 | 0 | copy_htsopt(from, to); |
353 | 0 | ST_FAIL_IF(err, to->delete_old != HTS_TRUE); |
354 | | |
355 | | /* external travels both ways, and HTS_DEFAULT keeps the target's. */ |
356 | 0 | from->external = HTS_TRUE; |
357 | 0 | to->external = HTS_FALSE; |
358 | 0 | copy_htsopt(from, to); |
359 | 0 | ST_FAIL_IF(err, to->external != HTS_TRUE); |
360 | 0 | from->external = HTS_FALSE; |
361 | 0 | copy_htsopt(from, to); |
362 | 0 | ST_FAIL_IF(err, to->external != HTS_FALSE); |
363 | 0 | from->external = HTS_DEFAULT; |
364 | 0 | to->external = HTS_TRUE; |
365 | 0 | copy_htsopt(from, to); |
366 | 0 | ST_FAIL_IF(err, to->external != HTS_TRUE); |
367 | | |
368 | | /* fragment: 0 is a real setting, only -1 skips. */ |
369 | 0 | from->fragment = 0; |
370 | 0 | to->fragment = 123456; |
371 | 0 | copy_htsopt(from, to); |
372 | 0 | ST_FAIL_IF(err, to->fragment != 0); |
373 | 0 | from->fragment = 654321; |
374 | 0 | copy_htsopt(from, to); |
375 | 0 | ST_FAIL_IF(err, to->fragment != 654321); |
376 | 0 | from->fragment = -1; |
377 | 0 | copy_htsopt(from, to); |
378 | 0 | ST_FAIL_IF(err, to->fragment != 654321); |
379 | | |
380 | | /* sitemap pair: the flag latches on, the URL takes the String deep copy */ |
381 | 0 | from->sitemap = HTS_TRUE; |
382 | 0 | StringCopy(from->sitemap_url, "http://h.test/sitemap.xml"); |
383 | 0 | to->sitemap = HTS_FALSE; |
384 | 0 | StringCopy(to->sitemap_url, ""); |
385 | 0 | copy_htsopt(from, to); |
386 | 0 | if (!to->sitemap || |
387 | 0 | strcmp(StringBuff(to->sitemap_url), "http://h.test/sitemap.xml") != 0) |
388 | 0 | err = 1; |
389 | 0 | from->sitemap = HTS_FALSE; |
390 | 0 | StringCopy(from->sitemap_url, ""); |
391 | 0 | copy_htsopt(from, to); |
392 | 0 | if (!to->sitemap || |
393 | 0 | strcmp(StringBuff(to->sitemap_url), "http://h.test/sitemap.xml") != 0) |
394 | 0 | err = 1; |
395 | | |
396 | | /* #185 pause pair: copied when enabled (max>0), the 0 sentinel skips */ |
397 | 0 | from->pause_min_ms = 5000; |
398 | 0 | from->pause_max_ms = 10000; |
399 | 0 | to->pause_min_ms = to->pause_max_ms = 0; |
400 | 0 | copy_htsopt(from, to); |
401 | 0 | ST_FAIL_IF(err, to->pause_min_ms != 5000 || to->pause_max_ms != 10000); |
402 | 0 | from->pause_min_ms = from->pause_max_ms = 0; |
403 | 0 | copy_htsopt(from, to); |
404 | 0 | ST_FAIL_IF(err, to->pause_min_ms != 5000 || to->pause_max_ms != 10000); |
405 | | |
406 | | /* max_retry_after: 0 is a real setting (no wait), so only a negative skips */ |
407 | 0 | from->max_retry_after = 0; |
408 | 0 | to->max_retry_after = 60; |
409 | 0 | copy_htsopt(from, to); |
410 | 0 | ST_FAIL_IF(err, to->max_retry_after != 0); |
411 | 0 | from->max_retry_after = -1; |
412 | 0 | copy_htsopt(from, to); |
413 | 0 | ST_FAIL_IF(err, to->max_retry_after != 0); |
414 | 0 | from->max_retry_after = 7; |
415 | 0 | copy_htsopt(from, to); |
416 | 0 | ST_FAIL_IF(err, to->max_retry_after != 7); |
417 | | /* A fresh opt's default is not an edit, so it cannot be re-applied. */ |
418 | 0 | from->max_retry_after = HTS_DEFAULT_MAX_RETRY_AFTER; |
419 | 0 | copy_htsopt(from, to); |
420 | 0 | ST_FAIL_IF(err, to->max_retry_after != 7); |
421 | |
|
422 | 0 | hts_free_opt(from); |
423 | 0 | hts_free_opt(to); |
424 | 0 | return st_verdict("copy-htsopt", err); |
425 | 0 | } |
426 | | |
427 | | /* A run with no log file must still count its errors (#1681). */ |
428 | 0 | static int st_logcounters(httrackp *opt, int argc, char **argv) { |
429 | 0 | httrackp *o = hts_create_opt(); |
430 | 0 | const hts_stat_struct *stats; |
431 | 0 | int err = 0; |
432 | | |
433 | | /* -Q sets no log file and default verbosity. */ |
434 | 0 | o->log = NULL; |
435 | 0 | o->errlog = NULL; |
436 | 0 | o->debug = LOG_NOTICE; |
437 | |
|
438 | 0 | hts_log_print(o, LOG_ERROR, "first"); |
439 | | /* LOG_ERRNO rides in the high bits and must not shift the level. */ |
440 | 0 | hts_log_print(o, LOG_ERROR | LOG_ERRNO, "second"); |
441 | 0 | hts_log_print(o, LOG_WARNING, "third"); |
442 | 0 | hts_log_print(o, LOG_WARNING, "fourth"); |
443 | 0 | hts_log_print(o, LOG_WARNING, "fifth"); |
444 | 0 | hts_log_print(o, LOG_NOTICE, "sixth"); |
445 | | |
446 | | /* Three distinct totals, so a swapped counter cannot pass. */ |
447 | 0 | stats = hts_get_stats(o); |
448 | 0 | ST_FAIL_IF(err, stats == NULL || stats->stat_errors != 2 || |
449 | 0 | stats->stat_warnings != 3 || stats->stat_infos != 1); |
450 | | |
451 | | /* Polling hts_get_stats() must not increment the count. */ |
452 | 0 | stats = hts_get_stats(o); |
453 | 0 | ST_FAIL_IF(err, stats == NULL || stats->stat_errors != 2); |
454 | | |
455 | | /* LOG_INFO is above the default verbosity, so it is not counted. */ |
456 | 0 | hts_log_print(o, LOG_INFO, "seventh"); |
457 | 0 | stats = hts_get_stats(o); |
458 | 0 | ST_FAIL_IF(err, stats == NULL || stats->stat_infos != 1); |
459 | | |
460 | | /* Raising the verbosity lets the same level through. */ |
461 | 0 | o->debug = LOG_INFO; |
462 | 0 | hts_log_print(o, LOG_INFO, "eighth"); |
463 | 0 | stats = hts_get_stats(o); |
464 | 0 | ST_FAIL_IF(err, stats == NULL || stats->stat_infos != 2); |
465 | | |
466 | | /* A panic is fatal, so it counts as an error rather than as nothing. */ |
467 | 0 | hts_log_print(o, LOG_PANIC, "ninth"); |
468 | 0 | stats = hts_get_stats(o); |
469 | 0 | ST_FAIL_IF(err, stats == NULL || stats->stat_errors != 3); |
470 | |
|
471 | 0 | hts_free_opt(o); |
472 | 0 | return st_verdict("log-counters", err); |
473 | 0 | } |
474 | | |
475 | | /* A silent run must not hand back the previous mirror's note. */ |
476 | 0 | static int st_upperlinksnote(httrackp *opt, int argc, char **argv) { |
477 | 0 | httrackp *from = hts_create_opt(); |
478 | 0 | httrackp *to = hts_create_opt(); |
479 | 0 | char note[256]; |
480 | 0 | int err = 0; |
481 | |
|
482 | 0 | strcpybuff(note, "stale"); |
483 | | /* a fresh opt refused nothing, and must empty the buffer */ |
484 | 0 | ST_FAIL_IF(err, hts_upper_links_note(to, HTS_BUF(note)) || note[0] != '\0'); |
485 | |
|
486 | 0 | to->upper_links_refused = HTS_TRUE; |
487 | | /* the note must name the option that lifts the refusal */ |
488 | 0 | ST_FAIL_IF(err, !hts_upper_links_note(to, HTS_BUF(note)) || |
489 | 0 | strstr(note, "-B") == NULL); |
490 | | /* a short buffer clips the note, and an empty one is left alone */ |
491 | 0 | ST_FAIL_IF(err, !hts_upper_links_note(to, HTS_BUF_N(note, 8)) || |
492 | 0 | strlen(note) != 7 || strncmp(note, "Some li", 7) != 0); |
493 | 0 | strcpybuff(note, "stale"); |
494 | 0 | ST_FAIL_IF(err, hts_upper_links_note(to, HTS_BUF_N(note, 0)) || |
495 | 0 | strcmp(note, "stale") != 0); |
496 | |
|
497 | 0 | to->upper_links_refused = HTS_FALSE; |
498 | 0 | ST_FAIL_IF(err, hts_upper_links_note(to, HTS_BUF(note)) || note[0] != '\0'); |
499 | | |
500 | | /* the flag never travels onto another opt */ |
501 | 0 | from->upper_links_refused = HTS_TRUE; |
502 | 0 | copy_htsopt(from, to); |
503 | 0 | ST_FAIL_IF(err, to->upper_links_refused); |
504 | |
|
505 | 0 | hts_free_opt(from); |
506 | 0 | hts_free_opt(to); |
507 | 0 | return st_verdict("upper-links-note", err); |
508 | 0 | } |
509 | | |
510 | | /* These numbers are ABI in htsopt.h and are what -C prints. */ |
511 | | HTS_STATIC_ASSERT(HTS_CACHE_NONE == 0, cache_none_is_0); |
512 | | HTS_STATIC_ASSERT(HTS_CACHE_PRIORITY == 1, cache_priority_is_1); |
513 | | HTS_STATIC_ASSERT(HTS_CACHE_TEST_UPDATE == 2, cache_test_update_is_2); |
514 | | |
515 | | /* With no -C given, the default is C1 cache-priority, not C2 test-update. */ |
516 | 0 | static int st_cachedefault(httrackp *opt, int argc, char **argv) { |
517 | 0 | httrackp *const fresh = hts_create_opt(); |
518 | 0 | int err = 0; |
519 | |
|
520 | 0 | ST_FAIL_IF(err, fresh->cache != HTS_CACHE_PRIORITY); |
521 | 0 | hts_free_opt(fresh); |
522 | 0 | return st_verdict("cache-default", err); |
523 | 0 | } |
524 | | |
525 | | static char st_log_callback_seen[256]; |
526 | | |
527 | | static HTS_PRINTF_FUN(3, 0) void st_log_callback(httrackp *opt, int type, |
528 | | const char *format, |
529 | 0 | va_list args) { |
530 | 0 | (void) type; |
531 | 0 | (void) vsnprintf(st_log_callback_seen, sizeof(st_log_callback_seen), format, |
532 | 0 | args); |
533 | 0 | } |
534 | | |
535 | | /* The callback must not consume the va_list the log file's vfprintf() needs. */ |
536 | 0 | static int st_logcallback(httrackp *opt, int argc, char **argv) { |
537 | 0 | static const char want[] = "42 sentinel"; |
538 | 0 | static const char want_filtered[] = "7 filtered"; |
539 | 0 | char BIGSTK seen[sizeof(st_log_callback_seen)]; |
540 | 0 | char BIGSTK line[256]; |
541 | 0 | FILE *fp; |
542 | 0 | int rc = 1; |
543 | |
|
544 | 0 | fp = tmpfile(); |
545 | 0 | if (fp == NULL) { |
546 | 0 | fprintf(stderr, "logcallback: tmpfile() failed\n"); |
547 | 0 | return 1; |
548 | 0 | } |
549 | 0 | opt->log = fp; |
550 | 0 | opt->debug = LOG_NOTICE; |
551 | 0 | st_log_callback_seen[0] = '\0'; |
552 | 0 | hts_set_log_vprint_callback(st_log_callback); |
553 | 0 | hts_log_print(opt, LOG_NOTICE, "%d %s", 42, "sentinel"); |
554 | 0 | hts_set_log_vprint_callback(NULL); |
555 | 0 | opt->log = NULL; |
556 | 0 | strcpybuff(seen, st_log_callback_seen); |
557 | |
|
558 | 0 | rewind(fp); |
559 | 0 | if (fgets(line, (int) sizeof(line), fp) == NULL) { |
560 | 0 | fprintf(stderr, "logcallback: log file is empty, nothing was written\n"); |
561 | 0 | fclose(fp); |
562 | 0 | return 1; |
563 | 0 | } |
564 | 0 | fclose(fp); |
565 | | |
566 | | /* The callback must run above the level filter and with no log file. */ |
567 | 0 | st_log_callback_seen[0] = '\0'; |
568 | 0 | hts_set_log_vprint_callback(st_log_callback); |
569 | 0 | hts_log_print(opt, LOG_DEBUG, "%d %s", 7, "filtered"); |
570 | 0 | hts_set_log_vprint_callback(NULL); |
571 | | |
572 | | /* Same arguments both ways; the file line carries a level prefix. */ |
573 | 0 | if (strcmp(seen, want) != 0) |
574 | 0 | fprintf(stderr, "logcallback: callback got '%s' want '%s'\n", seen, want); |
575 | 0 | else if (strstr(line, want) == NULL) |
576 | 0 | fprintf(stderr, "logcallback: log file got '%s' want it to carry '%s'\n", |
577 | 0 | line, want); |
578 | 0 | else if (strcmp(st_log_callback_seen, want_filtered) != 0) |
579 | 0 | fprintf(stderr, "logcallback: unfiltered callback got '%s' want '%s'\n", |
580 | 0 | st_log_callback_seen, want_filtered); |
581 | 0 | else |
582 | 0 | rc = 0; |
583 | |
|
584 | 0 | if (rc == 0) |
585 | 0 | printf("logcallback self-test OK\n"); |
586 | 0 | return rc; |
587 | 0 | } |
588 | | |
589 | | #ifdef HTS_CRASH_TEST |
590 | | static char st_crash_seen[256]; |
591 | | static int st_crash_level; |
592 | | static hts_boolean st_crash_had_opt; |
593 | | |
594 | | static HTS_PRINTF_FUN(3, 0) void st_crash_log(httrackp *opt, int type, |
595 | | const char *format, |
596 | 0 | va_list args) { |
597 | 0 | st_crash_had_opt = opt != NULL ? HTS_TRUE : HTS_FALSE; |
598 | 0 | st_crash_level = type; |
599 | 0 | (void) vsnprintf(st_crash_seen, sizeof(st_crash_seen), format, args); |
600 | 0 | } |
601 | | |
602 | | static FILE *st_crash_fp; |
603 | | |
604 | | static HTS_PRINTF_FUN(3, 0) void st_crash_file_log(httrackp *opt, int type, |
605 | | const char *format, |
606 | 0 | va_list args) { |
607 | 0 | (void) type; |
608 | 0 | vfprintf(st_crash_fp, format, args); |
609 | 0 | fputc('\n', st_crash_fp); |
610 | | /* Flushed line by line, because the caller below is about to fault. */ |
611 | 0 | fflush(st_crash_fp); |
612 | 0 | } |
613 | | |
614 | | /* Given a file, this writes the log callback's text there during a -#c |
615 | | crash. See htscrashtest.h for why both channels carry the marker. */ |
616 | 0 | static int st_crashannounce(httrackp *opt, int argc, char **argv) { |
617 | 0 | static const char want[] = "crash announce 42"; |
618 | 0 | int rc = 1; |
619 | |
|
620 | 0 | if (argc >= 1) { |
621 | 0 | st_crash_fp = fopen(argv[0], "wb"); |
622 | 0 | if (st_crash_fp == NULL) { |
623 | 0 | fprintf(stderr, "crashannounce: cannot write %s\n", argv[0]); |
624 | 0 | return 1; |
625 | 0 | } |
626 | 0 | hts_set_log_vprint_callback(st_crash_file_log); |
627 | 0 | (void) hts_crash_test("dnssegv"); |
628 | 0 | hts_crash_test_worker(HTS_CRASH_WORKER_DNS); |
629 | | /* Only a handler that swallowed the fault gets here, so hand the log |
630 | | channel back rather than funnelling a later case into this file. */ |
631 | 0 | hts_set_log_vprint_callback(NULL); |
632 | 0 | fclose(st_crash_fp); |
633 | 0 | st_crash_fp = NULL; |
634 | 0 | fprintf(stderr, "crashannounce: the armed fault did not crash\n"); |
635 | 0 | return 1; |
636 | 0 | } |
637 | | |
638 | 0 | hts_set_log_vprint_callback(st_crash_log); |
639 | | /* The engine must not need an opt to reach the front end here. */ |
640 | 0 | hts_crash_test_announce("crash announce %d", 42); |
641 | 0 | hts_set_log_vprint_callback(NULL); |
642 | |
|
643 | 0 | if (strcmp(st_crash_seen, want) != 0) |
644 | 0 | fprintf(stderr, "crashannounce: callback got '%s' want '%s'\n", |
645 | 0 | st_crash_seen, want); |
646 | 0 | else if (st_crash_level != LOG_ERROR) |
647 | 0 | fprintf(stderr, "crashannounce: logged at level %d, want %d (LOG_ERROR)\n", |
648 | 0 | st_crash_level, (int) LOG_ERROR); |
649 | 0 | else if (st_crash_had_opt) |
650 | 0 | fprintf(stderr, "crashannounce: the callback was handed an opt\n"); |
651 | 0 | else |
652 | 0 | rc = 0; |
653 | |
|
654 | 0 | if (rc == 0) |
655 | 0 | printf("crashannounce self-test OK\n"); |
656 | 0 | return rc; |
657 | 0 | } |
658 | | #endif |
659 | | |
660 | | /* One optalias_check() run: its short form, warning and words consumed. */ |
661 | | typedef struct st_alias { |
662 | | char got[HTS_CDLMAXSIZE * 2]; |
663 | | char warn[256]; |
664 | | int used; |
665 | | } st_alias; |
666 | | |
667 | | /* Returns the short form optalias_check() emits, or NULL on refusal. */ |
668 | | static const char *st_optalias_expand(st_alias *t, const char *word, |
669 | 0 | const char *next) { |
670 | 0 | char BIGSTK out[2][HTS_CDLMAXSIZE]; |
671 | 0 | char *outv[2] = {out[0], out[1]}; |
672 | 0 | const char *argv[2]; |
673 | 0 | char error[256]; |
674 | 0 | const int argc = next != NULL ? 2 : 1; |
675 | 0 | int outc = 0; |
676 | |
|
677 | 0 | argv[0] = word; |
678 | 0 | argv[1] = next; |
679 | 0 | out[0][0] = out[1][0] = t->got[0] = t->warn[0] = '\0'; |
680 | 0 | t->used = optalias_check(argc, argv, 0, &outc, outv, sizeof(out[0]), |
681 | 0 | HTS_BUF(error)); |
682 | 0 | if (t->used == 0) { |
683 | 0 | assertf(error[0] != '\0'); /* a refusal has to say why */ |
684 | 0 | return NULL; |
685 | 0 | } |
686 | 0 | hts_buf_cpy(HTS_BUF(t->warn), error); |
687 | 0 | assertf(outc >= 1 && outc <= 2); |
688 | 0 | hts_buf_cpy(HTS_BUF(t->got), out[0]); |
689 | 0 | if (outc == 2) { |
690 | 0 | hts_buf_cat(HTS_BUF(t->got), " "); |
691 | 0 | hts_buf_cat(HTS_BUF(t->got), out[1]); |
692 | 0 | } |
693 | 0 | return t->got; |
694 | 0 | } |
695 | | |
696 | | /* Does WORD [NEXT] expand to WANT, with a warning only if WARNED? */ |
697 | | static hts_boolean st_alias_gives(st_alias *t, const char *want, |
698 | | const char *word, const char *next, |
699 | 0 | hts_boolean warned) { |
700 | 0 | const char *const out = st_optalias_expand(t, word, next); |
701 | |
|
702 | 0 | return out != NULL && strcmp(out, want) == 0 && |
703 | 0 | (t->warn[0] != '\0') == warned; |
704 | 0 | } |
705 | | |
706 | | /* Does WORD [NEXT] expand to WANT without a warning? */ |
707 | | static hts_boolean st_expands(st_alias *t, const char *want, const char *word, |
708 | 0 | const char *next) { |
709 | 0 | return st_alias_gives(t, want, word, next, HTS_FALSE); |
710 | 0 | } |
711 | | |
712 | | /* Does WORD [NEXT] expand to WANT with a warning? */ |
713 | | static hts_boolean st_warns(st_alias *t, const char *want, const char *word, |
714 | 0 | const char *next) { |
715 | 0 | return st_alias_gives(t, want, word, next, HTS_TRUE); |
716 | 0 | } |
717 | | |
718 | | /* Is WORD [NEXT] refused? */ |
719 | 0 | static hts_boolean st_refuses(st_alias *t, const char *word, const char *next) { |
720 | 0 | return st_optalias_expand(t, word, next) == NULL; |
721 | 0 | } |
722 | | |
723 | | /* --index=0 must not read back as a bare --index (#1195). */ |
724 | 0 | static int st_optalias(httrackp *opt, int argc, char **argv) { |
725 | 0 | st_alias t; |
726 | 0 | int i, params = 0; |
727 | | |
728 | | /* -list gives 282 the whole table: the rows to try against the engine's |
729 | | own parser, and the class of every name the wizard writes a value to */ |
730 | 0 | if (argc == 1 && strcmp(argv[0], "-list") == 0) { |
731 | 0 | for (i = 0; optalias_value(i)[0] != '\0'; i++) |
732 | 0 | printf("%s %s\n", optclass_name(optclass_of(i)), optalias_value(i)); |
733 | 0 | return 0; |
734 | 0 | } |
735 | 0 | if (argc >= 1) { |
736 | 0 | const char *const out = |
737 | 0 | st_optalias_expand(&t, argv[0], argc >= 2 ? argv[1] : NULL); |
738 | |
|
739 | 0 | printf("%s\n", out != NULL ? out : "(refused)"); |
740 | 0 | return out != NULL ? 0 : 1; |
741 | 0 | } |
742 | | |
743 | | /* the long form can disable the index like -I0 */ |
744 | 0 | assertf(st_expands(&t, "-I0", "--index=0", NULL)); |
745 | 0 | assertf(st_expands(&t, "-I0", "--index=off", NULL)); |
746 | 0 | assertf(st_expands(&t, "-I0", "--noindex", NULL)); |
747 | 0 | assertf(st_expands(&t, "-I", "--index=1", NULL)); |
748 | 0 | assertf(st_expands(&t, "-I", "--index=on", NULL)); |
749 | | /* detached, as a config file writes it ("index off"), and never a URL */ |
750 | 0 | assertf(st_expands(&t, "-I0", "--index", "off")); |
751 | 0 | assertf(t.used == 2); |
752 | 0 | assertf(st_expands(&t, "-I", "--index", "http://foo/")); |
753 | 0 | assertf(t.used == 1); |
754 | 0 | assertf(st_expands(&t, "-I", "--index", NULL)); |
755 | 0 | assertf(t.used == 1); |
756 | | /* -I takes only 0, and any other value is an error */ |
757 | 0 | assertf(st_refuses(&t, "--index=2", NULL)); |
758 | 0 | assertf(st_refuses(&t, "--index=yes", NULL)); |
759 | | |
760 | | /* a numeric level reaches the short form whole */ |
761 | 0 | assertf(st_expands(&t, "-%I0", "--search-index=0", NULL)); |
762 | 0 | assertf(st_expands(&t, "-%I2", "--search-index=2", NULL)); |
763 | 0 | assertf(st_expands(&t, "-%v2", "--display=2", NULL)); |
764 | 0 | assertf(st_expands(&t, "-%N0", "--delayed-type-check=0", NULL)); |
765 | 0 | assertf(st_expands(&t, "-o0", "--generate-errors=0", NULL)); |
766 | 0 | assertf(st_refuses(&t, "--display=full", NULL)); |
767 | 0 | assertf(st_refuses(&t, "--display=99999999999999999999", NULL)); |
768 | | |
769 | | /* these rows take no following token, so =1 and a next URL both work */ |
770 | 0 | assertf(st_expands(&t, "-X", "--purge-old", NULL)); |
771 | 0 | assertf(st_expands(&t, "-X", "--purge-old", "http://foo/")); |
772 | 0 | assertf(t.used == 1); |
773 | 0 | assertf(st_expands(&t, "-X0", "--purge-old=0", NULL)); |
774 | 0 | assertf(st_expands(&t, "-X", "--purge-old=1", NULL)); |
775 | 0 | assertf(st_expands(&t, "-X0", "--purge-old", "0")); |
776 | 0 | assertf(t.used == 2); |
777 | 0 | assertf(st_expands(&t, "-X", "--purge-old", "1")); |
778 | 0 | assertf(st_expands(&t, "-X0", "--nopurge-old", NULL)); |
779 | 0 | assertf(st_expands(&t, "-%f0", "--httpproxy-ftp=0", NULL)); |
780 | 0 | assertf(st_expands(&t, "-%f", "--httpproxy-ftp=1", NULL)); |
781 | 0 | assertf(st_expands(&t, "-%P0", "--extended-parsing=0", NULL)); |
782 | 0 | assertf(st_expands(&t, "-%P", "--extended-parsing=1", NULL)); |
783 | | |
784 | | /* a flag that takes no value refuses one rather than dropping it */ |
785 | 0 | assertf(st_refuses(&t, "--mirror=0", NULL)); |
786 | 0 | assertf(st_refuses(&t, "--nomirror", NULL)); |
787 | 0 | assertf(st_refuses(&t, "--warc=0", NULL)); |
788 | 0 | assertf(st_refuses(&t, "--single-file=s", NULL)); |
789 | | /* ...and so does a compound alias, whose value would land in the cluster */ |
790 | 0 | assertf(st_refuses(&t, "--spider=0", NULL)); |
791 | | |
792 | | /* an alias whose own name begins with "no" is not a negation */ |
793 | 0 | assertf(st_expands(&t, "-%T0", "--no-utf8-conversion", NULL)); |
794 | 0 | assertf(st_expands(&t, "-y0", "--no-background-on-suspend", NULL)); |
795 | 0 | assertf(st_expands(&t, "-%T", "--utf8-conversion", NULL)); |
796 | | |
797 | | /* the --wide-/--tiny- prefix glues the --wide/--tiny connection count onto |
798 | | the plain clusters, and is refused wherever the glue would be misread */ |
799 | 0 | assertf(st_expands(&t, "-wc32", "--wide-mirror", NULL)); |
800 | 0 | assertf(t.used == 1); |
801 | 0 | assertf(st_expands(&t, "-wc1", "--tiny-mirror", NULL)); |
802 | 0 | assertf(st_expands(&t, "-p0C0I0tc32", "--wide-spider", NULL)); |
803 | 0 | assertf(st_expands(&t, "-qgc1", "--tiny-get", NULL)); |
804 | 0 | assertf(st_expands(&t, "-Xc32", "--wide-purge-old", NULL)); |
805 | 0 | assertf(st_expands(&t, "-o2c1", "--tiny-generate-errors", "2")); |
806 | 0 | assertf(t.used == 2); |
807 | | /* elsewhere the alias applies without the count, and says so */ |
808 | 0 | assertf(st_warns(&t, "-O /tmp", "--wide-path", |
809 | 0 | "/tmp")); /* the value is a word of its own */ |
810 | 0 | assertf(st_warns(&t, "+*.gif", "--tiny-allow", "*.gif")); |
811 | 0 | assertf(st_warns(&t, "--clean", "--wide-clean", NULL)); /* a long form */ |
812 | 0 | assertf( |
813 | 0 | st_warns(&t, "-c8", "--wide-sockets", "8")); /* -c8 already: 8 or 32? */ |
814 | 0 | assertf( |
815 | 0 | st_warns(&t, "-c32", "--tiny-wide", NULL)); /* -c32 already: 32 or 1? */ |
816 | 0 | assertf( |
817 | 0 | st_warns(&t, "-N1", "--wide-structure", "1")); /* -N takes a template */ |
818 | 0 | assertf(st_warns(&t, "-%r", "--wide-warc", |
819 | 0 | NULL)); /* -%rc is --warc-cdx, not -%r -c */ |
820 | 0 | assertf(st_warns(&t, "-#h", "--wide-version", |
821 | 0 | NULL)); /* -#h is matched as a whole word */ |
822 | 0 | assertf(st_warns(&t, "-h", "--tiny-help", NULL)); |
823 | | |
824 | | /* the value-taking classes are untouched */ |
825 | 0 | assertf(st_expands(&t, "-C0", "--cache=0", NULL)); |
826 | 0 | assertf(st_expands(&t, "-C0", "--nocache", NULL)); |
827 | 0 | assertf(st_expands(&t, "-C2", "--cache", "2")); |
828 | 0 | assertf(st_expands(&t, "-P proxy:8080", "--proxy", "proxy:8080")); |
829 | 0 | assertf(st_expands(&t, "+*.gif", "--allow", "*.gif")); |
830 | | |
831 | | /* #1426, both directions and table-wide over the "param" rows */ |
832 | 0 | for (i = 0; optalias_value(i)[0] != '\0'; i++) { |
833 | 0 | const int p = optalias_find(optalias_value(i)); |
834 | 0 | char word[HTS_CDLMAXSIZE], want[HTS_CDLMAXSIZE]; |
835 | |
|
836 | 0 | if (optclass_of(p) != HTS_OPT_PARAM) |
837 | 0 | continue; |
838 | 0 | params++; |
839 | 0 | snprintf(word, sizeof(word), "--%s=yes", optalias_value(i)); |
840 | 0 | assertf(st_refuses(&t, word, NULL)); |
841 | 0 | snprintf(word, sizeof(word), "--%s=none", optalias_value(i)); |
842 | 0 | assertf(st_refuses(&t, word, NULL)); |
843 | 0 | snprintf(word, sizeof(word), "--%s=OFF", optalias_value(i)); |
844 | 0 | assertf(st_refuses(&t, word, NULL)); |
845 | 0 | snprintf(word, sizeof(word), "--%s", optalias_value(i)); |
846 | 0 | assertf(st_refuses(&t, word, "http://foo/")); |
847 | | /* control: a number, and the on/off mapping, still pass in silence */ |
848 | 0 | snprintf(word, sizeof(word), "--%s=2", optalias_value(i)); |
849 | 0 | snprintf(want, sizeof(want), "%s2", optreal_value(p)); |
850 | 0 | assertf(st_expands(&t, want, word, NULL)); |
851 | 0 | snprintf(word, sizeof(word), "--%s=off", optalias_value(i)); |
852 | 0 | snprintf(want, sizeof(want), "%s0", optreal_value(p)); |
853 | 0 | assertf(st_expands(&t, want, word, NULL)); |
854 | 0 | snprintf(word, sizeof(word), "--%s=on", optalias_value(i)); |
855 | 0 | assertf(st_expands(&t, optreal_value(p), word, NULL)); |
856 | | /* the bound is on magnitude, so zero padding passes (#1437) */ |
857 | 0 | snprintf(word, sizeof(word), "--%s=00000000000000000002", |
858 | 0 | optalias_value(i)); |
859 | 0 | snprintf(want, sizeof(want), "%s00000000000000000002", optreal_value(p)); |
860 | 0 | assertf(st_expands(&t, want, word, NULL)); |
861 | 0 | snprintf(word, sizeof(word), "--%s=2147483647", optalias_value(i)); |
862 | 0 | snprintf(want, sizeof(want), "%s2147483647", optreal_value(p)); |
863 | 0 | assertf(st_expands(&t, want, word, NULL)); |
864 | 0 | snprintf(word, sizeof(word), "--%s=99999999999999999999", |
865 | 0 | optalias_value(i)); |
866 | 0 | assertf(st_refuses(&t, word, NULL)); |
867 | | /* a sign is not a digit run: -2 would spill into the cluster loop */ |
868 | 0 | snprintf(word, sizeof(word), "--%s=-2", optalias_value(i)); |
869 | 0 | assertf(st_refuses(&t, word, NULL)); |
870 | 0 | snprintf(word, sizeof(word), "--%s=+2", optalias_value(i)); |
871 | 0 | assertf(st_refuses(&t, word, NULL)); |
872 | 0 | } |
873 | 0 | assertf(params >= 27); /* the rows the issue enumerates were all walked */ |
874 | | |
875 | | /* the separator -m and -%c read besides digits, anchored to one occurrence |
876 | | with a digit after it: 1.2.3 otherwise reached maxconn as 1.2 */ |
877 | 0 | assertf(st_expands(&t, "-m,5000", "--max-files=,5000", NULL)); |
878 | 0 | assertf(st_expands(&t, "-m100,5000", "--max-files=100,5000", NULL)); |
879 | 0 | assertf(st_expands(&t, "-%c0.5", "--connection-per-second=0.5", NULL)); |
880 | 0 | assertf(st_expands(&t, "-%c.5", "--connection-per-second=.5", NULL)); |
881 | 0 | assertf(st_refuses(&t, "--connection-per-second=1.2.3", NULL)); |
882 | 0 | assertf(st_refuses(&t, "--connection-per-second=0.", NULL)); |
883 | 0 | assertf(st_refuses(&t, "--max-files=,", NULL)); |
884 | 0 | assertf(st_refuses(&t, "--max-files=100,", NULL)); |
885 | 0 | assertf(st_refuses(&t, "--max-files=,,,5", NULL)); |
886 | 0 | assertf(st_refuses(&t, "--max-files=1,2,3", NULL)); |
887 | 0 | assertf(st_refuses(&t, "--sockets=8,4", NULL)); |
888 | 0 | assertf(st_refuses(&t, "--sockets=0.5", NULL)); |
889 | 0 | assertf(st_refuses(&t, "--advanced-maxlinks=99999999999999999999", NULL)); |
890 | | /* --sockets=8I0 mirrored without a top index: the tail reached -I0 */ |
891 | 0 | assertf(st_refuses(&t, "--sockets=8I0", NULL)); |
892 | | /* an empty value is the bare short form, as it has always been */ |
893 | 0 | assertf(st_expands(&t, "-c", "--sockets=", NULL)); |
894 | | |
895 | | /* --structure glues a preset and detaches a template (#1380): the engine |
896 | | reads a detached -N value as a user template whatever it holds */ |
897 | 0 | assertf(st_expands(&t, "-N1", "--structure=1", NULL)); |
898 | 0 | assertf(st_expands(&t, "-N100", "--structure=100", NULL)); |
899 | 0 | assertf(st_expands(&t, "-N1", "--structure", "1")); |
900 | 0 | assertf(t.used == 2); |
901 | 0 | assertf(st_expands(&t, "-N %h%p/%n%q.%t", "--structure=%h%p/%n%q.%t", NULL)); |
902 | 0 | assertf(t.used == 1); |
903 | 0 | assertf(st_expands(&t, "-N %h%p/%n%q.%t", "--structure", "%h%p/%n%q.%t")); |
904 | 0 | assertf(t.used == 2); |
905 | 0 | assertf( |
906 | 0 | st_expands(&t, "-N %h%p/%n%q.%t", "--user-structure", "%h%p/%n%q.%t")); |
907 | | /* an empty value is -N "", which the engine reads as "back to the default" */ |
908 | 0 | assertf(st_expands(&t, "-N ", "--structure=", NULL)); |
909 | | /* a preset trailed by more short options is refused, even with a % */ |
910 | 0 | assertf(st_refuses(&t, "--structure=1L0", NULL)); |
911 | 0 | assertf(st_refuses(&t, "--structure", "1L0")); |
912 | 0 | assertf(st_refuses(&t, "--structure=1c8", NULL)); |
913 | 0 | assertf(st_refuses(&t, "--structure=1%c8", NULL)); |
914 | 0 | assertf(st_refuses(&t, "--structure=8I0", NULL)); |
915 | | /* on/off are whole values rather than tails, and still map onto the preset */ |
916 | 0 | assertf(st_expands(&t, "-N0", "--structure=off", NULL)); |
917 | | /* both name the default preset: a bare -N would take the next word, and |
918 | | with a URL there it mirrored nothing (#1434) */ |
919 | 0 | assertf(st_expands(&t, "-N0", "--structure=on", NULL)); |
920 | 0 | assertf(st_expands(&t, "-N0", "--structure", "on")); |
921 | 0 | assertf(t.used == 2); |
922 | | /* a template may open with a digit, so the leading digits do not decide it */ |
923 | 0 | assertf(st_expands(&t, "-N 2col/%n.%t", "--structure=2col/%n.%t", NULL)); |
924 | 0 | assertf(st_expands(&t, "-N 2col/%n.%t", "--structure", "2col/%n.%t")); |
925 | | /* past 9 digits sscanf("%d") wraps onto -1, and with no % to make it a |
926 | | template either the value has nowhere left to go */ |
927 | 0 | assertf(st_expands(&t, "-N999999999", "--structure=999999999", NULL)); |
928 | 0 | assertf(st_refuses(&t, "--structure=4294967295", NULL)); |
929 | | /* leading zeros are not part of the number: this is the preset 1 */ |
930 | 0 | assertf(st_expands(&t, "-N0000000001", "--structure=0000000001", NULL)); |
931 | 0 | assertf(st_expands(&t, "-N0000000001", "-N", "0000000001")); |
932 | 0 | assertf(t.used == 2); |
933 | | /* a padded run glues while "-N" plus it fits the output, else stays apart */ |
934 | 0 | { |
935 | 0 | char run[HTS_CDLMAXSIZE], want[HTS_CDLMAXSIZE + 4]; |
936 | 0 | size_t len; |
937 | |
|
938 | 0 | for (len = HTS_CDLMAXSIZE - 3; len <= HTS_CDLMAXSIZE - 2; len++) { |
939 | 0 | memset(run, '0', len - 1); |
940 | 0 | run[len - 1] = '7'; |
941 | 0 | run[len] = '\0'; |
942 | 0 | snprintf(want, sizeof(want), "-N%s%s", |
943 | 0 | len < HTS_CDLMAXSIZE - 2 ? "" : " ", run); |
944 | 0 | assertf(st_expands(&t, want, "-N", run)); |
945 | 0 | } |
946 | 0 | } |
947 | | /* a %-free value would map every URL onto one name, so it is a typo */ |
948 | 0 | assertf(st_refuses(&t, "--structure=OFF", NULL)); |
949 | 0 | assertf(st_refuses(&t, "--structure=flat", NULL)); |
950 | 0 | assertf(st_refuses(&t, "--structure", "none")); |
951 | 0 | assertf(st_refuses(&t, "--structure=-1", NULL)); |
952 | | /* --user-structure is how to ask for such a value on purpose */ |
953 | 0 | assertf(st_expands(&t, "-N flat", "--user-structure", "flat")); |
954 | | /* the short form agrees, rather than reading -N 1 as a template named 1 */ |
955 | 0 | assertf(st_expands(&t, "-N1", "-N", "1")); |
956 | 0 | assertf(t.used == 2); |
957 | 0 | assertf(st_expands(&t, "-N %h%p/%n%q.%t", "-N", "%h%p/%n%q.%t")); |
958 | 0 | assertf(t.used == 2); |
959 | | /* there only a bare digit run glues, and anything else stays a template */ |
960 | 0 | assertf(st_expands(&t, "-N 2col/%n.%t", "-N", "2col/%n.%t")); |
961 | 0 | assertf(t.used == 2); |
962 | 0 | assertf(st_expands(&t, "-N 4294967295", "-N", "4294967295")); |
963 | 0 | assertf(t.used == 2); |
964 | 0 | assertf(st_expands(&t, "-N 1L0", "-N", "1L0")); |
965 | 0 | assertf(t.used == 2); |
966 | | /* an empty word is still the template, not a URL */ |
967 | 0 | assertf(st_expands(&t, "-N ", "-N", "")); |
968 | 0 | assertf(t.used == 2); |
969 | | /* a template may open with '-', but not name an option or be missing */ |
970 | 0 | assertf(st_expands(&t, "-N -my%n", "-N", "-my%n")); |
971 | 0 | assertf(t.used == 2); |
972 | 0 | assertf(st_expands(&t, "-N", "-N", "-q")); |
973 | 0 | assertf(t.used == 1); |
974 | 0 | assertf(st_expands(&t, "-N", "-N", "-r1")); |
975 | 0 | assertf(t.used == 1); |
976 | 0 | assertf(st_expands(&t, "-N", "-N", NULL)); |
977 | 0 | assertf(t.used == 1); |
978 | | |
979 | | /* each -# row maps to the arm its name describes (#427) */ |
980 | 0 | assertf(st_expands(&t, "-#E", "--extract-cache", NULL)); |
981 | 0 | assertf(st_expands(&t, "-#C *.gif", "--debug-cache", "*.gif")); |
982 | 0 | assertf(st_expands(&t, "-#t", "--autotest", NULL)); |
983 | 0 | assertf(st_expands(&t, "-#T", "--debug-xfrstats", NULL)); |
984 | 0 | assertf(st_refuses(&t, "--debug-testfilters", NULL)); |
985 | 0 | assertf(st_refuses(&t, "--extract-cache=1", NULL)); |
986 | | /* and the reverse lookup the generated help prints takes the first row */ |
987 | 0 | assertf(strcmp(optalias_value(optreal_find("-#E")), "extract-cache") == 0); |
988 | 0 | assertf(strcmp(optalias_value(optreal_find("-#C")), "debug-cache") == 0); |
989 | 0 | assertf(strcmp(optalias_value(optreal_find("-#t")), "autotest") == 0); |
990 | 0 | assertf(strcmp(optalias_value(optreal_find("-#T")), "debug-xfrstats") == 0); |
991 | | |
992 | | /* invariant: every name's bare long form still emits its short form, and a |
993 | | param name still demands its own value. A duplicate name (test, continue) |
994 | | resolves to its first row */ |
995 | 0 | for (i = 0; optalias_value(i)[0] != '\0'; i++) { |
996 | 0 | const int p = optalias_find(optalias_value(i)); |
997 | 0 | char word[HTS_CDLMAXSIZE]; |
998 | |
|
999 | 0 | assertf(p >= 0); |
1000 | | /* every row's class is one of the seven, and its name reads back */ |
1001 | 0 | assertf(optclass_of(i) != HTS_OPT_UNKNOWN); |
1002 | 0 | assertf(strcmp(optclass_name(optclass_of(i)), hts_optalias[i][2]) == 0); |
1003 | 0 | snprintf(word, sizeof(word), "--%s", optalias_value(i)); |
1004 | 0 | if (optclass_takes_param(optclass_of(p))) { |
1005 | 0 | assertf(st_refuses(&t, word, NULL)); |
1006 | 0 | } else { |
1007 | 0 | assertf(st_expands(&t, optreal_value(p), word, NULL)); |
1008 | 0 | assertf(t.used == 1); |
1009 | 0 | } |
1010 | 0 | } |
1011 | 0 | assertf(i > 100); /* the table was walked, not skipped */ |
1012 | | |
1013 | 0 | printf("optalias self-test OK\n"); |
1014 | 0 | return 0; |
1015 | 0 | } |
1016 | | |
1017 | | /* Runs hts_scan_token() into the first destsize bytes of an arena poisoned to |
1018 | | its end, and reports whether anything past them moved. */ |
1019 | | static hts_boolean st_scantoken_run(char *src, size_t destsize, hts_buf arena, |
1020 | 0 | hts_boolean *fit, size_t *advance) { |
1021 | 0 | char *p = src; |
1022 | 0 | size_t i; |
1023 | |
|
1024 | 0 | assertf(destsize <= arena.size); |
1025 | | /* poisoned with '#', not 0, or the stray NUL of an off-by-one would read as |
1026 | | untouched */ |
1027 | 0 | memset(arena.p, '#', arena.size); |
1028 | 0 | *fit = hts_scan_token(&p, arena.p, destsize); |
1029 | 0 | *advance = (size_t) (p - src); |
1030 | 0 | for (i = destsize; i < arena.size; i++) { |
1031 | 0 | if (arena.p[i] != '#') |
1032 | 0 | return HTS_FALSE; |
1033 | 0 | } |
1034 | 0 | return HTS_TRUE; |
1035 | 0 | } |
1036 | | |
1037 | | /* A token longer than the buffer it is copied into must be refused, never |
1038 | | written past the end (#1271). */ |
1039 | 0 | static int st_scantoken(httrackp *opt, int argc, char **argv) { |
1040 | | /* the engine's own filter bound, and a small one the same code must honour */ |
1041 | 0 | const size_t caps[] = {8, HTS_URLMAXSIZE * 2 - 1}; |
1042 | 0 | const size_t guard = 64; |
1043 | 0 | size_t c; |
1044 | |
|
1045 | 0 | for (c = 0; c < sizeof(caps) / sizeof(caps[0]); c++) { |
1046 | 0 | const size_t destsize = caps[c]; |
1047 | 0 | const size_t room = destsize - 1; /* characters, the NUL aside */ |
1048 | | /* empty, short, and the four lengths that straddle the bound */ |
1049 | 0 | const size_t lens[] = {0, 1, room - 1, room, room + 1, room + guard}; |
1050 | 0 | char *arena = malloct(destsize + guard); |
1051 | 0 | char *src = malloct(room + guard + 8); |
1052 | 0 | size_t k; |
1053 | |
|
1054 | 0 | assertf(arena != NULL && src != NULL); |
1055 | 0 | for (k = 0; k < sizeof(lens) / sizeof(lens[0]); k++) { |
1056 | 0 | const size_t len = lens[k]; |
1057 | 0 | const size_t kept = len < room ? len : room; |
1058 | 0 | hts_boolean fit; |
1059 | 0 | size_t advance, i; |
1060 | | |
1061 | | /* varied bytes catch a copy landing on the wrong index */ |
1062 | 0 | for (i = 0; i < len; i++) |
1063 | 0 | src[i] = (char) ('a' + i % 26); |
1064 | 0 | memcpy(src + len, " \tb", 4); |
1065 | 0 | assertf(st_scantoken_run( |
1066 | 0 | src, destsize, hts_buf_at(arena, destsize + guard), &fit, &advance)); |
1067 | 0 | assertf(fit == (len <= room ? HTS_TRUE : HTS_FALSE)); |
1068 | 0 | assertf(strlen(arena) == kept && memcmp(arena, src, kept) == 0); |
1069 | 0 | assertf(advance == len + 2); |
1070 | | |
1071 | | /* when the token ends the string, the cursor stops on its NUL */ |
1072 | 0 | src[len] = '\0'; |
1073 | 0 | assertf(st_scantoken_run( |
1074 | 0 | src, destsize, hts_buf_at(arena, destsize + guard), &fit, &advance)); |
1075 | 0 | assertf(fit == (len <= room ? HTS_TRUE : HTS_FALSE)); |
1076 | 0 | assertf(strlen(arena) == kept && memcmp(arena, src, kept) == 0); |
1077 | 0 | assertf(advance == len); |
1078 | 0 | } |
1079 | 0 | freet(arena); |
1080 | 0 | freet(src); |
1081 | 0 | } |
1082 | 0 | printf("scantoken self-test OK\n"); |
1083 | 0 | return 0; |
1084 | 0 | } |
1085 | | |
1086 | | /* Runs one http_postfile_body() case into a block before a poisoned guard. */ |
1087 | | static int st_postfile_case(const char *path, size_t start, size_t expect, |
1088 | 0 | const char *what) { |
1089 | 0 | enum { capacity = 64, guard = 16 }; |
1090 | |
|
1091 | 0 | char *arena = malloct(capacity + guard); |
1092 | 0 | FILE *fp = FOPEN(path, "rb"); |
1093 | 0 | size_t pos; |
1094 | 0 | size_t i; |
1095 | 0 | int err = 0; |
1096 | |
|
1097 | 0 | if (arena == NULL || fp == NULL) { |
1098 | 0 | printf(" FAIL %s: cannot open %s\n", what, path); |
1099 | 0 | freet(arena); |
1100 | 0 | if (fp != NULL) |
1101 | 0 | fclose(fp); |
1102 | 0 | return 1; |
1103 | 0 | } |
1104 | 0 | memset(arena, '#', capacity + guard); |
1105 | 0 | arena[capacity + guard - 1] = '\0'; /* a runaway strlen ends here, not past */ |
1106 | 0 | memset(arena, 'R', start); /* what the request line already wrote */ |
1107 | 0 | arena[start] = '\0'; |
1108 | |
|
1109 | 0 | pos = http_postfile_body(hts_buf_at(arena, capacity), start, fp); |
1110 | 0 | fclose(fp); |
1111 | |
|
1112 | 0 | if (pos != expect) { |
1113 | 0 | printf(" FAIL %s: position %d, expected %d\n", what, (int) pos, |
1114 | 0 | (int) expect); |
1115 | 0 | err = 1; |
1116 | 0 | } else if (arena[pos] != '\0') { |
1117 | 0 | printf(" FAIL %s: no terminator at %d\n", what, (int) pos); |
1118 | 0 | err = 1; |
1119 | 0 | } |
1120 | 0 | for (i = capacity; i + 1 < capacity + guard; i++) { |
1121 | 0 | if (arena[i] != '#') { |
1122 | 0 | printf(" FAIL %s: guard byte %d written\n", what, (int) (i - capacity)); |
1123 | 0 | err = 1; |
1124 | 0 | break; |
1125 | 0 | } |
1126 | 0 | } |
1127 | 0 | freet(arena); |
1128 | 0 | return err; |
1129 | 0 | } |
1130 | | |
1131 | | /* The argv fixtures are short, long, NUL-holding and empty >postfile bodies. */ |
1132 | 0 | static int st_postfile(httrackp *opt, int argc, char **argv) { |
1133 | 0 | int err = 0; |
1134 | |
|
1135 | 0 | if (argc < 4) { |
1136 | 0 | printf("usage: -#test=postfile <short> <long> <embedded-nul> <empty>\n"); |
1137 | 0 | return 1; |
1138 | 0 | } |
1139 | | /* room left is 64 - 8 - 1 = 55 bytes */ |
1140 | 0 | err |= st_postfile_case(argv[0], 8, 8 + 3, "short"); |
1141 | 0 | err |= st_postfile_case(argv[1], 8, 8 + 55, "long"); |
1142 | 0 | err |= st_postfile_case(argv[2], 8, 8 + 2, "embedded-nul"); |
1143 | 0 | err |= st_postfile_case(argv[3], 8, 8, "empty"); |
1144 | |
|
1145 | 0 | return st_verdict("postfile self-test", err); |
1146 | 0 | } |
1147 | | |
1148 | | // -#test=abortlock|stoplock <dir>: hts_take_lock_request() takes a request only |
1149 | | // when it is stamped after this run's hts-in_progress.lock and the engine could |
1150 | | // also delete it. It leaves every other lock file alone. |
1151 | 0 | static hts_boolean st_lockrule_touch(const char *path) { |
1152 | 0 | FILE *const fp = FOPEN(path, "wb"); |
1153 | |
|
1154 | 0 | if (fp == NULL) |
1155 | 0 | return HTS_FALSE; |
1156 | 0 | fclose(fp); |
1157 | 0 | return HTS_TRUE; |
1158 | 0 | } |
1159 | | |
1160 | 0 | static hts_boolean st_lockrule_stamp(const char *path, time_t when) { |
1161 | 0 | STRUCT_UTIMBUF times; |
1162 | |
|
1163 | 0 | times.actime = times.modtime = when; |
1164 | 0 | return UTIME(path, ×) == 0 ? HTS_TRUE : HTS_FALSE; |
1165 | 0 | } |
1166 | | |
1167 | | /* Stamp PATH at SEC plus NSEC, both in the units hts_file_mtime() reports, so |
1168 | | the caller reads SEC back rather than taking it from a clock. False on |
1169 | | failure, and NSEC is dropped where this build has no sub-second call. */ |
1170 | | static hts_boolean st_lockrule_stamp_ns(const char *path, int64_t sec, |
1171 | 0 | int32_t nsec) { |
1172 | | #if defined(_WIN32) |
1173 | | LPWSTR wpath = hts_pathToUCS2(path); |
1174 | | ULARGE_INTEGER ticks; |
1175 | | FILETIME ft; |
1176 | | HANDLE h; |
1177 | | hts_boolean ok; |
1178 | | |
1179 | | if (wpath == NULL) |
1180 | | return HTS_FALSE; |
1181 | | h = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, |
1182 | | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
1183 | | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
1184 | | freet(wpath); |
1185 | | if (h == INVALID_HANDLE_VALUE) |
1186 | | return HTS_FALSE; |
1187 | | ticks.QuadPart = (ULONGLONG) sec * 10000000ULL + (ULONGLONG) (nsec / 100); |
1188 | | ft.dwLowDateTime = ticks.LowPart; |
1189 | | ft.dwHighDateTime = ticks.HighPart; |
1190 | | ok = SetFileTime(h, NULL, NULL, &ft) ? HTS_TRUE : HTS_FALSE; |
1191 | | CloseHandle(h); |
1192 | | return ok; |
1193 | | #elif defined(HAVE_UTIMENSAT) |
1194 | | struct timespec times[2]; |
1195 | |
|
1196 | 0 | times[0].tv_sec = times[1].tv_sec = (time_t) sec; |
1197 | 0 | times[0].tv_nsec = times[1].tv_nsec = (long) nsec; |
1198 | 0 | return utimensat(AT_FDCWD, path, times, 0) == 0 ? HTS_TRUE : HTS_FALSE; |
1199 | | #else |
1200 | | return st_lockrule_stamp(path, (time_t) sec); |
1201 | | #endif |
1202 | 0 | } |
1203 | | |
1204 | | /* Does hts_file_mtime() report the seconds and nanoseconds the filesystem |
1205 | | holds? It is always true on Windows and without a nanosecond field. */ |
1206 | | static hts_boolean st_lockrule_time_matches_stat(const char *path, int64_t sec, |
1207 | 0 | int32_t nsec) { |
1208 | 0 | #if !defined(_WIN32) && (defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC) || \ |
1209 | 0 | defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)) |
1210 | 0 | STRUCT_STAT buf; |
1211 | 0 | long raw; |
1212 | |
|
1213 | 0 | if (STAT(path, &buf) != 0) |
1214 | 0 | return HTS_FALSE; |
1215 | 0 | #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC) |
1216 | 0 | raw = (long) buf.st_mtim.tv_nsec; |
1217 | | #else |
1218 | | raw = (long) buf.st_mtimespec.tv_nsec; |
1219 | | #endif |
1220 | 0 | return sec == (int64_t) buf.st_mtime && nsec == (int32_t) raw ? HTS_TRUE |
1221 | 0 | : HTS_FALSE; |
1222 | | #else |
1223 | | (void) path; |
1224 | | (void) sec; |
1225 | | (void) nsec; |
1226 | | return HTS_TRUE; |
1227 | | #endif |
1228 | 0 | } |
1229 | | |
1230 | | /* Tests a request in the mirror's start second. It reports what covered it, |
1231 | | because whole-second filesystems hold none of these stamps. */ |
1232 | | static int st_lockrule_subsecond(httrackp *opt, const char *tag, |
1233 | | const char *name, const char *lock, |
1234 | 0 | const char *progress) { |
1235 | | /* Tests 436 and 459 write their request a microsecond after the lock. */ |
1236 | 0 | const int32_t later = 500001000, same = 500000000, earlier = 499999000; |
1237 | 0 | hts_filetime_t started, probe; |
1238 | 0 | int err = 0; |
1239 | |
|
1240 | 0 | if (!hts_file_mtime(progress, &started)) { |
1241 | 0 | fprintf(stderr, "%s: %s has no timestamp\n", tag, progress); |
1242 | 0 | return 1; |
1243 | 0 | } |
1244 | 0 | if (!st_lockrule_touch(lock) || |
1245 | 0 | !st_lockrule_stamp_ns(lock, started.sec, later) || |
1246 | 0 | !hts_file_mtime(lock, &probe)) { |
1247 | 0 | fprintf(stderr, "%s: cannot stamp %s\n", tag, lock); |
1248 | 0 | return 1; |
1249 | 0 | } |
1250 | | /* Before believing the read back, pin it to what the filesystem holds. */ |
1251 | 0 | if (!st_lockrule_time_matches_stat(lock, probe.sec, probe.nsec)) { |
1252 | 0 | fprintf(stderr, |
1253 | 0 | "%s: hts_file_mtime() does not report %s's own seconds and" |
1254 | 0 | " nanoseconds\n", |
1255 | 0 | tag, lock); |
1256 | 0 | (void) UNLINK(lock); |
1257 | 0 | return 1; |
1258 | 0 | } |
1259 | 0 | if (probe.sec != started.sec || probe.nsec != later) { |
1260 | | /* FAT and exFAT round the stamp to even seconds. */ |
1261 | 0 | printf("%s: same-second request: NOT COVERED (asked for %d ns, read back" |
1262 | 0 | " %d%s)\n", |
1263 | 0 | tag, (int) later, (int) probe.nsec, |
1264 | 0 | probe.sec != started.sec ? ", in another second" : ""); |
1265 | 0 | (void) UNLINK(lock); |
1266 | 0 | return err; |
1267 | 0 | } |
1268 | | |
1269 | 0 | if (!st_lockrule_stamp_ns(progress, started.sec, same)) { |
1270 | 0 | fprintf(stderr, "%s: cannot stamp %s\n", tag, progress); |
1271 | 0 | return 1; |
1272 | 0 | } |
1273 | 0 | if (!hts_take_lock_request(opt, name)) { |
1274 | 0 | fprintf(stderr, |
1275 | 0 | "%s: a request made in the second the mirror started was" |
1276 | 0 | " ignored\n", |
1277 | 0 | tag); |
1278 | 0 | err = 1; |
1279 | 0 | } |
1280 | 0 | if (fexist_utf8(lock)) { |
1281 | 0 | fprintf(stderr, "%s: the request outlived the action it caused\n", tag); |
1282 | 0 | err = 1; |
1283 | 0 | } |
1284 | | |
1285 | | /* Equal to the nanosecond is not later, so this is still an earlier run's. */ |
1286 | 0 | if (!st_lockrule_touch(lock) || |
1287 | 0 | !st_lockrule_stamp_ns(lock, started.sec, same)) { |
1288 | 0 | fprintf(stderr, "%s: cannot stamp %s\n", tag, lock); |
1289 | 0 | return 1; |
1290 | 0 | } |
1291 | 0 | if (hts_take_lock_request(opt, name)) { |
1292 | 0 | fprintf(stderr, |
1293 | 0 | "%s: a request stamped exactly when the mirror started was" |
1294 | 0 | " taken\n", |
1295 | 0 | tag); |
1296 | 0 | err = 1; |
1297 | 0 | } |
1298 | | |
1299 | | /* The lock's sub-second part must be read here. The case above deleted the |
1300 | | lock, so it is re-created. */ |
1301 | 0 | if (!st_lockrule_touch(lock) || |
1302 | 0 | !st_lockrule_stamp_ns(lock, started.sec, earlier)) { |
1303 | 0 | fprintf(stderr, "%s: cannot stamp %s\n", tag, lock); |
1304 | 0 | return 1; |
1305 | 0 | } |
1306 | 0 | if (hts_take_lock_request(opt, name)) { |
1307 | 0 | fprintf(stderr, |
1308 | 0 | "%s: a request older than the mirror by a fraction was taken\n", |
1309 | 0 | tag); |
1310 | 0 | err = 1; |
1311 | 0 | } |
1312 | 0 | if (!fexist_utf8(lock)) { |
1313 | 0 | fprintf(stderr, "%s: a request the engine refused was deleted\n", tag); |
1314 | 0 | err = 1; |
1315 | 0 | } |
1316 | | |
1317 | | /* An earlier second is earlier despite the fraction, or a request left |
1318 | | by the run before would be taken for one aimed at this mirror. */ |
1319 | 0 | if (!st_lockrule_stamp_ns(lock, started.sec - 1, later)) { |
1320 | 0 | fprintf(stderr, "%s: cannot stamp %s\n", tag, lock); |
1321 | 0 | return 1; |
1322 | 0 | } |
1323 | 0 | if (hts_take_lock_request(opt, name)) { |
1324 | 0 | fprintf(stderr, |
1325 | 0 | "%s: a request from the second before the mirror started was" |
1326 | 0 | " taken\n", |
1327 | 0 | tag); |
1328 | 0 | err = 1; |
1329 | 0 | } |
1330 | 0 | if (!fexist_utf8(lock)) { |
1331 | 0 | fprintf(stderr, "%s: a request the engine refused was deleted\n", tag); |
1332 | 0 | err = 1; |
1333 | 0 | } |
1334 | 0 | (void) UNLINK(lock); |
1335 | | |
1336 | | /* Backdating keeps the fraction, or the second it buys the engine would be |
1337 | | short by up to another one. */ |
1338 | 0 | if (!hts_file_backdate(progress, 1) || !hts_file_mtime(progress, &probe)) { |
1339 | 0 | fprintf(stderr, "%s: cannot backdate %s\n", tag, progress); |
1340 | 0 | return 1; |
1341 | 0 | } |
1342 | 0 | if (probe.sec != started.sec - 1 || probe.nsec != same) { |
1343 | 0 | fprintf(stderr, |
1344 | 0 | "%s: backdating %s moved it by something other than one second\n", |
1345 | 0 | tag, progress); |
1346 | 0 | err = 1; |
1347 | 0 | } |
1348 | 0 | printf("%s: same-second request: covered (stamps a microsecond apart)\n", |
1349 | 0 | tag); |
1350 | 0 | return err; |
1351 | 0 | } |
1352 | | |
1353 | | /* The other request file, which taking NAME must leave untouched. */ |
1354 | 0 | static const char *st_lockrule_other(const char *name) { |
1355 | 0 | return strcmp(name, HTS_ABORT_LOCKNAME) == 0 ? HTS_PAUSE_LOCKNAME |
1356 | 0 | : HTS_ABORT_LOCKNAME; |
1357 | 0 | } |
1358 | | |
1359 | | /* Set up <dir>/, its progress lock and both requests, NAME stamped at WHEN. */ |
1360 | | static hts_boolean st_lockrule_setup(const char *dir, const char *name, |
1361 | | hts_buf lock, hts_buf progress, |
1362 | 0 | hts_buf other, time_t when) { |
1363 | 0 | hts_buf_cpy(lock, dir); |
1364 | 0 | hts_buf_cat(lock, name); |
1365 | 0 | hts_buf_cpy(progress, dir); |
1366 | 0 | hts_buf_cat(progress, "hts-in_progress.lock"); |
1367 | 0 | hts_buf_cpy(other, dir); |
1368 | 0 | hts_buf_cat(other, st_lockrule_other(name)); |
1369 | 0 | structcheck(lock.p); |
1370 | 0 | return st_lockrule_touch(progress.p) && st_lockrule_touch(other.p) && |
1371 | 0 | st_lockrule_touch(lock.p) && st_lockrule_stamp(lock.p, when); |
1372 | 0 | } |
1373 | | |
1374 | | /* A request the engine cannot remove must never be taken. */ |
1375 | | static int st_lockrule_undeletable(httrackp *opt, const char *base, |
1376 | 0 | const char *tag, const char *name) { |
1377 | | #ifdef _WIN32 |
1378 | | (void) base; |
1379 | | (void) name; |
1380 | | /* That needs a sharing violation, held by a second process. */ |
1381 | | printf("%s: undeletable request: NOT COVERED (no portable way to make" |
1382 | | " DeleteFile fail here)\n", |
1383 | | tag); |
1384 | | return 0; |
1385 | | #else |
1386 | 0 | char BIGSTK rodir[HTS_URLMAXSIZE]; |
1387 | 0 | char BIGSTK rolock[HTS_URLMAXSIZE * 2]; |
1388 | 0 | char BIGSTK roprogress[HTS_URLMAXSIZE * 2]; |
1389 | 0 | char BIGSTK roother[HTS_URLMAXSIZE * 2]; |
1390 | 0 | const uid_t self = geteuid(); |
1391 | 0 | uid_t drop = self; |
1392 | 0 | int rofd; |
1393 | 0 | int poll; |
1394 | 0 | int err = 0; |
1395 | |
|
1396 | 0 | strlcpybuff(rodir, base, sizeof(rodir)); |
1397 | 0 | strlcatbuff(rodir, "readonly/", sizeof(rodir)); |
1398 | 0 | if (!st_lockrule_setup(rodir, name, HTS_BUF(rolock), HTS_BUF(roprogress), |
1399 | 0 | HTS_BUF(roother), time(NULL) + 60)) { |
1400 | 0 | fprintf(stderr, "%s: cannot set up %s\n", tag, rolock); |
1401 | 0 | return 1; |
1402 | 0 | } |
1403 | | /* One descriptor for both mode changes, so the name is resolved once. */ |
1404 | 0 | rofd = open(rodir, O_RDONLY | O_DIRECTORY); |
1405 | 0 | if (rofd == -1) { |
1406 | 0 | fprintf(stderr, "%s: cannot open %s\n", tag, rodir); |
1407 | 0 | return 1; |
1408 | 0 | } |
1409 | | /* Keep it readable, or an engine that saw no request would pass. */ |
1410 | 0 | if (fchmod(rofd, 0555) != 0) { |
1411 | 0 | fprintf(stderr, "%s: cannot make %s read-only\n", tag, rodir); |
1412 | 0 | close(rofd); |
1413 | 0 | return 1; |
1414 | 0 | } |
1415 | | /* Root may write to a read-only directory, so borrow an unprivileged euid. */ |
1416 | 0 | if (self == 0) { |
1417 | 0 | const struct passwd *const pw = getpwnam("nobody"); |
1418 | |
|
1419 | 0 | drop = pw != NULL ? pw->pw_uid : (uid_t) 65534; |
1420 | 0 | if (seteuid(drop) != 0) { |
1421 | 0 | printf("%s: undeletable request: NOT COVERED (root, and euid %d " |
1422 | 0 | "is not reachable)\n", |
1423 | 0 | tag, (int) drop); |
1424 | 0 | (void) fchmod(rofd, 0700); |
1425 | 0 | close(rofd); |
1426 | 0 | return 0; |
1427 | 0 | } |
1428 | 0 | } |
1429 | | |
1430 | 0 | StringCopy(opt->path_log, rodir); |
1431 | | /* Probing by removal keeps no stat that could go stale before the engine's |
1432 | | own unlink, and ENOENT means the euid cannot reach the request at all. */ |
1433 | 0 | errno = 0; |
1434 | 0 | if (UNLINK(rolock) == 0) { |
1435 | 0 | printf("%s: undeletable request: NOT COVERED (euid %d could still" |
1436 | 0 | " delete %s)\n", |
1437 | 0 | tag, (int) drop, rolock); |
1438 | 0 | } else if (errno == ENOENT) { |
1439 | 0 | printf("%s: undeletable request: NOT COVERED (%s is out of reach of" |
1440 | 0 | " euid %d)\n", |
1441 | 0 | tag, rodir, (int) drop); |
1442 | 0 | } else { |
1443 | 0 | for (poll = 0; poll < 2; poll++) { |
1444 | 0 | if (hts_take_lock_request(opt, name)) { |
1445 | 0 | fprintf(stderr, |
1446 | 0 | "%s: a request that cannot be deleted was taken (poll %d)\n", |
1447 | 0 | tag, poll); |
1448 | 0 | err = 1; |
1449 | 0 | } |
1450 | 0 | } |
1451 | 0 | errno = 0; |
1452 | 0 | if (UNLINK(rolock) == 0 || errno == ENOENT) { |
1453 | 0 | fprintf(stderr, "%s: the undeletable request went away\n", tag); |
1454 | 0 | err = 1; |
1455 | 0 | } |
1456 | 0 | printf("%s: undeletable request: covered (unwritable directory," |
1457 | 0 | " euid %d)\n", |
1458 | 0 | tag, (int) drop); |
1459 | 0 | } |
1460 | |
|
1461 | 0 | if (self == 0 && seteuid(self) != 0) { |
1462 | 0 | fprintf(stderr, "%s: cannot get euid %d back\n", tag, (int) self); |
1463 | 0 | err = 1; |
1464 | 0 | } |
1465 | 0 | #ifdef __linux__ |
1466 | | /* The euid switch clears the dumpable flag, which LeakSanitizer needs. */ |
1467 | 0 | if (self == 0) { |
1468 | 0 | (void) prctl(PR_SET_DUMPABLE, 1, 0, 0, 0); |
1469 | 0 | } |
1470 | 0 | #endif |
1471 | 0 | (void) fchmod(rofd, 0700); |
1472 | 0 | close(rofd); |
1473 | 0 | return err; |
1474 | 0 | #endif |
1475 | 0 | } |
1476 | | |
1477 | | static int st_lockrule(httrackp *opt, int argc, char **argv, const char *tag, |
1478 | 0 | const char *name) { |
1479 | 0 | char BIGSTK base[HTS_URLMAXSIZE]; |
1480 | 0 | char BIGSTK lock[HTS_URLMAXSIZE * 2]; |
1481 | 0 | char BIGSTK progress[HTS_URLMAXSIZE * 2]; |
1482 | 0 | char BIGSTK other[HTS_URLMAXSIZE * 2]; |
1483 | 0 | String saved = STRING_EMPTY; |
1484 | 0 | time_t started; |
1485 | 0 | int err = 0; |
1486 | |
|
1487 | 0 | if (argc < 1) { |
1488 | 0 | fprintf(stderr, "usage: -#test=%s <writable directory>\n", tag); |
1489 | 0 | return 1; |
1490 | 0 | } |
1491 | 0 | strcpybuff(base, argv[0]); |
1492 | 0 | if (base[0] != '\0' && hts_lastchar(base) != '/') |
1493 | 0 | strcatbuff(base, "/"); |
1494 | | /* The engine still has to shut down through its own path_log. */ |
1495 | 0 | StringCopy(saved, StringBuff(opt->path_log)); |
1496 | 0 | StringCopy(opt->path_log, base); |
1497 | |
|
1498 | 0 | strlcpybuff(lock, base, sizeof(lock)); |
1499 | 0 | strlcatbuff(lock, name, sizeof(lock)); |
1500 | 0 | structcheck(lock); |
1501 | 0 | if (hts_take_lock_request(opt, name)) { |
1502 | 0 | fprintf(stderr, "%s: a mirror with no request took one\n", tag); |
1503 | 0 | err = 1; |
1504 | 0 | } |
1505 | |
|
1506 | 0 | if (!st_lockrule_setup(base, name, HTS_BUF(lock), HTS_BUF(progress), |
1507 | 0 | HTS_BUF(other), time(NULL))) { |
1508 | 0 | fprintf(stderr, "%s: cannot set up %s\n", tag, base); |
1509 | 0 | err = 1; |
1510 | 0 | } |
1511 | 0 | started = get_filetime(progress); |
1512 | 0 | if (started == (time_t) -1) { |
1513 | 0 | fprintf(stderr, "%s: %s has no timestamp\n", tag, progress); |
1514 | 0 | err = 1; |
1515 | 0 | } |
1516 | | |
1517 | | /* An earlier mirror left this in the run's start second. It pins the |
1518 | | boundary only on whole-second filesystems. */ |
1519 | 0 | if (!st_lockrule_stamp(lock, started)) { |
1520 | 0 | fprintf(stderr, "%s: cannot stamp %s\n", tag, lock); |
1521 | 0 | err = 1; |
1522 | 0 | } |
1523 | 0 | if (hts_take_lock_request(opt, name)) { |
1524 | 0 | fprintf(stderr, |
1525 | 0 | "%s: a request no newer than the mirror it found was taken\n", tag); |
1526 | 0 | err = 1; |
1527 | 0 | } |
1528 | 0 | if (!fexist_utf8(lock)) { |
1529 | 0 | fprintf(stderr, "%s: a request the engine refused was deleted\n", tag); |
1530 | 0 | err = 1; |
1531 | 0 | } |
1532 | | |
1533 | | /* The engine backdates its own progress lock by a second, so a request |
1534 | | written the instant that lock appears is newer than it. */ |
1535 | 0 | if (!hts_file_backdate(progress, 1)) { |
1536 | 0 | fprintf(stderr, "%s: cannot backdate %s\n", tag, progress); |
1537 | 0 | err = 1; |
1538 | 0 | } |
1539 | 0 | if (!hts_take_lock_request(opt, name)) { |
1540 | 0 | fprintf(stderr, |
1541 | 0 | "%s: with the progress lock backdated, a request stamped when the" |
1542 | 0 | " mirror started was still refused\n", |
1543 | 0 | tag); |
1544 | 0 | err = 1; |
1545 | 0 | } |
1546 | | /* A minute earlier is an earlier run's, backdating or not. */ |
1547 | 0 | if (!st_lockrule_touch(lock) || !st_lockrule_stamp(lock, started - 60)) { |
1548 | 0 | fprintf(stderr, "%s: cannot stamp %s\n", tag, lock); |
1549 | 0 | err = 1; |
1550 | 0 | } |
1551 | 0 | if (hts_take_lock_request(opt, name)) { |
1552 | 0 | fprintf(stderr, "%s: a request a minute older than the mirror was taken\n", |
1553 | 0 | tag); |
1554 | 0 | err = 1; |
1555 | 0 | } |
1556 | 0 | if (!fexist_utf8(lock)) { |
1557 | 0 | fprintf(stderr, "%s: a request the engine refused was deleted\n", tag); |
1558 | 0 | err = 1; |
1559 | 0 | } |
1560 | 0 | if (!st_lockrule_stamp(progress, started)) { |
1561 | 0 | fprintf(stderr, "%s: cannot put %s back to when the mirror started\n", tag, |
1562 | 0 | progress); |
1563 | 0 | err = 1; |
1564 | 0 | } |
1565 | | |
1566 | | /* Stamped after the mirror started: aimed at this run. */ |
1567 | 0 | if (!st_lockrule_touch(lock) || !st_lockrule_stamp(lock, started + 1)) { |
1568 | 0 | fprintf(stderr, "%s: cannot stamp %s\n", tag, lock); |
1569 | 0 | err = 1; |
1570 | 0 | } |
1571 | 0 | if (!hts_take_lock_request(opt, name)) { |
1572 | 0 | fprintf(stderr, "%s: a request the engine can delete was ignored\n", tag); |
1573 | 0 | err = 1; |
1574 | 0 | } |
1575 | 0 | if (fexist_utf8(lock)) { |
1576 | 0 | fprintf(stderr, "%s: the request outlived the action it caused\n", tag); |
1577 | 0 | err = 1; |
1578 | 0 | } |
1579 | 0 | if (!fexist_utf8(progress) || !fexist_utf8(other)) { |
1580 | 0 | fprintf(stderr, "%s: taking a request deleted a neighbouring lock\n", tag); |
1581 | 0 | err = 1; |
1582 | 0 | } |
1583 | 0 | if (hts_take_lock_request(opt, name)) { |
1584 | 0 | fprintf(stderr, "%s: one request was taken twice\n", tag); |
1585 | 0 | err = 1; |
1586 | 0 | } |
1587 | |
|
1588 | 0 | err |= st_lockrule_subsecond(opt, tag, name, lock, progress); |
1589 | | |
1590 | | /* No progress lock means no mirror to reach, whatever the request says. */ |
1591 | 0 | (void) UNLINK(progress); |
1592 | 0 | if (!st_lockrule_touch(lock) || !st_lockrule_stamp(lock, time(NULL) + 60)) { |
1593 | 0 | fprintf(stderr, "%s: cannot re-create %s\n", tag, lock); |
1594 | 0 | err = 1; |
1595 | 0 | } |
1596 | 0 | if (hts_take_lock_request(opt, name)) { |
1597 | 0 | fprintf(stderr, "%s: a request reached a mirror that never ran\n", tag); |
1598 | 0 | err = 1; |
1599 | 0 | } |
1600 | |
|
1601 | 0 | err |= st_lockrule_undeletable(opt, base, tag, name); |
1602 | |
|
1603 | 0 | StringCopy(opt->path_log, StringBuff(saved)); |
1604 | 0 | StringFree(saved); |
1605 | 0 | printf("%s self-test: %s\n", tag, err ? "FAIL" : "OK"); |
1606 | 0 | return err; |
1607 | 0 | } |
1608 | | |
1609 | 0 | static int st_abortlock(httrackp *opt, int argc, char **argv) { |
1610 | 0 | return st_lockrule(opt, argc, argv, "abortlock", HTS_ABORT_LOCKNAME); |
1611 | 0 | } |
1612 | | |
1613 | 0 | static int st_stoplock(httrackp *opt, int argc, char **argv) { |
1614 | 0 | return st_lockrule(opt, argc, argv, "stoplock", HTS_PAUSE_LOCKNAME); |
1615 | 0 | } |
1616 | | |
1617 | | // -#test=lockpoll <dir>: the engine looks for hts-abort.lock at most every |
1618 | | // 250 ms. |
1619 | 0 | static int st_lockpoll(httrackp *opt, int argc, char **argv) { |
1620 | 0 | char BIGSTK base[HTS_URLMAXSIZE]; |
1621 | 0 | char BIGSTK lock[HTS_URLMAXSIZE * 2]; |
1622 | 0 | char BIGSTK progress[HTS_URLMAXSIZE * 2]; |
1623 | 0 | char BIGSTK other[HTS_URLMAXSIZE * 2]; |
1624 | 0 | FILE *const saved_log = opt->log; |
1625 | 0 | String saved = STRING_EMPTY; |
1626 | 0 | const time_t now = time(NULL); |
1627 | 0 | int err = 0; |
1628 | |
|
1629 | 0 | if (argc < 1) { |
1630 | 0 | fprintf(stderr, "usage: -#test=lockpoll <writable directory>\n"); |
1631 | 0 | return 1; |
1632 | 0 | } |
1633 | 0 | strcpybuff(base, argv[0]); |
1634 | 0 | if (base[0] != '\0' && hts_lastchar(base) != '/') |
1635 | 0 | strcatbuff(base, "/"); |
1636 | 0 | StringCopy(saved, StringBuff(opt->path_log)); |
1637 | 0 | StringCopy(opt->path_log, base); |
1638 | 0 | opt->log = NULL; /* each take logs, and this output is exact */ |
1639 | 0 | if (!st_lockrule_setup(base, HTS_ABORT_LOCKNAME, HTS_BUF(lock), |
1640 | 0 | HTS_BUF(progress), HTS_BUF(other), now) || |
1641 | 0 | !st_lockrule_stamp(progress, now - 10)) { |
1642 | 0 | fprintf(stderr, "lockpoll: cannot set up %s\n", base); |
1643 | 0 | err = 1; |
1644 | 0 | } |
1645 | |
|
1646 | 0 | opt->abort_lock_polled_at = mtime_monotonic() + 60000; |
1647 | 0 | if (!hts_poll_abort_request(opt)) { |
1648 | 0 | fprintf(stderr, "lockpoll: a clock stepping back stopped the polls\n"); |
1649 | 0 | err = 1; |
1650 | 0 | } |
1651 | | /* the take deleted the lock, so put it back */ |
1652 | 0 | if (!st_lockrule_touch(lock) || !st_lockrule_stamp(lock, now)) { |
1653 | 0 | fprintf(stderr, "lockpoll: cannot stamp %s\n", lock); |
1654 | 0 | err = 1; |
1655 | 0 | } |
1656 | 0 | if (hts_poll_abort_request(opt) || !fexist_utf8(lock)) { |
1657 | 0 | fprintf(stderr, "lockpoll: a poll right after the last one looked\n"); |
1658 | 0 | err = 1; |
1659 | 0 | } |
1660 | 0 | opt->abort_lock_polled_at = mtime_monotonic() - 200; |
1661 | 0 | if (hts_poll_abort_request(opt)) { |
1662 | 0 | fprintf(stderr, "lockpoll: a poll 200 ms after the last one looked\n"); |
1663 | 0 | err = 1; |
1664 | 0 | } |
1665 | 0 | opt->abort_lock_polled_at = mtime_monotonic() - 300; |
1666 | 0 | if (!hts_poll_abort_request(opt)) { |
1667 | 0 | fprintf(stderr, "lockpoll: a poll 300 ms later missed the request\n"); |
1668 | 0 | err = 1; |
1669 | 0 | } |
1670 | |
|
1671 | 0 | (void) UNLINK(progress); |
1672 | 0 | (void) UNLINK(other); |
1673 | 0 | opt->log = saved_log; |
1674 | 0 | opt->abort_lock_polled_at = 0; |
1675 | 0 | StringCopy(opt->path_log, StringBuff(saved)); |
1676 | 0 | StringFree(saved); |
1677 | 0 | printf("lockpoll self-test: %s\n", err ? "FAIL" : "OK"); |
1678 | 0 | return err; |
1679 | 0 | } |
1680 | | |
1681 | | /* A cancel request whose URL cannot be copied is refused, not stored empty. */ |
1682 | 0 | static int st_cancelpushoom(httrackp *opt, int argc, char **argv) { |
1683 | 0 | static const size_t huge = 64 * 1024 * 1024; |
1684 | 0 | char *big; |
1685 | 0 | char *popped; |
1686 | 0 | int ret = 0; |
1687 | 0 | int rc = 1; |
1688 | |
|
1689 | 0 | big = malloct(huge); |
1690 | 0 | assertf(big != NULL); |
1691 | 0 | memset(big, 'x', huge - 1); |
1692 | 0 | big[huge - 1] = '\0'; |
1693 | 0 | #ifndef _WIN32 |
1694 | 0 | { |
1695 | 0 | struct rlimit saved, tight; |
1696 | |
|
1697 | 0 | if (getrlimit(RLIMIT_AS, &saved) != 0) { |
1698 | 0 | printf("cancelpushoom: cannot cap memory, skipped\n"); |
1699 | 0 | rc = 0; |
1700 | 0 | goto done; |
1701 | 0 | } |
1702 | 0 | tight = saved; |
1703 | 0 | tight.rlim_cur = 1024 * 1024; |
1704 | 0 | if (setrlimit(RLIMIT_AS, &tight) != 0) { |
1705 | 0 | printf("cancelpushoom: cannot cap memory, skipped\n"); |
1706 | 0 | rc = 0; |
1707 | 0 | goto done; |
1708 | 0 | } |
1709 | 0 | ret = hts_cancel_file_push(opt, big); |
1710 | 0 | (void) setrlimit(RLIMIT_AS, &saved); |
1711 | 0 | } |
1712 | | #else |
1713 | | printf("cancelpushoom: cannot cap memory, skipped\n"); |
1714 | | rc = 0; |
1715 | | goto done; |
1716 | | #endif |
1717 | 0 | if (opt->state.cancel != NULL && opt->state.cancel->url == NULL) { |
1718 | 0 | printf("cancelpushoom: FAILED (an entry was stored with no URL)\n"); |
1719 | 0 | goto done; |
1720 | 0 | } |
1721 | 0 | if (ret == 0) { |
1722 | 0 | printf("cancelpushoom: cap did not bite, skipped\n"); |
1723 | 0 | rc = 0; |
1724 | 0 | goto done; |
1725 | 0 | } |
1726 | 0 | if (opt->state.cancel != NULL) { |
1727 | 0 | printf("cancelpushoom: FAILED (a refused push left an entry)\n"); |
1728 | 0 | goto done; |
1729 | 0 | } |
1730 | 0 | if (hts_cancel_file_push(opt, "/a.html") != 0) { |
1731 | 0 | printf("cancelpushoom: FAILED (no push after the refusal)\n"); |
1732 | 0 | goto done; |
1733 | 0 | } |
1734 | 0 | popped = hts_cancel_file_pop(opt); |
1735 | 0 | if (popped == NULL || strcmp(popped, "/a.html") != 0 || |
1736 | 0 | opt->state.cancel != NULL) { |
1737 | 0 | printf("cancelpushoom: FAILED (pop did not return the one entry)\n"); |
1738 | 0 | freet(popped); |
1739 | 0 | goto done; |
1740 | 0 | } |
1741 | 0 | freet(popped); |
1742 | 0 | printf("cancelpushoom: oom OK\n"); |
1743 | 0 | rc = 0; |
1744 | |
|
1745 | 0 | done: |
1746 | 0 | while ((popped = hts_cancel_file_pop(opt)) != NULL) { |
1747 | 0 | freet(popped); |
1748 | 0 | } |
1749 | 0 | freet(big); |
1750 | 0 | return rc; |
1751 | 0 | } |
1752 | | |
1753 | | /* Two capacities and every offset catch a postprocess-html bound that |
1754 | | ignores "size" or checks only the base pointer. */ |
1755 | 0 | static int st_postprocsize(httrackp *opt, int argc, char **argv) { |
1756 | 0 | char buf[16] = {0}, own[16] = {0}; |
1757 | 0 | const size_t capa = sizeof(buf); |
1758 | 0 | size_t size; |
1759 | | |
1760 | | /* the engine's own storage, whatever the reply points at inside it */ |
1761 | 0 | assertf(hts_postprocess_reply_inplace(buf, buf, capa)); |
1762 | 0 | assertf(hts_postprocess_reply_inplace(buf + capa - 1, buf, capa)); |
1763 | 0 | assertf(!hts_postprocess_reply_inplace(buf + capa, buf, capa)); |
1764 | 0 | assertf(!hts_postprocess_reply_inplace(own, buf, capa)); |
1765 | 0 | assertf(!hts_postprocess_reply_inplace(buf, buf, 0)); |
1766 | 0 | for (size = 4; size <= capa; size *= 2) { |
1767 | 0 | size_t at; |
1768 | |
|
1769 | 0 | for (at = 0; at <= size; at++) { |
1770 | | /* in place, a reply owns what is left of "size" past its own offset */ |
1771 | 0 | assertf(hts_postprocess_reply_ok(buf + at, (int) (size - at), buf, size, |
1772 | 0 | capa)); |
1773 | | /* one past the storage is another object's business, not ours to bound */ |
1774 | 0 | if (at < capa) |
1775 | 0 | assertf(!hts_postprocess_reply_ok(buf + at, (int) (size - at) + 1, buf, |
1776 | 0 | size, capa)); |
1777 | 0 | } |
1778 | | /* in the buffer's slack, past every byte the engine handed out */ |
1779 | 0 | if (size < capa) |
1780 | 0 | assertf(!hts_postprocess_reply_ok(buf + size + 1, 0, buf, size, capa)); |
1781 | | /* a buffer of the callback's own is bounded by the callback */ |
1782 | 0 | assertf(hts_postprocess_reply_ok(own, (int) capa + 1, buf, size, capa)); |
1783 | | /* neither may report a negative count */ |
1784 | 0 | assertf(!hts_postprocess_reply_ok(own, -1, buf, size, capa)); |
1785 | 0 | assertf(!hts_postprocess_reply_ok(buf, -1, buf, size, capa)); |
1786 | 0 | } |
1787 | 0 | printf("postprocess reply self-test OK\n"); |
1788 | 0 | return 0; |
1789 | 0 | } |
1790 | | |
1791 | | /* Print the verdict for a -V template, then one "ARG <index> <value>" line |
1792 | | per vector entry. A replaying caller must pass the save name as an entry. */ |
1793 | 0 | static int st_usercmd(httrackp *opt, int argc, char **argv) { |
1794 | 0 | char BIGSTK dest[8192]; |
1795 | | /* An optional third argument caps the room, because a -V template stops |
1796 | | at HTS_CDLMAXSIZE. */ |
1797 | 0 | size_t room = sizeof(dest); |
1798 | 0 | const char *why = "unnamed"; |
1799 | |
|
1800 | 0 | if (argc > 2) { |
1801 | 0 | const int asked = atoi(argv[2]); |
1802 | |
|
1803 | 0 | if (asked <= 0 || (size_t) asked > sizeof(dest)) { |
1804 | 0 | fprintf(stderr, "usercmd: room must be within 1..%d\n", |
1805 | 0 | (int) sizeof(dest)); |
1806 | 0 | return 1; |
1807 | 0 | } |
1808 | 0 | room = (size_t) asked; |
1809 | 0 | } |
1810 | 0 | switch (usercommand_expand(hts_buf_at(dest, room), argv[0], argv[1], &why)) { |
1811 | 0 | case USERCOMMAND_EXPAND_OK: |
1812 | 0 | break; |
1813 | 0 | case USERCOMMAND_EXPAND_REFUSED: |
1814 | 0 | printf("REFUSED %s\n", why); |
1815 | 0 | return 0; |
1816 | 0 | default: |
1817 | 0 | printf("TOOLONG\n"); |
1818 | 0 | return 0; |
1819 | 0 | } |
1820 | 0 | printf("OK\n"); |
1821 | 0 | #ifndef _WIN32 |
1822 | 0 | { |
1823 | 0 | char *vector[USERCOMMAND_ARGV_MAX]; |
1824 | 0 | const size_t n = usercommand_argv(vector, dest, argv[1]); |
1825 | 0 | size_t i; |
1826 | |
|
1827 | 0 | for (i = 0; i < n; i++) |
1828 | 0 | printf("ARG %d %s\n", (int) i, vector[i]); |
1829 | 0 | } |
1830 | | #else |
1831 | | /* cmd.exe takes no positional parameters, so the name is inside the text */ |
1832 | | printf("ARG 0 cmd\nARG 1 /c\nARG 2 %s\n", dest); |
1833 | | #endif |
1834 | 0 | return 0; |
1835 | 0 | } |
1836 | | |
1837 | | /* Run a -V template for real, through the engine's own spawn path, and report |
1838 | | what the shell exited with. The child's output goes to our stdout. */ |
1839 | 0 | static int st_usercmdrun(httrackp *opt, int argc, char **argv) { |
1840 | 0 | fflush(stdout); |
1841 | 0 | printf("EXIT %d\n", usercommand_exe(opt, argv[0], argv[1])); |
1842 | 0 | return 0; |
1843 | 0 | } |
1844 | | |
1845 | | /* No digit run produces this, so an untouched field shows. */ |
1846 | 0 | #define ST_GLUED_UNSET (-7) |
1847 | | |
1848 | | /* Read text as the glued digits of an option and print "got=G value=V adv=N". |
1849 | | N is the number of bytes the option pointer moved. */ |
1850 | | static void st_glued_read(hts_boolean wide, LLint min, LLint max, |
1851 | 0 | const char *text, hts_buf out) { |
1852 | 0 | char buf[256]; |
1853 | 0 | char *com = buf; |
1854 | 0 | LLint value = ST_GLUED_UNSET; |
1855 | 0 | int narrow = ST_GLUED_UNSET; |
1856 | 0 | hts_scan_result got; |
1857 | |
|
1858 | 0 | buf[0] = 'X'; |
1859 | 0 | buf[1] = '\0'; |
1860 | 0 | strlncatbuff(buf, text, sizeof(buf), sizeof(buf) - 1); |
1861 | 0 | if (wide) { |
1862 | 0 | got = cmdl_glued_llint(&com, min, max, &value); |
1863 | 0 | } else { |
1864 | 0 | got = cmdl_glued_int(&com, (int) min, (int) max, &narrow); |
1865 | 0 | value = narrow; |
1866 | 0 | } |
1867 | 0 | hts_buf_printf_clip(out, "got=%d value=" LLintP " adv=%d", (int) got, value, |
1868 | 0 | (int) (com - buf)); |
1869 | 0 | } |
1870 | | |
1871 | | /* Parses the text with the legacy sscanf() and digit loop. */ |
1872 | | static void st_glued_legacy(hts_boolean wide, const char *text, LLint *value, |
1873 | 0 | int *adv) { |
1874 | 0 | if (wide) { |
1875 | 0 | *value = ST_GLUED_UNSET; |
1876 | 0 | (void) sscanf(text, LLintP, value); |
1877 | 0 | } else { |
1878 | 0 | int narrow = ST_GLUED_UNSET; |
1879 | |
|
1880 | 0 | (void) sscanf(text, "%d", &narrow); |
1881 | 0 | *value = narrow; |
1882 | 0 | } |
1883 | 0 | for (*adv = 0; hts_isdigit((unsigned char) text[*adv]); (*adv)++) |
1884 | 0 | ; |
1885 | 0 | } |
1886 | | |
1887 | | /* <int|llint> <min> <max> <text>: one glued read. With no argument, every |
1888 | | converted option against its legacy parse over a generated corpus. */ |
1889 | 0 | static int st_gluedint(httrackp *opt, int argc, char **argv) { |
1890 | | /* Each glued option htscoremain.c reads, with the bounds it passes. */ |
1891 | 0 | static const struct { |
1892 | 0 | const char *name; |
1893 | 0 | hts_boolean wide; |
1894 | 0 | LLint max; |
1895 | 0 | } sites[] = { |
1896 | 0 | {"-r", HTS_FALSE, INT_MAX - 1}, {"-c", HTS_FALSE, INT_MAX}, |
1897 | 0 | {"-p", HTS_FALSE, INT_MAX}, {"-G", HTS_TRUE, INT64_MAX}, |
1898 | 0 | {"-M", HTS_TRUE, INT64_MAX}, {"-m", HTS_TRUE, INT64_MAX}, |
1899 | 0 | {"-m,", HTS_TRUE, INT64_MAX}, {"-T", HTS_FALSE, INT_MAX}, |
1900 | 0 | {"-J", HTS_FALSE, INT_MAX}, {"-R", HTS_FALSE, INT_MAX}, |
1901 | 0 | {"-E", HTS_FALSE, INT_MAX}, {"-H", HTS_FALSE, INT_MAX}, |
1902 | 0 | {"-A", HTS_FALSE, INT_MAX}, {"-j", HTS_FALSE, INT_MAX}, |
1903 | 0 | {"-%e", HTS_FALSE, INT_MAX}, {"-@i", HTS_FALSE, INT_MAX}, |
1904 | 0 | {"-@m", HTS_FALSE, INT_MAX}, {"-#C", HTS_FALSE, INT_MAX}, |
1905 | 0 | {"-#L", HTS_FALSE, INT_MAX}, {"-#F", HTS_FALSE, HTS_FILTERS_MAX}, |
1906 | 0 | {"-#u", HTS_FALSE, INT_MAX}}; |
1907 | |
|
1908 | 0 | static const char *const spaces[] = {"", " ", "\t"}; |
1909 | 0 | static const char *const signs[] = {"", "+", "-"}; |
1910 | 0 | static const char *const digits[] = {"", |
1911 | 0 | "0", |
1912 | 0 | "00", |
1913 | 0 | "7", |
1914 | 0 | "42", |
1915 | 0 | "0042", |
1916 | 0 | "65535", |
1917 | 0 | "2147483647", |
1918 | 0 | "2147483648", |
1919 | 0 | "4294967295", |
1920 | 0 | "4294967297", |
1921 | 0 | "9223372036854775807", |
1922 | 0 | "9223372036854775808", |
1923 | 0 | "99999999999999999999"}; |
1924 | 0 | static const char *const suffixes[] = {"", "x", ".5", ",3", "-1"}; |
1925 | 0 | size_t s, w, g, d, x; |
1926 | 0 | int compared = 0, unsigned_only = 0, too_large = 0, failures = 0; |
1927 | 0 | char in[128], cur[128], want[128]; |
1928 | |
|
1929 | 0 | if (argc == 4) { |
1930 | 0 | st_glued_read(strcmp(argv[0], "llint") == 0, strtoll(argv[1], NULL, 10), |
1931 | 0 | strtoll(argv[2], NULL, 10), argv[3], HTS_BUF(cur)); |
1932 | 0 | printf("%s\n", cur); |
1933 | 0 | return 0; |
1934 | 0 | } |
1935 | 0 | for (s = 0; s < sizeof(sites) / sizeof(sites[0]); s++) |
1936 | 0 | for (w = 0; w < sizeof(spaces) / sizeof(spaces[0]); w++) |
1937 | 0 | for (g = 0; g < sizeof(signs) / sizeof(signs[0]); g++) |
1938 | 0 | for (d = 0; d < sizeof(digits) / sizeof(digits[0]); d++) |
1939 | 0 | for (x = 0; x < sizeof(suffixes) / sizeof(suffixes[0]); x++) { |
1940 | 0 | LLint old, fits; |
1941 | 0 | int adv; |
1942 | 0 | hts_scan_result expect = HTS_SCAN_OK; |
1943 | |
|
1944 | 0 | snprintf(in, sizeof(in), "%s%s%s%s", spaces[w], signs[g], digits[d], |
1945 | 0 | suffixes[x]); |
1946 | 0 | st_glued_legacy(sites[s].wide, in, &old, &adv); |
1947 | | /* Only leading digits are read, unlike the legacy sscanf(). */ |
1948 | 0 | if (adv == 0) { |
1949 | 0 | expect = HTS_SCAN_NONE; |
1950 | 0 | } else if (!hts_parse_llint(in, NULL, 0, sites[s].max, &fits)) { |
1951 | 0 | expect = HTS_SCAN_REFUSED; |
1952 | 0 | } |
1953 | 0 | if (expect != HTS_SCAN_OK && old != ST_GLUED_UNSET) { |
1954 | 0 | old = ST_GLUED_UNSET; |
1955 | 0 | if (expect == HTS_SCAN_NONE) |
1956 | 0 | unsigned_only++; |
1957 | 0 | else |
1958 | 0 | too_large++; |
1959 | 0 | } |
1960 | 0 | snprintf(want, sizeof(want), "got=%d value=" LLintP " adv=%d", |
1961 | 0 | (int) expect, old, adv); |
1962 | 0 | st_glued_read(sites[s].wide, 0, sites[s].max, in, HTS_BUF(cur)); |
1963 | 0 | compared++; |
1964 | 0 | if (strcmp(cur, want) != 0) { |
1965 | 0 | printf("FAIL %s \"%s\": want \"%s\" now \"%s\"\n", sites[s].name, |
1966 | 0 | in, want, cur); |
1967 | 0 | failures++; |
1968 | 0 | } |
1969 | 0 | } |
1970 | | printf("glued-int: %d compared, %d signed or spaced now unread, %d too " |
1971 | 0 | "large now refused, %d failures\n", |
1972 | 0 | compared, unsigned_only, too_large, failures); |
1973 | 0 | return failures != 0; |
1974 | 0 | } |
1975 | | |
1976 | | /* ------------------------------------------------------------ */ |
1977 | | /* Registry: this module's tests, in the order -#test lists them. */ |
1978 | | /* ------------------------------------------------------------ */ |
1979 | | |
1980 | | const struct selftest_entry selftests_opt[] = { |
1981 | | {"glued-int", "[<int|llint> <min> <max> <text>]", |
1982 | | "glued numeric options (-c8) read as before, out-of-range refused", |
1983 | | st_gluedint}, |
1984 | | {"usercmd", "<-V template> <filename> [room]", |
1985 | | "rewrite a -V template's $0 and print the shell vector it would run", |
1986 | | st_usercmd}, |
1987 | | {"usercmdrun", "<-V template> <filename>", |
1988 | | "run a -V template through the engine's own spawn path", st_usercmdrun}, |
1989 | | {"log-counters", "", |
1990 | | "error and warning counts survive a run with no log file (#1681)", |
1991 | | st_logcounters}, |
1992 | | {"scantoken", "", |
1993 | | "option-string token copy is bounded and reports a refusal (#1271)", |
1994 | | st_scantoken}, |
1995 | | {"abortlock", "<writable directory>", |
1996 | | "an abort request stops the mirror only if it is fresh and deletable", |
1997 | | st_abortlock}, |
1998 | | {"stoplock", "<writable directory>", |
1999 | | "a pause request pauses the mirror only if it is fresh and deletable", |
2000 | | st_stoplock}, |
2001 | | {"lockpoll", "<writable directory>", |
2002 | | "the engine looks for an abort request at most every 250 ms", st_lockpoll}, |
2003 | | {"cancelpushoom", "", |
2004 | | "a cancel request that cannot be stored is refused, not stored empty", |
2005 | | st_cancelpushoom}, |
2006 | | {"postprocsize", "", |
2007 | | "a postprocess-html callback cannot claim bytes it was not handed", |
2008 | | st_postprocsize}, |
2009 | | {"postfile", "<short> <long> <embedded-nul> <empty>", |
2010 | | "a >postfile: body is clipped to the request block and terminated", |
2011 | | st_postfile}, |
2012 | | {"optalias", "[-list | <option> [<value>]]", |
2013 | | "long-option alias expansion (--index=0 and friends)", st_optalias}, |
2014 | | {"features", "", "which optional features this build has", st_features}, |
2015 | | {"footerfmt", "<template>", "-%F footer positional/named expansion", |
2016 | | st_footerfmt}, |
2017 | | {"cmdline-split", "", |
2018 | | "webhttrack command-line to argv split (bounds, quoting)", |
2019 | | st_cmdlinesplit}, |
2020 | | {"copyopt", "", "copy_htsopt option-copy self-test", st_copyopt}, |
2021 | | {"upperlinksnote", "", |
2022 | | "the above-the-start-directory note empties its buffer when silent", |
2023 | | st_upperlinksnote}, |
2024 | | {"cachedefault", "", "-C default is C1 cache-priority, not C2", |
2025 | | st_cachedefault}, |
2026 | | {"logcallback", "", "log callback must not consume the log file's va_list", |
2027 | | st_logcallback}, |
2028 | | #ifdef HTS_CRASH_TEST |
2029 | | {"crashannounce", "[logfile]", |
2030 | | "a crash-test marker reaches the log callback too", st_crashannounce}, |
2031 | | #endif |
2032 | | {NULL, NULL, NULL, NULL}, |
2033 | | }; |