tests: Add pam_malcontent.so tests

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 <withnall@endlessm.com>
This commit is contained in:
Philip Withnall 2019-12-06 17:10:16 +00:00
parent bd7b17ffd4
commit 876c155efb
3 changed files with 112 additions and 0 deletions

View File

@ -20,3 +20,5 @@ pam_malcontent = shared_library('pam_malcontent',
include_directories: root_inc,
install: true,
install_dir: pamlibdir)
subdir('tests')

48
pam/tests/meson.build Normal file
View File

@ -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

View File

@ -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 <withnall@endlessm.com>
*/
#include <dlfcn.h>
#include <glib.h>
#include <locale.h>
#include <stdlib.h>
/* 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 ();
}