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
36 static struct pollfd fds[1];
37 static mode_t mode = 0777;
39 static const char *path = "fileforaudit";
40 static const char *errpath = "dirdoesnotexist/fileforaudit";
41 static const char *successreg = "fileforaudit.*return,success";
42 static const char *failurereg = "fileforaudit.*return,failure";
45 ATF_TC_WITH_CLEANUP(rmdir_success);
46 ATF_TC_HEAD(rmdir_success, tc)
48 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
52 ATF_TC_BODY(rmdir_success, tc)
54 ATF_REQUIRE_EQ(0, mkdir(path, mode));
55 FILE *pipefd = setup(fds, "fd");
56 ATF_REQUIRE_EQ(0, rmdir(path));
57 check_audit(fds, successreg, pipefd);
60 ATF_TC_CLEANUP(rmdir_success, tc)
66 ATF_TC_WITH_CLEANUP(rmdir_failure);
67 ATF_TC_HEAD(rmdir_failure, tc)
69 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
73 ATF_TC_BODY(rmdir_failure, tc)
75 FILE *pipefd = setup(fds, "fd");
76 /* Failure reason: directory does not exist */
77 ATF_REQUIRE_EQ(-1, rmdir(errpath));
78 check_audit(fds, failurereg, pipefd);
81 ATF_TC_CLEANUP(rmdir_failure, tc)
87 ATF_TC_WITH_CLEANUP(rename_success);
88 ATF_TC_HEAD(rename_success, tc)
90 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
94 ATF_TC_BODY(rename_success, tc)
96 ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
97 FILE *pipefd = setup(fds, "fd");
98 ATF_REQUIRE_EQ(0, rename(path, "renamed"));
99 check_audit(fds, successreg, pipefd);
103 ATF_TC_CLEANUP(rename_success, tc)
109 ATF_TC_WITH_CLEANUP(rename_failure);
110 ATF_TC_HEAD(rename_failure, tc)
112 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
116 ATF_TC_BODY(rename_failure, tc)
118 FILE *pipefd = setup(fds, "fd");
119 /* Failure reason: file does not exist */
120 ATF_REQUIRE_EQ(-1, rename(path, "renamed"));
121 check_audit(fds, failurereg, pipefd);
124 ATF_TC_CLEANUP(rename_failure, tc)
130 ATF_TC_WITH_CLEANUP(renameat_success);
131 ATF_TC_HEAD(renameat_success, tc)
133 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
137 ATF_TC_BODY(renameat_success, tc)
139 ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
140 FILE *pipefd = setup(fds, "fd");
141 ATF_REQUIRE_EQ(0, renameat(AT_FDCWD, path, AT_FDCWD, "renamed"));
142 check_audit(fds, successreg, pipefd);
146 ATF_TC_CLEANUP(renameat_success, tc)
152 ATF_TC_WITH_CLEANUP(renameat_failure);
153 ATF_TC_HEAD(renameat_failure, tc)
155 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
159 ATF_TC_BODY(renameat_failure, tc)
161 FILE *pipefd = setup(fds, "fd");
162 /* Failure reason: file does not exist */
163 ATF_REQUIRE_EQ(-1, renameat(AT_FDCWD, path, AT_FDCWD, "renamed"));
164 check_audit(fds, failurereg, pipefd);
167 ATF_TC_CLEANUP(renameat_failure, tc)
173 ATF_TC_WITH_CLEANUP(unlink_success);
174 ATF_TC_HEAD(unlink_success, tc)
176 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
180 ATF_TC_BODY(unlink_success, tc)
182 ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
183 FILE *pipefd = setup(fds, "fd");
184 ATF_REQUIRE_EQ(0, unlink(path));
185 check_audit(fds, successreg, pipefd);
189 ATF_TC_CLEANUP(unlink_success, tc)
195 ATF_TC_WITH_CLEANUP(unlink_failure);
196 ATF_TC_HEAD(unlink_failure, tc)
198 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
202 ATF_TC_BODY(unlink_failure, tc)
204 FILE *pipefd = setup(fds, "fd");
205 /* Failure reason: file does not exist */
206 ATF_REQUIRE_EQ(-1, unlink(errpath));
207 check_audit(fds, failurereg, pipefd);
210 ATF_TC_CLEANUP(unlink_failure, tc)
216 ATF_TC_WITH_CLEANUP(unlinkat_success);
217 ATF_TC_HEAD(unlinkat_success, tc)
219 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
223 ATF_TC_BODY(unlinkat_success, tc)
225 ATF_REQUIRE_EQ(0, mkdir(path, mode));
226 FILE *pipefd = setup(fds, "fd");
227 ATF_REQUIRE_EQ(0, unlinkat(AT_FDCWD, path, AT_REMOVEDIR));
228 check_audit(fds, successreg, pipefd);
231 ATF_TC_CLEANUP(unlinkat_success, tc)
237 ATF_TC_WITH_CLEANUP(unlinkat_failure);
238 ATF_TC_HEAD(unlinkat_failure, tc)
240 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
244 ATF_TC_BODY(unlinkat_failure, tc)
246 FILE *pipefd = setup(fds, "fd");
247 /* Failure reason: directory does not exist */
248 ATF_REQUIRE_EQ(-1, unlinkat(AT_FDCWD, errpath, AT_REMOVEDIR));
249 check_audit(fds, failurereg, pipefd);
252 ATF_TC_CLEANUP(unlinkat_failure, tc)
260 ATF_TP_ADD_TC(tp, rmdir_success);
261 ATF_TP_ADD_TC(tp, rmdir_failure);
263 ATF_TP_ADD_TC(tp, rename_success);
264 ATF_TP_ADD_TC(tp, rename_failure);
265 ATF_TP_ADD_TC(tp, renameat_success);
266 ATF_TP_ADD_TC(tp, renameat_failure);
268 ATF_TP_ADD_TC(tp, unlink_success);
269 ATF_TP_ADD_TC(tp, unlink_failure);
270 ATF_TP_ADD_TC(tp, unlinkat_success);
271 ATF_TP_ADD_TC(tp, unlinkat_failure);
273 return (atf_no_error());