1# $NetBSD: t_woptions.sh,v 1.2 2020/09/08 06:11:32 mrg Exp $
2#
3# Copyright (c) 2020 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# This code is derived from software contributed to The NetBSD Foundation
7# by Jukka Ruohonen.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29
30# These tests play around with the wifi configuration on a system
31# which may not be safe, destroy configuration or hang.
32check_ifconfig_tests_enabled() {
33	if [ "${ATF_SBIN_IFCONFIG_WIFI_ENABLE}" != "yes" ]; then
34		atf_skip "Test triggers real device activity and may destroy configuration or hang."
35	fi
36}
37
38atf_test_case chan
39chan_head() {
40	atf_set "require.user" "root"
41	atf_set "descr" "Test with ifconfig(8) that setting " \
42		"802.11 channels does not panic (PR kern/55424)"
43}
44
45chan_body() {
46	check_ifconfig_tests_enabled
47
48	# This sequence covers both valid and invalid channels.
49	# Different 802.11 modes are not taken into account, and
50	# all interfaces are tested, including non-802.11 ones.
51	#
52	chans=$(seq 1 500)
53
54	pkill -9 hostapd
55	pkill -9 wpa_supplicant
56
57	for i in $(ifconfig -l); do
58
59		if [ ! -z "$(echo $i | grep urtwn)" ]; then
60			echo "Skipping $i (PR kern/55424)"
61			continue
62		fi
63
64		state="up"
65		ifconfig -s $i
66
67		if [ $? -eq 1 ]; then
68			state="down"
69		fi
70
71		m=""
72		mm=$(ifconfig $i | grep "chan")
73
74		if [ ! -z "$mm" ]; then
75			m=$(echo $mm | awk {'print $2'})
76		fi
77
78		for j in $chans; do
79			echo "Request: $i -> 802.11 chan $j"
80			ifconfig $i chan $j >/dev/null 2>&1
81			echo "Request: $i -> 802.11 -chan $j"
82			ifconfig $i -chan $j >/dev/null 2>&1
83		done
84
85		if [ ! -z $m ]; then
86			ifconfig $i chan $m >/dev/null 2>&1
87			echo "Restored the channel of $i to $m"
88		fi
89
90		ifconfig $i $state
91		echo "Restored state of $i to $state"
92		sleep 1
93	done
94
95	/bin/sh /etc/rc.d/hostapd restart >/dev/null 2>&1
96	/bin/sh /etc/rc.d/wpa_supplicant restart >/dev/null 2>&1
97
98	atf_pass
99}
100
101atf_test_case mediaopt
102mediaopt_head() {
103	atf_set "require.user" "root"
104	atf_set "descr" "Test with ifconfig(8) that setting " \
105		"802.11 media options does not panic (PR kern/35045)"
106}
107
108mediaopt_body() {
109	check_ifconfig_tests_enabled
110
111	# Again, also non-802.11 interfaces are tested.
112	#
113	opts="adhoc monitor hostap"
114
115	pkill -9 hostapd
116	pkill -9 wpa_supplicant
117
118	for i in $(ifconfig -l); do
119
120		state="up"
121		ifconfig -s $i
122
123		if [ $? -eq 1 ]; then
124			state="down"
125		fi
126
127		m=""
128		mm=$(ifconfig $i | grep "media")
129
130		for j in $opts; do
131
132			match=$(echo $mm | grep $j)
133
134			if [ ! -z "$match" ]; then
135				m=$j
136				break
137			fi
138		done
139
140		for j in $opts; do
141			echo "Request: $i -> 802.11 mediaopt $j"
142			ifconfig $i mediaopt $j >/dev/null 2>&1
143			echo "Request: $i -> 802.11 -mediaopt $j"
144			ifconfig $i -mediaopt $j >/dev/null 2>&1
145		done
146
147		if [ ! -z $m ]; then
148			ifconfig $i mode $m >/dev/null 2>&1
149			echo "Restored the mediaopt of $i to $m"
150		fi
151
152		ifconfig $i $state
153		echo "Restored state of $i to $state"
154		sleep 1
155	done
156
157	/bin/sh /etc/rc.d/hostapd restart >/dev/null 2>&1
158	/bin/sh /etc/rc.d/wpa_supplicant restart >/dev/null 2>&1
159
160	atf_pass
161}
162
163atf_test_case modes
164modes_head() {
165	atf_set "require.user" "root"
166	atf_set "descr" "Test with ifconfig(8) that setting " \
167		"802.11 modes does not panic (PR kern/45745)"
168}
169
170modes_body() {
171	check_ifconfig_tests_enabled
172
173	# Although 11n is not yet supported, the system
174	# should not panic from invalid input parameters.
175	# Therefore, the following will try to also set
176	# the four 802.11 modes for non-802.11 devices.
177	#
178	modes="11a 11b 11g 11n"
179
180	pkill -9 hostapd
181	pkill -9 wpa_supplicant
182
183	for i in $(ifconfig -l); do
184
185		m=""
186		mm=$(ifconfig $i | grep "media")
187
188		for j in $modes; do
189
190			match=$(echo $mm | grep $j)
191
192			if [ ! -z "$match" ]; then
193				m=$j
194				break
195			fi
196		done
197
198		for j in $modes; do
199			echo "Request: $i -> 802.11 mode $j"
200			ifconfig $i mode $j >/dev/null 2>&1
201		done
202
203		if [ ! -z $m ]; then
204			ifconfig $i mode $m >/dev/null 2>&1
205			echo "Restored the mode of $i to $m"
206		fi
207	done
208
209	/bin/sh /etc/rc.d/hostapd restart >/dev/null 2>&1
210	/bin/sh /etc/rc.d/wpa_supplicant restart >/dev/null 2>&1
211
212	atf_pass
213}
214
215atf_init_test_cases() {
216	atf_add_test_case chan
217	atf_add_test_case mediaopt
218	atf_add_test_case modes
219}
220