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