xref: /freebsd/sbin/sysctl/tests/sysctl_test.sh (revision 26e31700fc7d9bd033cb157f534c67bf9bc697dd)
1#
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2022 Yoshihiro Ota <ota@j.email.ne.jp>
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27
28sysctl_name="kern.ostype"
29sysctl_value="FreeBSD"
30sysctl_type="string"
31sysctl_description="Operating system type"
32
33atf_test_case sysctl_aflag
34sysctl_aflag_head()
35{
36	atf_set "descr" "Exercise all sysctl handlers"
37}
38sysctl_aflag_body()
39{
40	# Avoid using atf_check here since sysctl -ao generates tons of
41	# output and it would all otherwise be saved.
42	sysctl -ao >/dev/null 2>stderr
43	if [ $? -ne 0 ]; then
44		cat stderr
45		atf_fail "sysctl -ao failed"
46	elif [ -s stderr ]; then
47		cat stderr
48		atf_fail "sysctl -ao printed to stderr"
49	fi
50}
51
52
53atf_test_case sysctl_aflag_jail
54sysctl_aflag_jail_head()
55{
56	atf_set "descr" "Exercise all sysctl handlers in a jail"
57	atf_set "require.user" "root"
58}
59sysctl_aflag_jail_body()
60{
61	local jail
62
63	jail=sysctl_test_aflag_jail
64
65	# Avoid using atf_check here since sysctl -ao generates tons of
66	# output and it would all otherwise be saved.
67	jail -c name=$jail command=sysctl -ao >/dev/null 2>stderr
68	if [ $? -ne 0 ]; then
69		atf_fail "sysctl -ao failed"
70	elif [ -s stderr ]; then
71		cat stderr
72		atf_fail "sysctl -ao printed to stderr"
73	fi
74
75	jail -c name=$jail vnet command=sysctl -ao >/dev/null 2>stderr
76	if [ $? -ne 0 ]; then
77		atf_fail "sysctl -ao failed"
78	elif [ -s stderr ]; then
79		cat stderr
80		atf_fail "sysctl -ao printed to stderr"
81	fi
82}
83
84
85atf_test_case sysctl_by_name
86sysctl_by_name_head()
87{
88	atf_set "descr" "Verify name without any arguments"
89}
90sysctl_by_name_body()
91{
92	atf_check -o "inline:${sysctl_name}: ${sysctl_value}\n" sysctl ${sysctl_name}
93}
94
95
96atf_test_case sysctl_nflag
97sysctl_nflag_head()
98{
99	atf_set "descr" "Verify -n argument"
100}
101sysctl_nflag_body()
102{
103	atf_check -o "inline:${sysctl_value}\n" sysctl -n ${sysctl_name}
104}
105
106
107atf_test_case sysctl_eflag
108sysctl_eflag_head()
109{
110	atf_set "descr" "Verify -e argument"
111}
112sysctl_eflag_body()
113{
114	atf_check -o "inline:${sysctl_name}=${sysctl_value}\n" sysctl -e ${sysctl_name}
115}
116
117
118atf_test_case sysctl_tflag
119sysctl_tflag_head()
120{
121	atf_set "descr" "Verify -t argument"
122}
123sysctl_tflag_body()
124{
125	atf_check -o "inline:${sysctl_name}: ${sysctl_type}\n" sysctl -t ${sysctl_name}
126}
127
128
129atf_test_case sysctl_dflag
130sysctl_dflag_head()
131{
132	atf_set "descr" "Verify -d argument"
133}
134sysctl_dflag_body()
135{
136	atf_check -o "inline:${sysctl_name}: ${sysctl_description}\n" sysctl -d ${sysctl_name}
137}
138
139
140atf_test_case sysctl_tflag_dflag
141sysctl_tflag_dflag_head()
142{
143	atf_set "descr" "Verify -t -d arguments"
144}
145sysctl_tflag_dflag_body()
146{
147	atf_check -o "inline:${sysctl_name}: ${sysctl_type}: ${sysctl_description}\n" sysctl -t -d ${sysctl_name}
148	atf_check -o "inline:${sysctl_name}: ${sysctl_type}: ${sysctl_description}\n" sysctl -d -t ${sysctl_name}
149}
150
151
152atf_test_case sysctl_nflag_tflag_dflag
153sysctl_nflag_tflag_dflag_head()
154{
155	atf_set "descr" "Verify -n -t -d arguments"
156}
157sysctl_nflag_tflag_dflag_body()
158{
159	atf_check -o "inline:${sysctl_type}: ${sysctl_description}\n" sysctl -n -t -d ${sysctl_name}
160}
161
162
163atf_init_test_cases()
164{
165	atf_add_test_case sysctl_aflag
166	atf_add_test_case sysctl_aflag_jail
167	atf_add_test_case sysctl_by_name
168	atf_add_test_case sysctl_nflag
169	atf_add_test_case sysctl_eflag
170	atf_add_test_case sysctl_tflag
171	atf_add_test_case sysctl_dflag
172	atf_add_test_case sysctl_tflag_dflag
173	atf_add_test_case sysctl_nflag_tflag_dflag
174}
175