xref: /freebsd/tests/sys/auditpipe/auditpipe_test.c (revision 3468bf40ceecb894b2de46c84990c5d417303fdd)
1*3468bf40SAlan Somers /*-
2*3468bf40SAlan Somers  * Copyright (c) 2018 Aniket Pandey
3*3468bf40SAlan Somers  *
4*3468bf40SAlan Somers  * Redistribution and use in source and binary forms, with or without
5*3468bf40SAlan Somers  * modification, are permitted provided that the following conditions
6*3468bf40SAlan Somers  * are met:
7*3468bf40SAlan Somers  * 1. Redistributions of source code must retain the above copyright
8*3468bf40SAlan Somers  *    notice, this list of conditions and the following disclaimer.
9*3468bf40SAlan Somers  * 2. Redistributions in binary form must reproduce the above copyright
10*3468bf40SAlan Somers  *    notice, this list of conditions and the following disclaimer in the
11*3468bf40SAlan Somers  *    documentation and/or other materials provided with the distribution.
12*3468bf40SAlan Somers  *
13*3468bf40SAlan Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14*3468bf40SAlan Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15*3468bf40SAlan Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16*3468bf40SAlan Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17*3468bf40SAlan Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18*3468bf40SAlan Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19*3468bf40SAlan Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
20*3468bf40SAlan Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*3468bf40SAlan Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*3468bf40SAlan Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*3468bf40SAlan Somers  * SUCH DAMAGE.
24*3468bf40SAlan Somers  *
25*3468bf40SAlan Somers  * $FreeBSD$
26*3468bf40SAlan Somers  */
27*3468bf40SAlan Somers 
28*3468bf40SAlan Somers #include <sys/ioctl.h>
29*3468bf40SAlan Somers 
30*3468bf40SAlan Somers #include <bsm/audit.h>
31*3468bf40SAlan Somers #include <security/audit/audit_ioctl.h>
32*3468bf40SAlan Somers 
33*3468bf40SAlan Somers #include <atf-c.h>
34*3468bf40SAlan Somers #include <fcntl.h>
35*3468bf40SAlan Somers #include <stdio.h>
36*3468bf40SAlan Somers #include <unistd.h>
37*3468bf40SAlan Somers 
38*3468bf40SAlan Somers static int filedesc;
39*3468bf40SAlan Somers static FILE *fileptr;
40*3468bf40SAlan Somers 
41*3468bf40SAlan Somers ATF_TC(auditpipe_get_qlen);
42*3468bf40SAlan Somers ATF_TC_HEAD(auditpipe_get_qlen, tc)
43*3468bf40SAlan Somers {
44*3468bf40SAlan Somers 	atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "
45*3468bf40SAlan Somers 					"AUDITPIPE_GET_QLEN works properly");
46*3468bf40SAlan Somers }
47*3468bf40SAlan Somers 
48*3468bf40SAlan Somers ATF_TC_BODY(auditpipe_get_qlen, tc)
49*3468bf40SAlan Somers {
50*3468bf40SAlan Somers 	int qlen = -1;
51*3468bf40SAlan Somers 	ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);
52*3468bf40SAlan Somers 	ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLEN, &qlen));
53*3468bf40SAlan Somers 	ATF_REQUIRE(qlen != -1);
54*3468bf40SAlan Somers 	close(filedesc);
55*3468bf40SAlan Somers }
56*3468bf40SAlan Somers 
57*3468bf40SAlan Somers 
58*3468bf40SAlan Somers ATF_TC(auditpipe_get_qlimit);
59*3468bf40SAlan Somers ATF_TC_HEAD(auditpipe_get_qlimit, tc)
60*3468bf40SAlan Somers {
61*3468bf40SAlan Somers 	atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "
62*3468bf40SAlan Somers 					"AUDITPIPE_GET_QLIMIT works properly");
63*3468bf40SAlan Somers }
64*3468bf40SAlan Somers 
65*3468bf40SAlan Somers ATF_TC_BODY(auditpipe_get_qlimit, tc)
66*3468bf40SAlan Somers {
67*3468bf40SAlan Somers 	int qlimit = -1;
68*3468bf40SAlan Somers 	ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);
69*3468bf40SAlan Somers 	ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLIMIT, &qlimit));
70*3468bf40SAlan Somers 	ATF_REQUIRE(qlimit != -1);
71*3468bf40SAlan Somers 	close(filedesc);
72*3468bf40SAlan Somers }
73*3468bf40SAlan Somers 
74*3468bf40SAlan Somers 
75*3468bf40SAlan Somers ATF_TC_WITH_CLEANUP(auditpipe_set_qlimit);
76*3468bf40SAlan Somers ATF_TC_HEAD(auditpipe_set_qlimit, tc)
77*3468bf40SAlan Somers {
78*3468bf40SAlan Somers 	atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "
79*3468bf40SAlan Somers 					"AUDITPIPE_SET_QLIMIT works properly");
80*3468bf40SAlan Somers }
81*3468bf40SAlan Somers 
82*3468bf40SAlan Somers ATF_TC_BODY(auditpipe_set_qlimit, tc)
83*3468bf40SAlan Somers {
84*3468bf40SAlan Somers 	int test_qlimit, curr_qlimit, recv_qlimit;
85*3468bf40SAlan Somers 
86*3468bf40SAlan Somers 	ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);
87*3468bf40SAlan Somers 	/* Retreive the current QLIMIT value and store it in a file */
88*3468bf40SAlan Somers 	ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLIMIT, &curr_qlimit));
89*3468bf40SAlan Somers 	ATF_REQUIRE((fileptr = fopen("qlimit_store", "a")) != NULL);
90*3468bf40SAlan Somers 	ATF_REQUIRE_EQ(sizeof(curr_qlimit),
91*3468bf40SAlan Somers 		fprintf(fileptr, "%d\n", curr_qlimit));
92*3468bf40SAlan Somers 
93*3468bf40SAlan Somers 	/*
94*3468bf40SAlan Somers 	 * Set QLIMIT different from the current system value to confirm
95*3468bf40SAlan Somers 	 * proper functioning of AUDITPIPE_SET_QLIMIT ioctl.
96*3468bf40SAlan Somers 	 */
97*3468bf40SAlan Somers 	test_qlimit = curr_qlimit - 1;
98*3468bf40SAlan Somers 	ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_SET_QLIMIT, &test_qlimit));
99*3468bf40SAlan Somers 	/* Receive modified value and check whether QLIMIT was set correctly */
100*3468bf40SAlan Somers 	ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLIMIT, &recv_qlimit));
101*3468bf40SAlan Somers 	ATF_REQUIRE_EQ(test_qlimit, recv_qlimit);
102*3468bf40SAlan Somers 
103*3468bf40SAlan Somers 	fclose(fileptr);
104*3468bf40SAlan Somers 	close(filedesc);
105*3468bf40SAlan Somers }
106*3468bf40SAlan Somers 
107*3468bf40SAlan Somers ATF_TC_CLEANUP(auditpipe_set_qlimit, tc)
108*3468bf40SAlan Somers {
109*3468bf40SAlan Somers 	if (atf_utils_file_exists("qlimit_store")) {
110*3468bf40SAlan Somers 		int fd, curr_qlim;
111*3468bf40SAlan Somers 		ATF_REQUIRE((fileptr = fopen("qlimit_store", "r")) != NULL);
112*3468bf40SAlan Somers 		ATF_REQUIRE(fscanf(fileptr, "%d", &curr_qlim));
113*3468bf40SAlan Somers 
114*3468bf40SAlan Somers 		ATF_REQUIRE((fd = open("/dev/auditpipe", O_RDONLY)) != -1);
115*3468bf40SAlan Somers 		/* Set QLIMIT's value as it was prior to test-case invocation */
116*3468bf40SAlan Somers 		ATF_REQUIRE_EQ(0, ioctl(fd, AUDITPIPE_SET_QLIMIT, &curr_qlim));
117*3468bf40SAlan Somers 
118*3468bf40SAlan Somers 		close(fd);
119*3468bf40SAlan Somers 		fclose(fileptr);
120*3468bf40SAlan Somers 	}
121*3468bf40SAlan Somers }
122*3468bf40SAlan Somers 
123*3468bf40SAlan Somers 
124*3468bf40SAlan Somers ATF_TC(auditpipe_get_qlimit_min);
125*3468bf40SAlan Somers ATF_TC_HEAD(auditpipe_get_qlimit_min, tc)
126*3468bf40SAlan Somers {
127*3468bf40SAlan Somers 	atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "
128*3468bf40SAlan Somers 				"AUDITPIPE_GET_QLIMIT_MIN works properly");
129*3468bf40SAlan Somers }
130*3468bf40SAlan Somers 
131*3468bf40SAlan Somers ATF_TC_BODY(auditpipe_get_qlimit_min, tc)
132*3468bf40SAlan Somers {
133*3468bf40SAlan Somers 	int qlim_min = -1;
134*3468bf40SAlan Somers 	ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);
135*3468bf40SAlan Somers 	ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLIMIT_MIN, &qlim_min));
136*3468bf40SAlan Somers 	ATF_REQUIRE(qlim_min != -1);
137*3468bf40SAlan Somers 	close(filedesc);
138*3468bf40SAlan Somers }
139*3468bf40SAlan Somers 
140*3468bf40SAlan Somers 
141*3468bf40SAlan Somers ATF_TC(auditpipe_get_qlimit_max);
142*3468bf40SAlan Somers ATF_TC_HEAD(auditpipe_get_qlimit_max, tc)
143*3468bf40SAlan Somers {
144*3468bf40SAlan Somers 	atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "
145*3468bf40SAlan Somers 				"AUDITPIPE_GET_QLIMIT_MAX works properly");
146*3468bf40SAlan Somers }
147*3468bf40SAlan Somers 
148*3468bf40SAlan Somers ATF_TC_BODY(auditpipe_get_qlimit_max, tc)
149*3468bf40SAlan Somers {
150*3468bf40SAlan Somers 	int qlim_max = -1;
151*3468bf40SAlan Somers 	ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);
152*3468bf40SAlan Somers 	ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLIMIT_MAX, &qlim_max));
153*3468bf40SAlan Somers 	ATF_REQUIRE(qlim_max != -1);
154*3468bf40SAlan Somers 	close(filedesc);
155*3468bf40SAlan Somers }
156*3468bf40SAlan Somers 
157*3468bf40SAlan Somers 
158*3468bf40SAlan Somers ATF_TC(auditpipe_get_maxauditdata);
159*3468bf40SAlan Somers ATF_TC_HEAD(auditpipe_get_maxauditdata, tc)
160*3468bf40SAlan Somers {
161*3468bf40SAlan Somers 	atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "
162*3468bf40SAlan Somers 				"AUDITPIPE_GET_MAXAUDITDATA works properly");
163*3468bf40SAlan Somers }
164*3468bf40SAlan Somers 
165*3468bf40SAlan Somers ATF_TC_BODY(auditpipe_get_maxauditdata, tc)
166*3468bf40SAlan Somers {
167*3468bf40SAlan Somers 	int audata = -1;
168*3468bf40SAlan Somers 	ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);
169*3468bf40SAlan Somers 	ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_MAXAUDITDATA, &audata));
170*3468bf40SAlan Somers 	ATF_REQUIRE(audata != -1);
171*3468bf40SAlan Somers 	close(filedesc);
172*3468bf40SAlan Somers }
173*3468bf40SAlan Somers 
174*3468bf40SAlan Somers 
175*3468bf40SAlan Somers ATF_TP_ADD_TCS(tp)
176*3468bf40SAlan Somers {
177*3468bf40SAlan Somers 	ATF_TP_ADD_TC(tp, auditpipe_get_qlen);
178*3468bf40SAlan Somers 	ATF_TP_ADD_TC(tp, auditpipe_get_qlimit);
179*3468bf40SAlan Somers 	ATF_TP_ADD_TC(tp, auditpipe_set_qlimit);
180*3468bf40SAlan Somers 	ATF_TP_ADD_TC(tp, auditpipe_get_qlimit_min);
181*3468bf40SAlan Somers 	ATF_TP_ADD_TC(tp, auditpipe_get_qlimit_max);
182*3468bf40SAlan Somers 	ATF_TP_ADD_TC(tp, auditpipe_get_maxauditdata);
183*3468bf40SAlan Somers 
184*3468bf40SAlan Somers 	return (atf_no_error());
185*3468bf40SAlan Somers }
186