1 /*
2 * SysDB - t/unit/utils/os_test.c
3 * Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include "utils/os.h"
33 #include "testutils.h"
35 #include <errno.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
40 #include <fcntl.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
46 START_TEST(test_mkdir_remove)
47 {
48 char tmpdir[] = "os_test_dir.XXXXXX";
49 char testdir[1024];
50 char testfile[1024];
51 int check;
53 mode_t mask;
54 struct stat st;
56 if (! mkdtemp(tmpdir)) {
57 fail("INTERNAL ERROR: mkdtemp failed");
58 return;
59 }
60 snprintf(testdir, sizeof(testdir), "%s/%s", tmpdir, "test1");
62 mask = umask(0022);
64 check = sdb_mkdir_all(testdir, 0777);
65 fail_unless(check == 0,
66 "sdb_mkdir_all(%s, %o) = %d (errno = %d); expected: 0",
67 testdir, 0777, errno);
68 check = stat(testdir, &st);
69 fail_unless(check == 0,
70 "stat(%s) = %d (errno = %d); expected: 0",
71 testdir, check, errno);
72 fail_unless((st.st_mode & 0777) == 0755,
73 "sdb_mkdir_all(%s, %o) created permissons %o; expected: %o",
74 testdir, 0777, st.st_mode, 0755);
76 check = sdb_mkdir_all(testdir, 0777);
77 fail_unless(check == 0,
78 "sdb_mkdir_all(%s, %o) = %d (errno = %d) (second attempt); "
79 "expected: 0", testdir, 0777, errno);
81 /* populate the directory */
82 snprintf(testfile, sizeof(testfile), "%s/%s", tmpdir, "testfile1");
83 check = creat(testfile, 0666);
84 fail_unless(check >= 0,
85 "INTERNAL ERROR: creat(%s) = %d (errno = %d); expected: 0",
86 testfile, check, errno);
87 close(check);
88 snprintf(testfile, sizeof(testfile), "%s/%s", testdir, "testfile2");
89 check = creat(testfile, 0666);
90 fail_unless(check >= 0,
91 "INTERNAL ERROR: creat(%s) = %d (errno = %d); expected: 0",
92 testfile, check, errno);
93 close(check);
95 check = sdb_remove_all(tmpdir);
96 fail_unless(check == 0,
97 "sdb_remove_all(%s) = %d (errno = %d); expected: 0",
98 tmpdir, check, errno);
99 fail_unless(access(tmpdir, F_OK),
100 "sdb_remove_all(%s) did not remove the directory");
102 umask(mask);
103 }
104 END_TEST
106 TEST_MAIN("utils::os")
107 {
108 TCase *tc = tcase_create("core");
109 tcase_add_test(tc, test_mkdir_remove);
110 ADD_TCASE(tc);
111 }
112 TEST_MAIN_END
114 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */