fpi-device: Only use emulation mode by pre-loading a library

Rather than just do it in production if set, make it more difficult to
potentially inject the test mode.

So rely on a weak definition of fpi_device_emulation_mode_enabled() that
can be only set externally
This commit is contained in:
Marco Trevisan (Treviño)
2026-06-19 15:50:18 +02:00
parent 98fb001883
commit 3933e5d468
4 changed files with 59 additions and 19 deletions
+4 -17
View File
@@ -55,28 +55,15 @@ fp_device_get_instance_private (FpDevice *self)
* @device: The #FpDevice to check * @device: The #FpDevice to check
* *
* Checks if the device is running in emulation mode, which is enabled by * Checks if the device is running in emulation mode, which is enabled by
* setting the FP_DEVICE_EMULATION environment variable to a '1' value. * setting the FP_DEVICE_EMULATION environment variable to a '1' value but
* only when the test emulation library is pre-loaded.
* This is used by some drivers to enable special behavior for testing * This is used by some drivers to enable special behavior for testing
* and development purposes. * and development purposes.
*/ */
gboolean __attribute__((weak)) gboolean
(fpi_device_emulation_mode_enabled) (FpDevice *device) (fpi_device_emulation_mode_enabled) (FpDevice *device)
{ {
static gsize emulation_mode = 0; return FALSE;
/* FIXME: Add some build constraints to make sure this cannot ever be enabled
* in production, outside in the installed tests case.
*/
/* This is a global value for now, but ideally we may support a different
* value per device.
*/
if (g_once_init_enter (&emulation_mode))
g_once_init_leave (&emulation_mode,
g_strcmp0 (g_getenv ("FP_DEVICE_EMULATION"), "1") == 0 ?
TRUE : G_MAXSIZE);
return emulation_mode == TRUE;
} }
/** /**
+1
View File
@@ -1,6 +1,7 @@
LIBFPRINT_2.0.0 { LIBFPRINT_2.0.0 {
global: global:
fp_*; fp_*;
fpi_device_emulation_mode_enabled;
local: local:
*; *;
}; };
+22 -2
View File
@@ -74,6 +74,18 @@ if get_option('introspection')
output: 'create-driver-test.py') output: 'create-driver-test.py')
endif endif
test_emulation_lib = shared_library('fprint-test-emulation',
'test-emulation.c',
dependencies: libfprint_private_dep,
install: installed_tests,
install_dir: installed_tests_execdir,
)
test_emulation_lib_name = fs.name(test_emulation_lib.full_path())
# Preload the test-emulation library so that the weak NULL implementation
# in libfprint is overridden with one that honours FP_DEVICE_EMULATION.
envs.prepend('LD_PRELOAD', test_emulation_lib.full_path())
env_parser_cmd = ''' env_parser_cmd = '''
import os; import os;
print(" ".join([f"{k}={v}" for k, v in os.environ.items() print(" ".join([f"{k}={v}" for k, v in os.environ.items()
@@ -86,6 +98,7 @@ envs_str = run_command(python3, '-c', env_parser_cmd,
envs_str = ' '.join([ envs_str = ' '.join([
envs_str, envs_str,
'LD_PRELOAD=' + (installed_tests_execdir / test_emulation_lib_name),
'G_TEST_SRCDIR=' + installed_tests_testdir, 'G_TEST_SRCDIR=' + installed_tests_testdir,
'G_TEST_BUILDDIR=' + installed_tests_execdir, 'G_TEST_BUILDDIR=' + installed_tests_execdir,
]) ])
@@ -133,7 +146,10 @@ if get_option('introspection')
python3, python3,
args: ut_args, args: ut_args,
suite: ut_suite, suite: ut_suite,
depends: libfprint_typelib, depends: [
libfprint_typelib,
test_emulation_lib,
],
env: envs, env: envs,
) )
endforeach endforeach
@@ -187,7 +203,10 @@ if get_option('introspection')
env: driver_envs, env: driver_envs,
suite: ['drivers'], suite: ['drivers'],
timeout: args.get('timeout', 15), timeout: args.get('timeout', 15),
depends: libfprint_typelib, depends: [
libfprint_typelib,
test_emulation_lib,
],
) )
if installed_tests if installed_tests
@@ -206,6 +225,7 @@ if get_option('introspection')
'driver_test': driver_test, 'driver_test': driver_test,
'driver_env': ' '.join([ 'driver_env': ' '.join([
driver_envs_str, driver_envs_str,
'LD_PRELOAD=' + (installed_tests_execdir / test_emulation_lib_name),
'LD_LIBRARY_PATH=' + installed_tests_libdir, 'LD_LIBRARY_PATH=' + installed_tests_libdir,
# FIXME: Adding this requires gnome-desktop-testing!12 # FIXME: Adding this requires gnome-desktop-testing!12
# 'GI_TYPELIB_PATH=' + installed_tests_libdir / 'girepository-1.0', # 'GI_TYPELIB_PATH=' + installed_tests_libdir / 'girepository-1.0',
+32
View File
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2026 Canonical, Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "fpi-device.h"
gboolean
(fpi_device_emulation_mode_enabled) (FpDevice *device)
{
static gsize emulation_mode = 0;
if (g_once_init_enter (&emulation_mode))
g_once_init_leave (&emulation_mode,
g_strcmp0 (g_getenv ("FP_DEVICE_EMULATION"), "1") == 0 ?
TRUE : G_MAXSIZE);
return emulation_mode == TRUE;
}