xref: /freebsd/usr.sbin/mixer/tests/mixer_test.sh (revision 2668e76d6e764c5c361156ffa3d39eb02ce8e5d9)
1#
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2024 The FreeBSD Foundation
5#
6# This software was developed by Christos Margiolis <christos@FreeBSD.org>
7# under sponsorship from the FreeBSD Foundation.
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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28# SUCH DAMAGE.
29
30mixer_exists()
31{
32	if ! mixer >/dev/null 2>&1; then
33		atf_skip "no mixer available"
34	fi
35}
36
37save_conf()
38{
39	atf_check -o save:test_mixer_conf mixer -o
40}
41
42restore_conf()
43{
44	mixer_exists
45	test -r "test_mixer_conf" && mixer $(cat test_mixer_conf)
46}
47
48load_dummy()
49{
50	if ! kldload -n snd_dummy; then
51		atf_skip "cannot load snd_dummy.ko"
52	fi
53}
54
55set_default()
56{
57	deflt_unit="$(mixer | grep ^pcm | cut -f1 -d:)"
58	dummy_unit="$(mixer -a | grep "Dummy Audio Device" | cut -f1 -d:)"
59
60	atf_check -o save:test_mixer_deflt_unit echo ${deflt_unit}
61	atf_check -o save:test_mixer_dummy_unit echo ${dummy_unit}
62
63	# Set the dummy as the default
64	mixer -d ${dummy_unit}
65}
66
67restore_default()
68{
69	test -r "test_mixer_deflt_unit" && mixer -d $(cat test_mixer_deflt_unit)
70}
71
72atf_test_case o_flag cleanup
73o_flag_head()
74{
75	atf_set "descr" "Verify that the output of the -o flag can be used " \
76		"as valid input"
77}
78o_flag_body()
79{
80	load_dummy
81	mixer_exists
82	set_default
83
84	atf_check -o ignore -e empty mixer $(mixer -o)
85}
86o_flag_cleanup()
87{
88	restore_default
89}
90
91atf_test_case d_flag cleanup
92d_flag_head()
93{
94	atf_set "descr" "Test default unit setting"
95}
96d_flag_body()
97{
98	load_dummy
99	mixer_exists
100	set_default
101
102	dev="${dummy_unit}"
103	unit=$(echo ${dev} | sed 's/pcm//')
104
105	atf_check -o ignore -e empty mixer -d ${dev}
106	atf_check -o ignore -e empty mixer -d ${unit}
107}
108d_flag_cleanup()
109{
110	restore_default
111}
112
113atf_test_case volume cleanup
114volume_head()
115{
116	atf_set "descr" "Test volume setting"
117}
118volume_body()
119{
120	load_dummy
121	mixer_exists
122	set_default
123	save_conf
124
125	# Test lower bound
126	mixer vol.volume=0
127	atf_check -o match:"0.00:0.00" mixer vol.volume
128
129	mixer vol.volume=-2
130	atf_check -o match:"0.00:0.00" mixer vol.volume
131
132	mixer vol.volume=-1:-2
133	atf_check -o match:"0.00:0.00" mixer vol.volume
134
135	mixer vol.volume=-110%
136	atf_check -o match:"0.00:0.00" mixer vol.volume
137
138	# Test higher bound
139	mixer vol.volume=1
140	atf_check -o match:"1.00:1.00" mixer vol.volume
141
142	mixer vol.volume=+1.01
143	atf_check -o match:"1.00:1.00" mixer vol.volume
144
145	mixer vol.volume=2
146	atf_check -o match:"1.00:1.00" mixer vol.volume
147
148	mixer vol.volume=+1:+1
149	atf_check -o match:"1.00:1.00" mixer vol.volume
150
151	mixer vol.volume=2:2
152	atf_check -o match:"1.00:1.00" mixer vol.volume
153
154	mixer vol.volume=100%
155	atf_check -o match:"1.00:1.00" mixer vol.volume
156
157	mixer vol.volume=110%
158	atf_check -o match:"1.00:1.00" mixer vol.volume
159
160	mixer vol.volume=+110%
161	atf_check -o match:"1.00:1.00" mixer vol.volume
162
163	# Test percentages
164	mixer vol.volume=1
165
166	mixer vol.volume=-10%
167	atf_check -o match:"0.90:0.90" mixer vol.volume
168
169	mixer vol.volume=+5%
170	atf_check -o match:"0.95:0.95" mixer vol.volume
171
172	mixer vol.volume=80%
173	atf_check -o match:"0.80:0.80" mixer vol.volume
174
175	# Test left:right assignment
176	mixer vol.volume=0.80:0.70
177	atf_check -o match:"0.80:0.70" mixer vol.volume
178
179	mixer vol.volume=+5%:+10%
180	atf_check -o match:"0.85:0.80" mixer vol.volume
181
182	mixer vol.volume=-5%:-10%
183	atf_check -o match:"0.80:0.70" mixer vol.volume
184
185	mixer vol.volume=+10%:-15%
186	atf_check -o match:"0.90:0.55" mixer vol.volume
187
188	# Test wrong values
189	atf_check -o ignore -e not-empty mixer vol.volume=foobar
190	atf_check -o ignore -e not-empty mixer vol.volume=2oo:b4r
191	atf_check -o ignore -e not-empty mixer vol.volume=+f0o:1
192}
193volume_cleanup()
194{
195	restore_conf
196	restore_default
197}
198
199atf_test_case mute cleanup
200mute_head()
201{
202	atf_set "descr" "Test muting"
203}
204mute_body()
205{
206	load_dummy
207	mixer_exists
208	set_default
209	save_conf
210
211	# Check that the mute control exists
212	atf_check -o not-empty mixer vol.mute
213
214	atf_check -o ignore -e empty mixer vol.mute=off
215	atf_check -o match:"=off" mixer vol.mute
216
217	atf_check -o ignore -e empty mixer vol.mute=on
218	atf_check -o match:"=on" mixer vol.mute
219
220	atf_check -o ignore -e empty mixer vol.mute=toggle
221	atf_check -o match:"=off" mixer vol.mute
222
223	# Test deprecated interface
224	atf_check -o ignore -e empty mixer vol.mute=0
225	atf_check -o match:"=off" mixer vol.mute
226
227	atf_check -o ignore -e empty mixer vol.mute=1
228	atf_check -o match:"=on" mixer vol.mute
229
230	atf_check -o ignore -e empty mixer vol.mute=^
231	atf_check -o match:"=off" mixer vol.mute
232
233	# Test wrong values
234	atf_check -o ignore -e not-empty mixer vol.mute=foobar
235	atf_check -o ignore -e not-empty mixer vol.mute=10
236}
237mute_cleanup()
238{
239	restore_conf
240	restore_default
241}
242
243atf_test_case recsrc cleanup
244recsrc_head()
245{
246	atf_set "descr" "Test recording source handling"
247}
248recsrc_body()
249{
250	load_dummy
251	mixer_exists
252	set_default
253	save_conf
254	test -n "$(mixer -s)" || atf_skip "no recording source found"
255
256	recsrc=$(mixer -s | awk '{print $2}')
257	atf_check -o ignore -e empty mixer ${recsrc}.recsrc=add
258	atf_check -o ignore -e empty mixer ${recsrc}.recsrc=remove
259	atf_check -o ignore -e empty mixer ${recsrc}.recsrc=set
260	atf_check -o ignore -e empty mixer ${recsrc}.recsrc=toggle
261
262	# Test deprecated interface
263	atf_check -o ignore -e empty mixer ${recsrc}.recsrc=+
264	atf_check -o ignore -e empty mixer ${recsrc}.recsrc=-
265	atf_check -o ignore -e empty mixer ${recsrc}.recsrc==
266	atf_check -o ignore -e empty mixer ${recsrc}.recsrc=^
267
268	# Test wrong values
269	atf_check -o ignore -e not-empty mixer ${recsrc}.recsrc=foobar
270	atf_check -o ignore -e not-empty mixer ${recsrc}.recsrc=10
271}
272recsrc_cleanup()
273{
274	restore_conf
275	restore_default
276}
277
278atf_init_test_cases()
279{
280	atf_add_test_case o_flag
281	atf_add_test_case d_flag
282	atf_add_test_case volume
283	atf_add_test_case mute
284	atf_add_test_case recsrc
285}
286