Debian Codegraph @

tests/api/str.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server testsuite
3 * Copyright (c) 2008-2023 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, The ProFTPD Project team and other respective
20 * copyright holders give permission to link this program with OpenSSL, and
21 * distribute the resulting executable, without including the source code for
22 * OpenSSL in the source distribution.
23 */
24
25 /* String API tests. */
26
27 #include "tests.h"
28
29 static pool *p = NULL;
30
31 static void set_up(void) {
32 if (p == NULL) {
33 p = make_sub_pool(NULL);
34 }
35 }
36
37 static void tear_down(void) {
38 if (p) {
39 destroy_pool(p);
40 p = NULL;
41 }
42 }
43
44 START_TEST (sstrncpy_test) {
45 char *ok, *dst;
46 size_t len, sz = 32;
47 int res;
48
49 len = 0;
50 res = sstrncpy(NULL, NULL, len);
51 ck_assert_msg(res == -1, "Failed to handle null arguments");
52
53 dst = "";
54 res = sstrncpy(dst, "foo", 0);
55 ck_assert_msg(res == 0, "Failed to handle zero length");
56
57 dst = pcalloc(p, sz);
58 memset(dst, 'A', sz);
59
60 len = 1;
61 res = sstrncpy(dst, NULL, len);
62 ck_assert_msg(res == -1, "Failed to handle null arguments");
63
64 ok = "Therefore, all progress depends on the unreasonable man";
65
66 mark_point();
67 res = sstrncpy(ok, ok, 1);
68 ck_assert_msg(res == 1, "Expected result 1, got %d", res);
69
70 mark_point();
71 memset(dst, 'A', sz);
72 len = 1;
73
74 res = sstrncpy(dst, ok, len);
75 ck_assert_msg((size_t) res <= len, "Expected result %lu, got %d",
76 (unsigned long)len, res);
77 ck_assert_msg(strlen(dst) == (len - 1), "Expected len %lu, got len %lu",
78 (unsigned long)len - 1, (unsigned long)strlen(dst));
79 ck_assert_msg(dst[len-1] == '\0', "Expected NUL, got '%c'", dst[len-1]);
80
81 memset(dst, 'A', sz);
82 len = 7;
83
84 res = sstrncpy(dst, ok, len);
85 ck_assert_msg((size_t) res <= len, "Expected result %lu, got %d",
86 (unsigned long)len, res);
87 ck_assert_msg(strlen(dst) == (len - 1), "Expected len %lu, got len %lu",
88 (unsigned long)len - 1, (unsigned long)strlen(dst));
89 ck_assert_msg(dst[len-1] == '\0', "Expected NUL, got '%c'", dst[len-1]);
90
91 memset(dst, 'A', sz);
92 len = sz;
93
94 res = sstrncpy(dst, ok, len);
95 ck_assert_msg((size_t) res <= len, "Expected result %lu, got %d",
96 (unsigned long)len, res);
97 ck_assert_msg(strlen(dst) == (len - 1), "Expected len %lu, got len %lu",
98 (unsigned long)len - 1, (unsigned long)strlen(dst));
99 ck_assert_msg(dst[len-1] == '\0', "Expected NUL, got '%c'", dst[len-1]);
100
101 memset(dst, 'A', sz);
102 len = sz;
103
104 res = sstrncpy(dst, "", len);
105 ck_assert_msg((size_t) res <= len, "Expected result %lu, got %d",
106 (unsigned long)len, res);
107 ck_assert_msg(strlen(dst) == 0, "Expected len %u, got len %lu", 0,
108 (unsigned long)strlen(dst));
109 ck_assert_msg(*dst == '\0', "Expected NUL, got '%c'", *dst);
110 }
111 END_TEST
112
113 START_TEST (sstrcat_test) {
114 register unsigned int i;
115 char c = 'A', src[1024], dst[1024], *res;
116
117 res = sstrcat(dst, src, 0);
118 ck_assert_msg(res == NULL, "Non-null result for zero-length strcat");
119
120 src[0] = 'f';
121 src[1] = '\0';
122 dst[0] = 'e';
123 dst[1] = '\0';
124 res = sstrcat(dst, src, 1);
125 ck_assert_msg(res == dst, "Returned wrong destination buffer");
126
127 /* In this case, we told sstrcat() that dst is len 1, which means that
128 * sstrcat() should set dst[0] to NUL.
129 */
130 ck_assert_msg(dst[0] == 0, "Failed to terminate destination buffer");
131
132 src[0] = 'f';
133 src[1] = '\0';
134 dst[0] = 'e';
135 dst[1] = '\0';
136 res = sstrcat(dst, src, 2);
137 ck_assert_msg(res == dst, "Returned wrong destination buffer");
138
139 /* In this case, we told sstrcat() that dst is len 2, which means that
140 * sstrcat() should preserve the value at 0, and set dst[1] to NUL.
141 */
142 ck_assert_msg(dst[0] == 'e',
143 "Failed to preserve destination buffer (expected '%c' at index 0, "
144 "got '%c')", 'e', dst[0]);
145
146 ck_assert_msg(dst[1] == 0, "Failed to terminate destination buffer");
147
148 mark_point();
149 src[0] = 'f';
150 src[1] = '\0';
151 dst[0] = 'e';
152 dst[1] = '\0';
153 res = sstrcat(dst, src, 3);
154 ck_assert_msg(res == dst, "Returned wrong destination buffer");
155
156 mark_point();
157 ck_assert_msg(dst[0] == 'e',
158 "Failed to preserve destination buffer (expected '%c' at index 0, "
159 "got '%c')", 'e', dst[0]);
160
161 mark_point();
162 ck_assert_msg(dst[1] == 'f',
163 "Failed to copy source buffer (expected '%c' at index 1, got '%c')",
164 'f', dst[1]);
165
166 mark_point();
167 ck_assert_msg(dst[2] == 0, "Failed to terminate destination buffer");
168
169 mark_point();
170 memset(src, c, sizeof(src)-1);
171
172 /* Note: we need to NUL-terminate the source buffer, for e.g. strlcat(3)
173 * implementations. Failure to do so can yield SIGABRT/SIGSEGV problems
174 * during e.g. unit tests.
175 */
176 src[sizeof(src)-1] = '\0';
177 dst[0] = '\0';
178
179 mark_point();
180 res = sstrcat(dst, src, sizeof(dst));
181
182 mark_point();
183 ck_assert_msg(res == dst, "Returned wrong destination buffer");
184
185 mark_point();
186 ck_assert_msg(dst[sizeof(dst)-1] == 0,
187 "Failed to terminate destination buffer");
188
189 mark_point();
190 ck_assert_msg(strlen(dst) == (sizeof(dst)-1),
191 "Failed to copy all the data (expected len %lu, got len %lu)",
192 (unsigned long)sizeof(dst)-1, (unsigned long)strlen(dst));
193
194 mark_point();
195 for (i = 0; i < sizeof(dst)-1; i++) {
196 ck_assert_msg(dst[i] == c, "Copied wrong value (expected '%c', got '%c')",
197 c, dst[i]);
198 }
199 }
200 END_TEST
201
202 START_TEST (sreplace_test) {
203 const char *res;
204 char *fmt = NULL, *ok;
205
206 res = sreplace(NULL, NULL, 0);
207 ck_assert_msg(res == NULL, "Failed to handle invalid arguments");
208 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
209
210 res = sreplace(NULL, "", 0);
211 ck_assert_msg(res == NULL, "Failed to handle invalid arguments");
212 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
213
214 res = sreplace(p, NULL, 0);
215 ck_assert_msg(res == NULL, "Failed to handle invalid arguments");
216 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
217
218 fmt = "%a";
219 res = sreplace(p, fmt, "foo", NULL);
220 ck_assert_msg(strcmp(res, fmt) == 0, "Expected '%s', got '%s'", fmt, res);
221
222 fmt = "foo %a";
223 res = sreplace(p, fmt, "%b", NULL);
224 ck_assert_msg(strcmp(res, fmt) == 0, "Expected '%s', got '%s'", fmt, res);
225
226 fmt = "foo %a";
227 ok = "foo bar";
228 res = sreplace(p, fmt, "%a", "bar", NULL);
229 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
230
231 fmt = "foo %a %a";
232 ok = "foo bar bar";
233 res = sreplace(p, fmt, "%a", "bar", NULL);
234 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
235
236 fmt = "foo %a %a %a %a %a %a %a %a";
237 ok = "foo bar bar bar bar bar bar bar bar";
238 res = sreplace(p, fmt, "%a", "bar", NULL);
239 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
240
241 /* sreplace() will not handle more than 8 occurrences of the same escape
242 * sequence in the same line. Make sure this happens.
243 */
244 fmt = "foo %a %a %a %a %a %a %a %a %a";
245 ok = "foo bar bar bar bar bar bar bar bar bar";
246 res = sreplace(p, fmt, "%a", "bar", NULL);
247 ck_assert_msg(strcmp(res, fmt) == 0, "Expected '%s', got '%s'", fmt, res);
248 }
249 END_TEST
250
251 START_TEST (sreplace_enospc_test) {
252 const char *res;
253 char *fmt = NULL;
254 size_t bufsz = 8192;
255
256 fmt = palloc(p, bufsz + 1);
257 memset(fmt, ' ', bufsz);
258 fmt[bufsz-2] = '%';
259 fmt[bufsz-1] = 'a';
260 fmt[bufsz] = '\0';
261
262 res = sreplace(p, fmt, "%a", "foo", NULL);
263 ck_assert_msg(res == NULL, "Failed to reject too-long buffer");
264 ck_assert_msg(errno == ENOSPC, "Failed to set errno to ENOSPC");
265 }
266 END_TEST
267
268 START_TEST (sreplace_bug3614_test) {
269 const char *res;
270 char *fmt = NULL, *ok;
271
272 fmt = "%a %b %c %d %e %f %g %h %i %j %k %l %m "
273 "%n %o %p %q %r %s %t %u %v %w %x %y %z "
274 "%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 "%0 %1 %2 %3 %4 %5 %6 %7 %8 %9 "
277 "%{a} %{b} %{c} %{d} %{e} %{f} %{g} %{h} %{i} %{j} %{k} %{l} %{m} "
278 "%{n} %{o} %{p} %{q} %{r} %{s} %{t} %{u} %{v} %{w} %{x} %{y} %{z} "
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 "%{aa} %{bb} %{cc} %{dd} %{ee} %{ff} %{gg} %{hh} %{ii} %{jj} "
282 "%{kk} %{ll} %{mm} %{nn} %{oo} %{pp} %{qq} %{rr} %{ss} %{tt} "
283 "%{uu} %{vv} %{ww} %{xx} %{yy} %{zz}";
284
285 /* We put a limit on the maximum number of replacements that sreplace()
286 * will perform on a given string, per Bug#3614.
287 */
288 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}";
289
290 res = sreplace(p, fmt,
291 "%a", "bar", "%b", "bar", "%c", "bar", "%d", "bar", "%e", "bar",
292 "%f", "bar", "%g", "bar", "%h", "bar", "%i", "bar", "%j", "bar",
293 "%k", "bar", "%l", "bar", "%m", "bar", "%n", "bar", "%o", "bar",
294 "%p", "bar", "%q", "bar", "%r", "bar", "%s", "bar", "%t", "bar",
295 "%u", "bar", "%v", "bar", "%w", "bar", "%x", "bar", "%y", "bar",
296 "%z", "bar",
297 "%A", "bar", "%B", "bar", "%C", "bar", "%D", "bar", "%E", "bar",
298 "%F", "bar", "%G", "bar", "%H", "bar", "%I", "bar", "%J", "bar",
299 "%K", "bar", "%L", "bar", "%M", "bar", "%N", "bar", "%O", "bar",
300 "%P", "bar", "%Q", "bar", "%R", "bar", "%S", "bar", "%T", "bar",
301 "%U", "bar", "%V", "bar", "%W", "bar", "%X", "bar", "%Y", "bar",
302 "%Z", "bar",
303 "%0", "bar", "%1", "bar", "%2", "bar", "%3", "bar", "%4", "bar",
304 "%5", "bar", "%6", "bar", "%7", "bar", "%8", "bar", "%9", "bar",
305 "%{a}", "bar", "%{b}", "bar", "%{c}", "bar", "%{d}", "bar", "%{e}", "bar",
306 "%{f}", "bar", "%{g}", "bar", "%{h}", "bar", "%{i}", "bar", "%{j}", "bar",
307 "%{k}", "bar", "%{l}", "bar", "%{m}", "bar", "%{n}", "bar", "%{o}", "bar",
308 "%{p}", "bar", "%{q}", "bar", "%{r}", "bar", "%{s}", "bar", "%{t}", "bar",
309 "%{u}", "bar", "%{v}", "bar", "%{w}", "bar", "%{x}", "bar", "%{y}", "bar",
310 "%{z}", "bar",
311 "%{A}", "bar", "%{B}", "bar", "%{C}", "bar", "%{D}", "bar", "%{E}", "bar",
312 "%{F}", "bar", "%{G}", "bar", "%{H}", "bar", "%{I}", "bar", "%{J}", "bar",
313 "%{K}", "bar", "%{L}", "bar", "%{M}", "bar", "%{N}", "bar", "%{O}", "bar",
314 "%{P}", "bar", "%{Q}", "bar", "%{R}", "bar", "%{S}", "bar", "%{T}", "bar",
315 "%{U}", "bar", "%{V}", "bar", "%{W}", "bar", "%{X}", "bar", "%{Y}", "bar",
316 "%{Z}", "bar",
317 "%{aa}", "bar", "%{bb}", "bar", "%{cc}", "bar", "%{dd}", "bar",
318 "%{ee}", "bar", "%{ff}", "bar", "%{gg}", "bar", "%{hh}", "bar",
319 "%{ii}", "bar", "%{jj}", "bar", "%{kk}", "bar", "%{ll}", "bar",
320 "%{mm}", "bar", "%{nn}", "bar", "%{oo}", "bar", "%{pp}", "bar",
321 "%{qq}", "bar", "%{rr}", "bar", "%{ss}", "bar", "%{tt}", "bar",
322 "%{uu}", "bar", "%{vv}", "bar", "%{ww}", "bar", "%{xx}", "bar",
323 "%{yy}", "bar", "%{zz}", "bar",
324 NULL);
325 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
326 }
327 END_TEST
328
329 START_TEST (str_replace_test) {
330 const char *res;
331 char *fmt = NULL, *ok;
332 int max_replace = PR_STR_MAX_REPLACEMENTS;
333
334 res = pr_str_replace(NULL, max_replace, NULL, 0);
335 ck_assert_msg(res == NULL, "Failed to handle invalid arguments");
336 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
337
338 res = pr_str_replace(NULL, max_replace, "", 0);
339 ck_assert_msg(res == NULL, "Failed to handle invalid arguments");
340 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
341
342 res = pr_str_replace(p, max_replace, NULL, 0);
343 ck_assert_msg(res == NULL, "Failed to handle invalid arguments");
344 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
345
346 fmt = "%a";
347 res = pr_str_replace(p, max_replace, fmt, "foo", NULL);
348 ck_assert_msg(strcmp(res, fmt) == 0, "Expected '%s', got '%s'", fmt, res);
349
350 fmt = "foo %a";
351 res = pr_str_replace(p, max_replace, fmt, "%b", NULL);
352 ck_assert_msg(strcmp(res, fmt) == 0, "Expected '%s', got '%s'", fmt, res);
353
354 fmt = "foo %a";
355 ok = "foo bar";
356 res = pr_str_replace(p, max_replace, fmt, "%a", "bar", NULL);
357 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
358
359 fmt = "foo %a %a";
360 ok = "foo bar bar";
361 res = pr_str_replace(p, max_replace, fmt, "%a", "bar", NULL);
362 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
363
364 fmt = "foo %a %a %a %a %a %a %a %a";
365 ok = "foo bar bar bar bar bar bar bar bar";
366 res = pr_str_replace(p, max_replace, fmt, "%a", "bar", NULL);
367 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
368
369 fmt = "foo %a %a %a %a %a %a %a %a %a";
370 ok = "foo bar bar bar bar bar bar bar bar bar";
371 res = pr_str_replace(p, max_replace, fmt, "%a", "bar", NULL);
372 ck_assert_msg(res == NULL, "Failed to handle too many replacements");
373 ck_assert_msg(errno == E2BIG, "Failed to set errno to E2BIG");
374 }
375 END_TEST
376
377 START_TEST (pdircat_test) {
378 char *res, *ok;
379
380 res = pdircat(NULL, 0, NULL);
381 ck_assert_msg(res == NULL, "Failed to handle null arguments");
382 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
383
384 res = pdircat(p, 0, NULL);
385 ck_assert_msg(res != NULL,
386 "Failed to handle empty arguments (expected '', got NULL)");
387 ck_assert_msg(strcmp(res, "") == 0, "Expected '%s', got '%s'", "", res);
388
389 /* Comments in the pdircat() function suggest that an empty string
390 * should be treated as a leading slash. However, that never got
391 * implemented. Is this a bug, or just an artifact? I doubt that it
392 * is causing problems at present.
393 */
394 res = pdircat(p, "", NULL);
395 ok = "";
396 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
397
398 res = pdircat(p, "foo", "bar", NULL);
399 ok = "foo/bar";
400 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
401
402 res = pdircat(p, "", "foo", "bar", NULL);
403 ok = "foo/bar";
404 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
405
406 res = pdircat(p, "/", "/foo/", "/bar/", NULL);
407 ok = "/foo/bar/";
408 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
409
410 /* Sadly, pdircat() only handles single leading/trailing slashes, not
411 * an arbitrary number of leading/trailing slashes.
412 */
413 res = pdircat(p, "//", "//foo//", "//bar//", NULL);
414 ok = "///foo///bar//";
415 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
416 }
417 END_TEST
418
419 START_TEST (pstrcat_test) {
420 char *res, *ok;
421
422 res = pstrcat(NULL, 0, NULL);
423 ck_assert_msg(res == NULL, "Failed to handle null arguments");
424 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
425
426 res = pstrcat(p, 0, NULL);
427 ck_assert_msg(res != NULL,
428 "Failed to handle empty arguments (expected '', got NULL)");
429 ck_assert_msg(strcmp(res, "") == 0, "Expected '%s', got '%s'", "", res);
430
431 res = pstrcat(p, "", NULL);
432 ok = "";
433 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
434
435 res = pstrcat(p, "foo", "bar", NULL);
436 ok = "foobar";
437 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
438
439 res = pstrcat(p, "", "foo", "bar", NULL);
440 ok = "foobar";
441 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
442
443 res = pstrcat(p, "/", "/foo/", "/bar/", NULL);
444 ok = "//foo//bar/";
445 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
446
447 res = pdircat(p, "//", "//foo//", NULL, "//bar//", NULL);
448 ok = "///foo//";
449 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
450 }
451 END_TEST
452
453 START_TEST (pstrdup_test) {
454 char *res, *ok;
455
456 res = pstrdup(NULL, NULL);
457 ck_assert_msg(res == NULL, "Failed to handle null arguments");
458 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
459
460 res = pstrdup(p, NULL);
461 ck_assert_msg(res == NULL, "Failed to handle null arguments");
462 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
463
464 res = pstrdup(NULL, "");
465 ck_assert_msg(res == NULL, "Failed to handle null arguments");
466 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
467
468 res = pstrdup(p, "foo");
469 ok = "foo";
470 ck_assert_msg(strlen(res) == strlen(ok), "Expected len %lu, got len %lu",
471 (unsigned long)strlen(ok), (unsigned long)strlen(res));
472 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
473 }
474 END_TEST
475
476 START_TEST (pstrndup_test) {
477 char *res, *ok;
478
479 res = pstrndup(NULL, NULL, 0);
480 ck_assert_msg(res == NULL, "Failed to handle null arguments");
481 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
482
483 res = pstrndup(p, NULL, 0);
484 ck_assert_msg(res == NULL, "Failed to handle null arguments");
485 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
486
487 res = pstrndup(NULL, "", 0);
488 ck_assert_msg(res == NULL, "Failed to handle null arguments");
489 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
490
491 res = pstrndup(p, "foo", 0);
492 ok = "";
493 ck_assert_msg(strlen(res) == strlen(ok), "Expected len %lu, got len %lu",
494 (unsigned long)strlen(ok), (unsigned long)strlen(res));
495 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
496
497 res = pstrndup(p, "foo", 1);
498 ok = "f";
499 ck_assert_msg(strlen(res) == strlen(ok), "Expected len %lu, got len %lu",
500 (unsigned long)strlen(ok), (unsigned long)strlen(res));
501 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
502
503 res = pstrndup(p, "foo", 10);
504 ok = "foo";
505 ck_assert_msg(strlen(res) == strlen(ok), "Expected len %lu, got len %lu",
506 (unsigned long)strlen(ok), (unsigned long)strlen(res));
507 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
508 }
509 END_TEST
510
511 START_TEST (strip_test) {
512 const char *ok, *res, *str;
513
514 res = pr_str_strip(NULL, NULL);
515 ck_assert_msg(res == NULL, "Failed to handle null arguments");
516 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
517
518 res = pr_str_strip(p, NULL);
519 ck_assert_msg(res == NULL, "Failed to handle null str argument");
520 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
521
522 res = pr_str_strip(NULL, "foo");
523 ck_assert_msg(res == NULL, "Failed to handle null pool argument");
524 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
525
526 str = pstrdup(p, "foo");
527 res = pr_str_strip(p, str);
528 ck_assert_msg(res != NULL, "Failed to strip '%s': %s", str, strerror(errno));
529
530 ok = "foo";
531 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
532
533 str = pstrdup(p, " \n \t foo");
534 res = pr_str_strip(p, str);
535 ck_assert_msg(res != NULL, "Failed to strip '%s': %s", str, strerror(errno));
536
537 ok = "foo";
538 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
539
540 str = pstrdup(p, "foo \n \t \r");
541 res = pr_str_strip(p, str);
542 ck_assert_msg(res != NULL, "Failed to strip '%s': %s", str, strerror(errno));
543
544 ok = "foo";
545 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
546
547 str = pstrdup(p, "\r \n\n\t foo \n \t \r");
548 res = pr_str_strip(p, str);
549 ck_assert_msg(res != NULL, "Failed to strip '%s': %s", str, strerror(errno));
550
551 ok = "foo";
552 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
553 }
554 END_TEST
555
556 START_TEST (strip_end_test) {
557 char *ch, *ok, *res, *str;
558
559 res = pr_str_strip_end(NULL, NULL);
560 ck_assert_msg(res == NULL, "Failed to handle null arguments");
561 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
562
563 str = pstrdup(p, "foo");
564
565 res = pr_str_strip_end(str, NULL);
566 ck_assert_msg(res == NULL, "Failed to handle null char argument");
567 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
568
569 ch = "\r\n";
570
571 res = pr_str_strip_end(NULL, ch);
572 ck_assert_msg(res == NULL, "Failed to handle null str argument");
573 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
574
575 res = pr_str_strip_end(str, ch);
576 ck_assert_msg(res != NULL, "Failed to strip '%s' from end of '%s': %s",
577 ch, str, strerror(errno));
578
579 ok = "foo";
580 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
581
582 str = pstrdup(p, "foo\r\n");
583 res = pr_str_strip_end(str, ch);
584 ck_assert_msg(res != NULL, "Failed to strip '%s' from end of '%s': %s",
585 ch, str, strerror(errno));
586
587 ok = "foo";
588 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
589
590 str = pstrdup(p, "foo\r\n\r\n\r\n");
591 res = pr_str_strip_end(str, ch);
592 ck_assert_msg(res != NULL, "Failed to strip '%s' from end of '%s': %s",
593 ch, str, strerror(errno));
594
595 ok = "foo";
596 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
597 }
598 END_TEST
599
600 START_TEST (get_token_test) {
601 char *ok, *res, *str;
602
603 res = pr_str_get_token(NULL, NULL);
604 ck_assert_msg(res == NULL, "Failed to handle null arguments");
605 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
606
607 str = NULL;
608 res = pr_str_get_token(&str, NULL);
609 ck_assert_msg(res == NULL, "Failed to handle null str argument");
610 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
611
612 str = pstrdup(p, "foo,bar,baz");
613 res = pr_str_get_token(&str, NULL);
614 ck_assert_msg(res == NULL, "Failed to handle null sep argument");
615 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
616
617 res = pr_str_get_token(&str, ",");
618 ck_assert_msg(res != NULL, "Failed to get token from '%s': %s", str,
619 strerror(errno));
620
621 ok = "foo";
622 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
623
624 res = pr_str_get_token(&str, ",");
625 ck_assert_msg(res != NULL, "Failed to get token from '%s': %s", str,
626 strerror(errno));
627
628 ok = "bar";
629 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
630
631 res = pr_str_get_token(&str, ",");
632 ck_assert_msg(res != NULL, "Failed to get token from '%s': %s", str,
633 strerror(errno));
634
635 ok = "baz";
636 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
637
638 res = pr_str_get_token(&str, ",");
639 ck_assert_msg(res == NULL, "Unexpectedly got token '%s'", res);
640 }
641 END_TEST
642
643 START_TEST (get_token2_test) {
644 char *ok, *res, *str;
645 size_t len = 0, ok_len;
646
647 res = pr_str_get_token2(NULL, NULL, NULL);
648 ck_assert_msg(res == NULL, "Failed to handle null arguments");
649 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
650
651 str = NULL;
652 res = pr_str_get_token2(&str, NULL, NULL);
653 ck_assert_msg(res == NULL, "Failed to handle null str argument");
654 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
655
656 str = pstrdup(p, "foo,bar,bazz");
657 res = pr_str_get_token2(&str, NULL, NULL);
658 ck_assert_msg(res == NULL, "Failed to handle null sep argument");
659 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
660
661 res = pr_str_get_token2(&str, ",", &len);
662 ck_assert_msg(res != NULL, "Failed to get token from '%s': %s", str,
663 strerror(errno));
664
665 ok = "foo";
666 ok_len = 3;
667 ck_assert_msg(len == ok_len, "Expected len %lu, got %lu",
668 (unsigned long) ok_len, (unsigned long) len);
669 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
670
671 res = pr_str_get_token2(&str, ",", &len);
672 ck_assert_msg(res != NULL, "Failed to get token from '%s': %s", str,
673 strerror(errno));
674
675 ok = "bar";
676 ok_len = 3;
677 ck_assert_msg(len == ok_len, "Expected len %lu, got %lu",
678 (unsigned long) ok_len, (unsigned long) len);
679 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
680
681 res = pr_str_get_token2(&str, ",", &len);
682 ck_assert_msg(res != NULL, "Failed to get token from '%s': %s", str,
683 strerror(errno));
684
685 ok = "bazz";
686 ok_len = 4;
687 ck_assert_msg(len == ok_len, "Expected len %lu, got %lu",
688 (unsigned long) ok_len, (unsigned long) len);
689 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
690
691 res = pr_str_get_token2(&str, ",", &len);
692
693 ok_len = 0;
694 ck_assert_msg(len == ok_len, "Expected len %lu, got %lu",
695 (unsigned long) ok_len, (unsigned long) len);
696 ck_assert_msg(res == NULL, "Unexpectedly got token '%s'", res);
697 }
698 END_TEST
699
700 START_TEST (get_word_test) {
701 char *ok, *res, *str;
702
703 mark_point();
704 res = pr_str_get_word(NULL, 0);
705 ck_assert_msg(res == NULL, "Failed to handle null arguments");
706 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
707
708 mark_point();
709 str = NULL;
710 res = pr_str_get_word(&str, 0);
711 ck_assert_msg(res == NULL, "Failed to handle null str argument");
712 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
713
714 mark_point();
715 str = pstrdup(p, " ");
716 res = pr_str_get_word(&str, 0);
717 ck_assert_msg(res == NULL, "Failed to handle whitespace argument");
718
719 mark_point();
720 str = pstrdup(p, " foo");
721 res = pr_str_get_word(&str, PR_STR_FL_PRESERVE_WHITESPACE);
722 ck_assert_msg(res != NULL, "Failed to handle whitespace argument: %s",
723 strerror(errno));
724
725 ok = "";
726 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
727
728 res = pr_str_get_word(&str, PR_STR_FL_PRESERVE_WHITESPACE);
729 ck_assert_msg(res != NULL, "Failed to handle whitespace argument: %s",
730 strerror(errno));
731
732 ok = "foo";
733 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
734
735 mark_point();
736 str = pstrdup(p, " # foo");
737 res = pr_str_get_word(&str, 0);
738 ck_assert_msg(res == NULL, "Failed to handle commented argument");
739
740 res = pr_str_get_word(&str, PR_STR_FL_PRESERVE_COMMENTS);
741 ck_assert_msg(res != NULL, "Failed to handle commented argument: %s",
742 strerror(errno));
743
744 ok = "#";
745 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
746
747 res = pr_str_get_word(&str, PR_STR_FL_PRESERVE_COMMENTS);
748 ck_assert_msg(res != NULL, "Failed to handle commented argument: %s",
749 strerror(errno));
750
751 ok = "foo";
752 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
753
754 /* Test multiple embedded quotes. */
755
756 mark_point();
757 str = pstrdup(p, "foo \"bar baz\" qux \"quz norf\"");
758 res = pr_str_get_word(&str, 0);
759 ck_assert_msg(res != NULL, "Failed to handle quoted argument: %s",
760 strerror(errno));
761
762 ok = "foo";
763 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
764
765 res = pr_str_get_word(&str, 0);
766 ck_assert_msg(res != NULL, "Failed to handle quoted argument: %s",
767 strerror(errno));
768
769 ok = "bar baz";
770 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
771
772 res = pr_str_get_word(&str, 0);
773 ck_assert_msg(res != NULL, "Failed to handle quoted argument: %s",
774 strerror(errno));
775
776 ok = "qux";
777 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
778
779 res = pr_str_get_word(&str, 0);
780 ck_assert_msg(res != NULL, "Failed to handle quoted argument: %s",
781 strerror(errno));
782
783 ok = "quz norf";
784 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
785
786 /* Test embedded quotes with backslashes (Issue #1683). */
787 mark_point();
788
789 str = pstrdup(p, "\"\\\\SYST\"");
790 res = pr_str_get_word(&str, 0);
791 ck_assert_msg(res != NULL, "Failed to handle quoted argument: %s",
792 strerror(errno));
793
794 ok = "\\SYST";
795 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
796
797 mark_point();
798 str = pstrdup(p, "\"\"\\\\SYST");
799 res = pr_str_get_word(&str, 0);
800 ck_assert_msg(res != NULL, "Failed to handle quoted argument: %s",
801 strerror(errno));
802
803 /* Note that pr_str_get_word() is intended to be called multiple times
804 * on an advancing buffer, effectively tokenizing the buffer. This is
805 * why the function does NOT decrement its quote mode.
806 */
807 ok = "";
808 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
809
810 /* Now do the same tests with the IGNORE_QUOTES flag */
811 mark_point();
812
813 str = ok = pstrdup(p, "\"\\\\SYST\"");
814 res = pr_str_get_word(&str, PR_STR_FL_IGNORE_QUOTES);
815 ck_assert_msg(res != NULL, "Failed to handle quoted argument: %s",
816 strerror(errno));
817 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
818
819 mark_point();
820 str = ok = pstrdup(p, "\"\"\\\\SYST");
821 res = pr_str_get_word(&str, PR_STR_FL_IGNORE_QUOTES);
822 ck_assert_msg(res != NULL, "Failed to handle quoted argument: %s",
823 strerror(errno));
824 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
825 }
826 END_TEST
827
828 START_TEST (get_word_utf8_test) {
829 const char *path;
830 FILE *fh;
831
832 /* Test UT8 spaces. Note that in order to do this, I had to use
833 * some other tool (Perl) to emit the desired UTF8 characters to
834 * a file; we then read in the bytes to parse from that file. Some
835 * compilers (e.g. gcc), in conjunction with the terminal/editor I'm
836 * using, don't like using the '\uNNNN' syntax for encoding UTF8 in C
837 * source code.
838 */
839
840 path = "api/etc/str/utf8-space.txt";
841 fh = fopen(path, "r");
842 if (fh != NULL) {
843 char *ok, *res, *str;
844 size_t nread = 0, sz;
845
846 sz = 256;
847 str = pcalloc(p, sz);
848
849 nread = fread(str, sizeof(char), sz-1, fh);
850 ck_assert_msg(!ferror(fh), "Error reading '%s': %s", path, strerror(errno));
851 ck_assert_msg(nread > 0, "Expected >0 bytes read, got 0");
852
853 res = pr_str_get_word(&str, 0);
854 ck_assert_msg(res != NULL, "Failed to handle UTF8 argument: %s",
855 strerror(errno));
856
857 ok = "foo";
858 ck_assert_msg(strcmp(res, ok) != 0, "Did NOT expect '%s'", ok);
859
860 fclose(fh);
861 }
862 }
863 END_TEST
864
865 START_TEST (is_boolean_test) {
866 int res;
867
868 res = pr_str_is_boolean(NULL);
869 ck_assert_msg(res == -1, "Failed to handle null argument");
870 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
871 errno);
872
873 res = pr_str_is_boolean("on");
874 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
875
876 res = pr_str_is_boolean("Yes");
877 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
878
879 res = pr_str_is_boolean("TrUe");
880 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
881
882 res = pr_str_is_boolean("1");
883 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
884
885 res = pr_str_is_boolean("oFF");
886 ck_assert_msg(res == FALSE, "Expected FALSE, got TRUE");
887
888 res = pr_str_is_boolean("no");
889 ck_assert_msg(res == FALSE, "Expected FALSE, got TRUE");
890
891 res = pr_str_is_boolean("false");
892 ck_assert_msg(res == FALSE, "Expected FALSE, got TRUE");
893
894 res = pr_str_is_boolean("0");
895 ck_assert_msg(res == FALSE, "Expected FALSE, got TRUE");
896
897 res = pr_str_is_boolean("foo");
898 ck_assert_msg(res == -1, "Failed to handle null argument");
899 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
900 errno);
901 }
902 END_TEST
903
904 START_TEST (is_fnmatch_test) {
905 int res;
906 char *str;
907
908 res = pr_str_is_fnmatch(NULL);
909 ck_assert_msg(res == FALSE, "Expected false for NULL");
910
911 str = "foo";
912 res = pr_str_is_fnmatch(str);
913 ck_assert_msg(res == FALSE, "Expected false for string '%s'", str);
914
915 str = "foo?";
916 res = pr_str_is_fnmatch(str);
917 ck_assert_msg(res == TRUE, "Expected true for string '%s'", str);
918
919 str = "foo*";
920 res = pr_str_is_fnmatch(str);
921 ck_assert_msg(res == TRUE, "Expected true for string '%s'", str);
922
923 str = "foo[";
924 res = pr_str_is_fnmatch(str);
925 ck_assert_msg(res == FALSE, "Expected false for string '%s'", str);
926
927 str = "foo]";
928 res = pr_str_is_fnmatch(str);
929 ck_assert_msg(res == FALSE, "Expected false for string '%s'", str);
930
931 str = "foo[]";
932 res = pr_str_is_fnmatch(str);
933 ck_assert_msg(res == TRUE, "Expected true for string '%s'", str);
934
935 /* Now the fun cases using the escape character. */
936
937 str = "f\\oo";
938 res = pr_str_is_fnmatch(str);
939 ck_assert_msg(res == FALSE, "Expected false for string '%s'", str);
940
941 str = "foo\\";
942 res = pr_str_is_fnmatch(str);
943 ck_assert_msg(res == FALSE, "Expected false for string '%s'", str);
944
945 str = "foo\\?";
946 res = pr_str_is_fnmatch(str);
947 ck_assert_msg(res == FALSE, "Expected false for string '%s'", str);
948
949 str = "foo\\??";
950 res = pr_str_is_fnmatch(str);
951 ck_assert_msg(res == TRUE, "Expected true for string '%s'", str);
952 }
953 END_TEST
954
955 START_TEST (get_nbytes_test) {
956 char *str, *units;
957 off_t nbytes;
958 int res;
959
960 res = pr_str_get_nbytes(NULL, NULL, NULL);
961 ck_assert_msg(res == -1, "Failed to handle null arguments");
962 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
963
964 str = NULL;
965 res = pr_str_get_nbytes(str, NULL, NULL);
966 ck_assert_msg(res == -1, "Failed to handle null str argument");
967 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
968
969 str = "1";
970 units = "f";
971 res = pr_str_get_nbytes(str, units, NULL);
972 ck_assert_msg(res == -1, "Failed to handle bad suffix argument");
973 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
974
975 str = "a";
976 units = "";
977 res = pr_str_get_nbytes(str, units, NULL);
978 ck_assert_msg(res == -1, "Failed to handle invalid str argument");
979 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
980
981 str = "1 1";
982 units = "";
983 res = pr_str_get_nbytes(str, units, NULL);
984 ck_assert_msg(res == -1, "Failed to handle invalid str argument");
985 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
986
987 str = "1.1";
988 units = "";
989 res = pr_str_get_nbytes(str, units, NULL);
990 ck_assert_msg(res == -1, "Failed to handle invalid str argument");
991 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
992
993 str = "-1";
994 units = "";
995 res = pr_str_get_nbytes(str, units, NULL);
996 ck_assert_msg(res == -1, "Failed to handle invalid str argument");
997 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
998
999 /* XXX Test good suffix: B, KB, MB, GB, TB */
1000
1001 str = "1";
1002 units = "";
1003 res = pr_str_get_nbytes(str, units, &nbytes);
1004 ck_assert_msg(res == 0, "Expected result 0, got %d: %s", res, strerror(errno));
1005 ck_assert_msg(nbytes == 1, "Expected nbytes = 1, got %" PR_LU,
1006 (pr_off_t) nbytes);
1007
1008 str = "1";
1009 units = "B";
1010 res = pr_str_get_nbytes(str, units, &nbytes);
1011 ck_assert_msg(res == 0, "Expected result 0, got %d: %s", res, strerror(errno));
1012 ck_assert_msg(nbytes == 1,
1013 "Expected nbytes = 1, got %" PR_LU, (pr_off_t) nbytes);
1014
1015 str = "1";
1016 units = "KB";
1017 res = pr_str_get_nbytes(str, units, &nbytes);
1018 ck_assert_msg(res == 0, "Expected result 0, got %d: %s", res, strerror(errno));
1019 ck_assert_msg(nbytes == 1024UL,
1020 "Expected nbytes = 1024, got %" PR_LU, (pr_off_t) nbytes);
1021
1022 str = "1";
1023 units = "MB";
1024 res = pr_str_get_nbytes(str, units, &nbytes);
1025 ck_assert_msg(res == 0, "Expected result 0, got %d: %s", res, strerror(errno));
1026 ck_assert_msg(nbytes == 1048576UL,
1027 "Expected nbytes = 1048576, got %" PR_LU, (pr_off_t) nbytes);
1028
1029 str = "1";
1030 units = "GB";
1031 res = pr_str_get_nbytes(str, units, &nbytes);
1032 ck_assert_msg(res == 0, "Expected result 0, got %d: %s", res, strerror(errno));
1033 ck_assert_msg(nbytes == 1073741824UL,
1034 "Expected nbytes = 1073741824, got %" PR_LU, (pr_off_t) nbytes);
1035
1036 str = "1";
1037 units = "TB";
1038 res = pr_str_get_nbytes(str, units, &nbytes);
1039 ck_assert_msg(res == 0, "Expected result 0, got %d: %s", res, strerror(errno));
1040 ck_assert_msg(nbytes == 1099511627776UL,
1041 "Expected nbytes = 1099511627776, got %" PR_LU, (pr_off_t) nbytes);
1042
1043 /* This should definitely trigger the ERANGE error. */
1044 str = "1099511627776";
1045 units = "TB";
1046 res = pr_str_get_nbytes(str, units, &nbytes);
1047 ck_assert_msg(res == -1, "Expected ERANGE failure, succeeded unexpectedly");
1048 ck_assert_msg(errno == ERANGE, "Expected %s [%d], got %s [%d]",
1049 strerror(ERANGE), ERANGE, strerror(errno), errno);
1050 }
1051 END_TEST
1052
1053 START_TEST (get_duration_test) {
1054 char *str;
1055 int duration, expected;
1056 int res;
1057
1058 res = pr_str_get_duration(NULL, NULL);
1059 ck_assert_msg(res == -1, "Failed to handle null arguments");
1060 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1061
1062 str = "";
1063 res = pr_str_get_duration(str, NULL);
1064 ck_assert_msg(res == -1,
1065 "Failed to handle badly formatted string '%s'", str);
1066 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1067
1068 str = "-1:-1:-1";
1069 res = pr_str_get_duration(str, NULL);
1070 ck_assert_msg(res == -1,
1071 "Failed to handle badly formatted string '%s'", str);
1072 ck_assert_msg(errno == ERANGE, "Failed to set errno to ERANGE");
1073
1074 str = "a:b:c";
1075 res = pr_str_get_duration(str, NULL);
1076 ck_assert_msg(res == -1,
1077 "Failed to handle badly formatted string '%s'", str);
1078 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1079
1080 str = "111:222:333";
1081 res = pr_str_get_duration(str, NULL);
1082 ck_assert_msg(res == -1,
1083 "Failed to handle badly formatted string '%s'", str);
1084 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1085
1086 /* Test well-formatted hh::mm::ss strings. */
1087
1088 str = "00:00:00";
1089 expected = 0;
1090 res = pr_str_get_duration(str, &duration);
1091 ck_assert_msg(res == 0,
1092 "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1093 ck_assert_msg(duration == expected,
1094 "Expected duration %d secs, got %d", expected, duration);
1095
1096 str = "01:02:03";
1097 expected = 3723;
1098 res = pr_str_get_duration(str, &duration);
1099 ck_assert_msg(res == 0,
1100 "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1101 ck_assert_msg(duration == expected,
1102 "Expected duration %d secs, got %d", expected, duration);
1103
1104 str = "99:99:99";
1105 expected = 362439;
1106 res = pr_str_get_duration(str, &duration);
1107 ck_assert_msg(res == 0,
1108 "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1109 ck_assert_msg(duration == expected,
1110 "Expected duration %d secs, got %d", expected, duration);
1111
1112 /* Test bad suffixes: -1h, -1hr, 9999foo, etc */
1113
1114 str = "-1h";
1115 res = pr_str_get_duration(str, NULL);
1116 ck_assert_msg(res == -1,
1117 "Failed to handle badly formatted suffix string '%s'", str);
1118 ck_assert_msg(errno == ERANGE, "Failed to set errno to ERANGE");
1119
1120 str = "-1hr";
1121 res = pr_str_get_duration(str, NULL);
1122 ck_assert_msg(res == -1,
1123 "Failed to handle badly formatted suffix string '%s'", str);
1124 ck_assert_msg(errno == ERANGE, "Failed to set errno to ERANGE");
1125
1126 str = "99foo";
1127 res = pr_str_get_duration(str, NULL);
1128 ck_assert_msg(res == -1,
1129 "Failed to handle badly formatted suffix string '%s'", str);
1130 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1131
1132 str = "foo";
1133 res = pr_str_get_duration(str, NULL);
1134 ck_assert_msg(res == -1,
1135 "Failed to handle badly formatted suffix string '%s'", str);
1136 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1137
1138 /* Test good suffices: "H"/"h"/"hr", "M"/"m"/"min", "S"/"s"/"sec" */
1139
1140 str = "76H";
1141 expected = 273600;
1142 res = pr_str_get_duration(str, &duration);
1143 ck_assert_msg(res == 0,
1144 "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1145 ck_assert_msg(duration == expected,
1146 "Expected duration %d secs, got %d", expected, duration);
1147
1148 str = "76h";
1149 expected = 273600;
1150 res = pr_str_get_duration(str, &duration);
1151 ck_assert_msg(res == 0,
1152 "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1153 ck_assert_msg(duration == expected,
1154 "Expected duration %d secs, got %d", expected, duration);
1155
1156 str = "76Hr";
1157 expected = 273600;
1158 res = pr_str_get_duration(str, &duration);
1159 ck_assert_msg(res == 0,
1160 "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1161 ck_assert_msg(duration == expected,
1162 "Expected duration %d secs, got %d", expected, duration);
1163
1164 str = "888M";
1165 expected = 53280;
1166 res = pr_str_get_duration(str, &duration);
1167 ck_assert_msg(res == 0,
1168 "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1169 ck_assert_msg(duration == expected,
1170 "Expected duration %d secs, got %d", expected, duration);
1171
1172 str = "888m";
1173 expected = 53280;
1174 res = pr_str_get_duration(str, &duration);
1175 ck_assert_msg(res == 0,
1176 "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1177 ck_assert_msg(duration == expected,
1178 "Expected duration %d secs, got %d", expected, duration);
1179
1180 str = "888MiN";
1181 expected = 53280;
1182 res = pr_str_get_duration(str, &duration);
1183 ck_assert_msg(res == 0,
1184 "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1185 ck_assert_msg(duration == expected,
1186 "Expected duration %d secs, got %d", expected, duration);
1187
1188 str = "999S";
1189 expected = 999;
1190 res = pr_str_get_duration(str, &duration);
1191 ck_assert_msg(res == 0,
1192 "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1193 ck_assert_msg(duration == expected,
1194 "Expected duration %d secs, got %d", expected, duration);
1195
1196 str = "999s";
1197 expected = 999;
1198 res = pr_str_get_duration(str, &duration);
1199 ck_assert_msg(res == 0,
1200 "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1201 ck_assert_msg(duration == expected,
1202 "Expected duration %d secs, got %d", expected, duration);
1203
1204 str = "999sEc";
1205 expected = 999;
1206 res = pr_str_get_duration(str, &duration);
1207 ck_assert_msg(res == 0,
1208 "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1209 ck_assert_msg(duration == expected,
1210 "Expected duration %d secs, got %d", expected, duration);
1211
1212 str = "0h";
1213 expected = 0;
1214 res = pr_str_get_duration(str, &duration);
1215 ck_assert_msg(res == 0,
1216 "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1217 ck_assert_msg(duration == expected,
1218 "Expected duration %d secs, got %d", expected, duration);
1219
1220 str = "0M";
1221 expected = 0;
1222 res = pr_str_get_duration(str, &duration);
1223 ck_assert_msg(res == 0,
1224 "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1225 ck_assert_msg(duration == expected,
1226 "Expected duration %d secs, got %d", expected, duration);
1227
1228 str = "0sec";
1229 expected = 0;
1230 res = pr_str_get_duration(str, &duration);
1231 ck_assert_msg(res == 0,
1232 "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1233 ck_assert_msg(duration == expected,
1234 "Expected duration %d secs, got %d", expected, duration);
1235
1236 str = "17";
1237 expected = 17;
1238 res = pr_str_get_duration(str, &duration);
1239 ck_assert_msg(res == 0,
1240 "Failed to parse well-formed time string '%s': %s", str, strerror(errno));
1241 ck_assert_msg(duration == expected,
1242 "Expected duration %d secs, got %d", expected, duration);
1243
1244 str = "-1";
1245 res = pr_str_get_duration(str, NULL);
1246 ck_assert_msg(res == -1,
1247 "Failed to handle badly formatted suffix string '%s'", str);
1248 ck_assert_msg(errno == ERANGE, "Failed to set errno to ERANGE");
1249 }
1250 END_TEST
1251
1252 START_TEST (strnrstr_test) {
1253 int res, flags = 0;
1254 const char *s = NULL, *suffix = NULL;
1255
1256 res = pr_strnrstr(NULL, 0, NULL, 0, flags);
1257 ck_assert_msg(res == -1, "Failed to handle null arguments");
1258 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1259
1260 res = pr_strnrstr(NULL, 0, "", 0, flags);
1261 ck_assert_msg(res == -1, "Failed to handle null s");
1262 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1263
1264 res = pr_strnrstr("", 0, NULL, 0, flags);
1265 ck_assert_msg(res == -1, "Failed to handle null suffix");
1266 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
1267
1268 s = suffix = "";
1269 res = pr_strnrstr(s, 0, suffix, 0, flags);
1270 ck_assert_msg(res == TRUE, "Expected true, got false");
1271
1272 s = "";
1273 suffix = "s";
1274 res = pr_strnrstr(s, 0, suffix, 0, flags);
1275 ck_assert_msg(res == FALSE, "Expected false, got true");
1276
1277 s = "food";
1278 suffix = "ood";
1279 res = pr_strnrstr(s, 0, suffix, 0, flags);
1280 ck_assert_msg(res == TRUE, "Expected true, got false");
1281
1282 s = "food";
1283 suffix = "ood";
1284 res = pr_strnrstr(s, 4, suffix, 3, flags);
1285 ck_assert_msg(res == TRUE, "Expected true, got false");
1286
1287 s = "FOOD";
1288 suffix = "ood";
1289 res = pr_strnrstr(s, 4, suffix, 3, flags);
1290 ck_assert_msg(res == FALSE, "Expected false, got true");
1291
1292 flags = PR_STR_FL_IGNORE_CASE;
1293 s = "FOOD";
1294 suffix = "ood";
1295 res = pr_strnrstr(s, 4, suffix, 3, flags);
1296 ck_assert_msg(res == TRUE, "Expected true, got false");
1297 }
1298 END_TEST
1299
1300 START_TEST (bin2hex_test) {
1301 char *expected, *res;
1302 const unsigned char *str;
1303
1304 res = pr_str_bin2hex(NULL, NULL, 0, 0);
1305 ck_assert_msg(res == NULL, "Failed to handle null arguments");
1306 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1307 strerror(errno), errno);
1308
1309 res = pr_str_bin2hex(p, NULL, 0, 0);
1310 ck_assert_msg(res == NULL, "Failed to handle null data argument");
1311 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1312 strerror(errno), errno);
1313
1314 /* Empty string. */
1315 str = (const unsigned char *) "foobar";
1316 expected = "";
1317 res = pr_str_bin2hex(p, (const unsigned char *) str, 0, 0);
1318 ck_assert_msg(res != NULL, "Failed to hexify '%s': %s", str, strerror(errno));
1319 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'",
1320 expected, res);
1321
1322 /* default (lowercase) */
1323 expected = "666f6f626172";
1324 res = pr_str_bin2hex(p, str, strlen((char *) str), 0);
1325 ck_assert_msg(res != NULL, "Failed to hexify '%s': %s", str, strerror(errno));
1326 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'",
1327 expected, res);
1328
1329 /* lowercase */
1330 expected = "666f6f626172";
1331 res = pr_str_bin2hex(p, str, strlen((char *) str), 0);
1332 ck_assert_msg(res != NULL, "Failed to hexify '%s': %s", str, strerror(errno));
1333 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'",
1334 expected, res);
1335
1336 /* uppercase */
1337 expected = "666F6F626172";
1338 res = pr_str_bin2hex(p, str, strlen((char *) str), PR_STR_FL_HEX_USE_UC);
1339 ck_assert_msg(res != NULL, "Failed to hexify '%s': %s", str, strerror(errno));
1340 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'",
1341 expected, res);
1342 }
1343 END_TEST
1344
1345 START_TEST (hex2bin_test) {
1346 unsigned char *expected, *res;
1347 const unsigned char *hex;
1348 size_t expected_len, hex_len, len;
1349
1350 res = pr_str_hex2bin(NULL, NULL, 0, NULL);
1351 ck_assert_msg(res == NULL, "Failed to handle null arguments");
1352 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1353 strerror(errno), errno);
1354
1355 res = pr_str_hex2bin(p, NULL, 0, 0);
1356 ck_assert_msg(res == NULL, "Failed to handle null data argument");
1357 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1358 strerror(errno), errno);
1359
1360 /* Empty string. */
1361 hex = (const unsigned char *) "";
1362 hex_len = strlen((char *) hex);
1363 expected = (unsigned char *) "";
1364 res = pr_str_hex2bin(p, hex, hex_len, &len);
1365 ck_assert_msg(res != NULL, "Failed to unhexify '%s': %s", hex, strerror(errno));
1366 ck_assert_msg(strcmp((const char *) res, (const char *) expected) == 0,
1367 "Expected '%s', got '%s'", expected, res);
1368
1369 hex = (const unsigned char *) "112233";
1370 hex_len = strlen((char *) hex);
1371 expected_len = 3;
1372 expected = palloc(p, expected_len);
1373 expected[0] = 17;
1374 expected[1] = 34;
1375 expected[2] = 51;
1376
1377 res = pr_str_hex2bin(p, (const unsigned char *) hex, hex_len, &len);
1378 ck_assert_msg(res != NULL, "Failed to unhexify '%s': %s", hex, strerror(errno));
1379 ck_assert_msg(len == expected_len, "Expected len %lu, got %lu",
1380 (unsigned long) expected_len, (unsigned long) len);
1381 ck_assert_msg(memcmp(res, expected, len) == 0,
1382 "Did not receive expected unhexified data");
1383
1384 /* lowercase */
1385 hex = (const unsigned char *) "666f6f626172";
1386 hex_len = strlen((char *) hex);
1387 expected_len = 6;
1388 expected = palloc(p, expected_len);
1389 expected[0] = 'f';
1390 expected[1] = 'o';
1391 expected[2] = 'o';
1392 expected[3] = 'b';
1393 expected[4] = 'a';
1394 expected[5] = 'r';
1395
1396 res = pr_str_hex2bin(p, (const unsigned char *) hex, hex_len, &len);
1397 ck_assert_msg(res != NULL, "Failed to unhexify '%s': %s", hex, strerror(errno));
1398 ck_assert_msg(len == expected_len, "Expected len %lu, got %lu",
1399 (unsigned long) expected_len, (unsigned long) len);
1400 ck_assert_msg(memcmp(res, expected, len) == 0,
1401 "Did not receive expected unhexified data");
1402
1403 /* uppercase */
1404 hex = (const unsigned char *) "666F6F626172";
1405 hex_len = strlen((char *) hex);
1406
1407 res = pr_str_hex2bin(p, (const unsigned char *) hex, hex_len, &len);
1408 ck_assert_msg(res != NULL, "Failed to unhexify '%s': %s", hex, strerror(errno));
1409 ck_assert_msg(len == expected_len, "Expected len %lu, got %lu",
1410 (unsigned long) expected_len, (unsigned long) len);
1411 ck_assert_msg(memcmp(res, expected, len) == 0,
1412 "Did not receive expected unhexified data");
1413
1414 /* Handle known not-hex data properly. */
1415 hex = (const unsigned char *) "Hello, World!\n";
1416 hex_len = strlen((char *) hex);
1417 res = pr_str_hex2bin(p, hex, hex_len, &len);
1418 ck_assert_msg(res == NULL, "Successfully unhexified '%s' unexpectedly", hex);
1419 ck_assert_msg(errno == ERANGE, "Expected ERANGE (%d), got %s (%d)", ERANGE,
1420 strerror(errno), errno);
1421 }
1422 END_TEST
1423
1424 START_TEST (levenshtein_test) {
1425 int res, expected, flags = 0;
1426 const char *a, *b;
1427
1428 mark_point();
1429 res = pr_str_levenshtein(NULL, NULL, NULL, 0, 0, 0, 0, flags);
1430 ck_assert_msg(res < 0, "Failed to handle null pool");
1431 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1432 strerror(errno), errno);
1433
1434 mark_point();
1435 res = pr_str_levenshtein(p, NULL, NULL, 0, 0, 0, 0, flags);
1436 ck_assert_msg(res < 0, "Failed to handle null a string");
1437 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1438 strerror(errno), errno);
1439
1440 a = "foo";
1441
1442 mark_point();
1443 res = pr_str_levenshtein(p, a, NULL, 0, 0, 0, 0, flags);
1444 ck_assert_msg(res < 0, "Failed to handle null b string");
1445 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1446 strerror(errno), errno);
1447
1448 expected = 0;
1449 b = "Foo";
1450
1451 mark_point();
1452 res = pr_str_levenshtein(p, a, b, 0, 0, 0, 0, flags);
1453 ck_assert_msg(res >= 0,
1454 "Failed to compute Levenshtein distance from '%s' to '%s': %s", a, b,
1455 strerror(errno));
1456 ck_assert_msg(expected == res, "Expected distance %d, got %d", expected, res);
1457
1458 expected = 3;
1459 b = "Foo";
1460 res = pr_str_levenshtein(p, a, b, 0, 1, 1, 1, flags);
1461 ck_assert_msg(res >= 0,
1462 "Failed to compute Levenshtein distance from '%s' to '%s': %s", a, b,
1463 strerror(errno));
1464 ck_assert_msg(expected == res, "Expected distance %d, got %d", expected, res);
1465
1466 flags = PR_STR_FL_IGNORE_CASE;
1467 expected = 2;
1468 b = "Foo";
1469 res = pr_str_levenshtein(p, a, b, 0, 1, 1, 1, flags);
1470 ck_assert_msg(res >= 0,
1471 "Failed to compute Levenshtein distance from '%s' to '%s': %s", a, b,
1472 strerror(errno));
1473 ck_assert_msg(expected == res, "Expected distance %d, got %d", expected, res);
1474 }
1475 END_TEST
1476
1477 START_TEST (similars_test) {
1478 array_header *res, *candidates;
1479 const char *s, **similars, *expected;
1480
1481 mark_point();
1482 res = pr_str_get_similars(NULL, NULL, NULL, 0, 0);
1483 ck_assert_msg(res == NULL, "Failed to handle null pool");
1484 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1485 strerror(errno), errno);
1486
1487 mark_point();
1488 res = pr_str_get_similars(p, NULL, NULL, 0, 0);
1489 ck_assert_msg(res == NULL, "Failed to handle null string");
1490 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1491 strerror(errno), errno);
1492
1493 s = "foo";
1494
1495 mark_point();
1496 res = pr_str_get_similars(p, s, NULL, 0, 0);
1497 ck_assert_msg(res == NULL, "Failed to handle null candidates");
1498 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1499 strerror(errno), errno);
1500
1501 candidates = make_array(p, 5, sizeof(const char *));
1502
1503 mark_point();
1504 res = pr_str_get_similars(p, s, candidates, 0, 0);
1505 ck_assert_msg(res == NULL, "Failed to handle empty candidates");
1506 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
1507 strerror(errno), errno);
1508
1509 *((const char **) push_array(candidates)) = pstrdup(p, "fools");
1510 *((const char **) push_array(candidates)) = pstrdup(p, "odd");
1511 *((const char **) push_array(candidates)) = pstrdup(p, "bar");
1512 *((const char **) push_array(candidates)) = pstrdup(p, "FOO");
1513
1514 mark_point();
1515 res = pr_str_get_similars(p, s, candidates, 0, 0);
1516 ck_assert_msg(res != NULL, "Failed to find similar strings to '%s': %s", s,
1517 strerror(errno));
1518 ck_assert_msg(res->nelts > 0, "Expected >0 similar strings, got %u",
1519 res->nelts);
1520
1521 mark_point();
1522 similars = (const char **) res->elts;
1523
1524 /*
1525 * Note: expected distances are as follows:
1526 *
1527 * Candidate Case-Sensitive Case-Insensitive
1528 * fools 0 0
1529 * odd 5 5
1530 * bar 5 5
1531 * FOO 5 0
1532 */
1533
1534 expected = "fools";
1535
1536 ck_assert_msg(strcmp(similars[0], expected) == 0,
1537 "Expected similar '%s', got '%s'", expected, similars[0]);
1538
1539 ck_assert_msg(strcmp(similars[1], expected) != 0,
1540 "Unexpectedly got similar '%s'", similars[1]);
1541
1542 mark_point();
1543 res = pr_str_get_similars(p, s, candidates, 0, PR_STR_FL_IGNORE_CASE);
1544 ck_assert_msg(res != NULL, "Failed to find similar strings to '%s': %s", s,
1545 strerror(errno));
1546 ck_assert_msg(res->nelts > 0, "Expected >0 similar strings, got %u",
1547 res->nelts);
1548
1549 mark_point();
1550 similars = (const char **) res->elts;
1551
1552 /*
1553 * similars[0] and similars[1] should be "FOO" and "fools", but
1554 * not necessarily in that order
1555 */
1556 expected = "FOO";
1557 if (strcmp(similars[0], expected) != 0) {
1558 expected = similars[0];
1559 similars[0] = similars[1];
1560 similars[1] = expected;
1561 expected = "FOO";
1562 }
1563
1564 ck_assert_msg(strcmp(similars[0], expected) == 0,
1565 "Expected similar '%s', got '%s'", expected, similars[0]);
1566
1567 expected = "fools";
1568
1569 ck_assert_msg(strcmp(similars[1], expected) == 0,
1570 "Expected similar '%s', got '%s'", expected, similars[1]);
1571 }
1572 END_TEST
1573
1574 START_TEST (str2uid_test) {
1575 int res;
1576
1577 res = pr_str2uid(NULL, NULL);
1578 ck_assert_msg(res == -1, "Failed to handle null arguments");
1579 }
1580 END_TEST
1581
1582 START_TEST (str2gid_test) {
1583 int res;
1584
1585 res = pr_str2gid(NULL, NULL);
1586 ck_assert_msg(res == -1, "Failed to handle null arguments");
1587 }
1588 END_TEST
1589
1590 START_TEST (uid2str_test) {
1591 const char *res;
1592
1593 res = pr_uid2str(NULL, (uid_t) 1);
1594 ck_assert_msg(strcmp(res, "1") == 0, "Expected '1', got '%s'", res);
1595
1596 res = pr_uid2str(NULL, (uid_t) -1);
1597 ck_assert_msg(strcmp(res, "-1") == 0, "Expected '-1', got '%s'", res);
1598 }
1599 END_TEST
1600
1601 START_TEST (gid2str_test) {
1602 const char *res;
1603
1604 res = pr_gid2str(NULL, (gid_t) 1);
1605 ck_assert_msg(strcmp(res, "1") == 0, "Expected '1', got '%s'", res);
1606
1607 res = pr_gid2str(NULL, (gid_t) -1);
1608 ck_assert_msg(strcmp(res, "-1") == 0, "Expected '-1', got '%s'", res);
1609 }
1610 END_TEST
1611
1612 START_TEST (str_quote_test) {
1613 const char *res;
1614 char *expected, *path;
1615
1616 res = pr_str_quote(NULL, NULL);
1617 ck_assert_msg(res == NULL, "Failed to handle null arguments");
1618 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1619 strerror(errno), errno);
1620
1621 res = pr_str_quote(p, NULL);
1622 ck_assert_msg(res == NULL, "Failed to handle null path argument");
1623 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1624 strerror(errno), errno);
1625
1626 path = "/tmp/";
1627 expected = path;
1628 res = pr_str_quote(p, path);
1629 ck_assert_msg(res != NULL, "Failed to quote '%s': %s", path, strerror(errno));
1630 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1631 res);
1632
1633 path = "/\"tmp\"/";
1634 expected = "/\"\"tmp\"\"/";
1635 res = pr_str_quote(p, path);
1636 ck_assert_msg(res != NULL, "Failed to quote '%s': %s", path, strerror(errno));
1637 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1638 res);
1639 }
1640 END_TEST
1641
1642 START_TEST (quote_dir_test) {
1643 const char *res;
1644 char *expected, *path;
1645
1646 res = quote_dir(NULL, NULL);
1647 ck_assert_msg(res == NULL, "Failed to handle null arguments");
1648 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1649 strerror(errno), errno);
1650
1651 res = quote_dir(p, NULL);
1652 ck_assert_msg(res == NULL, "Failed to handle null path argument");
1653 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1654 strerror(errno), errno);
1655
1656 path = "/tmp/";
1657 expected = path;
1658 res = quote_dir(p, path);
1659 ck_assert_msg(res != NULL, "Failed to quote '%s': %s", path, strerror(errno));
1660 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1661 res);
1662
1663 path = "/\"tmp\"/";
1664 expected = "/\"\"tmp\"\"/";
1665 res = quote_dir(p, path);
1666 ck_assert_msg(res != NULL, "Failed to quote '%s': %s", path, strerror(errno));
1667 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1668 res);
1669 }
1670 END_TEST
1671
1672 START_TEST (text_to_array_test) {
1673 register unsigned int i;
1674 array_header *res;
1675 const char *text, *elt;
1676
1677 mark_point();
1678 res = pr_str_text_to_array(NULL, NULL, ',');
1679 ck_assert_msg(res == NULL, "Failed to handle null pool");
1680 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1681 strerror(errno), errno);
1682
1683 mark_point();
1684 res = pr_str_text_to_array(p, NULL, ',');
1685 ck_assert_msg(res == NULL, "Failed to handle null text");
1686 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1687 strerror(errno), errno);
1688
1689 mark_point();
1690 text = "";
1691 res = pr_str_text_to_array(p, text, ',');
1692 ck_assert_msg(res != NULL, "Failed to handle text '%s': %s", text,
1693 strerror(errno));
1694 ck_assert_msg(res->nelts == 0, "Expected 0 items, got %u", res->nelts);
1695
1696 mark_point();
1697 text = ",";
1698 res = pr_str_text_to_array(p, text, ',');
1699 ck_assert_msg(res != NULL, "Failed to handle text '%s': %s", text,
1700 strerror(errno));
1701 ck_assert_msg(res->nelts == 0, "Expected 0 items, got %u", res->nelts);
1702
1703 mark_point();
1704 text = ",,,";
1705 res = pr_str_text_to_array(p, text, ',');
1706 ck_assert_msg(res != NULL, "Failed to handle text '%s': %s", text,
1707 strerror(errno));
1708 ck_assert_msg(res->nelts == 0, "Expected 0 items, got %u", res->nelts);
1709
1710 mark_point();
1711 text = "foo";
1712 res = pr_str_text_to_array(p, text, ',');
1713 ck_assert_msg(res != NULL, "Failed to handle text '%s': %s", text,
1714 strerror(errno));
1715 ck_assert_msg(res->nelts == 1, "Expected 1 item, got %u", res->nelts);
1716 elt = ((char **) res->elts)[0];
1717 ck_assert_msg(elt != NULL, "Expected non-null item");
1718 ck_assert_msg(strcmp(elt, text) == 0, "Expected '%s', got '%s'", text, elt);
1719
1720 mark_point();
1721 text = "foo,foo,foo";
1722 res = pr_str_text_to_array(p, text, ',');
1723 ck_assert_msg(res != NULL, "Failed to handle text '%s': %s", text,
1724 strerror(errno));
1725 ck_assert_msg(res->nelts == 3, "Expected 3 items, got %u", res->nelts);
1726 for (i = 0; i < res->nelts; i++) {
1727 char *item, *expected;
1728
1729 item = ((char **) res->elts)[i];
1730 ck_assert_msg(item != NULL, "Expected item at index %u, got null", i);
1731
1732 expected = "foo";
1733 ck_assert_msg(strcmp(item, expected) == 0,
1734 "Expected '%s' at index %u, got '%s'", expected, i, item);
1735 }
1736
1737 mark_point();
1738 text = "foo,foo,foo,";
1739 res = pr_str_text_to_array(p, text, ',');
1740 ck_assert_msg(res != NULL, "Failed to handle text '%s': %s", text,
1741 strerror(errno));
1742 ck_assert_msg(res->nelts == 3, "Expected 3 items, got %u", res->nelts);
1743 for (i = 0; i < res->nelts; i++) {
1744 char *item, *expected;
1745
1746 item = ((char **) res->elts)[i];
1747 ck_assert_msg(item != NULL, "Expected item at index %u, got null", i);
1748
1749 expected = "foo";
1750 ck_assert_msg(strcmp(item, expected) == 0,
1751 "Expected '%s' at index %u, got '%s'", expected, i, item);
1752 }
1753
1754 mark_point();
1755 text = "foo|foo|foo";
1756 res = pr_str_text_to_array(p, text, '|');
1757 ck_assert_msg(res != NULL, "Failed to handle text '%s': %s", text,
1758 strerror(errno));
1759 ck_assert_msg(res->nelts == 3, "Expected 3 items, got %u", res->nelts);
1760 for (i = 0; i < res->nelts; i++) {
1761 char *item, *expected;
1762
1763 item = ((char **) res->elts)[i];
1764 ck_assert_msg(item != NULL, "Expected item at index %u, got null", i);
1765
1766 expected = "foo";
1767 ck_assert_msg(strcmp(item, expected) == 0,
1768 "Expected '%s' at index %u, got '%s'", expected, i, item);
1769 }
1770
1771 /* With a leading delimiter character. */
1772 mark_point();
1773 text = "/foo/foo/foo";
1774 res = pr_str_text_to_array(p, text, '/');
1775 ck_assert_msg(res != NULL, "Failed to handle text '%s': %s", text,
1776 strerror(errno));
1777 ck_assert_msg(res->nelts == 3, "Expected 3 items, got %u", res->nelts);
1778 for (i = 0; i < res->nelts; i++) {
1779 char *item, *expected;
1780
1781 item = ((char **) res->elts)[i];
1782 ck_assert_msg(item != NULL, "Expected item at index %u, got null", i);
1783
1784 expected = "foo";
1785 ck_assert_msg(strcmp(item, expected) == 0,
1786 "Expected '%s' at index %u, got '%s'", expected, i, item);
1787 }
1788
1789 /* With a trailing delimiter character. */
1790 mark_point();
1791 text = "/foo/foo/foo/";
1792 res = pr_str_text_to_array(p, text, '/');
1793 ck_assert_msg(res != NULL, "Failed to handle text '%s': %s", text,
1794 strerror(errno));
1795 ck_assert_msg(res->nelts == 3, "Expected 3 items, got %u", res->nelts);
1796 for (i = 0; i < res->nelts; i++) {
1797 char *item, *expected;
1798
1799 item = ((char **) res->elts)[i];
1800 ck_assert_msg(item != NULL, "Expected item at index %u, got null", i);
1801
1802 expected = "foo";
1803 ck_assert_msg(strcmp(item, expected) == 0,
1804 "Expected '%s' at index %u, got '%s'", expected, i, item);
1805 }
1806
1807 /* Without leading or trailing delimiter characters. */
1808 mark_point();
1809 text = "foo/foo/foo";
1810 res = pr_str_text_to_array(p, text, '/');
1811 ck_assert_msg(res != NULL, "Failed to handle text '%s': %s", text,
1812 strerror(errno));
1813 ck_assert_msg(res->nelts == 3, "Expected 3 items, got %u", res->nelts);
1814 for (i = 0; i < res->nelts; i++) {
1815 char *item, *expected;
1816
1817 item = ((char **) res->elts)[i];
1818 ck_assert_msg(item != NULL, "Expected item at index %u, got null", i);
1819
1820 expected = "foo";
1821 ck_assert_msg(strcmp(item, expected) == 0,
1822 "Expected '%s' at index %u, got '%s'", expected, i, item);
1823 }
1824 }
1825 END_TEST
1826
1827 START_TEST (array_to_text_test) {
1828 array_header *items;
1829 const char *delimiter;
1830 char *res, *expected;
1831
1832 mark_point();
1833 res = pr_str_array_to_text(NULL, NULL, NULL);
1834 ck_assert_msg(res == NULL, "Failed to handle null pool");
1835 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1836 strerror(errno), errno);
1837
1838 mark_point();
1839 res = pr_str_array_to_text(p, NULL, NULL);
1840 ck_assert_msg(res == NULL, "Failed to handle null items");
1841 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1842 strerror(errno), errno);
1843
1844 items = make_array(p, 0, sizeof(char *));
1845
1846 mark_point();
1847 res = pr_str_array_to_text(p, items, NULL);
1848 ck_assert_msg(res == NULL, "Failed to handle null delimiter");
1849 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1850 strerror(errno), errno);
1851
1852 delimiter = ":";
1853 expected = "";
1854
1855 mark_point();
1856 res = pr_str_array_to_text(p, items, delimiter);
1857 ck_assert_msg(res != NULL, "Error converting items to text: %s",
1858 strerror(errno));
1859 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1860 res);
1861
1862 *((char **) push_array(items)) = pstrdup(p, "foo");
1863 expected = "foo";
1864
1865 mark_point();
1866 res = pr_str_array_to_text(p, items, delimiter);
1867 ck_assert_msg(res != NULL, "Error converting items to text: %s",
1868 strerror(errno));
1869 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1870 res);
1871
1872 *((char **) push_array(items)) = pstrdup(p, "bar");
1873 expected = "foo:bar";
1874
1875 mark_point();
1876 res = pr_str_array_to_text(p, items, delimiter);
1877 ck_assert_msg(res != NULL, "Error converting items to text: %s",
1878 strerror(errno));
1879 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1880 res);
1881 }
1882 END_TEST
1883
1884 Suite *tests_get_str_suite(void) {
1885 Suite *suite;
1886 TCase *testcase;
1887
1888 suite = suite_create("str");
1889
1890 testcase = tcase_create("base");
1891 tcase_add_checked_fixture(testcase, set_up, tear_down);
1892
1893 tcase_add_test(testcase, sstrncpy_test);
1894 tcase_add_test(testcase, sstrcat_test);
1895 tcase_add_test(testcase, sreplace_test);
1896 tcase_add_test(testcase, sreplace_enospc_test);
1897 tcase_add_test(testcase, sreplace_bug3614_test);
1898 tcase_add_test(testcase, str_replace_test);
1899 tcase_add_test(testcase, pdircat_test);
1900 tcase_add_test(testcase, pstrcat_test);
1901 tcase_add_test(testcase, pstrdup_test);
1902 tcase_add_test(testcase, pstrndup_test);
1903 tcase_add_test(testcase, strip_test);
1904 tcase_add_test(testcase, strip_end_test);
1905 tcase_add_test(testcase, get_token_test);
1906 tcase_add_test(testcase, get_token2_test);
1907 tcase_add_test(testcase, get_word_test);
1908 tcase_add_test(testcase, get_word_utf8_test);
1909 tcase_add_test(testcase, is_boolean_test);
1910 tcase_add_test(testcase, is_fnmatch_test);
1911 tcase_add_test(testcase, get_nbytes_test);
1912 tcase_add_test(testcase, get_duration_test);
1913 tcase_add_test(testcase, bin2hex_test);
1914 tcase_add_test(testcase, hex2bin_test);
1915 tcase_add_test(testcase, levenshtein_test);
1916 tcase_add_test(testcase, similars_test);
1917 tcase_add_test(testcase, strnrstr_test);
1918 tcase_add_test(testcase, str2uid_test);
1919 tcase_add_test(testcase, str2gid_test);
1920 tcase_add_test(testcase, uid2str_test);
1921 tcase_add_test(testcase, gid2str_test);
1922 tcase_add_test(testcase, str_quote_test);
1923 tcase_add_test(testcase, quote_dir_test);
1924 tcase_add_test(testcase, text_to_array_test);
1925 tcase_add_test(testcase, array_to_text_test);
1926
1927 suite_add_tcase(suite, testcase);
1928 return suite;
1929 }