Coverage Report

Created: 2026-12-01 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/httrack/src/htsbundle_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: htsbundle_selftest.c subroutines:                      */
30
/*       self-tests for the login bundles                       */
31
/* ------------------------------------------------------------ */
32
33
#include "htsselftest_int.h"
34
35
#include "htsbundle.h"
36
#include "htsthread.h"
37
38
/* -#test=bundlecut <url>: the bundle a start URL names, and the URL left. */
39
0
static int st_bundlecut(httrackp *opt, int argc, char **argv) {
40
0
  static const char nodir[] = "/nonexistent-bundlecut/";
41
0
  char url[HTS_URLMAXSIZE * 2];
42
0
  char err[1024];
43
0
  const char *why;
44
45
0
  if (argc != 1 || !hts_buf_try_cpy(HTS_BUF(url), argv[0])) {
46
0
    fprintf(stderr, "bundlecut: takes <url>\n");
47
0
    return 1;
48
0
  }
49
0
  hts_bundle_free(opt);
50
0
  why = hts_bundle_take(opt, url);
51
0
  if (why != NULL) {
52
0
    printf("bad: %s\n", why);
53
0
  } else if (opt->bundle_state == NULL) {
54
0
    printf("none\n");
55
0
  } else {
56
    /* the name shows in the file a load from a missing folder cannot open */
57
0
    StringCopy(opt->bundle_dir, nodir);
58
0
    assertf(!hts_bundle_load_all(opt, NULL, HTS_BUF(err)) &&
59
0
            strncmp(err, nodir, sizeof(nodir) - 1) == 0 &&
60
0
            strstr(err, ".hts: ") != NULL);
61
0
    printf("%.*s %s\n",
62
0
           (int) (strstr(err, ".hts: ") - err) - (int) (sizeof(nodir) - 1),
63
0
           err + sizeof(nodir) - 1, url);
64
0
    StringCopy(opt->bundle_dir, "");
65
0
    hts_bundle_free(opt);
66
0
  }
67
0
  return 0;
68
0
}
69
70
/* Print err with each "<dir>/" cut to "/", since the folder is the caller's. */
71
0
static void print_without_dir(const char *err, const char *dir) {
72
0
  const size_t dlen = strlen(dir);
73
0
  const char *p = err;
74
0
  const char *at;
75
76
0
  while ((at = strstr(p, dir)) != NULL) {
77
0
    const hts_boolean whole = at[dlen] == '/' || at[dlen] == '\\';
78
79
0
    printf("%.*s", (int) (at - p) + (whole ? 0 : (int) dlen), p);
80
0
    p = at + dlen;
81
0
  }
82
0
  printf("%s\n", p);
83
0
}
84
85
/* -#test=bundle <dir> <url>... [host:<adr>]... [hide:<text>]... [jar:<file>]
86
   [twice] [b0]: load the bundles the URLs name from dir over the jar file,
87
   twice when asked, with cookies off for b0, then print their url lines, the
88
   jar, the UA and headers each adr gets, and each text as the logs show it. */
89
0
static int st_bundle(httrackp *opt, int argc, char **argv) {
90
0
  FILE *const log = opt->log;
91
0
  t_cookie *const prev = opt->cookie;
92
0
  t_cookie *jar = calloct(1, sizeof(t_cookie));
93
0
  const char *jar_file = NULL;
94
0
  int loads = 1;
95
0
  char err[1024];
96
0
  const char *url;
97
0
  char *rec;
98
0
  int i;
99
100
0
  if (argc < 2 || jar == NULL) {
101
0
    fprintf(stderr, "bundle: takes <dir> <url>... [host:<adr>]... "
102
0
                    "[hide:<text>]... [jar:<file>] [twice] [b0]\n");
103
0
    freet(jar);
104
0
    return 1;
105
0
  }
106
0
  jar->max_len = COOKIE_MAX_JAR; /* httpmirror's */
107
0
  opt->log = stderr;    /* warnings stay out of the graded output */
108
0
  opt->cookie = jar;
109
0
  hts_bundle_free(opt);
110
0
  StringCopy(opt->bundle_dir, argv[0]);
111
0
  for (i = 1; i < argc; i++) {
112
0
    char buff[HTS_URLMAXSIZE * 2];
113
114
0
    if (strncmp(argv[i], "jar:", 4) == 0) {
115
0
      jar_file = argv[i] + 4;
116
0
      (void) cookie_load(opt, jar, "", jar_file);
117
0
    } else if (strcmp(argv[i], "twice") == 0) {
118
0
      loads = 2;
119
0
    } else if (strcmp(argv[i], "b0") == 0) {
120
0
      opt->cookie = NULL;
121
0
    } else if (strncmp(argv[i], "host:", 5) != 0 &&
122
0
               strncmp(argv[i], "hide:", 5) != 0 &&
123
0
               hts_buf_try_cpy(HTS_BUF(buff), argv[i])) {
124
0
      assertf(hts_bundle_take(opt, buff) == NULL);
125
0
    }
126
0
  }
127
0
  for (i = 0; i < loads; i++) {
128
0
    if (!hts_bundle_load_all(opt, jar_file, HTS_BUF(err))) {
129
0
      printf("refused: ");
130
0
      print_without_dir(err, argv[0]);
131
0
      goto done;
132
0
    }
133
0
  }
134
0
  for (i = 0; (url = hts_bundle_url(opt, i)) != NULL; i++)
135
0
    printf("url %s\n", url);
136
0
  for (rec = jar->data; *rec != '\0'; rec = cookie_nextfield(rec)) {
137
0
    char dom[8192], cname[8192], value[8192];
138
139
0
    printf("cookie %s %s=%s\n", cookie_get(dom, sizeof(dom), rec, 0),
140
0
           cookie_get(cname, sizeof(cname), rec, 5),
141
0
           cookie_get(value, sizeof(value), rec, 6));
142
0
  }
143
0
  for (i = 1; i < argc; i++) {
144
0
    if (strncmp(argv[i], "host:", 5) == 0) {
145
0
      htsrequest req;
146
0
      const char *p;
147
148
0
      memset(&req, 0, sizeof(req));
149
0
      req.user_agent = req.headers = "global";
150
0
      hts_bundle_identity(opt, argv[i] + 5, &req);
151
0
      printf("%s ua=%s headers=", argv[i] + 5, req.user_agent);
152
      /* one line per host, so the block's line ends print as '|' */
153
0
      for (p = req.headers; *p != '\0'; p++) {
154
0
        if (*p != '\r')
155
0
          putchar(*p == '\n' ? '|' : *p);
156
0
      }
157
0
      putchar('\n');
158
0
    } else if (strncmp(argv[i], "hide:", 5) == 0) {
159
0
      char shown[HTS_URLMAXSIZE * 2];
160
161
0
      (void) strclipbuff(shown, sizeof(shown), argv[i] + 5);
162
0
      hts_hide_secrets(opt, shown, sizeof(shown));
163
0
      printf("hide %s\n", shown);
164
0
    }
165
0
  }
166
0
done:
167
0
  hts_bundle_free(opt);
168
0
  StringCopy(opt->bundle_dir, "");
169
0
  opt->cookie = prev;
170
0
  opt->log = log;
171
0
  freet(jar);
172
0
  return 0;
173
0
}
174
175
/* -#test=regdomain <host>...: each host's registrable domain, or "none". */
176
0
static int st_regdomain(httrackp *opt, int argc, char **argv) {
177
0
  int i;
178
179
0
  for (i = 0; i < argc; i++) {
180
0
    char reg[256];
181
182
0
    printf("%s %s\n", argv[i],
183
0
           hts_registrable_domain(argv[i], reg, sizeof(reg)) ? reg : "none");
184
0
  }
185
0
  return 0;
186
0
}
187
188
/* -#test=bundlescope <cookie domain> <url>...: "keep" when a bundle naming the
189
   URLs keeps a cookie set for the domain, else "drop". */
190
0
static int st_bundlescope(httrackp *opt, int argc, char **argv) {
191
0
  if (argc < 1) {
192
0
    fprintf(stderr, "bundlescope: takes <cookie domain> <url>...\n");
193
0
    return 1;
194
0
  }
195
0
  printf("%s\n", hts_bundle_cookie_in_scope((const char *const *) &argv[1],
196
0
                                            (size_t) (argc - 1), argv[0])
197
0
                     ? "keep"
198
0
                     : "drop");
199
0
  return 0;
200
0
}
201
202
/* -#test=bundlenames <urls> [<list file>]: the count and the bundle names the
203
   addresses use, or "refused: <why>". */
204
0
static int st_bundlenames(httrackp *opt, int argc, char **argv) {
205
0
  char *stale[1] = {NULL};
206
0
  char **names;
207
0
  char error[64];
208
0
  int n;
209
0
  int i;
210
211
0
  if (argc < 1 || argc > 2) {
212
0
    fprintf(stderr, "bundlenames: takes <urls> [<list file>]\n");
213
0
    return 1;
214
0
  }
215
  /* so a failure must reset them */
216
0
  names = stale;
217
0
  strcpybuff(error, "stale");
218
0
  n = hts_bundle_names_in_use_checked(argv[0], argc == 2 ? argv[1] : NULL,
219
0
                                      &names, error, sizeof(error));
220
0
  if (n < 0) {
221
0
    assertf(names == NULL);
222
0
    printf("refused: %s\n", error);
223
0
    return 0;
224
0
  }
225
0
  assertf(names[n] == NULL);
226
0
  printf("%d:", n);
227
0
  for (i = 0; i < n; i++)
228
0
    printf(" %s", names[i]);
229
0
  printf("\n");
230
0
  hts_bundle_names_free(names);
231
0
  return 0;
232
0
}
233
234
/* Calls the deprecated form on purpose. */
235
#if defined(__GNUC__) || defined(__clang__)
236
#pragma GCC diagnostic push
237
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
238
#elif defined(_MSC_VER)
239
#pragma warning(push)
240
#pragma warning(disable : 4996)
241
#endif
242
/* Each form's result for paste under flags, "checked/legacy", then the last
243
   name or error. */
244
0
static void bundlechecked_paste(const char *paste, unsigned int flags) {
245
0
  const hts_boolean dry = (flags & HTS_PASTE_DRY_RUN) != 0;
246
0
  hts_paste_result res, prev;
247
0
  hts_boolean ok = HTS_FALSE;
248
0
  int i;
249
250
0
  for (i = 0; i < 6; i++) {
251
252
0
    res.size = sizeof(res);
253
0
    switch (i) {
254
0
    case 0:
255
0
      ok = hts_bundle_from_paste_checked(paste, NULL, NULL, NULL, dry, &res);
256
0
      break;
257
0
    case 1:
258
0
      ok = hts_bundle_from_paste(paste, NULL, NULL, NULL, dry, &res);
259
0
      break;
260
0
    case 2:
261
0
      ok = hts_bundle_from_paste_flags_checked(paste, NULL, NULL, NULL, flags,
262
0
                                               &res);
263
0
      break;
264
0
    case 3:
265
0
      ok = hts_bundle_from_paste_flags(paste, NULL, NULL, NULL, flags, &res);
266
0
      break;
267
0
    case 4:
268
0
      ok = hts_bundle_from_paste_hosts_checked(paste, NULL, NULL, NULL, NULL,
269
0
                                               flags, &res);
270
0
      break;
271
0
    default:
272
0
      ok = hts_bundle_from_paste_hosts(paste, NULL, NULL, NULL, NULL, flags,
273
0
                                       &res);
274
0
      break;
275
0
    }
276
0
    printf("%s%d", i % 2 == 0 ? " " : "/", ok);
277
0
    if (i % 2 == 0)
278
0
      prev = res;
279
0
    else if (strcmp(res.name, prev.name) != 0 ||
280
0
             strcmp(res.url, prev.url) != 0 ||
281
0
             strcmp(res.error, prev.error) != 0 || res.cookies != prev.cookies)
282
0
      printf("(result differs)");
283
0
  }
284
0
  printf(" %s", ok ? res.name : res.error);
285
0
}
286
287
/* -#test=bundlechecked: each _checked form and its deprecated twin agree, on a
288
   refusal and on an accepted dry run. */
289
0
static int st_bundlechecked(httrackp *opt, int argc, char **argv) {
290
0
  static const char paste[] = "curl 'https://www.shop.example/' -b 'SID=1'";
291
0
  char **names = NULL;
292
0
  int n;
293
294
0
  printf("refused:");
295
0
  bundlechecked_paste("", HTS_PASTE_DRY_RUN);
296
0
  printf("\naccepted:");
297
0
  bundlechecked_paste(paste, HTS_PASTE_DRY_RUN);
298
0
  printf("\nnames:");
299
0
  n = hts_bundle_names_in_use_checked("-V", NULL, &names, NULL, 0);
300
0
  printf(" %d/%d", n, hts_bundle_names_in_use("-V", NULL, &names, NULL, 0));
301
0
  n = hts_bundle_names_in_use_checked("http://a/#httrack-bundle=shop", NULL,
302
0
                                      &names, NULL, 0);
303
0
  hts_bundle_names_free(names);
304
0
  printf(" %d/%d\n", n,
305
0
         hts_bundle_names_in_use("http://a/#httrack-bundle=shop", NULL, &names,
306
0
                                 NULL, 0));
307
0
  hts_bundle_names_free(names);
308
0
  return 0;
309
0
}
310
#if defined(__GNUC__) || defined(__clang__)
311
#pragma GCC diagnostic pop
312
#elif defined(_MSC_VER)
313
#pragma warning(pop)
314
#endif
315
316
/* -#test=bundlename <name>: "ok", or why the name is refused. */
317
0
static int st_bundlename(httrackp *opt, int argc, char **argv) {
318
0
  const char *why;
319
320
0
  if (argc != 1) {
321
0
    fprintf(stderr, "bundlename: takes <name>\n");
322
0
    return 1;
323
0
  }
324
0
  why = hts_bundle_name_refusal(argv[0]);
325
0
  printf("%s\n", why != NULL ? why : "ok");
326
0
  return 0;
327
0
}
328
329
/* -#test=paste <dir> <file> [agent:<ua>] [url:<url>] [hosts:<list>] [dry]
330
   [keep]: turn the paste held in file into a bundle in dir, and print what
331
   came back. keep passes HTS_PASTE_KEEP_EMPTY. */
332
0
static int st_paste(httrackp *opt, int argc, char **argv) {
333
0
  const char *agent = NULL, *url = NULL, *hosts = NULL;
334
0
  hts_boolean dry = HTS_FALSE, keep = HTS_FALSE;
335
0
  unsigned int flags;
336
0
  hts_paste_result *res = calloct(1, sizeof(*res));
337
0
  String paste = STRING_EMPTY;
338
0
  char *file = NULL;
339
0
  LLint filesize = 0;
340
0
  int i;
341
342
0
  if (res != NULL)
343
0
    res->size = sizeof(*res);
344
0
  if (argc >= 2)
345
0
    file = readfile2_utf8(argv[1], &filesize);
346
0
  if (file == NULL || res == NULL) {
347
0
    fprintf(stderr, "paste: takes <dir> <file> [agent:<ua>] [url:<url>] "
348
0
                    "[hosts:<list>] "
349
0
                    "[dry] [keep]\n");
350
0
    freet(file);
351
0
    freet(res);
352
0
    return 1;
353
0
  }
354
0
  StringMemcat(paste, file, (size_t) filesize);
355
0
  freet(file);
356
0
  for (i = 2; i < argc; i++) {
357
0
    if (strncmp(argv[i], "agent:", 6) == 0)
358
0
      agent = argv[i] + 6;
359
0
    else if (strncmp(argv[i], "url:", 4) == 0)
360
0
      url = argv[i] + 4;
361
0
    else if (strncmp(argv[i], "hosts:", 6) == 0)
362
0
      hosts = argv[i] + 6;
363
0
    else if (strcmp(argv[i], "dry") == 0)
364
0
      dry = HTS_TRUE;
365
0
    else if (strcmp(argv[i], "keep") == 0)
366
0
      keep = HTS_TRUE;
367
0
  }
368
0
  if (!StringNotEmpty(paste))
369
0
    StringCat(paste, "");
370
0
  flags = (keep ? HTS_PASTE_KEEP_EMPTY : 0) | (dry ? HTS_PASTE_DRY_RUN : 0);
371
0
  if (strlen(StringBuff(paste)) != StringLength(paste)) {
372
0
    printf("refused: the file holds a NUL\n");
373
0
  } else if (!(hosts != NULL ? hts_bundle_from_paste_hosts_checked(
374
0
                                   StringBuff(paste), argv[0], agent, url,
375
0
                                   hosts, flags, res)
376
0
               : keep
377
0
                   ? hts_bundle_from_paste_flags_checked(
378
0
                         StringBuff(paste), argv[0], agent, url, flags, res)
379
0
                   : hts_bundle_from_paste_checked(StringBuff(paste), argv[0],
380
0
                                                   agent, url, dry, res))) {
381
0
    assertf(res->name[0] == '\0' && res->url[0] == '\0' &&
382
0
            res->agent[0] == '\0' && res->hosts[0] == '\0' &&
383
0
            res->cookies == 0 && res->error[0] != '\0');
384
0
    printf("refused: %s\n", res->error);
385
0
  } else {
386
0
    assertf(res->error[0] == '\0');
387
0
    printf("name=%s cookies=%d hosts=%s\nurl=%s\nagent=%s\n", res->name,
388
0
           res->cookies, res->hosts, res->url, res->agent);
389
0
    if (hosts != NULL)
390
0
      printf("dropped=%d\n", res->dropped);
391
0
  }
392
0
  StringFree(paste);
393
0
  freet(res);
394
0
  return 0;
395
0
}
396
397
typedef struct paste_race {
398
  const char *dir;
399
  int rounds;
400
  volatile int failed;
401
  char why[HTS_PASTE_ERROR_SIZE]; /* the first failure */
402
} paste_race;
403
404
0
static void paste_race_worker(void *arg) {
405
0
  paste_race *const race = (paste_race *) arg;
406
0
  hts_paste_result res;
407
0
  int i;
408
409
0
  for (i = 0; i < race->rounds; i++) {
410
0
    res.size = sizeof(res);
411
0
    if (!hts_bundle_from_paste_checked("curl 'https://race.test/' -b a=1",
412
0
                                       race->dir, NULL, NULL, HTS_FALSE,
413
0
                                       &res) &&
414
0
        hts_increment_int(&race->failed) == 1)
415
0
      strcpybuff(race->why, res.error);
416
0
  }
417
0
}
418
419
/* Two threads of one process write the same bundle at once. */
420
0
static int st_pasterace(httrackp *opt, int argc, char **argv) {
421
0
  paste_race race;
422
423
0
  race.dir = argv[0];
424
0
  race.rounds = atoi(argv[1]);
425
0
  race.failed = 0;
426
0
  race.why[0] = '\0';
427
0
  if (hts_newthread_checked(paste_race_worker, &race) != 0 ||
428
0
      hts_newthread_checked(paste_race_worker, &race) != 0) {
429
0
    fprintf(stderr, "pasterace: cannot start a thread\n");
430
0
    return 1;
431
0
  }
432
0
  htsthread_wait_n(0);
433
0
  printf("failed=%d\n", hts_load_acquire_int(&race.failed));
434
0
  if (race.why[0] != '\0')
435
    printf("first: %s\n", race.why);
436
0
  return 0;
437
0
}
438
439
const struct selftest_entry selftests_bundle[] = {
440
    {"bundlecut", "<url>", "the login bundle a start URL names", st_bundlecut},
441
    {"bundle",
442
     "<dir> <url>... [host:<adr>]... [hide:<text>]... [jar:<file>] [twice] "
443
     "[b0]",
444
     "load login bundles and print the identity each host gets", st_bundle},
445
    {"regdomain", "[host]...", "the registrable domain of each host",
446
     st_regdomain},
447
    {"bundlename", "<name>", "is name a login bundle name?", st_bundlename},
448
    {"bundlescope", "<cookie domain> [url...]",
449
     "does a login bundle naming the URLs keep a cookie of that domain?",
450
     st_bundlescope},
451
    {"bundlenames", "<urls> [<list file>]",
452
     "the login bundle names the start addresses use", st_bundlenames},
453
    {"bundlechecked", "",
454
     "each _checked bundle form and its deprecated twin agree",
455
     st_bundlechecked},
456
    {"paste",
457
     "<dir> <file> [agent:<ua>] [url:<url>] [hosts:<list>] [dry] [keep]",
458
     "turn a pasted cURL command or cookies.txt into a login bundle", st_paste},
459
    {"pasterace", "<dir> <rounds>",
460
     "two threads write the same login bundle at once", st_pasterace},
461
    {NULL, NULL, NULL, NULL},
462
};