From bc331ca133a7f718efde2d59bf58d78e480263c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=ADcolas=20F=2E=20R=2E=20A=2E=20Prado?= Date: Tue, 5 Sep 2023 15:20:37 -0400 Subject: [PATCH] helpers: Introduce assert_usb_probed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Current bootrr helpers, assert_device_present and assert_driver_present, use driver and device names, both of which are not part of the kernel's stable ABI and end up requiring extra maintenance whenever those names are changed, in order to check for the expected name on each kernel version. Introduce a new helper, assert_usb_probed, that verifies a USB device has been probed, taking as parameters the device's hardware identifying properties, which are documented kernel ABI: idVendor, idProduct, bcdDevice, bDeviceClass, bDeviceSubClass, bDeviceProtocol, bInterfaceClass, bInterfaceSubClass, bInterfaceProtocol, bInterfaceNumber. A 'count' parameter is also required, to inform the number of devices matching these criteria that should have been probed. This allows the test to verify the probe of multiple identical devices. A '*' can be used as wildcard for any of the matching fields as necessary. Signed-off-by: NĂ­colas F. R. A. Prado --- helpers/assert_usb_probed | 61 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 helpers/assert_usb_probed diff --git a/helpers/assert_usb_probed b/helpers/assert_usb_probed new file mode 100755 index 0000000..69288b5 --- /dev/null +++ b/helpers/assert_usb_probed @@ -0,0 +1,61 @@ +#!/bin/sh + +. bootrr + +TEST_CASE_ID="$1" +COUNT="$2" +VENDOR="$3" +PRODUCT="$4" +DEVICE="$5" +DEV_CLASS="$6" +DEV_SUBCLASS="$7" +DEV_PROTOCOL="$8" +INTF_CLASS="$9" +INTF_SUBCLASS="${10}" +INTF_PROTOCOL="${11}" +INTF_NUM="${12}" + +if [ -z "${TEST_CASE_ID}" -o -z "${COUNT}" -o -z "${VENDOR}" ]; then + echo "Usage: $0 [ ]" + echo "Note: '*' can be used as wildcard for any field" + exit 1 +fi + +match() +{ + FILE="$1" + ID="$2" + + [ ! -f "$FILE" ] && return 1 + [ "$ID" = "*" -o "$ID" = "" ] && return 0 + [ $(cat "$FILE") != "$ID" ] && return 1 + return 0 +} + +cur_count=0 + +for dev in $(find /sys/bus/usb/devices -maxdepth 1); do + match "$dev"/idVendor "$VENDOR" || continue + match "$dev"/idProduct "$PRODUCT" || continue + match "$dev"/bcdDevice "$DEVICE" || continue + match "$dev"/bDeviceClass "$DEV_CLASS" || continue + match "$dev"/bDeviceSubClass "$DEV_SUBCLASS" || continue + match "$dev"/bDeviceProtocol "$DEV_PROTOCOL" || continue + + # Matched device. Now search through interfaces + for intf in $(find "$dev"/ -maxdepth 1 -type d); do + match "$intf"/bInterfaceClass "$INTF_CLASS" || continue + match "$intf"/bInterfaceSubClass "$INTF_SUBCLASS" || continue + match "$intf"/bInterfaceProtocol "$INTF_PROTOCOL" || continue + match "$intf"/bInterfaceNumber "$INTF_NUM" || continue + + # Matched interface. Add to count if it was probed by driver. + [ -d "$intf"/driver ] && cur_count=$((cur_count+1)) + done +done + +if [ "$cur_count" -eq "$COUNT" ]; then + test_report_exit pass +else + test_report_exit fail +fi