Coverage Report

Created: 2026-12-01 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/httrack/src/htstools_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: htstools_selftest.c subroutines:                       */
30
/*       self-tests for the attribute scanner, winprofile.ini   */
31
/* Author: Xavier Roche                                         */
32
/* ------------------------------------------------------------ */
33
34
#include "htsselftest_int.h"
35
36
/* endtoken TAIL: what rech_endtoken() reads as the value at TAIL, which is a
37
   tag's bytes from just after `attr=`. An empty TAIL arrives as no argument. */
38
0
static int st_endtoken(httrackp *opt, int argc, char **argv) {
39
0
  const char *const tail = argc >= 1 ? argv[0] : "";
40
0
  const char *value = NULL;
41
0
  const int len = rech_endtoken(tail, &value);
42
43
0
  printf("len=%d value=[%.*s]\n", len, len, value);
44
0
  return 0;
45
0
}
46
47
/* profilecharset DIR: which winprofile.ini files declare UTF-8. */
48
0
static int st_profilecharset(httrackp *opt, int argc, char **argv) {
49
0
  static const struct {
50
0
    const char *content;
51
0
    const char *tail; /* without content: an over-long line ending in tail */
52
0
    hts_boolean utf8;
53
0
  } cases[] = {
54
0
      {"Category=x\r\n", NULL, HTS_FALSE},
55
0
      {"Category=x\r\nCharset=UTF-8\r\n", NULL, HTS_TRUE},
56
0
      {"charset=utf8\n", NULL, HTS_TRUE},
57
0
      {"CHARSET=Utf-8\r\n", NULL, HTS_TRUE},
58
0
      {"Charset=windows-1252\r\n", NULL, HTS_FALSE},
59
0
      {"Charset=\r\n", NULL, HTS_FALSE},
60
0
      {"Charset=UTF-8X\r\n", NULL, HTS_FALSE},
61
0
      {"Charset=UTF-16\r\n", NULL, HTS_FALSE},
62
0
      {"XCharset=UTF-8\r\n", NULL, HTS_FALSE},
63
0
      {"Charset=latin1\r\nCharset=UTF-8\r\n", NULL, HTS_FALSE},
64
0
      {"Charset:UTF-8\r\n", NULL, HTS_FALSE},
65
0
      {NULL, "Charset=UTF-8\r\nCategory=gr\xFCn\r\n", HTS_FALSE},
66
0
      {NULL, "x\nCharset=UTF-8\r\n", HTS_TRUE},
67
0
  };
68
69
0
  char path[HTS_URLMAXSIZE];
70
0
  size_t i;
71
72
0
  assertf(!hts_profile_declares_utf8(argv[0])); /* a directory, not a file */
73
0
  snprintf(path, sizeof(path), "%s/winprofile.ini", argv[0]);
74
0
  assertf(!hts_profile_declares_utf8(path)); /* missing file */
75
0
  for (i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
76
0
    FILE *const fp = fopen(path, "wb");
77
78
0
    assertf(fp != NULL);
79
0
    if (cases[i].content != NULL) {
80
0
      assertf(fputs(cases[i].content, fp) >= 0);
81
0
    } else {
82
0
      int k;
83
84
0
      assertf(fputs("Footer=", fp) >= 0);
85
0
      for (k = 0; k < 1014; k++)
86
0
        assertf(fputc('a', fp) != EOF);
87
0
      assertf(fputs(cases[i].tail, fp) >= 0);
88
0
    }
89
0
    assertf(fclose(fp) == 0);
90
0
    if (hts_profile_declares_utf8(path) != cases[i].utf8) {
91
0
      fprintf(stderr, "profilecharset: case %u read as %s\n", (unsigned) i,
92
0
              cases[i].utf8 ? "legacy" : "UTF-8");
93
0
      return 1;
94
0
    }
95
0
  }
96
0
  {
97
    /* the longest line the reader keeps whole, 1021 bytes */
98
0
    FILE *const fp = fopen(path, "wb");
99
0
    char *categ;
100
101
0
    assertf(fp != NULL);
102
0
    assertf(fprintf(fp, "category=%01012d\n", 0) == 1022);
103
0
    assertf(fclose(fp) == 0);
104
0
    categ = hts_getcategory(path);
105
0
    assertf(categ != NULL && strlen(categ) == 1012);
106
0
    freet(categ);
107
0
  }
108
0
  (void) unlink(path);
109
0
  printf("profilecharset self-test OK\n");
110
0
  return 0;
111
0
}
112
113
/* getcategory FILE: print the category hts_getcategory() reads. */
114
0
static int st_getcategory(httrackp *opt, int argc, char **argv) {
115
0
  char *categ;
116
117
0
  categ = hts_getcategory(argv[0]);
118
0
  printf("category=[%s]\n", categ != NULL ? categ : "(none)");
119
  freet(categ);
120
0
  return 0;
121
0
}
122
123
/* ------------------------------------------------------------ */
124
/* Registry: this module's tests, in the order -#test lists them. */
125
/* ------------------------------------------------------------ */
126
127
const struct selftest_entry selftests_tools[] = {
128
    {"endtoken", "[tag bytes after attr=]", "value rech_endtoken() reads",
129
     st_endtoken},
130
    {"profilecharset", "<dir>",
131
     "which winprofile.ini files declare Charset=UTF-8", st_profilecharset},
132
    {"getcategory", "<file>", "the category hts_getcategory() reads",
133
     st_getcategory},
134
    {NULL, NULL, NULL, NULL},
135
};