xref: /freebsd/usr.sbin/mixer/tests/mixer_test.sh (revision 080c85127e3fba2c8cfb78cb75f7b306aee4028d)
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" &&
70	mixer -d $(cat test_mixer_deflt_unit) || true
71}
72
73atf_test_case o_flag cleanup
74o_flag_head()
75{
76	atf_set "descr" "Verify that the output of the -o flag can be used " \
77		"as valid input"
78}
79o_flag_body()
80{
81	load_dummy
82	mixer_exists
83	set_default
84
85	atf_check -o ignore -e empty mixer $(mixer -o)
86}
87o_flag_cleanup()
88{
89	restore_default
90}
91
92atf_test_case d_flag cleanup
93d_flag_head()
94{
95	atf_set "descr" "Test default unit setting"
96}
97d_flag_body()
98{
99	load_dummy
100	mixer_exists
101	set_default
102
103	dev="${dummy_unit}"
104	unit=$(echo ${dev} | sed 's/pcm//')
105
106	atf_check -o ignore -e empty mixer -d ${dev}
107	atf_check -o ignore -e empty mixer -d ${unit}
108}
109d_flag_cleanup()
110{
111	restore_default
112}
113
114atf_test_case volume cleanup
115volume_head()
116{
117	atf_set "descr" "Test volume setting"
118}
119volume_body()
120{
121	load_dummy
122	mixer_exists
123	set_default
124	save_conf
125
126	# Test lower bound
127	mixer vol.volume=0
128	atf_check -o match:"0.00:0.00" mixer vol.volume
129
130	mixer vol.volume=-2
131	atf_check -o match:"0.00:0.00" mixer vol.volume
132
133	mixer vol.volume=-1:-2
134	atf_check -o match:"0.00:0.00" mixer vol.volume
135
136	mixer vol.volume=-110%
137	atf_check -o match:"0.00:0.00" mixer vol.volume
138
139	# Test higher bound
140	mixer vol.volume=1
141	atf_check -o match:"1.00:1.00" mixer vol.volume
142
143	mixer vol.volume=+1.01
144	atf_check -o match:"1.00:1.00" mixer vol.volume
145
146	mixer vol.volume=2
147	atf_check -o match:"1.00:1.00" mixer vol.volume
148
149	mixer vol.volume=+1:+1
150	atf_check -o match:"1.00:1.00" mixer vol.volume
151
152	mixer vol.volume=2:2
153	atf_check -o match:"1.00:1.00" mixer vol.volume
154
155	mixer vol.volume=100%
156	atf_check -o match:"1.00:1.00" mixer vol.volume
157
158	mixer vol.volume=110%
159	atf_check -o match:"1.00:1.00" mixer vol.volume
160
161	mixer vol.volume=+110%
162	atf_check -o match:"1.00:1.00" mixer vol.volume
163
164	# Test percentages
165	mixer vol.volume=1
166
167	mixer vol.volume=-10%
168	atf_check -o match:"0.90:0.90" mixer vol.volume
169
170	mixer vol.volume=+5%
171	atf_check -o match:"0.95:0.95" mixer vol.volume
172
173	mixer vol.volume=80%
174	atf_check -o match:"0.80:0.80" mixer vol.volume
175
176	# Test left:right assignment
177	mixer vol.volume=0.80:0.70
178	atf_check -o match:"0.80:0.70" mixer vol.volume
179
180	mixer vol.volume=+5%:+10%
181	atf_check -o match:"0.85:0.80" mixer vol.volume
182
183	mixer vol.volume=-5%:-10%
184	atf_check -o match:"0.80:0.70" mixer vol.volume
185
186	mixer vol.volume=+10%:-15%
187	atf_check -o match:"0.90:0.55" mixer vol.volume
188
189	# Test wrong values
190	atf_check -o ignore -e not-empty mixer vol.volume=foobar
191	atf_check -o ignore -e not-empty mixer vol.volume=2oo:b4r
192	atf_check -o ignore -e not-empty mixer vol.volume=+f0o:1
193}
194volume_cleanup()
195{
196	restore_conf
197	restore_default
198}
199
200atf_test_case mute cleanup
201mute_head()
202{
203	atf_set "descr" "Test muting"
204}
205mute_body()
206{
207	load_dummy
208	mixer_exists
209	set_default
210	save_conf
211
212	# Check that the mute control exists
213	atf_check -o not-empty mixer vol.mute
214
215	atf_check -o ignore -e empty mixer vol.mute=off
216	atf_check -o match:"=off" mixer vol.mute
217
218	atf_check -o ignore -e empty mixer vol.mute=on
219	atf_check -o match:"=on" mixer vol.mute
220
221	atf_check -o ignore -e empty mixer vol.mute=toggle
222	atf_check -o match:"=off" mixer vol.mute
223
224	# Test deprecated interface
225	atf_check -o ignore -e empty mixer vol.mute=0
226	atf_check -o match:"=off" mixer vol.mute
227
228	atf_check -o ignore -e empty mixer vol.mute=1
229	atf_check -o match:"=on" mixer vol.mute
230
231	atf_check -o ignore -e empty mixer vol.mute=^
232	atf_check -o match:"=off" mixer vol.mute
233
234	# Test wrong values
235	atf_check -o ignore -e not-empty mixer vol.mute=foobar
236	atf_check -o ignore -e not-empty mixer vol.mute=10
237}
238mute_cleanup()
239{
240	restore_conf
241	restore_default
242}
243
244atf_test_case recsrc cleanup
245recsrc_head()
246{
247	atf_set "descr" "Test recording source handling"
248}
249recsrc_body()
250{
251	load_dummy
252	mixer_exists
253	set_default
254	save_conf
255	test -n "$(mixer -s)" || atf_skip "no recording source found"
256
257	recsrc=$(mixer -s | awk '{print $2}')
258	atf_check -o ignore -e empty mixer ${recsrc}.recsrc=add
259	atf_check -o ignore -e empty mixer ${recsrc}.recsrc=remove
260	atf_check -o ignore -e empty mixer ${recsrc}.recsrc=set
261	atf_check -o ignore -e empty mixer ${recsrc}.recsrc=toggle
262
263	# Test deprecated interface
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	atf_check -o ignore -e empty mixer ${recsrc}.recsrc=^
268
269	# Test wrong values
270	atf_check -o ignore -e not-empty mixer ${recsrc}.recsrc=foobar
271	atf_check -o ignore -e not-empty mixer ${recsrc}.recsrc=10
272}
273recsrc_cleanup()
274{
275	restore_conf
276	restore_default
277}
278
279atf_init_test_cases()
280{
281	atf_add_test_case o_flag
282	atf_add_test_case d_flag
283	atf_add_test_case volume
284	atf_add_test_case mute
285	atf_add_test_case recsrc
286}
287