1# $NetBSD: t_modload.sh,v 1.14 2024/04/28 07:27:41 rillig Exp $
2#
3# Copyright (c) 2008 The NetBSD Foundation, Inc.
4# All rights reserved.
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25# POSSIBILITY OF SUCH DAMAGE.
26#
27
28check_sysctl() {
29	echo "${1} = ${2}" >expout
30	atf_check -s exit:0 -o file:expout -e empty sysctl ${1}
31}
32
33atf_test_case plain cleanup
34plain_head() {
35	atf_set "descr" "Test load without arguments"
36	atf_set "require.user" "root"
37}
38plain_body() {
39
40	# XXX: Adjust when modctl(8) fails consistently.
41	#
42	$(atf_get_srcdir)/k_helper3 \
43		"%s/k_helper/k_helper.kmod" $(atf_get_srcdir)
44
45	if [ $? -eq 1 ] || [ $? -eq 78 ]; then
46		atf_skip "host does not support modules"
47	fi
48
49	cat >experr <<EOF
50modload: No such file or directory
51EOF
52	atf_check -s exit:1 -o empty -e ignore modload non-existent.o
53
54	atf_check -s exit:0 -o empty -e empty \
55	    modload $(atf_get_srcdir)/k_helper/k_helper.kmod
56	check_sysctl vendor.k_helper.present 1
57	check_sysctl vendor.k_helper.prop_int_ok 0
58	check_sysctl vendor.k_helper.prop_str_ok 0
59	atf_check -s exit:0 -o empty -e empty modunload k_helper
60	touch done
61}
62plain_cleanup() {
63	test -f done || modunload k_helper >/dev/null 2>&1
64}
65
66atf_test_case bflag cleanup
67bflag_head() {
68	atf_set "descr" "Test the -b flag"
69	atf_set "require.user" "root"
70}
71bflag_body() {
72	echo "Checking error conditions"
73
74	atf_check -s exit:1 -o empty -e save:stderr \
75	    modload -b foo k_helper.kmod
76	atf_check -s exit:0 -o ignore -e empty \
77	    grep 'Invalid parameter.*foo' stderr
78
79	atf_check -s exit:1 -o empty -e save:stderr \
80	    modload -b foo= k_helper.kmod
81	atf_check -s exit:0 -o ignore -e empty \
82	    grep 'Invalid boolean value' stderr
83
84	atf_check -s exit:1 -o empty -e save:stderr \
85	    modload -b foo=bar k_helper.kmod
86	atf_check -s exit:0 -o ignore -e empty \
87	    grep 'Invalid boolean value.*bar' stderr
88
89	atf_check -s exit:1 -o empty -e save:stderr \
90	    modload -b foo=falsea k_helper.kmod
91	atf_check -s exit:0 -o ignore -e empty \
92	    grep 'Invalid boolean value.*falsea' stderr
93
94	atf_check -s exit:1 -o empty -e save:stderr \
95	    modload -b foo=truea k_helper.kmod
96	atf_check -s exit:0 -o ignore -e empty \
97	    grep 'Invalid boolean value.*truea' stderr
98
99	# TODO Once sysctl(8) supports CTLTYPE_BOOL nodes.
100	#echo "Checking valid values"
101}
102bflag_cleanup() {
103	modunload k_helper >/dev/null 2>&1 || true
104}
105
106atf_test_case iflag cleanup
107iflag_head() {
108	atf_set "descr" "Test the -i flag"
109	atf_set "require.user" "root"
110}
111iflag_body() {
112
113	# XXX: Adjust when modctl(8) fails consistently.
114	#
115	$(atf_get_srcdir)/k_helper3 \
116		"%s/k_helper/k_helper.kmod" $(atf_get_srcdir)
117
118	if [ $? -eq 1 ] || [ $? -eq 78 ]; then
119		atf_skip "host does not support modules"
120	fi
121
122	echo "Checking error conditions"
123
124	atf_check -s exit:1 -o empty -e save:stderr modload -i foo \
125	    k_helper/k_helper.kmod
126	atf_check -s exit:0 -o ignore -e empty \
127	    grep 'Invalid parameter.*foo' stderr
128
129	atf_check -s exit:1 -o empty -e save:stderr modload -i foo= \
130	    k_helper/k_helper.kmod
131	atf_check -s exit:0 -o ignore -e empty \
132	    grep 'Invalid integer value' stderr
133
134	atf_check -s exit:1 -o empty -e save:stderr \
135	    modload -i foo=bar k_helper/k_helper.kmod
136	atf_check -s exit:0 -o ignore -e empty \
137	    grep 'Invalid integer value.*bar' stderr
138
139	atf_check -s exit:1 -o empty -e save:stderr \
140	    modload -i foo=123a k_helper/k_helper.kmod
141	atf_check -s exit:0 -o ignore -e empty \
142	    grep 'Invalid integer value.*123a' stderr
143
144	echo "Checking valid values"
145
146	for v in 5 10; do
147		atf_check -s exit:0 -o empty -e empty \
148		    modload -i prop_int="${v}" \
149		    $(atf_get_srcdir)/k_helper/k_helper.kmod
150		check_sysctl vendor.k_helper.prop_int_ok 1
151		check_sysctl vendor.k_helper.prop_int_val "${v}"
152		atf_check -s exit:0 -o empty -e empty modunload k_helper
153	done
154	touch done
155}
156iflag_cleanup() {
157	test -f done || modunload k_helper >/dev/null 2>&1
158}
159
160atf_test_case sflag cleanup
161sflag_head() {
162	atf_set "descr" "Test the -s flag"
163	atf_set "require.user" "root"
164}
165sflag_body() {
166
167	# XXX: Adjust when modctl(8) fails consistently.
168	#
169	$(atf_get_srcdir)/k_helper3 \
170		"%s/k_helper/k_helper.kmod" $(atf_get_srcdir)
171
172	if [ $? -eq 1 ] || [ $? -eq 78 ]; then
173		atf_skip "host does not support modules"
174	fi
175
176	echo "Checking error conditions"
177
178	atf_check -s exit:1 -o empty -e save:stderr modload -s foo \
179	    k_helper/k_helper.kmod
180	atf_check -s exit:0 -o ignore -e empty \
181	    grep 'Invalid parameter.*foo' stderr
182
183	echo "Checking valid values"
184
185	for v in '1st string' '2nd string'; do
186		atf_check -s exit:0 -o empty -e empty \
187		    modload -s prop_str="${v}" \
188		    $(atf_get_srcdir)/k_helper/k_helper.kmod
189		check_sysctl vendor.k_helper.prop_str_ok 1
190		check_sysctl vendor.k_helper.prop_str_val "${v}"
191		atf_check -s exit:0 -o empty -e empty modunload k_helper
192	done
193	touch done
194}
195sflag_cleanup() {
196	test -f done || modunload k_helper >/dev/null 2>&1
197}
198
199atf_init_test_cases()
200{
201	atf_add_test_case plain
202	atf_add_test_case bflag
203	atf_add_test_case iflag
204	atf_add_test_case sflag
205}
206