xref: /freebsd/usr.sbin/mixer/tests/mixer_test.sh (revision 7cc42f6d)
1#
2# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3#
4# Copyright (c) 2019 Mateusz Piotrowski <0mp@FreeBSD.org>
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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26
27# $FreeBSD$
28
29mixer_unavailable()
30{
31	! { mixer && mixer vol; } >/dev/null 2>&1
32}
33
34save_mixer_vol()
35{
36	atf_check -o match:'^[0-9]{1,3}:[0-9]{1,3}$' -o save:saved_vol \
37		-x "mixer vol | awk '{print \$7}'"
38}
39
40set_mixer_vol()
41{
42	atf_check \
43		-o match:'^Setting the mixer vol from [0-9]{1,3}:[0-9]{1,3} to 0:0\.$' \
44		mixer vol 0
45}
46
47restore_mixer_vol()
48{
49	if [ -r "saved_vol" ]; then
50		mixer vol "$(cat saved_vol)"
51	fi
52}
53
54atf_test_case s_flag cleanup
55s_flag_head()
56{
57	atf_set	"descr" "Verify that the output of the -s flag could be " \
58		"reused as command-line arguments to the mixer command"
59}
60s_flag_body()
61{
62	if mixer_unavailable; then
63		atf_skip "This test requires mixer support"
64	fi
65	save_mixer_vol
66	set_mixer_vol
67	atf_check -o inline:"vol 0:0" -o save:values mixer -s vol
68	atf_check -o inline:"Setting the mixer vol from 0:0 to 0:0.\n" \
69		mixer $(cat values)
70}
71s_flag_cleanup()
72{
73	restore_mixer_vol
74}
75
76atf_test_case S_flag cleanup
77S_flag_head()
78{
79	atf_set	"descr" "Verify that the output of the -S flag is " \
80		"matching the documented behavior"
81}
82S_flag_body()
83{
84	if mixer_unavailable; then
85		atf_skip "This test requires mixer support"
86	fi
87	save_mixer_vol
88	set_mixer_vol
89	atf_check -o inline:"vol:0:0" mixer -S vol
90}
91S_flag_cleanup()
92{
93	restore_mixer_vol
94}
95
96atf_test_case set_empty_value cleanup
97set_empty_value_head()
98{
99	atf_set	"descr" "Verify that mixer returns when the provided " \
100		"value to set is an empty string instead of a number"
101	atf_set "timeout" "1"
102}
103set_empty_value_body()
104{
105	if mixer_unavailable; then
106		atf_skip "This test requires mixer support"
107	fi
108	save_mixer_vol
109	atf_check -s exit:1 -e inline:"mixer: invalid value: \n" \
110		-o match:"^usage:" mixer vol ""
111}
112set_empty_value_cleanup()
113{
114	restore_mixer_vol
115}
116
117
118atf_init_test_cases()
119{
120	atf_add_test_case s_flag
121	atf_add_test_case S_flag
122	atf_add_test_case set_empty_value
123}
124