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: stable/11/tests/sys/audit/file-write.c 339087 2018-10-02 16:23:33Z asomers $ 26 */ 27 28#include <atf-c.h> 29#include <fcntl.h> 30 31#include "utils.h" 32 33static struct pollfd fds[1]; 34static mode_t mode = 0777; 35static int filedesc; 36static off_t offlen = 0; 37static const char *path = "fileforaudit"; 38static const char *errpath = "dirdoesnotexist/fileforaudit"; 39static const char *successreg = "fileforaudit.*return,success"; 40static const char *failurereg = "fileforaudit.*return,failure"; 41 42 43ATF_TC_WITH_CLEANUP(truncate_success); 44ATF_TC_HEAD(truncate_success, tc) 45{ 46 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " 47 "truncate(2) call"); 48} 49 50ATF_TC_BODY(truncate_success, tc) 51{ 52 /* File needs to exist to call truncate(2) */ 53 ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1); 54 FILE *pipefd = setup(fds, "fw"); 55 ATF_REQUIRE_EQ(0, truncate(path, offlen)); 56 check_audit(fds, successreg, pipefd); 57 close(filedesc); 58} 59 60ATF_TC_CLEANUP(truncate_success, tc) 61{ 62 cleanup(); 63} 64 65 66ATF_TC_WITH_CLEANUP(truncate_failure); 67ATF_TC_HEAD(truncate_failure, tc) 68{ 69 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " 70 "truncate(2) call"); 71} 72 73ATF_TC_BODY(truncate_failure, tc) 74{ 75 FILE *pipefd = setup(fds, "fw"); 76 /* Failure reason: file does not exist */ 77 ATF_REQUIRE_EQ(-1, truncate(errpath, offlen)); 78 check_audit(fds, failurereg, pipefd); 79} 80 81ATF_TC_CLEANUP(truncate_failure, tc) 82{ 83 cleanup(); 84} 85 86 87ATF_TC_WITH_CLEANUP(ftruncate_success); 88ATF_TC_HEAD(ftruncate_success, tc) 89{ 90 atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " 91 "ftruncate(2) call"); 92} 93 94ATF_TC_BODY(ftruncate_success, tc) 95{ 96 const char *regex = "ftruncate.*return,success"; 97 /* Valid file descriptor needs to exist to call ftruncate(2) */ 98 ATF_REQUIRE((filedesc = open(path, O_CREAT | O_RDWR)) != -1); 99 FILE *pipefd = setup(fds, "fw"); 100 ATF_REQUIRE_EQ(0, ftruncate(filedesc, offlen)); 101 check_audit(fds, regex, pipefd); 102 close(filedesc); 103} 104 105ATF_TC_CLEANUP(ftruncate_success, tc) 106{ 107 cleanup(); 108} 109 110 111ATF_TC_WITH_CLEANUP(ftruncate_failure); 112ATF_TC_HEAD(ftruncate_failure, tc) 113{ 114 atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " 115 "ftruncate(2) call"); 116} 117 118ATF_TC_BODY(ftruncate_failure, tc) 119{ 120 const char *regex = "ftruncate.*return,failure"; 121 FILE *pipefd = setup(fds, "fw"); 122 /* Failure reason: bad file descriptor */ 123 ATF_REQUIRE_EQ(-1, ftruncate(-1, offlen)); 124 check_audit(fds, regex, pipefd); 125} 126 127ATF_TC_CLEANUP(ftruncate_failure, tc) 128{ 129 cleanup(); 130} 131 132 133ATF_TP_ADD_TCS(tp) 134{ 135 ATF_TP_ADD_TC(tp, truncate_success); 136 ATF_TP_ADD_TC(tp, truncate_failure); 137 ATF_TP_ADD_TC(tp, ftruncate_success); 138 ATF_TP_ADD_TC(tp, ftruncate_failure); 139 140 return (atf_no_error()); 141} 142