Debian Codegraph @

tests/api/fsio.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server testsuite
3 * Copyright (c) 2008-2024 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 /* FSIO API tests */
26
27 #include "tests.h"
28
29 #ifdef PR_USE_XATTR
30 /* Handle the case where ENOATTR may not be defined. */
31 # ifndef ENOATTR
32 # define ENOATTR ENODATA
33 # endif
34 #endif
35
36 static pool *p = NULL;
37
38 static char *fsio_cwd = NULL;
39 static const char *fsio_test_path = "/tmp/prt-foo.bar.baz";
40 static const char *fsio_test2_path = "/tmp/prt-foo.bar.baz.quxx.quzz";
41 static const char *fsio_unlink_path = "/tmp/prt-fsio-link.dat";
42 static const char *fsio_link_path = "/tmp/prt-fsio-symlink.lnk";
43 static const char *fsio_testdir_path = "/tmp/prt-fsio-test.d";
44 static const char *fsio_copy_src_path = "/tmp/prt-fs-src.dat";
45 static const char *fsio_copy_dst_path = "/tmp/prt-fs-dst.dat";
46
47 /* Fixtures */
48
49 static void set_up(void) {
50 (void) unlink(fsio_test_path);
51 (void) unlink(fsio_test2_path);
52 (void) unlink(fsio_link_path);
53 (void) unlink(fsio_unlink_path);
54 (void) rmdir(fsio_testdir_path);
55
56 if (fsio_cwd != NULL) {
57 free(fsio_cwd);
58 }
59
60 fsio_cwd = getcwd(NULL, 0);
61
62 if (p == NULL) {
63 p = permanent_pool = make_sub_pool(NULL);
64 }
65
66 init_fs();
67 pr_fs_statcache_set_policy(PR_TUNABLE_FS_STATCACHE_SIZE,
68 PR_TUNABLE_FS_STATCACHE_MAX_AGE, 0);
69
70 if (getenv("TEST_VERBOSE") != NULL) {
71 pr_trace_set_levels("error", 1, 20);
72 pr_trace_set_levels("fsio", 1, 20);
73 pr_trace_set_levels("fs.statcache", 1, 20);
74 }
75 }
76
77 static void tear_down(void) {
78 if (fsio_cwd != NULL) {
79 free(fsio_cwd);
80 fsio_cwd = NULL;
81 }
82
83 (void) pr_fsio_guard_chroot(FALSE);
84 pr_fs_statcache_set_policy(PR_TUNABLE_FS_STATCACHE_SIZE,
85 PR_TUNABLE_FS_STATCACHE_MAX_AGE, 0);
86
87 pr_unregister_fs("/testuite");
88
89 if (getenv("TEST_VERBOSE") != NULL) {
90 pr_trace_set_levels("error", 0, 0);
91 pr_trace_set_levels("fsio", 0, 0);
92 pr_trace_set_levels("fs.statcache", 0, 0);
93 }
94
95 (void) unlink(fsio_test_path);
96 (void) unlink(fsio_test2_path);
97 (void) unlink(fsio_link_path);
98 (void) unlink(fsio_unlink_path);
99 (void) rmdir(fsio_testdir_path);
100
101 if (p) {
102 destroy_pool(p);
103 p = permanent_pool = NULL;
104 }
105 }
106
107 static const char *get_errnum(pool *err_pool, int xerrno) {
108 char errnum[32];
109 memset(errnum, '\0', sizeof(errnum));
110 snprintf(errnum, sizeof(errnum)-1, "%d", xerrno);
111 return pstrdup(err_pool, errnum);
112 }
113
114 /* Tests */
115
116 START_TEST (fsio_sys_open_test) {
117 int flags;
118 pr_fh_t *fh;
119
120 mark_point();
121 flags = O_CREAT|O_EXCL|O_RDONLY;
122 fh = pr_fsio_open(NULL, flags);
123 ck_assert_msg(fh == NULL, "Failed to handle null arguments");
124 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), %s (%d)", EINVAL,
125 strerror(errno), errno);
126
127 mark_point();
128 flags = O_RDONLY;
129 fh = pr_fsio_open(fsio_test_path, flags);
130 ck_assert_msg(fh == NULL, "Failed to handle non-existent file '%s'",
131 fsio_test_path);
132 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
133 strerror(errno), errno);
134
135 mark_point();
136 flags = O_RDONLY;
137 fh = pr_fsio_open("/etc/hosts", flags);
138 ck_assert_msg(fh != NULL, "Failed to open /etc/hosts: %s", strerror(errno));
139
140 (void) pr_fsio_close(fh);
141 }
142 END_TEST
143
144 START_TEST (fsio_sys_open_canon_test) {
145 int flags;
146 pr_fh_t *fh;
147
148 flags = O_CREAT|O_EXCL|O_RDONLY;
149 fh = pr_fsio_open_canon(NULL, flags);
150 ck_assert_msg(fh == NULL, "Failed to handle null arguments");
151 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), %s (%d)", EINVAL,
152 strerror(errno), errno);
153
154 flags = O_RDONLY;
155 fh = pr_fsio_open_canon(fsio_test_path, flags);
156 ck_assert_msg(fh == NULL, "Failed to handle non-existent file '%s'",
157 fsio_test_path);
158 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
159 strerror(errno), errno);
160
161 flags = O_RDONLY;
162 fh = pr_fsio_open_canon("/etc/hosts", flags);
163 ck_assert_msg(fh != NULL, "Failed to open /etc/hosts: %s", strerror(errno));
164
165 (void) pr_fsio_close(fh);
166 }
167 END_TEST
168
169 START_TEST (fsio_sys_open_chroot_guard_test) {
170 int flags, res;
171 pr_fh_t *fh;
172 const char *path;
173
174 res = pr_fsio_guard_chroot(TRUE);
175 ck_assert_msg(res == FALSE, "Expected FALSE (%d), got %d", FALSE, res);
176
177 path = "/etc/hosts";
178 flags = O_CREAT|O_RDONLY;
179 fh = pr_fsio_open(path, flags);
180 if (fh != NULL) {
181 (void) pr_fsio_close(fh);
182 ck_abort_msg("open(2) of %s succeeded unexpectedly", path);
183 }
184
185 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s (%d)", EACCES,
186 strerror(errno), errno);
187
188 path = "/&Z";
189 flags = O_WRONLY;
190 fh = pr_fsio_open(path, flags);
191 if (fh != NULL) {
192 (void) pr_fsio_close(fh);
193 ck_abort_msg("open(2) of %s succeeded unexpectedly", path);
194 }
195
196 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
197 strerror(errno), errno);
198
199 path = "/etc";
200 fh = pr_fsio_open(path, flags);
201 if (fh != NULL) {
202 (void) pr_fsio_close(fh);
203 ck_abort_msg("open(2) of %s succeeded unexpectedly", path);
204 }
205
206 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s (%d)", EACCES,
207 strerror(errno), errno);
208
209 path = "/lib";
210 fh = pr_fsio_open(path, flags);
211 if (fh != NULL) {
212 (void) pr_fsio_close(fh);
213 ck_abort_msg("open(2) of %s succeeded unexpectedly", path);
214 }
215
216 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s (%d)", EACCES,
217 strerror(errno), errno);
218
219 (void) pr_fsio_guard_chroot(FALSE);
220
221 path = "/etc/hosts";
222 flags = O_RDONLY;
223 fh = pr_fsio_open(path, flags);
224 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", path, strerror(errno));
225 (void) pr_fsio_close(fh);
226 }
227 END_TEST
228
229 START_TEST (fsio_sys_close_test) {
230 int res;
231 pr_fh_t *fh;
232
233 res = pr_fsio_close(NULL);
234 ck_assert_msg(res < 0, "Failed to handle null arguments");
235 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s %d", EINVAL,
236 strerror(errno), errno);
237
238 fh = pr_fsio_open("/etc/hosts", O_RDONLY);
239 ck_assert_msg(fh != NULL, "Failed to open /etc/hosts: %s",
240 strerror(errno));
241
242 res = pr_fsio_close(fh);
243 ck_assert_msg(res == 0, "Failed to close file handle: %s", strerror(errno));
244 }
245 END_TEST
246
247 START_TEST (fsio_sys_unlink_test) {
248 int res;
249 pr_fh_t *fh;
250
251 res = pr_fsio_unlink(NULL);
252 ck_assert_msg(res < 0, "Failed to handle null arguments");
253 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
254 strerror(errno), errno);
255
256 fh = pr_fsio_open(fsio_unlink_path, O_CREAT|O_EXCL|O_WRONLY);
257 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", fsio_unlink_path,
258 strerror(errno));
259 (void) pr_fsio_close(fh);
260
261 res = pr_fsio_unlink(fsio_unlink_path);
262 ck_assert_msg(res == 0, "Failed to unlink '%s': %s", fsio_unlink_path,
263 strerror(errno));
264 }
265 END_TEST
266
267 START_TEST (fsio_sys_unlink_chroot_guard_test) {
268 int res;
269
270 res = pr_fsio_guard_chroot(TRUE);
271 ck_assert_msg(res == FALSE, "Expected FALSE (%d), got %d", FALSE, res);
272
273 res = pr_fsio_unlink("/etc/hosts");
274 ck_assert_msg(res < 0, "Deleted /etc/hosts unexpectedly");
275 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s %d", EACCES,
276 strerror(errno), errno);
277
278 (void) pr_fsio_guard_chroot(FALSE);
279
280 res = pr_fsio_unlink("/lib/foo.bar.baz");
281 ck_assert_msg(res < 0, "Deleted /lib/foo.bar.baz unexpectedly");
282 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s %d", ENOENT,
283 strerror(errno), errno);
284 }
285 END_TEST
286
287 START_TEST (fsio_sys_stat_test) {
288 int res;
289 struct stat st;
290 unsigned int cache_size = 3, max_age = 1, policy_flags = 0;
291
292 res = pr_fsio_stat(NULL, &st);
293 ck_assert_msg(res < 0, "Failed to handle null path");
294 ck_assert_msg(errno == EINVAL, "Expected EINVAL, got %s (%d)", strerror(errno),
295 errno);
296
297 res = pr_fsio_stat("/", NULL);
298 ck_assert_msg(res < 0, "Failed to handle null struct stat");
299 ck_assert_msg(errno == EINVAL, "Expected EINVAL, got %s (%d)", strerror(errno),
300 errno);
301
302 res = pr_fsio_stat("/", &st);
303 ck_assert_msg(res == 0, "Unexpected stat(2) error on '/': %s",
304 strerror(errno));
305 ck_assert_msg(S_ISDIR(st.st_mode), "'/' is not a directory as expected");
306
307 /* Now, do the stat(2) again, and make sure we get the same information
308 * from the cache.
309 */
310 res = pr_fsio_stat("/", &st);
311 ck_assert_msg(res == 0, "Unexpected stat(2) error on '/': %s",
312 strerror(errno));
313 ck_assert_msg(S_ISDIR(st.st_mode), "'/' is not a directory as expected");
314
315 pr_fs_statcache_reset();
316 res = pr_fs_statcache_set_policy(cache_size, max_age, policy_flags);
317 ck_assert_msg(res == 0, "Failed to set statcache policy: %s", strerror(errno));
318
319 res = pr_fsio_stat("/foo/bar/baz/quxx", &st);
320 ck_assert_msg(res < 0, "Failed to handle nonexistent path");
321 ck_assert_msg(errno == ENOENT, "Expected ENOENT, got %s (%d)", strerror(errno),
322 errno);
323
324 res = pr_fsio_stat("/foo/bar/baz/quxx", &st);
325 ck_assert_msg(res < 0, "Failed to handle nonexistent path");
326 ck_assert_msg(errno == ENOENT, "Expected ENOENT, got %s (%d)", strerror(errno),
327 errno);
328
329 /* Now wait for longer than 1 second (our configured max age) */
330 sleep(max_age + 1);
331
332 res = pr_fsio_stat("/foo/bar/baz/quxx", &st);
333 ck_assert_msg(res < 0, "Failed to handle nonexistent path");
334 ck_assert_msg(errno == ENOENT, "Expected ENOENT, got %s (%d)", strerror(errno),
335 errno);
336
337 /* Stat a symlink path */
338 res = pr_fsio_symlink("/tmp", fsio_link_path);
339 ck_assert_msg(res == 0, "Failed to create symlink to '%s': %s", fsio_link_path,
340 strerror(errno));
341
342 res = pr_fsio_stat(fsio_link_path, &st);
343 ck_assert_msg(res == 0, "Failed to stat '%s': %s", fsio_link_path,
344 strerror(errno));
345
346 (void) unlink(fsio_link_path);
347 }
348 END_TEST
349
350 START_TEST (fsio_sys_fstat_test) {
351 int res;
352 pr_fh_t *fh;
353 struct stat st;
354
355 res = pr_fsio_fstat(NULL, NULL);
356 ck_assert_msg(res < 0, "Failed to handle null arguments");
357 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
358 strerror(errno), errno);
359
360 fh = pr_fsio_open("/etc/hosts", O_RDONLY);
361 ck_assert_msg(fh != NULL, "Failed to open /etc/hosts: %s",
362 strerror(errno));
363
364 res = pr_fsio_fstat(fh, &st);
365 ck_assert_msg(res == 0, "Failed to fstat /etc/hosts: %s",
366 strerror(errno));
367 (void) pr_fsio_close(fh);
368 }
369 END_TEST
370
371 START_TEST (fsio_sys_read_test) {
372 int res;
373 pr_fh_t *fh;
374 char *buf;
375 size_t buflen;
376
377 res = pr_fsio_read(NULL, NULL, 0);
378 ck_assert_msg(res < 0, "Failed to handle null arguments");
379 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
380 strerror(errno), errno);
381
382 fh = pr_fsio_open("/etc/hosts", O_RDONLY);
383 ck_assert_msg(fh != NULL, "Failed to open /etc/hosts: %s",
384 strerror(errno));
385
386 res = pr_fsio_read(fh, NULL, 0);
387 ck_assert_msg(res < 0, "Failed to handle null buffer");
388 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
389 strerror(errno), errno);
390
391 buflen = 32;
392 buf = palloc(p, buflen);
393
394 res = pr_fsio_read(fh, buf, 0);
395 ck_assert_msg(res < 0, "Failed to handle zero buffer length");
396 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
397 strerror(errno), errno);
398
399 res = pr_fsio_read(fh, buf, 1);
400 ck_assert_msg(res == 1, "Failed to read 1 byte: %s", strerror(errno));
401
402 (void) pr_fsio_close(fh);
403 }
404 END_TEST
405
406 START_TEST (fsio_sys_pread_test) {
407 ssize_t res;
408 pr_fh_t *fh;
409 char *buf;
410 size_t buflen;
411
412 res = pr_fsio_pread(NULL, NULL, 0, 0);
413 ck_assert_msg(res < 0, "Failed to handle null arguments");
414 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
415 strerror(errno), errno);
416
417 fh = pr_fsio_open("/etc/hosts", O_RDONLY);
418 ck_assert_msg(fh != NULL, "Failed to open /etc/hosts: %s",
419 strerror(errno));
420
421 res = pr_fsio_pread(fh, NULL, 0, 0);
422 ck_assert_msg(res < 0, "Failed to handle null buffer");
423 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
424 strerror(errno), errno);
425
426 buflen = 32;
427 buf = palloc(p, buflen);
428
429 res = pr_fsio_pread(fh, buf, 0, 0);
430 ck_assert_msg(res < 0, "Failed to handle zero buffer length");
431 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
432 strerror(errno), errno);
433
434 res = pr_fsio_pread(fh, buf, 1, 0);
435 ck_assert_msg(res == 1, "Failed to read 1 byte: %s", strerror(errno));
436
437 (void) pr_fsio_close(fh);
438 }
439 END_TEST
440
441 START_TEST (fsio_sys_write_test) {
442 int res;
443 pr_fh_t *fh;
444 char *buf;
445 size_t buflen;
446
447 res = pr_fsio_write(NULL, NULL, 0);
448 ck_assert_msg(res < 0, "Failed to handle null arguments");
449 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
450 strerror(errno), errno);
451
452 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_WRONLY);
453 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", fsio_test_path,
454 strerror(errno));
455
456 /* XXX What happens if we use NULL buffer, zero length? */
457 res = pr_fsio_write(fh, NULL, 0);
458 ck_assert_msg(res < 0, "Failed to handle null buffer");
459 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
460 strerror(errno), errno);
461
462 buflen = 32;
463 buf = palloc(p, buflen);
464 memset(buf, 'c', buflen);
465
466 res = pr_fsio_write(fh, buf, 0);
467 ck_assert_msg(res == 0, "Failed to handle zero buffer length");
468
469 res = pr_fsio_write(fh, buf, buflen);
470 ck_assert_msg((size_t) res == buflen, "Failed to write %lu bytes: %s",
471 (unsigned long) buflen, strerror(errno));
472
473 (void) pr_fsio_close(fh);
474 (void) pr_fsio_unlink(fsio_test_path);
475 }
476 END_TEST
477
478 START_TEST (fsio_sys_pwrite_test) {
479 ssize_t res;
480 pr_fh_t *fh;
481 char *buf;
482 size_t buflen;
483
484 res = pr_fsio_pwrite(NULL, NULL, 0, 0);
485 ck_assert_msg(res < 0, "Failed to handle null arguments");
486 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
487 strerror(errno), errno);
488
489 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_WRONLY);
490 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", fsio_test_path,
491 strerror(errno));
492
493 /* XXX What happens if we use NULL buffer, zero length? */
494 res = pr_fsio_pwrite(fh, NULL, 0, 0);
495 ck_assert_msg(res < 0, "Failed to handle null buffer");
496 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
497 strerror(errno), errno);
498
499 buflen = 32;
500 buf = palloc(p, buflen);
501 memset(buf, 'c', buflen);
502
503 res = pr_fsio_pwrite(fh, buf, 0, 0);
504 ck_assert_msg(res == 0, "Failed to handle zero buffer length");
505
506 res = pr_fsio_pwrite(fh, buf, buflen, 0);
507 ck_assert_msg((size_t) res == buflen, "Failed to write %lu bytes: %s",
508 (unsigned long) buflen, strerror(errno));
509
510 (void) pr_fsio_close(fh);
511 (void) pr_fsio_unlink(fsio_test_path);
512 }
513 END_TEST
514
515 START_TEST (fsio_sys_lseek_test) {
516 int res;
517 pr_fh_t *fh;
518
519 res = pr_fsio_lseek(NULL, 0, 0);
520 ck_assert_msg(res < 0, "Failed to handle null arguments");
521 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
522 strerror(errno), errno);
523
524 fh = pr_fsio_open("/etc/hosts", O_RDONLY);
525 ck_assert_msg(fh != NULL, "Failed to open /etc/hosts: %s",
526 strerror(errno));
527
528 res = pr_fsio_lseek(fh, 0, 0);
529 ck_assert_msg(res == 0, "Failed to seek to byte 0: %s", strerror(errno));
530
531 (void) pr_fsio_close(fh);
532 }
533 END_TEST
534
535 START_TEST (fsio_sys_link_test) {
536 int res;
537 const char *target_path, *link_path;
538 pr_fh_t *fh;
539
540 target_path = link_path = NULL;
541 res = pr_fsio_link(target_path, link_path);
542 ck_assert_msg(res < 0, "Failed to handle null arguments");
543 ck_assert_msg(errno == EINVAL, "Expected EINVAL, got %s (%d)", strerror(errno),
544 errno);
545
546 target_path = fsio_test_path;
547 link_path = NULL;
548 res = pr_fsio_link(target_path, link_path);
549 ck_assert_msg(res < 0, "Failed to handle null link_path argument");
550 ck_assert_msg(errno == EINVAL, "Expected EINVAL, got %s (%d)", strerror(errno),
551 errno);
552
553 target_path = NULL;
554 link_path = fsio_link_path;
555 res = pr_fsio_link(target_path, link_path);
556 ck_assert_msg(res < 0, "Failed to handle null target_path argument");
557 ck_assert_msg(errno == EINVAL, "Expected EINVAL, got %s (%d)", strerror(errno),
558 errno);
559
560 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_WRONLY);
561 ck_assert_msg(fh != NULL, "Failed to create '%s': %s", fsio_test_path,
562 strerror(errno));
563 (void) pr_fsio_close(fh);
564
565 /* Link a file (that exists) to itself */
566 link_path = target_path = fsio_test_path;
567 res = pr_fsio_link(target_path, link_path);
568 ck_assert_msg(res < 0, "Failed to handle same existing source/destination");
569 ck_assert_msg(errno == EEXIST, "Expected EEXIST, got %s (%d)", strerror(errno),
570 errno);
571
572 /* Create expected link */
573 link_path = fsio_link_path;
574 target_path = fsio_test_path;
575 res = pr_fsio_link(target_path, link_path);
576 ck_assert_msg(res == 0, "Failed to create link from '%s' to '%s': %s",
577 link_path, target_path, strerror(errno));
578 (void) unlink(link_path);
579 (void) pr_fsio_unlink(fsio_test_path);
580 }
581 END_TEST
582
583 START_TEST (fsio_sys_link_chroot_guard_test) {
584 int res;
585
586 res = pr_fsio_guard_chroot(TRUE);
587 ck_assert_msg(res == FALSE, "Expected FALSE (%d), got %d", FALSE, res);
588
589 res = pr_fsio_link(fsio_link_path, "/etc/foo.bar.baz");
590 ck_assert_msg(res < 0, "Linked /etc/foo.bar.baz unexpectedly");
591 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s %d", EACCES,
592 strerror(errno), errno);
593
594 (void) pr_fsio_guard_chroot(FALSE);
595
596 (void) pr_fsio_unlink(fsio_link_path);
597 res = pr_fsio_link(fsio_link_path, "/lib/foo/bar/baz");
598 ck_assert_msg(res < 0, "Linked /lib/foo/bar/baz unexpectedly");
599 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s %d", ENOENT,
600 strerror(errno), errno);
601 }
602 END_TEST
603
604 START_TEST (fsio_sys_symlink_test) {
605 int res;
606 const char *target_path, *link_path;
607
608 target_path = link_path = NULL;
609 res = pr_fsio_symlink(target_path, link_path);
610 ck_assert_msg(res < 0, "Failed to handle null arguments");
611 ck_assert_msg(errno == EINVAL, "Expected EINVAL, got %s (%d)", strerror(errno),
612 errno);
613
614 target_path = "/tmp";
615 link_path = NULL;
616 res = pr_fsio_symlink(target_path, link_path);
617 ck_assert_msg(res < 0, "Failed to handle null link_path argument");
618 ck_assert_msg(errno == EINVAL, "Expected EINVAL, got %s (%d)", strerror(errno),
619 errno);
620
621 target_path = NULL;
622 link_path = fsio_link_path;
623 res = pr_fsio_symlink(target_path, link_path);
624 ck_assert_msg(res < 0, "Failed to handle null target_path argument");
625 ck_assert_msg(errno == EINVAL, "Expected EINVAL, got %s (%d)", strerror(errno),
626 errno);
627
628 /* Symlink a file (that exists) to itself */
629 link_path = target_path = "/tmp";
630 res = pr_fsio_symlink(target_path, link_path);
631 ck_assert_msg(res < 0, "Failed to handle same existing source/destination");
632 ck_assert_msg(errno == EEXIST, "Expected EEXIST, got %s (%d)", strerror(errno),
633 errno);
634
635 /* Create expected symlink */
636 link_path = fsio_link_path;
637 target_path = "/tmp";
638 res = pr_fsio_symlink(target_path, link_path);
639 ck_assert_msg(res == 0, "Failed to create symlink from '%s' to '%s': %s",
640 link_path, target_path, strerror(errno));
641 (void) unlink(link_path);
642 }
643 END_TEST
644
645 START_TEST (fsio_sys_symlink_chroot_guard_test) {
646 int res;
647
648 res = pr_fsio_guard_chroot(TRUE);
649 ck_assert_msg(res == FALSE, "Expected FALSE (%d), got %d", FALSE, res);
650
651 res = pr_fsio_symlink(fsio_link_path, "/etc/foo.bar.baz");
652 ck_assert_msg(res < 0, "Symlinked /etc/foo.bar.baz unexpectedly");
653 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s %d", EACCES,
654 strerror(errno), errno);
655
656 (void) pr_fsio_guard_chroot(FALSE);
657 (void) pr_fsio_unlink(fsio_link_path);
658
659 res = pr_fsio_symlink(fsio_link_path, "/lib/foo/bar/baz");
660 ck_assert_msg(res < 0, "Symlinked /lib/foo/bar/baz unexpectedly");
661 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s %d", ENOENT,
662 strerror(errno), errno);
663 }
664 END_TEST
665
666 START_TEST (fsio_sys_readlink_test) {
667 int res;
668 char buf[PR_TUNABLE_BUFFER_SIZE];
669 const char *link_path, *target_path, *path;
670
671 res = pr_fsio_readlink(NULL, NULL, 0);
672 ck_assert_msg(res < 0, "Failed to handle null arguments");
673 ck_assert_msg(errno == EINVAL, "Expected EINVAL, got %s (%d)", strerror(errno),
674 errno);
675
676 /* Read a non-symlink file */
677 path = "/";
678 res = pr_fsio_readlink(path, buf, sizeof(buf)-1);
679 ck_assert_msg(res < 0, "Failed to handle non-symlink path");
680 ck_assert_msg(errno == EINVAL, "Expected EINVAL, got %s (%d)", strerror(errno),
681 errno);
682
683 /* Read a symlink file */
684 target_path = "/tmp";
685 link_path = fsio_link_path;
686 res = pr_fsio_symlink(target_path, link_path);
687 ck_assert_msg(res == 0, "Failed to create symlink from '%s' to '%s': %s",
688 link_path, target_path, strerror(errno));
689
690 memset(buf, '\0', sizeof(buf));
691 res = pr_fsio_readlink(link_path, buf, sizeof(buf)-1);
692 ck_assert_msg(res > 0, "Failed to read symlink '%s': %s", link_path,
693 strerror(errno));
694 buf[res] = '\0';
695 ck_assert_msg(strcmp(buf, target_path) == 0, "Expected '%s', got '%s'",
696 target_path, buf);
697
698 /* Read a symlink file using a zero-length buffer */
699 res = pr_fsio_readlink(link_path, buf, 0);
700 ck_assert_msg(res <= 0, "Expected length <= 0, got %d", res);
701
702 (void) unlink(link_path);
703 }
704 END_TEST
705
706 START_TEST (fsio_sys_lstat_test) {
707 int res;
708 struct stat st;
709 unsigned int cache_size = 3, max_age = 1, policy_flags = 0;
710
711 res = pr_fsio_lstat(NULL, &st);
712 ck_assert_msg(res < 0, "Failed to handle null path");
713 ck_assert_msg(errno == EINVAL, "Expected EINVAL, got %s (%d)", strerror(errno),
714 errno);
715
716 res = pr_fsio_lstat("/", NULL);
717 ck_assert_msg(res < 0, "Failed to handle null struct stat");
718 ck_assert_msg(errno == EINVAL, "Expected EINVAL, got %s (%d)", strerror(errno),
719 errno);
720
721 res = pr_fsio_lstat("/", &st);
722 ck_assert_msg(res == 0, "Unexpected lstat(2) error on '/': %s",
723 strerror(errno));
724 ck_assert_msg(S_ISDIR(st.st_mode), "'/' is not a directory as expected");
725
726 /* Now, do the lstat(2) again, and make sure we get the same information
727 * from the cache.
728 */
729 res = pr_fsio_lstat("/", &st);
730 ck_assert_msg(res == 0, "Unexpected lstat(2) error on '/': %s",
731 strerror(errno));
732 ck_assert_msg(S_ISDIR(st.st_mode), "'/' is not a directory as expected");
733
734 pr_fs_statcache_reset();
735 res = pr_fs_statcache_set_policy(cache_size, max_age, policy_flags);
736 ck_assert_msg(res == 0, "Failed to set statcache policy: %s", strerror(errno));
737
738 res = pr_fsio_lstat("/foo/bar/baz/quxx", &st);
739 ck_assert_msg(res < 0, "Failed to handle nonexistent path");
740 ck_assert_msg(errno == ENOENT, "Expected ENOENT, got %s (%d)", strerror(errno),
741 errno);
742
743 res = pr_fsio_lstat("/foo/bar/baz/quxx", &st);
744 ck_assert_msg(res < 0, "Failed to handle nonexistent path");
745 ck_assert_msg(errno == ENOENT, "Expected ENOENT, got %s (%d)", strerror(errno),
746 errno);
747
748 /* Now wait for longer than 1 second (our configured max age) */
749 sleep(max_age + 1);
750
751 res = pr_fsio_lstat("/foo/bar/baz/quxx", &st);
752 ck_assert_msg(res < 0, "Failed to handle nonexistent path");
753 ck_assert_msg(errno == ENOENT, "Expected ENOENT, got %s (%d)", strerror(errno),
754 errno);
755
756 /* lstat a symlink path */
757 res = pr_fsio_symlink("/tmp", fsio_link_path);
758 ck_assert_msg(res == 0, "Failed to create symlink to '%s': %s", fsio_link_path,
759 strerror(errno));
760
761 res = pr_fsio_lstat(fsio_link_path, &st);
762 ck_assert_msg(res == 0, "Failed to lstat '%s': %s", fsio_link_path,
763 strerror(errno));
764
765 (void) unlink(fsio_link_path);
766 }
767 END_TEST
768
769 START_TEST (fsio_sys_access_dir_test) {
770 int res;
771 uid_t uid = getuid();
772 gid_t gid = getgid();
773 mode_t perms;
774 array_header *suppl_gids;
775
776 res = pr_fsio_access(NULL, X_OK, uid, gid, NULL);
777 ck_assert_msg(res < 0, "Failed to handle null path");
778 ck_assert_msg(errno == EINVAL, "Expected EINVAL, got %s (%d)", strerror(errno),
779 errno);
780
781 res = pr_fsio_access("/baz/bar/foo", X_OK, uid, gid, NULL);
782 ck_assert_msg(res < 0, "Failed to handle nonexistent path");
783 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
784 strerror(errno), errno);
785
786 /* Make the directory to check; we want it to have perms 771.*/
787 perms = (mode_t) 0771;
788 res = mkdir(fsio_testdir_path, perms);
789 ck_assert_msg(res >= 0, "Unable to create directory '%s': %s", fsio_testdir_path,
790 strerror(errno));
791
792 /* Use chmod(2) to ensure that the directory has the perms we want,
793 * regardless of any umask settings.
794 */
795 res = chmod(fsio_testdir_path, perms);
796 ck_assert_msg(res >= 0, "Unable to set perms %04o on directory '%s': %s", perms,
797 fsio_testdir_path, strerror(errno));
798
799 /* First, check that we ourselves can access our own directory. */
800
801 pr_fs_clear_cache2(fsio_testdir_path);
802 res = pr_fsio_access(fsio_testdir_path, F_OK, uid, gid, NULL);
803 ck_assert_msg(res == 0, "Failed to check for file access on directory: %s",
804 strerror(errno));
805
806 pr_fs_clear_cache2(fsio_testdir_path);
807 res = pr_fsio_access(fsio_testdir_path, R_OK, uid, gid, NULL);
808 ck_assert_msg(res == 0, "Failed to check for read access on directory: %s",
809 strerror(errno));
810
811 pr_fs_clear_cache2(fsio_testdir_path);
812 res = pr_fsio_access(fsio_testdir_path, W_OK, uid, gid, NULL);
813 ck_assert_msg(res == 0, "Failed to check for write access on directory: %s",
814 strerror(errno));
815
816 pr_fs_clear_cache2(fsio_testdir_path);
817 res = pr_fsio_access(fsio_testdir_path, X_OK, uid, gid, NULL);
818 ck_assert_msg(res == 0, "Failed to check for execute access on directory: %s",
819 strerror(errno));
820
821 suppl_gids = make_array(p, 1, sizeof(gid_t));
822 *((gid_t *) push_array(suppl_gids)) = gid;
823
824 pr_fs_clear_cache2(fsio_testdir_path);
825 res = pr_fsio_access(fsio_testdir_path, X_OK, uid, gid, suppl_gids);
826 ck_assert_msg(res == 0, "Failed to check for execute access on directory: %s",
827 strerror(errno));
828
829 pr_fs_clear_cache2(fsio_testdir_path);
830 res = pr_fsio_access(fsio_testdir_path, R_OK, uid, gid, suppl_gids);
831 ck_assert_msg(res == 0, "Failed to check for read access on directory: %s",
832 strerror(errno));
833
834 pr_fs_clear_cache2(fsio_testdir_path);
835 res = pr_fsio_access(fsio_testdir_path, W_OK, uid, gid, suppl_gids);
836 ck_assert_msg(res == 0, "Failed to check for write access on directory: %s",
837 strerror(errno));
838
839 if (getenv("CI") == NULL) {
840 uid_t other_uid;
841 gid_t other_gid;
842
843 /* Deliberately use IDs other than the current ones. */
844 other_uid = uid - 1;
845 other_gid = gid - 1;
846
847 /* Next, check that others can access the directory. */
848 pr_fs_clear_cache2(fsio_testdir_path);
849 res = pr_fsio_access(fsio_testdir_path, F_OK, other_uid, other_gid,
850 NULL);
851 ck_assert_msg(res == 0,
852 "Failed to check for other file access on directory: %s",
853 strerror(errno));
854
855 pr_fs_clear_cache2(fsio_testdir_path);
856 res = pr_fsio_access(fsio_testdir_path, R_OK, other_uid, other_gid,
857 NULL);
858 ck_assert_msg(res < 0,
859 "other read access on directory succeeded unexpectedly");
860 ck_assert_msg(errno == EACCES, "Expected EACCES, got %s (%d)",
861 strerror(errno), errno);
862
863 pr_fs_clear_cache2(fsio_testdir_path);
864 res = pr_fsio_access(fsio_testdir_path, W_OK, other_uid, other_gid,
865 NULL);
866 ck_assert_msg(res < 0,
867 "other write access on directory succeeded unexpectedly");
868 ck_assert_msg(errno == EACCES, "Expected EACCES, got %s (%d)",
869 strerror(errno), errno);
870
871 pr_fs_clear_cache2(fsio_testdir_path);
872 res = pr_fsio_access(fsio_testdir_path, X_OK, other_uid, other_gid,
873 NULL);
874 ck_assert_msg(res == 0, "Failed to check for execute access on directory: %s",
875 strerror(errno));
876 }
877
878 (void) rmdir(fsio_testdir_path);
879 }
880 END_TEST
881
882 START_TEST (fsio_sys_access_file_test) {
883 int fd, res;
884 uid_t uid = getuid();
885 gid_t gid = getgid();
886 mode_t perms = 0665;
887 array_header *suppl_gids;
888
889 /* Make the file to check; we want it to have perms 664.*/
890 fd = open(fsio_test_path, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR);
891 ck_assert_msg(fd >= 0, "Unable to create file '%s': %s", fsio_test_path,
892 strerror(errno));
893
894 /* Use chmod(2) to ensure that the file has the perms we want,
895 * regardless of any umask settings.
896 */
897 res = chmod(fsio_test_path, perms);
898 ck_assert_msg(res >= 0, "Unable to set perms %04o on file '%s': %s", perms,
899 fsio_test_path, strerror(errno));
900
901 /* First, check that we ourselves can access our own file. */
902
903 pr_fs_clear_cache2(fsio_test_path);
904 res = pr_fsio_access(fsio_test_path, F_OK, uid, gid, NULL);
905 ck_assert_msg(res == 0, "Failed to check for file access on '%s': %s",
906 fsio_test_path, strerror(errno));
907
908 pr_fs_clear_cache2(fsio_test_path);
909 res = pr_fsio_access(fsio_test_path, R_OK, uid, gid, NULL);
910 ck_assert_msg(res == 0, "Failed to check for read access on '%s': %s",
911 fsio_test_path, strerror(errno));
912
913 pr_fs_clear_cache2(fsio_test_path);
914 res = pr_fsio_access(fsio_test_path, W_OK, uid, gid, NULL);
915 ck_assert_msg(res == 0, "Failed to check for write access on '%s': %s",
916 fsio_test_path, strerror(errno));
917
918 pr_fs_clear_cache2(fsio_test_path);
919 res = pr_fsio_access(fsio_test_path, X_OK, uid, gid, NULL);
920 ck_assert_msg(res == 0, "Failed to check for execute access on '%s': %s",
921 fsio_test_path, strerror(errno));
922
923 suppl_gids = make_array(p, 1, sizeof(gid_t));
924 *((gid_t *) push_array(suppl_gids)) = gid;
925
926 pr_fs_clear_cache2(fsio_test_path);
927 res = pr_fsio_access(fsio_test_path, X_OK, uid, gid, suppl_gids);
928 ck_assert_msg(res == 0, "Failed to check for execute access on '%s': %s",
929 fsio_test_path, strerror(errno));
930
931 pr_fs_clear_cache2(fsio_test_path);
932 res = pr_fsio_access(fsio_test_path, R_OK, uid, gid, suppl_gids);
933 ck_assert_msg(res == 0, "Failed to check for read access on '%s': %s",
934 fsio_test_path, strerror(errno));
935
936 pr_fs_clear_cache2(fsio_test_path);
937 res = pr_fsio_access(fsio_test_path, W_OK, uid, gid, suppl_gids);
938 ck_assert_msg(res == 0, "Failed to check for write access on '%s': %s",
939 fsio_test_path, strerror(errno));
940
941 (void) unlink(fsio_test_path);
942 }
943 END_TEST
944
945 START_TEST (fsio_sys_faccess_test) {
946 int res;
947 uid_t uid = getuid();
948 gid_t gid = getgid();
949 mode_t perms = 0664;
950 pr_fh_t *fh;
951
952 res = pr_fsio_faccess(NULL, F_OK, uid, gid, NULL);
953 ck_assert_msg(res < 0, "Failed to handle null arguments");
954 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
955 strerror(errno), errno);
956
957 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_WRONLY);
958 ck_assert_msg(fh != NULL, "Unable to create file '%s': %s", fsio_test_path,
959 strerror(errno));
960
961 /* Use chmod(2) to ensure that the file has the perms we want,
962 * regardless of any umask settings.
963 */
964 res = chmod(fsio_test_path, perms);
965 ck_assert_msg(res >= 0, "Unable to set perms %04o on file '%s': %s", perms,
966 fsio_test_path, strerror(errno));
967
968 /* First, check that we ourselves can access our own file. */
969
970 pr_fs_clear_cache2(fsio_test_path);
971 res = pr_fsio_faccess(fh, F_OK, uid, gid, NULL);
972 ck_assert_msg(res == 0, "Failed to check for file access on '%s': %s",
973 fsio_test_path, strerror(errno));
974
975 pr_fs_clear_cache2(fsio_test_path);
976 res = pr_fsio_faccess(fh, R_OK, uid, gid, NULL);
977 ck_assert_msg(res == 0, "Failed to check for read access on '%s': %s",
978 fsio_test_path, strerror(errno));
979
980 pr_fs_clear_cache2(fsio_test_path);
981 res = pr_fsio_faccess(fh, W_OK, uid, gid, NULL);
982 ck_assert_msg(res == 0, "Failed to check for write access on '%s': %s",
983 fsio_test_path, strerror(errno));
984
985 pr_fs_clear_cache2(fsio_test_path);
986 res = pr_fsio_faccess(fh, X_OK, uid, gid, NULL);
987 ck_assert_msg(res < 0,
988 "Check for execute access on '%s' succeeded unexpectedly", fsio_test_path);
989
990 (void) pr_fsio_close(fh);
991 (void) pr_fsio_unlink(fsio_test_path);
992 }
993 END_TEST
994
995 START_TEST (fsio_sys_truncate_test) {
996 int res;
997 off_t len = 0;
998 pr_fh_t *fh;
999
1000 res = pr_fsio_truncate(NULL, 0);
1001 ck_assert_msg(res < 0, "Failed to handle null arguments");
1002 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1003 strerror(errno), errno);
1004
1005 res = pr_fsio_truncate(fsio_test_path, 0);
1006 ck_assert_msg(res < 0, "Truncated '%s' unexpectedly", fsio_test_path);
1007 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
1008 strerror(errno), errno);
1009
1010 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_WRONLY);
1011 ck_assert_msg(fh != NULL, "Failed to create '%s': %s", fsio_test_path,
1012 strerror(errno));
1013
1014 res = pr_fsio_truncate(fsio_test_path, len);
1015 ck_assert_msg(res == 0, "Failed to truncate '%s': %s", fsio_test_path,
1016 strerror(errno));
1017
1018 (void) pr_fsio_close(fh);
1019 (void) pr_fsio_unlink(fsio_test_path);
1020 }
1021 END_TEST
1022
1023 START_TEST (fsio_sys_truncate_chroot_guard_test) {
1024 int res;
1025
1026 res = pr_fsio_guard_chroot(TRUE);
1027 ck_assert_msg(res == FALSE, "Expected FALSE (%d), got %d", FALSE, res);
1028
1029 res = pr_fsio_truncate("/etc/foo.bar.baz", 0);
1030 ck_assert_msg(res < 0, "Truncated /etc/foo.bar.baz unexpectedly");
1031 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s %d", EACCES,
1032 strerror(errno), errno);
1033
1034 (void) pr_fsio_guard_chroot(FALSE);
1035
1036 res = pr_fsio_truncate("/lib/foo/bar/baz", 0);
1037 ck_assert_msg(res < 0, "Truncated /lib/foo/bar/baz unexpectedly");
1038 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s %d", ENOENT,
1039 strerror(errno), errno);
1040 }
1041 END_TEST
1042
1043 START_TEST (fsio_sys_ftruncate_test) {
1044 int res;
1045 off_t len = 0;
1046 pr_fh_t *fh;
1047 pr_buffer_t *buf;
1048
1049 res = pr_fsio_ftruncate(NULL, 0);
1050 ck_assert_msg(res < 0, "Failed to handle null arguments");
1051 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1052 strerror(errno), errno);
1053
1054 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_WRONLY);
1055 ck_assert_msg(fh != NULL, "Failed to create '%s': %s", fsio_test_path,
1056 strerror(errno));
1057
1058 mark_point();
1059 res = pr_fsio_ftruncate(fh, len);
1060 ck_assert_msg(res == 0, "Failed to truncate '%s': %s", fsio_test_path,
1061 strerror(errno));
1062
1063 /* Attach a read buffer to the handle, make sure it is cleared. */
1064 buf = pcalloc(fh->fh_pool, sizeof(pr_buffer_t));
1065 buf->buflen = 100;
1066 buf->remaining = 1;
1067
1068 fh->fh_buf = buf;
1069
1070 mark_point();
1071 res = pr_fsio_ftruncate(fh, len);
1072 ck_assert_msg(res == 0, "Failed to truncate '%s': %s", fsio_test_path,
1073 strerror(errno));
1074 ck_assert_msg(buf->remaining == buf->buflen,
1075 "Expected %lu, got %lu", (unsigned long) buf->buflen,
1076 (unsigned long) buf->remaining);
1077
1078 (void) pr_fsio_close(fh);
1079 (void) pr_fsio_unlink(fsio_test_path);
1080 }
1081 END_TEST
1082
1083 START_TEST (fsio_sys_chmod_test) {
1084 int res;
1085 mode_t mode = 0644;
1086 pr_fh_t *fh;
1087
1088 res = pr_fsio_chmod(NULL, mode);
1089 ck_assert_msg(res < 0, "Failed to handle null arguments");
1090 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1091 strerror(errno), errno);
1092
1093 res = pr_fsio_chmod(fsio_test_path, 0);
1094 ck_assert_msg(res < 0, "Changed perms of '%s' unexpectedly", fsio_test_path);
1095 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
1096 strerror(errno), errno);
1097
1098 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_WRONLY);
1099 ck_assert_msg(fh != NULL, "Failed to create '%s': %s", fsio_test_path,
1100 strerror(errno));
1101
1102 res = pr_fsio_chmod(fsio_test_path, mode);
1103 ck_assert_msg(res == 0, "Failed to set perms of '%s': %s", fsio_test_path,
1104 strerror(errno));
1105
1106 (void) pr_fsio_close(fh);
1107 (void) pr_fsio_unlink(fsio_test_path);
1108 }
1109 END_TEST
1110
1111 START_TEST (fsio_sys_chmod_chroot_guard_test) {
1112 int res;
1113 mode_t mode = 0644;
1114
1115 res = pr_fsio_guard_chroot(TRUE);
1116 ck_assert_msg(res == FALSE, "Expected FALSE (%d), got %d", FALSE, res);
1117
1118 res = pr_fsio_chmod("/etc/foo.bar.baz", mode);
1119 ck_assert_msg(res < 0, "Set mode on /etc/foo.bar.baz unexpectedly");
1120 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s %d", EACCES,
1121 strerror(errno), errno);
1122
1123 (void) pr_fsio_guard_chroot(FALSE);
1124
1125 res = pr_fsio_chmod("/lib/foo/bar/baz", mode);
1126 ck_assert_msg(res < 0, "Set mode on /lib/foo/bar/baz unexpectedly");
1127 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s %d", ENOENT,
1128 strerror(errno), errno);
1129 }
1130 END_TEST
1131
1132 START_TEST (fsio_sys_fchmod_test) {
1133 int res;
1134 mode_t mode = 0644;
1135 pr_fh_t *fh;
1136
1137 res = pr_fsio_fchmod(NULL, mode);
1138 ck_assert_msg(res < 0, "Failed to handle null arguments");
1139 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1140 strerror(errno), errno);
1141
1142 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_WRONLY);
1143 ck_assert_msg(fh != NULL, "Failed to create '%s': %s", fsio_test_path,
1144 strerror(errno));
1145
1146 res = pr_fsio_fchmod(fh, mode);
1147 ck_assert_msg(res == 0, "Failed to set perms of '%s': %s", fsio_test_path,
1148 strerror(errno));
1149
1150 (void) pr_fsio_close(fh);
1151 (void) pr_fsio_unlink(fsio_test_path);
1152 }
1153 END_TEST
1154
1155 START_TEST (fsio_sys_chown_test) {
1156 int res;
1157 uid_t uid = getuid();
1158 gid_t gid = getgid();
1159 pr_fh_t *fh;
1160
1161 res = pr_fsio_chown(NULL, uid, gid);
1162 ck_assert_msg(res < 0, "Failed to handle null arguments");
1163 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1164 strerror(errno), errno);
1165
1166 res = pr_fsio_chown(fsio_test_path, uid, gid);
1167 ck_assert_msg(res < 0, "Changed ownership of '%s' unexpectedly",
1168 fsio_test_path);
1169 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
1170 strerror(errno), errno);
1171
1172 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_WRONLY);
1173 ck_assert_msg(fh != NULL, "Failed to create '%s': %s", fsio_test_path,
1174 strerror(errno));
1175
1176 res = pr_fsio_chown(fsio_test_path, uid, gid);
1177 ck_assert_msg(res == 0, "Failed to set ownership of '%s': %s", fsio_test_path,
1178 strerror(errno));
1179
1180 (void) pr_fsio_close(fh);
1181 (void) pr_fsio_unlink(fsio_test_path);
1182 }
1183 END_TEST
1184
1185 START_TEST (fsio_sys_chown_chroot_guard_test) {
1186 int res;
1187 uid_t uid = getuid();
1188 gid_t gid = getgid();
1189
1190 res = pr_fsio_guard_chroot(TRUE);
1191 ck_assert_msg(res == FALSE, "Expected FALSE (%d), got %d", FALSE, res);
1192
1193 res = pr_fsio_chown("/etc/foo.bar.baz", uid, gid);
1194 ck_assert_msg(res < 0, "Set ownership on /etc/foo.bar.baz unexpectedly");
1195 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s %d", EACCES,
1196 strerror(errno), errno);
1197
1198 (void) pr_fsio_guard_chroot(FALSE);
1199
1200 res = pr_fsio_chown("/lib/foo/bar/baz", uid, gid);
1201 ck_assert_msg(res < 0, "Set ownership on /lib/foo/bar/baz unexpectedly");
1202 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s %d", ENOENT,
1203 strerror(errno), errno);
1204 }
1205 END_TEST
1206
1207 START_TEST (fsio_sys_fchown_test) {
1208 int res;
1209 uid_t uid = getuid();
1210 gid_t gid = getgid();
1211 pr_fh_t *fh;
1212
1213 res = pr_fsio_fchown(NULL, uid, gid);
1214 ck_assert_msg(res < 0, "Failed to handle null arguments");
1215 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1216 strerror(errno), errno);
1217
1218 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_WRONLY);
1219 ck_assert_msg(fh != NULL, "Failed to create '%s': %s", fsio_test_path,
1220 strerror(errno));
1221
1222 res = pr_fsio_fchown(fh, uid, gid);
1223 ck_assert_msg(res == 0, "Failed to set ownership of '%s': %s", fsio_test_path,
1224 strerror(errno));
1225
1226 (void) pr_fsio_close(fh);
1227 (void) pr_fsio_unlink(fsio_test_path);
1228 }
1229 END_TEST
1230
1231 START_TEST (fsio_sys_lchown_test) {
1232 int res;
1233 uid_t uid = getuid();
1234 gid_t gid = getgid();
1235 pr_fh_t *fh;
1236
1237 res = pr_fsio_lchown(NULL, uid, gid);
1238 ck_assert_msg(res < 0, "Failed to handle null arguments");
1239 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1240 strerror(errno), errno);
1241
1242 res = pr_fsio_lchown(fsio_test_path, uid, gid);
1243 ck_assert_msg(res < 0, "Changed ownership of '%s' unexpectedly",
1244 fsio_test_path);
1245 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
1246 strerror(errno), errno);
1247
1248 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_WRONLY);
1249 ck_assert_msg(fh != NULL, "Failed to create '%s': %s", fsio_test_path,
1250 strerror(errno));
1251
1252 res = pr_fsio_lchown(fsio_test_path, uid, gid);
1253 ck_assert_msg(res == 0, "Failed to set ownership of '%s': %s", fsio_test_path,
1254 strerror(errno));
1255
1256 (void) pr_fsio_close(fh);
1257 (void) pr_fsio_unlink(fsio_test_path);
1258 }
1259 END_TEST
1260
1261 START_TEST (fsio_sys_lchown_chroot_guard_test) {
1262 int res;
1263 uid_t uid = getuid();
1264 gid_t gid = getgid();
1265
1266 res = pr_fsio_guard_chroot(TRUE);
1267 ck_assert_msg(res == FALSE, "Expected FALSE (%d), got %d", FALSE, res);
1268
1269 res = pr_fsio_lchown("/etc/foo.bar.baz", uid, gid);
1270 ck_assert_msg(res < 0, "Set ownership on /etc/foo.bar.baz unexpectedly");
1271 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s %d", EACCES,
1272 strerror(errno), errno);
1273
1274 (void) pr_fsio_guard_chroot(FALSE);
1275
1276 res = pr_fsio_lchown("/lib/foo/bar/baz", uid, gid);
1277 ck_assert_msg(res < 0, "Set ownership on /lib/foo/bar/baz unexpectedly");
1278 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s %d", ENOENT,
1279 strerror(errno), errno);
1280 }
1281 END_TEST
1282
1283 START_TEST (fsio_sys_rename_test) {
1284 int res;
1285 pr_fh_t *fh;
1286
1287 res = pr_fsio_rename(NULL, NULL);
1288 ck_assert_msg(res < 0, "Failed to handle null arguments");
1289 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1290 strerror(errno), errno);
1291
1292 res = pr_fsio_rename(fsio_test_path, NULL);
1293 ck_assert_msg(res < 0, "Failed to handle null dst argument");
1294 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1295 strerror(errno), errno);
1296
1297 res = pr_fsio_rename(fsio_test_path, fsio_test2_path);
1298 ck_assert_msg(res < 0, "Failed to handle non-existent files");
1299 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
1300 strerror(errno), errno);
1301
1302 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_WRONLY);
1303 ck_assert_msg(fh != NULL, "Failed to create '%s': %s", fsio_test_path,
1304 strerror(errno));
1305 (void) pr_fsio_close(fh);
1306
1307 res = pr_fsio_rename(fsio_test_path, fsio_test2_path);
1308 ck_assert_msg(res == 0, "Failed to rename '%s' to '%s': %s", fsio_test_path,
1309 fsio_test2_path, strerror(errno));
1310
1311 (void) pr_fsio_unlink(fsio_test_path);
1312 (void) pr_fsio_unlink(fsio_test2_path);
1313 }
1314 END_TEST
1315
1316 START_TEST (fsio_sys_rename_chroot_guard_test) {
1317 int res;
1318 pr_fh_t *fh;
1319
1320 res = pr_fsio_guard_chroot(TRUE);
1321 ck_assert_msg(res == FALSE, "Expected FALSE (%d), got %d", FALSE, res);
1322
1323 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_WRONLY);
1324 ck_assert_msg(fh != NULL, "Failed to create '%s': %s", fsio_test_path,
1325 strerror(errno));
1326 (void) pr_fsio_close(fh);
1327
1328 res = pr_fsio_rename(fsio_test_path, "/etc/foo.bar.baz");
1329 ck_assert_msg(res < 0, "Renamed '%s' unexpectedly", fsio_test_path);
1330 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s (%d)", EACCES,
1331 strerror(errno), errno);
1332
1333 res = pr_fsio_rename("/etc/foo.bar.baz", fsio_test_path);
1334 ck_assert_msg(res < 0, "Renamed '/etc/foo.bar.baz' unexpectedly");
1335 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s (%d)", EACCES,
1336 strerror(errno), errno);
1337
1338 (void) pr_fsio_guard_chroot(FALSE);
1339
1340 res = pr_fsio_rename("/etc/foo/bar/baz", "/lib/quxx/quzz");
1341 ck_assert_msg(res < 0, "Renamed '/etc/foo/bar/baz' unexpectedly");
1342 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
1343 strerror(errno), errno);
1344
1345 (void) pr_fsio_unlink(fsio_test_path);
1346 }
1347 END_TEST
1348
1349 START_TEST (fsio_sys_utimes_test) {
1350 int res;
1351 struct timeval tvs[3];
1352 pr_fh_t *fh;
1353
1354 memset(tvs, 0, sizeof(tvs));
1355
1356 res = pr_fsio_utimes(NULL, NULL);
1357 ck_assert_msg(res < 0, "Failed to handle null arguments");
1358 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1359 strerror(errno), errno);
1360
1361 res = pr_fsio_utimes(fsio_test_path, (struct timeval *) &tvs);
1362 ck_assert_msg(res < 0, "Changed times of '%s' unexpectedly", fsio_test_path);
1363 ck_assert_msg(errno == ENOENT || errno == EINVAL,
1364 "Expected ENOENT (%d) or EINVAL (%d), got %s (%d)", ENOENT, EINVAL,
1365 strerror(errno), errno);
1366
1367 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_WRONLY);
1368 ck_assert_msg(fh != NULL, "Failed to create '%s': %s", fsio_test_path,
1369 strerror(errno));
1370
1371 memset(&tvs, 0, sizeof(tvs));
1372 res = pr_fsio_utimes(fsio_test_path, (struct timeval *) &tvs);
1373 ck_assert_msg(res == 0, "Failed to set times of '%s': %s", fsio_test_path,
1374 strerror(errno));
1375
1376 (void) pr_fsio_close(fh);
1377 (void) pr_fsio_unlink(fsio_test_path);
1378 }
1379 END_TEST
1380
1381 START_TEST (fsio_sys_utimes_chroot_guard_test) {
1382 int res;
1383 struct timeval tvs[3];
1384
1385 memset(tvs, 0, sizeof(tvs));
1386
1387 res = pr_fsio_guard_chroot(TRUE);
1388 ck_assert_msg(res == FALSE, "Expected FALSE (%d), got %d", FALSE, res);
1389
1390 res = pr_fsio_utimes("/etc/foo.bar.baz", (struct timeval *) &tvs);
1391 ck_assert_msg(res < 0, "Set times on /etc/foo.bar.baz unexpectedly");
1392 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s %d", EACCES,
1393 strerror(errno), errno);
1394
1395 (void) pr_fsio_guard_chroot(FALSE);
1396
1397 res = pr_fsio_utimes("/lib/foo/bar/baz", (struct timeval *) &tvs);
1398 ck_assert_msg(res < 0, "Set times on /lib/foo/bar/baz unexpectedly");
1399 ck_assert_msg(errno == ENOENT || errno == EINVAL,
1400 "Expected ENOENT (%d) or EINVAL (%d), got %s %d", ENOENT, EINVAL,
1401 strerror(errno), errno);
1402 }
1403 END_TEST
1404
1405 START_TEST (fsio_sys_futimes_test) {
1406 int res;
1407 struct timeval tvs[3];
1408 pr_fh_t *fh;
1409
1410 memset(tvs, 0, sizeof(tvs));
1411
1412 res = pr_fsio_futimes(NULL, NULL);
1413 ck_assert_msg(res < 0, "Failed to handle null arguments");
1414 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1415 strerror(errno), errno);
1416
1417 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_WRONLY);
1418 ck_assert_msg(fh != NULL, "Failed to create '%s': %s", fsio_test_path,
1419 strerror(errno));
1420
1421 memset(&tvs, 0, sizeof(tvs));
1422 res = pr_fsio_futimes(fh, (struct timeval *) &tvs);
1423 ck_assert_msg(res == 0, "Failed to set times of '%s': %s", fsio_test_path,
1424 strerror(errno));
1425
1426 (void) pr_fsio_close(fh);
1427 (void) pr_fsio_unlink(fsio_test_path);
1428 }
1429 END_TEST
1430
1431 START_TEST (fsio_sys_fsync_test) {
1432 int res;
1433 pr_fh_t *fh;
1434
1435 res = pr_fsio_fsync(NULL);
1436 ck_assert_msg(res < 0, "Failed to handle null arguments");
1437 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1438 strerror(errno), errno);
1439
1440 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_WRONLY);
1441 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", fsio_test_path,
1442 strerror(errno));
1443
1444 res = pr_fsio_fsync(fh);
1445 #ifdef HAVE_FSYNC
1446 ck_assert_msg(res == 0, "fsync of '%s' failed: %s", fsio_test_path,
1447 strerror(errno));
1448 #else
1449 ck_assert_msg(res < 0, "fsync of '%s' succeeded unexpectedly", fsio_test_path);
1450 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
1451 strerror(errno), errno);
1452 #endif /* HAVE_FSYNC */
1453
1454 (void) pr_fsio_close(fh);
1455 (void) pr_fsio_unlink(fsio_test_path);
1456 }
1457 END_TEST
1458
1459 START_TEST (fsio_sys_realpath_test) {
1460 const char *res;
1461
1462 mark_point();
1463 res = pr_fsio_realpath(NULL, NULL);
1464 ck_assert_msg(res == NULL, "Failed to handle null pool");
1465 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1466 strerror(errno), errno);
1467
1468 mark_point();
1469 res = pr_fsio_realpath(p, NULL);
1470 ck_assert_msg(res == NULL, "Failed to handle null path");
1471 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1472 strerror(errno), errno);
1473
1474 mark_point();
1475 res = pr_fsio_realpath(p, "/tmp");
1476 ck_assert_msg(res != NULL, "Failed to resolve path '/tmp': %s",
1477 strerror(errno));
1478 }
1479 END_TEST
1480
1481 START_TEST (fsio_sys_getxattr_test) {
1482 ssize_t res;
1483 const char *path, *name;
1484 unsigned long fsio_opts;
1485
1486 res = pr_fsio_getxattr(NULL, NULL, NULL, NULL, 0);
1487 ck_assert_msg(res < 0, "Failed to handle null arguments");
1488 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1489 strerror(errno), errno);
1490
1491 res = pr_fsio_getxattr(p, NULL, NULL, NULL, 0);
1492 ck_assert_msg(res < 0, "Failed to handle null path");
1493 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1494 strerror(errno), errno);
1495
1496 path = fsio_test_path;
1497 res = pr_fsio_getxattr(p, path, NULL, NULL, 0);
1498 ck_assert_msg(res < 0, "Failed to handle null arguments");
1499 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1500 strerror(errno), errno);
1501
1502 name = "foo.bar";
1503
1504 fsio_opts = pr_fsio_set_options(PR_FSIO_OPT_IGNORE_XATTR);
1505 res = pr_fsio_getxattr(p, path, name, NULL, 0);
1506 ck_assert_msg(res < 0, "Failed to handle disabled xattr");
1507 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
1508 strerror(errno), errno);
1509
1510 (void) pr_fsio_set_options(fsio_opts);
1511 res = pr_fsio_getxattr(p, path, name, NULL, 0);
1512 #ifdef PR_USE_XATTR
1513 ck_assert_msg(res < 0, "Failed to handle nonexist attribute '%s'", name);
1514 ck_assert_msg(errno == ENOENT || errno == ENOATTR || errno == ENOTSUP,
1515 "Expected ENOENT (%d), ENOATTR (%d) or ENOTSUP (%d), got %s (%d)",
1516 ENOENT, ENOATTR, ENOTSUP, strerror(errno), errno);
1517
1518 #else
1519 ck_assert_msg(res < 0, "Failed to handle --disable-xattr");
1520 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
1521 strerror(errno), errno);
1522 #endif /* PR_USE_XATTR */
1523 }
1524 END_TEST
1525
1526 START_TEST (fsio_sys_lgetxattr_test) {
1527 ssize_t res;
1528 const char *path, *name;
1529 unsigned long fsio_opts;
1530
1531 res = pr_fsio_lgetxattr(NULL, NULL, NULL, NULL, 0);
1532 ck_assert_msg(res < 0, "Failed to handle null arguments");
1533 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1534 strerror(errno), errno);
1535
1536 res = pr_fsio_lgetxattr(p, NULL, NULL, NULL, 0);
1537 ck_assert_msg(res < 0, "Failed to handle null path");
1538 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1539 strerror(errno), errno);
1540
1541 path = fsio_test_path;
1542 res = pr_fsio_lgetxattr(p, path, NULL, NULL, 0);
1543 ck_assert_msg(res < 0, "Failed to handle null xattr name");
1544 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1545 strerror(errno), errno);
1546
1547 name = "foo.bar";
1548
1549 fsio_opts = pr_fsio_set_options(PR_FSIO_OPT_IGNORE_XATTR);
1550 res = pr_fsio_lgetxattr(p, path, name, NULL, 0);
1551 ck_assert_msg(res < 0, "Failed to handle disabled xattr");
1552 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
1553 strerror(errno), errno);
1554
1555 pr_fsio_set_options(fsio_opts);
1556 res = pr_fsio_lgetxattr(p, path, name, NULL, 0);
1557 #ifdef PR_USE_XATTR
1558 ck_assert_msg(res < 0, "Failed to handle nonexist attribute '%s'", name);
1559 ck_assert_msg(errno == ENOENT || errno == ENOATTR || errno == ENOTSUP,
1560 "Expected ENOENT (%d), ENOATTR (%d) or ENOTSUP (%d), got %s (%d)",
1561 ENOENT, ENOATTR, ENOTSUP, strerror(errno), errno);
1562
1563 #else
1564 ck_assert_msg(res < 0, "Failed to handle --disable-xattr");
1565 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
1566 strerror(errno), errno);
1567 #endif /* PR_USE_XATTR */
1568 }
1569 END_TEST
1570
1571 START_TEST (fsio_sys_fgetxattr_test) {
1572 ssize_t res;
1573 pr_fh_t *fh;
1574 const char *name;
1575 unsigned long fsio_opts;
1576
1577 res = pr_fsio_fgetxattr(NULL, NULL, NULL, NULL, 0);
1578 ck_assert_msg(res < 0, "Failed to handle null arguments");
1579 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1580 strerror(errno), errno);
1581
1582 res = pr_fsio_fgetxattr(p, NULL, NULL, NULL, 0);
1583 ck_assert_msg(res < 0, "Failed to handle null file handle");
1584 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1585 strerror(errno), errno);
1586
1587 (void) unlink(fsio_test_path);
1588 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_RDWR);
1589 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", fsio_test_path,
1590 strerror(errno));
1591
1592 res = pr_fsio_fgetxattr(p, fh, NULL, NULL, 0);
1593 ck_assert_msg(res < 0, "Failed to handle null xattr name");
1594 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1595 strerror(errno), errno);
1596
1597 name = "foo.bar";
1598
1599 fsio_opts = pr_fsio_set_options(PR_FSIO_OPT_IGNORE_XATTR);
1600 res = pr_fsio_fgetxattr(p, fh, name, NULL, 0);
1601 ck_assert_msg(res < 0, "Failed to handle disabled xattr");
1602 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
1603 strerror(errno), errno);
1604
1605 pr_fsio_set_options(fsio_opts);
1606 res = pr_fsio_fgetxattr(p, fh, name, NULL, 0);
1607 #ifdef PR_USE_XATTR
1608 ck_assert_msg(res < 0, "Failed to handle nonexist attribute '%s'", name);
1609 ck_assert_msg(errno == ENOENT || errno == ENOATTR || errno == ENOTSUP,
1610 "Expected ENOENT (%d), ENOATTR (%d) or ENOTSUP (%d), got %s (%d)",
1611 ENOENT, ENOATTR, ENOTSUP, strerror(errno), errno);
1612
1613 #else
1614 ck_assert_msg(res < 0, "Failed to handle --disable-xattr");
1615 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
1616 strerror(errno), errno);
1617 #endif /* PR_USE_XATTR */
1618
1619 pr_fsio_close(fh);
1620 (void) unlink(fsio_test_path);
1621 }
1622 END_TEST
1623
1624 START_TEST (fsio_sys_listxattr_test) {
1625 int res;
1626 const char *path;
1627 pr_fh_t *fh = NULL;
1628 array_header *names = NULL;
1629 unsigned long fsio_opts;
1630
1631 res = pr_fsio_listxattr(NULL, NULL, NULL);
1632 ck_assert_msg(res < 0, "Failed to handle null arguments");
1633 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1634 strerror(errno), errno);
1635
1636 res = pr_fsio_listxattr(p, NULL, NULL);
1637 ck_assert_msg(res < 0, "Failed to handle null path");
1638 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1639 strerror(errno), errno);
1640
1641 path = fsio_test_path;
1642 res = pr_fsio_listxattr(p, path, NULL);
1643 ck_assert_msg(res < 0, "Failed to handle null array");
1644 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1645 strerror(errno), errno);
1646
1647 fsio_opts = pr_fsio_set_options(PR_FSIO_OPT_IGNORE_XATTR);
1648 res = pr_fsio_listxattr(p, path, &names);
1649 ck_assert_msg(res < 0, "Failed to handle disabled xattr");
1650 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
1651 strerror(errno), errno);
1652
1653 pr_fsio_set_options(fsio_opts);
1654 res = pr_fsio_listxattr(p, path, &names);
1655 #ifdef PR_USE_XATTR
1656 ck_assert_msg(res < 0, "Failed to handle nonexistent path '%s'", path);
1657 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
1658 strerror(errno), errno);
1659
1660 (void) unlink(fsio_test_path);
1661 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_WRONLY);
1662 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", fsio_test_path,
1663 strerror(errno));
1664 pr_fsio_close(fh);
1665
1666 res = pr_fsio_listxattr(p, path, &names);
1667 ck_assert_msg(res >= 0, "Failed to list xattrs for '%s': %s", path, strerror(errno));
1668
1669 (void) unlink(fsio_test_path);
1670 #else
1671 (void) fh;
1672 ck_assert_msg(res < 0, "Failed to handle --disable-xattr");
1673 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
1674 strerror(errno), errno);
1675 #endif /* PR_USE_XATTR */
1676 }
1677 END_TEST
1678
1679 START_TEST (fsio_sys_llistxattr_test) {
1680 int res;
1681 const char *path;
1682 pr_fh_t *fh = NULL;
1683 array_header *names = NULL;
1684 unsigned long fsio_opts;
1685
1686 res = pr_fsio_llistxattr(NULL, NULL, NULL);
1687 ck_assert_msg(res < 0, "Failed to handle null arguments");
1688 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1689 strerror(errno), errno);
1690
1691 res = pr_fsio_llistxattr(p, NULL, NULL);
1692 ck_assert_msg(res < 0, "Failed to handle null path");
1693 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1694 strerror(errno), errno);
1695
1696 path = fsio_test_path;
1697 res = pr_fsio_llistxattr(p, path, NULL);
1698 ck_assert_msg(res < 0, "Failed to handle null array");
1699 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1700 strerror(errno), errno);
1701
1702 fsio_opts = pr_fsio_set_options(PR_FSIO_OPT_IGNORE_XATTR);
1703 res = pr_fsio_llistxattr(p, path, &names);
1704 ck_assert_msg(res < 0, "Failed to handle disabled xattr");
1705 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
1706 strerror(errno), errno);
1707
1708 pr_fsio_set_options(fsio_opts);
1709 res = pr_fsio_llistxattr(p, path, &names);
1710 #ifdef PR_USE_XATTR
1711 ck_assert_msg(res < 0, "Failed to handle nonexistent path '%s'", path);
1712 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
1713 strerror(errno), errno);
1714
1715 (void) unlink(fsio_test_path);
1716 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_WRONLY);
1717 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", fsio_test_path,
1718 strerror(errno));
1719 pr_fsio_close(fh);
1720
1721 res = pr_fsio_listxattr(p, path, &names);
1722 ck_assert_msg(res >= 0, "Failed to list xattrs for '%s': %s", path, strerror(errno));
1723
1724 (void) unlink(fsio_test_path);
1725 #else
1726 (void) fh;
1727 ck_assert_msg(res < 0, "Failed to handle --disable-xattr");
1728 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
1729 strerror(errno), errno);
1730 #endif /* PR_USE_XATTR */
1731 }
1732 END_TEST
1733
1734 START_TEST (fsio_sys_flistxattr_test) {
1735 int res;
1736 pr_fh_t *fh;
1737 array_header *names = NULL;
1738 unsigned long fsio_opts;
1739
1740 res = pr_fsio_flistxattr(NULL, NULL, NULL);
1741 ck_assert_msg(res < 0, "Failed to handle null arguments");
1742 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1743 strerror(errno), errno);
1744
1745 res = pr_fsio_flistxattr(p, NULL, NULL);
1746 ck_assert_msg(res < 0, "Failed to handle null file handle");
1747 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1748 strerror(errno), errno);
1749
1750 (void) unlink(fsio_test_path);
1751 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_RDWR);
1752 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", fsio_test_path,
1753 strerror(errno));
1754
1755 res = pr_fsio_flistxattr(p, fh, NULL);
1756 ck_assert_msg(res < 0, "Failed to handle null array");
1757 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1758 strerror(errno), errno);
1759
1760 fsio_opts = pr_fsio_set_options(PR_FSIO_OPT_IGNORE_XATTR);
1761 res = pr_fsio_flistxattr(p, fh, &names);
1762 ck_assert_msg(res < 0, "Failed to handle disabled xattr");
1763 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
1764 strerror(errno), errno);
1765
1766 pr_fsio_set_options(fsio_opts);
1767 res = pr_fsio_flistxattr(p, fh, &names);
1768 #ifdef PR_USE_XATTR
1769 ck_assert_msg(res >= 0, "Failed to list xattrs for '%s': %s", fsio_test_path,
1770 strerror(errno));
1771
1772 #else
1773 ck_assert_msg(res < 0, "Failed to handle --disable-xattr");
1774 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
1775 strerror(errno), errno);
1776 #endif /* PR_USE_XATTR */
1777
1778 pr_fsio_close(fh);
1779 (void) unlink(fsio_test_path);
1780 }
1781 END_TEST
1782
1783 START_TEST (fsio_sys_removexattr_test) {
1784 int res;
1785 const char *path, *name;
1786 unsigned long fsio_opts;
1787
1788 res = pr_fsio_removexattr(NULL, NULL, NULL);
1789 ck_assert_msg(res < 0, "Failed to handle null arguments");
1790 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1791 strerror(errno), errno);
1792
1793 res = pr_fsio_removexattr(p, NULL, NULL);
1794 ck_assert_msg(res < 0, "Failed to handle null path");
1795 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1796 strerror(errno), errno);
1797
1798 path = fsio_test_path;
1799 res = pr_fsio_removexattr(p, path, NULL);
1800 ck_assert_msg(res < 0, "Failed to handle null attribute name");
1801 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1802 strerror(errno), errno);
1803
1804 name = "foo.bar";
1805
1806 fsio_opts = pr_fsio_set_options(PR_FSIO_OPT_IGNORE_XATTR);
1807 res = pr_fsio_removexattr(p, path, name);
1808 ck_assert_msg(res < 0, "Failed to handle disabled xattr");
1809 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
1810 strerror(errno), errno);
1811
1812 pr_fsio_set_options(fsio_opts);
1813 res = pr_fsio_removexattr(p, path, name);
1814 #ifdef PR_USE_XATTR
1815 ck_assert_msg(res < 0, "Failed to handle nonexistent attribute '%s'", name);
1816 ck_assert_msg(errno == ENOENT || errno == ENOATTR || errno == ENOTSUP,
1817 "Expected ENOENT (%d), ENOATTR (%d) or ENOTSUP (%d), got %s (%d)",
1818 ENOENT, ENOATTR, ENOTSUP, strerror(errno), errno);
1819
1820 #else
1821 ck_assert_msg(res < 0, "Failed to handle --disable-xattr");
1822 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
1823 strerror(errno), errno);
1824 #endif /* PR_USE_XATTR */
1825 }
1826 END_TEST
1827
1828 START_TEST (fsio_sys_lremovexattr_test) {
1829 int res;
1830 const char *path, *name;
1831 unsigned long fsio_opts;
1832
1833 res = pr_fsio_lremovexattr(NULL, NULL, NULL);
1834 ck_assert_msg(res < 0, "Failed to handle null arguments");
1835 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1836 strerror(errno), errno);
1837
1838 res = pr_fsio_lremovexattr(p, NULL, NULL);
1839 ck_assert_msg(res < 0, "Failed to handle null path");
1840 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1841 strerror(errno), errno);
1842
1843 path = fsio_test_path;
1844 res = pr_fsio_lremovexattr(p, path, NULL);
1845 ck_assert_msg(res < 0, "Failed to handle null attribute name");
1846 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1847 strerror(errno), errno);
1848
1849 name = "foo.bar";
1850
1851 fsio_opts = pr_fsio_set_options(PR_FSIO_OPT_IGNORE_XATTR);
1852 res = pr_fsio_lremovexattr(p, path, name);
1853 ck_assert_msg(res < 0, "Failed to handle disabled xattr");
1854 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
1855 strerror(errno), errno);
1856
1857 pr_fsio_set_options(fsio_opts);
1858 res = pr_fsio_lremovexattr(p, path, name);
1859 #ifdef PR_USE_XATTR
1860 ck_assert_msg(res < 0, "Failed to handle nonexistent attribute '%s'", name);
1861 ck_assert_msg(errno == ENOENT || errno == ENOATTR || errno == ENOTSUP,
1862 "Expected ENOENT (%d), ENOATTR (%d) or ENOTSUP (%d), got %s (%d)",
1863 ENOENT, ENOATTR, ENOTSUP, strerror(errno), errno);
1864
1865 #else
1866 ck_assert_msg(res < 0, "Failed to handle --disable-xattr");
1867 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
1868 strerror(errno), errno);
1869 #endif /* PR_USE_XATTR */
1870 }
1871 END_TEST
1872
1873 START_TEST (fsio_sys_fremovexattr_test) {
1874 int res;
1875 pr_fh_t *fh;
1876 const char *name;
1877 unsigned long fsio_opts;
1878
1879 res = pr_fsio_fremovexattr(NULL, NULL, NULL);
1880 ck_assert_msg(res < 0, "Failed to handle null arguments");
1881 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1882 strerror(errno), errno);
1883
1884 res = pr_fsio_fremovexattr(p, NULL, NULL);
1885 ck_assert_msg(res < 0, "Failed to handle null arguments");
1886 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1887 strerror(errno), errno);
1888
1889 (void) unlink(fsio_test_path);
1890 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_RDWR);
1891 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", fsio_test_path,
1892 strerror(errno));
1893
1894 res = pr_fsio_fremovexattr(p, fh, NULL);
1895 ck_assert_msg(res < 0, "Failed to handle null attribute name");
1896 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1897 strerror(errno), errno);
1898
1899 name = "foo.bar";
1900
1901 fsio_opts = pr_fsio_set_options(PR_FSIO_OPT_IGNORE_XATTR);
1902 res = pr_fsio_fremovexattr(p, fh, name);
1903 ck_assert_msg(res < 0, "Failed to handle disabled xattr");
1904 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
1905 strerror(errno), errno);
1906
1907 pr_fsio_set_options(fsio_opts);
1908 res = pr_fsio_fremovexattr(p, fh, name);
1909 #ifdef PR_USE_XATTR
1910 ck_assert_msg(res < 0, "Failed to handle nonexistent attribute '%s'", name);
1911 ck_assert_msg(errno == ENOENT || errno == ENOATTR || errno == ENOTSUP,
1912 "Expected ENOENT (%d), ENOATTR (%d) or ENOTSUP (%d), got %s (%d)",
1913 ENOENT, ENOATTR, ENOTSUP, strerror(errno), errno);
1914
1915 #else
1916 ck_assert_msg(res < 0, "Failed to handle --disable-xattr");
1917 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
1918 strerror(errno), errno);
1919 #endif /* PR_USE_XATTR */
1920
1921 pr_fsio_close(fh);
1922 (void) unlink(fsio_test_path);
1923 }
1924 END_TEST
1925
1926 START_TEST (fsio_sys_setxattr_test) {
1927 int res, flags;
1928 const char *path, *name;
1929 pr_fh_t *fh;
1930 unsigned long fsio_opts;
1931
1932 res = pr_fsio_setxattr(NULL, NULL, NULL, NULL, 0, 0);
1933 ck_assert_msg(res < 0, "Failed to handle null arguments");
1934 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1935 strerror(errno), errno);
1936
1937 res = pr_fsio_setxattr(p, NULL, NULL, NULL, 0, 0);
1938 ck_assert_msg(res < 0, "Failed to handle null path");
1939 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1940 strerror(errno), errno);
1941
1942 path = fsio_test_path;
1943 res = pr_fsio_setxattr(p, path, NULL, NULL, 0, 0);
1944 ck_assert_msg(res < 0, "Failed to handle null attribute name");
1945 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1946 strerror(errno), errno);
1947
1948 name = "foo.bar";
1949 flags = PR_FSIO_XATTR_FL_CREATE;
1950
1951 fsio_opts = pr_fsio_set_options(PR_FSIO_OPT_IGNORE_XATTR);
1952 res = pr_fsio_setxattr(p, path, name, NULL, 0, flags);
1953 ck_assert_msg(res < 0, "Failed to handle disabled xattr");
1954 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
1955 strerror(errno), errno);
1956
1957 pr_fsio_set_options(fsio_opts);
1958 res = pr_fsio_setxattr(p, path, name, NULL, 0, flags);
1959 #ifdef PR_USE_XATTR
1960 ck_assert_msg(res < 0, "Failed to handle nonexistent file '%s'", path);
1961 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
1962 strerror(errno), errno);
1963
1964 (void) unlink(fsio_test_path);
1965 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_WRONLY);
1966 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", fsio_test_path,
1967 strerror(errno));
1968 pr_fsio_close(fh);
1969
1970 res = pr_fsio_setxattr(p, path, name, NULL, 0, flags);
1971 if (res < 0) {
1972 ck_assert_msg(errno == ENOTSUP, "Expected ENOTSUP (%d), got %s (%d)", ENOTSUP,
1973 strerror(errno), errno);
1974 }
1975
1976 (void) unlink(fsio_test_path);
1977 #else
1978 (void) fh;
1979 ck_assert_msg(res < 0, "Failed to handle --disable-xattr");
1980 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
1981 strerror(errno), errno);
1982 #endif /* PR_USE_XATTR */
1983 }
1984 END_TEST
1985
1986 START_TEST (fsio_sys_lsetxattr_test) {
1987 int res, flags;
1988 const char *path, *name;
1989 pr_fh_t *fh;
1990 unsigned long fsio_opts;
1991
1992 res = pr_fsio_lsetxattr(NULL, NULL, NULL, NULL, 0, 0);
1993 ck_assert_msg(res < 0, "Failed to handle null arguments");
1994 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1995 strerror(errno), errno);
1996
1997 res = pr_fsio_lsetxattr(p, NULL, NULL, NULL, 0, 0);
1998 ck_assert_msg(res < 0, "Failed to handle null path");
1999 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2000 strerror(errno), errno);
2001
2002 path = fsio_test_path;
2003 res = pr_fsio_lsetxattr(p, path, NULL, NULL, 0, 0);
2004 ck_assert_msg(res < 0, "Failed to handle null attribute name");
2005 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2006 strerror(errno), errno);
2007
2008 name = "foo.bar";
2009 flags = PR_FSIO_XATTR_FL_CREATE;
2010
2011 fsio_opts = pr_fsio_set_options(PR_FSIO_OPT_IGNORE_XATTR);
2012 res = pr_fsio_lsetxattr(p, path, name, NULL, 0, flags);
2013 ck_assert_msg(res < 0, "Failed to handle disabled xattr");
2014 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
2015 strerror(errno), errno);
2016
2017 pr_fsio_set_options(fsio_opts);
2018 res = pr_fsio_lsetxattr(p, path, name, NULL, 0, flags);
2019 #ifdef PR_USE_XATTR
2020 ck_assert_msg(res < 0, "Failed to handle nonexistent file '%s'", path);
2021 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
2022 strerror(errno), errno);
2023
2024 (void) unlink(fsio_test_path);
2025 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_WRONLY);
2026 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", fsio_test_path,
2027 strerror(errno));
2028 pr_fsio_close(fh);
2029
2030 res = pr_fsio_lsetxattr(p, path, name, NULL, 0, flags);
2031 if (res < 0) {
2032 ck_assert_msg(errno == ENOTSUP, "Expected ENOTSUP (%d), got %s (%d)", ENOTSUP,
2033 strerror(errno), errno);
2034 }
2035
2036 (void) unlink(fsio_test_path);
2037 #else
2038 (void) fh;
2039 ck_assert_msg(res < 0, "Failed to handle --disable-xattr");
2040 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
2041 strerror(errno), errno);
2042 #endif /* PR_USE_XATTR */
2043 }
2044 END_TEST
2045
2046 START_TEST (fsio_sys_fsetxattr_test) {
2047 int res, flags;
2048 pr_fh_t *fh;
2049 const char *name;
2050 unsigned long fsio_opts;
2051
2052 res = pr_fsio_fsetxattr(NULL, NULL, NULL, NULL, 0, 0);
2053 ck_assert_msg(res < 0, "Failed to handle null arguments");
2054 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2055 strerror(errno), errno);
2056
2057 res = pr_fsio_fsetxattr(p, NULL, NULL, NULL, 0, 0);
2058 ck_assert_msg(res < 0, "Failed to handle null file handle");
2059 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2060 strerror(errno), errno);
2061
2062 (void) unlink(fsio_test_path);
2063 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_RDWR);
2064 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", fsio_test_path,
2065 strerror(errno));
2066
2067 res = pr_fsio_fsetxattr(p, fh, NULL, NULL, 0, 0);
2068 ck_assert_msg(res < 0, "Failed to handle null attribute name");
2069 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2070 strerror(errno), errno);
2071
2072 name = "foo.bar";
2073 flags = PR_FSIO_XATTR_FL_CREATE;
2074
2075 fsio_opts = pr_fsio_set_options(PR_FSIO_OPT_IGNORE_XATTR);
2076 res = pr_fsio_fsetxattr(p, fh, name, NULL, 0, flags);
2077 ck_assert_msg(res < 0, "Failed to handle disabled xattr");
2078 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
2079 strerror(errno), errno);
2080
2081 pr_fsio_set_options(fsio_opts);
2082 res = pr_fsio_fsetxattr(p, fh, name, NULL, 0, flags);
2083 #ifdef PR_USE_XATTR
2084 if (res < 0) {
2085 ck_assert_msg(errno == ENOTSUP, "Expected ENOTSUP (%d), got %s (%d)", ENOTSUP,
2086 strerror(errno), errno);
2087 }
2088
2089 #else
2090 ck_assert_msg(res < 0, "Failed to handle --disable-xattr");
2091 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
2092 strerror(errno), errno);
2093 #endif /* PR_USE_XATTR */
2094
2095 pr_fsio_close(fh);
2096 (void) unlink(fsio_test_path);
2097 }
2098 END_TEST
2099
2100 START_TEST (fsio_sys_mkdir_test) {
2101 int res;
2102 mode_t mode = 0755;
2103
2104 res = pr_fsio_mkdir(NULL, mode);
2105 ck_assert_msg(res < 0, "Failed to handle null arguments");
2106 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2107 strerror(errno), errno);
2108
2109 res = pr_fsio_mkdir(fsio_testdir_path, mode);
2110 ck_assert_msg(res == 0, "Failed to create '%s': %s", fsio_testdir_path,
2111 strerror(errno));
2112
2113 (void) pr_fsio_rmdir(fsio_testdir_path);
2114 }
2115 END_TEST
2116
2117 START_TEST (fsio_sys_mkdir_chroot_guard_test) {
2118 int res;
2119 mode_t mode = 0755;
2120
2121 res = pr_fsio_guard_chroot(TRUE);
2122 ck_assert_msg(res == FALSE, "Expected FALSE (%d), got %d", FALSE, res);
2123
2124 res = pr_fsio_mkdir("/etc/foo.bar.baz.d", mode);
2125 ck_assert_msg(res < 0, "Created /etc/foo.bar.baz.d unexpectedly");
2126 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s %d", EACCES,
2127 strerror(errno), errno);
2128
2129 (void) pr_fsio_guard_chroot(FALSE);
2130
2131 res = pr_fsio_mkdir("/lib/foo/bar/baz.d", mode);
2132 ck_assert_msg(res < 0, "Created /lib/foo/bar/baz.d unexpectedly");
2133 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s %d", ENOENT,
2134 strerror(errno), errno);
2135 }
2136 END_TEST
2137
2138 START_TEST (fsio_sys_rmdir_test) {
2139 int res;
2140 mode_t mode = 0755;
2141
2142 res = pr_fsio_rmdir(NULL);
2143 ck_assert_msg(res < 0, "Failed to handle null arguments");
2144 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2145 strerror(errno), errno);
2146
2147 res = pr_fsio_rmdir(fsio_testdir_path);
2148 ck_assert_msg(res < 0, "Failed to handle null arguments");
2149 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
2150 strerror(errno), errno);
2151
2152 res = pr_fsio_mkdir(fsio_testdir_path, mode);
2153 ck_assert_msg(res == 0, "Failed to create '%s': %s", fsio_testdir_path,
2154 strerror(errno));
2155
2156 res = pr_fsio_rmdir(fsio_testdir_path);
2157 ck_assert_msg(res == 0, "Failed to remove '%s': %s", fsio_testdir_path,
2158 strerror(errno));
2159 }
2160 END_TEST
2161
2162 START_TEST (fsio_sys_rmdir_chroot_guard_test) {
2163 int res;
2164
2165 res = pr_fsio_guard_chroot(TRUE);
2166 ck_assert_msg(res == FALSE, "Expected FALSE (%d), got %d", FALSE, res);
2167
2168 res = pr_fsio_rmdir("/etc/foo.bar.baz.d");
2169 ck_assert_msg(res < 0, "Removed /etc/foo.bar.baz.d unexpectedly");
2170 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s %d", EACCES,
2171 strerror(errno), errno);
2172
2173 (void) pr_fsio_guard_chroot(FALSE);
2174
2175 res = pr_fsio_rmdir("/lib/foo/bar/baz.d");
2176 ck_assert_msg(res < 0, "Removed /lib/etc/foo.bar.baz.d unexpectedly");
2177 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s %d", ENOENT,
2178 strerror(errno), errno);
2179 }
2180 END_TEST
2181
2182 START_TEST (fsio_sys_chdir_test) {
2183 int res;
2184
2185 res = pr_fsio_chdir(NULL, FALSE);
2186 ck_assert_msg(res < 0, "Failed to handle null arguments");
2187 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2188 strerror(errno), errno);
2189
2190 res = pr_fsio_chdir("/etc/hosts", FALSE);
2191 ck_assert_msg(res < 0, "Failed to handle file argument");
2192 ck_assert_msg(errno == EINVAL || errno == ENOTDIR,
2193 "Expected EINVAL (%d) or ENOTDIR (%d), got %s (%d)", EINVAL, ENOTDIR,
2194 strerror(errno), errno);
2195
2196 res = pr_fsio_chdir("/tmp", FALSE);
2197 ck_assert_msg(res == 0, "Failed to chdir to '%s': %s", fsio_cwd,
2198 strerror(errno));
2199
2200 res = pr_fsio_chdir(fsio_cwd, FALSE);
2201 ck_assert_msg(res == 0, "Failed to chdir to '%s': %s", fsio_cwd,
2202 strerror(errno));
2203 }
2204 END_TEST
2205
2206 START_TEST (fsio_sys_chdir_canon_test) {
2207 int res;
2208
2209 res = pr_fsio_chdir_canon(NULL, FALSE);
2210 ck_assert_msg(res < 0, "Failed to handle null arguments");
2211 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2212 strerror(errno), errno);
2213
2214 res = pr_fsio_chdir_canon("/tmp", FALSE);
2215 ck_assert_msg(res == 0, "Failed to chdir to '%s': %s", fsio_cwd,
2216 strerror(errno));
2217
2218 res = pr_fsio_chdir_canon(fsio_cwd, FALSE);
2219 ck_assert_msg(res == 0, "Failed to chdir to '%s': %s", fsio_cwd,
2220 strerror(errno));
2221 }
2222 END_TEST
2223
2224 START_TEST (fsio_sys_chroot_test) {
2225 int res;
2226
2227 res = pr_fsio_chroot(NULL);
2228 ck_assert_msg(res < 0, "Failed to handle null arguments");
2229 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2230 strerror(errno), errno);
2231
2232 if (getuid() != 0) {
2233 res = pr_fsio_chroot("/tmp");
2234 ck_assert_msg(res < 0, "Failed to chroot without root privs");
2235 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2236 strerror(errno), errno);
2237 }
2238 }
2239 END_TEST
2240
2241 START_TEST (fsio_sys_opendir_test) {
2242 void *res = NULL, *res2 = NULL;
2243 const char *path;
2244
2245 mark_point();
2246 res = pr_fsio_opendir(NULL);
2247 ck_assert_msg(res == NULL, "Failed to handle null arguments");
2248 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2249 strerror(errno), errno);
2250
2251 mark_point();
2252 path = "/etc/hosts";
2253 res = pr_fsio_opendir(path);
2254 ck_assert_msg(res == NULL, "Failed to handle file argument");
2255 ck_assert_msg(errno == ENOTDIR, "Expected ENOTDIR (%d), got %s (%d)", ENOTDIR,
2256 strerror(errno), errno);
2257
2258 mark_point();
2259 path = "/tmp/";
2260 res = pr_fsio_opendir(path);
2261 ck_assert_msg(res != NULL, "Failed to open '%s': %s", path, strerror(errno));
2262
2263 mark_point();
2264 path = "/usr/";
2265 res2 = pr_fsio_opendir(path);
2266 ck_assert_msg(res != NULL, "Failed to open '%s': %s", path, strerror(errno));
2267
2268 (void) pr_fsio_closedir(res);
2269 (void) pr_fsio_closedir(res2);
2270 }
2271 END_TEST
2272
2273 START_TEST (fsio_sys_readdir_test) {
2274 void *dirh;
2275 struct dirent *dent;
2276
2277 dent = pr_fsio_readdir(NULL);
2278 ck_assert_msg(dent == NULL, "Failed to handle null arguments");
2279 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2280 strerror(errno), errno);
2281
2282 dent = pr_fsio_readdir("/etc/hosts");
2283 ck_assert_msg(dent == NULL, "Failed to handle file argument");
2284 ck_assert_msg(errno == ENOTDIR, "Expected ENOTDIR (%d), got %s (%d)", ENOTDIR,
2285 strerror(errno), errno);
2286
2287 mark_point();
2288 dirh = pr_fsio_opendir("/tmp/");
2289 ck_assert_msg(dirh != NULL, "Failed to open '/tmp/': %s", strerror(errno));
2290
2291 dent = pr_fsio_readdir(dirh);
2292 ck_assert_msg(dent != NULL, "Failed to read directory entry: %s",
2293 strerror(errno));
2294
2295 (void) pr_fsio_closedir(dirh);
2296 }
2297 END_TEST
2298
2299 START_TEST (fsio_sys_closedir_test) {
2300 void *dirh;
2301 int res;
2302
2303 res = pr_fsio_closedir(NULL);
2304 ck_assert_msg(res < 0, "Failed to handle null arguments");
2305 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2306 strerror(errno), errno);
2307
2308 dirh = pr_fsio_opendir("/tmp/");
2309 ck_assert_msg(dirh != NULL, "Failed to open '/tmp/': %s", strerror(errno));
2310
2311 res = pr_fsio_closedir(dirh);
2312 ck_assert_msg(res == 0, "Failed to close '/tmp/': %s", strerror(errno));
2313
2314 /* Closing an already-closed directory descriptor should fail. */
2315 res = pr_fsio_closedir(dirh);
2316 ck_assert_msg(res < 0, "Failed to handle already-closed directory handle");
2317 ck_assert_msg(errno == ENOTDIR, "Expected ENOTDIR (%d), got %s (%d)", ENOTDIR,
2318 strerror(errno), errno);
2319 }
2320 END_TEST
2321
2322 static const char *test_chmod_explainer(pool *err_pool, int xerrno,
2323 const char *path, mode_t mode, const char **args) {
2324 *args = pstrdup(err_pool, "fake args");
2325 return pstrdup(err_pool, "test mode is not real");
2326 }
2327
2328 START_TEST (fsio_sys_chmod_with_error_test) {
2329 int res;
2330 pr_error_t *err = NULL;
2331 const char *errstr, *expected;
2332 module m;
2333 pr_error_explainer_t *explainer;
2334
2335 mark_point();
2336 res = pr_fsio_chmod_with_error(NULL, fsio_test_path, 0755, NULL);
2337 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
2338 fsio_test_path);
2339 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
2340 strerror(errno), errno);
2341
2342 mark_point();
2343 res = pr_fsio_chmod_with_error(p, fsio_test_path, 0755, &err);
2344 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
2345 fsio_test_path);
2346 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
2347 strerror(errno), errno);
2348 ck_assert_msg(err == NULL, "Unexpectedly populated error");
2349
2350 memset(&m, 0, sizeof(m));
2351 m.name = "error";
2352
2353 explainer = pr_error_register_explainer(p, &m, "error");
2354 explainer->explain_chmod = test_chmod_explainer;
2355
2356 mark_point();
2357 res = pr_fsio_chmod_with_error(p, fsio_test_path, 0755, &err);
2358 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
2359 fsio_test_path);
2360 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
2361 strerror(errno), errno);
2362 ck_assert_msg(err != NULL, "Failed to populate error");
2363
2364 expected = pstrcat(p,
2365 "chmod() failed with \"No such file or directory [ENOENT (",
2366 get_errnum(p, ENOENT), ")]\"", NULL);
2367 errstr = pr_error_strerror(err, PR_ERROR_FORMAT_USE_MINIMAL);
2368 ck_assert_msg(strcmp(errstr, expected) == 0, "Expected '%s', got '%s'",
2369 expected, errstr);
2370
2371 (void) pr_error_unregister_explainer(p, &m, NULL);
2372 pr_error_destroy(err);
2373 }
2374 END_TEST
2375
2376 static const char *test_chown_explainer(pool *err_pool, int xerrno,
2377 const char *path, uid_t uid, gid_t gid, const char **args) {
2378 *args = pstrdup(err_pool, "fake args");
2379 return pstrdup(err_pool, "test mode is not real");
2380 }
2381
2382 START_TEST (fsio_sys_chown_with_error_test) {
2383 int res;
2384 pr_error_t *err = NULL;
2385 const char *errstr, *expected;
2386 module m;
2387 pr_error_explainer_t *explainer;
2388
2389 mark_point();
2390 res = pr_fsio_chown_with_error(NULL, fsio_test_path, 1, 1, NULL);
2391 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
2392 fsio_test_path);
2393 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
2394 strerror(errno), errno);
2395
2396 mark_point();
2397 res = pr_fsio_chown_with_error(p, fsio_test_path, 1, 1, &err);
2398 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
2399 fsio_test_path);
2400 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
2401 strerror(errno), errno);
2402 ck_assert_msg(err == NULL, "Unexpectedly populated error");
2403
2404 memset(&m, 0, sizeof(m));
2405 m.name = "error";
2406
2407 explainer = pr_error_register_explainer(p, &m, "error");
2408 explainer->explain_chown = test_chown_explainer;
2409
2410 mark_point();
2411 res = pr_fsio_chown_with_error(p, fsio_test_path, 1, 1, &err);
2412 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
2413 fsio_test_path);
2414 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
2415 strerror(errno), errno);
2416 ck_assert_msg(err != NULL, "Failed to populate error");
2417
2418 expected = pstrcat(p,
2419 "chown() failed with \"No such file or directory [ENOENT (",
2420 get_errnum(p, ENOENT), ")]\"", NULL);
2421 errstr = pr_error_strerror(err, PR_ERROR_FORMAT_USE_MINIMAL);
2422 ck_assert_msg(strcmp(errstr, expected) == 0, "Expected '%s', got '%s'",
2423 expected, errstr);
2424
2425 (void) pr_error_unregister_explainer(p, &m, NULL);
2426 pr_error_destroy(err);
2427 }
2428 END_TEST
2429
2430 static const char *test_chroot_explainer(pool *err_pool, int xerrno,
2431 const char *path, const char **args) {
2432 *args = pstrdup(err_pool, "fake args");
2433 return pstrdup(err_pool, "test mode is not real");
2434 }
2435
2436 START_TEST (fsio_sys_chroot_with_error_test) {
2437 int res, xerrno = 0;
2438 pr_error_t *err = NULL;
2439 const char *errstr, *expected;
2440 module m;
2441 pr_error_explainer_t *explainer;
2442
2443 mark_point();
2444 res = pr_fsio_chroot_with_error(NULL, fsio_testdir_path, NULL);
2445 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
2446 fsio_testdir_path);
2447 ck_assert_msg(errno == EPERM || errno == ENOENT,
2448 "Expected EPERM (%d) or ENOENT (%d), %s (%d)", EPERM, ENOENT,
2449 strerror(errno), errno);
2450
2451 mark_point();
2452 res = pr_fsio_chroot_with_error(p, fsio_testdir_path, &err);
2453 xerrno = errno;
2454 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
2455 fsio_testdir_path);
2456 ck_assert_msg(errno == EPERM || errno == ENOENT,
2457 "Expected EPERM (%d) or ENOENT (%d), %s (%d)", EPERM, ENOENT,
2458 strerror(errno), errno);
2459 ck_assert_msg(err == NULL, "Unexpectedly populated error");
2460
2461 memset(&m, 0, sizeof(m));
2462 m.name = "error";
2463
2464 explainer = pr_error_register_explainer(p, &m, "error");
2465 explainer->explain_chroot = test_chroot_explainer;
2466
2467 mark_point();
2468 res = pr_fsio_chroot_with_error(p, fsio_testdir_path, &err);
2469 xerrno = errno;
2470 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
2471 fsio_testdir_path);
2472 ck_assert_msg(errno == EPERM || errno == ENOENT,
2473 "Expected EPERM (%d) or ENOENT (%d), %s (%d)", EPERM, ENOENT,
2474 strerror(errno), errno);
2475 ck_assert_msg(err != NULL, "Failed to populate error");
2476
2477 expected = pstrcat(p,
2478 "chroot() failed with \"", strerror(xerrno), " [",
2479 xerrno == ENOENT ? "ENOENT" : "EPERM", " (",
2480 get_errnum(p, xerrno), ")]\"", NULL);
2481 errstr = pr_error_strerror(err, PR_ERROR_FORMAT_USE_MINIMAL);
2482 ck_assert_msg(strcmp(errstr, expected) == 0, "Expected '%s', got '%s'",
2483 expected, errstr);
2484
2485 (void) pr_error_unregister_explainer(p, &m, NULL);
2486 pr_error_destroy(err);
2487 }
2488 END_TEST
2489
2490 static const char *test_close_explainer(pool *err_pool, int xerrno, int fd,
2491 const char **args) {
2492 *args = pstrdup(err_pool, "fake args");
2493 return pstrdup(err_pool, "test mode is not real");
2494 }
2495
2496 START_TEST (fsio_sys_close_with_error_test) {
2497 int res;
2498 pr_error_t *err = NULL;
2499 const char *errstr, *expected;
2500 module m;
2501 pr_error_explainer_t *explainer;
2502
2503 mark_point();
2504 res = pr_fsio_close_with_error(NULL, NULL, NULL);
2505 ck_assert_msg(res < 0, "Failed to handle null fh");
2506 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), %s (%d)", EINVAL,
2507 strerror(errno), errno);
2508
2509 mark_point();
2510 res = pr_fsio_close_with_error(p, NULL, &err);
2511 ck_assert_msg(res < 0, "Failed to handle null fh");
2512 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), %s (%d)", EINVAL,
2513 strerror(errno), errno);
2514 ck_assert_msg(err == NULL, "Unexpectedly populated error");
2515
2516 memset(&m, 0, sizeof(m));
2517 m.name = "error";
2518
2519 explainer = pr_error_register_explainer(p, &m, "error");
2520 explainer->explain_close = test_close_explainer;
2521
2522 mark_point();
2523 res = pr_fsio_close_with_error(p, NULL, &err);
2524 ck_assert_msg(res < 0, "Failed to handle null fh");
2525 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), %s (%d)", EINVAL,
2526 strerror(errno), errno);
2527 ck_assert_msg(err != NULL, "Failed to populate error");
2528
2529 expected = pstrcat(p,
2530 "close() failed with \"Invalid argument [EINVAL (",
2531 get_errnum(p, EINVAL), ")]\"", NULL);
2532 errstr = pr_error_strerror(err, PR_ERROR_FORMAT_USE_MINIMAL);
2533 ck_assert_msg(strcmp(errstr, expected) == 0, "Expected '%s', got '%s'",
2534 expected, errstr);
2535
2536 (void) pr_error_unregister_explainer(p, &m, NULL);
2537 pr_error_destroy(err);
2538 }
2539 END_TEST
2540
2541 static const char *test_fchmod_explainer(pool *err_pool, int xerrno, int fd,
2542 mode_t mode, const char **args) {
2543 *args = pstrdup(err_pool, "fake args");
2544 return pstrdup(err_pool, "test mode is not real");
2545 }
2546
2547 START_TEST (fsio_sys_fchmod_with_error_test) {
2548 int res;
2549 pr_error_t *err = NULL;
2550 const char *errstr, *expected;
2551 module m;
2552 pr_error_explainer_t *explainer;
2553
2554 mark_point();
2555 res = pr_fsio_fchmod_with_error(NULL, NULL, 0755, NULL);
2556 ck_assert_msg(res < 0, "Failed to handle null fh");
2557 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), %s (%d)", EINVAL,
2558 strerror(errno), errno);
2559
2560 mark_point();
2561 res = pr_fsio_fchmod_with_error(p, NULL, 0755, &err);
2562 ck_assert_msg(res < 0, "Failed to handle null fh");
2563 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), %s (%d)", EINVAL,
2564 strerror(errno), errno);
2565 ck_assert_msg(err == NULL, "Unexpectedly populated error");
2566
2567 memset(&m, 0, sizeof(m));
2568 m.name = "error";
2569
2570 explainer = pr_error_register_explainer(p, &m, "error");
2571 explainer->explain_fchmod = test_fchmod_explainer;
2572
2573 mark_point();
2574 res = pr_fsio_fchmod_with_error(p, NULL, 0755, &err);
2575 ck_assert_msg(res < 0, "Failed to handle null fh");
2576 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), %s (%d)", EINVAL,
2577 strerror(errno), errno);
2578 ck_assert_msg(err != NULL, "Failed to populate error");
2579
2580 expected = pstrcat(p,
2581 "fchmod() failed with \"Invalid argument [EINVAL (",
2582 get_errnum(p, EINVAL), ")]\"", NULL);
2583 errstr = pr_error_strerror(err, PR_ERROR_FORMAT_USE_MINIMAL);
2584 ck_assert_msg(strcmp(errstr, expected) == 0, "Expected '%s', got '%s'",
2585 expected, errstr);
2586
2587 (void) pr_error_unregister_explainer(p, &m, NULL);
2588 pr_error_destroy(err);
2589 }
2590 END_TEST
2591
2592 static const char *test_fchown_explainer(pool *err_pool, int xerrno, int fd,
2593 uid_t uid, gid_t gid, const char **args) {
2594 *args = pstrdup(err_pool, "fake args");
2595 return pstrdup(err_pool, "test mode is not real");
2596 }
2597
2598 START_TEST (fsio_sys_fchown_with_error_test) {
2599 int res;
2600 pr_error_t *err = NULL;
2601 const char *errstr, *expected;
2602 module m;
2603 pr_error_explainer_t *explainer;
2604
2605 mark_point();
2606 res = pr_fsio_fchown_with_error(NULL, NULL, 1, 1, NULL);
2607 ck_assert_msg(res < 0, "Failed to handle null fh");
2608 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), %s (%d)", EINVAL,
2609 strerror(errno), errno);
2610
2611 mark_point();
2612 res = pr_fsio_fchown_with_error(p, NULL, 1, 1, &err);
2613 ck_assert_msg(res < 0, "Failed to handle null fh");
2614 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), %s (%d)", EINVAL,
2615 strerror(errno), errno);
2616 ck_assert_msg(err == NULL, "Unexpectedly populated error");
2617
2618 memset(&m, 0, sizeof(m));
2619 m.name = "error";
2620
2621 explainer = pr_error_register_explainer(p, &m, "error");
2622 explainer->explain_fchown = test_fchown_explainer;
2623
2624 mark_point();
2625 res = pr_fsio_fchown_with_error(p, NULL, 1, 1, &err);
2626 ck_assert_msg(res < 0, "Failed to handle null fh");
2627 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), %s (%d)", EINVAL,
2628 strerror(errno), errno);
2629 ck_assert_msg(err != NULL, "Failed to populate error");
2630
2631 expected = pstrcat(p,
2632 "fchown() failed with \"Invalid argument [EINVAL (",
2633 get_errnum(p, EINVAL), ")]\"", NULL);
2634 errstr = pr_error_strerror(err, PR_ERROR_FORMAT_USE_MINIMAL);
2635 ck_assert_msg(strcmp(errstr, expected) == 0, "Expected '%s', got '%s'",
2636 expected, errstr);
2637
2638 (void) pr_error_unregister_explainer(p, &m, NULL);
2639 pr_error_destroy(err);
2640 }
2641 END_TEST
2642
2643 static const char *test_lchown_explainer(pool *err_pool, int xerrno,
2644 const char *path, uid_t uid, gid_t gid, const char **args) {
2645 *args = pstrdup(err_pool, "fake args");
2646 return pstrdup(err_pool, "test mode is not real");
2647 }
2648
2649 START_TEST (fsio_sys_lchown_with_error_test) {
2650 int res;
2651 pr_error_t *err = NULL;
2652 const char *errstr, *expected;
2653 module m;
2654 pr_error_explainer_t *explainer;
2655
2656 mark_point();
2657 res = pr_fsio_lchown_with_error(NULL, fsio_test_path, 1, 1, NULL);
2658 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
2659 fsio_test_path);
2660 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
2661 strerror(errno), errno);
2662
2663 mark_point();
2664 res = pr_fsio_lchown_with_error(p, fsio_test_path, 1, 1, &err);
2665 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
2666 fsio_test_path);
2667 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
2668 strerror(errno), errno);
2669 ck_assert_msg(err == NULL, "Unexpectedly populated error");
2670
2671 memset(&m, 0, sizeof(m));
2672 m.name = "error";
2673
2674 explainer = pr_error_register_explainer(p, &m, "error");
2675 explainer->explain_lchown = test_lchown_explainer;
2676
2677 mark_point();
2678 res = pr_fsio_lchown_with_error(p, fsio_test_path, 1, 1, &err);
2679 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
2680 fsio_test_path);
2681 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
2682 strerror(errno), errno);
2683 ck_assert_msg(err != NULL, "Failed to populate error");
2684
2685 expected = pstrcat(p,
2686 "lchown() failed with \"No such file or directory [ENOENT (",
2687 get_errnum(p, ENOENT), ")]\"", NULL);
2688 errstr = pr_error_strerror(err, PR_ERROR_FORMAT_USE_MINIMAL);
2689 ck_assert_msg(strcmp(errstr, expected) == 0, "Expected '%s', got '%s'",
2690 expected, errstr);
2691
2692 (void) pr_error_unregister_explainer(p, &m, NULL);
2693 pr_error_destroy(err);
2694 }
2695 END_TEST
2696
2697 static const char *test_lstat_explainer(pool *err_pool, int xerrno,
2698 const char *path, struct stat *st, const char **args) {
2699 *args = pstrdup(err_pool, "fake args");
2700 return pstrdup(err_pool, "test mode is not real");
2701 }
2702
2703 START_TEST (fsio_sys_lstat_with_error_test) {
2704 int res;
2705 struct stat st;
2706 pr_error_t *err = NULL;
2707 const char *errstr, *expected;
2708 module m;
2709 pr_error_explainer_t *explainer;
2710
2711 mark_point();
2712 res = pr_fsio_lstat_with_error(NULL, fsio_test_path, &st, NULL);
2713 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
2714 fsio_test_path);
2715 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
2716 strerror(errno), errno);
2717
2718 mark_point();
2719 res = pr_fsio_lstat_with_error(p, fsio_test_path, &st, &err);
2720 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
2721 fsio_test_path);
2722 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
2723 strerror(errno), errno);
2724 ck_assert_msg(err == NULL, "Unexpectedly populated error");
2725
2726 memset(&m, 0, sizeof(m));
2727 m.name = "error";
2728
2729 explainer = pr_error_register_explainer(p, &m, "error");
2730 explainer->explain_lstat = test_lstat_explainer;
2731
2732 mark_point();
2733 res = pr_fsio_lstat_with_error(p, fsio_test_path, &st, &err);
2734 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
2735 fsio_test_path);
2736 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
2737 strerror(errno), errno);
2738 ck_assert_msg(err != NULL, "Failed to populate error");
2739
2740 expected = pstrcat(p,
2741 "lstat() failed with \"No such file or directory [ENOENT (",
2742 get_errnum(p, ENOENT), ")]\"", NULL);
2743 errstr = pr_error_strerror(err, PR_ERROR_FORMAT_USE_MINIMAL);
2744 ck_assert_msg(strcmp(errstr, expected) == 0, "Expected '%s', got '%s'",
2745 expected, errstr);
2746
2747 (void) pr_error_unregister_explainer(p, &m, NULL);
2748 pr_error_destroy(err);
2749 }
2750 END_TEST
2751
2752 static const char *test_mkdir_explainer(pool *err_pool, int xerrno,
2753 const char *path, mode_t mode, const char **args) {
2754 *args = pstrdup(err_pool, "fake args");
2755 return pstrdup(err_pool, "test mode is not real");
2756 }
2757
2758 START_TEST (fsio_sys_mkdir_with_error_test) {
2759 int res;
2760 pr_error_t *err = NULL;
2761 const char *errstr, *expected, *path;
2762 module m;
2763 pr_error_explainer_t *explainer;
2764
2765 path = "/tmp/foo/bar/baz/quxx/quzz.d";
2766
2767 mark_point();
2768 res = pr_fsio_mkdir_with_error(NULL, path, 0755, NULL);
2769 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
2770 fsio_testdir_path);
2771 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
2772 strerror(errno), errno);
2773
2774 mark_point();
2775 res = pr_fsio_mkdir_with_error(p, path, 0755, &err);
2776 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
2777 fsio_testdir_path);
2778 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
2779 strerror(errno), errno);
2780 ck_assert_msg(err == NULL, "Unexpectedly populated error");
2781
2782 memset(&m, 0, sizeof(m));
2783 m.name = "error";
2784
2785 explainer = pr_error_register_explainer(p, &m, "error");
2786 explainer->explain_mkdir = test_mkdir_explainer;
2787
2788 mark_point();
2789 res = pr_fsio_mkdir_with_error(p, path, 0755, &err);
2790 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
2791 fsio_testdir_path);
2792 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
2793 strerror(errno), errno);
2794 ck_assert_msg(err != NULL, "Failed to populate error");
2795
2796 expected = pstrcat(p,
2797 "mkdir() failed with \"No such file or directory [ENOENT (",
2798 get_errnum(p, ENOENT), ")]\"", NULL);
2799 errstr = pr_error_strerror(err, PR_ERROR_FORMAT_USE_MINIMAL);
2800 ck_assert_msg(strcmp(errstr, expected) == 0, "Expected '%s', got '%s'",
2801 expected, errstr);
2802
2803 (void) pr_error_unregister_explainer(p, &m, NULL);
2804 pr_error_destroy(err);
2805 }
2806 END_TEST
2807
2808 static const char *test_open_explainer(pool *err_pool, int xerrno,
2809 const char *path, int flags, mode_t mode, const char **args) {
2810 *args = pstrdup(err_pool, "fake args");
2811 return pstrdup(err_pool, "test mode is not real");
2812 }
2813
2814 START_TEST (fsio_sys_open_with_error_test) {
2815 pr_fh_t *fh;
2816 pr_error_t *err = NULL;
2817 const char *errstr, *expected;
2818 module m;
2819 pr_error_explainer_t *explainer;
2820
2821 mark_point();
2822 fh = pr_fsio_open_with_error(NULL, fsio_test_path, O_RDONLY, NULL);
2823 ck_assert_msg(fh == NULL, "Failed to handle non-existent file '%s'",
2824 fsio_test_path);
2825 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
2826 strerror(errno), errno);
2827
2828 mark_point();
2829 fh = pr_fsio_open_with_error(p, fsio_test_path, O_RDONLY, &err);
2830 ck_assert_msg(fh == NULL, "Failed to handle non-existent file '%s'",
2831 fsio_test_path);
2832 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
2833 strerror(errno), errno);
2834 ck_assert_msg(err == NULL, "Unexpectedly populated error");
2835
2836 memset(&m, 0, sizeof(m));
2837 m.name = "error";
2838
2839 explainer = pr_error_register_explainer(p, &m, "error");
2840 explainer->explain_open = test_open_explainer;
2841
2842 mark_point();
2843 fh = pr_fsio_open_with_error(p, fsio_test_path, O_RDONLY, &err);
2844 ck_assert_msg(fh == NULL, "Failed to handle non-existent file '%s'",
2845 fsio_test_path);
2846 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
2847 strerror(errno), errno);
2848 ck_assert_msg(err != NULL, "Failed to populate error");
2849
2850 expected = pstrcat(p,
2851 "open() failed with \"No such file or directory [ENOENT (",
2852 get_errnum(p, ENOENT), ")]\"", NULL);
2853 errstr = pr_error_strerror(err, PR_ERROR_FORMAT_USE_MINIMAL);
2854 ck_assert_msg(strcmp(errstr, expected) == 0, "Expected '%s', got '%s'",
2855 expected, errstr);
2856
2857 (void) pr_error_unregister_explainer(p, &m, NULL);
2858 pr_error_destroy(err);
2859 }
2860 END_TEST
2861
2862 static const char *test_read_explainer(pool *err_pool, int xerrno, int fd,
2863 void *buf, size_t sz, const char **args) {
2864 *args = pstrdup(err_pool, "fake args");
2865 return pstrdup(err_pool, "test mode is not real");
2866 }
2867
2868 START_TEST (fsio_sys_read_with_error_test) {
2869 int res;
2870 pr_error_t *err = NULL;
2871 const char *errstr, *expected;
2872 module m;
2873 pr_error_explainer_t *explainer;
2874
2875 mark_point();
2876 res = pr_fsio_read_with_error(NULL, NULL, NULL, 0, NULL);
2877 ck_assert_msg(res < 0, "Failed to handle null fh");
2878 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), %s (%d)", EINVAL,
2879 strerror(errno), errno);
2880
2881 mark_point();
2882 res = pr_fsio_read_with_error(p, NULL, NULL, 0, &err);
2883 ck_assert_msg(res < 0, "Failed to handle null fh");
2884 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), %s (%d)", EINVAL,
2885 strerror(errno), errno);
2886 ck_assert_msg(err == NULL, "Unexpectedly populated error");
2887
2888 memset(&m, 0, sizeof(m));
2889 m.name = "error";
2890
2891 explainer = pr_error_register_explainer(p, &m, "error");
2892 explainer->explain_read = test_read_explainer;
2893
2894 mark_point();
2895 res = pr_fsio_read_with_error(p, NULL, NULL, 0, &err);
2896 ck_assert_msg(res < 0, "Failed to handle null fh");
2897 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), %s (%d)", EINVAL,
2898 strerror(errno), errno);
2899 ck_assert_msg(err != NULL, "Failed to populate error");
2900
2901 expected = pstrcat(p,
2902 "read() failed with \"Invalid argument [EINVAL (",
2903 get_errnum(p, EINVAL), ")]\"", NULL);
2904 errstr = pr_error_strerror(err, PR_ERROR_FORMAT_USE_MINIMAL);
2905 ck_assert_msg(strcmp(errstr, expected) == 0, "Expected '%s', got '%s'",
2906 expected, errstr);
2907
2908 (void) pr_error_unregister_explainer(p, &m, NULL);
2909 pr_error_destroy(err);
2910 }
2911 END_TEST
2912
2913 static const char *test_rename_explainer(pool *err_pool, int xerrno,
2914 const char *from, const char *to, const char **args) {
2915 *args = pstrdup(err_pool, "fake args");
2916 return pstrdup(err_pool, "test mode is not real");
2917 }
2918
2919 START_TEST (fsio_sys_rename_with_error_test) {
2920 int res;
2921 pr_error_t *err = NULL;
2922 const char *errstr, *expected;
2923 module m;
2924 pr_error_explainer_t *explainer;
2925
2926 mark_point();
2927 res = pr_fsio_rename_with_error(NULL, fsio_test_path, fsio_test2_path, NULL);
2928 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
2929 fsio_test_path);
2930 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
2931 strerror(errno), errno);
2932
2933 mark_point();
2934 res = pr_fsio_rename_with_error(p, fsio_test_path, fsio_test2_path, &err);
2935 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
2936 fsio_test_path);
2937 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
2938 strerror(errno), errno);
2939 ck_assert_msg(err == NULL, "Unexpectedly populated error");
2940
2941 memset(&m, 0, sizeof(m));
2942 m.name = "error";
2943
2944 explainer = pr_error_register_explainer(p, &m, "error");
2945 explainer->explain_rename = test_rename_explainer;
2946
2947 mark_point();
2948 res = pr_fsio_rename_with_error(p, fsio_test_path, fsio_test2_path, &err);
2949 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
2950 fsio_test_path);
2951 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
2952 strerror(errno), errno);
2953 ck_assert_msg(err != NULL, "Failed to populate error");
2954
2955 expected = pstrcat(p,
2956 "rename() failed with \"No such file or directory [ENOENT (",
2957 get_errnum(p, ENOENT), ")]\"", NULL);
2958 errstr = pr_error_strerror(err, PR_ERROR_FORMAT_USE_MINIMAL);
2959 ck_assert_msg(strcmp(errstr, expected) == 0, "Expected '%s', got '%s'",
2960 expected, errstr);
2961
2962 (void) pr_error_unregister_explainer(p, &m, NULL);
2963 pr_error_destroy(err);
2964 }
2965 END_TEST
2966
2967 static const char *test_rmdir_explainer(pool *err_pool, int xerrno,
2968 const char *path, const char **args) {
2969 *args = pstrdup(err_pool, "fake args");
2970 return pstrdup(err_pool, "test mode is not real");
2971 }
2972
2973 START_TEST (fsio_sys_rmdir_with_error_test) {
2974 int res;
2975 pr_error_t *err = NULL;
2976 const char *errstr, *expected;
2977 module m;
2978 pr_error_explainer_t *explainer;
2979
2980 mark_point();
2981 res = pr_fsio_rmdir_with_error(NULL, fsio_testdir_path, NULL);
2982 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
2983 fsio_testdir_path);
2984 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
2985 strerror(errno), errno);
2986 ck_assert_msg(err == NULL, "Unexpectedly populated error");
2987
2988 memset(&m, 0, sizeof(m));
2989 m.name = "error";
2990
2991 explainer = pr_error_register_explainer(p, &m, "error");
2992 explainer->explain_rmdir = test_rmdir_explainer;
2993
2994 mark_point();
2995 res = pr_fsio_rmdir_with_error(p, fsio_testdir_path, &err);
2996 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
2997 fsio_testdir_path);
2998 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
2999 strerror(errno), errno);
3000 ck_assert_msg(err != NULL, "Failed to populate error");
3001
3002 expected = pstrcat(p,
3003 "rmdir() failed with \"No such file or directory [ENOENT (",
3004 get_errnum(p, ENOENT), ")]\"", NULL);
3005 errstr = pr_error_strerror(err, PR_ERROR_FORMAT_USE_MINIMAL);
3006 ck_assert_msg(strcmp(errstr, expected) == 0, "Expected '%s', got '%s'",
3007 expected, errstr);
3008
3009 (void) pr_error_unregister_explainer(p, &m, NULL);
3010 pr_error_destroy(err);
3011 }
3012 END_TEST
3013
3014 static const char *test_stat_explainer(pool *err_pool, int xerrno,
3015 const char *path, struct stat *st, const char **args) {
3016 *args = pstrdup(err_pool, "fake args");
3017 return pstrdup(err_pool, "test mode is not real");
3018 }
3019
3020 START_TEST (fsio_sys_stat_with_error_test) {
3021 int res;
3022 struct stat st;
3023 pr_error_t *err = NULL;
3024 const char *errstr, *expected;
3025 module m;
3026 pr_error_explainer_t *explainer;
3027
3028 mark_point();
3029 res = pr_fsio_stat_with_error(NULL, fsio_test_path, &st, NULL);
3030 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
3031 fsio_test_path);
3032 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
3033 strerror(errno), errno);
3034
3035 mark_point();
3036 res = pr_fsio_stat_with_error(p, fsio_test_path, &st, &err);
3037 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
3038 fsio_test_path);
3039 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
3040 strerror(errno), errno);
3041 ck_assert_msg(err == NULL, "Unexpectedly populated error");
3042
3043 memset(&m, 0, sizeof(m));
3044 m.name = "error";
3045
3046 explainer = pr_error_register_explainer(p, &m, "error");
3047 explainer->explain_stat = test_stat_explainer;
3048
3049 mark_point();
3050 res = pr_fsio_stat_with_error(p, fsio_test_path, &st, &err);
3051 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
3052 fsio_test_path);
3053 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
3054 strerror(errno), errno);
3055 ck_assert_msg(err != NULL, "Failed to populate error");
3056
3057 expected = pstrcat(p,
3058 "stat() failed with \"No such file or directory [ENOENT (",
3059 get_errnum(p, ENOENT), ")]\"", NULL);
3060 errstr = pr_error_strerror(err, PR_ERROR_FORMAT_USE_MINIMAL);
3061 ck_assert_msg(strcmp(errstr, expected) == 0, "Expected '%s', got '%s'",
3062 expected, errstr);
3063
3064 (void) pr_error_unregister_explainer(p, &m, NULL);
3065 pr_error_destroy(err);
3066 }
3067 END_TEST
3068
3069 static const char *test_unlink_explainer(pool *err_pool, int xerrno,
3070 const char *path, const char **args) {
3071 *args = pstrdup(err_pool, "fake args");
3072 return pstrdup(err_pool, "test mode is not real");
3073 }
3074
3075 START_TEST (fsio_sys_unlink_with_error_test) {
3076 int res;
3077 pr_error_t *err = NULL;
3078 const char *errstr, *expected;
3079 module m;
3080 pr_error_explainer_t *explainer;
3081
3082 mark_point();
3083 res = pr_fsio_unlink_with_error(NULL, fsio_test_path, NULL);
3084 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
3085 fsio_test_path);
3086 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
3087 strerror(errno), errno);
3088
3089 mark_point();
3090 res = pr_fsio_unlink_with_error(p, fsio_test_path, &err);
3091 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
3092 fsio_test_path);
3093 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
3094 strerror(errno), errno);
3095 ck_assert_msg(err == NULL, "Unexpectedly populated error");
3096
3097 memset(&m, 0, sizeof(m));
3098 m.name = "error";
3099
3100 explainer = pr_error_register_explainer(p, &m, "error");
3101 explainer->explain_unlink = test_unlink_explainer;
3102
3103 mark_point();
3104 res = pr_fsio_unlink_with_error(p, fsio_test_path, &err);
3105 ck_assert_msg(res < 0, "Failed to handle non-existent file '%s'",
3106 fsio_test_path);
3107 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), %s (%d)", ENOENT,
3108 strerror(errno), errno);
3109 ck_assert_msg(err != NULL, "Failed to populate error");
3110
3111 expected = pstrcat(p,
3112 "unlink() failed with \"No such file or directory [ENOENT (",
3113 get_errnum(p, ENOENT), ")]\"", NULL);
3114 errstr = pr_error_strerror(err, PR_ERROR_FORMAT_USE_MINIMAL);
3115 ck_assert_msg(strcmp(errstr, expected) == 0, "Expected '%s', got '%s'",
3116 expected, errstr);
3117
3118 (void) pr_error_unregister_explainer(p, &m, NULL);
3119 pr_error_destroy(err);
3120 }
3121 END_TEST
3122
3123 static const char *test_write_explainer(pool *err_pool, int xerrno, int fd,
3124 const void *buf, size_t sz, const char **args) {
3125 *args = pstrdup(err_pool, "fake args");
3126 return pstrdup(err_pool, "test mode is not real");
3127 }
3128
3129 START_TEST (fsio_sys_write_with_error_test) {
3130 int res;
3131 pr_error_t *err = NULL;
3132 const char *errstr, *expected;
3133 module m;
3134 pr_error_explainer_t *explainer;
3135
3136 mark_point();
3137 res = pr_fsio_write_with_error(NULL, NULL, NULL, 0, NULL);
3138 ck_assert_msg(res < 0, "Failed to handle null pool");
3139 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), %s (%d)", EINVAL,
3140 strerror(errno), errno);
3141
3142 mark_point();
3143 res = pr_fsio_write_with_error(p, NULL, NULL, 0, &err);
3144 ck_assert_msg(res < 0, "Failed to handle null fh");
3145 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), %s (%d)", EINVAL,
3146 strerror(errno), errno);
3147 ck_assert_msg(err == NULL, "Unexpectedly populated error");
3148
3149 memset(&m, 0, sizeof(m));
3150 m.name = "error";
3151
3152 explainer = pr_error_register_explainer(p, &m, "error");
3153 explainer->explain_write = test_write_explainer;
3154
3155 mark_point();
3156 res = pr_fsio_write_with_error(p, NULL, NULL, 0, &err);
3157 ck_assert_msg(res < 0, "Failed to handle null fh");
3158 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), %s (%d)", EINVAL,
3159 strerror(errno), errno);
3160 ck_assert_msg(err != NULL, "Failed to populate error");
3161
3162 expected = pstrcat(p,
3163 "write() failed with \"Invalid argument [EINVAL (",
3164 get_errnum(p, EINVAL), ")]\"", NULL);
3165 expected = "write() failed with \"Invalid argument [EINVAL (22)]\"";
3166 errstr = pr_error_strerror(err, PR_ERROR_FORMAT_USE_MINIMAL);
3167 ck_assert_msg(strcmp(errstr, expected) == 0, "Expected '%s', got '%s'",
3168 expected, errstr);
3169
3170 (void) pr_error_unregister_explainer(p, &m, NULL);
3171 pr_error_destroy(err);
3172 }
3173 END_TEST
3174
3175 START_TEST (fsio_statcache_clear_cache_test) {
3176 int expected, res;
3177 struct stat st;
3178 char *cwd;
3179
3180 mark_point();
3181 pr_fs_clear_cache();
3182
3183 res = pr_fs_clear_cache2("/testsuite");
3184 ck_assert_msg(res == 0, "Failed to clear cache: %s", strerror(errno));
3185
3186 res = pr_fsio_stat("/tmp", &st);
3187 ck_assert_msg(res == 0, "Failed to stat '/tmp': %s", strerror(errno));
3188
3189 res = pr_fs_clear_cache2("/tmp");
3190 expected = 1;
3191 ck_assert_msg(res == expected, "Expected %d, got %d", expected, res);
3192
3193 res = pr_fs_clear_cache2("/testsuite");
3194 expected = 0;
3195 ck_assert_msg(res == expected, "Expected %d, got %d", expected, res);
3196
3197 res = pr_fsio_stat("/tmp", &st);
3198 ck_assert_msg(res == 0, "Failed to stat '/tmp': %s", strerror(errno));
3199
3200 res = pr_fsio_lstat("/tmp", &st);
3201 ck_assert_msg(res == 0, "Failed to lstat '/tmp': %s", strerror(errno));
3202
3203 res = pr_fs_clear_cache2("/tmp");
3204 expected = 2;
3205 ck_assert_msg(res == expected, "Expected %d, got %d", expected, res);
3206
3207 res = pr_fsio_stat("/tmp", &st);
3208 ck_assert_msg(res == 0, "Failed to stat '/tmp': %s", strerror(errno));
3209
3210 res = pr_fsio_lstat("/tmp", &st);
3211 ck_assert_msg(res == 0, "Failed to lstat '/tmp': %s", strerror(errno));
3212
3213 cwd = getcwd(NULL, 0);
3214 ck_assert_msg(cwd != NULL, "Failed to get cwd: %s", strerror(errno));
3215
3216 res = pr_fs_setcwd("/");
3217 ck_assert_msg(res == 0, "Failed to set cwd to '/': %s", strerror(errno));
3218
3219 res = pr_fs_clear_cache2("tmp");
3220 expected = 2;
3221 ck_assert_msg(res == expected, "Expected %d, got %d", expected, res);
3222
3223 res = pr_fs_setcwd(cwd);
3224 ck_assert_msg(res == 0, "Failed to set cwd to '%s': %s", cwd,
3225 strerror(errno));
3226
3227 free(cwd);
3228 }
3229 END_TEST
3230
3231 START_TEST (fsio_statcache_cache_hit_test) {
3232 int res;
3233 struct stat st;
3234
3235 /* First is a cache miss...*/
3236 res = pr_fsio_stat("/tmp", &st);
3237 ck_assert_msg(res == 0, "Failed to stat '/tmp': %s", strerror(errno));
3238
3239 /* This is a cache hit, hopefully. */
3240 res = pr_fsio_stat("/tmp", &st);
3241 ck_assert_msg(res == 0, "Failed to stat '/tmp': %s", strerror(errno));
3242
3243 pr_fs_clear_cache();
3244 }
3245 END_TEST
3246
3247 START_TEST (fsio_statcache_negative_cache_test) {
3248 int res;
3249 struct stat st;
3250
3251 /* First is a cache miss...*/
3252 res = pr_fsio_stat("/foo.bar.baz.d", &st);
3253 ck_assert_msg(res < 0, "Check of '/foo.bar.baz.d' succeeded unexpectedly");
3254 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
3255 strerror(errno), errno);
3256
3257 /* This is a cache hit, hopefully. */
3258 res = pr_fsio_stat("/foo.bar.baz.d", &st);
3259 ck_assert_msg(res < 0, "Check of '/foo.bar.baz.d' succeeded unexpectedly");
3260 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
3261 strerror(errno), errno);
3262
3263 pr_fs_clear_cache();
3264 }
3265 END_TEST
3266
3267 START_TEST (fsio_statcache_expired_test) {
3268 unsigned int cache_size, max_age;
3269 int res;
3270 struct stat st;
3271
3272 cache_size = max_age = 1;
3273 pr_fs_statcache_set_policy(cache_size, max_age, 0);
3274
3275 /* First is a cache miss...*/
3276 mark_point();
3277 res = pr_fsio_stat("/tmp", &st);
3278 ck_assert_msg(res == 0, "Failed to stat '/tmp': %s", strerror(errno));
3279
3280 /* Wait for that cached data to expire...*/
3281 sleep(max_age + 1);
3282
3283 /* This is another cache miss, hopefully. */
3284 mark_point();
3285 res = pr_fsio_stat("/tmp2", &st);
3286 ck_assert_msg(res < 0, "Check of '/tmp2' succeeded unexpectedly");
3287 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
3288 strerror(errno), errno);
3289
3290 mark_point();
3291 pr_fs_clear_cache();
3292 }
3293 END_TEST
3294
3295 START_TEST (fsio_statcache_dump_test) {
3296 mark_point();
3297 pr_fs_statcache_dump();
3298 }
3299 END_TEST
3300
3301 START_TEST (fs_create_fs_test) {
3302 pr_fs_t *fs;
3303
3304 fs = pr_create_fs(NULL, NULL);
3305 ck_assert_msg(fs == NULL, "Failed to handle null arguments");
3306 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
3307 strerror(errno), errno);
3308
3309 fs = pr_create_fs(p, NULL);
3310 ck_assert_msg(fs == NULL, "Failed to handle null name");
3311 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
3312 strerror(errno), errno);
3313
3314 fs = pr_create_fs(p, "testsuite");
3315 ck_assert_msg(fs != NULL, "Failed to create FS: %s", strerror(errno));
3316 }
3317 END_TEST
3318
3319 START_TEST (fs_insert_fs_test) {
3320 pr_fs_t *fs, *fs2;
3321 int res;
3322
3323 res = pr_insert_fs(NULL, NULL);
3324 ck_assert_msg(res < 0, "Failed to handle null arguments");
3325 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
3326 strerror(errno), errno);
3327
3328 fs = pr_create_fs(p, "testsuite");
3329 ck_assert_msg(fs != NULL, "Failed to create FS: %s", strerror(errno));
3330
3331 res = pr_insert_fs(fs, NULL);
3332 ck_assert_msg(res < 0, "Failed to handle null path");
3333 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
3334 strerror(errno), errno);
3335
3336 res = pr_insert_fs(fs, "/testsuite");
3337 ck_assert_msg(res == TRUE, "Failed to insert FS: %s", strerror(errno));
3338
3339 res = pr_insert_fs(fs, "/testsuite");
3340 ck_assert_msg(res == FALSE, "Failed to handle duplicate paths");
3341 ck_assert_msg(errno == EEXIST, "Expected EEXIST (%d), got %s (%d)", EEXIST,
3342 strerror(errno), errno);
3343
3344 fs2 = pr_create_fs(p, "testsuite2");
3345 ck_assert_msg(fs2 != NULL, "Failed to create FS: %s", strerror(errno));
3346
3347 res = pr_insert_fs(fs2, "/testsuite2");
3348 ck_assert_msg(res == TRUE, "Failed to insert FS: %s", strerror(errno));
3349
3350 fs2 = pr_create_fs(p, "testsuite3");
3351 ck_assert_msg(fs2 != NULL, "Failed to create FS: %s", strerror(errno));
3352
3353 /* Push this FS on top of the previously registered path; FSes can be
3354 * stacked like this.
3355 */
3356 res = pr_insert_fs(fs2, "/testsuite2");
3357 ck_assert_msg(res == TRUE, "Failed to insert FS: %s", strerror(errno));
3358
3359 (void) pr_remove_fs("/testsuite");
3360 (void) pr_remove_fs("/testsuite2");
3361 (void) pr_remove_fs("/testsuite3");
3362 }
3363 END_TEST
3364
3365 START_TEST (fs_get_fs_test) {
3366 pr_fs_t *fs, *fs2, *fs3;
3367 int exact_match = FALSE, res;
3368
3369 fs = pr_get_fs(NULL, NULL);
3370 ck_assert_msg(fs == NULL, "Failed to handle null arguments");
3371
3372 fs = pr_get_fs("/testsuite", &exact_match);
3373 ck_assert_msg(fs != NULL, "Failed to get FS: %s", strerror(errno));
3374 ck_assert_msg(exact_match == FALSE, "Expected FALSE, got TRUE");
3375
3376 fs2 = pr_create_fs(p, "testsuite");
3377 ck_assert_msg(fs2 != NULL, "Failed to create FS: %s", strerror(errno));
3378
3379 res = pr_insert_fs(fs2, "/testsuite");
3380 ck_assert_msg(res == TRUE, "Failed to insert FS: %s", strerror(errno));
3381
3382 fs = pr_get_fs("/testsuite", &exact_match);
3383 ck_assert_msg(fs != NULL, "Failed to get FS: %s", strerror(errno));
3384 ck_assert_msg(exact_match == TRUE, "Expected TRUE, got FALSE");
3385
3386 fs3 = pr_create_fs(p, "testsuite2");
3387 ck_assert_msg(fs3 != NULL, "Failed to create FS: %s", strerror(errno));
3388
3389 res = pr_insert_fs(fs3, "/testsuite2/");
3390 ck_assert_msg(res == TRUE, "Failed to insert FS: %s", strerror(errno));
3391
3392 exact_match = FALSE;
3393 fs = pr_get_fs("/testsuite2/foo/bar/baz", &exact_match);
3394 ck_assert_msg(fs != NULL, "Failed to get FS: %s", strerror(errno));
3395 ck_assert_msg(exact_match == FALSE, "Expected FALSE, got TRUE");
3396
3397 (void) pr_remove_fs("/testsuite2");
3398 (void) pr_remove_fs("/testsuite");
3399 }
3400 END_TEST
3401
3402 START_TEST (fs_unmount_fs_test) {
3403 pr_fs_t *fs, *fs2;
3404 int res;
3405
3406 fs = pr_unmount_fs(NULL, NULL);
3407 ck_assert_msg(fs == NULL, "Failed to handle null arguments");
3408 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
3409 strerror(errno), errno);
3410
3411 fs = pr_unmount_fs("/testsuite", NULL);
3412 ck_assert_msg(fs == NULL, "Failed to handle absent FS");
3413 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s (%d)", EACCES,
3414 strerror(errno), errno);
3415
3416 fs2 = pr_create_fs(p, "testsuite");
3417 ck_assert_msg(fs2 != NULL, "Failed to create FS: %s", strerror(errno));
3418
3419 res = pr_insert_fs(fs2, "/testsuite");
3420 ck_assert_msg(res == TRUE, "Failed to insert FS: %s", strerror(errno));
3421
3422 fs = pr_unmount_fs("/testsuite", "foo bar");
3423 ck_assert_msg(fs == NULL, "Failed to mismatched path AND name");
3424 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
3425 strerror(errno), errno);
3426
3427 fs = pr_unmount_fs("/testsuite2", NULL);
3428 ck_assert_msg(fs == NULL, "Failed to handle nonexistent path");
3429 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
3430 strerror(errno), errno);
3431
3432 fs2 = pr_unmount_fs("/testsuite", NULL);
3433 ck_assert_msg(fs2 != NULL, "Failed to unmount '/testsuite': %s",
3434 strerror(errno));
3435
3436 fs2 = pr_create_fs(p, "testsuite");
3437 ck_assert_msg(fs2 != NULL, "Failed to create FS: %s", strerror(errno));
3438
3439 res = pr_insert_fs(fs2, "/testsuite");
3440 ck_assert_msg(res == TRUE, "Failed to insert FS: %s", strerror(errno));
3441
3442 fs2 = pr_create_fs(p, "testsuite2");
3443 ck_assert_msg(fs2 != NULL, "Failed to create FS: %s", strerror(errno));
3444
3445 res = pr_insert_fs(fs2, "/testsuite");
3446 ck_assert_msg(res == TRUE, "Failed to insert FS: %s", strerror(errno));
3447
3448 fs2 = pr_unmount_fs("/testsuite", NULL);
3449 ck_assert_msg(fs2 != NULL, "Failed to unmount '/testsuite': %s",
3450 strerror(errno));
3451
3452 fs2 = pr_unmount_fs("/testsuite", NULL);
3453 ck_assert_msg(fs2 != NULL, "Failed to unmount '/testsuite': %s",
3454 strerror(errno));
3455
3456 (void) pr_remove_fs("/testsuite");
3457 (void) pr_remove_fs("/testsuite");
3458 }
3459 END_TEST
3460
3461 START_TEST (fs_remove_fs_test) {
3462 pr_fs_t *fs, *fs2;
3463 int res;
3464
3465 fs = pr_remove_fs(NULL);
3466 ck_assert_msg(fs == NULL, "Failed to handle null arguments");
3467 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
3468 strerror(errno), errno);
3469
3470 fs = pr_remove_fs("/testsuite");
3471 ck_assert_msg(fs == NULL, "Failed to handle absent FS");
3472 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s (%d)", EACCES,
3473 strerror(errno), errno);
3474
3475 fs2 = pr_create_fs(p, "testsuite");
3476 ck_assert_msg(fs2 != NULL, "Failed to create FS: %s", strerror(errno));
3477
3478 res = pr_insert_fs(fs2, "/testsuite");
3479 ck_assert_msg(res == TRUE, "Failed to insert FS: %s", strerror(errno));
3480
3481 fs = pr_remove_fs("/testsuite2");
3482 ck_assert_msg(fs == NULL, "Failed to handle nonexistent path");
3483 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
3484 strerror(errno), errno);
3485
3486 fs2 = pr_remove_fs("/testsuite");
3487 ck_assert_msg(fs2 != NULL, "Failed to remove '/testsuite': %s",
3488 strerror(errno));
3489 }
3490 END_TEST
3491
3492 START_TEST (fs_register_fs_test) {
3493 pr_fs_t *fs, *fs2;
3494
3495 fs = pr_register_fs(NULL, NULL, NULL);
3496 ck_assert_msg(fs == NULL, "Failed to handle null arguments");
3497 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
3498 strerror(errno), errno);
3499
3500 fs = pr_register_fs(p, NULL, NULL);
3501 ck_assert_msg(fs == NULL, "Failed to handle null name");
3502 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
3503 strerror(errno), errno);
3504
3505 fs = pr_register_fs(p, "testsuite", NULL);
3506 ck_assert_msg(fs == NULL, "Failed to handle null path");
3507 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
3508 strerror(errno), errno);
3509
3510 fs = pr_register_fs(p, "testsuite", "/testsuite");
3511 ck_assert_msg(fs != NULL, "Failed to register FS: %s", strerror(errno));
3512
3513 fs2 = pr_register_fs2(p, "testsuite", "/testsuite", 0);
3514 ck_assert_msg(fs2 == NULL, "Failed to handle duplicate names");
3515 ck_assert_msg(errno == EEXIST, "Expected EEXIST (%d), got %s (%d)", EEXIST,
3516 strerror(errno), errno);
3517
3518 (void) pr_remove_fs("/testsuite");
3519 }
3520 END_TEST
3521
3522 START_TEST (fs_register_fs2_test) {
3523 pr_fs_t *fs, *fs2;
3524
3525 fs = pr_register_fs2(NULL, NULL, NULL, 0);
3526 ck_assert_msg(fs == NULL, "Failed to handle null arguments");
3527 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
3528 strerror(errno), errno);
3529
3530 fs = pr_register_fs2(p, NULL, NULL, 0);
3531 ck_assert_msg(fs == NULL, "Failed to handle null name");
3532 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
3533 strerror(errno), errno);
3534
3535 fs = pr_register_fs2(p, "testsuite", NULL, 0);
3536 ck_assert_msg(fs == NULL, "Failed to handle null path");
3537 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
3538 strerror(errno), errno);
3539
3540 fs = pr_register_fs2(p, "testsuite", "/testsuite", 0);
3541 ck_assert_msg(fs != NULL, "Failed to register FS: %s", strerror(errno));
3542
3543 fs2 = pr_register_fs2(p, "testsuite", "/testsuite", 0);
3544 ck_assert_msg(fs2 == NULL, "Failed to handle duplicate names");
3545 ck_assert_msg(errno == EEXIST, "Expected EEXIST (%d), got %s (%d)", EEXIST,
3546 strerror(errno), errno);
3547
3548 (void) pr_remove_fs("/testsuite");
3549 }
3550 END_TEST
3551
3552 START_TEST (fs_unregister_fs_test) {
3553 pr_fs_t *fs;
3554 int res;
3555
3556 res = pr_unregister_fs(NULL);
3557 ck_assert_msg(res < 0, "Failed to handle null argument");
3558 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
3559 strerror(errno), errno);
3560
3561 res = pr_unregister_fs("/testsuite");
3562 ck_assert_msg(res < 0, "Failed to handle nonexistent path");
3563 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
3564 strerror(errno), errno);
3565
3566 fs = pr_register_fs(p, "testsuite", "/testsuite");
3567 ck_assert_msg(fs != NULL, "Failed to register FS: %s", strerror(errno));
3568
3569 res = pr_unregister_fs("/testsuite");
3570 ck_assert_msg(res == 0, "Failed to unregister '/testsuite': %s",
3571 strerror(errno));
3572 }
3573 END_TEST
3574
3575 START_TEST (fs_resolve_fs_map_test) {
3576 pr_fs_t *fs;
3577 int res;
3578
3579 mark_point();
3580 pr_resolve_fs_map();
3581
3582 fs = pr_register_fs(p, "testsuite", "/testsuite");
3583 ck_assert_msg(fs != NULL, "Failed to register FS: %s", strerror(errno));
3584
3585 mark_point();
3586 pr_resolve_fs_map();
3587
3588 res = pr_unregister_fs("/testsuite");
3589 ck_assert_msg(res == 0, "Failed to unregister '/testsuite': %s",
3590 strerror(errno));
3591
3592 mark_point();
3593 pr_resolve_fs_map();
3594 }
3595 END_TEST
3596
3597 #if defined(PR_USE_DEVEL)
3598 START_TEST (fs_dump_fs_test) {
3599 pr_fs_t *fs, *root_fs;
3600
3601 mark_point();
3602 pr_fs_dump(NULL);
3603
3604 root_fs = pr_get_fs("/", NULL);
3605 fs = pr_register_fs(p, "testsuite", "/testsuite");
3606
3607 mark_point();
3608 pr_fs_dump(NULL);
3609
3610 fs->stat = root_fs->stat;
3611 fs->fstat = root_fs->fstat;
3612 fs->lstat = root_fs->lstat;
3613 fs->rename = root_fs->rename;
3614 fs->unlink = root_fs->unlink;
3615 fs->open = root_fs->open;
3616 fs->close = root_fs->close;
3617 fs->read = root_fs->read;
3618 fs->write = root_fs->write;
3619 fs->lseek = root_fs->lseek;
3620 fs->link = root_fs->link;
3621 fs->readlink = root_fs->readlink;
3622 fs->symlink = root_fs->symlink;
3623 fs->ftruncate = root_fs->ftruncate;
3624 fs->truncate = root_fs->truncate;
3625 fs->chmod = root_fs->chmod;
3626 fs->fchmod = root_fs->fchmod;
3627 fs->chown = root_fs->chown;
3628 fs->fchown = root_fs->fchown;
3629 fs->lchown = root_fs->lchown;
3630 fs->access = root_fs->access;
3631 fs->faccess = root_fs->faccess;
3632 fs->utimes = root_fs->utimes;
3633 fs->futimes = root_fs->futimes;
3634 fs->fsync = root_fs->fsync;
3635 fs->chdir = root_fs->chdir;
3636 fs->chroot = root_fs->chroot;
3637 fs->opendir = root_fs->opendir;
3638 fs->closedir = root_fs->closedir;
3639 fs->readdir = root_fs->readdir;
3640 fs->mkdir = root_fs->mkdir;
3641 fs->rmdir = root_fs->rmdir;
3642
3643 mark_point();
3644 pr_fs_dump(NULL);
3645
3646 pr_unregister_fs("/testsuite");
3647 }
3648 END_TEST
3649 #endif /* PR_USE_DEVEL */
3650
3651 static int fsio_chroot_cb(pr_fs_t *fs, const char *path) {
3652 return 0;
3653 }
3654
3655 START_TEST (fsio_custom_chroot_test) {
3656 pr_fs_t *fs;
3657 int res;
3658 const char *path;
3659
3660 fs = pr_register_fs(p, "custom", "/testsuite/");
3661 ck_assert_msg(fs != NULL, "Failed to register custom FS: %s", strerror(errno));
3662
3663 fs->chroot = fsio_chroot_cb;
3664
3665 mark_point();
3666 pr_resolve_fs_map();
3667
3668 path = "/testsuite/foo/bar";
3669 res = pr_fsio_chroot(path);
3670 ck_assert_msg(res == 0, "Failed to chroot (via custom FS) to '%s': %s", path,
3671 strerror(errno));
3672
3673 pr_unregister_fs("/testsuite");
3674 }
3675 END_TEST
3676
3677 START_TEST (fs_clean_path_test) {
3678 char res[PR_TUNABLE_PATH_MAX+1], *path, *expected;
3679
3680 mark_point();
3681 pr_fs_clean_path(NULL, NULL, 0);
3682 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
3683 strerror(errno), errno);
3684
3685 path = "/";
3686
3687 mark_point();
3688 pr_fs_clean_path(path, NULL, 0);
3689 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
3690 strerror(errno), errno);
3691
3692 res[sizeof(res)-1] = '\0';
3693
3694 mark_point();
3695 pr_fs_clean_path(path, res, 0);
3696
3697 pr_fs_clean_path(path, res, sizeof(res)-1);
3698 ck_assert_msg(strcmp(res, path) == 0, "Expected cleaned path '%s', got '%s'",
3699 path, res);
3700
3701 res[sizeof(res)-1] = '\0';
3702 path = "/test.txt";
3703 pr_fs_clean_path(path, res, sizeof(res)-1);
3704 ck_assert_msg(strcmp(res, path) == 0, "Expected cleaned path '%s', got '%s'",
3705 path, res);
3706
3707 res[sizeof(res)-1] = '\0';
3708 path = "/test.txt";
3709 pr_fs_clean_path(path, res, sizeof(res)-1);
3710 ck_assert_msg(strcmp(res, path) == 0, "Expected cleaned path '%s', got '%s'",
3711 path, res);
3712
3713 res[sizeof(res)-1] = '\0';
3714 path = "/./test.txt";
3715 pr_fs_clean_path(path, res, sizeof(res)-1);
3716 expected = "/test.txt";
3717 ck_assert_msg(strcmp(res, expected) == 0,
3718 "Expected cleaned path '%s', got '%s'", expected, res);
3719
3720 res[sizeof(res)-1] = '\0';
3721 path = "test.txt";
3722 pr_fs_clean_path(path, res, sizeof(res)-1);
3723 expected = "/test.txt";
3724 ck_assert_msg(strcmp(res, expected) == 0,
3725 "Expected cleaned path '%s', got '%s'", path, res);
3726 }
3727 END_TEST
3728
3729 START_TEST (fs_clean_path2_test) {
3730 char res[PR_TUNABLE_PATH_MAX+1], *path, *expected;
3731 int code;
3732
3733 mark_point();
3734 code = pr_fs_clean_path2(NULL, NULL, 0, 0);
3735 ck_assert_msg(code < 0, "Failed to handle null path");
3736 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
3737 strerror(errno), errno);
3738
3739 path = "/";
3740
3741 mark_point();
3742 code = pr_fs_clean_path2(path, NULL, 0, 0);
3743 ck_assert_msg(code < 0, "Failed to handle null buf");
3744 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
3745 strerror(errno), errno);
3746
3747 res[sizeof(res)-1] = '\0';
3748
3749 mark_point();
3750 code = pr_fs_clean_path2(path, res, 0, 0);
3751 ck_assert_msg(code == 0, "Failed to handle zero length buf: %s",
3752 strerror(errno));
3753
3754 res[sizeof(res)-1] = '\0';
3755 path = "test.txt";
3756 code = pr_fs_clean_path2(path, res, sizeof(res)-1, 0);
3757 ck_assert_msg(code == 0, "Failed to clean path '%s': %s", path,
3758 strerror(errno));
3759 ck_assert_msg(strcmp(res, path) == 0, "Expected cleaned path '%s', got '%s'",
3760 path, res);
3761
3762 res[sizeof(res)-1] = '\0';
3763 path = "/./test.txt";
3764 code = pr_fs_clean_path2(path, res, sizeof(res)-1, 0);
3765 ck_assert_msg(code == 0, "Failed to clean path '%s': %s", path,
3766 strerror(errno));
3767 expected = "/test.txt";
3768 ck_assert_msg(strcmp(res, expected) == 0,
3769 "Expected cleaned path '%s', got '%s'", expected, res);
3770
3771 res[sizeof(res)-1] = '\0';
3772 path = "test.d///test.txt";
3773 code = pr_fs_clean_path2(path, res, sizeof(res)-1, 0);
3774 ck_assert_msg(code == 0, "Failed to clean path '%s': %s", path,
3775 strerror(errno));
3776 expected = "test.d/test.txt";
3777 ck_assert_msg(strcmp(res, expected) == 0,
3778 "Expected cleaned path '%s', got '%s'", expected, res);
3779
3780 res[sizeof(res)-1] = '\0';
3781 path = "/test.d///test.txt";
3782 code = pr_fs_clean_path2(path, res, sizeof(res)-1,
3783 PR_FSIO_CLEAN_PATH_FL_MAKE_ABS_PATH);
3784 ck_assert_msg(code == 0, "Failed to clean path '%s': %s", path,
3785 strerror(errno));
3786 expected = "/test.d/test.txt";
3787 ck_assert_msg(strcmp(res, expected) == 0,
3788 "Expected cleaned path '%s', got '%s'", expected, res);
3789 }
3790 END_TEST
3791
3792 START_TEST (fs_dircat_test) {
3793 char buf[PR_TUNABLE_PATH_MAX+1], *a, *b, *ok;
3794 int res;
3795
3796 res = pr_fs_dircat(NULL, 0, NULL, NULL);
3797 ck_assert_msg(res == -1, "Failed to handle null arguments");
3798 ck_assert_msg(errno == EINVAL,
3799 "Failed to set errno to EINVAL for null arguments");
3800
3801 res = pr_fs_dircat(buf, 0, "foo", "bar");
3802 ck_assert_msg(res == -1, "Failed to handle zero-length buffer");
3803 ck_assert_msg(errno == EINVAL,
3804 "Failed to set errno to EINVAL for zero-length buffer");
3805
3806 res = pr_fs_dircat(buf, -1, "foo", "bar");
3807 ck_assert_msg(res == -1, "Failed to handle negative-length buffer");
3808 ck_assert_msg(errno == EINVAL,
3809 "Failed to set errno to EINVAL for negative-length buffer");
3810
3811 a = pcalloc(p, PR_TUNABLE_PATH_MAX);
3812 memset(a, 'A', PR_TUNABLE_PATH_MAX-1);
3813
3814 b = "foo";
3815
3816 res = pr_fs_dircat(buf, sizeof(buf)-1, a, b);
3817 ck_assert_msg(res == -1, "Failed to handle too-long paths");
3818 ck_assert_msg(errno == ENAMETOOLONG,
3819 "Failed to set errno to ENAMETOOLONG for too-long paths");
3820
3821 a = "foo";
3822 b = "/bar";
3823 ok = b;
3824 res = pr_fs_dircat(buf, sizeof(buf)-1, a, b);
3825 ck_assert_msg(res == 0, "Failed to concatenate abs-path path second dir");
3826 ck_assert_msg(strcmp(buf, ok) == 0,
3827 "Expected concatenated dir '%s', got '%s'", ok, buf);
3828
3829 a = "foo";
3830 b = "bar";
3831 ok = "foo/bar";
3832 res = pr_fs_dircat(buf, sizeof(buf)-1, a, b);
3833 ck_assert_msg(res == 0, "Failed to concatenate two normal paths");
3834 ck_assert_msg(strcmp(buf, ok) == 0,
3835 "Expected concatenated dir '%s', got '%s'", ok, buf);
3836
3837 a = "foo/";
3838 b = "bar";
3839 ok = "foo/bar";
3840 res = pr_fs_dircat(buf, sizeof(buf)-1, a, b);
3841 ck_assert_msg(res == 0, "Failed to concatenate first dir with trailing slash");
3842 ck_assert_msg(strcmp(buf, ok) == 0, "Expected concatenated dir '%s', got '%s'",
3843 ok, buf);
3844
3845 a = "";
3846 b = "";
3847 ok = "/";
3848 res = pr_fs_dircat(buf, sizeof(buf)-1, a, b);
3849 ck_assert_msg(res == 0, "Failed to concatenate two empty paths");
3850 ck_assert_msg(strcmp(buf, ok) == 0, "Expected concatenated dir '%s', got '%s'",
3851 ok, buf);
3852
3853 a = "/foo";
3854 b = "";
3855 ok = "/foo/";
3856 res = pr_fs_dircat(buf, sizeof(buf)-1, a, b);
3857 ck_assert_msg(res == 0, "Failed to concatenate two empty paths");
3858 ck_assert_msg(strcmp(buf, ok) == 0, "Expected concatenated dir '%s', got '%s'",
3859 ok, buf);
3860
3861 a = "";
3862 b = "/bar";
3863 ok = "/bar/";
3864 res = pr_fs_dircat(buf, sizeof(buf)-1, a, b);
3865 ck_assert_msg(res == 0, "Failed to concatenate two empty paths");
3866 ck_assert_msg(strcmp(buf, ok) == 0, "Expected concatenated dir '%s', got '%s'",
3867 ok, buf);
3868 }
3869 END_TEST
3870
3871 START_TEST (fs_setcwd_test) {
3872 int res;
3873 const char *wd;
3874
3875 /* Make sure that we don't segfault if we call pr_fs_setcwd() on the
3876 * buffer that it is already using.
3877 */
3878 res = pr_fs_setcwd(pr_fs_getcwd());
3879 ck_assert_msg(res == 0, "Failed to set cwd to '%s': %s", pr_fs_getcwd(),
3880 strerror(errno));
3881
3882 wd = pr_fs_getcwd();
3883 ck_assert_msg(wd != NULL, "Failed to get working directory: %s",
3884 strerror(errno));
3885 ck_assert_msg(strcmp(wd, fsio_cwd) == 0,
3886 "Expected '%s', got '%s'", fsio_cwd, wd);
3887
3888 wd = pr_fs_getvwd();
3889 ck_assert_msg(wd != NULL, "Failed to get working directory: %s",
3890 strerror(errno));
3891 ck_assert_msg(strcmp(wd, "/") == 0, "Expected '/', got '%s'", wd);
3892 }
3893 END_TEST
3894
3895 START_TEST (fs_glob_test) {
3896 glob_t pglob;
3897 int res;
3898
3899 res = pr_fs_glob(NULL, 0, NULL, NULL);
3900 ck_assert_msg(res < 0, "Failed to handle null arguments");
3901 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
3902 strerror(errno), errno);
3903
3904 res = pr_fs_glob(NULL, 0, NULL, &pglob);
3905 ck_assert_msg(res < 0, "Failed to handle null arguments");
3906 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
3907 strerror(errno), errno);
3908
3909 memset(&pglob, 0, sizeof(pglob));
3910 res = pr_fs_glob("*", 0, NULL, &pglob);
3911 ck_assert_msg(res == 0, "Failed to glob: glob(3) returned %d: %s", res,
3912 strerror(errno));
3913 ck_assert_msg(pglob.gl_pathc > 0, "Expected >0, got %lu",
3914 (unsigned long) pglob.gl_pathc);
3915
3916 mark_point();
3917 pr_fs_globfree(NULL);
3918 if (res == 0) {
3919 pr_fs_globfree(&pglob);
3920 }
3921 }
3922 END_TEST
3923
3924 START_TEST (fs_copy_file_test) {
3925 int res;
3926 char *src_path = NULL, *dst_path = NULL, *text;
3927 pr_fh_t *fh;
3928
3929 res = pr_fs_copy_file(NULL, NULL);
3930 ck_assert_msg(res < 0, "Failed to handle null arguments");
3931 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
3932 strerror(errno), errno);
3933
3934 src_path = (char *) fsio_copy_src_path;
3935 res = pr_fs_copy_file(src_path, NULL);
3936 ck_assert_msg(res < 0, "Failed to handle null destination path");
3937 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
3938 strerror(errno), errno);
3939
3940 dst_path = (char *) fsio_copy_dst_path;
3941 res = pr_fs_copy_file(src_path, dst_path);
3942 ck_assert_msg(res < 0, "Failed to handle nonexistent source path");
3943 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
3944 strerror(errno), errno);
3945
3946 res = pr_fs_copy_file("/tmp", dst_path);
3947 ck_assert_msg(res < 0, "Failed to handle directory source path");
3948 ck_assert_msg(errno == EISDIR, "Expected EISDIR (%d), got %s (%d)", EISDIR,
3949 strerror(errno), errno);
3950
3951 (void) unlink(src_path);
3952 fh = pr_fsio_open(src_path, O_CREAT|O_EXCL|O_WRONLY);
3953 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", src_path, strerror(errno));
3954
3955 text = "Hello, World!\n";
3956 res = pr_fsio_write(fh, text, strlen(text));
3957 ck_assert_msg(res >= 0, "Failed to write '%s' to '%s': %s", text, src_path,
3958 strerror(errno));
3959
3960 res = pr_fsio_close(fh);
3961 ck_assert_msg(res == 0, "Failed to close '%s': %s", src_path, strerror(errno));
3962
3963 res = pr_fs_copy_file(src_path, "/tmp");
3964 ck_assert_msg(res < 0, "Failed to handle directory destination path");
3965 ck_assert_msg(errno == EISDIR, "Expected EISDIR (%d), got %s (%d)", EISDIR,
3966 strerror(errno), errno);
3967
3968 res = pr_fs_copy_file(src_path, "/tmp/foo/bar/baz/quxx/quzz.dat");
3969 ck_assert_msg(res < 0, "Failed to handle nonexistent destination path");
3970 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
3971 strerror(errno), errno);
3972
3973 mark_point();
3974 res = pr_fs_copy_file(src_path, src_path);
3975 ck_assert_msg(res == 0, "Failed to copy file to itself: %s", strerror(errno));
3976
3977 (void) unlink(dst_path);
3978
3979 mark_point();
3980 res = pr_fs_copy_file(src_path, dst_path);
3981 ck_assert_msg(res == 0, "Failed to copy file: %s", strerror(errno));
3982
3983 (void) pr_fsio_unlink(src_path);
3984 (void) pr_fsio_unlink(dst_path);
3985 }
3986 END_TEST
3987
3988 static unsigned int copy_progress_iter = 0;
3989 static void copy_progress_cb(int nwritten) {
3990 copy_progress_iter++;
3991 }
3992
3993 START_TEST (fs_copy_file2_test) {
3994 int res, flags;
3995 char *src_path, *dst_path, *text;
3996 pr_fh_t *fh;
3997
3998 src_path = (char *) fsio_copy_src_path;
3999 dst_path = (char *) fsio_copy_dst_path;
4000 flags = PR_FSIO_COPY_FILE_FL_NO_DELETE_ON_FAILURE;
4001
4002 (void) unlink(src_path);
4003 (void) unlink(dst_path);
4004
4005 fh = pr_fsio_open(src_path, O_CREAT|O_EXCL|O_WRONLY);
4006 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", src_path, strerror(errno));
4007
4008 text = "Hello, World!\n";
4009 res = pr_fsio_write(fh, text, strlen(text));
4010 ck_assert_msg(res >= 0, "Failed to write '%s' to '%s': %s", text, src_path,
4011 strerror(errno));
4012
4013 res = pr_fsio_close(fh);
4014 ck_assert_msg(res == 0, "Failed to close '%s': %s", src_path, strerror(errno));
4015
4016 copy_progress_iter = 0;
4017
4018 mark_point();
4019 res = pr_fs_copy_file2(src_path, dst_path, flags, copy_progress_cb);
4020 ck_assert_msg(res == 0, "Failed to copy file: %s", strerror(errno));
4021
4022 (void) pr_fsio_unlink(src_path);
4023 (void) pr_fsio_unlink(dst_path);
4024
4025 ck_assert_msg(copy_progress_iter > 0, "Unexpected progress callback count (%u)",
4026 copy_progress_iter);
4027 }
4028 END_TEST
4029
4030 START_TEST (fs_interpolate_test) {
4031 int res;
4032 char buf[PR_TUNABLE_PATH_MAX], *path;
4033
4034 memset(buf, '\0', sizeof(buf));
4035
4036 mark_point();
4037 res = pr_fs_interpolate(NULL, NULL, 0);
4038 ck_assert_msg(res < 0, "Failed to handle null arguments");
4039 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4040 strerror(errno), errno);
4041
4042 mark_point();
4043 path = "/tmp";
4044 res = pr_fs_interpolate(path, NULL, 0);
4045 ck_assert_msg(res < 0, "Failed to handle null buffer");
4046 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4047 strerror(errno), errno);
4048
4049 mark_point();
4050 res = pr_fs_interpolate(path, buf, 0);
4051 ck_assert_msg(res < 0, "Failed to handle zero buffer length");
4052 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4053 strerror(errno), errno);
4054
4055 mark_point();
4056 res = pr_fs_interpolate(path, buf, sizeof(buf)-1);
4057 ck_assert_msg(res == 1, "Failed to interpolate path '%s': %s", path,
4058 strerror(errno));
4059 ck_assert_msg(strcmp(buf, path) == 0, "Expected '%s', got '%s'", path, buf);
4060
4061 mark_point();
4062 path = "~/foo/bar/baz/quzz/quzz.d";
4063 res = pr_fs_interpolate(path, buf, sizeof(buf)-1);
4064 ck_assert_msg(res == 1, "Failed to interpolate path '%s': %s", path,
4065 strerror(errno));
4066 ck_assert_msg(strcmp(buf, path+1) == 0, "Expected '%s', got '%s'",
4067 path + 1, buf);
4068
4069 mark_point();
4070 path = "~";
4071 res = pr_fs_interpolate(path, buf, sizeof(buf)-1);
4072 ck_assert_msg(res == 1, "Failed to interpolate path '%s': %s", path,
4073 strerror(errno));
4074 ck_assert_msg(strcmp(buf, "/") == 0, "Expected '/', got '%s'", buf);
4075
4076 mark_point();
4077 session.chroot_path = "/tmp";
4078 res = pr_fs_interpolate(path, buf, sizeof(buf)-1);
4079 ck_assert_msg(res == 1, "Failed to interpolate path '%s': %s", path,
4080 strerror(errno));
4081 ck_assert_msg(strcmp(buf, session.chroot_path) == 0,
4082 "Expected '%s', got '%s'", session.chroot_path, buf);
4083
4084 session.chroot_path = NULL;
4085
4086 mark_point();
4087 session.user_homedir = "/foo";
4088 res = pr_fs_interpolate(path, buf, sizeof(buf)-1);
4089 ck_assert_msg(res == 1, "Failed to interpolate path '%s': %s", path,
4090 strerror(errno));
4091 ck_assert_msg(strcmp(buf, session.user_homedir) == 0,
4092 "Expected '%s', got '%s'", session.user_homedir, buf);
4093
4094 session.user_homedir = NULL;
4095
4096 mark_point();
4097 path = "~foo.bar.baz.quzz";
4098 res = pr_fs_interpolate(path, buf, sizeof(buf)-1);
4099 ck_assert_msg(res < 0, "Interpolated '%s' unexpectedly", path);
4100 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
4101 strerror(errno), errno);
4102
4103 mark_point();
4104 session.user = "testsuite";
4105 path = "~/tmp.d/test.d/foo.d/bar.d";
4106 res = pr_fs_interpolate(path, buf, sizeof(buf)-1);
4107 ck_assert_msg(res < 0, "Interpolated '%s' unexpectedly", path);
4108 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
4109 strerror(errno), errno);
4110
4111 mark_point();
4112 session.user_homedir = "/cached";
4113 path = "~/tmp.d/test.d/foo.d/bar.d";
4114 res = pr_fs_interpolate(path, buf, sizeof(buf)-1);
4115 ck_assert_msg(res == 1, "Failed to interpolate path '%s': %s", path,
4116 strerror(errno));
4117 ck_assert_msg(strcmp(buf, "/cached/tmp.d/test.d/foo.d/bar.d") == 0,
4118 "Expected '/cached/tmp.d/test.d/foo.d/bar.d', got '%s'", buf);
4119
4120 session.user = NULL;
4121 session.user_homedir = NULL;
4122 }
4123 END_TEST
4124
4125 START_TEST (fs_resolve_partial_test) {
4126 int op = FSIO_FILE_STAT, res;
4127 char buf[PR_TUNABLE_PATH_MAX], *path;
4128
4129 res = pr_fs_resolve_partial(NULL, NULL, 0, op);
4130 ck_assert_msg(res < 0, "Failed to handle null arguments");
4131 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4132 strerror(errno), errno);
4133
4134 path = "/tmp";
4135 res = pr_fs_resolve_partial(path, NULL, 0, op);
4136 ck_assert_msg(res < 0, "Failed to handle null buffer");
4137 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4138 strerror(errno), errno);
4139
4140 memset(buf, '\0', sizeof(buf));
4141 res = pr_fs_resolve_partial(path, buf, 0, op);
4142 ck_assert_msg(res < 0, "Failed to handle zero buffer length");
4143 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4144 strerror(errno), errno);
4145
4146 res = pr_fs_resolve_partial(path, buf, sizeof(buf)-1, op);
4147 ck_assert_msg(res == 0, "Failed to resolve '%s': %s", path, strerror(errno));
4148 if (strcmp(buf, path) != 0) {
4149 /* Mac-specific hack */
4150 const char *prefix = "/private";
4151
4152 if (strncmp(buf, prefix, strlen(prefix)) != 0) {
4153 ck_abort_msg("Expected '%s', got '%s'", path, buf);
4154 }
4155 }
4156
4157 path = "/tmp/.////./././././.";
4158 res = pr_fs_resolve_partial(path, buf, sizeof(buf)-1, op);
4159 ck_assert_msg(res == 0, "Failed to resolve '%s': %s", path, strerror(errno));
4160 if (strcmp(buf, path) != 0) {
4161 /* Mac-specific hack */
4162 const char *prefix = "/private";
4163
4164 if (strncmp(buf, prefix, strlen(prefix)) != 0 &&
4165 strcmp(buf, "/tmp/") != 0) {
4166 ck_abort_msg("Expected '%s', got '%s'", path, buf);
4167 }
4168 }
4169
4170 path = "/../../../.././..///../";
4171 res = pr_fs_resolve_partial(path, buf, sizeof(buf)-1, op);
4172 ck_assert_msg(res == 0, "Failed to resolve '%s': %s", path, strerror(errno));
4173 if (strcmp(buf, "/") != 0) {
4174 /* Mac-specific hack */
4175 const char *prefix = "/private";
4176
4177 if (strncmp(buf, prefix, strlen(prefix)) != 0) {
4178 ck_abort_msg("Expected '%s', got '%s'", path, buf);
4179 }
4180 }
4181
4182 path = "/tmp/.///../../..../";
4183 res = pr_fs_resolve_partial(path, buf, sizeof(buf)-1, op);
4184 ck_assert_msg(res < 0, "Resolved '%s' unexpectedly", path);
4185 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
4186 strerror(errno), errno);
4187
4188 path = "~foo/.///../../..../";
4189 res = pr_fs_resolve_partial(path, buf, sizeof(buf)-1, op);
4190 ck_assert_msg(res < 0, "Resolved '%s' unexpectedly", path);
4191 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
4192 strerror(errno), errno);
4193
4194 path = "../../..../";
4195 res = pr_fs_resolve_partial(path, buf, sizeof(buf)-1, op);
4196 ck_assert_msg(res < 0, "Resolved '%s' unexpectedly", path);
4197 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
4198 strerror(errno), errno);
4199
4200 /* Resolve a symlink path */
4201 res = pr_fsio_symlink("/tmp", fsio_link_path);
4202 ck_assert_msg(res == 0, "Failed to create symlink to '%s': %s", fsio_link_path,
4203 strerror(errno));
4204
4205 res = pr_fs_resolve_partial(fsio_link_path, buf, sizeof(buf)-1, op);
4206 ck_assert_msg(res == 0, "Failed to resolve '%s': %s", fsio_link_path,
4207 strerror(errno));
4208
4209 (void) unlink(fsio_link_path);
4210 }
4211 END_TEST
4212
4213 START_TEST (fs_resolve_path_test) {
4214 int op = FSIO_FILE_STAT, res;
4215 char buf[PR_TUNABLE_PATH_MAX], *path;
4216
4217 memset(buf, '\0', sizeof(buf));
4218
4219 res = pr_fs_resolve_path(NULL, NULL, 0, op);
4220 ck_assert_msg(res < 0, "Failed to handle null arguments");
4221 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4222 strerror(errno), errno);
4223
4224 path = "/tmp";
4225 res = pr_fs_resolve_path(path, NULL, 0, op);
4226 ck_assert_msg(res < 0, "Failed to handle null buffer");
4227 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4228 strerror(errno), errno);
4229
4230 res = pr_fs_resolve_path(path, buf, 0, op);
4231 ck_assert_msg(res < 0, "Failed to handle zero buffer length");
4232 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4233 strerror(errno), errno);
4234
4235 res = pr_fs_resolve_path(path, buf, sizeof(buf)-1, op);
4236 ck_assert_msg(res == 0, "Failed to resolve path '%s': %s", path,
4237 strerror(errno));
4238 if (strcmp(buf, path) != 0) {
4239 /* Mac-specific hack */
4240 const char *prefix = "/private";
4241
4242 if (strncmp(buf, prefix, strlen(prefix)) != 0) {
4243 ck_abort_msg("Expected '%s', got '%s'", path, buf);
4244 }
4245 }
4246
4247 /* Resolve a symlink path */
4248 res = pr_fsio_symlink("/tmp", fsio_link_path);
4249 ck_assert_msg(res == 0, "Failed to create symlink to '%s': %s", fsio_link_path,
4250 strerror(errno));
4251
4252 res = pr_fs_resolve_path(fsio_link_path, buf, sizeof(buf)-1, op);
4253 ck_assert_msg(res == 0, "Failed to resolve '%s': %s", fsio_link_path,
4254 strerror(errno));
4255
4256 (void) unlink(fsio_link_path);
4257 }
4258 END_TEST
4259
4260 START_TEST (fs_use_encoding_test) {
4261 int res;
4262
4263 res = pr_fs_use_encoding(-1);
4264 ck_assert_msg(res < 0, "Failed to handle invalid setting");
4265 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4266 strerror(errno), errno);
4267
4268 res = pr_fs_use_encoding(TRUE);
4269 ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
4270
4271 res = pr_fs_use_encoding(FALSE);
4272 ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
4273
4274 res = pr_fs_use_encoding(TRUE);
4275 ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
4276 }
4277 END_TEST
4278
4279 START_TEST (fs_decode_path2_test) {
4280 int flags = 0;
4281 char junk[32], *res;
4282 const char *path;
4283
4284 res = pr_fs_decode_path(NULL, NULL);
4285 ck_assert_msg(res == NULL, "Failed to handle null arguments");
4286 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4287 strerror(errno), errno);
4288
4289 res = pr_fs_decode_path(p, NULL);
4290 ck_assert_msg(res == NULL, "Failed to handle null path");
4291 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4292 strerror(errno), errno);
4293
4294 path = "/tmp";
4295 res = pr_fs_decode_path2(p, path, flags);
4296 ck_assert_msg(res != NULL, "Failed to decode path '%s': %s", path,
4297 strerror(errno));
4298
4299 path = "/tmp";
4300 res = pr_fs_decode_path2(p, path, flags);
4301 ck_assert_msg(res != NULL, "Failed to decode path '%s': %s", path,
4302 strerror(errno));
4303
4304 /* Test a path that cannot be decoded, using junk data from the stack */
4305 junk[sizeof(junk)-1] = '\0';
4306 path = junk;
4307 res = pr_fs_decode_path2(p, path, flags);
4308 ck_assert_msg(res != NULL, "Failed to decode path: %s", strerror(errno));
4309
4310 /* XXX Use the FSIO_DECODE_FL_TELL_ERRORS flags, AND set the encode
4311 * policy to use PR_ENCODE_POLICY_FL_REQUIRE_VALID_ENCODING.
4312 */
4313 }
4314 END_TEST
4315
4316 START_TEST (fs_encode_path_test) {
4317 char junk[32], *res;
4318 const char *path;
4319
4320 res = pr_fs_encode_path(NULL, NULL);
4321 ck_assert_msg(res == NULL, "Failed to handle null arguments");
4322 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4323 strerror(errno), errno);
4324
4325 res = pr_fs_encode_path(p, NULL);
4326 ck_assert_msg(res == NULL, "Failed to handle null path");
4327 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4328 strerror(errno), errno);
4329
4330 path = "/tmp";
4331 res = pr_fs_encode_path(p, path);
4332 ck_assert_msg(res != NULL, "Failed to encode path '%s': %s", path,
4333 strerror(errno));
4334
4335 /* Test a path that cannot be encoded, using junk data from the stack */
4336 junk[sizeof(junk)-1] = '\0';
4337 path = junk;
4338 res = pr_fs_encode_path(p, path);
4339 ck_assert_msg(res != NULL, "Failed to encode path: %s", strerror(errno));
4340 }
4341 END_TEST
4342
4343 START_TEST (fs_split_path_test) {
4344 array_header *res;
4345 const char *path, *elt;
4346
4347 mark_point();
4348 res = pr_fs_split_path(NULL, NULL);
4349 ck_assert_msg(res == NULL, "Failed to handle null pool");
4350 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4351 strerror(errno), errno);
4352
4353 mark_point();
4354 res = pr_fs_split_path(p, NULL);
4355 ck_assert_msg(res == NULL, "Failed to handle null path");
4356 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4357 strerror(errno), errno);
4358
4359 path = "";
4360
4361 mark_point();
4362 res = pr_fs_split_path(p, path);
4363 ck_assert_msg(res == NULL, "Failed to handle empty path");
4364 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4365 strerror(errno), errno);
4366
4367 path = ".";
4368
4369 mark_point();
4370 res = pr_fs_split_path(p, path);
4371 ck_assert_msg(res != NULL, "Failed to split path '%s': %s", path,
4372 strerror(errno));
4373 ck_assert_msg(res->nelts == 1, "Expected 1, got %u", res->nelts);
4374 elt = ((char **) res->elts)[0];
4375
4376 /* Note: pr_fs_split_path() cleans the path into an absolute path. And
4377 * the default "cwd" test setting is "/", so yes, this is surprising but
4378 * expected. Subject to change in the future.
4379 */
4380 ck_assert_msg(strcmp(elt, "/") == 0, "Expected '/', got '%s'", elt);
4381
4382 path = "/";
4383
4384 mark_point();
4385 res = pr_fs_split_path(p, path);
4386 ck_assert_msg(res != NULL, "Failed to split path '%s': %s", path,
4387 strerror(errno));
4388 ck_assert_msg(res->nelts == 1, "Expected 1, got %u", res->nelts);
4389 elt = ((char **) res->elts)[0];
4390 ck_assert_msg(strcmp(elt, "/") == 0, "Expected '/', got '%s'", elt);
4391
4392 path = "///";
4393
4394 mark_point();
4395 res = pr_fs_split_path(p, path);
4396 ck_assert_msg(res != NULL, "Failed to split path '%s': %s", path,
4397 strerror(errno));
4398 ck_assert_msg(res->nelts == 1, "Expected 1, got %u", res->nelts);
4399 elt = ((char **) res->elts)[0];
4400 ck_assert_msg(strcmp(elt, "/") == 0, "Expected '/', got '%s'", elt);
4401
4402 path = "/foo/bar/baz/";
4403
4404 mark_point();
4405 res = pr_fs_split_path(p, path);
4406 ck_assert_msg(res != NULL, "Failed to split path '%s': %s", path,
4407 strerror(errno));
4408 ck_assert_msg(res->nelts == 4, "Expected 4, got %u", res->nelts);
4409 elt = ((char **) res->elts)[0];
4410 ck_assert_msg(strcmp(elt, "/") == 0, "Expected '/', got '%s'", elt);
4411 elt = ((char **) res->elts)[1];
4412 ck_assert_msg(strcmp(elt, "foo") == 0, "Expected 'foo', got '%s'", elt);
4413 elt = ((char **) res->elts)[2];
4414 ck_assert_msg(strcmp(elt, "bar") == 0, "Expected 'bar', got '%s'", elt);
4415 elt = ((char **) res->elts)[3];
4416 ck_assert_msg(strcmp(elt, "baz") == 0, "Expected 'baz', got '%s'", elt);
4417
4418 path = "/foo//bar//baz//";
4419
4420 mark_point();
4421 res = pr_fs_split_path(p, path);
4422 ck_assert_msg(res != NULL, "Failed to split path '%s': %s", path,
4423 strerror(errno));
4424 ck_assert_msg(res->nelts == 4, "Expected 4, got %u", res->nelts);
4425 elt = ((char **) res->elts)[0];
4426 ck_assert_msg(strcmp(elt, "/") == 0, "Expected '/', got '%s'", elt);
4427 elt = ((char **) res->elts)[1];
4428 ck_assert_msg(strcmp(elt, "foo") == 0, "Expected 'foo', got '%s'", elt);
4429 elt = ((char **) res->elts)[2];
4430 ck_assert_msg(strcmp(elt, "bar") == 0, "Expected 'bar', got '%s'", elt);
4431 elt = ((char **) res->elts)[3];
4432 ck_assert_msg(strcmp(elt, "baz") == 0, "Expected 'baz', got '%s'", elt);
4433
4434 path = "/foo/bar/baz";
4435
4436 mark_point();
4437 res = pr_fs_split_path(p, path);
4438 ck_assert_msg(res != NULL, "Failed to split path '%s': %s", path,
4439 strerror(errno));
4440 ck_assert_msg(res->nelts == 4, "Expected 4, got %u", res->nelts);
4441 elt = ((char **) res->elts)[0];
4442 ck_assert_msg(strcmp(elt, "/") == 0, "Expected '/', got '%s'", elt);
4443 elt = ((char **) res->elts)[1];
4444 ck_assert_msg(strcmp(elt, "foo") == 0, "Expected 'foo', got '%s'", elt);
4445 elt = ((char **) res->elts)[2];
4446 ck_assert_msg(strcmp(elt, "bar") == 0, "Expected 'bar', got '%s'", elt);
4447 elt = ((char **) res->elts)[3];
4448 ck_assert_msg(strcmp(elt, "baz") == 0, "Expected 'baz', got '%s'", elt);
4449
4450 path = "foo/bar/baz";
4451
4452 mark_point();
4453 res = pr_fs_split_path(p, path);
4454 ck_assert_msg(res != NULL, "Failed to split path '%s': %s", path,
4455 strerror(errno));
4456 ck_assert_msg(res->nelts == 3, "Expected 3, got %u", res->nelts);
4457 elt = ((char **) res->elts)[0];
4458 ck_assert_msg(strcmp(elt, "foo") == 0, "Expected 'foo', got '%s'", elt);
4459 elt = ((char **) res->elts)[1];
4460 ck_assert_msg(strcmp(elt, "bar") == 0, "Expected 'bar', got '%s'", elt);
4461 elt = ((char **) res->elts)[2];
4462 ck_assert_msg(strcmp(elt, "baz") == 0, "Expected 'baz', got '%s'", elt);
4463 }
4464 END_TEST
4465
4466 START_TEST (fs_join_path_test) {
4467 char *path;
4468 array_header *components;
4469
4470 mark_point();
4471 path = pr_fs_join_path(NULL, NULL, 0);
4472 ck_assert_msg(path == NULL, "Failed to handle null pool");
4473 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4474 strerror(errno), errno);
4475
4476 mark_point();
4477 path = pr_fs_join_path(p, NULL, 0);
4478 ck_assert_msg(path == NULL, "Failed to handle null components");
4479 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4480 strerror(errno), errno);
4481
4482 components = make_array(p, 0, sizeof(char **));
4483
4484 mark_point();
4485 path = pr_fs_join_path(p, components, 0);
4486 ck_assert_msg(path == NULL, "Failed to handle empty components");
4487 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4488 strerror(errno), errno);
4489
4490 *((char **) push_array(components)) = pstrdup(p, "/");
4491
4492 mark_point();
4493 path = pr_fs_join_path(p, components, 0);
4494 ck_assert_msg(path == NULL, "Failed to handle empty count");
4495 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4496 strerror(errno), errno);
4497
4498 mark_point();
4499 path = pr_fs_join_path(p, components, 3);
4500 ck_assert_msg(path == NULL, "Failed to handle invalid count");
4501 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4502 strerror(errno), errno);
4503
4504 mark_point();
4505 path = pr_fs_join_path(p, components, 1);
4506 ck_assert_msg(path != NULL, "Failed to join path: %s", strerror(errno));
4507 ck_assert_msg(strcmp(path, "/") == 0, "Expected '/', got '%s'", path);
4508
4509 *((char **) push_array(components)) = pstrdup(p, "foo");
4510 *((char **) push_array(components)) = pstrdup(p, "bar");
4511 *((char **) push_array(components)) = pstrdup(p, "baz");
4512
4513 mark_point();
4514 path = pr_fs_join_path(p, components, 4);
4515 ck_assert_msg(path != NULL, "Failed to join path: %s", strerror(errno));
4516 ck_assert_msg(strcmp(path, "/foo/bar/baz") == 0,
4517 "Expected '/foo/bar/baz', got '%s'", path);
4518
4519 mark_point();
4520 path = pr_fs_join_path(p, components, 3);
4521 ck_assert_msg(path != NULL, "Failed to join path: %s", strerror(errno));
4522 ck_assert_msg(strcmp(path, "/foo/bar") == 0, "Expected '/foo/bar', got '%s'",
4523 path);
4524
4525 mark_point();
4526 components = make_array(p, 0, sizeof(char **));
4527
4528 *((char **) push_array(components)) = pstrdup(p, "foo");
4529 *((char **) push_array(components)) = pstrdup(p, "bar");
4530 *((char **) push_array(components)) = pstrdup(p, "baz");
4531
4532 path = pr_fs_join_path(p, components, components->nelts);
4533 ck_assert_msg(path != NULL, "Failed to join path: %s", strerror(errno));
4534 ck_assert_msg(strcmp(path, "foo/bar/baz") == 0,
4535 "Expected 'foo/bar/baz', got '%s'", path);
4536 }
4537 END_TEST
4538
4539 START_TEST (fs_virtual_path_test) {
4540 const char *path;
4541 char buf[PR_TUNABLE_PATH_MAX];
4542
4543 mark_point();
4544 pr_fs_virtual_path(NULL, NULL, 0);
4545
4546 mark_point();
4547 path = "/tmp";
4548 pr_fs_virtual_path(path, NULL, 0);
4549
4550 mark_point();
4551 memset(buf, '\0', sizeof(buf));
4552 pr_fs_virtual_path(path, buf, 0);
4553 ck_assert_msg(*buf == '\0', "Expected empty buffer, got '%s'", buf);
4554
4555 mark_point();
4556 memset(buf, '\0', sizeof(buf));
4557 pr_fs_virtual_path(path, buf, sizeof(buf)-1);
4558 ck_assert_msg(strcmp(buf, path) == 0, "Expected '%s', got '%s'", path, buf);
4559
4560 mark_point();
4561 memset(buf, '\0', sizeof(buf));
4562 path = "tmp";
4563 pr_fs_virtual_path(path, buf, sizeof(buf)-1);
4564 ck_assert_msg(strcmp(buf, "/tmp") == 0, "Expected '/tmp', got '%s'", buf);
4565
4566 mark_point();
4567 memset(buf, '\0', sizeof(buf));
4568 path = "/tmp/././";
4569 pr_fs_virtual_path(path, buf, sizeof(buf)-1);
4570 ck_assert_msg(strcmp(buf, "/tmp") == 0 || strcmp(buf, "/tmp/") == 0,
4571 "Expected '/tmp', got '%s'", buf);
4572
4573 mark_point();
4574 memset(buf, '\0', sizeof(buf));
4575 path = "tmp/../../";
4576 pr_fs_virtual_path(path, buf, sizeof(buf)-1);
4577 ck_assert_msg(strcmp(buf, "/") == 0, "Expected '/', got '%s'", buf);
4578 }
4579 END_TEST
4580
4581 #if 0
4582 /* This test is commented out, since libcheck is very unhappy when we
4583 * close its logging fds out from underneath it. Thus we keep this
4584 * test here, for any future tinkering, just not enabled by default.
4585 */
4586 START_TEST (fs_close_extra_fds_test) {
4587 mark_point();
4588 pr_fs_close_extra_fds();
4589 }
4590 END_TEST
4591 #endif
4592
4593 START_TEST (fs_get_usable_fd_test) {
4594 int fd, res;
4595
4596 fd = -1;
4597 res = pr_fs_get_usable_fd(fd);
4598 ck_assert_msg(res < 0, "Failed to handle bad fd");
4599 ck_assert_msg(errno == EBADF || errno == EINVAL,
4600 "Expected EBADF (%d) or EINVAL (%d), got %s (%d)", EBADF, EINVAL,
4601 strerror(errno), errno);
4602
4603 fd = STDERR_FILENO + 1;
4604 res = pr_fs_get_usable_fd(fd);
4605 ck_assert_msg(res == fd, "Expected %d, got %d", fd, res);
4606
4607 fd = STDERR_FILENO - 1;
4608 res = pr_fs_get_usable_fd(fd);
4609 ck_assert_msg(res > STDERR_FILENO, "Failed to get usable fd for %d: %s", fd,
4610 strerror(errno));
4611 (void) close(res);
4612 }
4613 END_TEST
4614
4615 START_TEST (fs_get_usable_fd2_test) {
4616 int fd, res;
4617
4618 res = pr_fs_get_usable_fd2(NULL);
4619 ck_assert_msg(res < 0, "Failed to handle null argument");
4620 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4621 strerror(errno), errno);
4622
4623 fd = -1;
4624 res = pr_fs_get_usable_fd2(&fd);
4625 ck_assert_msg(res < 0, "Failed to handle bad fd");
4626 ck_assert_msg(errno == EBADF || errno == EINVAL,
4627 "Expected EBADF (%d) or EINVAL (%d), got %s (%d)", EBADF, EINVAL,
4628 strerror(errno), errno);
4629
4630 fd = STDERR_FILENO + 1;
4631 res = pr_fs_get_usable_fd2(&fd);
4632 ck_assert_msg(res == 0, "Failed to handle fd: %s", strerror(errno));
4633 ck_assert_msg(fd == (STDERR_FILENO + 1), "Expected %d, got %d",
4634 STDERR_FILENO + 1, fd);
4635
4636 fd = STDERR_FILENO - 1;
4637 res = pr_fs_get_usable_fd2(&fd);
4638 ck_assert_msg(res == 0, "Failed to handle fd: %s", strerror(errno));
4639 ck_assert_msg(fd > STDERR_FILENO, "Expected >%d, got %d", STDERR_FILENO, fd);
4640 (void) close(fd);
4641 }
4642 END_TEST
4643
4644 START_TEST (fs_getsize_test) {
4645 off_t res;
4646 char *path;
4647
4648 res = pr_fs_getsize(NULL);
4649 ck_assert_msg(res == (off_t) -1, "Failed to handle null argument");
4650 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4651 strerror(errno), errno);
4652
4653 path = "/tmp";
4654 res = pr_fs_getsize(path);
4655 ck_assert_msg(res != (off_t) -1, "Failed to get fs size for '%s': %s", path,
4656 strerror(errno));
4657 }
4658 END_TEST
4659
4660 START_TEST (fs_getsize2_test) {
4661 int res;
4662 char *path;
4663 off_t sz = 0;
4664
4665 res = pr_fs_getsize2(NULL, NULL);
4666 ck_assert_msg(res < 0, "Failed to handle null arguments");
4667 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4668 strerror(errno), errno);
4669
4670 path = "/tmp";
4671 res = pr_fs_getsize2(path, NULL);
4672 ck_assert_msg(res < 0, "Failed to handle null size argument");
4673 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4674 strerror(errno), errno);
4675
4676 res = pr_fs_getsize2(path, &sz);
4677 ck_assert_msg(res == 0, "Failed to get fs size for '%s': %s", path,
4678 strerror(errno));
4679 }
4680 END_TEST
4681
4682 START_TEST (fs_fgetsize_test) {
4683 int fd = -1, res;
4684 off_t fs_sz = 0;
4685
4686 mark_point();
4687 res = pr_fs_fgetsize(fd, &fs_sz);
4688 ck_assert_msg(res < 0, "Failed to handle bad file descriptor");
4689 ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
4690 strerror(errno), errno);
4691
4692 mark_point();
4693 fd = 0;
4694 fs_sz = 0;
4695 res = pr_fs_fgetsize(fd, NULL);
4696 ck_assert_msg(res < 0, "Failed to handle null size argument");
4697 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4698 strerror(errno), errno);
4699
4700 mark_point();
4701 fd = 0;
4702 fs_sz = 0;
4703 res = pr_fs_fgetsize(fd, &fs_sz);
4704 ck_assert_msg(res == 0, "Failed to get fs size for fd %d: %s", fd,
4705 strerror(errno));
4706 }
4707 END_TEST
4708
4709 START_TEST (fs_fadvise_test) {
4710 int advice, fd = -1;
4711 off_t off = 0, len = 0;
4712
4713 /* We make these function calls to exercise the code paths, even
4714 * though there's no good way to verify the behavior changed.
4715 */
4716
4717 advice = PR_FS_FADVISE_NORMAL;
4718 pr_fs_fadvise(fd, off, len, advice);
4719
4720 advice = PR_FS_FADVISE_RANDOM;
4721 pr_fs_fadvise(fd, off, len, advice);
4722
4723 advice = PR_FS_FADVISE_SEQUENTIAL;
4724 pr_fs_fadvise(fd, off, len, advice);
4725
4726 advice = PR_FS_FADVISE_WILLNEED;
4727 pr_fs_fadvise(fd, off, len, advice);
4728
4729 advice = PR_FS_FADVISE_DONTNEED;
4730 pr_fs_fadvise(fd, off, len, advice);
4731
4732 advice = PR_FS_FADVISE_NOREUSE;
4733 pr_fs_fadvise(fd, off, len, advice);
4734 }
4735 END_TEST
4736
4737 START_TEST (fs_have_access_test) {
4738 int res;
4739 struct stat st;
4740 uid_t uid;
4741 gid_t gid;
4742 array_header *suppl_gids;
4743
4744 mark_point();
4745 res = pr_fs_have_access(NULL, R_OK, 0, 0, NULL);
4746 ck_assert_msg(res < 0, "Failed to handle null stat");
4747 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4748 strerror(errno), errno);
4749
4750 memset(&st, 0, sizeof(struct stat));
4751
4752 mark_point();
4753 res = pr_fs_have_access(&st, R_OK, 0, 0, NULL);
4754 ck_assert_msg(res == 0, "Failed to handle root access: %s", strerror(errno));
4755
4756 /* Use cases: no matching UID or GID; R_OK, W_OK, X_OK. */
4757 memset(&st, 0, sizeof(struct stat));
4758 uid = 1;
4759 gid = 1;
4760
4761 mark_point();
4762 res = pr_fs_have_access(&st, R_OK, uid, gid, NULL);
4763 ck_assert_msg(res < 0, "Failed to handle missing other R_OK access");
4764 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s (%d)", EACCES,
4765 strerror(errno), errno);
4766
4767 mark_point();
4768 res = pr_fs_have_access(&st, W_OK, uid, gid, NULL);
4769 ck_assert_msg(res < 0, "Failed to handle missing other W_OK access");
4770 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s (%d)", EACCES,
4771 strerror(errno), errno);
4772
4773 mark_point();
4774 res = pr_fs_have_access(&st, X_OK, uid, gid, NULL);
4775 ck_assert_msg(res < 0, "Failed to handle missing other X_OK access");
4776 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s (%d)", EACCES,
4777 strerror(errno), errno);
4778
4779 st.st_mode = S_IFMT|S_IROTH|S_IWOTH|S_IXOTH;
4780
4781 mark_point();
4782 res = pr_fs_have_access(&st, R_OK, uid, gid, NULL);
4783 ck_assert_msg(res == 0, "Failed to handle other R_OK access: %s",
4784 strerror(errno));
4785
4786 mark_point();
4787 res = pr_fs_have_access(&st, W_OK, uid, gid, NULL);
4788 ck_assert_msg(res == 0, "Failed to handle other W_OK access: %s",
4789 strerror(errno));
4790
4791 mark_point();
4792 res = pr_fs_have_access(&st, X_OK, uid, gid, NULL);
4793 ck_assert_msg(res == 0, "Failed to handle other X_OK access: %s",
4794 strerror(errno));
4795
4796 /* Use cases: matching UID, not GID; R_OK, W_OK, X_OK. */
4797 memset(&st, 0, sizeof(struct stat));
4798
4799 st.st_uid = uid;
4800
4801 mark_point();
4802 res = pr_fs_have_access(&st, R_OK, uid, gid, NULL);
4803 ck_assert_msg(res < 0, "Failed to handle missing user R_OK access");
4804 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s (%d)", EACCES,
4805 strerror(errno), errno);
4806
4807 mark_point();
4808 res = pr_fs_have_access(&st, W_OK, uid, gid, NULL);
4809 ck_assert_msg(res < 0, "Failed to handle missing user W_OK access");
4810 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s (%d)", EACCES,
4811 strerror(errno), errno);
4812
4813 mark_point();
4814 res = pr_fs_have_access(&st, X_OK, uid, gid, NULL);
4815 ck_assert_msg(res < 0, "Failed to handle missing user X_OK access");
4816 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s (%d)", EACCES,
4817 strerror(errno), errno);
4818
4819 st.st_mode = S_IFMT|S_IRUSR|S_IWUSR|S_IXUSR;
4820
4821 mark_point();
4822 res = pr_fs_have_access(&st, R_OK, uid, gid, NULL);
4823 ck_assert_msg(res == 0, "Failed to handle user R_OK access: %s",
4824 strerror(errno));
4825
4826 mark_point();
4827 res = pr_fs_have_access(&st, W_OK, uid, gid, NULL);
4828 ck_assert_msg(res == 0, "Failed to handle user W_OK access: %s",
4829 strerror(errno));
4830
4831 mark_point();
4832 res = pr_fs_have_access(&st, X_OK, uid, gid, NULL);
4833 ck_assert_msg(res == 0, "Failed to handle user X_OK access: %s",
4834 strerror(errno));
4835
4836 /* Use cases: matching GID, not UID; R_OK, W_OK, X_OK. */
4837 memset(&st, 0, sizeof(struct stat));
4838
4839 st.st_gid = gid;
4840
4841 mark_point();
4842 res = pr_fs_have_access(&st, R_OK, uid, gid, NULL);
4843 ck_assert_msg(res < 0, "Failed to handle missing group R_OK access");
4844 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s (%d)", EACCES,
4845 strerror(errno), errno);
4846
4847 mark_point();
4848 res = pr_fs_have_access(&st, W_OK, uid, gid, NULL);
4849 ck_assert_msg(res < 0, "Failed to handle missing group W_OK access");
4850 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s (%d)", EACCES,
4851 strerror(errno), errno);
4852
4853 mark_point();
4854 res = pr_fs_have_access(&st, X_OK, uid, gid, NULL);
4855 ck_assert_msg(res < 0, "Failed to handle missing group X_OK access");
4856 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s (%d)", EACCES,
4857 strerror(errno), errno);
4858
4859 st.st_mode = S_IFMT|S_IRGRP|S_IWGRP|S_IXGRP;
4860
4861 mark_point();
4862 res = pr_fs_have_access(&st, R_OK, uid, gid, NULL);
4863 ck_assert_msg(res == 0, "Failed to handle group R_OK access: %s",
4864 strerror(errno));
4865
4866 mark_point();
4867 res = pr_fs_have_access(&st, W_OK, uid, gid, NULL);
4868 ck_assert_msg(res == 0, "Failed to handle group W_OK access: %s",
4869 strerror(errno));
4870
4871 mark_point();
4872 res = pr_fs_have_access(&st, X_OK, uid, gid, NULL);
4873 ck_assert_msg(res == 0, "Failed to handle group X_OK access: %s",
4874 strerror(errno));
4875
4876 /* Use cases: matching suppl GID, not UID; R_OK, W_OK, X_OK. */
4877 memset(&st, 0, sizeof(struct stat));
4878
4879 suppl_gids = make_array(p, 1, sizeof(gid_t));
4880 *((gid_t *) push_array(suppl_gids)) = 100;
4881 *((gid_t *) push_array(suppl_gids)) = gid;
4882 st.st_gid = gid;
4883
4884 mark_point();
4885 res = pr_fs_have_access(&st, R_OK, uid, 0, suppl_gids);
4886 ck_assert_msg(res < 0, "Failed to handle missing group R_OK access");
4887 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s (%d)", EACCES,
4888 strerror(errno), errno);
4889
4890 mark_point();
4891 res = pr_fs_have_access(&st, W_OK, uid, 0, suppl_gids);
4892 ck_assert_msg(res < 0, "Failed to handle missing group W_OK access");
4893 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s (%d)", EACCES,
4894 strerror(errno), errno);
4895
4896 mark_point();
4897 res = pr_fs_have_access(&st, X_OK, uid, 0, suppl_gids);
4898 ck_assert_msg(res < 0, "Failed to handle missing group X_OK access");
4899 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s (%d)", EACCES,
4900 strerror(errno), errno);
4901
4902 st.st_mode = S_IFMT|S_IRGRP|S_IWGRP|S_IXGRP;
4903
4904 mark_point();
4905 res = pr_fs_have_access(&st, R_OK, uid, 0, suppl_gids);
4906 ck_assert_msg(res == 0, "Failed to handle group R_OK access: %s",
4907 strerror(errno));
4908
4909 mark_point();
4910 res = pr_fs_have_access(&st, W_OK, uid, 0, suppl_gids);
4911 ck_assert_msg(res == 0, "Failed to handle group W_OK access: %s",
4912 strerror(errno));
4913
4914 mark_point();
4915 res = pr_fs_have_access(&st, X_OK, uid, 0, suppl_gids);
4916 ck_assert_msg(res == 0, "Failed to handle group X_OK access: %s",
4917 strerror(errno));
4918 }
4919 END_TEST
4920
4921 START_TEST (fs_is_nfs_test) {
4922 int res;
4923
4924 res = pr_fs_is_nfs(NULL);
4925 ck_assert_msg(res < 0, "Failed to handle null argument");
4926 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4927 strerror(errno), errno);
4928
4929 res = pr_fs_is_nfs("/tmp");
4930 ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
4931 }
4932 END_TEST
4933
4934 START_TEST (fs_valid_path_test) {
4935 int res;
4936 const char *path;
4937 pr_fs_t *fs;
4938
4939 res = pr_fs_valid_path(NULL);
4940 ck_assert_msg(res < 0, "Failed to handle null arguments");
4941 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4942 strerror(errno), errno);
4943
4944 path = "/";
4945 res = pr_fs_valid_path(path);
4946 ck_assert_msg(res == 0, "'%s' is not a valid path: %s", path, strerror(errno));
4947
4948 path = ":tmp";
4949 res = pr_fs_valid_path(path);
4950 ck_assert_msg(res < 0, "Failed to handle invalid path");
4951 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
4952 strerror(errno), errno);
4953
4954 fs = pr_register_fs(p, "testsuite", "&");
4955 ck_assert_msg(fs != NULL, "Failed to register FS: %s", strerror(errno));
4956
4957 fs = pr_register_fs(p, "testsuite2", ":");
4958 ck_assert_msg(fs != NULL, "Failed to register FS: %s", strerror(errno));
4959
4960 res = pr_fs_valid_path(path);
4961 ck_assert_msg(res == 0, "Failed to handle valid path: %s", strerror(errno));
4962
4963 (void) pr_remove_fs("/testsuite2");
4964 (void) pr_remove_fs("/testsuite");
4965 }
4966 END_TEST
4967
4968 START_TEST (fsio_smkdir_test) {
4969 int res;
4970 const char *path;
4971 mode_t mode = 0755;
4972 uid_t uid = getuid();
4973 gid_t gid = getgid();
4974
4975 res = pr_fsio_smkdir(NULL, NULL, mode, uid, gid);
4976 ck_assert_msg(res < 0, "Failed to handle null arguments");
4977 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4978 strerror(errno), errno);
4979
4980 res = pr_fsio_smkdir(p, NULL, mode, uid, gid);
4981 ck_assert_msg(res < 0, "Failed to handle null path");
4982 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4983 strerror(errno), errno);
4984
4985 path = fsio_testdir_path;
4986 res = pr_fsio_smkdir(p, path, mode, uid, gid);
4987 ck_assert_msg(res == 0, "Failed to securely create '%s': %s", fsio_testdir_path,
4988 strerror(errno));
4989 (void) pr_fsio_rmdir(fsio_testdir_path);
4990
4991 res = pr_fsio_set_use_mkdtemp(-1);
4992 ck_assert_msg(res < 0, "Failed to handle invalid setting");
4993 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
4994 strerror(errno), errno);
4995
4996 #ifdef HAVE_MKDTEMP
4997 res = pr_fsio_set_use_mkdtemp(FALSE);
4998 ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
4999
5000 res = pr_fsio_smkdir(p, path, mode, uid, gid);
5001 ck_assert_msg(res == 0, "Failed to securely create '%s': %s", fsio_testdir_path,
5002 strerror(errno));
5003 (void) pr_fsio_rmdir(fsio_testdir_path);
5004
5005 res = pr_fsio_set_use_mkdtemp(TRUE);
5006 ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
5007 #else
5008 res = pr_fsio_set_use_mkdtemp(TRUE);
5009 ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
5010
5011 res = pr_fsio_set_use_mkdtemp(FALSE);
5012 ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
5013 #endif /* HAVE_MKDTEMP */
5014
5015 (void) pr_fsio_rmdir(fsio_testdir_path);
5016 }
5017 END_TEST
5018
5019 START_TEST (fsio_getpipebuf_test) {
5020 char *res;
5021 int fd = -1;
5022 long bufsz = 0;
5023
5024 res = pr_fsio_getpipebuf(NULL, fd, NULL);
5025 ck_assert_msg(res == NULL, "Failed to handle null arguments");
5026 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
5027 strerror(errno), errno);
5028
5029 res = pr_fsio_getpipebuf(p, fd, NULL);
5030 ck_assert_msg(res == NULL, "Failed to handle bad file descriptor");
5031 ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
5032 strerror(errno), errno);
5033
5034 fd = 0;
5035 res = pr_fsio_getpipebuf(p, fd, NULL);
5036 ck_assert_msg(res != NULL, "Failed to get pipebuf for fd %d: %s", fd,
5037 strerror(errno));
5038
5039 res = pr_fsio_getpipebuf(p, fd, &bufsz);
5040 ck_assert_msg(res != NULL, "Failed to get pipebuf for fd %d: %s", fd,
5041 strerror(errno));
5042 ck_assert_msg(bufsz > 0, "Expected >0, got %ld", bufsz);
5043 }
5044 END_TEST
5045
5046 START_TEST (fsio_gets_test) {
5047 char buf[PR_TUNABLE_PATH_MAX], *res, *text;
5048 pr_fh_t *fh;
5049 int res2;
5050
5051 res = pr_fsio_gets(NULL, 0, NULL);
5052 ck_assert_msg(res == NULL, "Failed to handle null arguments");
5053 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
5054 strerror(errno), errno);
5055
5056 res = pr_fsio_gets(buf, 0, NULL);
5057 ck_assert_msg(res == NULL, "Failed to handle null file handle");
5058 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
5059 strerror(errno), errno);
5060
5061 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_RDWR);
5062 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", fsio_test_path,
5063 strerror(errno));
5064
5065 res = pr_fsio_gets(buf, 0, fh);
5066 ck_assert_msg(res == NULL, "Failed to handle zero buffer length");
5067 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
5068 strerror(errno), errno);
5069
5070 text = "Hello, World!\n";
5071 res2 = pr_fsio_puts(text, fh);
5072 ck_assert_msg(res2 >= 0, "Error writing to '%s': %s", fsio_test_path,
5073 strerror(errno));
5074 pr_fsio_fsync(fh);
5075 pr_fsio_lseek(fh, 0, SEEK_SET);
5076
5077 memset(buf, '\0', sizeof(buf));
5078 res = pr_fsio_gets(buf, sizeof(buf)-1, fh);
5079 ck_assert_msg(res != NULL, "Failed reading from '%s': %s", fsio_test_path,
5080 strerror(errno));
5081 ck_assert_msg(strcmp(res, text) == 0, "Expected '%s', got '%s'", text, res);
5082
5083 (void) pr_fsio_close(fh);
5084 (void) pr_fsio_unlink(fsio_test_path);
5085 }
5086 END_TEST
5087
5088 START_TEST (fsio_getline_test) {
5089 char buf[PR_TUNABLE_PATH_MAX], *res, *text;
5090 pr_fh_t *fh;
5091 unsigned int lineno = 0;
5092 int res2;
5093
5094 res = pr_fsio_getline(NULL, 0, NULL, NULL);
5095 ck_assert_msg(res == NULL, "Failed to handle null arguments");
5096 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
5097 strerror(errno), errno);
5098
5099 res = pr_fsio_getline(buf, 0, NULL, NULL);
5100 ck_assert_msg(res == NULL, "Failed to handle file handle");
5101 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
5102 strerror(errno), errno);
5103
5104 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_RDWR);
5105 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", fsio_test_path,
5106 strerror(errno));
5107
5108 res = pr_fsio_getline(buf, 0, fh, NULL);
5109 ck_assert_msg(res == NULL, "Failed to handle zero buffer length");
5110 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
5111 strerror(errno), errno);
5112
5113 res = pr_fsio_getline(buf, sizeof(buf)-1, fh, &lineno);
5114 ck_assert_msg(res == NULL, "Failed to read empty '%s' file", fsio_test_path);
5115
5116 text = "Hello, World!\n";
5117 res2 = pr_fsio_puts(text, fh);
5118 ck_assert_msg(res2 >= 0, "Error writing to '%s': %s", fsio_test_path,
5119 strerror(errno));
5120
5121 text = "How\\\n are you?\n";
5122 res2 = pr_fsio_puts(text, fh);
5123 ck_assert_msg(res2 >= 0, "Error writing to '%s': %s", fsio_test_path,
5124 strerror(errno));
5125
5126 pr_fsio_fsync(fh);
5127 pr_fsio_lseek(fh, 0, SEEK_SET);
5128
5129 memset(buf, '\0', sizeof(buf));
5130 res = pr_fsio_getline(buf, sizeof(buf)-1, fh, &lineno);
5131 ck_assert_msg(res != NULL, "Failed to read line from '%s': %s", fsio_test_path,
5132 strerror(errno));
5133 ck_assert_msg(strcmp(res, "Hello, World!\n") == 0,
5134 "Expected 'Hello, World!\n', got '%s'", res);
5135 ck_assert_msg(lineno == 1, "Expected 1, got %u", lineno);
5136
5137 memset(buf, '\0', sizeof(buf));
5138 res = pr_fsio_getline(buf, sizeof(buf)-1, fh, &lineno);
5139 ck_assert_msg(res != NULL, "Failed to read line from '%s': %s", fsio_test_path,
5140 strerror(errno));
5141 ck_assert_msg(strcmp(res, "How are you?\n") == 0,
5142 "Expected 'How are you?\n', got '%s'", res);
5143 ck_assert_msg(lineno == 3, "Expected 3, got %u", lineno);
5144
5145 (void) pr_fsio_close(fh);
5146 (void) pr_fsio_unlink(fsio_test_path);
5147 }
5148 END_TEST
5149
5150 START_TEST (fsio_puts_test) {
5151 int res;
5152 const char *text;
5153 pr_fh_t *fh;
5154
5155 res = pr_fsio_puts(NULL, NULL);
5156 ck_assert_msg(res < 0, "Failed to handle null arguments");
5157 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
5158 strerror(errno), errno);
5159
5160 text = "Hello, World!\n";
5161 res = pr_fsio_puts(text, NULL);
5162 ck_assert_msg(res < 0, "Failed to handle null file handle");
5163 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
5164 strerror(errno), errno);
5165
5166 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_WRONLY);
5167 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", fsio_test_path,
5168 strerror(errno));
5169
5170 res = pr_fsio_puts(NULL, fh);
5171 ck_assert_msg(res < 0, "Failed to handle null buffer");
5172 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
5173 strerror(errno), errno);
5174
5175 (void) pr_fsio_close(fh);
5176 (void) pr_fsio_unlink(fsio_test_path);
5177 }
5178 END_TEST
5179
5180 START_TEST (fsio_blocking_test) {
5181 int fd, res;
5182 pr_fh_t *fh;
5183
5184 res = pr_fsio_set_block(NULL);
5185 ck_assert_msg(res < 0, "Failed to handle null argument");
5186 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
5187 strerror(errno), errno);
5188
5189 fh = pr_fsio_open(fsio_test_path, O_CREAT|O_EXCL|O_WRONLY);
5190 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", fsio_test_path,
5191 strerror(errno));
5192
5193 fd = fh->fh_fd;
5194 fh->fh_fd = -1;
5195
5196 res = pr_fsio_set_block(fh);
5197 ck_assert_msg(res < 0, "Failed to handle bad file descriptor");
5198 ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
5199 strerror(errno), errno);
5200
5201 fh->fh_fd = fd;
5202 res = pr_fsio_set_block(fh);
5203 ck_assert_msg(res == 0, "Failed to make '%s' blocking: %s", fsio_test_path,
5204 strerror(errno));
5205
5206 (void) pr_fsio_close(fh);
5207 (void) pr_fsio_unlink(fsio_test_path);
5208 }
5209 END_TEST
5210
5211 Suite *tests_get_fsio_suite(void) {
5212 Suite *suite;
5213 TCase *testcase;
5214
5215 suite = suite_create("fsio");
5216
5217 testcase = tcase_create("base");
5218 tcase_add_checked_fixture(testcase, set_up, tear_down);
5219
5220 /* Main FSIO API tests */
5221 tcase_add_test(testcase, fsio_sys_open_test);
5222 tcase_add_test(testcase, fsio_sys_open_canon_test);
5223 tcase_add_test(testcase, fsio_sys_open_chroot_guard_test);
5224 tcase_add_test(testcase, fsio_sys_close_test);
5225 tcase_add_test(testcase, fsio_sys_unlink_test);
5226 tcase_add_test(testcase, fsio_sys_unlink_chroot_guard_test);
5227 tcase_add_test(testcase, fsio_sys_stat_test);
5228 tcase_add_test(testcase, fsio_sys_fstat_test);
5229 tcase_add_test(testcase, fsio_sys_read_test);
5230 tcase_add_test(testcase, fsio_sys_pread_test);
5231 tcase_add_test(testcase, fsio_sys_write_test);
5232 tcase_add_test(testcase, fsio_sys_pwrite_test);
5233 tcase_add_test(testcase, fsio_sys_lseek_test);
5234 tcase_add_test(testcase, fsio_sys_link_test);
5235 tcase_add_test(testcase, fsio_sys_link_chroot_guard_test);
5236 tcase_add_test(testcase, fsio_sys_symlink_test);
5237 tcase_add_test(testcase, fsio_sys_symlink_chroot_guard_test);
5238 tcase_add_test(testcase, fsio_sys_readlink_test);
5239 tcase_add_test(testcase, fsio_sys_lstat_test);
5240 tcase_add_test(testcase, fsio_sys_access_dir_test);
5241 tcase_add_test(testcase, fsio_sys_access_file_test);
5242 tcase_add_test(testcase, fsio_sys_faccess_test);
5243 tcase_add_test(testcase, fsio_sys_truncate_test);
5244 tcase_add_test(testcase, fsio_sys_truncate_chroot_guard_test);
5245 tcase_add_test(testcase, fsio_sys_ftruncate_test);
5246 tcase_add_test(testcase, fsio_sys_chmod_test);
5247 tcase_add_test(testcase, fsio_sys_chmod_chroot_guard_test);
5248 tcase_add_test(testcase, fsio_sys_fchmod_test);
5249 tcase_add_test(testcase, fsio_sys_chown_test);
5250 tcase_add_test(testcase, fsio_sys_chown_chroot_guard_test);
5251 tcase_add_test(testcase, fsio_sys_fchown_test);
5252 tcase_add_test(testcase, fsio_sys_lchown_test);
5253 tcase_add_test(testcase, fsio_sys_lchown_chroot_guard_test);
5254 tcase_add_test(testcase, fsio_sys_rename_test);
5255 tcase_add_test(testcase, fsio_sys_rename_chroot_guard_test);
5256 tcase_add_test(testcase, fsio_sys_utimes_test);
5257 tcase_add_test(testcase, fsio_sys_utimes_chroot_guard_test);
5258 tcase_add_test(testcase, fsio_sys_futimes_test);
5259 tcase_add_test(testcase, fsio_sys_fsync_test);
5260
5261 tcase_add_test(testcase, fsio_sys_realpath_test);
5262
5263 /* Extended attribute tests */
5264 tcase_add_test(testcase, fsio_sys_getxattr_test);
5265 tcase_add_test(testcase, fsio_sys_lgetxattr_test);
5266 tcase_add_test(testcase, fsio_sys_fgetxattr_test);
5267 tcase_add_test(testcase, fsio_sys_listxattr_test);
5268 tcase_add_test(testcase, fsio_sys_llistxattr_test);
5269 tcase_add_test(testcase, fsio_sys_flistxattr_test);
5270 tcase_add_test(testcase, fsio_sys_removexattr_test);
5271 tcase_add_test(testcase, fsio_sys_lremovexattr_test);
5272 tcase_add_test(testcase, fsio_sys_fremovexattr_test);
5273 tcase_add_test(testcase, fsio_sys_setxattr_test);
5274 tcase_add_test(testcase, fsio_sys_lsetxattr_test);
5275 tcase_add_test(testcase, fsio_sys_fsetxattr_test);
5276
5277 tcase_add_test(testcase, fsio_sys_mkdir_test);
5278 tcase_add_test(testcase, fsio_sys_mkdir_chroot_guard_test);
5279 tcase_add_test(testcase, fsio_sys_rmdir_test);
5280 tcase_add_test(testcase, fsio_sys_rmdir_chroot_guard_test);
5281 tcase_add_test(testcase, fsio_sys_chdir_test);
5282 tcase_add_test(testcase, fsio_sys_chdir_canon_test);
5283 tcase_add_test(testcase, fsio_sys_chroot_test);
5284 tcase_add_test(testcase, fsio_sys_opendir_test);
5285 tcase_add_test(testcase, fsio_sys_readdir_test);
5286 tcase_add_test(testcase, fsio_sys_closedir_test);
5287
5288 /* FSIO with error tests */
5289 tcase_add_test(testcase, fsio_sys_chmod_with_error_test);
5290 tcase_add_test(testcase, fsio_sys_chown_with_error_test);
5291 tcase_add_test(testcase, fsio_sys_chroot_with_error_test);
5292 tcase_add_test(testcase, fsio_sys_close_with_error_test);
5293 tcase_add_test(testcase, fsio_sys_fchmod_with_error_test);
5294 tcase_add_test(testcase, fsio_sys_fchown_with_error_test);
5295 tcase_add_test(testcase, fsio_sys_lchown_with_error_test);
5296 tcase_add_test(testcase, fsio_sys_lstat_with_error_test);
5297 tcase_add_test(testcase, fsio_sys_mkdir_with_error_test);
5298 tcase_add_test(testcase, fsio_sys_open_with_error_test);
5299 tcase_add_test(testcase, fsio_sys_read_with_error_test);
5300 tcase_add_test(testcase, fsio_sys_rename_with_error_test);
5301 tcase_add_test(testcase, fsio_sys_rmdir_with_error_test);
5302 tcase_add_test(testcase, fsio_sys_stat_with_error_test);
5303 tcase_add_test(testcase, fsio_sys_unlink_with_error_test);
5304 tcase_add_test(testcase, fsio_sys_write_with_error_test);
5305
5306 /* FSIO statcache tests */
5307 tcase_add_test(testcase, fsio_statcache_clear_cache_test);
5308 tcase_add_test(testcase, fsio_statcache_cache_hit_test);
5309 tcase_add_test(testcase, fsio_statcache_negative_cache_test);
5310 tcase_add_test(testcase, fsio_statcache_expired_test);
5311 tcase_add_test(testcase, fsio_statcache_dump_test);
5312
5313 /* Custom FSIO management tests */
5314 tcase_add_test(testcase, fs_create_fs_test);
5315 tcase_add_test(testcase, fs_insert_fs_test);
5316 tcase_add_test(testcase, fs_get_fs_test);
5317 tcase_add_test(testcase, fs_unmount_fs_test);
5318 tcase_add_test(testcase, fs_remove_fs_test);
5319 tcase_add_test(testcase, fs_register_fs_test);
5320 tcase_add_test(testcase, fs_register_fs2_test);
5321 tcase_add_test(testcase, fs_unregister_fs_test);
5322 tcase_add_test(testcase, fs_resolve_fs_map_test);
5323 #if defined(PR_USE_DEVEL)
5324 tcase_add_test(testcase, fs_dump_fs_test);
5325 #endif /* PR_USE_DEVEL */
5326
5327 /* Custom FSIO tests */
5328 tcase_add_test(testcase, fsio_custom_chroot_test);
5329
5330 /* Misc */
5331 tcase_add_test(testcase, fs_clean_path_test);
5332 tcase_add_test(testcase, fs_clean_path2_test);
5333
5334 tcase_add_test(testcase, fs_dircat_test);
5335 tcase_add_test(testcase, fs_setcwd_test);
5336 tcase_add_test(testcase, fs_glob_test);
5337 tcase_add_test(testcase, fs_copy_file_test);
5338 tcase_add_test(testcase, fs_copy_file2_test);
5339 tcase_add_test(testcase, fs_interpolate_test);
5340 tcase_add_test(testcase, fs_resolve_partial_test);
5341 tcase_add_test(testcase, fs_resolve_path_test);
5342 tcase_add_test(testcase, fs_use_encoding_test);
5343 tcase_add_test(testcase, fs_decode_path2_test);
5344 tcase_add_test(testcase, fs_encode_path_test);
5345 tcase_add_test(testcase, fs_split_path_test);
5346 tcase_add_test(testcase, fs_join_path_test);
5347 tcase_add_test(testcase, fs_virtual_path_test);
5348 #if 0
5349 tcase_add_test(testcase, fs_close_extra_fds_test);
5350 #endif
5351 tcase_add_test(testcase, fs_get_usable_fd_test);
5352 tcase_add_test(testcase, fs_get_usable_fd2_test);
5353 tcase_add_test(testcase, fs_getsize_test);
5354 tcase_add_test(testcase, fs_getsize2_test);
5355 tcase_add_test(testcase, fs_fgetsize_test);
5356 tcase_add_test(testcase, fs_fadvise_test);
5357 tcase_add_test(testcase, fs_have_access_test);
5358 #if defined(HAVE_STATFS_F_TYPE) || defined(HAVE_STATFS_F_FSTYPENAME)
5359 tcase_add_test(testcase, fs_is_nfs_test);
5360 #endif
5361 tcase_add_test(testcase, fs_valid_path_test);
5362 tcase_add_test(testcase, fsio_smkdir_test);
5363 tcase_add_test(testcase, fsio_getpipebuf_test);
5364 tcase_add_test(testcase, fsio_gets_test);
5365 tcase_add_test(testcase, fsio_getline_test);
5366 tcase_add_test(testcase, fsio_puts_test);
5367 tcase_add_test(testcase, fsio_blocking_test);
5368
5369 suite_add_tcase(suite, testcase);
5370 return suite;
5371 }