From 876c155efb8aa399ef92ceb258a4aafa6f708cfe Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Fri, 6 Dec 2019 17:10:16 +0000 Subject: [PATCH] tests: Add pam_malcontent.so tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These tests check that the built `pam_malcontent.so` module can be loaded using `dlopen()` and that it exports the right symbol. This should mean that PAM can load it and use it. Unfortunately, we can’t actually run the module, since PAM hard-codes its configuration path as being in `/etc`, and there seems to be no way to override that to load a dummy configuration from a test directory. So the only way to test the PAM module is to use a file system bind mount to fake `/etc` (which requires privileges); or to actually install it on your system and integrate it into your real PAM configuration. Neither of those are acceptable for a unit test. It might be possible to re-execute a test under `bwrap` (if installed) to achieve this, bind mounting a dummy `/etc/pam.d/dummy` service file into the subprocess’ mount namespace, and otherwise bind mounting `/` to `/`. It would need a mock malcontent D-Bus API to talk to. Something to experiment with another time. (See `_pam_init_handlers()` in https://github.com/linux-pam/linux-pam/blob/master/libpam/pam_handlers.c for details of how PAM modules are loaded.) Signed-off-by: Philip Withnall --- pam/meson.build | 2 ++ pam/tests/meson.build | 48 +++++++++++++++++++++++++++++ pam/tests/pam_malcontent.c | 62 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 pam/tests/meson.build create mode 100644 pam/tests/pam_malcontent.c diff --git a/pam/meson.build b/pam/meson.build index f6a516f..8d3992d 100644 --- a/pam/meson.build +++ b/pam/meson.build @@ -20,3 +20,5 @@ pam_malcontent = shared_library('pam_malcontent', include_directories: root_inc, install: true, install_dir: pamlibdir) + +subdir('tests') diff --git a/pam/tests/meson.build b/pam/tests/meson.build new file mode 100644 index 0000000..0560dcb --- /dev/null +++ b/pam/tests/meson.build @@ -0,0 +1,48 @@ +deps = [ + dependency('glib-2.0', version: '>= 2.60.0'), + cc.find_library('dl'), +] + +envs = test_env + [ + 'G_TEST_SRCDIR=' + meson.current_source_dir(), + 'G_TEST_BUILDDIR=' + meson.current_build_dir(), +] + +test_programs = [ + ['pam_malcontent', [], deps], +] + +installed_tests_metadir = join_paths(datadir, 'installed-tests', + 'libmalcontent-' + libmalcontent_api_version) +installed_tests_execdir = join_paths(libexecdir, 'installed-tests', + 'libmalcontent-' + libmalcontent_api_version) + +foreach program: test_programs + test_conf = configuration_data() + test_conf.set('installed_tests_dir', installed_tests_execdir) + test_conf.set('program', program[0]) + + configure_file( + input: test_template, + output: program[0] + '.test', + install: enable_installed_tests, + install_dir: installed_tests_metadir, + configuration: test_conf, + ) + + exe = executable( + program[0], + [program[0] + '.c'] + program[1], + dependencies: program[2], + include_directories: root_inc, + install: enable_installed_tests, + install_dir: installed_tests_execdir, + ) + + test( + program[0], + exe, + env: envs, + args: ['--tap'], + ) +endforeach diff --git a/pam/tests/pam_malcontent.c b/pam/tests/pam_malcontent.c new file mode 100644 index 0000000..871516b --- /dev/null +++ b/pam/tests/pam_malcontent.c @@ -0,0 +1,62 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright © 2019 Endless Mobile, Inc. + * + * 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 + * + * Authors: + * - Philip Withnall + */ + +#include +#include +#include +#include + +/* Test that the `pam_malcontent.so` module can be loaded using dlopen() and + * that it exports the appropriate symbols for PAM to be able to use it. */ +static void +test_pam_malcontent_dlopen (void) +{ + g_autofree gchar *module_path = NULL; + void *handle; + int retval; + void *fn; + + module_path = g_test_build_filename (G_TEST_BUILT, "..", "pam_malcontent.so", NULL); + + /* Check the module can be loaded. */ + handle = dlopen (module_path, RTLD_NOW); + g_assert_nonnull (handle); + + /* Check the appropriate symbols exist. */ + fn = dlsym (handle, "pam_sm_acct_mgmt"); + g_assert_nonnull (fn); + + retval = dlclose (handle); + g_assert_cmpint (retval, ==, 0); +} + +int +main (int argc, + char **argv) +{ + setlocale (LC_ALL, ""); + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/pam_malcontent/dlopen", test_pam_malcontent_dlopen); + + return g_test_run (); +}