test-case: add new test case for check mic privacy#1278
test-case: add new test case for check mic privacy#1278cgturner1 merged 2 commits intothesofproject:mainfrom
Conversation
There was a problem hiding this comment.
Pull Request Overview
This pull request adds a new test case to verify the mic privacy mode by toggling a USB relay and checking audio behavior using alsabat.
- Adds the new test script test-case/test-mic-privacy.sh.
- Implements test steps including checking for usbrelay installation, running alsabat tests before and after toggling the mic privacy switch.
- Logs relevant parameters and captures output for troubleshooting.
test-case/test-mic-privacy.sh
Outdated
| if [ $alsabat_status -ne 0 ]; then | ||
| if grep -q -e "Amplitude: 0.0; Percentage: \[0\]" -e "Return value is -1001" "$alsabat_output" | ||
| then | ||
| # Do nothing if signal is zero, this is expected | ||
| # Return value is -1001 | ||
| dlogi "Alsabat output indicates zero signal as expected." | ||
| : | ||
| else | ||
| dloge "alsabat failed with status $alsabat_status, but signal is not zero." | ||
| __upload_wav_file | ||
| dloge "alsabat output: $(cat "$alsabat_output")." | ||
| exit 1 | ||
| fi | ||
| else | ||
| dloge "alsabat passed, upload the wav files." | ||
| dloge "alsabat output: $(cat "$alsabat_output")" | ||
| exit 1 | ||
| fi |
There was a problem hiding this comment.
[nitpick] Consider extracting the alsabat result check and its error handling logic into a separate function to reduce complexity in the main test flow and improve maintainability.
| if [ $alsabat_status -ne 0 ]; then | |
| if grep -q -e "Amplitude: 0.0; Percentage: \[0\]" -e "Return value is -1001" "$alsabat_output" | |
| then | |
| # Do nothing if signal is zero, this is expected | |
| # Return value is -1001 | |
| dlogi "Alsabat output indicates zero signal as expected." | |
| : | |
| else | |
| dloge "alsabat failed with status $alsabat_status, but signal is not zero." | |
| __upload_wav_file | |
| dloge "alsabat output: $(cat "$alsabat_output")." | |
| exit 1 | |
| fi | |
| else | |
| dloge "alsabat passed, upload the wav files." | |
| dloge "alsabat output: $(cat "$alsabat_output")" | |
| exit 1 | |
| fi | |
| handle_alsabat_result "$alsabat_status" "$alsabat_output" | |
| handle_alsabat_result "$alsabat_status" "$alsabat_output" |
test-case/test-mic-privacy.sh
Outdated
| exit 2 | ||
| fi | ||
|
|
||
| # check if usbrelay tool is installed | ||
| if ! command -v usbrelay >/dev/null 2>&1; then | ||
| dloge "usbrelay command not found. Please install usbrelay to control the mic privacy switch." | ||
| exit 2 |
There was a problem hiding this comment.
[nitpick] Using hard-coded exit codes (such as exit 2) may reduce clarity; consider defining constants or adding inline comments to explain the meaning of these values.
| exit 2 | |
| fi | |
| # check if usbrelay tool is installed | |
| if ! command -v usbrelay >/dev/null 2>&1; then | |
| dloge "usbrelay command not found. Please install usbrelay to control the mic privacy switch." | |
| exit 2 | |
| exit $EXIT_CODE_MISSING_PCM # Exit due to missing playback or capture PCM | |
| fi | |
| # check if usbrelay tool is installed | |
| if ! command -v usbrelay >/dev/null 2>&1; then | |
| dloge "usbrelay command not found. Please install usbrelay to control the mic privacy switch." | |
| exit $EXIT_CODE_MISSING_PCM # Exit due to missing usbrelay command |
9675aae to
dba4f4a
Compare
| ## Enable MIC privacy. | ||
| ## Run alsabat process perform both playback and capture again. |
There was a problem hiding this comment.
I think this test should do two more steps:
- Disable MIC privacy
- playback/capture to check the MIC privacy mode is disabled
There was a problem hiding this comment.
there is this check before runs test
test-case/test-mic-privacy.sh
Outdated
| fi | ||
|
|
||
| dlogi "Turn off the mic privacy switch" | ||
| usbrelay HURTM_1=0 |
There was a problem hiding this comment.
is a check possible on success for relay switch on/off ?
There was a problem hiding this comment.
yes, it is. implemented in the function usbrelay_switch in lib.sh
test-case/test-mic-privacy.sh
Outdated
| alsabat -P"$pcm_p" -C"$pcm_c" -c 2 -r $rate || { | ||
| # upload failed wav file | ||
| __upload_wav_file | ||
| exit 1 |
There was a problem hiding this comment.
| exit 1 | |
| skip_test "No audio loopback connected." |
dba4f4a to
fed81ff
Compare
test-case/test-mic-privacy.sh
Outdated
|
|
||
| if [ "$pcm_p" = "" ]||[ "$pcm_c" = "" ]; then | ||
| dloge "No playback or capture PCM is specified. Skip the alsabat test." | ||
| exit 2 |
There was a problem hiding this comment.
There is a skip_test 'reason' function, please use it (AI was right!)
test-case/test-mic-privacy.sh
Outdated
| done | ||
| } | ||
|
|
||
| function check_playback_capture |
There was a problem hiding this comment.
| function check_playback_capture | |
| check_playback_capture |
Unnecessary bashism.
case-lib/lib.sh
Outdated
| # needs usbrelay package: https://github.com/darrylb123/usbrelay | ||
| # param1: switch name | ||
| # param2: switch state | ||
| usbrelay_switch() { |
There was a problem hiding this comment.
Consistency nit
| usbrelay_switch() { | |
| usbrelay_switch() | |
| { |
case-lib/lib.sh
Outdated
| # param2: switch state | ||
| usbrelay_switch() { | ||
| switch_name=$1 | ||
| state=$2 |
There was a problem hiding this comment.
| state=$2 | |
| local state=$2 |
case-lib/lib.sh
Outdated
| switch_name=$1 | ||
| state=$2 | ||
| dlogi "Setting usbrelay switch $switch_name to $state." | ||
| if ! usbrelay "$switch_name=$state" >/dev/null 2>&1; then |
There was a problem hiding this comment.
You can easily avoid negations
| if ! usbrelay "$switch_name=$state" >/dev/null 2>&1; then | |
| usbrelay "$switch_name=$state" >/dev/null 2>&1 || { |
Reads as plain english "do or skip"
case-lib/lib.sh
Outdated
| current_state=$(usbrelay | grep "$switch_name" | awk -F= '{print $2}') | ||
|
|
||
| # Check if current_state is equal to the requested state | ||
| if [[ "$current_state" != "$state" ]]; then |
There was a problem hiding this comment.
Nit
| if [[ "$current_state" != "$state" ]]; then | |
| [[ "$current_state" == "$state" ]] || { |
Reads as "assert or die"
test-case/test-mic-privacy.sh
Outdated
| set_alsa_settings "$MODEL" | ||
| fi | ||
|
|
||
| logger_disabled || func_lib_start_log_collect |
There was a problem hiding this comment.
That's coming surprisingly late... are you sure?
There was a problem hiding this comment.
yes, you'll might right. move it upper.
fed81ff to
2b296b1
Compare
|
@golowanow & @marc-hb thanks for review. Many valuable comments. |
case-lib/lib.sh
Outdated
| local switch_name=$1 | ||
| local state=$2 | ||
| dlogi "Setting usbrelay switch $switch_name to $state." | ||
| usbrelay "$switch_name=$state" >/dev/null 2>&1 || { |
There was a problem hiding this comment.
do you think usbrelay-s output with possible errors is not useful for diagnostics and troubleshooting ?
| usbrelay "$switch_name=$state" >/dev/null 2>&1 || { | |
| usbrelay "$switch_name=$state" 2>&1 || { |
There was a problem hiding this comment.
Discarding stderr is wrong 99.999% of the time. The remaining 0.001% requires a comment with a very strong rationale.
That's because 99.999% of the time, developers use stderr because they think the error is so bad that the message should never be discarded.
Discarding stderr even more wrong in TEST code! It's a really bad code smell.
case-lib/lib.sh
Outdated
| if [[ "$current_state" == "1" ]]; then | ||
| dlogi "Current state of $switch_name is: on" | ||
| elif [[ "$current_state" == "0" ]]; then | ||
| dlogi "Current state of $switch_name is: off" | ||
| else | ||
| dloge "Invalid state for $switch_name: $current_state" | ||
| exit 1 | ||
| fi |
There was a problem hiding this comment.
it seems this if/elif/else chunk should be before $current_state == $state comparison, or this function should reject the incorrect $state parameter early at the entry.
There was a problem hiding this comment.
I think the most important is state of $current_state and it should be equal expected $state, so we should verify it.
If the $state will not be 0 || 1 then the command usbrelay "$switch_name=$state" returns error.
IMO moving the chunk before $current_state == $state doesn't matter.
test-case/test-mic-privacy.sh
Outdated
| fi | ||
|
|
||
| # check if usbrelay tool is installed | ||
| command -v usbrelay >/dev/null 2>&1 || { |
There was a problem hiding this comment.
| command -v usbrelay >/dev/null 2>&1 || { | |
| command -v usbrelay 2>&1 || { |
| dlogi "Turn on the mic privacy switch" | ||
| usbrelay_switch "$relay" 1 | ||
|
|
||
| alsabat_output=$(mktemp) |
There was a problem hiding this comment.
mktemp needs the same explicit template name here, isn't it ?
There was a problem hiding this comment.
no needs the explicit template name, without template the cmd return /tmp/tmp.xxxxxx
32e0fc7 to
74def0a
Compare
case-lib/relay.sh
Outdated
| elif [[ "$current_state" == "0" ]]; then | ||
| dlogi "Current state of $switch_name is: off" | ||
| else | ||
| die "Invalid state for $switch_name: $current_state" |
There was a problem hiding this comment.
case ... in works great for this. Find one example (among many others) at the bottom of 41c1427
test-case/test-mic-privacy.sh
Outdated
|
|
||
| logger_disabled || func_lib_start_log_collect | ||
|
|
||
| if [ "$pcm_p" = "" ]||[ "$pcm_c" = "" ]; then |
There was a problem hiding this comment.
| if [ "$pcm_p" = "" ]||[ "$pcm_c" = "" ]; then | |
| if [ -z "$pcm_p" ] || [ -z "$pcm_c" ]; then |
test-case/test-mic-privacy.sh
Outdated
| logger_disabled || func_lib_start_log_collect | ||
|
|
||
| if [ "$pcm_p" = "" ]||[ "$pcm_c" = "" ]; then | ||
| skip_test "No playback or capture PCM is specified. Skip the alsabat test." |
There was a problem hiding this comment.
| skip_test "No playback or capture PCM is specified. Skip the alsabat test." | |
| skip_test "No playback or capture PCM is specified. Skip the $0 test." |
There is already some test-case/alsabat.sh somewhere.
test-case/test-mic-privacy.sh
Outdated
| alsabat -P"$pcm_p" -C"$pcm_c" -c 2 -r "$rate" >"$alsabat_output" 2>&1 | ||
| alsabat_status=$? | ||
|
|
||
| if [ $alsabat_status -ne 0 ]; then |
There was a problem hiding this comment.
Prefer the "early return" pattern
| if [ $alsabat_status -ne 0 ]; then | |
| alsabat -P"$pcm_p" -C"$pcm_c" -c 2 -r "$rate" >"$alsabat_output" 2>&1 || { | |
| dloge "alsabat passed unexpectedly, upload the wav files." | |
| __upload_wav_files | |
| die "MIC privacy doesn't work. alsabat output: $(cat "$alsabat_output")" | |
| } |
7e5bff3 to
3c4a6f7
Compare
case-lib/relay.sh
Outdated
| local state=$2 | ||
|
|
||
| dlogi "Setting usbrelay switch $switch_name to $state." | ||
| usbrelay "$switch_name=$state" > /dev/null || { |
There was a problem hiding this comment.
| usbrelay "$switch_name=$state" > /dev/null || { | |
| usbrelay "$switch_name=$state" || { |
as already discussed #1278 (comment)
There was a problem hiding this comment.
The command returns a previous state of relays this could be confusing and imo isn't usable..
case-lib/relay.sh
Outdated
|
|
||
| dlogi "Setting usbrelay switch $switch_name to $state." | ||
| usbrelay "$switch_name=$state" > /dev/null || { | ||
| # if not detect relays hw module, skip the test |
There was a problem hiding this comment.
| # if not detect relays hw module, skip the test |
The error message explains already.
test-case/test-mic-privacy.sh
Outdated
| ALSABAT_WAV_FILES="/tmp/mc.wav.*" | ||
| rm -f "$ALSABAT_WAV_FILES" | ||
|
|
||
| TWO_SECONDS=2 |
There was a problem hiding this comment.
does it serve the same purpose as USBRELAY_SETTLE_TIME=0.5s ?
or is it a real USBRELAY_SETTLE_TIME ?
anyway, better to name this variable differently than TWO_SECONDS, just in case someone needs to increase it to 3 seconds
There was a problem hiding this comment.
yes, might you right
test-case/test-mic-privacy.sh
Outdated
| alsabat -P"$pcm_p" -C"$pcm_c" -c 2 -r "$rate" || { | ||
| # upload failed wav file | ||
| __upload_wav_files | ||
| die "alsabat failed" |
There was a problem hiding this comment.
| die "alsabat failed" | |
| die "check_playback_capture() failed: no loopback connected ?" |
test-case/test-mic-privacy.sh
Outdated
| # reset sof volume to 0dB | ||
| reset_sof_volume | ||
|
|
||
| # If MODEL is defined, set proper gain for the platform | ||
| if [ -z "$MODEL" ]; then | ||
| # treat as warning only | ||
| dlogw "NO MODEL is defined. Please define MODEL to run alsa_settings/MODEL.sh" | ||
| else | ||
| dlogi "apply alsa settings for alsa_settings/MODEL.sh" | ||
| set_alsa_settings "$MODEL" | ||
| fi |
There was a problem hiding this comment.
| # reset sof volume to 0dB | |
| reset_sof_volume | |
| # If MODEL is defined, set proper gain for the platform | |
| if [ -z "$MODEL" ]; then | |
| # treat as warning only | |
| dlogw "NO MODEL is defined. Please define MODEL to run alsa_settings/MODEL.sh" | |
| else | |
| dlogi "apply alsa settings for alsa_settings/MODEL.sh" | |
| set_alsa_settings "$MODEL" | |
| fi | |
| set_alsa() |
|
|
||
| local switch_name=$1 | ||
| local state=$2 | ||
|
|
There was a problem hiding this comment.
please add something like this to log the initial usbrelay status, to see all its relays, also to check it is runnable:
usbrelay --debug || die "usbrelay actual state read failed"
e547530 to
2d582c3
Compare
2d582c3 to
43b6d50
Compare
marc-hb
left a comment
There was a problem hiding this comment.
Code looks good, someone must review the actual audio logic.
case-lib/relay.sh
Outdated
| # param2: switch state | ||
| usbrelay_switch() | ||
| { | ||
| [[ "$1" == "--debug" ]] && { |
There was a problem hiding this comment.
| [[ "$1" == "--debug" ]] && { | |
| if [[ "$1" == "--debug" ]]; then { |
The && "shortcut" can sometimes save a couple lines but it does not save anything here. Also, && can be deceptive, for instance:
true && grep notfound /etc/passwd || echo 'ELSE is also run!!'
|| saves a negation and is less subtle than &&
See explanation in "Prefer || over && when possible" in #312
9e69829 to
d74ea44
Compare
|
This has the same relay.sh file as #1280 but it's not added in a separate commit. |
Add new test case to check hardware mic privacy mode enablement feature. For switching mic privacy state is using a USB relay. https://github.com/darrylb123/usbrelay Signed-off-by: Artur Wilczak <arturx.wilczak@intel.com>
The file is added in the separate PR 1280: thesofproject#1280 Signed-off-by: Artur Wilczak <arturx.wilczak@intel.com>
d74ea44 to
6ecdb54
Compare
The file is added in the separate PR 1278: thesofproject#1278 Signed-off-by: Artur Wilczak <arturx.wilczak@intel.com>
Add new test case to check hardware mic privacy mode enablement feature.
For switching mic privacy state is using a USB relay.
https://github.com/darrylb123/usbrelay