tests/create-driver-test: Use argparse and allow to record only one case

This commit is contained in:
Marco Trevisan (Treviño)
2026-07-23 14:36:02 +02:00
parent 71cff7966e
commit c02d58a369
2 changed files with 33 additions and 30 deletions
+7 -4
View File
@@ -14,9 +14,9 @@ script, capture it and store the capture to `custom.pcapng`.
'capture' and 'custom' Test Creation 'capture' and 'custom' Test Creation
------------------------------------ ------------------------------------
For image devices the `capture.py` script will be used to capture one reference For image devices, use the `capture` test to capture one reference image. For
image. If the driver is a non-image driver, then a `custom.py` script should be non-image drivers, create a `custom.py` script in advance and select the
created in advance, which will be run instead. `custom` test instead.
1. Make sure that libfprint is built with support for the device driver 1. Make sure that libfprint is built with support for the device driver
that you want to create a test case for. that you want to create a test case for.
@@ -26,9 +26,12 @@ created in advance, which will be run instead.
but the hardware is slightly different, you might want to pass a variant but the hardware is slightly different, you might want to pass a variant
name as a command-line options, for example: name as a command-line options, for example:
```sh ```sh
$ sudo tests/create-driver-test.py driver [variant] $ sudo tests/create-driver-test.py [--test capture|custom] driver [variant]
``` ```
By default, the tool runs `capture.py` and, when present, the test directory's
`custom.py`. Use `--test capture` or `--test custom` to record only that test.
3. If the capture is not successful, run the tool again to start another capture. 3. If the capture is not successful, run the tool again to start another capture.
4. Add driver test name to `drivers_tests` in the `meson.build`, as instructed, 4. Add driver test name to `drivers_tests` in the `meson.build`, as instructed,
+23 -23
View File
@@ -3,9 +3,10 @@
BUILDDIR='@BUILDDIR@' BUILDDIR='@BUILDDIR@'
SRCDIR='@SRCDIR@' SRCDIR='@SRCDIR@'
import signal
import argparse
import os import os
import sys import sys
import signal
library_path = BUILDDIR + '/libfprint/' library_path = BUILDDIR + '/libfprint/'
# Relaunch ourselves with a changed environment so # Relaunch ourselves with a changed environment so
@@ -27,35 +28,31 @@ from gi.repository import FPrint
gi.require_version('GUsb', '1.0') gi.require_version('GUsb', '1.0')
from gi.repository import GUsb from gi.repository import GUsb
import re
import shutil import shutil
import subprocess import subprocess
import tempfile import tempfile
import time import time
def print_usage(): def test_variant(value):
print(f'Usage: {sys.argv[0]} driver [test-variant-name]') if (not value or any(not (char.islower() or char == '-') for char in value) or
print('A test variant name is optional, and must be all lower case letters, or dashes, with no spaces') value.startswith('-') or value.endswith('-')):
print(f'The captured data will be stored in {SRCDIR}/tests/[driver name]-[test variant name]') raise argparse.ArgumentTypeError(
print(f'Create custom.py prior to execution for non image device tests.') 'must contain only lowercase letters and dashes')
return value
if len(sys.argv) > 3: parser = argparse.ArgumentParser(
print_usage() description='Capture USB traffic for a libfprint driver test.')
sys.exit(1) parser.add_argument('driver')
parser.add_argument('variant', nargs='?', type=test_variant,
help='Optional, lowercase test variant name')
parser.add_argument('--test', choices=('capture', 'custom'),
help='Run only the selected test (default: run both)')
options = parser.parse_args()
driver_name = sys.argv[1] driver_name = options.driver
os.environ['FP_DRIVERS_ALLOWLIST'] = driver_name os.environ['FP_DRIVERS_ALLOWLIST'] = driver_name
test_variant = None test_variant = options.variant
if len(sys.argv) == 3:
valid_re = re.compile('[a-z-]*')
test_variant = sys.argv[2]
if (not valid_re.match(test_variant) or
test_variant.startswith('-') or
test_variant.endswith('-')):
print(f'Invalid variant name {test_variant}\n')
print_usage()
sys.exit(1)
# Check that running as root # Check that running as root
@@ -94,6 +91,9 @@ print(f'### Detected USB device /dev/bus/usb/{bus_num:03d}/{device_num:03d}')
test_dir = SRCDIR + '/tests/' + test_name test_dir = SRCDIR + '/tests/' + test_name
os.makedirs(test_dir, mode=0o775, exist_ok=True) os.makedirs(test_dir, mode=0o775, exist_ok=True)
custom_script = os.path.join(test_dir, 'custom.py')
if options.test == 'custom' and not os.path.exists(custom_script):
parser.error(f'custom test script does not exist: {custom_script}')
# Capture device info # Capture device info
@@ -168,12 +168,12 @@ def t_waitpid(pid, timeout):
return r return r
if options.test in (None, 'capture'):
capture_test([SRCDIR + '/tests/capture.py', test_dir + '/capture.png'], capture_test([SRCDIR + '/tests/capture.py', test_dir + '/capture.png'],
'capture.pcapng', 'capture.pcapng',
'### Capturing fingerprint, please swipe or press your finger on the reader') '### Capturing fingerprint, please swipe or press your finger on the reader')
custom_script = os.path.join(test_dir, 'custom.py') if options.test in (None, 'custom') and os.path.exists(custom_script):
if os.path.exists(custom_script):
capture_test([custom_script], 'custom.pcapng', capture_test([custom_script], 'custom.pcapng',
'### Running the custom fingerprint capture') '### Running the custom fingerprint capture')