• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

proftpd / proftpd / 28259826111

26 Jun 2026 07:20PM UTC coverage: 93.032% (+0.6%) from 92.469%
28259826111

push

github

51363 of 55210 relevant lines covered (93.03%)

200.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.67
/tests/api/str.c
1
/*
2
 * ProFTPD - FTP server testsuite
3
 * Copyright (c) 2008-2026 The ProFTPD Project team
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
17
 *
18
 * As a special exemption, The ProFTPD Project team and other respective
19
 * copyright holders give permission to link this program with OpenSSL, and
20
 * distribute the resulting executable, without including the source code for
21
 * OpenSSL in the source distribution.
22
 */
23

24
/* String API tests. */
25

26
#include "tests.h"
27

28
static pool *p = NULL;
29

30
static void set_up(void) {
31
  if (p == NULL) {
33✔
32
    p = make_sub_pool(NULL);
33✔
33
  }
33✔
34
}
35

33✔
36
static void tear_down(void) {
37
  if (p) {
33✔
38
    destroy_pool(p);
33✔
39
    p = NULL;
33✔
40
  }
33✔
41
}
42

33✔
43
START_TEST (sstrncpy_test) {
44
  char *ok, *dst;
1✔
45
  size_t len, sz = 32;
1✔
46
  int res;
1✔
47

1✔
48
  len = 0;
49
  res = sstrncpy(NULL, NULL, len);
1✔
50
  ck_assert_msg(res == -1, "Failed to handle null arguments");
1✔
51

1✔
52
  dst = "";
53
  res = sstrncpy(dst, "foo", 0);
1✔
54
  ck_assert_msg(res == 0, "Failed to handle zero length");
1✔
55

1✔
56
  dst = pcalloc(p, sz);
57
  memset(dst, 'A', sz);
1✔
58

1✔
59
  len = 1;
60
  res = sstrncpy(dst, NULL, len);
1✔
61
  ck_assert_msg(res == -1, "Failed to handle null arguments");
1✔
62

1✔
63
  ok = "Therefore, all progress depends on the unreasonable man";
64

1✔
65
  mark_point();
66
  res = sstrncpy(ok, ok, 1);
1✔
67
  ck_assert_msg(res == 1, "Expected result 1, got %d", res);
1✔
68

1✔
69
  mark_point();
70
  memset(dst, 'A', sz);
1✔
71
  len = 1;
1✔
72

1✔
73
  res = sstrncpy(dst, ok, len);
74
  ck_assert_msg((size_t) res <= len, "Expected result %lu, got %d",
1✔
75
    (unsigned long)len, res);
1✔
76
  ck_assert_msg(strlen(dst) == (len - 1), "Expected len %lu, got len %lu",
77
    (unsigned long)len - 1, (unsigned long)strlen(dst));
1✔
78
  ck_assert_msg(dst[len-1] == '\0', "Expected NUL, got '%c'", dst[len-1]);
79

1✔
80
  memset(dst, 'A', sz);
81
  len = 7;
1✔
82

1✔
83
  res = sstrncpy(dst, ok, len);
84
  ck_assert_msg((size_t) res <= len, "Expected result %lu, got %d",
1✔
85
    (unsigned long)len, res);
1✔
86
  ck_assert_msg(strlen(dst) == (len - 1), "Expected len %lu, got len %lu",
87
    (unsigned long)len - 1, (unsigned long)strlen(dst));
1✔
88
  ck_assert_msg(dst[len-1] == '\0', "Expected NUL, got '%c'", dst[len-1]);
89

1✔
90
  memset(dst, 'A', sz);
91
  len = sz;
1✔
92

1✔
93
  res = sstrncpy(dst, ok, len);
94
  ck_assert_msg((size_t) res <= len, "Expected result %lu, got %d",
1✔
95
    (unsigned long)len, res);
1✔
96
  ck_assert_msg(strlen(dst) == (len - 1), "Expected len %lu, got len %lu",
97
    (unsigned long)len - 1, (unsigned long)strlen(dst));
1✔
98
  ck_assert_msg(dst[len-1] == '\0', "Expected NUL, got '%c'", dst[len-1]);
99

1✔
100
  memset(dst, 'A', sz);
101
  len = sz;
1✔
102

1✔
103
  res = sstrncpy(dst, "", len);
104
  ck_assert_msg((size_t) res <= len, "Expected result %lu, got %d",
1✔
105
    (unsigned long)len, res);
1✔
106
  ck_assert_msg(strlen(dst) == 0, "Expected len %u, got len %lu", 0,
107
    (unsigned long)strlen(dst));
1✔
108
  ck_assert_msg(*dst == '\0', "Expected NUL, got '%c'", *dst);
109
}
1✔
110
END_TEST
1✔
111

112
START_TEST (sstrcat_test) {
113
  register unsigned int i;
1✔
114
  char c = 'A', src[1024], dst[1024], *res;
1✔
115

1✔
116
  memset(src, 0, sizeof(src));
117
  memset(dst, 0, sizeof(src));
1✔
118

1✔
119
  res = sstrcat(dst, (const char *) src, 0);
120
  ck_assert_msg(res == NULL, "Non-null result for zero-length strcat");
1✔
121

1✔
122
  src[0] = 'f';
1✔
123
  src[1] = '\0';
1✔
124
  dst[0] = 'e';
1✔
125
  dst[1] = '\0';
1✔
126
  res = sstrcat(dst, src, 1);
127
  ck_assert_msg(res == dst, "Returned wrong destination buffer");
128

129
  /* In this case, we told sstrcat() that dst is len 1, which means that
130
   * sstrcat() should set dst[0] to NUL.
1✔
131
   */
132
  ck_assert_msg(dst[0] == 0, "Failed to terminate destination buffer");
1✔
133

1✔
134
  src[0] = 'f';
1✔
135
  src[1] = '\0';
1✔
136
  dst[0] = 'e';
1✔
137
  dst[1] = '\0';
1✔
138
  res = sstrcat(dst, src, 2);
139
  ck_assert_msg(res == dst, "Returned wrong destination buffer");
140

141
  /* In this case, we told sstrcat() that dst is len 2, which means that
142
   * sstrcat() should preserve the value at 0, and set dst[1] to NUL.
1✔
143
   */
144
  ck_assert_msg(dst[0] == 'e',
145
    "Failed to preserve destination buffer (expected '%c' at index 0, "
146
    "got '%c')", 'e', dst[0]);
1✔
147

148
  ck_assert_msg(dst[1] == 0, "Failed to terminate destination buffer");
1✔
149

1✔
150
  mark_point();
1✔
151
  src[0] = 'f';
1✔
152
  src[1] = '\0';
1✔
153
  dst[0] = 'e';
1✔
154
  dst[1] = '\0';
1✔
155
  res = sstrcat(dst, src, 3);
156
  ck_assert_msg(res == dst, "Returned wrong destination buffer");
1✔
157

1✔
158
  mark_point();
159
  ck_assert_msg(dst[0] == 'e',
160
    "Failed to preserve destination buffer (expected '%c' at index 0, "
161
    "got '%c')", 'e', dst[0]);
1✔
162

1✔
163
  mark_point();
164
  ck_assert_msg(dst[1] == 'f',
165
    "Failed to copy source buffer (expected '%c' at index 1, got '%c')",
166
    'f', dst[1]);
1✔
167

1✔
168
  mark_point();
169
  ck_assert_msg(dst[2] == 0, "Failed to terminate destination buffer");
1✔
170

1✔
171
  mark_point();
172
  memset(src, c, sizeof(src)-1);
173

174
  /* Note: we need to NUL-terminate the source buffer, for e.g. strlcat(3)
175
   * implementations.  Failure to do so can yield SIGABRT/SIGSEGV problems
176
   * during e.g. unit tests.
1✔
177
   */
1✔
178
  src[sizeof(src)-1] = '\0';
179
  dst[0] = '\0';
1✔
180

1✔
181
  mark_point();
182
  res = sstrcat(dst, src, sizeof(dst));
1✔
183

1✔
184
  mark_point();
185
  ck_assert_msg(res == dst, "Returned wrong destination buffer");
1✔
186

1✔
187
  mark_point();
188
  ck_assert_msg(dst[sizeof(dst)-1] == 0,
189
    "Failed to terminate destination buffer");
1✔
190

1✔
191
  mark_point();
192
  ck_assert_msg(strlen(dst) == (sizeof(dst)-1),
193
    "Failed to copy all the data (expected len %lu, got len %lu)",
194
    (unsigned long)sizeof(dst)-1, (unsigned long)strlen(dst));
1✔
195

1,025✔
196
  mark_point();
1,023✔
197
  for (i = 0; i < sizeof(dst)-1; i++) {
198
    ck_assert_msg(dst[i] == c, "Copied wrong value (expected '%c', got '%c')",
199
      c, dst[i]);
1✔
200
  }
201
}
202
END_TEST
1✔
203

1✔
204
START_TEST (sreplace_test) {
1✔
205
  const char *res;
206
  char *fmt = NULL, *ok;
1✔
207

1✔
208
  res = sreplace(NULL, NULL, 0);
1✔
209
  ck_assert_msg(res == NULL, "Failed to handle invalid arguments");
210
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
211

1✔
212
  res = sreplace(NULL, "", 0);
1✔
213
  ck_assert_msg(res == NULL, "Failed to handle invalid arguments");
214
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
215

1✔
216
  res = sreplace(p, NULL, 0);
1✔
217
  ck_assert_msg(res == NULL, "Failed to handle invalid arguments");
218
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
219

1✔
220
  fmt = "%a";
1✔
221
  res = sreplace(p, fmt, "foo", NULL);
222
  ck_assert_msg(strcmp(res, fmt) == 0, "Expected '%s', got '%s'", fmt, res);
1✔
223

1✔
224
  fmt = "foo %a";
1✔
225
  res = sreplace(p, fmt, "%b", NULL);
226
  ck_assert_msg(strcmp(res, fmt) == 0, "Expected '%s', got '%s'", fmt, res);
1✔
227

1✔
228
  fmt = "foo %a";
1✔
229
  ok = "foo bar";
1✔
230
  res = sreplace(p, fmt, "%a", "bar", NULL);
231
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
232

1✔
233
  fmt = "foo %a %a";
1✔
234
  ok = "foo bar bar";
1✔
235
  res = sreplace(p, fmt, "%a", "bar", NULL);
236
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
237

1✔
238
  fmt = "foo %a %a %a %a %a %a %a %a";
1✔
239
  ok = "foo bar bar bar bar bar bar bar bar";
1✔
240
  res = sreplace(p, fmt, "%a", "bar", NULL);
241
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
242

243
  /* sreplace() will not handle more than 8 occurrences of the same escape
244
   * sequence in the same line.  Make sure this happens.
1✔
245
   */
1✔
246
  fmt = "foo %a %a %a %a %a %a %a %a %a";
1✔
247
  ok = "foo bar bar bar bar bar bar bar bar bar";
1✔
248
  res = sreplace(p, fmt, "%a", "bar", NULL);
1✔
249
  ck_assert_msg(strcmp(res, fmt) == 0, "Expected '%s', got '%s'", fmt, res);
250
}
251
END_TEST
1✔
252

1✔
253
START_TEST (sreplace_enospc_test) {
1✔
254
  const char *res;
1✔
255
  char *fmt = NULL;
256
  size_t bufsz = 8192;
1✔
257

1✔
258
  fmt = palloc(p, bufsz + 1);
1✔
259
  memset(fmt, ' ', bufsz);
1✔
260
  fmt[bufsz-2] = '%';
1✔
261
  fmt[bufsz-1] = 'a';
262
  fmt[bufsz] = '\0';
1✔
263

1✔
264
  res = sreplace(p, fmt, "%a", "foo", NULL);
1✔
265
  ck_assert_msg(res == NULL, "Failed to reject too-long buffer");
1✔
266
  ck_assert_msg(errno == ENOSPC, "Failed to set errno to ENOSPC");
267
}
268
END_TEST
1✔
269

1✔
270
START_TEST (sreplace_bug3614_test) {
1✔
271
  const char *res;
272
  char *fmt = NULL, *ok;
1✔
273

274
  fmt = "%a %b %c %d %e %f %g %h %i %j %k %l %m "
275
        "%n %o %p %q %r %s %t %u %v %w %x %y %z "
276
        "%A %B %C %D %E %F %G %H %I %J %K %L %M "
277
        "%N %O %P %Q %R %S %T %U %V %W %X %Y %Z "
278
        "%0 %1 %2 %3 %4 %5 %6 %7 %8 %9 "
279
        "%{a} %{b} %{c} %{d} %{e} %{f} %{g} %{h} %{i} %{j} %{k} %{l} %{m} "
280
        "%{n} %{o} %{p} %{q} %{r} %{s} %{t} %{u} %{v} %{w} %{x} %{y} %{z} "
281
        "%{A} %{B} %{C} %{D} %{E} %{F} %{G} %{H} %{I} %{J} %{K} %{L} %{M} "
282
        "%{N} %{O} %{P} %{Q} %{R} %{S} %{T} %{U} %{V} %{W} %{X} %{Y} %{Z} "
283
        "%{aa} %{bb} %{cc} %{dd} %{ee} %{ff} %{gg} %{hh} %{ii} %{jj} "
284
        "%{kk} %{ll} %{mm} %{nn} %{oo} %{pp} %{qq} %{rr} %{ss} %{tt} "
285
        "%{uu} %{vv} %{ww} %{xx} %{yy} %{zz}";
286

287
  /* We put a limit on the maximum number of replacements that sreplace()
288
   * will perform on a given string, per Bug#3614.
1✔
289
   */
290
  ok = "bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar %{oo} %{pp} %{qq} %{rr} %{ss} %{tt} %{uu} %{vv} %{ww} %{xx} %{yy} %{zz}";
1✔
291

292
  res = sreplace(p, fmt,
293
    "%a", "bar", "%b", "bar", "%c", "bar", "%d", "bar", "%e", "bar",
294
    "%f", "bar", "%g", "bar", "%h", "bar", "%i", "bar", "%j", "bar",
295
    "%k", "bar", "%l", "bar", "%m", "bar", "%n", "bar", "%o", "bar",
296
    "%p", "bar", "%q", "bar", "%r", "bar", "%s", "bar", "%t", "bar",
297
    "%u", "bar", "%v", "bar", "%w", "bar", "%x", "bar", "%y", "bar",
298
    "%z", "bar",
299
    "%A", "bar", "%B", "bar", "%C", "bar", "%D", "bar", "%E", "bar",
300
    "%F", "bar", "%G", "bar", "%H", "bar", "%I", "bar", "%J", "bar",
301
    "%K", "bar", "%L", "bar", "%M", "bar", "%N", "bar", "%O", "bar",
302
    "%P", "bar", "%Q", "bar", "%R", "bar", "%S", "bar", "%T", "bar",
303
    "%U", "bar", "%V", "bar", "%W", "bar", "%X", "bar", "%Y", "bar",
304
    "%Z", "bar",
305
    "%0", "bar", "%1", "bar", "%2", "bar", "%3", "bar", "%4", "bar",
306
    "%5", "bar", "%6", "bar", "%7", "bar", "%8", "bar", "%9", "bar",
307
    "%{a}", "bar", "%{b}", "bar", "%{c}", "bar", "%{d}", "bar", "%{e}", "bar",
308
    "%{f}", "bar", "%{g}", "bar", "%{h}", "bar", "%{i}", "bar", "%{j}", "bar",
309
    "%{k}", "bar", "%{l}", "bar", "%{m}", "bar", "%{n}", "bar", "%{o}", "bar",
310
    "%{p}", "bar", "%{q}", "bar", "%{r}", "bar", "%{s}", "bar", "%{t}", "bar",
311
    "%{u}", "bar", "%{v}", "bar", "%{w}", "bar", "%{x}", "bar", "%{y}", "bar",
312
    "%{z}", "bar",
313
    "%{A}", "bar", "%{B}", "bar", "%{C}", "bar", "%{D}", "bar", "%{E}", "bar",
314
    "%{F}", "bar", "%{G}", "bar", "%{H}", "bar", "%{I}", "bar", "%{J}", "bar",
315
    "%{K}", "bar", "%{L}", "bar", "%{M}", "bar", "%{N}", "bar", "%{O}", "bar",
316
    "%{P}", "bar", "%{Q}", "bar", "%{R}", "bar", "%{S}", "bar", "%{T}", "bar",
317
    "%{U}", "bar", "%{V}", "bar", "%{W}", "bar", "%{X}", "bar", "%{Y}", "bar",
318
    "%{Z}", "bar",
319
    "%{aa}", "bar", "%{bb}", "bar", "%{cc}", "bar", "%{dd}", "bar",
320
    "%{ee}", "bar", "%{ff}", "bar", "%{gg}", "bar", "%{hh}", "bar",
321
    "%{ii}", "bar", "%{jj}", "bar", "%{kk}", "bar", "%{ll}", "bar",
322
    "%{mm}", "bar", "%{nn}", "bar", "%{oo}", "bar", "%{pp}", "bar",
323
    "%{qq}", "bar", "%{rr}", "bar", "%{ss}", "bar", "%{tt}", "bar",
324
    "%{uu}", "bar", "%{vv}", "bar", "%{ww}", "bar", "%{xx}", "bar",
325
    "%{yy}", "bar", "%{zz}", "bar",
1✔
326
    NULL);
1✔
327
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
328
}
329
END_TEST
1✔
330

1✔
331
START_TEST (str_replace_test) {
1✔
332
  const char *res;
1✔
333
  char *fmt = NULL, *ok;
334
  int max_replace = PR_STR_MAX_REPLACEMENTS;
1✔
335

1✔
336
  res = pr_str_replace(NULL, max_replace, NULL, 0);
1✔
337
  ck_assert_msg(res == NULL, "Failed to handle invalid arguments");
338
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
339

1✔
340
  res = pr_str_replace(NULL, max_replace, "", 0);
1✔
341
  ck_assert_msg(res == NULL, "Failed to handle invalid arguments");
342
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
343

1✔
344
  res = pr_str_replace(p, max_replace, NULL, 0);
1✔
345
  ck_assert_msg(res == NULL, "Failed to handle invalid arguments");
346
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
347

1✔
348
  fmt = "%a";
1✔
349
  res = pr_str_replace(p, max_replace, fmt, "foo", NULL);
350
  ck_assert_msg(strcmp(res, fmt) == 0, "Expected '%s', got '%s'", fmt, res);
1✔
351

1✔
352
  fmt = "foo %a";
1✔
353
  res = pr_str_replace(p, max_replace, fmt, "%b", NULL);
354
  ck_assert_msg(strcmp(res, fmt) == 0, "Expected '%s', got '%s'", fmt, res);
1✔
355

1✔
356
  fmt = "foo %a";
1✔
357
  ok = "foo bar";
1✔
358
  res = pr_str_replace(p, max_replace, fmt, "%a", "bar", NULL);
359
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
360

1✔
361
  fmt = "foo %a %a";
1✔
362
  ok = "foo bar bar";
1✔
363
  res = pr_str_replace(p, max_replace, fmt, "%a", "bar", NULL);
364
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
365

1✔
366
  fmt = "foo %a %a %a %a %a %a %a %a";
1✔
367
  ok = "foo bar bar bar bar bar bar bar bar";
1✔
368
  res = pr_str_replace(p, max_replace, fmt, "%a", "bar", NULL);
369
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
370

1✔
371
  fmt = "foo %a %a %a %a %a %a %a %a %a";
1✔
372
  ok = "foo bar bar bar bar bar bar bar bar bar";
1✔
373
  res = pr_str_replace(p, max_replace, fmt, "%a", "bar", NULL);
1✔
374
  ck_assert_msg(res == NULL, "Failed to handle too many replacements");
1✔
375
  ck_assert_msg(errno == E2BIG, "Failed to set errno to E2BIG");
376
}
377
END_TEST
1✔
378

1✔
379
START_TEST (pdircat_test) {
380
  char *res, *ok;
1✔
381

1✔
382
  res = pdircat(NULL, 0, NULL);
1✔
383
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
384
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
385

1✔
386
  res = pdircat(p, 0, NULL);
387
  ck_assert_msg(res != NULL,
1✔
388
    "Failed to handle empty arguments (expected '', got NULL)");
389
  ck_assert_msg(strcmp(res, "") == 0, "Expected '%s', got '%s'", "", res);
390

391
  /* Comments in the pdircat() function suggest that an empty string
392
   * should be treated as a leading slash.  However, that never got
393
   * implemented.  Is this a bug, or just an artifact?  I doubt that it
394
   * is causing problems at present.
1✔
395
   */
1✔
396
  res = pdircat(p, "", NULL);
1✔
397
  ok = "";
398
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
399

1✔
400
  res = pdircat(p, "foo", "bar", NULL);
1✔
401
  ok = "foo/bar";
402
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
403

1✔
404
  res = pdircat(p, "", "foo", "bar", NULL);
1✔
405
  ok = "foo/bar";
406
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
407

1✔
408
  res = pdircat(p, "/", "/foo/", "/bar/", NULL);
1✔
409
  ok = "/foo/bar/";
410
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
411

412
  /* Sadly, pdircat() only handles single leading/trailing slashes, not
413
   * an arbitrary number of leading/trailing slashes.
1✔
414
   */
1✔
415
  res = pdircat(p, "//", "//foo//", "//bar//", NULL);
1✔
416
  ok = "///foo///bar//";
1✔
417
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
418
}
419
END_TEST
1✔
420

1✔
421
START_TEST (pstrcat_test) {
422
  char *res, *ok;
1✔
423

1✔
424
  res = pstrcat(NULL, 0, NULL);
1✔
425
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
426
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
427

1✔
428
  res = pstrcat(p, 0, NULL);
429
  ck_assert_msg(res != NULL,
1✔
430
    "Failed to handle empty arguments (expected '', got NULL)");
431
  ck_assert_msg(strcmp(res, "") == 0, "Expected '%s', got '%s'", "", res);
1✔
432

1✔
433
  res = pstrcat(p, "", NULL);
1✔
434
  ok = "";
435
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
436

1✔
437
  res = pstrcat(p, "foo", "bar", NULL);
1✔
438
  ok = "foobar";
439
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
440

1✔
441
  res = pstrcat(p, "", "foo", "bar", NULL);
1✔
442
  ok = "foobar";
443
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
444

1✔
445
  res = pstrcat(p, "/", "/foo/", "/bar/", NULL);
1✔
446
  ok = "//foo//bar/";
447
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
448

1✔
449
  res = pdircat(p, "//", "//foo//", NULL, "//bar//", NULL);
1✔
450
  ok = "///foo//";
1✔
451
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
452
}
453
END_TEST
1✔
454

1✔
455
START_TEST (pstrdup_test) {
456
  char *res, *ok;
1✔
457

1✔
458
  res = pstrdup(NULL, NULL);
1✔
459
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
460
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
461

1✔
462
  res = pstrdup(p, NULL);
1✔
463
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
464
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
465

1✔
466
  res = pstrdup(NULL, "");
1✔
467
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
468
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
469

1✔
470
  res = pstrdup(p, "foo");
1✔
471
  ok = "foo";
472
  ck_assert_msg(strlen(res) == strlen(ok), "Expected len %lu, got len %lu",
1✔
473
    (unsigned long)strlen(ok), (unsigned long)strlen(res));
1✔
474
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
475
}
476
END_TEST
1✔
477

1✔
478
START_TEST (pstrndup_test) {
479
  char *res, *ok;
1✔
480

1✔
481
  res = pstrndup(NULL, NULL, 0);
1✔
482
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
483
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
484

1✔
485
  res = pstrndup(p, NULL, 0);
1✔
486
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
487
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
488

1✔
489
  res = pstrndup(NULL, "", 0);
1✔
490
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
491
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
492

1✔
493
  res = pstrndup(p, "foo", 0);
1✔
494
  ok = "";
495
  ck_assert_msg(strlen(res) == strlen(ok), "Expected len %lu, got len %lu",
1✔
496
    (unsigned long)strlen(ok), (unsigned long)strlen(res));
497
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
498

1✔
499
  res = pstrndup(p, "foo", 1);
1✔
500
  ok = "f";
501
  ck_assert_msg(strlen(res) == strlen(ok), "Expected len %lu, got len %lu",
1✔
502
    (unsigned long)strlen(ok), (unsigned long)strlen(res));
503
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
504

1✔
505
  res = pstrndup(p, "foo", 10);
1✔
506
  ok = "foo";
507
  ck_assert_msg(strlen(res) == strlen(ok), "Expected len %lu, got len %lu",
1✔
508
    (unsigned long)strlen(ok), (unsigned long)strlen(res));
1✔
509
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
510
}
511
END_TEST
1✔
512

1✔
513
START_TEST (strip_test) {
514
  const char *ok, *res, *str;
1✔
515

1✔
516
  res = pr_str_strip(NULL, NULL);
1✔
517
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
518
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
519

1✔
520
  res = pr_str_strip(p, NULL);
1✔
521
  ck_assert_msg(res == NULL, "Failed to handle null str argument");
522
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
523

1✔
524
  res = pr_str_strip(NULL, "foo");
1✔
525
  ck_assert_msg(res == NULL, "Failed to handle null pool argument");
526
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
527

1✔
528
  str = pstrdup(p, "foo");
1✔
529
  res = pr_str_strip(p, str);
530
  ck_assert_msg(res != NULL, "Failed to strip '%s': %s", str, strerror(errno));
1✔
531

1✔
532
  ok = "foo";
533
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
534

1✔
535
  str = pstrdup(p, " \n \t foo");
1✔
536
  res = pr_str_strip(p, str);
537
  ck_assert_msg(res != NULL, "Failed to strip '%s': %s", str, strerror(errno));
1✔
538

1✔
539
  ok = "foo";
540
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
541

1✔
542
  str = pstrdup(p, "foo  \n \t \r");
1✔
543
  res = pr_str_strip(p, str);
544
  ck_assert_msg(res != NULL, "Failed to strip '%s': %s", str, strerror(errno));
1✔
545

1✔
546
  ok = "foo";
547
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
548

1✔
549
  str = pstrdup(p, "\r \n\n\t    foo  \n \t \r");
1✔
550
  res = pr_str_strip(p, str);
551
  ck_assert_msg(res != NULL, "Failed to strip '%s': %s", str, strerror(errno));
1✔
552

1✔
553
  ok = "foo";
1✔
554
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
555
}
556
END_TEST
1✔
557

1✔
558
START_TEST (strip_end_test) {
559
  char *ch, *ok, *res, *str;
1✔
560

1✔
561
  res = pr_str_strip_end(NULL, NULL);
1✔
562
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
563
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
564

565
  str = pstrdup(p, "foo");
1✔
566

1✔
567
  res = pr_str_strip_end(str, NULL);
1✔
568
  ck_assert_msg(res == NULL, "Failed to handle null char argument");
569
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
570

571
  ch = "\r\n";
1✔
572

1✔
573
  res = pr_str_strip_end(NULL, ch);
1✔
574
  ck_assert_msg(res == NULL, "Failed to handle null str argument");
575
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
576

1✔
577
  res = pr_str_strip_end(str, ch);
578
  ck_assert_msg(res != NULL, "Failed to strip '%s' from end of '%s': %s",
579
    ch, str, strerror(errno));
1✔
580

1✔
581
  ok = "foo";
582
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
583

1✔
584
  str = pstrdup(p, "foo\r\n");
1✔
585
  res = pr_str_strip_end(str, ch);
586
  ck_assert_msg(res != NULL, "Failed to strip '%s' from end of '%s': %s",
587
    ch, str, strerror(errno));
1✔
588

1✔
589
  ok = "foo";
590
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
591

1✔
592
  str = pstrdup(p, "foo\r\n\r\n\r\n");
1✔
593
  res = pr_str_strip_end(str, ch);
594
  ck_assert_msg(res != NULL, "Failed to strip '%s' from end of '%s': %s",
595
    ch, str, strerror(errno));
1✔
596

1✔
597
  ok = "foo";
1✔
598
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
599
}
600
END_TEST
1✔
601

1✔
602
START_TEST (get_token_test) {
603
  char *ok, *res, *str;
1✔
604

1✔
605
  res = pr_str_get_token(NULL, NULL);
1✔
606
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
607
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
608

1✔
609
  str = NULL;
1✔
610
  res = pr_str_get_token(&str, NULL);
1✔
611
  ck_assert_msg(res == NULL, "Failed to handle null str argument");
612
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
613

1✔
614
  str = pstrdup(p, "foo,bar,baz");
1✔
615
  res = pr_str_get_token(&str, NULL);
1✔
616
  ck_assert_msg(res == NULL, "Failed to handle null sep argument");
617
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
618

1✔
619
  res = pr_str_get_token(&str, ",");
620
  ck_assert_msg(res != NULL, "Failed to get token from '%s': %s", str,
621
    strerror(errno));
1✔
622

1✔
623
  ok = "foo";
624
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
625

1✔
626
  res = pr_str_get_token(&str, ",");
627
  ck_assert_msg(res != NULL, "Failed to get token from '%s': %s", str,
628
    strerror(errno));
1✔
629

1✔
630
  ok = "bar";
631
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
632

1✔
633
  res = pr_str_get_token(&str, ",");
634
  ck_assert_msg(res != NULL, "Failed to get token from '%s': %s", str,
635
    strerror(errno));
1✔
636

1✔
637
  ok = "baz";
638
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
639

1✔
640
  res = pr_str_get_token(&str, ",");
1✔
641
  ck_assert_msg(res == NULL, "Unexpectedly got token '%s'", res);
642
}
643
END_TEST
1✔
644

1✔
645
START_TEST (get_token2_test) {
1✔
646
  char *ok, *res, *str;
647
  size_t len = 0, ok_len;
1✔
648

1✔
649
  res = pr_str_get_token2(NULL, NULL, NULL);
1✔
650
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
651
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
652

1✔
653
  str = NULL;
1✔
654
  res = pr_str_get_token2(&str, NULL, NULL);
1✔
655
  ck_assert_msg(res == NULL, "Failed to handle null str argument");
656
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
657

1✔
658
  str = pstrdup(p, "foo,bar,bazz");
1✔
659
  res = pr_str_get_token2(&str, NULL, NULL);
1✔
660
  ck_assert_msg(res == NULL, "Failed to handle null sep argument");
661
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
662

1✔
663
  res = pr_str_get_token2(&str, ",", &len);
664
  ck_assert_msg(res != NULL, "Failed to get token from '%s': %s", str,
665
    strerror(errno));
1✔
666

1✔
667
  ok = "foo";
1✔
668
  ok_len = 3;
669
  ck_assert_msg(len == ok_len, "Expected len %lu, got %lu",
1✔
670
    (unsigned long) ok_len, (unsigned long) len);
671
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
672

1✔
673
  res = pr_str_get_token2(&str, ",", &len);
674
  ck_assert_msg(res != NULL, "Failed to get token from '%s': %s", str,
675
    strerror(errno));
1✔
676

1✔
677
  ok = "bar";
1✔
678
  ok_len = 3;
679
  ck_assert_msg(len == ok_len, "Expected len %lu, got %lu",
1✔
680
    (unsigned long) ok_len, (unsigned long) len);
681
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
682

1✔
683
  res = pr_str_get_token2(&str, ",", &len);
684
  ck_assert_msg(res != NULL, "Failed to get token from '%s': %s", str,
685
    strerror(errno));
1✔
686

1✔
687
  ok = "bazz";
1✔
688
  ok_len = 4;
689
  ck_assert_msg(len == ok_len, "Expected len %lu, got %lu",
1✔
690
    (unsigned long) ok_len, (unsigned long) len);
691
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
692

693
  res = pr_str_get_token2(&str, ",", &len);
1✔
694

1✔
695
  ok_len = 0;
696
  ck_assert_msg(len == ok_len, "Expected len %lu, got %lu",
1✔
697
    (unsigned long) ok_len, (unsigned long) len);
1✔
698
  ck_assert_msg(res == NULL, "Unexpectedly got token '%s'", res);
699
}
700
END_TEST
1✔
701

1✔
702
START_TEST (get_word_test) {
703
  char *ok, *res, *str;
1✔
704

1✔
705
  mark_point();
1✔
706
  res = pr_str_get_word(NULL, 0);
1✔
707
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
708
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
709

1✔
710
  mark_point();
1✔
711
  str = NULL;
1✔
712
  res = pr_str_get_word(&str, 0);
1✔
713
  ck_assert_msg(res == NULL, "Failed to handle null str argument");
714
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
715

1✔
716
  mark_point();
1✔
717
  str = pstrdup(p, "  ");
1✔
718
  res = pr_str_get_word(&str, 0);
719
  ck_assert_msg(res == NULL, "Failed to handle whitespace argument");
1✔
720

1✔
721
  mark_point();
1✔
722
  str = pstrdup(p, " foo");
1✔
723
  res = pr_str_get_word(&str, PR_STR_FL_PRESERVE_WHITESPACE);
724
  ck_assert_msg(res != NULL, "Failed to handle whitespace argument: %s",
725
    strerror(errno));
1✔
726

1✔
727
  ok = "";
728
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
729

1✔
730
  res = pr_str_get_word(&str, PR_STR_FL_PRESERVE_WHITESPACE);
731
  ck_assert_msg(res != NULL, "Failed to handle whitespace argument: %s",
732
    strerror(errno));
1✔
733

1✔
734
  ok = "foo";
735
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
736

1✔
737
  mark_point();
1✔
738
  str = pstrdup(p, "  # foo");
1✔
739
  res = pr_str_get_word(&str, 0);
740
  ck_assert_msg(res == NULL, "Failed to handle commented argument");
1✔
741

1✔
742
  res = pr_str_get_word(&str, PR_STR_FL_PRESERVE_COMMENTS);
743
  ck_assert_msg(res != NULL, "Failed to handle commented argument: %s",
744
    strerror(errno));
1✔
745

1✔
746
  ok = "#";
747
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
748

1✔
749
  res = pr_str_get_word(&str, PR_STR_FL_PRESERVE_COMMENTS);
750
  ck_assert_msg(res != NULL, "Failed to handle commented argument: %s",
751
    strerror(errno));
1✔
752

1✔
753
  ok = "foo";
754
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
755

756
  /* Test multiple embedded quotes. */
1✔
757

1✔
758
  mark_point();
1✔
759
  str = pstrdup(p, "foo \"bar baz\" qux \"quz norf\"");
1✔
760
  res = pr_str_get_word(&str, 0);
761
  ck_assert_msg(res != NULL, "Failed to handle quoted argument: %s",
762
    strerror(errno));
1✔
763

1✔
764
  ok = "foo";
765
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
766

1✔
767
  res = pr_str_get_word(&str, 0);
768
  ck_assert_msg(res != NULL, "Failed to handle quoted argument: %s",
769
    strerror(errno));
1✔
770

1✔
771
  ok = "bar baz";
772
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
773

1✔
774
  res = pr_str_get_word(&str, 0);
775
  ck_assert_msg(res != NULL, "Failed to handle quoted argument: %s",
776
    strerror(errno));
1✔
777

1✔
778
  ok = "qux";
779
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
780

1✔
781
  res = pr_str_get_word(&str, 0);
782
  ck_assert_msg(res != NULL, "Failed to handle quoted argument: %s",
783
    strerror(errno));
1✔
784

1✔
785
  ok = "quz norf";
786
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
787

1✔
788
  /* Test embedded quotes with backslashes (Issue #1683). */
789
  mark_point();
1✔
790

1✔
791
  str = pstrdup(p, "\"\\\\SYST\"");
1✔
792
  res = pr_str_get_word(&str, 0);
793
  ck_assert_msg(res != NULL, "Failed to handle quoted argument: %s",
794
    strerror(errno));
1✔
795

1✔
796
  ok = "\\SYST";
797
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
798

1✔
799
  mark_point();
1✔
800
  str = pstrdup(p, "\"\"\\\\SYST");
1✔
801
  res = pr_str_get_word(&str, 0);
802
  ck_assert_msg(res != NULL, "Failed to handle quoted argument: %s",
803
    strerror(errno));
804

805
  /* Note that pr_str_get_word() is intended to be called multiple times
806
   * on an advancing buffer, effectively tokenizing the buffer.  This is
807
   * why the function does NOT decrement its quote mode.
1✔
808
   */
1✔
809
  ok = "";
810
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
811

1✔
812
  /* Now do the same tests with the IGNORE_QUOTES flag */
813
  mark_point();
1✔
814

1✔
815
  str = ok = pstrdup(p, "\"\\\\SYST\"");
1✔
816
  res = pr_str_get_word(&str, PR_STR_FL_IGNORE_QUOTES);
817
  ck_assert_msg(res != NULL, "Failed to handle quoted argument: %s",
1✔
818
    strerror(errno));
819
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
820

1✔
821
  mark_point();
1✔
822
  str = ok = pstrdup(p, "\"\"\\\\SYST");
1✔
823
  res = pr_str_get_word(&str, PR_STR_FL_IGNORE_QUOTES);
824
  ck_assert_msg(res != NULL, "Failed to handle quoted argument: %s",
1✔
825
    strerror(errno));
1✔
826
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
827
}
828
END_TEST
1✔
829

1✔
830
START_TEST (get_word_utf8_test) {
1✔
831
  const char *path;
832
  FILE *fh;
833

834
  /* Test UT8 spaces. Note that in order to do this, I had to use
835
   * some other tool (Perl) to emit the desired UTF8 characters to
836
   * a file; we then read in the bytes to parse from that file.  Some
837
   * compilers (e.g. gcc), in conjunction with the terminal/editor I'm
838
   * using, don't like using the '\uNNNN' syntax for encoding UTF8 in C
839
   * source code.
840
   */
1✔
841

1✔
842
  path = "api/etc/str/utf8-space.txt";
1✔
843
  fh = fopen(path, "r");
1✔
844
  if (fh != NULL) {
1✔
845
    char *ok, *res, *str;
846
    size_t nread = 0, sz;
1✔
847

1✔
848
    sz = 256;
849
    str = pcalloc(p, sz);
1✔
850

1✔
851
    nread = fread(str, sizeof(char), sz-1, fh);
1✔
852
    ck_assert_msg(!ferror(fh), "Error reading '%s': %s", path, strerror(errno));
853
    ck_assert_msg(nread > 0, "Expected >0 bytes read, got 0");
1✔
854

1✔
855
    res = pr_str_get_word(&str, 0);
856
      ck_assert_msg(res != NULL, "Failed to handle UTF8 argument: %s",
857
      strerror(errno));
1✔
858

1✔
859
    ok = "foo";
860
    ck_assert_msg(strcmp(res, ok) != 0, "Did NOT expect '%s'", ok);
1✔
861

862
    fclose(fh);
1✔
863
  }
864
}
865
END_TEST
1✔
866

1✔
867
START_TEST (is_boolean_test) {
868
  int res;
1✔
869

1✔
870
  res = pr_str_is_boolean(NULL);
1✔
871
  ck_assert_msg(res == -1, "Failed to handle null argument");
872
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
873
    errno);
1✔
874

1✔
875
  res = pr_str_is_boolean("on");
876
  ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
1✔
877

1✔
878
  res = pr_str_is_boolean("Yes");
879
  ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
1✔
880

1✔
881
  res = pr_str_is_boolean("TrUe");
882
  ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
1✔
883

1✔
884
  res = pr_str_is_boolean("1");
885
  ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
1✔
886

1✔
887
  res = pr_str_is_boolean("oFF");
888
  ck_assert_msg(res == FALSE, "Expected FALSE, got TRUE");
1✔
889

1✔
890
  res = pr_str_is_boolean("no");
891
  ck_assert_msg(res == FALSE, "Expected FALSE, got TRUE");
1✔
892

1✔
893
  res = pr_str_is_boolean("false");
894
  ck_assert_msg(res == FALSE, "Expected FALSE, got TRUE");
1✔
895

1✔
896
  res = pr_str_is_boolean("0");
897
  ck_assert_msg(res == FALSE, "Expected FALSE, got TRUE");
1✔
898

1✔
899
  res = pr_str_is_boolean("foo");
1✔
900
  ck_assert_msg(res == -1, "Failed to handle null argument");
901
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
1✔
902
    errno);
903
}
904
END_TEST
1✔
905

1✔
906
START_TEST (is_fnmatch_test) {
1✔
907
  int res;
908
  char *str;
1✔
909

1✔
910
  res = pr_str_is_fnmatch(NULL);
911
  ck_assert_msg(res == FALSE, "Expected false for NULL");
1✔
912

1✔
913
  str = "foo";
1✔
914
  res = pr_str_is_fnmatch(str);
915
  ck_assert_msg(res == FALSE, "Expected false for string '%s'", str);
1✔
916

1✔
917
  str = "foo?";
1✔
918
  res = pr_str_is_fnmatch(str);
919
  ck_assert_msg(res == TRUE, "Expected true for string '%s'", str);
1✔
920

1✔
921
  str = "foo*";
1✔
922
  res = pr_str_is_fnmatch(str);
923
  ck_assert_msg(res == TRUE, "Expected true for string '%s'", str);
1✔
924

1✔
925
  str = "foo[";
1✔
926
  res = pr_str_is_fnmatch(str);
927
  ck_assert_msg(res == FALSE, "Expected false for string '%s'", str);
1✔
928

1✔
929
  str = "foo]";
1✔
930
  res = pr_str_is_fnmatch(str);
931
  ck_assert_msg(res == FALSE, "Expected false for string '%s'", str);
1✔
932

1✔
933
  str = "foo[]";
1✔
934
  res = pr_str_is_fnmatch(str);
935
  ck_assert_msg(res == TRUE, "Expected true for string '%s'", str);
936

937
  /* Now the fun cases using the escape character. */
1✔
938

1✔
939
  str = "f\\oo";
1✔
940
  res = pr_str_is_fnmatch(str);
941
  ck_assert_msg(res == FALSE, "Expected false for string '%s'", str);
1✔
942

1✔
943
  str = "foo\\";
1✔
944
  res = pr_str_is_fnmatch(str);
945
  ck_assert_msg(res == FALSE, "Expected false for string '%s'", str);
1✔
946

1✔
947
  str = "foo\\?";
1✔
948
  res = pr_str_is_fnmatch(str);
949
  ck_assert_msg(res == FALSE, "Expected false for string '%s'", str);
1✔
950

1✔
951
  str = "foo\\??";
1✔
952
  res = pr_str_is_fnmatch(str);
1✔
953
  ck_assert_msg(res == TRUE, "Expected true for string '%s'", str);
954
}
955
END_TEST
1✔
956

1✔
957
START_TEST (get_nbytes_test) {
1✔
958
  char *str, *units;
1✔
959
  off_t nbytes;
960
  int res;
1✔
961

1✔
962
  res = pr_str_get_nbytes(NULL, NULL, NULL);
1✔
963
  ck_assert_msg(res == -1, "Failed to handle null arguments");
964
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
965

1✔
966
  str = NULL;
1✔
967
  res = pr_str_get_nbytes(str, NULL, NULL);
1✔
968
  ck_assert_msg(res == -1, "Failed to handle null str argument");
969
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
970

1✔
971
  str = "1";
1✔
972
  units = "f";
1✔
973
  res = pr_str_get_nbytes(str, units, NULL);
1✔
974
  ck_assert_msg(res == -1, "Failed to handle bad suffix argument");
975
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
976

1✔
977
  str = "a";
1✔
978
  units = "";
1✔
979
  res = pr_str_get_nbytes(str, units, NULL);
1✔
980
  ck_assert_msg(res == -1, "Failed to handle invalid str argument");
981
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
982

1✔
983
  str = "1 1";
1✔
984
  units = "";
1✔
985
  res = pr_str_get_nbytes(str, units, NULL);
1✔
986
  ck_assert_msg(res == -1, "Failed to handle invalid str argument");
987
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
988

1✔
989
  str = "1.1";
1✔
990
  units = "";
1✔
991
  res = pr_str_get_nbytes(str, units, NULL);
1✔
992
  ck_assert_msg(res == -1, "Failed to handle invalid str argument");
993
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
994

1✔
995
  str = "-1";
1✔
996
  units = "";
1✔
997
  res = pr_str_get_nbytes(str, units, NULL);
1✔
998
  ck_assert_msg(res == -1, "Failed to handle invalid str argument");
999
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1000

1001
  /* XXX Test good suffix: B, KB, MB, GB, TB */
1✔
1002

1✔
1003
  str = "1";
1✔
1004
  units = "";
1✔
1005
  res = pr_str_get_nbytes(str, units, &nbytes);
1✔
1006
  ck_assert_msg(res == 0, "Expected result 0, got %d: %s", res, strerror(errno));
1007
  ck_assert_msg(nbytes == 1, "Expected nbytes = 1, got %" PR_LU,
1008
    (pr_off_t) nbytes);
1✔
1009

1✔
1010
  str = "1";
1✔
1011
  units = "B";
1✔
1012
  res = pr_str_get_nbytes(str, units, &nbytes);
1✔
1013
  ck_assert_msg(res == 0, "Expected result 0, got %d: %s", res, strerror(errno));
1014
  ck_assert_msg(nbytes == 1,
1015
    "Expected nbytes = 1, got %" PR_LU, (pr_off_t) nbytes);
1✔
1016

1✔
1017
  str = "1";
1✔
1018
  units = "KB";
1✔
1019
  res = pr_str_get_nbytes(str, units, &nbytes);
1✔
1020
  ck_assert_msg(res == 0, "Expected result 0, got %d: %s", res, strerror(errno));
1021
  ck_assert_msg(nbytes == 1024UL,
1022
    "Expected nbytes = 1024, got %" PR_LU, (pr_off_t) nbytes);
1✔
1023

1✔
1024
  str = "1";
1✔
1025
  units = "MB";
1✔
1026
  res = pr_str_get_nbytes(str, units, &nbytes);
1✔
1027
  ck_assert_msg(res == 0, "Expected result 0, got %d: %s", res, strerror(errno));
1028
  ck_assert_msg(nbytes == 1048576UL,
1029
    "Expected nbytes = 1048576, got %" PR_LU, (pr_off_t) nbytes);
1✔
1030

1✔
1031
  str = "1";
1✔
1032
  units = "GB";
1✔
1033
  res = pr_str_get_nbytes(str, units, &nbytes);
1✔
1034
  ck_assert_msg(res == 0, "Expected result 0, got %d: %s", res, strerror(errno));
1035
  ck_assert_msg(nbytes == 1073741824UL,
1036
    "Expected nbytes = 1073741824, got %" PR_LU, (pr_off_t) nbytes);
1✔
1037

1✔
1038
  str = "1";
1✔
1039
  units = "TB";
1✔
1040
  res = pr_str_get_nbytes(str, units, &nbytes);
1✔
1041
  ck_assert_msg(res == 0, "Expected result 0, got %d: %s", res, strerror(errno));
1042
  ck_assert_msg(nbytes == 1099511627776UL,
1043
    "Expected nbytes = 1099511627776, got %" PR_LU, (pr_off_t) nbytes);
1044

1✔
1045
  /* This should definitely trigger the ERANGE error. */
1✔
1046
  str = "1099511627776";
1✔
1047
  units = "TB";
1✔
1048
  res = pr_str_get_nbytes(str, units, &nbytes);
1✔
1049
  ck_assert_msg(res == -1, "Expected ERANGE failure, succeeded unexpectedly");
1050
  ck_assert_msg(errno == ERANGE, "Expected %s [%d], got %s [%d]",
1✔
1051
    strerror(ERANGE), ERANGE, strerror(errno), errno);
1052
}
1053
END_TEST
1✔
1054

1✔
1055
START_TEST (get_duration_test) {
1✔
1056
  char *str;
1✔
1057
  int duration, expected;
1058
  int res;
1✔
1059

1✔
1060
  res = pr_str_get_duration(NULL, NULL);
1✔
1061
  ck_assert_msg(res == -1, "Failed to handle null arguments");
1062
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
1063

1✔
1064
  str = "";
1✔
1065
  res = pr_str_get_duration(str, NULL);
1066
  ck_assert_msg(res == -1,
1✔
1067
    "Failed to handle badly formatted string '%s'", str);
1068
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
1069

1✔
1070
  str = "-1:-1:-1";
1✔
1071
  res = pr_str_get_duration(str, NULL);
1072
  ck_assert_msg(res == -1,
1✔
1073
    "Failed to handle badly formatted string '%s'", str);
1074
  ck_assert_msg(errno == ERANGE, "Failed to set errno to ERANGE");
1✔
1075

1✔
1076
  str = "a:b:c";
1✔
1077
  res = pr_str_get_duration(str, NULL);
1078
  ck_assert_msg(res == -1,
1✔
1079
    "Failed to handle badly formatted string '%s'", str);
1080
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
1081

1✔
1082
  str = "111:222:333";
1✔
1083
  res = pr_str_get_duration(str, NULL);
1084
  ck_assert_msg(res == -1,
1✔
1085
    "Failed to handle badly formatted string '%s'", str);
1086
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1087

1088
  /* Test well-formatted hh::mm::ss strings. */
1✔
1089

1✔
1090
  str = "00:00:00";
1✔
1091
  expected = 0;
1✔
1092
  res = pr_str_get_duration(str, &duration);
1093
  ck_assert_msg(res == 0,
1✔
1094
    "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1095
  ck_assert_msg(duration == expected,
1096
    "Expected duration %d secs, got %d", expected, duration);
1✔
1097

1✔
1098
  str = "01:02:03";
1✔
1099
  expected = 3723;
1✔
1100
  res = pr_str_get_duration(str, &duration);
1101
  ck_assert_msg(res == 0,
1✔
1102
    "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1103
  ck_assert_msg(duration == expected,
1104
    "Expected duration %d secs, got %d", expected, duration);
1✔
1105

1✔
1106
  str = "99:99:99";
1✔
1107
  expected = 362439;
1✔
1108
  res = pr_str_get_duration(str, &duration);
1109
  ck_assert_msg(res == 0,
1✔
1110
    "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1111
  ck_assert_msg(duration == expected,
1112
    "Expected duration %d secs, got %d", expected, duration);
1113

1114
  /* Test bad suffixes: -1h, -1hr, 9999foo, etc */
1✔
1115

1✔
1116
  str = "-1h";
1✔
1117
  res = pr_str_get_duration(str, NULL);
1118
  ck_assert_msg(res == -1,
1✔
1119
    "Failed to handle badly formatted suffix string '%s'", str);
1120
  ck_assert_msg(errno == ERANGE, "Failed to set errno to ERANGE");
1✔
1121

1✔
1122
  str = "-1hr";
1✔
1123
  res = pr_str_get_duration(str, NULL);
1124
  ck_assert_msg(res == -1,
1✔
1125
    "Failed to handle badly formatted suffix string '%s'", str);
1126
  ck_assert_msg(errno == ERANGE, "Failed to set errno to ERANGE");
1✔
1127

1✔
1128
  str = "99foo";
1✔
1129
  res = pr_str_get_duration(str, NULL);
1130
  ck_assert_msg(res == -1,
1✔
1131
    "Failed to handle badly formatted suffix string '%s'", str);
1132
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
1133

1✔
1134
  str = "foo";
1✔
1135
  res = pr_str_get_duration(str, NULL);
1136
  ck_assert_msg(res == -1,
1✔
1137
    "Failed to handle badly formatted suffix string '%s'", str);
1138
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1139

1140
  /* Test good suffices: "H"/"h"/"hr", "M"/"m"/"min", "S"/"s"/"sec" */
1✔
1141

1✔
1142
  str = "76H";
1✔
1143
  expected = 273600;
1✔
1144
  res = pr_str_get_duration(str, &duration);
1145
  ck_assert_msg(res == 0,
1✔
1146
    "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1147
  ck_assert_msg(duration == expected,
1148
    "Expected duration %d secs, got %d", expected, duration);
1✔
1149

1✔
1150
  str = "76h";
1✔
1151
  expected = 273600;
1✔
1152
  res = pr_str_get_duration(str, &duration);
1153
  ck_assert_msg(res == 0,
1✔
1154
    "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1155
  ck_assert_msg(duration == expected,
1156
    "Expected duration %d secs, got %d", expected, duration);
1✔
1157

1✔
1158
  str = "76Hr";
1✔
1159
  expected = 273600;
1✔
1160
  res = pr_str_get_duration(str, &duration);
1161
  ck_assert_msg(res == 0,
1✔
1162
    "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1163
  ck_assert_msg(duration == expected,
1164
    "Expected duration %d secs, got %d", expected, duration);
1✔
1165

1✔
1166
  str = "888M";
1✔
1167
  expected = 53280;
1✔
1168
  res = pr_str_get_duration(str, &duration);
1169
  ck_assert_msg(res == 0,
1✔
1170
    "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1171
  ck_assert_msg(duration == expected,
1172
    "Expected duration %d secs, got %d", expected, duration);
1✔
1173

1✔
1174
  str = "888m";
1✔
1175
  expected = 53280;
1✔
1176
  res = pr_str_get_duration(str, &duration);
1177
  ck_assert_msg(res == 0,
1✔
1178
    "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1179
  ck_assert_msg(duration == expected,
1180
    "Expected duration %d secs, got %d", expected, duration);
1✔
1181

1✔
1182
  str = "888MiN";
1✔
1183
  expected = 53280;
1✔
1184
  res = pr_str_get_duration(str, &duration);
1185
  ck_assert_msg(res == 0,
1✔
1186
    "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1187
  ck_assert_msg(duration == expected,
1188
    "Expected duration %d secs, got %d", expected, duration);
1✔
1189

1✔
1190
  str = "999S";
1✔
1191
  expected = 999;
1✔
1192
  res = pr_str_get_duration(str, &duration);
1193
  ck_assert_msg(res == 0,
1✔
1194
    "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1195
  ck_assert_msg(duration == expected,
1196
    "Expected duration %d secs, got %d", expected, duration);
1✔
1197

1✔
1198
  str = "999s";
1✔
1199
  expected = 999;
1✔
1200
  res = pr_str_get_duration(str, &duration);
1201
  ck_assert_msg(res == 0,
1✔
1202
    "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1203
  ck_assert_msg(duration == expected,
1204
    "Expected duration %d secs, got %d", expected, duration);
1✔
1205

1✔
1206
  str = "999sEc";
1✔
1207
  expected = 999;
1✔
1208
  res = pr_str_get_duration(str, &duration);
1209
  ck_assert_msg(res == 0,
1✔
1210
    "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1211
  ck_assert_msg(duration == expected,
1212
    "Expected duration %d secs, got %d", expected, duration);
1✔
1213

1✔
1214
  str = "0h";
1✔
1215
  expected = 0;
1✔
1216
  res = pr_str_get_duration(str, &duration);
1217
  ck_assert_msg(res == 0,
1✔
1218
    "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1219
  ck_assert_msg(duration == expected,
1220
    "Expected duration %d secs, got %d", expected, duration);
1✔
1221

1✔
1222
  str = "0M";
1✔
1223
  expected = 0;
1✔
1224
  res = pr_str_get_duration(str, &duration);
1225
  ck_assert_msg(res == 0,
1✔
1226
    "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1227
  ck_assert_msg(duration == expected,
1228
    "Expected duration %d secs, got %d", expected, duration);
1✔
1229

1✔
1230
  str = "0sec";
1✔
1231
  expected = 0;
1✔
1232
  res = pr_str_get_duration(str, &duration);
1233
  ck_assert_msg(res == 0,
1✔
1234
    "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1235
  ck_assert_msg(duration == expected,
1236
    "Expected duration %d secs, got %d", expected, duration);
1✔
1237

1✔
1238
  str = "17";
1✔
1239
  expected = 17;
1✔
1240
  res = pr_str_get_duration(str, &duration);
1241
  ck_assert_msg(res == 0,
1✔
1242
    "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1243
  ck_assert_msg(duration == expected,
1244
    "Expected duration %d secs, got %d", expected, duration);
1✔
1245

1✔
1246
  str = "-1";
1✔
1247
  res = pr_str_get_duration(str, NULL);
1248
  ck_assert_msg(res == -1,
1✔
1249
    "Failed to handle badly formatted suffix string '%s'", str);
1✔
1250
  ck_assert_msg(errno == ERANGE, "Failed to set errno to ERANGE");
1251
}
1252
END_TEST
1✔
1253

1✔
1254
START_TEST (strnrstr_test) {
1✔
1255
  int res, flags = 0;
1256
  const char *s = NULL, *suffix = NULL;
1✔
1257

1✔
1258
  res = pr_strnrstr(NULL, 0, NULL, 0, flags);
1✔
1259
  ck_assert_msg(res == -1, "Failed to handle null arguments");
1260
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
1261

1✔
1262
  res = pr_strnrstr(NULL, 0, "", 0, flags);
1✔
1263
  ck_assert_msg(res == -1, "Failed to handle null s");
1264
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
1265

1✔
1266
  res = pr_strnrstr("", 0, NULL, 0, flags);
1✔
1267
  ck_assert_msg(res == -1, "Failed to handle null suffix");
1268
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
1269

1✔
1270
  s = suffix = "";
1✔
1271
  res = pr_strnrstr(s, 0, suffix, 0, flags);
1272
  ck_assert_msg(res == TRUE, "Expected true, got false");
1✔
1273

1✔
1274
  s = "";
1✔
1275
  suffix = "s";
1✔
1276
  res = pr_strnrstr(s, 0, suffix, 0, flags);
1277
  ck_assert_msg(res == FALSE, "Expected false, got true");
1✔
1278

1✔
1279
  s = "food";
1✔
1280
  suffix = "ood";
1✔
1281
  res = pr_strnrstr(s, 0, suffix, 0, flags);
1282
  ck_assert_msg(res == TRUE, "Expected true, got false");
1✔
1283

1✔
1284
  s = "food";
1✔
1285
  suffix = "ood";
1✔
1286
  res = pr_strnrstr(s, 4, suffix, 3, flags);
1287
  ck_assert_msg(res == TRUE, "Expected true, got false");
1✔
1288

1✔
1289
  s = "FOOD";
1✔
1290
  suffix = "ood";
1✔
1291
  res = pr_strnrstr(s, 4, suffix, 3, flags);
1292
  ck_assert_msg(res == FALSE, "Expected false, got true");
1✔
1293

1✔
1294
  flags = PR_STR_FL_IGNORE_CASE;
1✔
1295
  s = "FOOD";
1✔
1296
  suffix = "ood";
1✔
1297
  res = pr_strnrstr(s, 4, suffix, 3, flags);
1✔
1298
  ck_assert_msg(res == TRUE, "Expected true, got false");
1299
}
1300
END_TEST
1✔
1301

1✔
1302
START_TEST (bin2hex_test) {
1✔
1303
  char *expected, *res;
1304
  const unsigned char *str;
1✔
1305

1✔
1306
  res = pr_str_bin2hex(NULL, NULL, 0, 0);
1✔
1307
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
1308
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1309
    strerror(errno), errno);
1✔
1310

1✔
1311
  res = pr_str_bin2hex(p, NULL, 0, 0);
1✔
1312
  ck_assert_msg(res == NULL, "Failed to handle null data argument");
1313
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1314
    strerror(errno), errno);
1315

1✔
1316
  /* Empty string. */
1✔
1317
  str = (const unsigned char *) "foobar";
1✔
1318
  expected = "";
1✔
1319
  res = pr_str_bin2hex(p, (const unsigned char *) str, 0, 0);
1✔
1320
  ck_assert_msg(res != NULL, "Failed to hexify '%s': %s", str, strerror(errno));
1321
  ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'",
1322
    expected, res);
1323

1✔
1324
  /* default (lowercase) */
1✔
1325
  expected = "666f6f626172";
1✔
1326
  res = pr_str_bin2hex(p, str, strlen((char *) str), 0);
1✔
1327
  ck_assert_msg(res != NULL, "Failed to hexify '%s': %s", str, strerror(errno));
1328
  ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'",
1329
    expected, res);
1330

1✔
1331
  /* lowercase */
1✔
1332
  expected = "666f6f626172";
1✔
1333
  res = pr_str_bin2hex(p, str, strlen((char *) str), 0);
1✔
1334
  ck_assert_msg(res != NULL, "Failed to hexify '%s': %s", str, strerror(errno));
1335
  ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'",
1336
    expected, res);
1337

1✔
1338
  /* uppercase */
1✔
1339
  expected = "666F6F626172";
1✔
1340
  res = pr_str_bin2hex(p, str, strlen((char *) str), PR_STR_FL_HEX_USE_UC);
1✔
1341
  ck_assert_msg(res != NULL, "Failed to hexify '%s': %s", str, strerror(errno));
1342
  ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'",
1✔
1343
    expected, res);
1344
}
1345
END_TEST
1✔
1346

1✔
1347
START_TEST (hex2bin_test) {
1✔
1348
  unsigned char *expected, *res;
1✔
1349
  const unsigned char *hex;
1350
  size_t expected_len, hex_len, len;
1✔
1351

1✔
1352
  res = pr_str_hex2bin(NULL, NULL, 0, NULL);
1✔
1353
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
1354
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1355
    strerror(errno), errno);
1✔
1356

1✔
1357
  res = pr_str_hex2bin(p, NULL, 0, 0);
1✔
1358
  ck_assert_msg(res == NULL, "Failed to handle null data argument");
1359
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1360
    strerror(errno), errno);
1361

1✔
1362
  /* Empty string. */
1✔
1363
  hex = (const unsigned char *) "";
1✔
1364
  hex_len = strlen((char *) hex);
1✔
1365
  expected = (unsigned char *) "";
1✔
1366
  res = pr_str_hex2bin(p, hex, hex_len, &len);
1✔
1367
  ck_assert_msg(res != NULL, "Failed to unhexify '%s': %s", hex, strerror(errno));
1368
  ck_assert_msg(strcmp((const char *) res, (const char *) expected) == 0,
1369
    "Expected '%s', got '%s'", expected, res);
1✔
1370

1✔
1371
  hex = (const unsigned char *) "112233";
1✔
1372
  hex_len = strlen((char *) hex);
1✔
1373
  expected_len = 3;
1✔
1374
  expected = palloc(p, expected_len);
1✔
1375
  expected[0] = 17;
1✔
1376
  expected[1] = 34;
1377
  expected[2] = 51;
1✔
1378

1✔
1379
  res = pr_str_hex2bin(p, (const unsigned char *) hex, hex_len, &len);
1✔
1380
  ck_assert_msg(res != NULL, "Failed to unhexify '%s': %s", hex, strerror(errno));
1381
  ck_assert_msg(len == expected_len, "Expected len %lu, got %lu",
1✔
1382
    (unsigned long) expected_len, (unsigned long) len);
1383
  ck_assert_msg(memcmp(res, expected, len) == 0,
1384
    "Did not receive expected unhexified data");
1385

1✔
1386
  /* lowercase */
1✔
1387
  hex = (const unsigned char *) "666f6f626172";
1✔
1388
  hex_len = strlen((char *) hex);
1✔
1389
  expected_len = 6;
1✔
1390
  expected = palloc(p, expected_len);
1✔
1391
  expected[0] = 'f';
1✔
1392
  expected[1] = 'o';
1✔
1393
  expected[2] = 'o';
1✔
1394
  expected[3] = 'b';
1✔
1395
  expected[4] = 'a';
1396
  expected[5] = 'r';
1✔
1397

1✔
1398
  res = pr_str_hex2bin(p, (const unsigned char *) hex, hex_len, &len);
1✔
1399
  ck_assert_msg(res != NULL, "Failed to unhexify '%s': %s", hex, strerror(errno));
1400
  ck_assert_msg(len == expected_len, "Expected len %lu, got %lu",
1✔
1401
    (unsigned long) expected_len, (unsigned long) len);
1402
  ck_assert_msg(memcmp(res, expected, len) == 0,
1403
    "Did not receive expected unhexified data");
1404

1✔
1405
  /* uppercase */
1✔
1406
  hex = (const unsigned char *) "666F6F626172";
1407
  hex_len = strlen((char *) hex);
1✔
1408

1✔
1409
  res = pr_str_hex2bin(p, (const unsigned char *) hex, hex_len, &len);
1✔
1410
  ck_assert_msg(res != NULL, "Failed to unhexify '%s': %s", hex, strerror(errno));
1411
  ck_assert_msg(len == expected_len, "Expected len %lu, got %lu",
1✔
1412
    (unsigned long) expected_len, (unsigned long) len);
1413
  ck_assert_msg(memcmp(res, expected, len) == 0,
1414
    "Did not receive expected unhexified data");
1415

1✔
1416
  /* Handle known not-hex data properly. */
1✔
1417
  hex = (const unsigned char *) "Hello, World!\n";
1✔
1418
  hex_len = strlen((char *) hex);
1✔
1419
  res = pr_str_hex2bin(p, hex, hex_len, &len);
1✔
1420
  ck_assert_msg(res == NULL, "Successfully unhexified '%s' unexpectedly", hex);
1421
  ck_assert_msg(errno == ERANGE, "Expected ERANGE (%d), got %s (%d)", ERANGE,
1✔
1422
    strerror(errno), errno);
1423
}
1424
END_TEST
1✔
1425

1✔
1426
START_TEST (levenshtein_test) {
1✔
1427
  int res, expected, flags = 0;
1428
  const char *a, *b;
1✔
1429

1✔
1430
  mark_point();
1✔
1431
  res = pr_str_levenshtein(NULL, NULL, NULL, 0, 0, 0, 0, flags);
1✔
1432
  ck_assert_msg(res < 0, "Failed to handle null pool");
1433
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1434
    strerror(errno), errno);
1✔
1435

1✔
1436
  mark_point();
1✔
1437
  res = pr_str_levenshtein(p, NULL, NULL, 0, 0, 0, 0, flags);
1✔
1438
  ck_assert_msg(res < 0, "Failed to handle null a string");
1439
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1440
    strerror(errno), errno);
1✔
1441

1442
  a = "foo";
1✔
1443

1✔
1444
  mark_point();
1✔
1445
  res = pr_str_levenshtein(p, a, NULL, 0, 0, 0, 0, flags);
1✔
1446
  ck_assert_msg(res < 0, "Failed to handle null b string");
1447
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1448
    strerror(errno), errno);
1✔
1449

1✔
1450
  expected = 0;
1451
  b = "Foo";
1✔
1452

1✔
1453
  mark_point();
1✔
1454
  res = pr_str_levenshtein(p, a, b, 0, 0, 0, 0, flags);
1455
  ck_assert_msg(res >= 0,
1456
    "Failed to compute Levenshtein distance from '%s' to '%s': %s", a, b,
1✔
1457
    strerror(errno));
1458
  ck_assert_msg(expected == res, "Expected distance %d, got %d", expected, res);
1✔
1459

1✔
1460
  expected = 3;
1✔
1461
  b = "Foo";
1✔
1462
  res = pr_str_levenshtein(p, a, b, 0, 1, 1, 1, flags);
1463
  ck_assert_msg(res >= 0,
1464
    "Failed to compute Levenshtein distance from '%s' to '%s': %s", a, b,
1✔
1465
    strerror(errno));
1466
  ck_assert_msg(expected == res, "Expected distance %d, got %d", expected, res);
1✔
1467

1✔
1468
  flags = PR_STR_FL_IGNORE_CASE;
1✔
1469
  expected = 2;
1✔
1470
  b = "Foo";
1✔
1471
  res = pr_str_levenshtein(p, a, b, 0, 1, 1, 1, flags);
1472
  ck_assert_msg(res >= 0,
1473
    "Failed to compute Levenshtein distance from '%s' to '%s': %s", a, b,
1✔
1474
    strerror(errno));
1✔
1475
  ck_assert_msg(expected == res, "Expected distance %d, got %d", expected, res);
1476
}
1477
END_TEST
1✔
1478

1✔
1479
START_TEST (similars_test) {
1✔
1480
  array_header *res, *candidates;
1481
  const char *s, **similars, *expected;
1✔
1482

1✔
1483
  mark_point();
1✔
1484
  res = pr_str_get_similars(NULL, NULL, NULL, 0, 0);
1✔
1485
  ck_assert_msg(res == NULL, "Failed to handle null pool");
1486
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1487
    strerror(errno), errno);
1✔
1488

1✔
1489
  mark_point();
1✔
1490
  res = pr_str_get_similars(p, NULL, NULL, 0, 0);
1✔
1491
  ck_assert_msg(res == NULL, "Failed to handle null string");
1492
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1493
    strerror(errno), errno);
1✔
1494

1495
  s = "foo";
1✔
1496

1✔
1497
  mark_point();
1✔
1498
  res = pr_str_get_similars(p, s, NULL, 0, 0);
1✔
1499
  ck_assert_msg(res == NULL, "Failed to handle null candidates");
1500
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1501
    strerror(errno), errno);
1✔
1502

1503
  candidates = make_array(p, 5, sizeof(const char *));
1✔
1504

1✔
1505
  mark_point();
1✔
1506
  res = pr_str_get_similars(p, s, candidates, 0, 0);
1✔
1507
  ck_assert_msg(res == NULL, "Failed to handle empty candidates");
1508
  ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
1509
    strerror(errno), errno);
1✔
1510

1✔
1511
  *((const char **) push_array(candidates)) = pstrdup(p, "fools");
1✔
1512
  *((const char **) push_array(candidates)) = pstrdup(p, "odd");
1✔
1513
  *((const char **) push_array(candidates)) = pstrdup(p, "bar");
1514
  *((const char **) push_array(candidates)) = pstrdup(p, "FOO");
1✔
1515

1✔
1516
  mark_point();
1✔
1517
  res = pr_str_get_similars(p, s, candidates, 0, 0);
1518
  ck_assert_msg(res != NULL, "Failed to find similar strings to '%s': %s", s,
1✔
1519
    strerror(errno));
1520
  ck_assert_msg(res->nelts > 0, "Expected >0 similar strings, got %u",
1521
    res->nelts);
1✔
1522

1✔
1523
  mark_point();
1524
  similars = (const char **) res->elts;
1525

1526
  /*
1527
   * Note: expected distances are as follows:
1528
   *
1529
   * Candidate       Case-Sensitive      Case-Insensitive
1530
   * fools                 0                     0
1531
   * odd                   5                     5
1532
   * bar                   5                     5
1533
   * FOO                   5                     0
1534
   */
1✔
1535

1536
  expected = "fools";
1✔
1537

1538
  ck_assert_msg(strcmp(similars[0], expected) == 0,
1539
    "Expected similar '%s', got '%s'", expected, similars[0]);
1✔
1540

1541
  ck_assert_msg(strcmp(similars[1], expected) != 0,
1542
    "Unexpectedly got similar '%s'", similars[1]);
1✔
1543

1✔
1544
  mark_point();
1✔
1545
  res = pr_str_get_similars(p, s, candidates, 0, PR_STR_FL_IGNORE_CASE);
1546
  ck_assert_msg(res != NULL, "Failed to find similar strings to '%s': %s", s,
1✔
1547
    strerror(errno));
1548
  ck_assert_msg(res->nelts > 0, "Expected >0 similar strings, got %u",
1549
    res->nelts);
1✔
1550

1✔
1551
  mark_point();
1552
  similars = (const char **) res->elts;
1553

1554
  /*
1555
   * similars[0] and similars[1] should be "FOO" and "fools", but
1556
   * not necessarily in that order
1✔
1557
   */
1✔
1558
  expected = "FOO";
×
1559
  if (strcmp(similars[0], expected) != 0) {
×
1560
    expected = similars[0];
×
1561
    similars[0] = similars[1];
×
1562
    similars[1] = expected;
1563
    expected = "FOO";
1564
  }
1✔
1565

1566
  ck_assert_msg(strcmp(similars[0], expected) == 0,
1567
    "Expected similar '%s', got '%s'", expected, similars[0]);
1✔
1568

1569
  expected = "fools";
1✔
1570

1571
  ck_assert_msg(strcmp(similars[1], expected) == 0,
1✔
1572
    "Expected similar '%s', got '%s'", expected, similars[1]);
1573
}
1574
END_TEST
1✔
1575

1✔
1576
START_TEST (str2uid_test) {
1577
  int res;
1✔
1578

1✔
1579
  res = pr_str2uid(NULL, NULL);
1✔
1580
  ck_assert_msg(res == -1, "Failed to handle null arguments");
1581
}
1582
END_TEST
1✔
1583

1✔
1584
START_TEST (str2gid_test) {
1585
  int res;
1✔
1586

1✔
1587
  res = pr_str2gid(NULL, NULL);
1✔
1588
  ck_assert_msg(res == -1, "Failed to handle null arguments");
1589
}
1590
END_TEST
1✔
1591

1✔
1592
START_TEST (uid2str_test) {
1593
  const char *res;
1✔
1594

1✔
1595
  res = pr_uid2str(NULL, (uid_t) 1);
1596
  ck_assert_msg(strcmp(res, "1") == 0, "Expected '1', got '%s'", res);
1✔
1597

1✔
1598
  res = pr_uid2str(NULL, (uid_t) -1);
1✔
1599
  ck_assert_msg(strcmp(res, "-1") == 0, "Expected '-1', got '%s'", res);
1600
}
1601
END_TEST
1✔
1602

1✔
1603
START_TEST (gid2str_test) {
1604
  const char *res;
1✔
1605

1✔
1606
  res = pr_gid2str(NULL, (gid_t) 1);
1607
  ck_assert_msg(strcmp(res, "1") == 0, "Expected '1', got '%s'", res);
1✔
1608

1✔
1609
  res = pr_gid2str(NULL, (gid_t) -1);
1✔
1610
  ck_assert_msg(strcmp(res, "-1") == 0, "Expected '-1', got '%s'", res);
1611
}
1612
END_TEST
1✔
1613

1✔
1614
START_TEST (str_quote_test) {
1✔
1615
  const char *res;
1616
  char *expected, *path;
1✔
1617

1✔
1618
  res = pr_str_quote(NULL, NULL);
1✔
1619
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
1620
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1621
    strerror(errno), errno);
1✔
1622

1✔
1623
  res = pr_str_quote(p, NULL);
1✔
1624
  ck_assert_msg(res == NULL, "Failed to handle null path argument");
1625
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1626
    strerror(errno), errno);
1✔
1627

1✔
1628
  path = "/tmp/";
1✔
1629
  expected = path;
1✔
1630
  res = pr_str_quote(p, path);
1✔
1631
  ck_assert_msg(res != NULL, "Failed to quote '%s': %s", path, strerror(errno));
1632
  ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1633
    res);
1✔
1634

1✔
1635
  path = "/\"tmp\"/";
1✔
1636
  expected = "/\"\"tmp\"\"/";
1✔
1637
  res = pr_str_quote(p, path);
1✔
1638
  ck_assert_msg(res != NULL, "Failed to quote '%s': %s", path, strerror(errno));
1639
  ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1✔
1640
    res);
1641
}
1642
END_TEST
1✔
1643

1✔
1644
START_TEST (quote_dir_test) {
1✔
1645
  const char *res;
1646
  char *expected, *path;
1✔
1647

1✔
1648
  res = quote_dir(NULL, NULL);
1✔
1649
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
1650
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1651
    strerror(errno), errno);
1✔
1652

1✔
1653
  res = quote_dir(p, NULL);
1✔
1654
  ck_assert_msg(res == NULL, "Failed to handle null path argument");
1655
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1656
    strerror(errno), errno);
1✔
1657

1✔
1658
  path = "/tmp/";
1✔
1659
  expected = path;
1✔
1660
  res = quote_dir(p, path);
1✔
1661
  ck_assert_msg(res != NULL, "Failed to quote '%s': %s", path, strerror(errno));
1662
  ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1663
    res);
1✔
1664

1✔
1665
  path = "/\"tmp\"/";
1✔
1666
  expected = "/\"\"tmp\"\"/";
1✔
1667
  res = quote_dir(p, path);
1✔
1668
  ck_assert_msg(res != NULL, "Failed to quote '%s': %s", path, strerror(errno));
1669
  ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1✔
1670
    res);
1671
}
1672
END_TEST
1✔
1673

1✔
1674
START_TEST (text_to_array_test) {
1✔
1675
  register unsigned int i;
1✔
1676
  array_header *res;
1677
  const char *text, *elt;
1✔
1678

1✔
1679
  mark_point();
1✔
1680
  res = pr_str_text_to_array(NULL, NULL, ',');
1✔
1681
  ck_assert_msg(res == NULL, "Failed to handle null pool");
1682
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1683
    strerror(errno), errno);
1✔
1684

1✔
1685
  mark_point();
1✔
1686
  res = pr_str_text_to_array(p, NULL, ',');
1✔
1687
  ck_assert_msg(res == NULL, "Failed to handle null text");
1688
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1689
    strerror(errno), errno);
1✔
1690

1✔
1691
  mark_point();
1✔
1692
  text = "";
1✔
1693
  res = pr_str_text_to_array(p, text, ',');
1694
  ck_assert_msg(res != NULL, "Failed to handle text '%s': %s", text,
1✔
1695
    strerror(errno));
1696
  ck_assert_msg(res->nelts == 0, "Expected 0 items, got %u", res->nelts);
1✔
1697

1✔
1698
  mark_point();
1✔
1699
  text = ",";
1✔
1700
  res = pr_str_text_to_array(p, text, ',');
1701
  ck_assert_msg(res != NULL, "Failed to handle text '%s': %s", text,
1✔
1702
    strerror(errno));
1703
  ck_assert_msg(res->nelts == 0, "Expected 0 items, got %u", res->nelts);
1✔
1704

1✔
1705
  mark_point();
1✔
1706
  text = ",,,";
1✔
1707
  res = pr_str_text_to_array(p, text, ',');
1708
  ck_assert_msg(res != NULL, "Failed to handle text '%s': %s", text,
1✔
1709
    strerror(errno));
1710
  ck_assert_msg(res->nelts == 0, "Expected 0 items, got %u", res->nelts);
1✔
1711

1✔
1712
  mark_point();
1✔
1713
  text = "foo";
1✔
1714
  res = pr_str_text_to_array(p, text, ',');
1715
  ck_assert_msg(res != NULL, "Failed to handle text '%s': %s", text,
1✔
1716
    strerror(errno));
1✔
1717
  ck_assert_msg(res->nelts == 1, "Expected 1 item, got %u", res->nelts);
1✔
1718
  elt = ((char **) res->elts)[0];
1✔
1719
  ck_assert_msg(elt != NULL, "Expected non-null item");
1720
  ck_assert_msg(strcmp(elt, text) == 0, "Expected '%s', got '%s'", text, elt);
1✔
1721

1✔
1722
  mark_point();
1✔
1723
  text = "foo,foo,foo";
1✔
1724
  res = pr_str_text_to_array(p, text, ',');
1725
  ck_assert_msg(res != NULL, "Failed to handle text '%s': %s", text,
1✔
1726
    strerror(errno));
5✔
1727
  ck_assert_msg(res->nelts == 3, "Expected 3 items, got %u", res->nelts);
3✔
1728
  for (i = 0; i < res->nelts; i++) {
1729
    char *item, *expected;
3✔
1730

3✔
1731
    item = ((char **) res->elts)[i];
1732
    ck_assert_msg(item != NULL, "Expected item at index %u, got null", i);
3✔
1733

3✔
1734
    expected = "foo";
1735
    ck_assert_msg(strcmp(item, expected) == 0,
1736
      "Expected '%s' at index %u, got '%s'", expected, i, item);
1737
  }
1✔
1738

1✔
1739
  mark_point();
1✔
1740
  text = "foo,foo,foo,";
1✔
1741
  res = pr_str_text_to_array(p, text, ',');
1742
  ck_assert_msg(res != NULL, "Failed to handle text '%s': %s", text,
1✔
1743
    strerror(errno));
5✔
1744
  ck_assert_msg(res->nelts == 3, "Expected 3 items, got %u", res->nelts);
3✔
1745
  for (i = 0; i < res->nelts; i++) {
1746
    char *item, *expected;
3✔
1747

3✔
1748
    item = ((char **) res->elts)[i];
1749
    ck_assert_msg(item != NULL, "Expected item at index %u, got null", i);
3✔
1750

3✔
1751
    expected = "foo";
1752
    ck_assert_msg(strcmp(item, expected) == 0,
1753
      "Expected '%s' at index %u, got '%s'", expected, i, item);
1754
  }
1✔
1755

1✔
1756
  mark_point();
1✔
1757
  text = "foo|foo|foo";
1✔
1758
  res = pr_str_text_to_array(p, text, '|');
1759
  ck_assert_msg(res != NULL, "Failed to handle text '%s': %s", text,
1✔
1760
    strerror(errno));
5✔
1761
  ck_assert_msg(res->nelts == 3, "Expected 3 items, got %u", res->nelts);
3✔
1762
  for (i = 0; i < res->nelts; i++) {
1763
    char *item, *expected;
3✔
1764

3✔
1765
    item = ((char **) res->elts)[i];
1766
    ck_assert_msg(item != NULL, "Expected item at index %u, got null", i);
3✔
1767

3✔
1768
    expected = "foo";
1769
    ck_assert_msg(strcmp(item, expected) == 0,
1770
      "Expected '%s' at index %u, got '%s'", expected, i, item);
1771
  }
1772

1✔
1773
  /* With a leading delimiter character. */
1✔
1774
  mark_point();
1✔
1775
  text = "/foo/foo/foo";
1✔
1776
  res = pr_str_text_to_array(p, text, '/');
1777
  ck_assert_msg(res != NULL, "Failed to handle text '%s': %s", text,
1✔
1778
    strerror(errno));
5✔
1779
  ck_assert_msg(res->nelts == 3, "Expected 3 items, got %u", res->nelts);
3✔
1780
  for (i = 0; i < res->nelts; i++) {
1781
    char *item, *expected;
3✔
1782

3✔
1783
    item = ((char **) res->elts)[i];
1784
    ck_assert_msg(item != NULL, "Expected item at index %u, got null", i);
3✔
1785

3✔
1786
    expected = "foo";
1787
    ck_assert_msg(strcmp(item, expected) == 0,
1788
      "Expected '%s' at index %u, got '%s'", expected, i, item);
1789
  }
1790

1✔
1791
  /* With a trailing delimiter character. */
1✔
1792
  mark_point();
1✔
1793
  text = "/foo/foo/foo/";
1✔
1794
  res = pr_str_text_to_array(p, text, '/');
1795
  ck_assert_msg(res != NULL, "Failed to handle text '%s': %s", text,
1✔
1796
    strerror(errno));
5✔
1797
  ck_assert_msg(res->nelts == 3, "Expected 3 items, got %u", res->nelts);
3✔
1798
  for (i = 0; i < res->nelts; i++) {
1799
    char *item, *expected;
3✔
1800

3✔
1801
    item = ((char **) res->elts)[i];
1802
    ck_assert_msg(item != NULL, "Expected item at index %u, got null", i);
3✔
1803

3✔
1804
    expected = "foo";
1805
    ck_assert_msg(strcmp(item, expected) == 0,
1806
      "Expected '%s' at index %u, got '%s'", expected, i, item);
1807
  }
1808

1✔
1809
  /* Without leading or trailing delimiter characters. */
1✔
1810
  mark_point();
1✔
1811
  text = "foo/foo/foo";
1✔
1812
  res = pr_str_text_to_array(p, text, '/');
1813
  ck_assert_msg(res != NULL, "Failed to handle text '%s': %s", text,
1✔
1814
    strerror(errno));
5✔
1815
  ck_assert_msg(res->nelts == 3, "Expected 3 items, got %u", res->nelts);
3✔
1816
  for (i = 0; i < res->nelts; i++) {
1817
    char *item, *expected;
3✔
1818

3✔
1819
    item = ((char **) res->elts)[i];
1820
    ck_assert_msg(item != NULL, "Expected item at index %u, got null", i);
3✔
1821

3✔
1822
    expected = "foo";
1823
    ck_assert_msg(strcmp(item, expected) == 0,
1824
      "Expected '%s' at index %u, got '%s'", expected, i, item);
1✔
1825
  }
1826
}
1827
END_TEST
1✔
1828

1✔
1829
START_TEST (array_to_text_test) {
1✔
1830
  array_header *items;
1✔
1831
  const char *delimiter;
1832
  char *res, *expected;
1✔
1833

1✔
1834
  mark_point();
1✔
1835
  res = pr_str_array_to_text(NULL, NULL, NULL);
1✔
1836
  ck_assert_msg(res == NULL, "Failed to handle null pool");
1837
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1838
    strerror(errno), errno);
1✔
1839

1✔
1840
  mark_point();
1✔
1841
  res = pr_str_array_to_text(p, NULL, NULL);
1✔
1842
  ck_assert_msg(res == NULL, "Failed to handle null items");
1843
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1844
    strerror(errno), errno);
1✔
1845

1846
  items = make_array(p, 0, sizeof(char *));
1✔
1847

1✔
1848
  mark_point();
1✔
1849
  res = pr_str_array_to_text(p, items, NULL);
1✔
1850
  ck_assert_msg(res == NULL, "Failed to handle null delimiter");
1851
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1852
    strerror(errno), errno);
1✔
1853

1✔
1854
  delimiter = ":";
1855
  expected = "";
1✔
1856

1✔
1857
  mark_point();
1✔
1858
  res = pr_str_array_to_text(p, items, delimiter);
1859
  ck_assert_msg(res != NULL, "Error converting items to text: %s",
1✔
1860
    strerror(errno));
1861
  ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1862
    res);
1✔
1863

1✔
1864
  *((char **) push_array(items)) = pstrdup(p, "foo");
1865
  expected = "foo";
1✔
1866

1✔
1867
  mark_point();
1✔
1868
  res = pr_str_array_to_text(p, items, delimiter);
1869
  ck_assert_msg(res != NULL, "Error converting items to text: %s",
1✔
1870
    strerror(errno));
1871
  ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1872
    res);
1✔
1873

1✔
1874
  *((char **) push_array(items)) = pstrdup(p, "bar");
1875
  expected = "foo:bar";
1✔
1876

1✔
1877
  mark_point();
1✔
1878
  res = pr_str_array_to_text(p, items, delimiter);
1879
  ck_assert_msg(res != NULL, "Error converting items to text: %s",
1✔
1880
    strerror(errno));
1881
  ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1✔
1882
    res);
1883
}
1884
END_TEST
890✔
1885

890✔
1886
Suite *tests_get_str_suite(void) {
890✔
1887
  Suite *suite;
1888
  TCase *testcase;
890✔
1889

1890
  suite = suite_create("str");
890✔
1891

890✔
1892
  testcase = tcase_create("base");
1893
  tcase_add_checked_fixture(testcase, set_up, tear_down);
890✔
1894

890✔
1895
  tcase_add_test(testcase, sstrncpy_test);
890✔
1896
  tcase_add_test(testcase, sstrcat_test);
890✔
1897
  tcase_add_test(testcase, sreplace_test);
890✔
1898
  tcase_add_test(testcase, sreplace_enospc_test);
890✔
1899
  tcase_add_test(testcase, sreplace_bug3614_test);
890✔
1900
  tcase_add_test(testcase, str_replace_test);
890✔
1901
  tcase_add_test(testcase, pdircat_test);
890✔
1902
  tcase_add_test(testcase, pstrcat_test);
890✔
1903
  tcase_add_test(testcase, pstrdup_test);
890✔
1904
  tcase_add_test(testcase, pstrndup_test);
890✔
1905
  tcase_add_test(testcase, strip_test);
890✔
1906
  tcase_add_test(testcase, strip_end_test);
890✔
1907
  tcase_add_test(testcase, get_token_test);
890✔
1908
  tcase_add_test(testcase, get_token2_test);
890✔
1909
  tcase_add_test(testcase, get_word_test);
890✔
1910
  tcase_add_test(testcase, get_word_utf8_test);
890✔
1911
  tcase_add_test(testcase, is_boolean_test);
890✔
1912
  tcase_add_test(testcase, is_fnmatch_test);
890✔
1913
  tcase_add_test(testcase, get_nbytes_test);
890✔
1914
  tcase_add_test(testcase, get_duration_test);
890✔
1915
  tcase_add_test(testcase, bin2hex_test);
890✔
1916
  tcase_add_test(testcase, hex2bin_test);
890✔
1917
  tcase_add_test(testcase, levenshtein_test);
890✔
1918
  tcase_add_test(testcase, similars_test);
890✔
1919
  tcase_add_test(testcase, strnrstr_test);
890✔
1920
  tcase_add_test(testcase, str2uid_test);
890✔
1921
  tcase_add_test(testcase, str2gid_test);
890✔
1922
  tcase_add_test(testcase, uid2str_test);
890✔
1923
  tcase_add_test(testcase, gid2str_test);
890✔
1924
  tcase_add_test(testcase, str_quote_test);
890✔
1925
  tcase_add_test(testcase, quote_dir_test);
890✔
1926
  tcase_add_test(testcase, text_to_array_test);
1927
  tcase_add_test(testcase, array_to_text_test);
890✔
1928

890✔
1929
  suite_add_tcase(suite, testcase);
1930
  return suite;
1931
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc