summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIker Pedrosa <ipedrosa@redhat.com>2023-09-14 12:47:04 +0200
committerIker Pedrosa <ikerpedrosam@gmail.com>2023-09-29 09:24:01 +0200
commit015448b049a748126589463b4366a1fbda909c15 (patch)
treec396c0e2b8df00cb534c4b59474e1579b94926ef
parent163c424999a3e097c6c6da87cc9df3041b4e03f6 (diff)
tests: happy path for active_sessions_count()
Simple test to check the recently implemented logind functionality. It also contains the changes to the build infrastructure, and the gitignore. Resolves: https://github.com/shadow-maint/shadow/issues/790 Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
-rw-r--r--Makefile.am2
-rw-r--r--tests/unit/.gitignore6
-rw-r--r--tests/unit/Makefile.am32
-rw-r--r--tests/unit/test_logind.c69
4 files changed, 108 insertions, 1 deletions
diff --git a/Makefile.am b/Makefile.am
index d8dfc3bf..31554e71 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -8,7 +8,7 @@ if ENABLE_SUBIDS
SUBDIRS += libsubid
endif
-SUBDIRS += src po contrib doc etc
+SUBDIRS += src po contrib doc etc tests/unit
if ENABLE_REGENERATE_MAN
SUBDIRS += man
diff --git a/tests/unit/.gitignore b/tests/unit/.gitignore
new file mode 100644
index 00000000..f331069e
--- /dev/null
+++ b/tests/unit/.gitignore
@@ -0,0 +1,6 @@
+# General files applicable to all test files
+*.log
+*.trs
+
+# Specific files to be added each time a new file is included
+test_logind \ No newline at end of file
diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am
new file mode 100644
index 00000000..30277b63
--- /dev/null
+++ b/tests/unit/Makefile.am
@@ -0,0 +1,32 @@
+AM_CPPFLAGS = -I$(top_srcdir)/lib -I$(top_srcdir)
+
+if HAVE_CMOCKA
+TESTS = $(check_PROGRAMS)
+
+check_PROGRAMS =
+
+if ENABLE_LOGIND
+check_PROGRAMS += \
+ test_logind
+endif # ENABLE_LOGIND
+
+check_PROGRAMS += \
+ $(NULL)
+
+test_logind_SOURCES = \
+ ../../lib/logind.c \
+ test_logind.c \
+ $(NULL)
+test_logind_CFLAGS = \
+ $(AM_CFLAGS) \
+ -lsystemd \
+ $(NULL)
+test_logind_LDFLAGS = \
+ -Wl,-wrap,prefix_getpwnam \
+ -Wl,-wrap,sd_uid_get_sessions \
+ $(NULL)
+test_logind_LDADD = \
+ $(CMOCKA_LIBS) \
+ $(LIBSYSTEMD) \
+ $(NULL)
+endif # HAVE_CMOCKA
diff --git a/tests/unit/test_logind.c b/tests/unit/test_logind.c
new file mode 100644
index 00000000..f91782ce
--- /dev/null
+++ b/tests/unit/test_logind.c
@@ -0,0 +1,69 @@
+/*
+ * SPDX-FileCopyrightText: 2023, Iker Pedrosa <ipedrosa@redhat.com>
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <pwd.h>
+#include <stdlib.h>
+#include <sys/types.h>
+
+#include "prototypes.h"
+
+/***********************
+ * WRAPPERS
+ **********************/
+struct passwd *
+__wrap_prefix_getpwnam(uid_t uid)
+{
+ return (struct passwd*) mock();
+}
+
+int
+__wrap_sd_uid_get_sessions(uid_t uid, int require_active, char ***sessions)
+{
+ return mock();
+}
+
+/***********************
+ * TEST
+ **********************/
+static void test_active_sessions_count_return_ok(void **state)
+{
+ int count;
+ struct passwd *pw = malloc(sizeof(struct passwd));
+
+ will_return(__wrap_prefix_getpwnam, pw);
+ will_return(__wrap_sd_uid_get_sessions, 1);
+
+ count = active_sessions_count("testuser", 0);
+
+ assert_int_equal(count, 1);
+}
+
+static void test_active_sessions_count_prefix_getpwnam_failure(void **state)
+{
+ int count;
+ struct passwd *pw = NULL;
+
+ will_return(__wrap_prefix_getpwnam, pw);
+
+ count = active_sessions_count("testuser", 0);
+
+ assert_int_equal(count, 0);
+}
+
+int main(void)
+{
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_active_sessions_count_return_ok),
+ cmocka_unit_test(test_active_sessions_count_prefix_getpwnam_failure),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}