Debian Codegraph @

tests/api/misc.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server testsuite
3 * Copyright (c) 2015-2025 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 /* Miscellaneous tests
26 */
27
28 #include "tests.h"
29
30 static pool *p = NULL;
31
32 static unsigned int schedule_called = 0;
33 static const char *misc_test_shutmsg = "/tmp/prt-shutmsg.dat";
34 static const char *misc_test_readlink = "/tmp/prt-readlink.lnk";
35 static const char *misc_test_readlink2_dir = "/tmp/prt-readlink/";
36 static const char *misc_test_readlink2 = "/tmp/prt-readlink/test.lnk";
37
38 /* Fixtures */
39
40 static void set_up(void) {
41 (void) unlink(misc_test_readlink);
42 (void) unlink(misc_test_readlink2);
43 (void) unlink(misc_test_shutmsg);
44 (void) rmdir(misc_test_readlink2_dir);
45
46 if (p == NULL) {
47 p = permanent_pool = make_sub_pool(NULL);
48 }
49
50 init_fs();
51 pr_fs_statcache_set_policy(PR_TUNABLE_FS_STATCACHE_SIZE,
52 PR_TUNABLE_FS_STATCACHE_MAX_AGE, 0);
53
54 if (getenv("TEST_VERBOSE") != NULL) {
55 pr_trace_set_levels("auth", 1, 20);
56 pr_trace_set_levels("fsio", 1, 20);
57 pr_trace_set_levels("fs.statcache", 1, 20);
58 }
59
60 schedule_called = 0;
61 session.user = NULL;
62 }
63
64 static void tear_down(void) {
65 (void) unlink(misc_test_readlink);
66 (void) unlink(misc_test_readlink2);
67 (void) unlink(misc_test_shutmsg);
68 (void) rmdir(misc_test_readlink2_dir);
69
70 pr_fs_statcache_set_policy(PR_TUNABLE_FS_STATCACHE_SIZE,
71 PR_TUNABLE_FS_STATCACHE_MAX_AGE, 0);
72
73 if (getenv("TEST_VERBOSE") != NULL) {
74 pr_trace_set_levels("auth", 0, 0);
75 pr_trace_set_levels("fsio", 0, 0);
76 pr_trace_set_levels("fs.statcache", 0, 0);
77 }
78
79 session.user = NULL;
80
81 if (p) {
82 destroy_pool(p);
83 p = session.pool = permanent_pool = NULL;
84 }
85 }
86
87 static void schedule_cb(void *arg1, void *arg2, void *arg3, void *arg4) {
88 schedule_called++;
89 }
90
91 /* Tests */
92
93 START_TEST (schedule_test) {
94 mark_point();
95 schedule(NULL, 0, NULL, NULL, NULL, NULL);
96
97 mark_point();
98 schedule(schedule_cb, -1, NULL, NULL, NULL, NULL);
99
100 mark_point();
101 run_schedule();
102
103 mark_point();
104 schedule(schedule_cb, 0, NULL, NULL, NULL, NULL);
105
106 run_schedule();
107 ck_assert_msg(schedule_called == 1, "Expected 1, got %u", schedule_called);
108
109 run_schedule();
110 ck_assert_msg(schedule_called == 1, "Expected 1, got %u", schedule_called);
111
112 mark_point();
113 schedule(schedule_cb, 0, NULL, NULL, NULL, NULL);
114 schedule(schedule_cb, 0, NULL, NULL, NULL, NULL);
115
116 run_schedule();
117 ck_assert_msg(schedule_called == 3, "Expected 3, got %u", schedule_called);
118
119 run_schedule();
120 ck_assert_msg(schedule_called == 3, "Expected 3, got %u", schedule_called);
121
122 mark_point();
123
124 /* Schedule this callback to run after 2 "loops", i.e. calls to
125 * run_schedule().
126 */
127 schedule(schedule_cb, 2, NULL, NULL, NULL, NULL);
128
129 run_schedule();
130 ck_assert_msg(schedule_called == 3, "Expected 3, got %u", schedule_called);
131
132 run_schedule();
133 ck_assert_msg(schedule_called == 3, "Expected 3, got %u", schedule_called);
134
135 run_schedule();
136 ck_assert_msg(schedule_called == 4, "Expected 4, got %u", schedule_called);
137 }
138 END_TEST
139
140 START_TEST (get_name_max_test) {
141 long res;
142 char *path;
143 int fd;
144
145 res = get_name_max(NULL, -1);
146 ck_assert_msg(res < 0, "Failed to handle invalid arguments");
147 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
148 strerror(errno), errno);
149
150 path = "/";
151 res = get_name_max(path, -1);
152 ck_assert_msg(res >= 0, "Failed to handle path '%s': %s", path, strerror(errno));
153
154 fd = 1;
155 res = get_name_max(NULL, fd);
156
157 /* It seems that fpathconf(2) on some platforms will handle stdin as a
158 * valid file descriptor, and some will not.
159 */
160 if (res < 0) {
161 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
162 strerror(errno), errno);
163 }
164 }
165 END_TEST
166
167 START_TEST (dir_interpolate_test) {
168 char *res;
169 const char *path;
170
171 mark_point();
172 res = dir_interpolate(NULL, NULL);
173 ck_assert_msg(res == NULL, "Failed to handle null arguments");
174 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
175 strerror(errno), errno);
176
177 mark_point();
178 res = dir_interpolate(p, NULL);
179 ck_assert_msg(res == NULL, "Failed to handle null path");
180 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
181 strerror(errno), errno);
182
183 mark_point();
184 path = "/foo";
185 res = dir_interpolate(p, path);
186 ck_assert_msg(path != NULL, "Failed to interpolate '%s': %s", path,
187 strerror(errno));
188 ck_assert_msg(strcmp(res, path) == 0, "Expected '%s', got '%s'", path, res);
189
190 mark_point();
191 path = "~foo.bar.baz.quxx.quzz/foo";
192 res = dir_interpolate(p, path);
193 ck_assert_msg(path != NULL, "Failed to interpolate '%s': %s", path,
194 strerror(errno));
195 ck_assert_msg(*path == '~',
196 "Interpolated path with unknown user unexpectedly");
197 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
198 strerror(errno), errno);
199
200 mark_point();
201 session.user = "foo.bar.baz.quxx.quzz";
202 session.user_homedir = "/alef.d";
203 res = dir_interpolate(p, path);
204 ck_assert_msg(res != NULL, "Failed to interpolate '%s': %s", path,
205 strerror(errno));
206 ck_assert_msg(strcmp(res, "/alef.d/foo") == 0,
207 "Expected '/alef.d/foo', got '%s'", res);
208
209 session.user = NULL;
210 session.user_homedir = NULL;
211 }
212 END_TEST
213
214 START_TEST (dir_best_path_test) {
215 char *res;
216 const char *path;
217
218 res = dir_best_path(NULL, NULL);
219 ck_assert_msg(res == NULL, "Failed to handle null arguments");
220 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
221 strerror(errno), errno);
222
223 res = dir_best_path(p, NULL);
224 ck_assert_msg(res == NULL, "Failed to handle null path");
225 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
226 strerror(errno), errno);
227
228 mark_point();
229 path = "/foo";
230 res = dir_best_path(p, path);
231 ck_assert_msg(path != NULL, "Failed to get best path for '%s': %s", path,
232 strerror(errno));
233 ck_assert_msg(strcmp(res, path) == 0, "Expected '%s', got '%s'", path, res);
234 }
235 END_TEST
236
237 START_TEST (dir_canonical_path_test) {
238 char *res;
239 const char *path;
240
241 res = dir_canonical_path(NULL, NULL);
242 ck_assert_msg(res == NULL, "Failed to handle null arguments");
243 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
244 strerror(errno), errno);
245
246 res = dir_canonical_path(p, NULL);
247 ck_assert_msg(res == NULL, "Failed to handle null path");
248 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
249 strerror(errno), errno);
250
251 mark_point();
252 path = "/foo";
253 res = dir_canonical_path(p, path);
254 ck_assert_msg(path != NULL, "Failed to get canonical path for '%s': %s", path,
255 strerror(errno));
256 ck_assert_msg(strcmp(res, path) == 0, "Expected '%s', got '%s'", path, res);
257 }
258 END_TEST
259
260 START_TEST (dir_canonical_vpath_test) {
261 char *res;
262 const char *path;
263
264 res = dir_canonical_vpath(NULL, NULL);
265 ck_assert_msg(res == NULL, "Failed to handle null arguments");
266 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
267 strerror(errno), errno);
268
269 res = dir_canonical_vpath(p, NULL);
270 ck_assert_msg(res == NULL, "Failed to handle null path");
271 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
272 strerror(errno), errno);
273
274 mark_point();
275 path = "/foo";
276 res = dir_canonical_vpath(p, path);
277 ck_assert_msg(path != NULL, "Failed to get canonical vpath for '%s': %s", path,
278 strerror(errno));
279 ck_assert_msg(strcmp(res, path) == 0, "Expected '%s', got '%s'", path, res);
280 }
281 END_TEST
282
283 START_TEST (dir_readlink_test) {
284 int res, flags = 0;
285 const char *path;
286 char *buf, *dst_path, *expected_path;
287 size_t bufsz, dst_pathlen, expected_pathlen;
288
289 (void) unlink(misc_test_readlink);
290
291 /* Parameter validation */
292 res = dir_readlink(NULL, NULL, NULL, 0, flags);
293 ck_assert_msg(res < 0, "Failed to handle null arguments");
294 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
295 strerror(errno), errno);
296
297 res = dir_readlink(p, NULL, NULL, 0, flags);
298 ck_assert_msg(res < 0, "Failed to handle null path");
299 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
300 strerror(errno), errno);
301
302 path = misc_test_readlink;
303 res = dir_readlink(p, path, NULL, 0, flags);
304 ck_assert_msg(res < 0, "Failed to handle null buffer");
305 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
306 strerror(errno), errno);
307
308 bufsz = 1024;
309 buf = palloc(p, bufsz);
310 res = dir_readlink(p, path, buf, 0, flags);
311 ck_assert_msg(res == 0, "Failed to handle zero buffer length");
312
313 res = dir_readlink(p, path, buf, bufsz, flags);
314 ck_assert_msg(res < 0, "Failed to handle nonexistent file");
315 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
316 strerror(errno), errno);
317
318 dst_path = "";
319 res = symlink(dst_path, path);
320 if (res == 0) {
321 /* Some platforms will not allow creation of empty symlinks. Nice of
322 * them.
323 */
324 res = dir_readlink(p, path, buf, bufsz, flags);
325 ck_assert_msg(res == 0, "Failed to handle empty symlink");
326 }
327 (void) unlink(path);
328
329 /* Not chrooted, absolute dst path */
330 memset(buf, '\0', bufsz);
331 dst_path = "/home/user/file.dat";
332 dst_pathlen = strlen(dst_path);
333 res = symlink(dst_path, path);
334 ck_assert_msg(res == 0, "Failed to symlink '%s' to '%s': %s", path, dst_path,
335 strerror(errno));
336
337 res = dir_readlink(p, path, buf, bufsz, flags);
338 ck_assert_msg(res >= 0, "Failed to read '%s' symlink: %s", path, strerror(errno));
339 ck_assert_msg((size_t) res == dst_pathlen, "Expected length %lu, got %d",
340 (unsigned long) dst_pathlen, res);
341 ck_assert_msg(strcmp(buf, dst_path) == 0, "Expected '%s', got '%s'",
342 dst_path, buf);
343
344 /* Not chrooted, relative dst path, flags to ignore rel path */
345 memset(buf, '\0', bufsz);
346 dst_path = "./file.dat";
347 dst_pathlen = strlen(dst_path);
348
349 (void) unlink(path);
350 res = symlink(dst_path, path);
351 ck_assert_msg(res == 0, "Failed to symlink '%s' to '%s': %s", path, dst_path,
352 strerror(errno));
353
354 res = dir_readlink(p, path, buf, bufsz, flags);
355 ck_assert_msg(res >= 0, "Failed to read '%s' symlink: %s", path, strerror(errno));
356 ck_assert_msg((size_t) res == dst_pathlen, "Expected length %lu, got %d",
357 (unsigned long) dst_pathlen, res);
358 ck_assert_msg(strcmp(buf, dst_path) == 0, "Expected '%s', got '%s'",
359 dst_path, buf);
360
361 /* Not chrooted, relative dst path without leading '.', flags to ignore rel
362 * path.
363 */
364 memset(buf, '\0', bufsz);
365 dst_path = "file.dat";
366 dst_pathlen = strlen(dst_path);
367
368 (void) unlink(path);
369 res = symlink(dst_path, path);
370 ck_assert_msg(res == 0, "Failed to symlink '%s' to '%s': %s", path, dst_path,
371 strerror(errno));
372
373 res = dir_readlink(p, path, buf, bufsz, flags);
374 ck_assert_msg(res >= 0, "Failed to read '%s' symlink: %s", path, strerror(errno));
375 ck_assert_msg((size_t) res == dst_pathlen, "Expected length %lu, got %d",
376 (unsigned long) dst_pathlen, res);
377 ck_assert_msg(strcmp(buf, dst_path) == 0, "Expected '%s', got '%s'",
378 dst_path, buf);
379
380 /* Not chrooted, relative dst path, flags to HANDLE rel path */
381 memset(buf, '\0', bufsz);
382 dst_path = "./file.dat";
383 dst_pathlen = strlen(dst_path);
384 expected_path = "/tmp/file.dat";
385 expected_pathlen = strlen(expected_path);
386
387 (void) unlink(path);
388 res = symlink(dst_path, path);
389 ck_assert_msg(res == 0, "Failed to symlink '%s' to '%s': %s", path, dst_path,
390 strerror(errno));
391
392 flags = PR_DIR_READLINK_FL_HANDLE_REL_PATH;
393 res = dir_readlink(p, path, buf, bufsz, flags);
394 ck_assert_msg(res >= 0, "Failed to read '%s' symlink: %s", path, strerror(errno));
395 ck_assert_msg((size_t) res == expected_pathlen, "Expected length %lu, got %d",
396 (unsigned long) expected_pathlen, res);
397 ck_assert_msg(strcmp(buf, expected_path) == 0, "Expected '%s', got '%s'",
398 expected_path, buf);
399
400 /* Not chrooted, relative dst path without leading '.', flags to HANDLE rel
401 * path.
402 */
403 memset(buf, '\0', bufsz);
404 dst_path = "file.dat";
405 dst_pathlen = strlen(dst_path);
406 expected_path = "/tmp/file.dat";
407 expected_pathlen = strlen(expected_path);
408
409 (void) unlink(path);
410 res = symlink(dst_path, path);
411 ck_assert_msg(res == 0, "Failed to symlink '%s' to '%s': %s", path, dst_path,
412 strerror(errno));
413
414 flags = PR_DIR_READLINK_FL_HANDLE_REL_PATH;
415 res = dir_readlink(p, path, buf, bufsz, flags);
416 ck_assert_msg(res >= 0, "Failed to read '%s' symlink: %s", path, strerror(errno));
417 ck_assert_msg((size_t) res == expected_pathlen, "Expected length %lu, got %d",
418 (unsigned long) expected_pathlen, res);
419 ck_assert_msg(strcmp(buf, expected_path) == 0, "Expected '%s', got '%s'",
420 expected_path, buf);
421
422 /* Not chrooted, dst path longer than given buffer */
423 flags = 0;
424 memset(buf, '\0', bufsz);
425 res = dir_readlink(p, path, buf, 2, flags);
426 ck_assert_msg(res >= 0, "Failed to read '%s' symlink: %s", path, strerror(errno));
427 ck_assert_msg(res == 2, "Expected length 2, got %d", res);
428 ck_assert_msg(strncmp(buf, dst_path, 2) == 0, "Expected '%.*s', got '%.*s'",
429 2, dst_path, 2, buf);
430
431 /* Chrooted to "/" */
432 session.chroot_path = "/";
433 memset(buf, '\0', bufsz);
434 res = dir_readlink(p, path, buf, bufsz, flags);
435 ck_assert_msg(res >= 0, "Failed to read '%s' symlink: %s", path, strerror(errno));
436 ck_assert_msg((size_t) res == dst_pathlen, "Expected length %lu, got %d",
437 (unsigned long) dst_pathlen, res);
438 ck_assert_msg(strcmp(buf, dst_path) == 0, "Expected '%s', got '%s'",
439 dst_path, buf);
440
441 /* Chrooted, absolute destination path shorter than chroot path */
442 session.chroot_path = "/home/user";
443 memset(buf, '\0', bufsz);
444 dst_path = "/foo";
445 dst_pathlen = strlen(dst_path);
446
447 (void) unlink(path);
448 res = symlink(dst_path, path);
449 ck_assert_msg(res == 0, "Failed to symlink '%s' to '%s': %s", path, dst_path,
450 strerror(errno));
451
452 res = dir_readlink(p, path, buf, bufsz, flags);
453 ck_assert_msg(res >= 0, "Failed to read '%s' symlink: %s", path, strerror(errno));
454 ck_assert_msg((size_t) res == dst_pathlen, "Expected length %lu, got %d",
455 (unsigned long) dst_pathlen, res);
456 ck_assert_msg(strcmp(buf, dst_path) == 0, "Expected '%s', got '%s'",
457 dst_path, buf);
458
459 /* Chrooted, overlapping chroot to non-dir */
460 memset(buf, '\0', bufsz);
461 dst_path = "/home/user2";
462 dst_pathlen = strlen(dst_path);
463
464 (void) unlink(path);
465 res = symlink(dst_path, path);
466 ck_assert_msg(res == 0, "Failed to symlink '%s' to '%s': %s", path, dst_path,
467 strerror(errno));
468
469 res = dir_readlink(p, path, buf, bufsz, flags);
470 ck_assert_msg(res >= 0, "Failed to read '%s' symlink: %s", path, strerror(errno));
471 ck_assert_msg((size_t) res == dst_pathlen, "Expected length %lu, got %d",
472 (unsigned long) dst_pathlen, res);
473 ck_assert_msg(strcmp(buf, dst_path) == 0, "Expected '%s', got '%s'",
474 dst_path, buf);
475
476 /* Chrooted, absolute destination within chroot */
477 memset(buf, '\0', bufsz);
478 dst_path = "/home/user/file.txt";
479 dst_pathlen = strlen(dst_path);
480 expected_path = "/file.txt";
481 expected_pathlen = strlen(expected_path);
482
483 (void) unlink(path);
484 res = symlink(dst_path, path);
485 ck_assert_msg(res == 0, "Failed to symlink '%s' to '%s': %s", path, dst_path,
486 strerror(errno));
487
488 res = dir_readlink(p, path, buf, bufsz, flags);
489 ck_assert_msg(res >= 0, "Failed to read '%s' symlink: %s", path, strerror(errno));
490 ck_assert_msg((size_t) res == expected_pathlen, "Expected length %lu, got %d",
491 (unsigned long) expected_pathlen, res);
492 ck_assert_msg(strcmp(buf, expected_path) == 0, "Expected '%s', got '%s'",
493 expected_path, buf);
494
495 /* Chrooted, absolute destination outside of chroot */
496 memset(buf, '\0', bufsz);
497 dst_path = "/home/user/../file.txt";
498 dst_pathlen = strlen(dst_path);
499 expected_path = "/home/file.txt";
500 expected_pathlen = strlen(expected_path);
501
502 (void) unlink(path);
503 res = symlink(dst_path, path);
504 ck_assert_msg(res == 0, "Failed to symlink '%s' to '%s': %s", path, dst_path,
505 strerror(errno));
506
507 res = dir_readlink(p, path, buf, bufsz, flags);
508 ck_assert_msg(res >= 0, "Failed to read '%s' symlink: %s", path, strerror(errno));
509 ck_assert_msg((size_t) res == expected_pathlen, "Expected length %lu, got %d",
510 (unsigned long) expected_pathlen, res);
511 ck_assert_msg(strcmp(buf, expected_path) == 0, "Expected '%s', got '%s'",
512 expected_path, buf);
513
514 /* Chrooted, relative destination within chroot */
515 memset(buf, '\0', bufsz);
516 dst_path = "./file.txt";
517 dst_pathlen = strlen(dst_path);
518 expected_path = "./file.txt";
519 expected_pathlen = strlen(expected_path);
520
521 (void) unlink(path);
522 res = symlink(dst_path, path);
523 ck_assert_msg(res == 0, "Failed to symlink '%s' to '%s': %s", path, dst_path,
524 strerror(errno));
525
526 res = dir_readlink(p, path, buf, bufsz, flags);
527 ck_assert_msg(res >= 0, "Failed to read '%s' symlink: %s", path, strerror(errno));
528 ck_assert_msg((size_t) res == expected_pathlen, "Expected length %lu, got %d",
529 (unsigned long) expected_pathlen, res);
530 ck_assert_msg(strcmp(buf, expected_path) == 0, "Expected '%s', got '%s'",
531 expected_path, buf);
532
533 /* Chrooted, relative destination (without leading '.') within chroot */
534 memset(buf, '\0', bufsz);
535 dst_path = "file.txt";
536 dst_pathlen = strlen(dst_path);
537 expected_path = "file.txt";
538 expected_pathlen = strlen(expected_path);
539
540 (void) unlink(path);
541 res = symlink(dst_path, path);
542 ck_assert_msg(res == 0, "Failed to symlink '%s' to '%s': %s", path, dst_path,
543 strerror(errno));
544
545 res = dir_readlink(p, path, buf, bufsz, flags);
546 ck_assert_msg(res >= 0, "Failed to read '%s' symlink: %s", path, strerror(errno));
547 ck_assert_msg((size_t) res == expected_pathlen, "Expected length %lu, got %d",
548 (unsigned long) expected_pathlen, res);
549 ck_assert_msg(strcmp(buf, expected_path) == 0, "Expected '%s', got '%s'",
550 expected_path, buf);
551
552 /* Chrooted, relative destination outside of chroot */
553 memset(buf, '\0', bufsz);
554 dst_path = "../file.txt";
555 dst_pathlen = strlen(dst_path);
556 expected_path = "../file.txt";
557 expected_pathlen = strlen(expected_path);
558
559 (void) unlink(path);
560 res = symlink(dst_path, path);
561 ck_assert_msg(res == 0, "Failed to symlink '%s' to '%s': %s", path, dst_path,
562 strerror(errno));
563
564 /* First, tell dir_readlink() to ignore relative destination paths. */
565 flags = 0;
566 res = dir_readlink(p, path, buf, bufsz, flags);
567 ck_assert_msg(res >= 0, "Failed to read '%s' symlink: %s", path, strerror(errno));
568 ck_assert_msg((size_t) res == expected_pathlen, "Expected length %lu, got %d",
569 (unsigned long) expected_pathlen, res);
570 ck_assert_msg(strcmp(buf, expected_path) == 0, "Expected '%s', got '%s'",
571 expected_path, buf);
572
573 /* Now do it again, telling dir_readlink() to handle relative destination
574 * paths.
575 */
576 memset(buf, '\0', bufsz);
577 dst_path = "../file.txt";
578 dst_pathlen = strlen(dst_path);
579 expected_path = "/file.txt";
580 expected_pathlen = strlen(expected_path);
581
582 (void) unlink(path);
583 res = symlink(dst_path, path);
584 ck_assert_msg(res == 0, "Failed to symlink '%s' to '%s': %s", path, dst_path,
585 strerror(errno));
586
587 flags = PR_DIR_READLINK_FL_HANDLE_REL_PATH;
588 res = dir_readlink(p, path, buf, bufsz, flags);
589 ck_assert_msg(res >= 0, "Failed to read '%s' symlink: %s", path, strerror(errno));
590 ck_assert_msg((size_t) res == expected_pathlen, "Expected length %lu, got %d",
591 (unsigned long) expected_pathlen, res);
592 ck_assert_msg(strcmp(buf, expected_path) == 0, "Expected '%s', got '%s'",
593 expected_path, buf);
594
595 /* One more time, this time changing the chroot path to align with the
596 * source path.
597 */
598 memset(buf, '\0', bufsz);
599 dst_path = "../file.txt";
600 dst_pathlen = strlen(dst_path);
601 expected_path = "/file.txt";
602 expected_pathlen = strlen(expected_path);
603
604 (void) unlink(path);
605 res = symlink(dst_path, path);
606 ck_assert_msg(res == 0, "Failed to symlink '%s' to '%s': %s", path, dst_path,
607 strerror(errno));
608
609 session.chroot_path = "/tmp";
610 flags = PR_DIR_READLINK_FL_HANDLE_REL_PATH;
611 res = dir_readlink(p, path, buf, bufsz, flags);
612 ck_assert_msg(res >= 0, "Failed to read '%s' symlink: %s", path, strerror(errno));
613 ck_assert_msg((size_t) res == expected_pathlen, "Expected length %lu, got %d",
614 (unsigned long) expected_pathlen, res);
615 ck_assert_msg(strcmp(buf, expected_path) == 0, "Expected '%s', got '%s'",
616 expected_path, buf);
617
618 /* Now use a relative path that does not start with '.' */
619 memset(buf, '\0', bufsz);
620 dst_path = "file.txt";
621 dst_pathlen = strlen(dst_path);
622 expected_path = "./file.txt";
623 expected_pathlen = strlen(expected_path);
624
625 (void) unlink(path);
626 res = symlink(dst_path, path);
627 ck_assert_msg(res == 0, "Failed to symlink '%s' to '%s': %s", path, dst_path,
628 strerror(errno));
629
630 session.chroot_path = "/tmp";
631 flags = PR_DIR_READLINK_FL_HANDLE_REL_PATH;
632 res = dir_readlink(p, path, buf, bufsz, flags);
633 ck_assert_msg(res >= 0, "Failed to read '%s' symlink: %s", path, strerror(errno));
634 ck_assert_msg((size_t) res == expected_pathlen,
635 "Expected length %lu, got %d (%s)", (unsigned long) expected_pathlen, res,
636 buf);
637 ck_assert_msg(strcmp(buf, expected_path) == 0, "Expected '%s', got '%s'",
638 expected_path, buf);
639
640 /* Now use a relative path that does not start with '.', and a chroot
641 * deeper down than one directory.
642 */
643 memset(buf, '\0', bufsz);
644 dst_path = "file.txt";
645 dst_pathlen = strlen(dst_path);
646 expected_path = "/tmp/file.txt";
647 expected_pathlen = strlen(expected_path);
648
649 (void) unlink(path);
650 res = symlink(dst_path, path);
651 ck_assert_msg(res == 0, "Failed to symlink '%s' to '%s': %s", path, dst_path,
652 strerror(errno));
653
654 session.chroot_path = "/tmp/foo/bar";
655 flags = PR_DIR_READLINK_FL_HANDLE_REL_PATH;
656 res = dir_readlink(p, path, buf, bufsz, flags);
657 ck_assert_msg(res >= 0, "Failed to read '%s' symlink: %s", path, strerror(errno));
658 ck_assert_msg((size_t) res == expected_pathlen,
659 "Expected length %lu, got %d (%s)", (unsigned long) expected_pathlen, res,
660 buf);
661 ck_assert_msg(strcmp(buf, expected_path) == 0, "Expected '%s', got '%s'",
662 expected_path, buf);
663
664 /* Now use a relative path, and a chroot deeper down than one directory, and
665 * a deeper/longer source path.
666 */
667 memset(buf, '\0', bufsz);
668 dst_path = "./file.txt";
669 dst_pathlen = strlen(dst_path);
670 expected_path = "/tmp/prt-readlink/file.txt";
671 expected_pathlen = strlen(expected_path);
672
673 (void) unlink(path);
674 (void) rmdir(misc_test_readlink2_dir);
675 (void) mkdir(misc_test_readlink2_dir, 0777);
676 path = misc_test_readlink2;
677 res = symlink(dst_path, path);
678 ck_assert_msg(res == 0, "Failed to symlink '%s' to '%s': %s", path, dst_path,
679 strerror(errno));
680
681 session.chroot_path = "/tmp/foo/bar";
682 flags = PR_DIR_READLINK_FL_HANDLE_REL_PATH;
683 res = dir_readlink(p, path, buf, bufsz, flags);
684 ck_assert_msg(res >= 0, "Failed to read '%s' symlink: %s", path, strerror(errno));
685 ck_assert_msg((size_t) res == expected_pathlen,
686 "Expected length %lu, got %d (%s)", (unsigned long) expected_pathlen, res,
687 buf);
688 ck_assert_msg(strcmp(buf, expected_path) == 0, "Expected '%s', got '%s'",
689 expected_path, buf);
690
691 /* Now use a relative path that does not start with '.', and a chroot
692 * deeper down than one directory, and a deeper/longer source path.
693 */
694 memset(buf, '\0', bufsz);
695 dst_path = "file.txt";
696 dst_pathlen = strlen(dst_path);
697 expected_path = "/tmp/prt-readlink/file.txt";
698 expected_pathlen = strlen(expected_path);
699
700 (void) unlink(path);
701 (void) rmdir(misc_test_readlink2_dir);
702 (void) mkdir(misc_test_readlink2_dir, 0777);
703 path = misc_test_readlink2;
704 res = symlink(dst_path, path);
705 ck_assert_msg(res == 0, "Failed to symlink '%s' to '%s': %s", path, dst_path,
706 strerror(errno));
707
708 session.chroot_path = "/tmp/foo/bar";
709 flags = PR_DIR_READLINK_FL_HANDLE_REL_PATH;
710 res = dir_readlink(p, path, buf, bufsz, flags);
711 ck_assert_msg(res >= 0, "Failed to read '%s' symlink: %s", path, strerror(errno));
712 ck_assert_msg((size_t) res == expected_pathlen,
713 "Expected length %lu, got %d (%s)", (unsigned long) expected_pathlen, res,
714 buf);
715 ck_assert_msg(strcmp(buf, expected_path) == 0, "Expected '%s', got '%s'",
716 expected_path, buf);
717
718 (void) unlink(misc_test_readlink);
719 (void) unlink(misc_test_readlink2);
720 (void) rmdir(misc_test_readlink2_dir);
721 }
722 END_TEST
723
724 START_TEST (dir_realpath_test) {
725 char *res;
726 const char *path;
727
728 res = dir_realpath(NULL, NULL);
729 ck_assert_msg(res == NULL, "Failed to handle null arguments");
730 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
731 strerror(errno), errno);
732
733 res = dir_realpath(p, NULL);
734 ck_assert_msg(res == NULL, "Failed to handle null path");
735 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
736 strerror(errno), errno);
737
738 mark_point();
739 path = "/foo";
740 res = dir_realpath(p, path);
741 ck_assert_msg(res == NULL, "Got real path for '%s' unexpectedly", path);
742 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
743 strerror(errno), errno);
744
745 mark_point();
746 path = "/";
747 res = dir_realpath(p, path);
748 ck_assert_msg(res != NULL, "Failed to get real path for '%s': %s", path,
749 strerror(errno));
750 ck_assert_msg(strcmp(res, path) == 0, "Expected '%s', got '%s'", path, res);
751 }
752 END_TEST
753
754 START_TEST (dir_abs_path_test) {
755 char *res;
756 const char *path;
757
758 res = dir_abs_path(NULL, NULL, TRUE);
759 ck_assert_msg(res == NULL, "Failed to handle null arguments");
760 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
761 strerror(errno), errno);
762
763 res = dir_abs_path(p, NULL, TRUE);
764 ck_assert_msg(res == NULL, "Failed to handle null path");
765 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
766 strerror(errno), errno);
767
768 mark_point();
769 path = "/foo";
770 res = dir_abs_path(p, path, TRUE);
771 ck_assert_msg(path != NULL, "Failed to get absolute path for '%s': %s", path,
772 strerror(errno));
773 ck_assert_msg(strcmp(res, path) == 0, "Expected '%s', got '%s'", path, res);
774 }
775 END_TEST
776
777 START_TEST (safe_token_test) {
778 char *res, *text, *expected;
779
780 mark_point();
781 expected = "";
782 res = safe_token(NULL);
783 ck_assert_msg(res != NULL, "Failed to handle null arguments");
784 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
785 res);
786
787 mark_point();
788 text = "";
789 expected = "";
790 res = safe_token(&text);
791 ck_assert_msg(res != NULL, "Failed to handle null arguments");
792 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
793 res);
794
795 mark_point();
796 text = "foo";
797 expected = text;
798 res = safe_token(&text);
799 ck_assert_msg(res != NULL, "Failed to handle null arguments");
800 ck_assert_msg(res == expected, "Expected '%s', got '%s'", expected, res);
801 ck_assert_msg(strcmp(text, "") == 0, "Expected '', got '%s'", text);
802
803 mark_point();
804 text = " foo";
805 expected = text + 2;
806 res = safe_token(&text);
807 ck_assert_msg(res != NULL, "Failed to handle null arguments");
808 ck_assert_msg(res == expected, "Expected '%s', got '%s'", expected, res);
809 ck_assert_msg(strcmp(text, "") == 0, "Expected '', got '%s'", text);
810
811 mark_point();
812 text = " \t";
813 expected = "";
814 res = safe_token(&text);
815 ck_assert_msg(res != NULL, "Failed to handle null arguments");
816 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
817 res);
818 }
819 END_TEST
820
821 static int write_shutmsg(const char *path, const char *line) {
822 FILE *fh;
823 int res;
824 size_t line_len;
825
826 fh = fopen(path, "w+");
827 if (fh == NULL) {
828 return -1;
829 }
830
831
832 line_len = strlen(line);
833 fwrite(line, line_len, 1, fh);
834
835 res = fclose(fh);
836 return res;
837 }
838
839 START_TEST (check_shutmsg_test) {
840 int res;
841 const char *path;
842 time_t when_shutdown = 0, when_deny = 0, when_disconnect = 0;
843 char shutdown_msg[PR_TUNABLE_BUFFER_SIZE];
844
845 res = check_shutmsg(NULL, NULL, NULL, NULL, NULL, NULL, 0);
846 ck_assert_msg(res < 0, "Failed to handle null arguments");
847 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
848 strerror(errno), errno);
849
850 path = "/foo/bar/baz/quxx/quzz";
851 res = check_shutmsg(p, path, NULL, NULL, NULL, NULL, 0);
852 ck_assert_msg(res < 0, "Failed to handle nonexistent path");
853 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
854 strerror(errno), errno);
855
856 path = "/";
857 res = check_shutmsg(p, path, NULL, NULL, NULL, NULL, 0);
858 ck_assert_msg(res < 0, "Failed to handle directory path");
859 ck_assert_msg(errno == EISDIR, "Expected EISDIR (%d), got %s (%d)", EISDIR,
860 strerror(errno), errno);
861
862 /* XXX More testing needed */
863
864 path = misc_test_shutmsg;
865
866 (void) unlink(path);
867 res = write_shutmsg(path,
868 "1970 1 1 0 0 0 0000 0000\nGoodbye, cruel world!\n");
869 ck_assert_msg(res == 0, "Failed to write '%s': %s", path, strerror(errno));
870
871 memset(shutdown_msg, '\0', sizeof(shutdown_msg));
872 pr_env_set(p, "TZ", "GMT");
873
874 mark_point();
875 res = check_shutmsg(p, path, &when_shutdown, &when_deny, &when_disconnect,
876 shutdown_msg, sizeof(shutdown_msg));
877 ck_assert_msg(res == 1, "Expected 1, got %d", res);
878 ck_assert_msg(when_shutdown == (time_t) 0, "Expected 0, got %lu",
879 (unsigned long) when_shutdown);
880 ck_assert_msg(when_deny == (time_t) 0, "Expected 0, got %lu",
881 (unsigned long) when_deny);
882 ck_assert_msg(when_disconnect == (time_t) 0, "Expected 0, got %lu",
883 (unsigned long) when_disconnect);
884 ck_assert_msg(strcmp(shutdown_msg, "Goodbye, cruel world!") == 0,
885 "Expected 'Goodbye, cruel world!', got '%s'", shutdown_msg);
886
887 (void) unlink(path);
888 res = write_shutmsg(path,
889 "2037 1 1 0 0 0 0000 0000\nGoodbye, cruel world!\n");
890 ck_assert_msg(res == 0, "Failed to write '%s': %s", path, strerror(errno));
891
892 mark_point();
893 res = check_shutmsg(p, path, NULL, NULL, NULL, NULL, 0);
894 ck_assert_msg(res == 1, "Expected 1, got %d", res);
895
896 (void) unlink(path);
897 res = write_shutmsg(path,
898 "0 0 0 0 0 0 0000 0000\nGoodbye, cruel world!\n");
899 ck_assert_msg(res == 0, "Failed to write '%s': %s", path, strerror(errno));
900
901 mark_point();
902 res = check_shutmsg(p, path, NULL, NULL, NULL, NULL, 0);
903
904 (void) unlink(misc_test_shutmsg);
905 }
906 END_TEST
907
908 START_TEST (memscrub_test) {
909 size_t len;
910 char *expected, *text;
911
912 mark_point();
913 pr_memscrub(NULL, 1);
914
915 expected = "Hello, World!";
916 text = pstrdup(p, expected);
917
918 mark_point();
919 pr_memscrub(text, 0);
920
921 len = strlen(text);
922
923 mark_point();
924 pr_memscrub(text, len);
925 ck_assert_msg(strncmp(text, expected, len + 1) != 0,
926 "Expected other than '%s'", expected);
927 }
928 END_TEST
929
930 START_TEST (getopt_reset_test) {
931 mark_point();
932 pr_getopt_reset();
933 }
934 END_TEST
935
936 START_TEST (exists_test) {
937 int res;
938 const char *path;
939
940 res = exists(NULL);
941 ck_assert_msg(res == FALSE, "Failed to handle null path");
942
943 path = "/";
944 res = exists(path);
945 ck_assert_msg(res == TRUE, "Expected TRUE for path '%s', got FALSE", path);
946 }
947 END_TEST
948
949 START_TEST (exists2_test) {
950 int res;
951 const char *path;
952
953 res = exists2(NULL, NULL);
954 ck_assert_msg(res == FALSE, "Failed to handle null arguments");
955
956 res = exists2(p, NULL);
957 ck_assert_msg(res == FALSE, "Failed to handle null path");
958
959 path = "/";
960 res = exists2(p, path);
961 ck_assert_msg(res == TRUE, "Expected TRUE for path '%s', got FALSE", path);
962 }
963 END_TEST
964
965 START_TEST (dir_exists_test) {
966 int res;
967 const char *path;
968
969 res = dir_exists(NULL);
970 ck_assert_msg(res == FALSE, "Failed to handle null path");
971
972 path = "/";
973 res = dir_exists(path);
974 ck_assert_msg(res == TRUE, "Expected TRUE for path '%s', got FALSE", path);
975
976 path = "./api-tests";
977 res = dir_exists(path);
978 ck_assert_msg(res == FALSE, "Expected FALSE for path '%s', got TRUE", path);
979 }
980 END_TEST
981
982 START_TEST (dir_exists2_test) {
983 int res;
984 const char *path;
985
986 res = dir_exists2(NULL, NULL);
987 ck_assert_msg(res == FALSE, "Failed to handle null arguments");
988
989 res = dir_exists2(p, NULL);
990 ck_assert_msg(res == FALSE, "Failed to handle null path");
991
992 path = "/";
993 res = dir_exists2(p, path);
994 ck_assert_msg(res == TRUE, "Expected TRUE for path '%s', got FALSE", path);
995
996 path = "./api-tests";
997 res = dir_exists2(p, path);
998 ck_assert_msg(res == FALSE, "Expected FALSE for path '%s', got TRUE", path);
999 }
1000 END_TEST
1001
1002 START_TEST (symlink_mode_test) {
1003 mode_t res;
1004 const char *path;
1005
1006 res = symlink_mode(NULL);
1007 ck_assert_msg(res == 0, "Failed to handle null arguments");
1008 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1009 strerror(errno), errno);
1010
1011 path = "/";
1012 res = symlink_mode(path);
1013 ck_assert_msg(res == 0, "Found mode for non-symlink '%s'", path);
1014 }
1015 END_TEST
1016
1017 START_TEST (symlink_mode2_test) {
1018 mode_t res;
1019 const char *path;
1020
1021 res = symlink_mode2(NULL, NULL);
1022 ck_assert_msg(res == 0, "Failed to handle null arguments");
1023 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1024 strerror(errno), errno);
1025
1026 res = symlink_mode2(p, NULL);
1027 ck_assert_msg(res == 0, "Failed to handle null path");
1028 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1029 strerror(errno), errno);
1030
1031 path = "/";
1032 res = symlink_mode2(p, path);
1033 ck_assert_msg(res == 0, "Found mode for non-symlink '%s'", path);
1034 }
1035 END_TEST
1036
1037 START_TEST (file_mode_test) {
1038 mode_t res;
1039 const char *path;
1040
1041 res = file_mode(NULL);
1042 ck_assert_msg(res == 0, "Failed to handle null path");
1043 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1044 strerror(errno), errno);
1045
1046 path = "/";
1047 res = file_mode(path);
1048 ck_assert_msg(res != 0, "Failed to find mode for '%s': %s", path,
1049 strerror(errno));
1050 }
1051 END_TEST
1052
1053 START_TEST (file_mode2_test) {
1054 mode_t res;
1055 const char *path;
1056
1057 res = file_mode2(NULL, NULL);
1058 ck_assert_msg(res == 0, "Failed to handle null arguments");
1059 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1060 strerror(errno), errno);
1061
1062 res = file_mode2(p, NULL);
1063 ck_assert_msg(res == 0, "Failed to handle null path");
1064 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1065 strerror(errno), errno);
1066
1067 path = "/";
1068 res = file_mode2(p, path);
1069 ck_assert_msg(res != 0, "Failed to find mode for '%s': %s", path,
1070 strerror(errno));
1071 }
1072 END_TEST
1073
1074 START_TEST (file_exists_test) {
1075 int res;
1076 const char *path;
1077
1078 res = file_exists(NULL);
1079 ck_assert_msg(res == FALSE, "Failed to handle null path");
1080
1081 path = "/";
1082 res = file_exists(path);
1083 ck_assert_msg(res == FALSE, "Expected FALSE for path '%s', got TRUE", path);
1084
1085 path = "./api-tests";
1086 res = file_exists(path);
1087 ck_assert_msg(res == TRUE, "Expected TRUE for path '%s', got FALSE", path);
1088 }
1089 END_TEST
1090
1091 START_TEST (file_exists2_test) {
1092 int res;
1093 const char *path;
1094
1095 res = file_exists2(NULL, NULL);
1096 ck_assert_msg(res == FALSE, "Failed to handle null arguments");
1097
1098 res = file_exists2(p, NULL);
1099 ck_assert_msg(res == FALSE, "Failed to handle null path");
1100
1101 path = "/";
1102 res = file_exists2(p, path);
1103 ck_assert_msg(res == FALSE, "Expected FALSE for path '%s', got TRUE", path);
1104
1105 path = "./api-tests";
1106 res = file_exists2(p, path);
1107 ck_assert_msg(res == TRUE, "Expected TRUE for path '%s', got FALSE", path);
1108 }
1109 END_TEST
1110
1111 START_TEST (gmtime_test) {
1112 struct tm *res;
1113 time_t now;
1114
1115 mark_point();
1116 res = pr_gmtime(NULL, NULL);
1117 ck_assert_msg(res == NULL, "Failed to handle null arguments");
1118 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1119 strerror(errno), errno);
1120
1121 time(&now);
1122
1123 mark_point();
1124 res = pr_gmtime(NULL, &now);
1125 #if defined(HAVE_GMTIME_R)
1126 ck_assert_msg(res == NULL, "Failed to handle null pool");
1127 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1128 strerror(errno), errno);
1129 #else
1130 ck_assert_msg(res != NULL, "Failed to handle %lu: %s", (unsigned long) now,
1131 strerror(errno));
1132 #endif /* HAVE_GMTIME_R */
1133
1134 mark_point();
1135 res = pr_gmtime(p, &now);
1136 ck_assert_msg(res != NULL, "Failed to handle %lu: %s", (unsigned long) now,
1137 strerror(errno));
1138 }
1139 END_TEST
1140
1141 START_TEST (localtime_test) {
1142 struct tm *res;
1143 time_t now;
1144
1145 mark_point();
1146 res = pr_localtime(NULL, NULL);
1147 ck_assert_msg(res == NULL, "Failed to handle null arguments");
1148 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1149 strerror(errno), errno);
1150
1151 time(&now);
1152
1153 mark_point();
1154 res = pr_localtime(NULL, &now);
1155 #if defined(HAVE_LOCALTIME_R)
1156 ck_assert_msg(res == NULL, "Failed to handle null pool");
1157 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1158 strerror(errno), errno);
1159 #else
1160 ck_assert_msg(res != NULL, "Failed to handle %lu: %s", (unsigned long) now,
1161 strerror(errno));
1162 #endif /* HAVE_LOCALTIME_R */
1163
1164 mark_point();
1165 res = pr_localtime(p, &now);
1166 ck_assert_msg(res != NULL, "Failed to handle %lu: %s", (unsigned long) now,
1167 strerror(errno));
1168 }
1169 END_TEST
1170
1171 START_TEST (strtime_test) {
1172 const char *res;
1173 time_t now;
1174
1175 mark_point();
1176 now = 0;
1177 res = pr_strtime(now);
1178 #if defined(HAVE_LOCALTIME_R)
1179 ck_assert_msg(res == NULL, "Failed to handle null pool");
1180 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1181 strerror(errno), errno);
1182 #else
1183 ck_assert_msg(res != NULL, "Failed to convert time %lu: %s",
1184 (unsigned long) now, strerror(errno));
1185 #endif /* HAVE_LOCALTIME_R */
1186 }
1187 END_TEST
1188
1189 START_TEST (strtime2_test) {
1190 const char *res;
1191 char *expected;
1192 time_t now;
1193
1194 mark_point();
1195 now = 0;
1196 expected = "Thu Jan 01 00:00:00 1970";
1197 res = pr_strtime2(now, TRUE);
1198 #if defined(HAVE_GMTIME_R)
1199 ck_assert_msg(res == NULL, "Failed to handle null pool");
1200 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1201 strerror(errno), errno);
1202 #else
1203 ck_assert_msg(res != NULL, "Failed to convert time %lu: %s",
1204 (unsigned long) now, strerror(errno));
1205 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1206 res);
1207 #endif /* HAVE_GMTIME_R */
1208 }
1209 END_TEST
1210
1211 START_TEST (strtime3_test) {
1212 const char *res;
1213 char *expected;
1214 time_t now;
1215
1216 mark_point();
1217 now = 0;
1218 #if defined(HAVE_GMTIME_R)
1219 res = pr_strtime3(NULL, now, TRUE);
1220 ck_assert_msg(res == NULL, "Failed to handle null pool argument");
1221 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%s), got %s (%d)",
1222 strerror(EINVAL), strerror(errno), errno);
1223 #endif /* HAVE_GMTIME_R */
1224
1225 mark_point();
1226 expected = "Thu Jan 01 00:00:00 1970";
1227 res = pr_strtime3(p, now, TRUE);
1228 ck_assert_msg(res != NULL, "Failed to convert time %lu: %s",
1229 (unsigned long) now, strerror(errno));
1230 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1231 res);
1232 }
1233 END_TEST
1234
1235 START_TEST (timeval2millis_test) {
1236 int res;
1237 struct timeval tv;
1238 uint64_t ms;
1239
1240 res = pr_timeval2millis(NULL, NULL);
1241 ck_assert_msg(res < 0, "Failed to handle null arguments");
1242 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1243 strerror(errno), errno);
1244
1245 res = pr_timeval2millis(&tv, NULL);
1246 ck_assert_msg(res < 0, "Failed to handle null millis argument");
1247 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1248 strerror(errno), errno);
1249
1250 tv.tv_sec = tv.tv_usec = 0;
1251 res = pr_timeval2millis(&tv, &ms);
1252 ck_assert_msg(res == 0, "Failed to convert timeval to millis: %s",
1253 strerror(errno));
1254 ck_assert_msg(ms == 0, "Expected 0 ms, got %lu", (unsigned long) ms);
1255 }
1256 END_TEST
1257
1258 START_TEST (gettimeofday_millis_test) {
1259 int res;
1260 uint64_t ms;
1261
1262 res = pr_gettimeofday_millis(NULL);
1263 ck_assert_msg(res < 0, "Failed to handle null argument");
1264 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1265 strerror(errno), errno);
1266
1267 ms = 0;
1268 res = pr_gettimeofday_millis(&ms);
1269 ck_assert_msg(res == 0, "Failed to get current time ms: %s", strerror(errno));
1270 ck_assert_msg(ms > 0, "Expected >0, got %lu", (unsigned long) ms);
1271 }
1272 END_TEST
1273
1274 START_TEST (snprintf_test) {
1275 char *buf;
1276 size_t bufsz;
1277 int res, expected;
1278
1279 res = pr_snprintf(NULL, 0, NULL);
1280 ck_assert_msg(res < 0, "Failed to handle null buffer");
1281 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1282 strerror(errno), errno);
1283
1284 bufsz = 1;
1285 buf = palloc(p, bufsz);
1286
1287 res = pr_snprintf(buf, 0, NULL);
1288 ck_assert_msg(res < 0, "Failed to handle null format");
1289 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1290 strerror(errno), errno);
1291
1292 res = pr_snprintf(buf, 0, "%d", 0);
1293 ck_assert_msg(res == 0, "Failed to handle zero-length buffer");
1294
1295 res = pr_snprintf(buf, bufsz, "%d", 0);
1296 ck_assert_msg(res < 0, "Failed to handle too-small buffer");
1297 ck_assert_msg(errno == ENOSPC, "Expected ENOSPC (%d), got %s (%d)", ENOSPC,
1298 strerror(errno), errno);
1299
1300 res = pr_snprintf(buf, bufsz, "%s", "foobar");
1301 ck_assert_msg(res < 0, "Failed to handle too-small buffer");
1302 ck_assert_msg(errno == ENOSPC, "Expected ENOSPC (%d), got %s (%d)", ENOSPC,
1303 strerror(errno), errno);
1304
1305 bufsz = 32;
1306 buf = palloc(p, bufsz);
1307
1308 expected = 6;
1309 res = pr_snprintf(buf, bufsz, "%s", "foobar");
1310 ck_assert_msg(res == expected, "Expected %d, got %d", expected, res);
1311 }
1312 END_TEST
1313
1314 START_TEST (snprintfl_test) {
1315 char *buf;
1316 size_t bufsz;
1317 int res, expected;
1318
1319 res = pr_snprintfl(NULL, -1, NULL, 0, NULL);
1320 ck_assert_msg(res < 0, "Failed to handle null buffer");
1321 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1322 strerror(errno), errno);
1323
1324 bufsz = 1;
1325 buf = palloc(p, bufsz);
1326
1327 res = pr_snprintfl(NULL, -1, buf, 0, NULL);
1328 ck_assert_msg(res < 0, "Failed to handle null format");
1329 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1330 strerror(errno), errno);
1331
1332 res = pr_snprintfl(__FILE__, __LINE__, buf, 0, "%d", 0);
1333 ck_assert_msg(res == 0, "Failed to handle zero-length buffer");
1334
1335 res = pr_snprintfl(__FILE__, __LINE__, buf, bufsz, "%d", 0);
1336 ck_assert_msg(res < 0, "Failed to handle too-small buffer");
1337 ck_assert_msg(errno == ENOSPC, "Expected ENOSPC (%d), got %s (%d)", ENOSPC,
1338 strerror(errno), errno);
1339
1340 res = pr_snprintfl(__FILE__, __LINE__, buf, bufsz, "%s", "foobar");
1341 ck_assert_msg(res < 0, "Failed to handle too-small buffer");
1342 ck_assert_msg(errno == ENOSPC, "Expected ENOSPC (%d), got %s (%d)", ENOSPC,
1343 strerror(errno), errno);
1344
1345 bufsz = 32;
1346 buf = palloc(p, bufsz);
1347
1348 expected = 6;
1349 res = pr_snprintfl(__FILE__, __LINE__, buf, bufsz, "%s", "foobar");
1350 ck_assert_msg(res == expected, "Expected %d, got %d", expected, res);
1351 }
1352 END_TEST
1353
1354 START_TEST (path_subst_uservar_test) {
1355 const char *path = NULL, *res, *original, *expected;
1356
1357 res = path_subst_uservar(NULL, NULL);
1358 ck_assert_msg(res == NULL, "Failed to handle null pool");
1359 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1360 strerror(errno), errno);
1361
1362 res = path_subst_uservar(p, NULL);
1363 ck_assert_msg(res == NULL, "Failed to handle null path pointer");
1364 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1365 strerror(errno), errno);
1366
1367 res = path_subst_uservar(p, &path);
1368 ck_assert_msg(res == NULL, "Failed to handle null path");
1369 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1370 strerror(errno), errno);
1371
1372 original = expected = "somepathhere";
1373 path = pstrdup(p, expected);
1374 mark_point();
1375 res = path_subst_uservar(p, &path);
1376 ck_assert_msg(res != NULL, "Failed to handle path '%s': %s", path,
1377 strerror(errno));
1378 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1379 res);
1380
1381 session.user = "user";
1382 original = "/home/%u";
1383 expected = "/home/user";
1384 path = pstrdup(p, original);
1385 mark_point();
1386 res = path_subst_uservar(p, &path);
1387 ck_assert_msg(res != NULL, "Failed to handle path '%s': %s", path,
1388 strerror(errno));
1389 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1390 res);
1391
1392 session.user = "user";
1393 original = "/home/%u[";
1394 expected = "/home/user[";
1395 path = pstrdup(p, original);
1396 mark_point();
1397 res = path_subst_uservar(p, &path);
1398 ck_assert_msg(res != NULL, "Failed to handle path '%s': %s", path,
1399 strerror(errno));
1400 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1401 res);
1402
1403 session.user = "user";
1404 original = "/home/%u[]";
1405 expected = "/home/user[]";
1406 path = pstrdup(p, original);
1407 mark_point();
1408 res = path_subst_uservar(p, &path);
1409 ck_assert_msg(res != NULL, "Failed to handle path '%s': %s", path,
1410 strerror(errno));
1411 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1412 res);
1413
1414 session.user = "user";
1415 original = "/home/users/%u[0]/%u[0]%u[1]/%u";
1416 expected = "/home/users/u/us/user";
1417 path = pstrdup(p, original);
1418 mark_point();
1419 res = path_subst_uservar(p, &path);
1420 ck_assert_msg(res != NULL, "Failed to handle path '%s': %s", path,
1421 strerror(errno));
1422 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1423 res);
1424
1425 /* Attempt to use an invalid index */
1426 session.user = "user";
1427 original = "/home/users/%u[a]/%u[b]%u[c]/%u";
1428 expected = original;
1429 path = pstrdup(p, original);
1430 mark_point();
1431 res = path_subst_uservar(p, &path);
1432 ck_assert_msg(res != NULL, "Failed to handle path '%s': %s", path,
1433 strerror(errno));
1434 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1435 res);
1436
1437 /* Attempt to use an out-of-bounds index */
1438 session.user = "user";
1439 original = "/home/users/%u[0]/%u[-1]%u[1]/%u";
1440 expected = original;
1441 path = pstrdup(p, original);
1442 mark_point();
1443 res = path_subst_uservar(p, &path);
1444 ck_assert_msg(res != NULL, "Failed to handle path '%s': %s", path,
1445 strerror(errno));
1446 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1447 res);
1448
1449 /* Attempt to use an out-of-bounds index */
1450 session.user = "user";
1451 original = "/home/users/%u[0]/%u[0]%u[4]/%u";
1452 expected = original;
1453 path = pstrdup(p, original);
1454 mark_point();
1455 res = path_subst_uservar(p, &path);
1456 ck_assert_msg(res != NULL, "Failed to handle path '%s': %s", path,
1457 strerror(errno));
1458 ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected,
1459 res);
1460 }
1461 END_TEST
1462
1463 Suite *tests_get_misc_suite(void) {
1464 Suite *suite;
1465 TCase *testcase;
1466
1467 suite = suite_create("misc");
1468
1469 testcase = tcase_create("base");
1470 tcase_add_checked_fixture(testcase, set_up, tear_down);
1471
1472 tcase_add_test(testcase, schedule_test);
1473 tcase_add_test(testcase, get_name_max_test);
1474 tcase_add_test(testcase, dir_interpolate_test);
1475 tcase_add_test(testcase, dir_best_path_test);
1476 tcase_add_test(testcase, dir_canonical_path_test);
1477 tcase_add_test(testcase, dir_canonical_vpath_test);
1478 tcase_add_test(testcase, dir_readlink_test);
1479 tcase_add_test(testcase, dir_realpath_test);
1480 tcase_add_test(testcase, dir_abs_path_test);
1481 tcase_add_test(testcase, symlink_mode_test);
1482 tcase_add_test(testcase, symlink_mode2_test);
1483 tcase_add_test(testcase, file_mode_test);
1484 tcase_add_test(testcase, file_mode2_test);
1485 tcase_add_test(testcase, exists_test);
1486 tcase_add_test(testcase, exists2_test);
1487 tcase_add_test(testcase, dir_exists_test);
1488 tcase_add_test(testcase, dir_exists2_test);
1489 tcase_add_test(testcase, file_exists_test);
1490 tcase_add_test(testcase, file_exists2_test);
1491 tcase_add_test(testcase, safe_token_test);
1492 tcase_add_test(testcase, check_shutmsg_test);
1493 tcase_add_test(testcase, memscrub_test);
1494 tcase_add_test(testcase, getopt_reset_test);
1495 tcase_add_test(testcase, gmtime_test);
1496 tcase_add_test(testcase, localtime_test);
1497 tcase_add_test(testcase, strtime_test);
1498 tcase_add_test(testcase, strtime2_test);
1499 tcase_add_test(testcase, strtime3_test);
1500 tcase_add_test(testcase, timeval2millis_test);
1501 tcase_add_test(testcase, gettimeofday_millis_test);
1502 tcase_add_test(testcase, snprintf_test);
1503 tcase_add_test(testcase, snprintfl_test);
1504 tcase_add_test(testcase, path_subst_uservar_test);
1505
1506 suite_add_tcase(suite, testcase);
1507 return suite;
1508 }