xref: /freebsd/contrib/netbsd-tests/usr.bin/config/t_config.sh (revision 5dae51da3da0cc94d17bd67b308fad304ebec7e0)
1# $NetBSD: t_config.sh,v 1.7 2015/10/04 07:59:47 uebayasi Exp $
2#
3# Copyright (c) 2008, 2010 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
28run_and_check_prep()
29{
30	local name="${1}"; shift
31
32	mkdir compile
33	supportdir="$(atf_get_srcdir)/support"
34
35	local config_str
36	eval config_str=\$${name}_config_str
37	if [ -n "$config_str" ]; then
38		config="d_${name}"
39		printf "$config_str" >"${config}"
40	else
41		config="$(atf_get_srcdir)/d_${name}"
42	fi
43}
44
45run_and_check_pass()
46{
47	local name="${1}"; shift
48
49	run_and_check_prep "${name}"
50
51	atf_check -o ignore -s eq:0 \
52	    config -s "${supportdir}" -b "compile/${name}" "${config}"
53}
54
55run_and_check_warn()
56{
57	local name="${1}"; shift
58
59	run_and_check_prep "${name}"
60
61	local stderr
62	eval stderr=\$${name}_stderr
63	atf_check -o ignore -e "${stderr}" -s eq:0 \
64	    config -s "${supportdir}" -b "compile/${name}" "${config}"
65}
66
67run_and_check_fail()
68{
69	local name="${1}"; shift
70
71	run_and_check_prep "${name}"
72
73	atf_check -o ignore -e ignore -s ne:0 \
74	    config -s "${supportdir}" -b "compile/${name}" "${config}"
75}
76
77test_output()
78{
79	local name="${1}"; shift
80	local res=1
81
82	run_and_check_prep "${name}"
83
84	config -s "${supportdir}" -b compile/"${name}" "${config}" >/dev/null &&
85	cd compile/"${name}" &&
86	check_${name} &&
87	cd $OLDPWD &&
88	res=0
89
90	atf_check test $res -eq 0
91}
92
93# Defines a test case for config(1).
94test_case()
95{
96	local name="${1}"; shift
97	local type="${1}"; shift
98	local descr="${*}"
99
100	atf_test_case "${name}"
101	eval "${name}_head() { \
102		atf_set descr \"${descr}\"; \
103		atf_set require.progs \"config\"; \
104	}"
105	eval "${name}_body() { \
106		run_and_check_${type} '${name}'; \
107	}"
108}
109
110test_case shadow_instance pass "Checks correct handling of shadowed instances"
111test_case loop pass "Checks correct handling of loops"
112test_case loop2 pass "Checks correct handling of devices that can be their" \
113    "own parents"
114test_case pseudo_parent pass "Checks correct handling of children of pseudo" \
115    "devices (PR/32329)"
116test_case postponed_orphan fail "Checks that config catches adding an" \
117    "instance of a child of a negated instance as error"
118test_case no_pseudo fail "Checks that config catches ommited 'pseudo-device'" \
119    "as error (PR/34111)"
120test_case deffs_redef fail "Checks that config doesn't allow a deffs to use" \
121    "the same name as a previous defflag/defparam"
122
123# Selecting an undefined option.
124undefined_opt_config_str='
125include "../d_min"
126options UNDEFINED
127'
128test_case undefined_opt pass \
129    "Checks that config allows a selection for an undefined options"
130
131# Negating an undefined option.
132no_undefined_opt_config_str='
133include "../d_min"
134no options UNDEFINED
135'
136no_undefined_opt_stderr='match:UNDEFINED'
137test_case no_undefined_opt warn \
138    "Checks that config allows a negation for an undefined options"
139
140# Attribute selection
141test_case select pass "Attribute selection"
142select_config_str='
143include "../d_min"
144select c
145'
146check_select()
147{
148	local f=Makefile
149
150	grep -q '^	a\.c ' $f &&
151	grep -q '^	b\.c ' $f &&
152	grep -q '^	c\.c ' $f &&
153	:
154}
155select_body() {
156	test_output select
157}
158
159# Attribute negation
160test_case no_select pass "Attribute negation"
161no_select_config_str='
162include "../d_min"
163select c
164no select a
165'
166check_no_select()
167{
168	local f=Makefile
169
170	: >tmp
171	grep -q '^a\.o:' $f >>tmp
172	grep -q '^b\.o:' $f >>tmp
173	grep -q '^c\.o:' $f >>tmp
174
175	[ ! -s tmp ] &&
176	:
177}
178no_select_body() {
179	test_output no_select
180}
181
182# Device instance
183test_case devi pass "Device instance"
184devi_config_str='
185include "../d_min"
186d0 at root
187'
188check_devi()
189{
190	local f=ioconf.c
191
192	sed -ne '/^struct cfdriver \* const cfdriver_list_initial\[\]/,/^};/p' $f >tmp.cfdriver
193	sed -ne '/^struct cfdata cfdata\[\]/,/^};/p' $f >tmp.cfdata
194
195	grep -q '^CFDRIVER_DECL(d, ' $f &&
196	grep -q '&d_cd,' tmp.cfdriver &&
197	grep -q '^extern struct cfattach d_ca;$' $f &&
198	grep -q '^static const struct cfiattrdata \* const d_attrs\[\]' $f &&
199	grep -q '^static const struct cfiattrdata icf_iattrdata' $f &&
200	grep -q '{ "d",' tmp.cfdata &&
201	:
202}
203devi_body() {
204	test_output devi
205}
206
207# Check minimal kernel config(1) output
208test_case min pass "Minimal config"
209check_min_files()
210{
211	test -e Makefile &&
212	test -e config_file.h &&
213	test -e config_time.src &&
214	test -e ioconf.c &&
215	test -e ioconf.h &&
216	test -e locators.h &&
217	test -e swapregress.c &&
218	test -h machine &&
219	test -h regress &&
220	:
221}
222check_min_makefile()
223{
224	local f=Makefile
225
226	grep -q '^%' $f >tmp.template
227
228	grep -q '^MACHINE=regress$' $f &&
229	grep -q '^PARAM=-DMAXUSERS=4$' $f &&
230	grep -q '^all: regress$' $f &&
231	grep -q '^regress:' $f &&
232	[ ! -s tmp.template ] &&
233	:
234}
235check_min()
236{
237	check_min_files &&
238	check_min_makefile &&
239	:
240}
241min_body() {
242	test_output min
243}
244
245atf_init_test_cases()
246{
247	atf_add_test_case shadow_instance
248	atf_add_test_case loop
249	atf_add_test_case loop2
250	atf_add_test_case pseudo_parent
251	atf_add_test_case postponed_orphan
252	atf_add_test_case no_pseudo
253	atf_add_test_case deffs_redef
254	atf_add_test_case undefined_opt
255	atf_add_test_case no_undefined_opt
256	atf_add_test_case select
257	atf_add_test_case no_select
258	atf_add_test_case devi
259	atf_add_test_case min
260}
261