1/*- 2 * Copyright 2018 Aniket Pandey 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 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. 12 * 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 23 * SUCH DAMAGE. 24 * 25 * $FreeBSD$ 26 */ 27 28#include <sys/stat.h> 29 30#include <atf-c.h> 31#include <fcntl.h> 32#include <unistd.h> 33 34#include "utils.h" 35 36static struct pollfd fds[1]; 37static mode_t mode = 0777; 38static int filedesc; 39static const char *path = "fileforaudit"; 40static const char *errpath = "dirdoesnotexist/fileforaudit"; 41static const char *successreg = "fileforaudit.*return,success"; 42static const char *failurereg = "fileforaudit.*return,failure"; 43 44 45ATF_TC_WITH_CLEANUP(rmdir_success); 46ATF_TC_HEAD(rmdir_success, tc) 47{ 48 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " 49 "rmdir(2) call"); 50} 51 52ATF_TC_BODY(rmdir_success, tc) 53{ 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); 58} 59 60ATF_TC_CLEANUP(rmdir_success, tc) 61{ 62 cleanup(); 63} 64 65 66ATF_TC_WITH_CLEANUP(rmdir_failure); 67ATF_TC_HEAD(rmdir_failure, tc) 68{ 69 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " 70 "rmdir(2) call"); 71} 72 73ATF_TC_BODY(rmdir_failure, tc) 74{ 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); 79} 80 81ATF_TC_CLEANUP(rmdir_failure, tc) 82{ 83 cleanup(); 84} 85 86 87ATF_TC_WITH_CLEANUP(rename_success); 88ATF_TC_HEAD(rename_success, tc) 89{ 90 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " 91 "rename(2) call"); 92} 93 94ATF_TC_BODY(rename_success, tc) 95{ 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); 100 close(filedesc); 101} 102 103ATF_TC_CLEANUP(rename_success, tc) 104{ 105 cleanup(); 106} 107 108 109ATF_TC_WITH_CLEANUP(rename_failure); 110ATF_TC_HEAD(rename_failure, tc) 111{ 112 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " 113 "rename(2) call"); 114} 115 116ATF_TC_BODY(rename_failure, tc) 117{ 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); 122} 123 124ATF_TC_CLEANUP(rename_failure, tc) 125{ 126 cleanup(); 127} 128 129 130ATF_TC_WITH_CLEANUP(renameat_success); 131ATF_TC_HEAD(renameat_success, tc) 132{ 133 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " 134 "renameat(2) call"); 135} 136 137ATF_TC_BODY(renameat_success, tc) 138{ 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); 143 close(filedesc); 144} 145 146ATF_TC_CLEANUP(renameat_success, tc) 147{ 148 cleanup(); 149} 150 151 152ATF_TC_WITH_CLEANUP(renameat_failure); 153ATF_TC_HEAD(renameat_failure, tc) 154{ 155 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " 156 "renameat(2) call"); 157} 158 159ATF_TC_BODY(renameat_failure, tc) 160{ 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); 165} 166 167ATF_TC_CLEANUP(renameat_failure, tc) 168{ 169 cleanup(); 170} 171 172 173ATF_TC_WITH_CLEANUP(unlink_success); 174ATF_TC_HEAD(unlink_success, tc) 175{ 176 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " 177 "unlink(2) call"); 178} 179 180ATF_TC_BODY(unlink_success, tc) 181{ 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); 186 close(filedesc); 187} 188 189ATF_TC_CLEANUP(unlink_success, tc) 190{ 191 cleanup(); 192} 193 194 195ATF_TC_WITH_CLEANUP(unlink_failure); 196ATF_TC_HEAD(unlink_failure, tc) 197{ 198 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " 199 "unlink(2) call"); 200} 201 202ATF_TC_BODY(unlink_failure, tc) 203{ 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); 208} 209 210ATF_TC_CLEANUP(unlink_failure, tc) 211{ 212 cleanup(); 213} 214 215 216ATF_TC_WITH_CLEANUP(unlinkat_success); 217ATF_TC_HEAD(unlinkat_success, tc) 218{ 219 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " 220 "unlinkat(2) call"); 221} 222 223ATF_TC_BODY(unlinkat_success, tc) 224{ 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); 229} 230 231ATF_TC_CLEANUP(unlinkat_success, tc) 232{ 233 cleanup(); 234} 235 236 237ATF_TC_WITH_CLEANUP(unlinkat_failure); 238ATF_TC_HEAD(unlinkat_failure, tc) 239{ 240 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " 241 "unlinkat(2) call"); 242} 243 244ATF_TC_BODY(unlinkat_failure, tc) 245{ 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); 250} 251 252ATF_TC_CLEANUP(unlinkat_failure, tc) 253{ 254 cleanup(); 255} 256 257 258ATF_TP_ADD_TCS(tp) 259{ 260 ATF_TP_ADD_TC(tp, rmdir_success); 261 ATF_TP_ADD_TC(tp, rmdir_failure); 262 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); 267 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); 272 273 return (atf_no_error()); 274} 275