diff --git a/libutempter.spec b/libutempter.spec index 691ef42..e82960a 100644 --- a/libutempter.spec +++ b/libutempter.spec @@ -3,13 +3,15 @@ Summary: A privileged helper for utmp/wtmp updates Name: libutempter Version: 1.2.1 -Release: 2 +Release: 3 License: LGPLv2+ URL: https://github.com/altlinux/libutempter Source0: http://download.basealt.ru/pub/people/ldv/libutempter/%{name}-%{version}.tar.gz -BuildRequires: gcc +Patch0: test-libutempter.patch + +BuildRequires: gcc CUnit-devel Requires(pre): shadow-utils Provides: utempter = %{utempter_compat_ver} @@ -34,6 +36,9 @@ Files for development with %{name}. make CFLAGS="$RPM_OPT_FLAGS" LDFLAGS="$RPM_LD_FLAGS" \ libdir="%{_libdir}" libexecdir="%{_libexecdir}" +%check +make check + %install %make_install libdir="%{_libdir}" libexecdir="%{_libexecdir}" %delete_la_and_a @@ -59,6 +64,12 @@ make CFLAGS="$RPM_OPT_FLAGS" LDFLAGS="$RPM_LD_FLAGS" \ %{_mandir}/man3/* %changelog +* Thu Mar 10 2022 ziyangc2 -1.2.1-3 +- Type:enhancement +- ID:NA +- SUG:NA +- DESC:add test unit for libutempter + * Fri Nov 6 2020 yixiangzhike - 1.2.1-2 - Type:enhancement - ID:NA diff --git a/test-libutempter.patch b/test-libutempter.patch new file mode 100644 index 0000000..1eda07a --- /dev/null +++ b/test-libutempter.patch @@ -0,0 +1,226 @@ +From b42bfd09d2877edf563d1c80589b661c044ce82e Mon Sep 17 00:00:00 2001 +From: ziyangc2 +Date: Mon, 14 Mar 2022 14:32:51 +0800 +Subject: [PATCH] add cunit test for libutempter + +--- + Makefile | 15 +++++ + run_test.sh | 16 +++++ + test/test_utempter.c | 145 +++++++++++++++++++++++++++++++++++++++++++ + 3 files changed, 176 insertions(+) + create mode 100755 run_test.sh + create mode 100644 test/test_utempter.c + +diff --git a/Makefile b/Makefile +index 8c90121..39bc334 100644 +--- a/Makefile ++++ b/Makefile + PROJECT = utempter + VERSION = $(shell sed '/^Version: */!d;s///;q' libutempter.spec) + MAJOR = 0 + + SHAREDLIB = lib$(PROJECT).so + SONAME = $(SHAREDLIB).$(MAJOR) +@@ -55,6 +57,12 @@ override CFLAGS := $(WARNINGS) $(CFLAGS) $(RPM_OPT_FLAGS) + override LDFLAGS := $(LINK_RELRO) $(LINK_STATS) $(LDFLAGS) + LDLIBS = + ++INC = -I /usr/include/CUnit ++LIBS = -L -dynamics /usr/lib64/libcunit.so ./libutempter.so iface.o ++TEST_FLAGS =-g -lutil ++TEST_FLAGS += $(INC) ++BIN = test_utempter ++test_utempter = ./test/test_utempter.c + all: $(TARGETS) + + %.os: %.c +@@ -91,5 +99,12 @@ install: + ln -s $(PROJECT).3 $(DESTDIR)$(man3dir)/$$n.3; \ + done + ++check: ++ echo "run check" ++ $(CC) $(test_utempter) -o $(BIN) $(TEST_FLAGS) $(LIBS) ++ chmod +x run_test.sh ++ ./run_test.sh ++ + clean: + $(RM) $(TARGETS) iface.o iface.os core *~ ++ $(RM) $(RMFLAGS) $(BIN) *.o *.gcda *.gcno *.xml app.info +\ No newline at end of file +diff --git a/run_test.sh b/run_test.sh +new file mode 100755 +index 0000000..951fd62 +--- /dev/null ++++ b/run_test.sh +@@ -0,0 +1,16 @@ ++#!/bin/sh ++# prepare environment ++mkdir -p /usr/lib/utempter ++cp utempter /usr/lib/utempter/ ++ ++# run test ++./test_utempter ++ ++# clean environment ++tmp_file="wtmp.txt" ++utmpdump /var/log/wtmp > "${tmp_file}" ++sed -i '/test_libutempter/,$d' "${tmp_file}" ++ ++# restore wtmp file ++utmpdump -r < "${tmp_file}" > /var/log/wtmp ++rm -rf "${tmp_file}" /usr/lib/utempter +diff --git a/test/test_utempter.c b/test/test_utempter.c +new file mode 100644 +index 0000000..5cc88ab +--- /dev/null ++++ b/test/test_utempter.c +@@ -0,0 +1,145 @@ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include "../utempter.h" ++#include "CUnit.h" ++#include "Automated.h" ++ ++int check_update_utempter_success(const char * command) ++{ ++ FILE* wtmp_fd; ++ char buff[255]; ++ ++ system("utmpdump /var/log/wtmp > wtmp.txt"); ++ wtmp_fd = fopen("wtmp.txt", "r"); ++ if (wtmp_fd == NULL) { ++ printf("open wtmp failed, err = %d\n", errno); ++ return -1; ++ } ++ ++ while((fgets(buff, 255, wtmp_fd)) != NULL) { ++ continue; ++ } ++ fclose(wtmp_fd); ++ ++ if (strstr(buff, "test_libutempter") != NULL) { ++ // find test hostname, print success ++ if (strcmp(command, "add") == 0) { ++ printf("utempter add success\n"); ++ return 0; ++ } else { ++ printf("utempter add failed\n"); ++ return -1; ++ } ++ } else { ++ if (strcmp(command, "del") == 0) { ++ printf("utempter remove success\n"); ++ return 0; ++ } else { ++ printf("utempter remove failed\n"); ++ return -1; ++ } ++ } ++} ++ ++void utcase_test_add_remove() ++{ ++ int master; ++ int slave; ++ int err; ++ int SUCCESS = 1; ++ char pid[32]; ++ ++ err = openpty(&master, &slave, NULL, NULL, NULL); ++ if(0 > err) { ++ printf("Error: %s\n", strerror(err)); ++ return; ++ } ++ ++ err = utempter_add_record(master, "test_libutempter"); ++ printf("add_record result = %d\n", err); ++ CU_ASSERT(err == SUCCESS) ++ ++ err = check_update_utempter_success("add"); ++ CU_ASSERT(err == 0); ++ ++ err = utempter_remove_added_record(); ++ printf("remove_record result = %d\n", err); ++ ++ CU_ASSERT(err == SUCCESS); ++ err = check_update_utempter_success("del"); ++ CU_ASSERT(err == 0); ++ ++ close(slave); ++ close(master); ++} ++ ++void utcase_test_set_path() ++{ ++ utempter_set_helper("/usr/lib/utempter/utempter"); ++ return; ++} ++ ++static CU_TestInfo ut_cases[] = ++{ ++ {"case:test_set_path", utcase_test_set_path}, ++ {"case:test_add_remove", utcase_test_add_remove}, ++ CU_TEST_INFO_NULL, ++}; ++ ++int suite_init(void) ++{ ++ return 0; ++} ++ ++int suite_clean(void) ++{ ++ return 0; ++} ++ ++static CU_SuiteInfo ut_suites[] = ++{ ++ {"my_first_suite", suite_init, suite_clean, NULL, NULL, ut_cases}, ++ CU_SUITE_INFO_NULL, ++}; ++ ++int main() { ++ int rc = 0; ++ CU_ErrorCode err = CUE_SUCCESS; ++ ++ err = CU_initialize_registry(); ++ if (err != CUE_SUCCESS) { ++ fprintf(stderr, "failed to initialize registry, error %d", err); ++ rc = 1; ++ goto l_out; ++ } ++ ++ err = CU_register_suites(ut_suites); ++ if (err != CUE_SUCCESS) { ++ fprintf(stderr, "failed to register suites, error %d, %s", err, CU_get_error_msg()); ++ rc = 1; ++ goto l_clean_register; ++ } ++ ++ CU_set_output_filename("cunit_sample"); ++ ++ err = CU_list_tests_to_file(); ++ if (err != CUE_SUCCESS) { ++ fprintf(stderr, "failed to list tests to file, error %d, %s", err, CU_get_error_msg()); ++ rc = 1; ++ goto l_clean_register; ++ } ++ ++ CU_automated_run_tests(); ++ ++l_clean_register: ++ CU_cleanup_registry(); ++ ++l_out: ++ return rc; ++} +-- +2.23.0 +