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 36 static struct pollfd fds[1]; 37 static mode_t mode = 0777; 38 static const char *path = "fileforaudit"; 39 static const char *errpath = "dirdoesnotexist/fileforaudit"; 40 static const char *successreg = "fileforaudit.*return,success"; 41 static const char *failurereg = "fileforaudit.*return,failure"; 42 43 44 ATF_TC_WITH_CLEANUP(rmdir_success); 45 ATF_TC_HEAD(rmdir_success, tc) 46 { 47 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " 48 "rmdir(2) call"); 49 } 50 51 ATF_TC_BODY(rmdir_success, tc) 52 { 53 ATF_REQUIRE_EQ(0, mkdir(path, mode)); 54 FILE *pipefd = setup(fds, "fd"); 55 ATF_REQUIRE_EQ(0, rmdir(path)); 56 check_audit(fds, successreg, pipefd); 57 } 58 59 ATF_TC_CLEANUP(rmdir_success, tc) 60 { 61 cleanup(); 62 } 63 64 65 ATF_TC_WITH_CLEANUP(rmdir_failure); 66 ATF_TC_HEAD(rmdir_failure, tc) 67 { 68 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " 69 "rmdir(2) call"); 70 } 71 72 ATF_TC_BODY(rmdir_failure, tc) 73 { 74 FILE *pipefd = setup(fds, "fd"); 75 /* Failure reason: directory does not exist */ 76 ATF_REQUIRE_EQ(-1, rmdir(errpath)); 77 check_audit(fds, failurereg, pipefd); 78 } 79 80 ATF_TC_CLEANUP(rmdir_failure, tc) 81 { 82 cleanup(); 83 } 84 85 86 ATF_TC_WITH_CLEANUP(rename_success); 87 ATF_TC_HEAD(rename_success, tc) 88 { 89 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " 90 "rename(2) call"); 91 } 92 93 ATF_TC_BODY(rename_success, tc) 94 { 95 ATF_REQUIRE(open(path, O_CREAT, mode) != -1); 96 FILE *pipefd = setup(fds, "fd"); 97 ATF_REQUIRE_EQ(0, rename(path, "renamed")); 98 check_audit(fds, successreg, pipefd); 99 } 100 101 ATF_TC_CLEANUP(rename_success, tc) 102 { 103 cleanup(); 104 } 105 106 107 ATF_TC_WITH_CLEANUP(rename_failure); 108 ATF_TC_HEAD(rename_failure, tc) 109 { 110 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " 111 "rename(2) call"); 112 } 113 114 ATF_TC_BODY(rename_failure, tc) 115 { 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); 120 } 121 122 ATF_TC_CLEANUP(rename_failure, tc) 123 { 124 cleanup(); 125 } 126 127 128 ATF_TC_WITH_CLEANUP(renameat_success); 129 ATF_TC_HEAD(renameat_success, tc) 130 { 131 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " 132 "renameat(2) call"); 133 } 134 135 ATF_TC_BODY(renameat_success, tc) 136 { 137 ATF_REQUIRE(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); 141 } 142 143 ATF_TC_CLEANUP(renameat_success, tc) 144 { 145 cleanup(); 146 } 147 148 149 ATF_TC_WITH_CLEANUP(renameat_failure); 150 ATF_TC_HEAD(renameat_failure, tc) 151 { 152 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " 153 "renameat(2) call"); 154 } 155 156 ATF_TC_BODY(renameat_failure, tc) 157 { 158 FILE *pipefd = setup(fds, "fd"); 159 /* Failure reason: file does not exist */ 160 ATF_REQUIRE_EQ(-1, renameat(AT_FDCWD, path, AT_FDCWD, "renamed")); 161 check_audit(fds, failurereg, pipefd); 162 } 163 164 ATF_TC_CLEANUP(renameat_failure, tc) 165 { 166 cleanup(); 167 } 168 169 170 ATF_TC_WITH_CLEANUP(unlink_success); 171 ATF_TC_HEAD(unlink_success, tc) 172 { 173 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " 174 "unlink(2) call"); 175 } 176 177 ATF_TC_BODY(unlink_success, tc) 178 { 179 ATF_REQUIRE(open(path, O_CREAT, mode) != -1); 180 FILE *pipefd = setup(fds, "fd"); 181 ATF_REQUIRE_EQ(0, unlink(path)); 182 check_audit(fds, successreg, pipefd); 183 } 184 185 ATF_TC_CLEANUP(unlink_success, tc) 186 { 187 cleanup(); 188 } 189 190 191 ATF_TC_WITH_CLEANUP(unlink_failure); 192 ATF_TC_HEAD(unlink_failure, tc) 193 { 194 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " 195 "unlink(2) call"); 196 } 197 198 ATF_TC_BODY(unlink_failure, tc) 199 { 200 FILE *pipefd = setup(fds, "fd"); 201 /* Failure reason: file does not exist */ 202 ATF_REQUIRE_EQ(-1, unlink(errpath)); 203 check_audit(fds, failurereg, pipefd); 204 } 205 206 ATF_TC_CLEANUP(unlink_failure, tc) 207 { 208 cleanup(); 209 } 210 211 212 ATF_TC_WITH_CLEANUP(unlinkat_success); 213 ATF_TC_HEAD(unlinkat_success, tc) 214 { 215 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " 216 "unlinkat(2) call"); 217 } 218 219 ATF_TC_BODY(unlinkat_success, tc) 220 { 221 ATF_REQUIRE_EQ(0, mkdir(path, mode)); 222 FILE *pipefd = setup(fds, "fd"); 223 ATF_REQUIRE_EQ(0, unlinkat(AT_FDCWD, path, AT_REMOVEDIR)); 224 check_audit(fds, successreg, pipefd); 225 } 226 227 ATF_TC_CLEANUP(unlinkat_success, tc) 228 { 229 cleanup(); 230 } 231 232 233 ATF_TC_WITH_CLEANUP(unlinkat_failure); 234 ATF_TC_HEAD(unlinkat_failure, tc) 235 { 236 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " 237 "unlinkat(2) call"); 238 } 239 240 ATF_TC_BODY(unlinkat_failure, tc) 241 { 242 FILE *pipefd = setup(fds, "fd"); 243 /* Failure reason: directory does not exist */ 244 ATF_REQUIRE_EQ(-1, unlinkat(AT_FDCWD, errpath, AT_REMOVEDIR)); 245 check_audit(fds, failurereg, pipefd); 246 } 247 248 ATF_TC_CLEANUP(unlinkat_failure, tc) 249 { 250 cleanup(); 251 } 252 253 254 ATF_TP_ADD_TCS(tp) 255 { 256 ATF_TP_ADD_TC(tp, rmdir_success); 257 ATF_TP_ADD_TC(tp, rmdir_failure); 258 259 ATF_TP_ADD_TC(tp, rename_success); 260 ATF_TP_ADD_TC(tp, rename_failure); 261 ATF_TP_ADD_TC(tp, renameat_success); 262 ATF_TP_ADD_TC(tp, renameat_failure); 263 264 ATF_TP_ADD_TC(tp, unlink_success); 265 ATF_TP_ADD_TC(tp, unlink_failure); 266 ATF_TP_ADD_TC(tp, unlinkat_success); 267 ATF_TP_ADD_TC(tp, unlinkat_failure); 268 269 return (atf_no_error()); 270 } 271