xref: /freebsd/tests/sys/auditpipe/auditpipe_test.c (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1 /*-
2  * Copyright (c) 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 
26 #include <sys/ioctl.h>
27 
28 #include <bsm/audit.h>
29 #include <security/audit/audit_ioctl.h>
30 
31 #include <atf-c.h>
32 #include <fcntl.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 
36 static int filedesc;
37 static FILE *fileptr;
38 
39 ATF_TC(auditpipe_get_qlen);
40 ATF_TC_HEAD(auditpipe_get_qlen, tc)
41 {
42 	atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "
43 					"AUDITPIPE_GET_QLEN works properly");
44 }
45 
46 ATF_TC_BODY(auditpipe_get_qlen, tc)
47 {
48 	int qlen = -1;
49 	ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);
50 	ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLEN, &qlen));
51 	ATF_REQUIRE(qlen != -1);
52 	close(filedesc);
53 }
54 
55 
56 ATF_TC(auditpipe_get_qlimit);
57 ATF_TC_HEAD(auditpipe_get_qlimit, tc)
58 {
59 	atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "
60 					"AUDITPIPE_GET_QLIMIT works properly");
61 }
62 
63 ATF_TC_BODY(auditpipe_get_qlimit, tc)
64 {
65 	int qlimit = -1;
66 	ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);
67 	ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLIMIT, &qlimit));
68 	ATF_REQUIRE(qlimit != -1);
69 	close(filedesc);
70 }
71 
72 
73 ATF_TC_WITH_CLEANUP(auditpipe_set_qlimit);
74 ATF_TC_HEAD(auditpipe_set_qlimit, tc)
75 {
76 	atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "
77 					"AUDITPIPE_SET_QLIMIT works properly");
78 }
79 
80 ATF_TC_BODY(auditpipe_set_qlimit, tc)
81 {
82 	int test_qlimit, curr_qlimit, recv_qlimit;
83 
84 	ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);
85 	/* Retreive the current QLIMIT value and store it in a file */
86 	ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLIMIT, &curr_qlimit));
87 	ATF_REQUIRE((fileptr = fopen("qlimit_store", "a")) != NULL);
88 	ATF_REQUIRE_EQ(sizeof(curr_qlimit),
89 		fprintf(fileptr, "%d\n", curr_qlimit));
90 
91 	/*
92 	 * Set QLIMIT different from the current system value to confirm
93 	 * proper functioning of AUDITPIPE_SET_QLIMIT ioctl.
94 	 */
95 	test_qlimit = curr_qlimit - 1;
96 	ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_SET_QLIMIT, &test_qlimit));
97 	/* Receive modified value and check whether QLIMIT was set correctly */
98 	ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLIMIT, &recv_qlimit));
99 	ATF_REQUIRE_EQ(test_qlimit, recv_qlimit);
100 
101 	fclose(fileptr);
102 	close(filedesc);
103 }
104 
105 ATF_TC_CLEANUP(auditpipe_set_qlimit, tc)
106 {
107 	if (atf_utils_file_exists("qlimit_store")) {
108 		int fd, curr_qlim;
109 		ATF_REQUIRE((fileptr = fopen("qlimit_store", "r")) != NULL);
110 		ATF_REQUIRE(fscanf(fileptr, "%d", &curr_qlim));
111 
112 		ATF_REQUIRE((fd = open("/dev/auditpipe", O_RDONLY)) != -1);
113 		/* Set QLIMIT's value as it was prior to test-case invocation */
114 		ATF_REQUIRE_EQ(0, ioctl(fd, AUDITPIPE_SET_QLIMIT, &curr_qlim));
115 
116 		close(fd);
117 		fclose(fileptr);
118 	}
119 }
120 
121 
122 ATF_TC(auditpipe_get_qlimit_min);
123 ATF_TC_HEAD(auditpipe_get_qlimit_min, tc)
124 {
125 	atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "
126 				"AUDITPIPE_GET_QLIMIT_MIN works properly");
127 }
128 
129 ATF_TC_BODY(auditpipe_get_qlimit_min, tc)
130 {
131 	int qlim_min = -1;
132 	ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);
133 	ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLIMIT_MIN, &qlim_min));
134 	ATF_REQUIRE(qlim_min != -1);
135 	close(filedesc);
136 }
137 
138 
139 ATF_TC(auditpipe_get_qlimit_max);
140 ATF_TC_HEAD(auditpipe_get_qlimit_max, tc)
141 {
142 	atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "
143 				"AUDITPIPE_GET_QLIMIT_MAX works properly");
144 }
145 
146 ATF_TC_BODY(auditpipe_get_qlimit_max, tc)
147 {
148 	int qlim_max = -1;
149 	ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);
150 	ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLIMIT_MAX, &qlim_max));
151 	ATF_REQUIRE(qlim_max != -1);
152 	close(filedesc);
153 }
154 
155 
156 ATF_TC(auditpipe_get_maxauditdata);
157 ATF_TC_HEAD(auditpipe_get_maxauditdata, tc)
158 {
159 	atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "
160 				"AUDITPIPE_GET_MAXAUDITDATA works properly");
161 }
162 
163 ATF_TC_BODY(auditpipe_get_maxauditdata, tc)
164 {
165 	int audata = -1;
166 	ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);
167 	ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_MAXAUDITDATA, &audata));
168 	ATF_REQUIRE(audata != -1);
169 	close(filedesc);
170 }
171 
172 
173 ATF_TP_ADD_TCS(tp)
174 {
175 	ATF_TP_ADD_TC(tp, auditpipe_get_qlen);
176 	ATF_TP_ADD_TC(tp, auditpipe_get_qlimit);
177 	ATF_TP_ADD_TC(tp, auditpipe_set_qlimit);
178 	ATF_TP_ADD_TC(tp, auditpipe_get_qlimit_min);
179 	ATF_TP_ADD_TC(tp, auditpipe_get_qlimit_max);
180 	ATF_TP_ADD_TC(tp, auditpipe_get_maxauditdata);
181 
182 	return (atf_no_error());
183 }
184