2 * Copyright 2018 Aniket Pandey
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 static struct pollfd fds[1];
35 static mode_t mode = 0777;
37 static const char *path = "fileforaudit";
38 static const char *errpath = "dirdoesnotexist/fileforaudit";
39 static const char *successreg = "fileforaudit.*return,success";
40 static const char *failurereg = "fileforaudit.*return,failure";
43 ATF_TC_WITH_CLEANUP(rmdir_success);
44 ATF_TC_HEAD(rmdir_success, tc)
46 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
50 ATF_TC_BODY(rmdir_success, tc)
52 ATF_REQUIRE_EQ(0, mkdir(path, mode));
53 FILE *pipefd = setup(fds, "fd");
54 ATF_REQUIRE_EQ(0, rmdir(path));
55 check_audit(fds, successreg, pipefd);
58 ATF_TC_CLEANUP(rmdir_success, tc)
64 ATF_TC_WITH_CLEANUP(rmdir_failure);
65 ATF_TC_HEAD(rmdir_failure, tc)
67 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
71 ATF_TC_BODY(rmdir_failure, tc)
73 FILE *pipefd = setup(fds, "fd");
74 /* Failure reason: directory does not exist */
75 ATF_REQUIRE_EQ(-1, rmdir(errpath));
76 check_audit(fds, failurereg, pipefd);
79 ATF_TC_CLEANUP(rmdir_failure, tc)
85 ATF_TC_WITH_CLEANUP(rename_success);
86 ATF_TC_HEAD(rename_success, tc)
88 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
92 ATF_TC_BODY(rename_success, tc)
94 ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
95 FILE *pipefd = setup(fds, "fd");
96 ATF_REQUIRE_EQ(0, rename(path, "renamed"));
97 check_audit(fds, successreg, pipefd);
101 ATF_TC_CLEANUP(rename_success, tc)
107 ATF_TC_WITH_CLEANUP(rename_failure);
108 ATF_TC_HEAD(rename_failure, tc)
110 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
114 ATF_TC_BODY(rename_failure, tc)
116 FILE *pipefd = setup(fds, "fd");
117 /* Failure reason: file does not exist */
118 ATF_REQUIRE_EQ(-1, rename(path, "renamed"));
119 check_audit(fds, failurereg, pipefd);
122 ATF_TC_CLEANUP(rename_failure, tc)
128 ATF_TC_WITH_CLEANUP(renameat_success);
129 ATF_TC_HEAD(renameat_success, tc)
131 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
135 ATF_TC_BODY(renameat_success, tc)
137 ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
138 FILE *pipefd = setup(fds, "fd");
139 ATF_REQUIRE_EQ(0, renameat(AT_FDCWD, path, AT_FDCWD, "renamed"));
140 check_audit(fds, successreg, pipefd);
144 ATF_TC_CLEANUP(renameat_success, tc)
150 ATF_TC_WITH_CLEANUP(renameat_failure);
151 ATF_TC_HEAD(renameat_failure, tc)
153 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
157 ATF_TC_BODY(renameat_failure, tc)
159 FILE *pipefd = setup(fds, "fd");
160 /* Failure reason: file does not exist */
161 ATF_REQUIRE_EQ(-1, renameat(AT_FDCWD, path, AT_FDCWD, "renamed"));
162 check_audit(fds, failurereg, pipefd);
165 ATF_TC_CLEANUP(renameat_failure, tc)
171 ATF_TC_WITH_CLEANUP(unlink_success);
172 ATF_TC_HEAD(unlink_success, tc)
174 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
178 ATF_TC_BODY(unlink_success, tc)
180 ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
181 FILE *pipefd = setup(fds, "fd");
182 ATF_REQUIRE_EQ(0, unlink(path));
183 check_audit(fds, successreg, pipefd);
187 ATF_TC_CLEANUP(unlink_success, tc)
193 ATF_TC_WITH_CLEANUP(unlink_failure);
194 ATF_TC_HEAD(unlink_failure, tc)
196 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
200 ATF_TC_BODY(unlink_failure, tc)
202 FILE *pipefd = setup(fds, "fd");
203 /* Failure reason: file does not exist */
204 ATF_REQUIRE_EQ(-1, unlink(errpath));
205 check_audit(fds, failurereg, pipefd);
208 ATF_TC_CLEANUP(unlink_failure, tc)
214 ATF_TC_WITH_CLEANUP(unlinkat_success);
215 ATF_TC_HEAD(unlinkat_success, tc)
217 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
221 ATF_TC_BODY(unlinkat_success, tc)
223 ATF_REQUIRE_EQ(0, mkdir(path, mode));
224 FILE *pipefd = setup(fds, "fd");
225 ATF_REQUIRE_EQ(0, unlinkat(AT_FDCWD, path, AT_REMOVEDIR));
226 check_audit(fds, successreg, pipefd);
229 ATF_TC_CLEANUP(unlinkat_success, tc)
235 ATF_TC_WITH_CLEANUP(unlinkat_failure);
236 ATF_TC_HEAD(unlinkat_failure, tc)
238 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
242 ATF_TC_BODY(unlinkat_failure, tc)
244 FILE *pipefd = setup(fds, "fd");
245 /* Failure reason: directory does not exist */
246 ATF_REQUIRE_EQ(-1, unlinkat(AT_FDCWD, errpath, AT_REMOVEDIR));
247 check_audit(fds, failurereg, pipefd);
250 ATF_TC_CLEANUP(unlinkat_failure, tc)
258 ATF_TP_ADD_TC(tp, rmdir_success);
259 ATF_TP_ADD_TC(tp, rmdir_failure);
261 ATF_TP_ADD_TC(tp, rename_success);
262 ATF_TP_ADD_TC(tp, rename_failure);
263 ATF_TP_ADD_TC(tp, renameat_success);
264 ATF_TP_ADD_TC(tp, renameat_failure);
266 ATF_TP_ADD_TC(tp, unlink_success);
267 ATF_TP_ADD_TC(tp, unlink_failure);
268 ATF_TP_ADD_TC(tp, unlinkat_success);
269 ATF_TP_ADD_TC(tp, unlinkat_failure);
271 return (atf_no_error());