1 # $NetBSD: t_mixerctl.sh,v 1.12 2022/08/10 00:14:22 charlotte Exp $ 2 # 3 # Copyright (c) 2017 The NetBSD Foundation, Inc. 4 # All rights reserved. 5 # 6 # This code is derived from software contributed to The NetBSD Foundation 7 # by Charlotte Koch. 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 31 audio_setup() { 32 # Open /dev/pad0 so we have a configured audio device. 33 # Do it in a way that guarantees the open happens before 34 # we proceed to the (</dev/mixer) below (do not expect 35 # cat to be running in time to open the device.) 36 37 # Note: it is not important that the open of pad0 succeeds, 38 # if there is real audio hardware on the system, that can (will) 39 # be used instead, and having pad0 open is irrelevant. 40 # So, no errors reported if pad0 open fails. If there turns 41 # out to be no audio device of any kind, then the open of the 42 # mixer will fail, causing the test to be skipped. 43 44 # Saving padpid and later killing it seems to be unnecessary, 45 # ATF appears to killpg() the process after the test finishes 46 # which is a good thing, otherwise a test that is skipped/fails 47 # would not kill the cat (doing it in a cleanup function is not 48 # convenient as it is a different execution environment, no shared 49 # variables, we would need to put $padpid in a file.) 50 51 unset padpid 52 ( true </dev/pad0 ) >/dev/null 2>&1 && 53 { { cat >/dev/null & } < /dev/pad0 ; } 2>/dev/null && padpid=$! 54 55 (</dev/mixer) >/dev/null 2>&1 || 56 atf_skip "no audio mixer available in kernel" 57 } 58 59 atf_test_case noargs_usage 60 noargs_usage_head() { 61 atf_set "descr" "Ensure mixerctl(1) with no args prints a usage message" 62 } 63 noargs_usage_body() { 64 audio_setup 65 66 atf_check -s exit:1 -o empty -e not-empty \ 67 mixerctl 68 69 ${padpid+kill -HUP ${padpid}} 2>/dev/null || : 70 } 71 72 atf_test_case showvalue 73 showvalue_head() { 74 atf_set "descr" "Ensure mixerctl(1) can print the value for all variables" 75 } 76 showvalue_body() { 77 audio_setup 78 79 for var in $(mixerctl -a | awk -F= '{print $1}'); do 80 atf_check -s exit:0 -e ignore -o match:"^${var}=" \ 81 mixerctl ${var} 82 done 83 84 ${padpid+kill -HUP ${padpid}} 2>/dev/null || : 85 } 86 87 atf_test_case nflag 88 nflag_head() { 89 atf_set "descr" "Ensure 'mixerctl -n' actually suppresses some output" 90 } 91 nflag_body() { 92 audio_setup 93 94 varname="$(mixerctl -a | sed -e 's/=.*//' -e q)" 95 96 atf_check -s exit:0 -o match:"${varname}" -e ignore \ 97 mixerctl ${varname} 98 99 atf_check -s exit:0 -o not-match:"${varname}" -e ignore \ 100 mixerctl -n ${varname} 101 102 ${padpid+kill -HUP ${padpid}} 2>/dev/null || : 103 } 104 105 atf_test_case nonexistant_device 106 nonexistant_device_head() { 107 atf_set "descr" "Ensure mixerctl(1) complains if provided a nonexistant mixer device" 108 } 109 nonexistant_device_body() { 110 atf_check -s not-exit:0 -o ignore -e match:"No such file" \ 111 mixerctl -a -d /a/b/c/d/e 112 } 113 114 atf_init_test_cases() { 115 atf_add_test_case noargs_usage 116 atf_add_test_case showvalue 117 atf_add_test_case nflag 118 atf_add_test_case nonexistant_device 119 } 120