Path: blob/main/tests/sys/auditpipe/auditpipe_test.c
39586 views
/*-1* Copyright (c) 2018 Aniket Pandey2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND13* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE14* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE15* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE16* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL17* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS18* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* SUCH DAMAGE.23*/2425#include <sys/ioctl.h>2627#include <bsm/audit.h>28#include <security/audit/audit_ioctl.h>2930#include <atf-c.h>31#include <fcntl.h>32#include <stdio.h>33#include <unistd.h>3435static int filedesc;36static FILE *fileptr;3738ATF_TC(auditpipe_get_qlen);39ATF_TC_HEAD(auditpipe_get_qlen, tc)40{41atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "42"AUDITPIPE_GET_QLEN works properly");43}4445ATF_TC_BODY(auditpipe_get_qlen, tc)46{47int qlen = -1;48ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);49ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLEN, &qlen));50ATF_REQUIRE(qlen != -1);51close(filedesc);52}535455ATF_TC(auditpipe_get_qlimit);56ATF_TC_HEAD(auditpipe_get_qlimit, tc)57{58atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "59"AUDITPIPE_GET_QLIMIT works properly");60}6162ATF_TC_BODY(auditpipe_get_qlimit, tc)63{64int qlimit = -1;65ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);66ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLIMIT, &qlimit));67ATF_REQUIRE(qlimit != -1);68close(filedesc);69}707172ATF_TC_WITH_CLEANUP(auditpipe_set_qlimit);73ATF_TC_HEAD(auditpipe_set_qlimit, tc)74{75atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "76"AUDITPIPE_SET_QLIMIT works properly");77}7879ATF_TC_BODY(auditpipe_set_qlimit, tc)80{81int test_qlimit, curr_qlimit, recv_qlimit;8283ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);84/* Retreive the current QLIMIT value and store it in a file */85ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLIMIT, &curr_qlimit));86ATF_REQUIRE((fileptr = fopen("qlimit_store", "a")) != NULL);87ATF_REQUIRE_EQ(sizeof(curr_qlimit),88fprintf(fileptr, "%d\n", curr_qlimit));8990/*91* Set QLIMIT different from the current system value to confirm92* proper functioning of AUDITPIPE_SET_QLIMIT ioctl.93*/94test_qlimit = curr_qlimit - 1;95ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_SET_QLIMIT, &test_qlimit));96/* Receive modified value and check whether QLIMIT was set correctly */97ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLIMIT, &recv_qlimit));98ATF_REQUIRE_EQ(test_qlimit, recv_qlimit);99100fclose(fileptr);101close(filedesc);102}103104ATF_TC_CLEANUP(auditpipe_set_qlimit, tc)105{106if (atf_utils_file_exists("qlimit_store")) {107int fd, curr_qlim;108ATF_REQUIRE((fileptr = fopen("qlimit_store", "r")) != NULL);109ATF_REQUIRE(fscanf(fileptr, "%d", &curr_qlim));110111ATF_REQUIRE((fd = open("/dev/auditpipe", O_RDONLY)) != -1);112/* Set QLIMIT's value as it was prior to test-case invocation */113ATF_REQUIRE_EQ(0, ioctl(fd, AUDITPIPE_SET_QLIMIT, &curr_qlim));114115close(fd);116fclose(fileptr);117}118}119120121ATF_TC(auditpipe_get_qlimit_min);122ATF_TC_HEAD(auditpipe_get_qlimit_min, tc)123{124atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "125"AUDITPIPE_GET_QLIMIT_MIN works properly");126}127128ATF_TC_BODY(auditpipe_get_qlimit_min, tc)129{130int qlim_min = -1;131ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);132ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLIMIT_MIN, &qlim_min));133ATF_REQUIRE(qlim_min != -1);134close(filedesc);135}136137138ATF_TC(auditpipe_get_qlimit_max);139ATF_TC_HEAD(auditpipe_get_qlimit_max, tc)140{141atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "142"AUDITPIPE_GET_QLIMIT_MAX works properly");143}144145ATF_TC_BODY(auditpipe_get_qlimit_max, tc)146{147int qlim_max = -1;148ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);149ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLIMIT_MAX, &qlim_max));150ATF_REQUIRE(qlim_max != -1);151close(filedesc);152}153154155ATF_TC(auditpipe_get_maxauditdata);156ATF_TC_HEAD(auditpipe_get_maxauditdata, tc)157{158atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "159"AUDITPIPE_GET_MAXAUDITDATA works properly");160}161162ATF_TC_BODY(auditpipe_get_maxauditdata, tc)163{164int audata = -1;165ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);166ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_MAXAUDITDATA, &audata));167ATF_REQUIRE(audata != -1);168close(filedesc);169}170171172ATF_TP_ADD_TCS(tp)173{174ATF_TP_ADD_TC(tp, auditpipe_get_qlen);175ATF_TP_ADD_TC(tp, auditpipe_get_qlimit);176ATF_TP_ADD_TC(tp, auditpipe_set_qlimit);177ATF_TP_ADD_TC(tp, auditpipe_get_qlimit_min);178ATF_TP_ADD_TC(tp, auditpipe_get_qlimit_max);179ATF_TP_ADD_TC(tp, auditpipe_get_maxauditdata);180181return (atf_no_error());182}183184185