diff --git a/tests/meson.build b/tests/meson.build index 61decd5..f15bd8a 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -33,43 +33,51 @@ drivers_tests = [ if get_option('introspection') envs.prepend('GI_TYPELIB_PATH', join_paths(meson.build_root(), 'libfprint')) + virtual_devices_tests = [ + 'virtual-image', + 'virtual-device', + ] - if 'virtual_image' in drivers - python3 = find_program('python3') - unittest_inspector = find_program('unittest_inspector.py') - base_args = files('virtual-image.py') - suite = [] + unittest_inspector = find_program('unittest_inspector.py') - r = run_command(unittest_inspector, files('virtual-image.py')) - unit_tests = r.stdout().strip().split('\n') + foreach vdtest: virtual_devices_tests + driver_name = '_'.join(vdtest.split('-')) + if driver_name in drivers + python3 = find_program('python3') + base_args = files(vdtest + '.py') + suite = ['virtual-driver'] - if r.returncode() == 0 and unit_tests.length() > 0 - suite += 'virtual-image' - else - unit_tests = ['virtual-image'] - endif + r = run_command(unittest_inspector, files(vdtest + '.py')) + unit_tests = r.stdout().strip().split('\n') - foreach ut: unit_tests - ut_suite = suite - ut_args = base_args - if unit_tests.length() > 1 - ut_args += ut - ut_suite += ut.split('.')[0] + if r.returncode() == 0 and unit_tests.length() > 0 + suite += vdtest + else + unit_tests = [vdtest] endif - test(ut, - python3, - args: ut_args, - suite: ut_suite, - depends: libfprint_typelib, - env: envs, + + foreach ut: unit_tests + ut_suite = suite + ut_args = base_args + if unit_tests.length() > 1 + ut_args += ut + ut_suite += ut.split('.')[0] + endif + test(ut, + python3, + args: ut_args, + suite: ut_suite, + depends: libfprint_typelib, + env: envs, + ) + endforeach + else + test(vdtest, + find_program('sh'), + args: ['-c', 'exit 77'] ) - endforeach - else - test('virtual-image', - find_program('sh'), - args: ['-c', 'exit 77'] - ) - endif + endif + endforeach foreach driver_test: drivers_tests driver_envs = envs diff --git a/tests/virtual-device.py b/tests/virtual-device.py new file mode 100644 index 0000000..8d15bfd --- /dev/null +++ b/tests/virtual-device.py @@ -0,0 +1,247 @@ +#!/usr/bin/env python3 + +import sys +try: + import gi + import re + import os + + from gi.repository import GLib, Gio + + import unittest + import socket + import struct + import subprocess + import shutil + import glob + import tempfile +except Exception as e: + print("Missing dependencies: %s" % str(e)) + sys.exit(77) + +FPrint = None + +# Re-run the test with the passed wrapper if set +wrapper = os.getenv('LIBFPRINT_TEST_WRAPPER') +if wrapper: + wrap_cmd = wrapper.split(' ') + [sys.executable, os.path.abspath(__file__)] + \ + sys.argv[1:] + os.unsetenv('LIBFPRINT_TEST_WRAPPER') + sys.exit(subprocess.check_call(wrap_cmd)) + +ctx = GLib.main_context_default() + + +class Connection: + + def __init__(self, addr): + self.addr = addr + + def __enter__(self): + self.con = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + self.con.connect(self.addr) + return self.con + + def __exit__(self, exc_type, exc_val, exc_tb): + self.con.close() + del self.con + +class VirtualDevice(unittest.TestCase): + + @classmethod + def setUpClass(cls): + unittest.TestCase.setUpClass() + cls.tmpdir = tempfile.mkdtemp(prefix='libfprint-') + + driver_name = cls.driver_name if hasattr(cls, 'driver_name') else None + if not driver_name: + driver_name = re.compile(r'(?