fs_test.c revision 1.1 1 1.1 jmmv /*
2 1.1 jmmv * Automated Testing Framework (atf)
3 1.1 jmmv *
4 1.1 jmmv * Copyright (c) 2007, 2008, 2009, 2010 The NetBSD Foundation, Inc.
5 1.1 jmmv * All rights reserved.
6 1.1 jmmv *
7 1.1 jmmv * Redistribution and use in source and binary forms, with or without
8 1.1 jmmv * modification, are permitted provided that the following conditions
9 1.1 jmmv * are met:
10 1.1 jmmv * 1. Redistributions of source code must retain the above copyright
11 1.1 jmmv * notice, this list of conditions and the following disclaimer.
12 1.1 jmmv * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmmv * notice, this list of conditions and the following disclaimer in the
14 1.1 jmmv * documentation and/or other materials provided with the distribution.
15 1.1 jmmv *
16 1.1 jmmv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 1.1 jmmv * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 1.1 jmmv * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 1.1 jmmv * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.1 jmmv * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 1.1 jmmv * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 jmmv * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 1.1 jmmv * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1 jmmv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 1.1 jmmv * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 1.1 jmmv * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 1.1 jmmv * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 jmmv */
29 1.1 jmmv
30 1.1 jmmv #include <sys/types.h>
31 1.1 jmmv #include <sys/stat.h>
32 1.1 jmmv
33 1.1 jmmv #include <errno.h>
34 1.1 jmmv #include <fcntl.h>
35 1.1 jmmv #include <stdio.h>
36 1.1 jmmv #include <stdlib.h>
37 1.1 jmmv #include <string.h>
38 1.1 jmmv #include <unistd.h>
39 1.1 jmmv
40 1.1 jmmv #include <atf-c.h>
41 1.1 jmmv
42 1.1 jmmv #include "fs.h"
43 1.1 jmmv #include "test_helpers.h"
44 1.1 jmmv #include "user.h"
45 1.1 jmmv
46 1.1 jmmv /* ---------------------------------------------------------------------
47 1.1 jmmv * Auxiliary functions.
48 1.1 jmmv * --------------------------------------------------------------------- */
49 1.1 jmmv
50 1.1 jmmv static
51 1.1 jmmv void
52 1.1 jmmv create_dir(const char *p, int mode)
53 1.1 jmmv {
54 1.1 jmmv int ret;
55 1.1 jmmv
56 1.1 jmmv ret = mkdir(p, mode);
57 1.1 jmmv if (ret == -1)
58 1.1 jmmv atf_tc_fail("Could not create helper directory %s", p);
59 1.1 jmmv }
60 1.1 jmmv
61 1.1 jmmv static
62 1.1 jmmv void
63 1.1 jmmv create_file(const char *p, int mode)
64 1.1 jmmv {
65 1.1 jmmv int fd;
66 1.1 jmmv
67 1.1 jmmv fd = open(p, O_CREAT | O_WRONLY | O_TRUNC, mode);
68 1.1 jmmv if (fd == -1)
69 1.1 jmmv atf_tc_fail("Could not create helper file %s", p);
70 1.1 jmmv close(fd);
71 1.1 jmmv }
72 1.1 jmmv
73 1.1 jmmv static
74 1.1 jmmv bool
75 1.1 jmmv exists(const atf_fs_path_t *p)
76 1.1 jmmv {
77 1.1 jmmv return access(atf_fs_path_cstring(p), F_OK) == 0;
78 1.1 jmmv }
79 1.1 jmmv
80 1.1 jmmv static
81 1.1 jmmv atf_error_t
82 1.1 jmmv mkstemp_discard_fd(atf_fs_path_t *p)
83 1.1 jmmv {
84 1.1 jmmv int fd;
85 1.1 jmmv atf_error_t err = atf_fs_mkstemp(p, &fd);
86 1.1 jmmv if (!atf_is_error(err))
87 1.1 jmmv close(fd);
88 1.1 jmmv return err;
89 1.1 jmmv }
90 1.1 jmmv
91 1.1 jmmv /* ---------------------------------------------------------------------
92 1.1 jmmv * Test cases for the "atf_fs_path" type.
93 1.1 jmmv * --------------------------------------------------------------------- */
94 1.1 jmmv
95 1.1 jmmv ATF_TC(path_normalize);
96 1.1 jmmv ATF_TC_HEAD(path_normalize, tc)
97 1.1 jmmv {
98 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the path's normalization");
99 1.1 jmmv }
100 1.1 jmmv ATF_TC_BODY(path_normalize, tc)
101 1.1 jmmv {
102 1.1 jmmv struct test {
103 1.1 jmmv const char *in;
104 1.1 jmmv const char *out;
105 1.1 jmmv } tests[] = {
106 1.1 jmmv { ".", ".", },
107 1.1 jmmv { "..", "..", },
108 1.1 jmmv
109 1.1 jmmv { "/", "/", },
110 1.1 jmmv { "//", "/", }, /* NO_CHECK_STYLE */
111 1.1 jmmv { "///", "/", }, /* NO_CHECK_STYLE */
112 1.1 jmmv
113 1.1 jmmv { "foo", "foo", },
114 1.1 jmmv { "foo/", "foo", },
115 1.1 jmmv { "foo/bar", "foo/bar", },
116 1.1 jmmv { "foo/bar/", "foo/bar", },
117 1.1 jmmv
118 1.1 jmmv { "/foo", "/foo", },
119 1.1 jmmv { "/foo/bar", "/foo/bar", },
120 1.1 jmmv { "/foo/bar/", "/foo/bar", },
121 1.1 jmmv
122 1.1 jmmv { "///foo", "/foo", }, /* NO_CHECK_STYLE */
123 1.1 jmmv { "///foo///bar", "/foo/bar", }, /* NO_CHECK_STYLE */
124 1.1 jmmv { "///foo///bar///", "/foo/bar", }, /* NO_CHECK_STYLE */
125 1.1 jmmv
126 1.1 jmmv { NULL, NULL }
127 1.1 jmmv };
128 1.1 jmmv struct test *t;
129 1.1 jmmv
130 1.1 jmmv for (t = &tests[0]; t->in != NULL; t++) {
131 1.1 jmmv atf_fs_path_t p;
132 1.1 jmmv
133 1.1 jmmv printf("Input : >%s<\n", t->in);
134 1.1 jmmv printf("Expected output: >%s<\n", t->out);
135 1.1 jmmv
136 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "%s", t->in));
137 1.1 jmmv printf("Output : >%s<\n", atf_fs_path_cstring(&p));
138 1.1 jmmv ATF_REQUIRE(strcmp(atf_fs_path_cstring(&p), t->out) == 0);
139 1.1 jmmv atf_fs_path_fini(&p);
140 1.1 jmmv
141 1.1 jmmv printf("\n");
142 1.1 jmmv }
143 1.1 jmmv }
144 1.1 jmmv
145 1.1 jmmv ATF_TC(path_copy);
146 1.1 jmmv ATF_TC_HEAD(path_copy, tc)
147 1.1 jmmv {
148 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_path_copy constructor");
149 1.1 jmmv }
150 1.1 jmmv ATF_TC_BODY(path_copy, tc)
151 1.1 jmmv {
152 1.1 jmmv atf_fs_path_t str, str2;
153 1.1 jmmv
154 1.1 jmmv RE(atf_fs_path_init_fmt(&str, "foo"));
155 1.1 jmmv RE(atf_fs_path_copy(&str2, &str));
156 1.1 jmmv
157 1.1 jmmv ATF_REQUIRE(atf_equal_fs_path_fs_path(&str, &str2));
158 1.1 jmmv
159 1.1 jmmv RE(atf_fs_path_append_fmt(&str2, "bar"));
160 1.1 jmmv
161 1.1 jmmv ATF_REQUIRE(!atf_equal_fs_path_fs_path(&str, &str2));
162 1.1 jmmv
163 1.1 jmmv atf_fs_path_fini(&str2);
164 1.1 jmmv atf_fs_path_fini(&str);
165 1.1 jmmv }
166 1.1 jmmv
167 1.1 jmmv ATF_TC(path_is_absolute);
168 1.1 jmmv ATF_TC_HEAD(path_is_absolute, tc)
169 1.1 jmmv {
170 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the path::is_absolute function");
171 1.1 jmmv }
172 1.1 jmmv ATF_TC_BODY(path_is_absolute, tc)
173 1.1 jmmv {
174 1.1 jmmv struct test {
175 1.1 jmmv const char *in;
176 1.1 jmmv bool abs;
177 1.1 jmmv } tests[] = {
178 1.1 jmmv { "/", true },
179 1.1 jmmv { "////", true }, /* NO_CHECK_STYLE */
180 1.1 jmmv { "////a", true }, /* NO_CHECK_STYLE */
181 1.1 jmmv { "//a//", true }, /* NO_CHECK_STYLE */
182 1.1 jmmv { "a////", false }, /* NO_CHECK_STYLE */
183 1.1 jmmv { "../foo", false },
184 1.1 jmmv { NULL, false },
185 1.1 jmmv };
186 1.1 jmmv struct test *t;
187 1.1 jmmv
188 1.1 jmmv for (t = &tests[0]; t->in != NULL; t++) {
189 1.1 jmmv atf_fs_path_t p;
190 1.1 jmmv
191 1.1 jmmv printf("Input : %s\n", t->in);
192 1.1 jmmv printf("Expected result: %s\n", t->abs ? "true" : "false");
193 1.1 jmmv
194 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "%s", t->in));
195 1.1 jmmv printf("Result : %s\n",
196 1.1 jmmv atf_fs_path_is_absolute(&p) ? "true" : "false");
197 1.1 jmmv if (t->abs)
198 1.1 jmmv ATF_REQUIRE(atf_fs_path_is_absolute(&p));
199 1.1 jmmv else
200 1.1 jmmv ATF_REQUIRE(!atf_fs_path_is_absolute(&p));
201 1.1 jmmv atf_fs_path_fini(&p);
202 1.1 jmmv
203 1.1 jmmv printf("\n");
204 1.1 jmmv }
205 1.1 jmmv }
206 1.1 jmmv
207 1.1 jmmv ATF_TC(path_is_root);
208 1.1 jmmv ATF_TC_HEAD(path_is_root, tc)
209 1.1 jmmv {
210 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the path::is_root function");
211 1.1 jmmv }
212 1.1 jmmv ATF_TC_BODY(path_is_root, tc)
213 1.1 jmmv {
214 1.1 jmmv struct test {
215 1.1 jmmv const char *in;
216 1.1 jmmv bool root;
217 1.1 jmmv } tests[] = {
218 1.1 jmmv { "/", true },
219 1.1 jmmv { "////", true }, /* NO_CHECK_STYLE */
220 1.1 jmmv { "////a", false }, /* NO_CHECK_STYLE */
221 1.1 jmmv { "//a//", false }, /* NO_CHECK_STYLE */
222 1.1 jmmv { "a////", false }, /* NO_CHECK_STYLE */
223 1.1 jmmv { "../foo", false },
224 1.1 jmmv { NULL, false },
225 1.1 jmmv };
226 1.1 jmmv struct test *t;
227 1.1 jmmv
228 1.1 jmmv for (t = &tests[0]; t->in != NULL; t++) {
229 1.1 jmmv atf_fs_path_t p;
230 1.1 jmmv
231 1.1 jmmv printf("Input : %s\n", t->in);
232 1.1 jmmv printf("Expected result: %s\n", t->root ? "true" : "false");
233 1.1 jmmv
234 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "%s", t->in));
235 1.1 jmmv printf("Result : %s\n",
236 1.1 jmmv atf_fs_path_is_root(&p) ? "true" : "false");
237 1.1 jmmv if (t->root)
238 1.1 jmmv ATF_REQUIRE(atf_fs_path_is_root(&p));
239 1.1 jmmv else
240 1.1 jmmv ATF_REQUIRE(!atf_fs_path_is_root(&p));
241 1.1 jmmv atf_fs_path_fini(&p);
242 1.1 jmmv
243 1.1 jmmv printf("\n");
244 1.1 jmmv }
245 1.1 jmmv }
246 1.1 jmmv
247 1.1 jmmv ATF_TC(path_branch_path);
248 1.1 jmmv ATF_TC_HEAD(path_branch_path, tc)
249 1.1 jmmv {
250 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_path_branch_path "
251 1.1 jmmv "function");
252 1.1 jmmv }
253 1.1 jmmv ATF_TC_BODY(path_branch_path, tc)
254 1.1 jmmv {
255 1.1 jmmv struct test {
256 1.1 jmmv const char *in;
257 1.1 jmmv const char *branch;
258 1.1 jmmv } tests[] = {
259 1.1 jmmv { ".", "." },
260 1.1 jmmv { "foo", "." },
261 1.1 jmmv { "foo/bar", "foo" },
262 1.1 jmmv { "/foo", "/" },
263 1.1 jmmv { "/foo/bar", "/foo" },
264 1.1 jmmv { NULL, NULL },
265 1.1 jmmv };
266 1.1 jmmv struct test *t;
267 1.1 jmmv
268 1.1 jmmv for (t = &tests[0]; t->in != NULL; t++) {
269 1.1 jmmv atf_fs_path_t p, bp;
270 1.1 jmmv
271 1.1 jmmv printf("Input : %s\n", t->in);
272 1.1 jmmv printf("Expected output: %s\n", t->branch);
273 1.1 jmmv
274 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "%s", t->in));
275 1.1 jmmv RE(atf_fs_path_branch_path(&p, &bp));
276 1.1 jmmv printf("Output : %s\n", atf_fs_path_cstring(&bp));
277 1.1 jmmv ATF_REQUIRE(strcmp(atf_fs_path_cstring(&bp), t->branch) == 0);
278 1.1 jmmv atf_fs_path_fini(&bp);
279 1.1 jmmv atf_fs_path_fini(&p);
280 1.1 jmmv
281 1.1 jmmv printf("\n");
282 1.1 jmmv }
283 1.1 jmmv }
284 1.1 jmmv
285 1.1 jmmv ATF_TC(path_leaf_name);
286 1.1 jmmv ATF_TC_HEAD(path_leaf_name, tc)
287 1.1 jmmv {
288 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_path_leaf_name "
289 1.1 jmmv "function");
290 1.1 jmmv }
291 1.1 jmmv ATF_TC_BODY(path_leaf_name, tc)
292 1.1 jmmv {
293 1.1 jmmv struct test {
294 1.1 jmmv const char *in;
295 1.1 jmmv const char *leaf;
296 1.1 jmmv } tests[] = {
297 1.1 jmmv { ".", "." },
298 1.1 jmmv { "foo", "foo" },
299 1.1 jmmv { "foo/bar", "bar" },
300 1.1 jmmv { "/foo", "foo" },
301 1.1 jmmv { "/foo/bar", "bar" },
302 1.1 jmmv { NULL, NULL },
303 1.1 jmmv };
304 1.1 jmmv struct test *t;
305 1.1 jmmv
306 1.1 jmmv for (t = &tests[0]; t->in != NULL; t++) {
307 1.1 jmmv atf_fs_path_t p;
308 1.1 jmmv atf_dynstr_t ln;
309 1.1 jmmv
310 1.1 jmmv printf("Input : %s\n", t->in);
311 1.1 jmmv printf("Expected output: %s\n", t->leaf);
312 1.1 jmmv
313 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "%s", t->in));
314 1.1 jmmv RE(atf_fs_path_leaf_name(&p, &ln));
315 1.1 jmmv printf("Output : %s\n", atf_dynstr_cstring(&ln));
316 1.1 jmmv ATF_REQUIRE(atf_equal_dynstr_cstring(&ln, t->leaf));
317 1.1 jmmv atf_dynstr_fini(&ln);
318 1.1 jmmv atf_fs_path_fini(&p);
319 1.1 jmmv
320 1.1 jmmv printf("\n");
321 1.1 jmmv }
322 1.1 jmmv }
323 1.1 jmmv
324 1.1 jmmv ATF_TC(path_append);
325 1.1 jmmv ATF_TC_HEAD(path_append, tc)
326 1.1 jmmv {
327 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the concatenation of multiple "
328 1.1 jmmv "paths");
329 1.1 jmmv }
330 1.1 jmmv ATF_TC_BODY(path_append, tc)
331 1.1 jmmv {
332 1.1 jmmv struct test {
333 1.1 jmmv const char *in;
334 1.1 jmmv const char *ap;
335 1.1 jmmv const char *out;
336 1.1 jmmv } tests[] = {
337 1.1 jmmv { "foo", "bar", "foo/bar" },
338 1.1 jmmv { "foo/", "/bar", "foo/bar" },
339 1.1 jmmv { "foo/", "/bar/baz", "foo/bar/baz" },
340 1.1 jmmv { "foo/", "///bar///baz", "foo/bar/baz" }, /* NO_CHECK_STYLE */
341 1.1 jmmv
342 1.1 jmmv { NULL, NULL, NULL }
343 1.1 jmmv };
344 1.1 jmmv struct test *t;
345 1.1 jmmv
346 1.1 jmmv for (t = &tests[0]; t->in != NULL; t++) {
347 1.1 jmmv atf_fs_path_t p;
348 1.1 jmmv
349 1.1 jmmv printf("Input : >%s<\n", t->in);
350 1.1 jmmv printf("Append : >%s<\n", t->ap);
351 1.1 jmmv printf("Expected output: >%s<\n", t->out);
352 1.1 jmmv
353 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "%s", t->in));
354 1.1 jmmv
355 1.1 jmmv RE(atf_fs_path_append_fmt(&p, "%s", t->ap));
356 1.1 jmmv
357 1.1 jmmv printf("Output : >%s<\n", atf_fs_path_cstring(&p));
358 1.1 jmmv ATF_REQUIRE(strcmp(atf_fs_path_cstring(&p), t->out) == 0);
359 1.1 jmmv
360 1.1 jmmv atf_fs_path_fini(&p);
361 1.1 jmmv
362 1.1 jmmv printf("\n");
363 1.1 jmmv }
364 1.1 jmmv }
365 1.1 jmmv
366 1.1 jmmv ATF_TC(path_to_absolute);
367 1.1 jmmv ATF_TC_HEAD(path_to_absolute, tc)
368 1.1 jmmv {
369 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_path_to_absolute "
370 1.1 jmmv "function");
371 1.1 jmmv atf_tc_set_md_var(tc, "use.fs", "true");
372 1.1 jmmv }
373 1.1 jmmv ATF_TC_BODY(path_to_absolute, tc)
374 1.1 jmmv {
375 1.1 jmmv const char *names[] = { ".", "dir", NULL };
376 1.1 jmmv const char **n;
377 1.1 jmmv
378 1.1 jmmv ATF_REQUIRE(mkdir("dir", 0755) != -1);
379 1.1 jmmv
380 1.1 jmmv for (n = names; *n != NULL; n++) {
381 1.1 jmmv atf_fs_path_t p, p2;
382 1.1 jmmv atf_fs_stat_t st1, st2;
383 1.1 jmmv
384 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "%s", *n));
385 1.1 jmmv RE(atf_fs_stat_init(&st1, &p));
386 1.1 jmmv printf("Relative path: %s\n", atf_fs_path_cstring(&p));
387 1.1 jmmv
388 1.1 jmmv RE(atf_fs_path_to_absolute(&p, &p2));
389 1.1 jmmv printf("Absolute path: %s\n", atf_fs_path_cstring(&p2));
390 1.1 jmmv
391 1.1 jmmv ATF_REQUIRE(atf_fs_path_is_absolute(&p2));
392 1.1 jmmv RE(atf_fs_stat_init(&st2, &p2));
393 1.1 jmmv
394 1.1 jmmv ATF_REQUIRE_EQ(atf_fs_stat_get_device(&st1),
395 1.1 jmmv atf_fs_stat_get_device(&st2));
396 1.1 jmmv ATF_REQUIRE_EQ(atf_fs_stat_get_inode(&st1),
397 1.1 jmmv atf_fs_stat_get_inode(&st2));
398 1.1 jmmv
399 1.1 jmmv atf_fs_stat_fini(&st2);
400 1.1 jmmv atf_fs_stat_fini(&st1);
401 1.1 jmmv atf_fs_path_fini(&p2);
402 1.1 jmmv atf_fs_path_fini(&p);
403 1.1 jmmv
404 1.1 jmmv printf("\n");
405 1.1 jmmv }
406 1.1 jmmv }
407 1.1 jmmv
408 1.1 jmmv ATF_TC(path_equal);
409 1.1 jmmv ATF_TC_HEAD(path_equal, tc)
410 1.1 jmmv {
411 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the equality operators for paths");
412 1.1 jmmv }
413 1.1 jmmv ATF_TC_BODY(path_equal, tc)
414 1.1 jmmv {
415 1.1 jmmv atf_fs_path_t p1, p2;
416 1.1 jmmv
417 1.1 jmmv RE(atf_fs_path_init_fmt(&p1, "foo"));
418 1.1 jmmv
419 1.1 jmmv RE(atf_fs_path_init_fmt(&p2, "foo"));
420 1.1 jmmv ATF_REQUIRE(atf_equal_fs_path_fs_path(&p1, &p2));
421 1.1 jmmv atf_fs_path_fini(&p2);
422 1.1 jmmv
423 1.1 jmmv RE(atf_fs_path_init_fmt(&p2, "bar"));
424 1.1 jmmv ATF_REQUIRE(!atf_equal_fs_path_fs_path(&p1, &p2));
425 1.1 jmmv atf_fs_path_fini(&p2);
426 1.1 jmmv
427 1.1 jmmv atf_fs_path_fini(&p1);
428 1.1 jmmv }
429 1.1 jmmv
430 1.1 jmmv /* ---------------------------------------------------------------------
431 1.1 jmmv * Test cases for the "atf_fs_stat" type.
432 1.1 jmmv * --------------------------------------------------------------------- */
433 1.1 jmmv
434 1.1 jmmv ATF_TC(stat_mode);
435 1.1 jmmv ATF_TC_HEAD(stat_mode, tc)
436 1.1 jmmv {
437 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_stat_get_mode function "
438 1.1 jmmv "and, indirectly, the constructor");
439 1.1 jmmv atf_tc_set_md_var(tc, "use.fs", "true");
440 1.1 jmmv }
441 1.1 jmmv ATF_TC_BODY(stat_mode, tc)
442 1.1 jmmv {
443 1.1 jmmv atf_fs_path_t p;
444 1.1 jmmv atf_fs_stat_t st;
445 1.1 jmmv
446 1.1 jmmv create_file("f1", 0400);
447 1.1 jmmv create_file("f2", 0644);
448 1.1 jmmv
449 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "f1"));
450 1.1 jmmv RE(atf_fs_stat_init(&st, &p));
451 1.1 jmmv ATF_CHECK_EQ(0400, atf_fs_stat_get_mode(&st));
452 1.1 jmmv atf_fs_stat_fini(&st);
453 1.1 jmmv atf_fs_path_fini(&p);
454 1.1 jmmv
455 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "f2"));
456 1.1 jmmv RE(atf_fs_stat_init(&st, &p));
457 1.1 jmmv ATF_CHECK_EQ(0644, atf_fs_stat_get_mode(&st));
458 1.1 jmmv atf_fs_stat_fini(&st);
459 1.1 jmmv atf_fs_path_fini(&p);
460 1.1 jmmv }
461 1.1 jmmv
462 1.1 jmmv ATF_TC(stat_type);
463 1.1 jmmv ATF_TC_HEAD(stat_type, tc)
464 1.1 jmmv {
465 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_stat_get_type function "
466 1.1 jmmv "and, indirectly, the constructor");
467 1.1 jmmv atf_tc_set_md_var(tc, "use.fs", "true");
468 1.1 jmmv }
469 1.1 jmmv ATF_TC_BODY(stat_type, tc)
470 1.1 jmmv {
471 1.1 jmmv atf_fs_path_t p;
472 1.1 jmmv atf_fs_stat_t st;
473 1.1 jmmv
474 1.1 jmmv create_dir("dir", 0755);
475 1.1 jmmv create_file("reg", 0644);
476 1.1 jmmv
477 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "dir"));
478 1.1 jmmv RE(atf_fs_stat_init(&st, &p));
479 1.1 jmmv ATF_REQUIRE_EQ(atf_fs_stat_get_type(&st), atf_fs_stat_dir_type);
480 1.1 jmmv atf_fs_stat_fini(&st);
481 1.1 jmmv atf_fs_path_fini(&p);
482 1.1 jmmv
483 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "reg"));
484 1.1 jmmv RE(atf_fs_stat_init(&st, &p));
485 1.1 jmmv ATF_REQUIRE_EQ(atf_fs_stat_get_type(&st), atf_fs_stat_reg_type);
486 1.1 jmmv atf_fs_stat_fini(&st);
487 1.1 jmmv atf_fs_path_fini(&p);
488 1.1 jmmv }
489 1.1 jmmv
490 1.1 jmmv ATF_TC(stat_perms);
491 1.1 jmmv ATF_TC_HEAD(stat_perms, tc)
492 1.1 jmmv {
493 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_stat_is_* functions");
494 1.1 jmmv atf_tc_set_md_var(tc, "use.fs", "true");
495 1.1 jmmv }
496 1.1 jmmv ATF_TC_BODY(stat_perms, tc)
497 1.1 jmmv {
498 1.1 jmmv atf_fs_path_t p;
499 1.1 jmmv atf_fs_stat_t st;
500 1.1 jmmv
501 1.1 jmmv create_file("reg", 0);
502 1.1 jmmv
503 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "reg"));
504 1.1 jmmv
505 1.1 jmmv #define perms(ur, uw, ux, gr, gw, gx, othr, othw, othx) \
506 1.1 jmmv { \
507 1.1 jmmv RE(atf_fs_stat_init(&st, &p)); \
508 1.1 jmmv ATF_REQUIRE(atf_fs_stat_is_owner_readable(&st) == ur); \
509 1.1 jmmv ATF_REQUIRE(atf_fs_stat_is_owner_writable(&st) == uw); \
510 1.1 jmmv ATF_REQUIRE(atf_fs_stat_is_owner_executable(&st) == ux); \
511 1.1 jmmv ATF_REQUIRE(atf_fs_stat_is_group_readable(&st) == gr); \
512 1.1 jmmv ATF_REQUIRE(atf_fs_stat_is_group_writable(&st) == gw); \
513 1.1 jmmv ATF_REQUIRE(atf_fs_stat_is_group_executable(&st) == gx); \
514 1.1 jmmv ATF_REQUIRE(atf_fs_stat_is_other_readable(&st) == othr); \
515 1.1 jmmv ATF_REQUIRE(atf_fs_stat_is_other_writable(&st) == othw); \
516 1.1 jmmv ATF_REQUIRE(atf_fs_stat_is_other_executable(&st) == othx); \
517 1.1 jmmv atf_fs_stat_fini(&st); \
518 1.1 jmmv }
519 1.1 jmmv
520 1.1 jmmv chmod("reg", 0000);
521 1.1 jmmv perms(false, false, false, false, false, false, false, false, false);
522 1.1 jmmv
523 1.1 jmmv chmod("reg", 0001);
524 1.1 jmmv perms(false, false, false, false, false, false, false, false, true);
525 1.1 jmmv
526 1.1 jmmv chmod("reg", 0010);
527 1.1 jmmv perms(false, false, false, false, false, true, false, false, false);
528 1.1 jmmv
529 1.1 jmmv chmod("reg", 0100);
530 1.1 jmmv perms(false, false, true, false, false, false, false, false, false);
531 1.1 jmmv
532 1.1 jmmv chmod("reg", 0002);
533 1.1 jmmv perms(false, false, false, false, false, false, false, true, false);
534 1.1 jmmv
535 1.1 jmmv chmod("reg", 0020);
536 1.1 jmmv perms(false, false, false, false, true, false, false, false, false);
537 1.1 jmmv
538 1.1 jmmv chmod("reg", 0200);
539 1.1 jmmv perms(false, true, false, false, false, false, false, false, false);
540 1.1 jmmv
541 1.1 jmmv chmod("reg", 0004);
542 1.1 jmmv perms(false, false, false, false, false, false, true, false, false);
543 1.1 jmmv
544 1.1 jmmv chmod("reg", 0040);
545 1.1 jmmv perms(false, false, false, true, false, false, false, false, false);
546 1.1 jmmv
547 1.1 jmmv chmod("reg", 0400);
548 1.1 jmmv perms(true, false, false, false, false, false, false, false, false);
549 1.1 jmmv
550 1.1 jmmv chmod("reg", 0644);
551 1.1 jmmv perms(true, true, false, true, false, false, true, false, false);
552 1.1 jmmv
553 1.1 jmmv chmod("reg", 0755);
554 1.1 jmmv perms(true, true, true, true, false, true, true, false, true);
555 1.1 jmmv
556 1.1 jmmv chmod("reg", 0777);
557 1.1 jmmv perms(true, true, true, true, true, true, true, true, true);
558 1.1 jmmv
559 1.1 jmmv #undef perms
560 1.1 jmmv
561 1.1 jmmv atf_fs_path_fini(&p);
562 1.1 jmmv }
563 1.1 jmmv
564 1.1 jmmv /* ---------------------------------------------------------------------
565 1.1 jmmv * Test cases for the free functions.
566 1.1 jmmv * --------------------------------------------------------------------- */
567 1.1 jmmv
568 1.1 jmmv ATF_TC(exists);
569 1.1 jmmv ATF_TC_HEAD(exists, tc)
570 1.1 jmmv {
571 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_exists function");
572 1.1 jmmv atf_tc_set_md_var(tc, "use.fs", "true");
573 1.1 jmmv }
574 1.1 jmmv ATF_TC_BODY(exists, tc)
575 1.1 jmmv {
576 1.1 jmmv atf_error_t err;
577 1.1 jmmv atf_fs_path_t pdir, pfile;
578 1.1 jmmv bool b;
579 1.1 jmmv
580 1.1 jmmv RE(atf_fs_path_init_fmt(&pdir, "dir"));
581 1.1 jmmv RE(atf_fs_path_init_fmt(&pfile, "dir/file"));
582 1.1 jmmv
583 1.1 jmmv create_dir(atf_fs_path_cstring(&pdir), 0755);
584 1.1 jmmv create_file(atf_fs_path_cstring(&pfile), 0644);
585 1.1 jmmv
586 1.1 jmmv printf("Checking existence of a directory\n");
587 1.1 jmmv RE(atf_fs_exists(&pdir, &b));
588 1.1 jmmv ATF_REQUIRE(b);
589 1.1 jmmv
590 1.1 jmmv printf("Checking existence of a file\n");
591 1.1 jmmv RE(atf_fs_exists(&pfile, &b));
592 1.1 jmmv ATF_REQUIRE(b);
593 1.1 jmmv
594 1.1 jmmv /* XXX: This should probably be a separate test case to let the user
595 1.1 jmmv * be aware that some tests were skipped because privileges were not
596 1.1 jmmv * correct. */
597 1.1 jmmv if (!atf_user_is_root()) {
598 1.1 jmmv printf("Checking existence of a file inside a directory without "
599 1.1 jmmv "permissions\n");
600 1.1 jmmv ATF_REQUIRE(chmod(atf_fs_path_cstring(&pdir), 0000) != -1);
601 1.1 jmmv err = atf_fs_exists(&pfile, &b);
602 1.1 jmmv ATF_REQUIRE(atf_is_error(err));
603 1.1 jmmv ATF_REQUIRE(atf_error_is(err, "libc"));
604 1.1 jmmv ATF_REQUIRE(chmod(atf_fs_path_cstring(&pdir), 0755) != -1);
605 1.1 jmmv atf_error_free(err);
606 1.1 jmmv }
607 1.1 jmmv
608 1.1 jmmv printf("Checking existence of a non-existent file\n");
609 1.1 jmmv ATF_REQUIRE(unlink(atf_fs_path_cstring(&pfile)) != -1);
610 1.1 jmmv RE(atf_fs_exists(&pfile, &b));
611 1.1 jmmv ATF_REQUIRE(!b);
612 1.1 jmmv
613 1.1 jmmv atf_fs_path_fini(&pfile);
614 1.1 jmmv atf_fs_path_fini(&pdir);
615 1.1 jmmv }
616 1.1 jmmv
617 1.1 jmmv ATF_TC(eaccess);
618 1.1 jmmv ATF_TC_HEAD(eaccess, tc)
619 1.1 jmmv {
620 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_eaccess function");
621 1.1 jmmv atf_tc_set_md_var(tc, "use.fs", "true");
622 1.1 jmmv }
623 1.1 jmmv ATF_TC_BODY(eaccess, tc)
624 1.1 jmmv {
625 1.1 jmmv const int modes[] = { atf_fs_access_f, atf_fs_access_r, atf_fs_access_w,
626 1.1 jmmv atf_fs_access_x, 0 };
627 1.1 jmmv const int *m;
628 1.1 jmmv struct tests {
629 1.1 jmmv mode_t fmode;
630 1.1 jmmv int amode;
631 1.1 jmmv int uerror;
632 1.1 jmmv int rerror;
633 1.1 jmmv } tests[] = {
634 1.1 jmmv { 0000, atf_fs_access_r, EACCES, 0 },
635 1.1 jmmv { 0000, atf_fs_access_w, EACCES, 0 },
636 1.1 jmmv { 0000, atf_fs_access_x, EACCES, EACCES },
637 1.1 jmmv
638 1.1 jmmv { 0001, atf_fs_access_r, EACCES, 0 },
639 1.1 jmmv { 0001, atf_fs_access_w, EACCES, 0 },
640 1.1 jmmv { 0001, atf_fs_access_x, EACCES, 0 },
641 1.1 jmmv { 0002, atf_fs_access_r, EACCES, 0 },
642 1.1 jmmv { 0002, atf_fs_access_w, EACCES, 0 },
643 1.1 jmmv { 0002, atf_fs_access_x, EACCES, EACCES },
644 1.1 jmmv { 0004, atf_fs_access_r, EACCES, 0 },
645 1.1 jmmv { 0004, atf_fs_access_w, EACCES, 0 },
646 1.1 jmmv { 0004, atf_fs_access_x, EACCES, EACCES },
647 1.1 jmmv
648 1.1 jmmv { 0010, atf_fs_access_r, EACCES, 0 },
649 1.1 jmmv { 0010, atf_fs_access_w, EACCES, 0 },
650 1.1 jmmv { 0010, atf_fs_access_x, 0, 0 },
651 1.1 jmmv { 0020, atf_fs_access_r, EACCES, 0 },
652 1.1 jmmv { 0020, atf_fs_access_w, 0, 0 },
653 1.1 jmmv { 0020, atf_fs_access_x, EACCES, EACCES },
654 1.1 jmmv { 0040, atf_fs_access_r, 0, 0 },
655 1.1 jmmv { 0040, atf_fs_access_w, EACCES, 0 },
656 1.1 jmmv { 0040, atf_fs_access_x, EACCES, EACCES },
657 1.1 jmmv
658 1.1 jmmv { 0100, atf_fs_access_r, EACCES, 0 },
659 1.1 jmmv { 0100, atf_fs_access_w, EACCES, 0 },
660 1.1 jmmv { 0100, atf_fs_access_x, 0, 0 },
661 1.1 jmmv { 0200, atf_fs_access_r, EACCES, 0 },
662 1.1 jmmv { 0200, atf_fs_access_w, 0, 0 },
663 1.1 jmmv { 0200, atf_fs_access_x, EACCES, EACCES },
664 1.1 jmmv { 0400, atf_fs_access_r, 0, 0 },
665 1.1 jmmv { 0400, atf_fs_access_w, EACCES, 0 },
666 1.1 jmmv { 0400, atf_fs_access_x, EACCES, EACCES },
667 1.1 jmmv
668 1.1 jmmv { 0, 0, 0, 0 }
669 1.1 jmmv };
670 1.1 jmmv struct tests *t;
671 1.1 jmmv atf_fs_path_t p;
672 1.1 jmmv atf_error_t err;
673 1.1 jmmv
674 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "the-file"));
675 1.1 jmmv
676 1.1 jmmv printf("Non-existent file checks\n");
677 1.1 jmmv for (m = &modes[0]; *m != 0; m++) {
678 1.1 jmmv err = atf_fs_eaccess(&p, *m);
679 1.1 jmmv ATF_REQUIRE(atf_is_error(err));
680 1.1 jmmv ATF_REQUIRE(atf_error_is(err, "libc"));
681 1.1 jmmv ATF_REQUIRE_EQ(atf_libc_error_code(err), ENOENT);
682 1.1 jmmv atf_error_free(err);
683 1.1 jmmv }
684 1.1 jmmv
685 1.1 jmmv create_file(atf_fs_path_cstring(&p), 0000);
686 1.1 jmmv ATF_REQUIRE(chown(atf_fs_path_cstring(&p), geteuid(), getegid()) != -1);
687 1.1 jmmv
688 1.1 jmmv for (t = &tests[0]; t->amode != 0; t++) {
689 1.1 jmmv const int experr = atf_user_is_root() ? t->rerror : t->uerror;
690 1.1 jmmv
691 1.1 jmmv printf("\n");
692 1.1 jmmv printf("File mode : %04o\n", (unsigned int)t->fmode);
693 1.1 jmmv printf("Access mode : 0x%02x\n", t->amode);
694 1.1 jmmv
695 1.1 jmmv ATF_REQUIRE(chmod(atf_fs_path_cstring(&p), t->fmode) != -1);
696 1.1 jmmv
697 1.1 jmmv /* First, existence check. */
698 1.1 jmmv err = atf_fs_eaccess(&p, atf_fs_access_f);
699 1.1 jmmv ATF_REQUIRE(!atf_is_error(err));
700 1.1 jmmv
701 1.1 jmmv /* Now do the specific test case. */
702 1.1 jmmv printf("Expected error: %d\n", experr);
703 1.1 jmmv err = atf_fs_eaccess(&p, t->amode);
704 1.1 jmmv if (atf_is_error(err)) {
705 1.1 jmmv if (atf_error_is(err, "libc"))
706 1.1 jmmv printf("Error : %d\n", atf_libc_error_code(err));
707 1.1 jmmv else
708 1.1 jmmv printf("Error : Non-libc error\n");
709 1.1 jmmv } else
710 1.1 jmmv printf("Error : None\n");
711 1.1 jmmv if (experr == 0) {
712 1.1 jmmv ATF_REQUIRE(!atf_is_error(err));
713 1.1 jmmv } else {
714 1.1 jmmv ATF_REQUIRE(atf_is_error(err));
715 1.1 jmmv ATF_REQUIRE(atf_error_is(err, "libc"));
716 1.1 jmmv ATF_REQUIRE_EQ(atf_libc_error_code(err), experr);
717 1.1 jmmv atf_error_free(err);
718 1.1 jmmv }
719 1.1 jmmv }
720 1.1 jmmv
721 1.1 jmmv atf_fs_path_fini(&p);
722 1.1 jmmv }
723 1.1 jmmv
724 1.1 jmmv ATF_TC(getcwd);
725 1.1 jmmv ATF_TC_HEAD(getcwd, tc)
726 1.1 jmmv {
727 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_getcwd function");
728 1.1 jmmv atf_tc_set_md_var(tc, "use.fs", "true");
729 1.1 jmmv }
730 1.1 jmmv ATF_TC_BODY(getcwd, tc)
731 1.1 jmmv {
732 1.1 jmmv atf_fs_path_t cwd1, cwd2;
733 1.1 jmmv
734 1.1 jmmv create_dir ("root", 0755);
735 1.1 jmmv
736 1.1 jmmv RE(atf_fs_getcwd(&cwd1));
737 1.1 jmmv ATF_REQUIRE(chdir("root") != -1);
738 1.1 jmmv RE(atf_fs_getcwd(&cwd2));
739 1.1 jmmv
740 1.1 jmmv RE(atf_fs_path_append_fmt(&cwd1, "root"));
741 1.1 jmmv
742 1.1 jmmv ATF_REQUIRE(atf_equal_fs_path_fs_path(&cwd1, &cwd2));
743 1.1 jmmv
744 1.1 jmmv atf_fs_path_fini(&cwd2);
745 1.1 jmmv atf_fs_path_fini(&cwd1);
746 1.1 jmmv }
747 1.1 jmmv
748 1.1 jmmv ATF_TC(rmdir_empty);
749 1.1 jmmv ATF_TC_HEAD(rmdir_empty, tc)
750 1.1 jmmv {
751 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_rmdir function");
752 1.1 jmmv atf_tc_set_md_var(tc, "use.fs", "true");
753 1.1 jmmv }
754 1.1 jmmv ATF_TC_BODY(rmdir_empty, tc)
755 1.1 jmmv {
756 1.1 jmmv atf_fs_path_t p;
757 1.1 jmmv
758 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "test-dir"));
759 1.1 jmmv
760 1.1 jmmv ATF_REQUIRE(mkdir("test-dir", 0755) != -1);
761 1.1 jmmv ATF_REQUIRE(exists(&p));
762 1.1 jmmv RE(atf_fs_rmdir(&p));
763 1.1 jmmv ATF_REQUIRE(!exists(&p));
764 1.1 jmmv
765 1.1 jmmv atf_fs_path_fini(&p);
766 1.1 jmmv }
767 1.1 jmmv
768 1.1 jmmv ATF_TC(rmdir_enotempty);
769 1.1 jmmv ATF_TC_HEAD(rmdir_enotempty, tc)
770 1.1 jmmv {
771 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_rmdir function");
772 1.1 jmmv atf_tc_set_md_var(tc, "use.fs", "true");
773 1.1 jmmv }
774 1.1 jmmv ATF_TC_BODY(rmdir_enotempty, tc)
775 1.1 jmmv {
776 1.1 jmmv atf_fs_path_t p;
777 1.1 jmmv atf_error_t err;
778 1.1 jmmv
779 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "test-dir"));
780 1.1 jmmv
781 1.1 jmmv ATF_REQUIRE(mkdir("test-dir", 0755) != -1);
782 1.1 jmmv ATF_REQUIRE(exists(&p));
783 1.1 jmmv create_file("test-dir/foo", 0644);
784 1.1 jmmv
785 1.1 jmmv err = atf_fs_rmdir(&p);
786 1.1 jmmv ATF_REQUIRE(atf_is_error(err));
787 1.1 jmmv ATF_REQUIRE(atf_error_is(err, "libc"));
788 1.1 jmmv ATF_REQUIRE_EQ(atf_libc_error_code(err), ENOTEMPTY);
789 1.1 jmmv atf_error_free(err);
790 1.1 jmmv
791 1.1 jmmv atf_fs_path_fini(&p);
792 1.1 jmmv }
793 1.1 jmmv
794 1.1 jmmv ATF_TC(rmdir_eperm);
795 1.1 jmmv ATF_TC_HEAD(rmdir_eperm, tc)
796 1.1 jmmv {
797 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_rmdir function");
798 1.1 jmmv atf_tc_set_md_var(tc, "use.fs", "true");
799 1.1 jmmv }
800 1.1 jmmv ATF_TC_BODY(rmdir_eperm, tc)
801 1.1 jmmv {
802 1.1 jmmv atf_fs_path_t p;
803 1.1 jmmv atf_error_t err;
804 1.1 jmmv
805 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "test-dir/foo"));
806 1.1 jmmv
807 1.1 jmmv ATF_REQUIRE(mkdir("test-dir", 0755) != -1);
808 1.1 jmmv ATF_REQUIRE(mkdir("test-dir/foo", 0755) != -1);
809 1.1 jmmv ATF_REQUIRE(chmod("test-dir", 0555) != -1);
810 1.1 jmmv ATF_REQUIRE(exists(&p));
811 1.1 jmmv
812 1.1 jmmv err = atf_fs_rmdir(&p);
813 1.1 jmmv if (atf_user_is_root()) {
814 1.1 jmmv ATF_REQUIRE(!atf_is_error(err));
815 1.1 jmmv } else {
816 1.1 jmmv ATF_REQUIRE(atf_is_error(err));
817 1.1 jmmv ATF_REQUIRE(atf_error_is(err, "libc"));
818 1.1 jmmv ATF_REQUIRE_EQ(atf_libc_error_code(err), EACCES);
819 1.1 jmmv atf_error_free(err);
820 1.1 jmmv }
821 1.1 jmmv
822 1.1 jmmv atf_fs_path_fini(&p);
823 1.1 jmmv }
824 1.1 jmmv
825 1.1 jmmv ATF_TC(mkdtemp_ok);
826 1.1 jmmv ATF_TC_HEAD(mkdtemp_ok, tc)
827 1.1 jmmv {
828 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_mkdtemp function, "
829 1.1 jmmv "successful execution");
830 1.1 jmmv atf_tc_set_md_var(tc, "use.fs", "true");
831 1.1 jmmv }
832 1.1 jmmv ATF_TC_BODY(mkdtemp_ok, tc)
833 1.1 jmmv {
834 1.1 jmmv atf_fs_path_t p1, p2;
835 1.1 jmmv atf_fs_stat_t s1, s2;
836 1.1 jmmv
837 1.1 jmmv RE(atf_fs_path_init_fmt(&p1, "testdir.XXXXXX"));
838 1.1 jmmv RE(atf_fs_path_init_fmt(&p2, "testdir.XXXXXX"));
839 1.1 jmmv RE(atf_fs_mkdtemp(&p1));
840 1.1 jmmv RE(atf_fs_mkdtemp(&p2));
841 1.1 jmmv ATF_REQUIRE(!atf_equal_fs_path_fs_path(&p1, &p2));
842 1.1 jmmv ATF_REQUIRE(exists(&p1));
843 1.1 jmmv ATF_REQUIRE(exists(&p2));
844 1.1 jmmv
845 1.1 jmmv RE(atf_fs_stat_init(&s1, &p1));
846 1.1 jmmv ATF_REQUIRE_EQ(atf_fs_stat_get_type(&s1), atf_fs_stat_dir_type);
847 1.1 jmmv ATF_REQUIRE( atf_fs_stat_is_owner_readable(&s1));
848 1.1 jmmv ATF_REQUIRE( atf_fs_stat_is_owner_writable(&s1));
849 1.1 jmmv ATF_REQUIRE( atf_fs_stat_is_owner_executable(&s1));
850 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_group_readable(&s1));
851 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_group_writable(&s1));
852 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_group_executable(&s1));
853 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_other_readable(&s1));
854 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_other_writable(&s1));
855 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_other_executable(&s1));
856 1.1 jmmv
857 1.1 jmmv RE(atf_fs_stat_init(&s2, &p2));
858 1.1 jmmv ATF_REQUIRE_EQ(atf_fs_stat_get_type(&s2), atf_fs_stat_dir_type);
859 1.1 jmmv ATF_REQUIRE( atf_fs_stat_is_owner_readable(&s2));
860 1.1 jmmv ATF_REQUIRE( atf_fs_stat_is_owner_writable(&s2));
861 1.1 jmmv ATF_REQUIRE( atf_fs_stat_is_owner_executable(&s2));
862 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_group_readable(&s2));
863 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_group_writable(&s2));
864 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_group_executable(&s2));
865 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_other_readable(&s2));
866 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_other_writable(&s2));
867 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_other_executable(&s2));
868 1.1 jmmv
869 1.1 jmmv atf_fs_stat_fini(&s2);
870 1.1 jmmv atf_fs_stat_fini(&s1);
871 1.1 jmmv atf_fs_path_fini(&p2);
872 1.1 jmmv atf_fs_path_fini(&p1);
873 1.1 jmmv }
874 1.1 jmmv
875 1.1 jmmv ATF_TC(mkdtemp_err);
876 1.1 jmmv ATF_TC_HEAD(mkdtemp_err, tc)
877 1.1 jmmv {
878 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_mkdtemp function, "
879 1.1 jmmv "error conditions");
880 1.1 jmmv atf_tc_set_md_var(tc, "require.user", "unprivileged");
881 1.1 jmmv atf_tc_set_md_var(tc, "use.fs", "true");
882 1.1 jmmv }
883 1.1 jmmv ATF_TC_BODY(mkdtemp_err, tc)
884 1.1 jmmv {
885 1.1 jmmv atf_error_t err;
886 1.1 jmmv atf_fs_path_t p;
887 1.1 jmmv
888 1.1 jmmv ATF_REQUIRE(mkdir("dir", 0555) != -1);
889 1.1 jmmv
890 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "dir/testdir.XXXXXX"));
891 1.1 jmmv
892 1.1 jmmv err = atf_fs_mkdtemp(&p);
893 1.1 jmmv ATF_REQUIRE(atf_is_error(err));
894 1.1 jmmv ATF_REQUIRE(atf_error_is(err, "libc"));
895 1.1 jmmv ATF_CHECK_EQ(atf_libc_error_code(err), EACCES);
896 1.1 jmmv atf_error_free(err);
897 1.1 jmmv
898 1.1 jmmv ATF_CHECK(!exists(&p));
899 1.1 jmmv ATF_CHECK(strcmp(atf_fs_path_cstring(&p), "dir/testdir.XXXXXX") == 0);
900 1.1 jmmv
901 1.1 jmmv atf_fs_path_fini(&p);
902 1.1 jmmv }
903 1.1 jmmv
904 1.1 jmmv static
905 1.1 jmmv void
906 1.1 jmmv do_umask_check(atf_error_t (*const mk_func)(atf_fs_path_t *),
907 1.1 jmmv atf_fs_path_t *path, const mode_t test_mask,
908 1.1 jmmv const char *str_mask, const char *exp_name)
909 1.1 jmmv {
910 1.1 jmmv char buf[1024];
911 1.1 jmmv int old_umask;
912 1.1 jmmv atf_error_t err;
913 1.1 jmmv
914 1.1 jmmv printf("Creating temporary %s with umask %s\n", exp_name, str_mask);
915 1.1 jmmv
916 1.1 jmmv old_umask = umask(test_mask);
917 1.1 jmmv err = mk_func(path);
918 1.1 jmmv (void)umask(old_umask);
919 1.1 jmmv
920 1.1 jmmv ATF_REQUIRE(atf_is_error(err));
921 1.1 jmmv ATF_REQUIRE(atf_error_is(err, "invalid_umask"));
922 1.1 jmmv atf_error_format(err, buf, sizeof(buf));
923 1.1 jmmv ATF_CHECK(strstr(buf, exp_name) != NULL);
924 1.1 jmmv ATF_CHECK(strstr(buf, str_mask) != NULL);
925 1.1 jmmv atf_error_free(err);
926 1.1 jmmv }
927 1.1 jmmv
928 1.1 jmmv ATF_TC(mkdtemp_umask);
929 1.1 jmmv ATF_TC_HEAD(mkdtemp_umask, tc)
930 1.1 jmmv {
931 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_mkdtemp function "
932 1.1 jmmv "causing an error due to a too strict umask");
933 1.1 jmmv }
934 1.1 jmmv ATF_TC_BODY(mkdtemp_umask, tc)
935 1.1 jmmv {
936 1.1 jmmv atf_fs_path_t p;
937 1.1 jmmv
938 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "testdir.XXXXXX"));
939 1.1 jmmv
940 1.1 jmmv do_umask_check(atf_fs_mkdtemp, &p, 00100, "00100", "directory");
941 1.1 jmmv do_umask_check(atf_fs_mkdtemp, &p, 00200, "00200", "directory");
942 1.1 jmmv do_umask_check(atf_fs_mkdtemp, &p, 00400, "00400", "directory");
943 1.1 jmmv do_umask_check(atf_fs_mkdtemp, &p, 00500, "00500", "directory");
944 1.1 jmmv do_umask_check(atf_fs_mkdtemp, &p, 00600, "00600", "directory");
945 1.1 jmmv
946 1.1 jmmv atf_fs_path_fini(&p);
947 1.1 jmmv }
948 1.1 jmmv
949 1.1 jmmv ATF_TC(mkstemp_ok);
950 1.1 jmmv ATF_TC_HEAD(mkstemp_ok, tc)
951 1.1 jmmv {
952 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_mkstemp function, "
953 1.1 jmmv "successful execution");
954 1.1 jmmv atf_tc_set_md_var(tc, "use.fs", "true");
955 1.1 jmmv }
956 1.1 jmmv ATF_TC_BODY(mkstemp_ok, tc)
957 1.1 jmmv {
958 1.1 jmmv int fd1, fd2;
959 1.1 jmmv atf_fs_path_t p1, p2;
960 1.1 jmmv atf_fs_stat_t s1, s2;
961 1.1 jmmv
962 1.1 jmmv RE(atf_fs_path_init_fmt(&p1, "testfile.XXXXXX"));
963 1.1 jmmv RE(atf_fs_path_init_fmt(&p2, "testfile.XXXXXX"));
964 1.1 jmmv fd1 = fd2 = -1;
965 1.1 jmmv RE(atf_fs_mkstemp(&p1, &fd1));
966 1.1 jmmv RE(atf_fs_mkstemp(&p2, &fd2));
967 1.1 jmmv ATF_REQUIRE(!atf_equal_fs_path_fs_path(&p1, &p2));
968 1.1 jmmv ATF_REQUIRE(exists(&p1));
969 1.1 jmmv ATF_REQUIRE(exists(&p2));
970 1.1 jmmv
971 1.1 jmmv ATF_CHECK(fd1 != -1);
972 1.1 jmmv ATF_CHECK(fd2 != -1);
973 1.1 jmmv ATF_CHECK(write(fd1, "foo", 3) == 3);
974 1.1 jmmv ATF_CHECK(write(fd2, "bar", 3) == 3);
975 1.1 jmmv close(fd1);
976 1.1 jmmv close(fd2);
977 1.1 jmmv
978 1.1 jmmv RE(atf_fs_stat_init(&s1, &p1));
979 1.1 jmmv ATF_CHECK_EQ(atf_fs_stat_get_type(&s1), atf_fs_stat_reg_type);
980 1.1 jmmv ATF_CHECK( atf_fs_stat_is_owner_readable(&s1));
981 1.1 jmmv ATF_CHECK( atf_fs_stat_is_owner_writable(&s1));
982 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_owner_executable(&s1));
983 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_group_readable(&s1));
984 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_group_writable(&s1));
985 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_group_executable(&s1));
986 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_other_readable(&s1));
987 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_other_writable(&s1));
988 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_other_executable(&s1));
989 1.1 jmmv
990 1.1 jmmv RE(atf_fs_stat_init(&s2, &p2));
991 1.1 jmmv ATF_CHECK_EQ(atf_fs_stat_get_type(&s2), atf_fs_stat_reg_type);
992 1.1 jmmv ATF_CHECK( atf_fs_stat_is_owner_readable(&s2));
993 1.1 jmmv ATF_CHECK( atf_fs_stat_is_owner_writable(&s2));
994 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_owner_executable(&s2));
995 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_group_readable(&s2));
996 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_group_writable(&s2));
997 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_group_executable(&s2));
998 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_other_readable(&s2));
999 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_other_writable(&s2));
1000 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_other_executable(&s2));
1001 1.1 jmmv
1002 1.1 jmmv atf_fs_stat_fini(&s2);
1003 1.1 jmmv atf_fs_stat_fini(&s1);
1004 1.1 jmmv atf_fs_path_fini(&p2);
1005 1.1 jmmv atf_fs_path_fini(&p1);
1006 1.1 jmmv }
1007 1.1 jmmv
1008 1.1 jmmv ATF_TC(mkstemp_err);
1009 1.1 jmmv ATF_TC_HEAD(mkstemp_err, tc)
1010 1.1 jmmv {
1011 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_mkstemp function, "
1012 1.1 jmmv "error conditions");
1013 1.1 jmmv atf_tc_set_md_var(tc, "require.user", "unprivileged");
1014 1.1 jmmv atf_tc_set_md_var(tc, "use.fs", "true");
1015 1.1 jmmv }
1016 1.1 jmmv ATF_TC_BODY(mkstemp_err, tc)
1017 1.1 jmmv {
1018 1.1 jmmv int fd;
1019 1.1 jmmv atf_error_t err;
1020 1.1 jmmv atf_fs_path_t p;
1021 1.1 jmmv
1022 1.1 jmmv ATF_REQUIRE(mkdir("dir", 0555) != -1);
1023 1.1 jmmv
1024 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "dir/testfile.XXXXXX"));
1025 1.1 jmmv fd = 1234;
1026 1.1 jmmv
1027 1.1 jmmv err = atf_fs_mkstemp(&p, &fd);
1028 1.1 jmmv ATF_REQUIRE(atf_is_error(err));
1029 1.1 jmmv ATF_REQUIRE(atf_error_is(err, "libc"));
1030 1.1 jmmv ATF_CHECK_EQ(atf_libc_error_code(err), EACCES);
1031 1.1 jmmv atf_error_free(err);
1032 1.1 jmmv
1033 1.1 jmmv ATF_CHECK(!exists(&p));
1034 1.1 jmmv ATF_CHECK(strcmp(atf_fs_path_cstring(&p), "dir/testfile.XXXXXX") == 0);
1035 1.1 jmmv ATF_CHECK_EQ(fd, 1234);
1036 1.1 jmmv
1037 1.1 jmmv atf_fs_path_fini(&p);
1038 1.1 jmmv }
1039 1.1 jmmv
1040 1.1 jmmv ATF_TC(mkstemp_umask);
1041 1.1 jmmv ATF_TC_HEAD(mkstemp_umask, tc)
1042 1.1 jmmv {
1043 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_mkstemp function "
1044 1.1 jmmv "causing an error due to a too strict umask");
1045 1.1 jmmv }
1046 1.1 jmmv ATF_TC_BODY(mkstemp_umask, tc)
1047 1.1 jmmv {
1048 1.1 jmmv atf_fs_path_t p;
1049 1.1 jmmv
1050 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "testfile.XXXXXX"));
1051 1.1 jmmv
1052 1.1 jmmv do_umask_check(mkstemp_discard_fd, &p, 00100, "00100", "regular file");
1053 1.1 jmmv do_umask_check(mkstemp_discard_fd, &p, 00200, "00200", "regular file");
1054 1.1 jmmv do_umask_check(mkstemp_discard_fd, &p, 00400, "00400", "regular file");
1055 1.1 jmmv
1056 1.1 jmmv atf_fs_path_fini(&p);
1057 1.1 jmmv }
1058 1.1 jmmv
1059 1.1 jmmv /* ---------------------------------------------------------------------
1060 1.1 jmmv * Main.
1061 1.1 jmmv * --------------------------------------------------------------------- */
1062 1.1 jmmv
1063 1.1 jmmv ATF_TP_ADD_TCS(tp)
1064 1.1 jmmv {
1065 1.1 jmmv /* Add the tests for the "atf_fs_path" type. */
1066 1.1 jmmv ATF_TP_ADD_TC(tp, path_normalize);
1067 1.1 jmmv ATF_TP_ADD_TC(tp, path_copy);
1068 1.1 jmmv ATF_TP_ADD_TC(tp, path_is_absolute);
1069 1.1 jmmv ATF_TP_ADD_TC(tp, path_is_root);
1070 1.1 jmmv ATF_TP_ADD_TC(tp, path_branch_path);
1071 1.1 jmmv ATF_TP_ADD_TC(tp, path_leaf_name);
1072 1.1 jmmv ATF_TP_ADD_TC(tp, path_append);
1073 1.1 jmmv ATF_TP_ADD_TC(tp, path_to_absolute);
1074 1.1 jmmv ATF_TP_ADD_TC(tp, path_equal);
1075 1.1 jmmv
1076 1.1 jmmv /* Add the tests for the "atf_fs_stat" type. */
1077 1.1 jmmv ATF_TP_ADD_TC(tp, stat_mode);
1078 1.1 jmmv ATF_TP_ADD_TC(tp, stat_type);
1079 1.1 jmmv ATF_TP_ADD_TC(tp, stat_perms);
1080 1.1 jmmv
1081 1.1 jmmv /* Add the tests for the free functions. */
1082 1.1 jmmv ATF_TP_ADD_TC(tp, eaccess);
1083 1.1 jmmv ATF_TP_ADD_TC(tp, exists);
1084 1.1 jmmv ATF_TP_ADD_TC(tp, getcwd);
1085 1.1 jmmv ATF_TP_ADD_TC(tp, rmdir_empty);
1086 1.1 jmmv ATF_TP_ADD_TC(tp, rmdir_enotempty);
1087 1.1 jmmv ATF_TP_ADD_TC(tp, rmdir_eperm);
1088 1.1 jmmv ATF_TP_ADD_TC(tp, mkdtemp_ok);
1089 1.1 jmmv ATF_TP_ADD_TC(tp, mkdtemp_err);
1090 1.1 jmmv ATF_TP_ADD_TC(tp, mkdtemp_umask);
1091 1.1 jmmv ATF_TP_ADD_TC(tp, mkstemp_ok);
1092 1.1 jmmv ATF_TP_ADD_TC(tp, mkstemp_err);
1093 1.1 jmmv ATF_TP_ADD_TC(tp, mkstemp_umask);
1094 1.1 jmmv
1095 1.1 jmmv return atf_no_error();
1096 1.1 jmmv }
1097