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 (); +}