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

proftpd / proftpd / 26127302613

19 May 2026 09:51PM UTC coverage: 93.024% (+0.4%) from 92.635%
26127302613

push

github

51329 of 55178 relevant lines covered (93.02%)

215.14 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
  res = sstrcat(dst, src, 0);
117
  ck_assert_msg(res == NULL, "Non-null result for zero-length strcat");
1✔
118

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

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

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

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

145
  ck_assert_msg(dst[1] == 0, "Failed to terminate destination buffer");
146

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

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

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

165
  mark_point();
166
  ck_assert_msg(dst[2] == 0, "Failed to terminate destination buffer");
1✔
167

1✔
168
  mark_point();
169
  memset(src, c, sizeof(src)-1);
1✔
170

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

1✔
178
  mark_point();
179
  res = sstrcat(dst, src, sizeof(dst));
1✔
180

1✔
181
  mark_point();
182
  ck_assert_msg(res == dst, "Returned wrong destination buffer");
1✔
183

1✔
184
  mark_point();
185
  ck_assert_msg(dst[sizeof(dst)-1] == 0,
1✔
186
    "Failed to terminate destination buffer");
1✔
187

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

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

201
START_TEST (sreplace_test) {
202
  const char *res;
1✔
203
  char *fmt = NULL, *ok;
1✔
204

1✔
205
  res = sreplace(NULL, NULL, 0);
206
  ck_assert_msg(res == NULL, "Failed to handle invalid arguments");
1✔
207
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
208

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

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

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

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

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

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

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

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

250
START_TEST (sreplace_enospc_test) {
251
  const char *res;
1✔
252
  char *fmt = NULL;
1✔
253
  size_t bufsz = 8192;
1✔
254

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

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

267
START_TEST (sreplace_bug3614_test) {
268
  const char *res;
1✔
269
  char *fmt = NULL, *ok;
1✔
270

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

284
  /* We put a limit on the maximum number of replacements that sreplace()
285
   * will perform on a given string, per Bug#3614.
286
   */
287
  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}";
288

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

328
START_TEST (str_replace_test) {
329
  const char *res;
1✔
330
  char *fmt = NULL, *ok;
1✔
331
  int max_replace = PR_STR_MAX_REPLACEMENTS;
1✔
332

1✔
333
  res = pr_str_replace(NULL, max_replace, NULL, 0);
334
  ck_assert_msg(res == NULL, "Failed to handle invalid arguments");
1✔
335
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
336

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

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

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

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

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

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

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

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

376
START_TEST (pdircat_test) {
377
  char *res, *ok;
1✔
378

1✔
379
  res = pdircat(NULL, 0, NULL);
380
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
1✔
381
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
382

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

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

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

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

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

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

418
START_TEST (pstrcat_test) {
419
  char *res, *ok;
1✔
420

1✔
421
  res = pstrcat(NULL, 0, NULL);
422
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
1✔
423
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
424

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

1✔
430
  res = pstrcat(p, "", NULL);
431
  ok = "";
1✔
432
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
433

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

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

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

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

452
START_TEST (pstrdup_test) {
453
  char *res, *ok;
1✔
454

1✔
455
  res = pstrdup(NULL, NULL);
456
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
1✔
457
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
458

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

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

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

475
START_TEST (pstrndup_test) {
476
  char *res, *ok;
1✔
477

1✔
478
  res = pstrndup(NULL, NULL, 0);
479
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
1✔
480
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
481

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

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

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

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

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

510
START_TEST (strip_test) {
511
  const char *ok, *res, *str;
1✔
512

1✔
513
  res = pr_str_strip(NULL, NULL);
514
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
1✔
515
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
516

1✔
517
  res = pr_str_strip(p, NULL);
518
  ck_assert_msg(res == NULL, "Failed to handle null str argument");
1✔
519
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
520

1✔
521
  res = pr_str_strip(NULL, "foo");
522
  ck_assert_msg(res == NULL, "Failed to handle null pool argument");
1✔
523
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
524

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

1✔
529
  ok = "foo";
530
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
531

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

1✔
536
  ok = "foo";
537
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
538

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

1✔
543
  ok = "foo";
544
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
545

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

1✔
550
  ok = "foo";
551
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
552
}
1✔
553
END_TEST
1✔
554

555
START_TEST (strip_end_test) {
556
  char *ch, *ok, *res, *str;
1✔
557

1✔
558
  res = pr_str_strip_end(NULL, NULL);
559
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
1✔
560
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
561

1✔
562
  str = pstrdup(p, "foo");
563

1✔
564
  res = pr_str_strip_end(str, NULL);
565
  ck_assert_msg(res == NULL, "Failed to handle null char argument");
1✔
566
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
567

1✔
568
  ch = "\r\n";
569

1✔
570
  res = pr_str_strip_end(NULL, ch);
571
  ck_assert_msg(res == NULL, "Failed to handle null str argument");
1✔
572
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
573

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

578
  ok = "foo";
579
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
580

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

586
  ok = "foo";
587
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
588

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

594
  ok = "foo";
595
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
596
}
1✔
597
END_TEST
1✔
598

599
START_TEST (get_token_test) {
600
  char *ok, *res, *str;
1✔
601

1✔
602
  res = pr_str_get_token(NULL, NULL);
603
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
1✔
604
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
605

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

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

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

620
  ok = "foo";
621
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
622

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

627
  ok = "bar";
628
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
629

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

634
  ok = "baz";
635
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
636

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

642
START_TEST (get_token2_test) {
643
  char *ok, *res, *str;
1✔
644
  size_t len = 0, ok_len;
1✔
645

1✔
646
  res = pr_str_get_token2(NULL, NULL, NULL);
647
  ck_assert_msg(res == NULL, "Failed to handle null arguments");
1✔
648
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
649

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

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

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

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

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

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

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

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

1✔
690
  res = pr_str_get_token2(&str, ",", &len);
691

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

699
START_TEST (get_word_test) {
700
  char *ok, *res, *str;
1✔
701

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

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

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

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

724
  ok = "";
725
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
726

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

731
  ok = "foo";
732
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
733

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

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

743
  ok = "#";
744
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
745

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

750
  ok = "foo";
751
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
752

1✔
753
  /* Test multiple embedded quotes. */
754

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

761
  ok = "foo";
762
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
763

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

768
  ok = "bar baz";
769
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
770

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

775
  ok = "qux";
776
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
777

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

782
  ok = "quz norf";
783
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
784

1✔
785
  /* Test embedded quotes with backslashes (Issue #1683). */
786
  mark_point();
787

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

793
  ok = "\\SYST";
794
  ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
1✔
795

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

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

1✔
809
  /* Now do the same tests with the IGNORE_QUOTES flag */
810
  mark_point();
811

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

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

827
START_TEST (get_word_utf8_test) {
828
  const char *path;
1✔
829
  FILE *fh;
1✔
830

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

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

1✔
845
    sz = 256;
846
    str = pcalloc(p, sz);
1✔
847

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

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

856
    ok = "foo";
857
    ck_assert_msg(strcmp(res, ok) != 0, "Did NOT expect '%s'", ok);
1✔
858

1✔
859
    fclose(fh);
860
  }
1✔
861
}
862
END_TEST
1✔
863

864
START_TEST (is_boolean_test) {
865
  int res;
1✔
866

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

872
  res = pr_str_is_boolean("on");
873
  ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
1✔
874

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

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

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

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

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

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

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

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

903
START_TEST (is_fnmatch_test) {
904
  int res;
1✔
905
  char *str;
1✔
906

1✔
907
  res = pr_str_is_fnmatch(NULL);
908
  ck_assert_msg(res == FALSE, "Expected false for NULL");
1✔
909

1✔
910
  str = "foo";
911
  res = pr_str_is_fnmatch(str);
1✔
912
  ck_assert_msg(res == FALSE, "Expected false for string '%s'", str);
1✔
913

1✔
914
  str = "foo?";
915
  res = pr_str_is_fnmatch(str);
1✔
916
  ck_assert_msg(res == TRUE, "Expected true for string '%s'", str);
1✔
917

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

1✔
922
  str = "foo[";
923
  res = pr_str_is_fnmatch(str);
1✔
924
  ck_assert_msg(res == FALSE, "Expected false for string '%s'", str);
1✔
925

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

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

1✔
934
  /* Now the fun cases using the escape character. */
935

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

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

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

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

954
START_TEST (get_nbytes_test) {
955
  char *str, *units;
1✔
956
  off_t nbytes;
1✔
957
  int res;
1✔
958

1✔
959
  res = pr_str_get_nbytes(NULL, NULL, NULL);
960
  ck_assert_msg(res == -1, "Failed to handle null arguments");
1✔
961
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
962

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

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

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

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

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

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

1✔
998
  /* XXX Test good suffix: B, KB, MB, GB, TB */
999

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

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

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

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

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

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

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

1052
START_TEST (get_duration_test) {
1053
  char *str;
1✔
1054
  int duration, expected;
1✔
1055
  int res;
1✔
1056

1✔
1057
  res = pr_str_get_duration(NULL, NULL);
1058
  ck_assert_msg(res == -1, "Failed to handle null arguments");
1✔
1059
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1✔
1060

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

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

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

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

1✔
1085
  /* Test well-formatted hh::mm::ss strings. */
1086

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

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

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

1111
  /* Test bad suffixes: -1h, -1hr, 9999foo, etc */
1112

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1251
START_TEST (strnrstr_test) {
1252
  int res, flags = 0;
1✔
1253
  const char *s = NULL, *suffix = NULL;
1✔
1254

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

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

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

1✔
1267
  s = suffix = "";
1268
  res = pr_strnrstr(s, 0, suffix, 0, flags);
1✔
1269
  ck_assert_msg(res == TRUE, "Expected true, got false");
1✔
1270

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

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

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

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

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

1299
START_TEST (bin2hex_test) {
1300
  char *expected, *res;
1✔
1301
  const unsigned char *str;
1✔
1302

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

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

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

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

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

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

1344
START_TEST (hex2bin_test) {
1345
  unsigned char *expected, *res;
1✔
1346
  const unsigned char *hex;
1✔
1347
  size_t expected_len, hex_len, len;
1✔
1348

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

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

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

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

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

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

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

1402
  /* uppercase */
1403
  hex = (const unsigned char *) "666F6F626172";
1404
  hex_len = strlen((char *) hex);
1✔
1405

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

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

1423
START_TEST (levenshtein_test) {
1424
  int res, expected, flags = 0;
1✔
1425
  const char *a, *b;
1✔
1426

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

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

1439
  a = "foo";
1440

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

1447
  expected = 0;
1448
  b = "Foo";
1✔
1449

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

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

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

1476
START_TEST (similars_test) {
1477
  array_header *res, *candidates;
1✔
1478
  const char *s, **similars, *expected;
1✔
1479

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

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

1492
  s = "foo";
1493

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

1500
  candidates = make_array(p, 5, sizeof(const char *));
1501

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

1508
  *((const char **) push_array(candidates)) = pstrdup(p, "fools");
1509
  *((const char **) push_array(candidates)) = pstrdup(p, "odd");
1✔
1510
  *((const char **) push_array(candidates)) = pstrdup(p, "bar");
1✔
1511
  *((const char **) push_array(candidates)) = pstrdup(p, "FOO");
1✔
1512

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

1520
  mark_point();
1521
  similars = (const char **) res->elts;
1✔
1522

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

1533
  expected = "fools";
1534

1✔
1535
  ck_assert_msg(strcmp(similars[0], expected) == 0,
1536
    "Expected similar '%s', got '%s'", expected, similars[0]);
1✔
1537

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

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

1548
  mark_point();
1549
  similars = (const char **) res->elts;
1✔
1550

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

1563
  ck_assert_msg(strcmp(similars[0], expected) == 0,
1564
    "Expected similar '%s', got '%s'", expected, similars[0]);
1✔
1565

1566
  expected = "fools";
1567

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

1573
START_TEST (str2uid_test) {
1574
  int res;
1✔
1575

1✔
1576
  res = pr_str2uid(NULL, NULL);
1577
  ck_assert_msg(res == -1, "Failed to handle null arguments");
1✔
1578
}
1✔
1579
END_TEST
1✔
1580

1581
START_TEST (str2gid_test) {
1582
  int res;
1✔
1583

1✔
1584
  res = pr_str2gid(NULL, NULL);
1585
  ck_assert_msg(res == -1, "Failed to handle null arguments");
1✔
1586
}
1✔
1587
END_TEST
1✔
1588

1589
START_TEST (uid2str_test) {
1590
  const char *res;
1✔
1591

1✔
1592
  res = pr_uid2str(NULL, (uid_t) 1);
1593
  ck_assert_msg(strcmp(res, "1") == 0, "Expected '1', got '%s'", 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
END_TEST
1✔
1599

1600
START_TEST (gid2str_test) {
1601
  const char *res;
1✔
1602

1✔
1603
  res = pr_gid2str(NULL, (gid_t) 1);
1604
  ck_assert_msg(strcmp(res, "1") == 0, "Expected '1', got '%s'", 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
END_TEST
1✔
1610

1611
START_TEST (str_quote_test) {
1612
  const char *res;
1✔
1613
  char *expected, *path;
1✔
1614

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

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

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

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

1641
START_TEST (quote_dir_test) {
1642
  const char *res;
1✔
1643
  char *expected, *path;
1✔
1644

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

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

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

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

1671
START_TEST (text_to_array_test) {
1672
  register unsigned int i;
1✔
1673
  array_header *res;
1✔
1674
  const char *text, *elt;
1✔
1675

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

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

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

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

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

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

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

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

3✔
1731
    expected = "foo";
1732
    ck_assert_msg(strcmp(item, expected) == 0,
3✔
1733
      "Expected '%s' at index %u, got '%s'", expected, i, item);
3✔
1734
  }
1735

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

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

3✔
1748
    expected = "foo";
1749
    ck_assert_msg(strcmp(item, expected) == 0,
3✔
1750
      "Expected '%s' at index %u, got '%s'", expected, i, item);
3✔
1751
  }
1752

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

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

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

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

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

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

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

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

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

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

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

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

1826
START_TEST (array_to_text_test) {
1827
  array_header *items;
1✔
1828
  const char *delimiter;
1✔
1829
  char *res, *expected;
1✔
1830

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

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

1843
  items = make_array(p, 0, sizeof(char *));
1844

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

1851
  delimiter = ":";
1852
  expected = "";
1✔
1853

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

1861
  *((char **) push_array(items)) = pstrdup(p, "foo");
1862
  expected = "foo";
1✔
1863

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

1871
  *((char **) push_array(items)) = pstrdup(p, "bar");
1872
  expected = "foo:bar";
1✔
1873

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

1883
Suite *tests_get_str_suite(void) {
1884
  Suite *suite;
889✔
1885
  TCase *testcase;
889✔
1886

889✔
1887
  suite = suite_create("str");
1888

889✔
1889
  testcase = tcase_create("base");
1890
  tcase_add_checked_fixture(testcase, set_up, tear_down);
889✔
1891

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

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

© 2026 Coveralls, Inc