From f7d75840e100570903a96122266653c87c05c4a0 Mon Sep 17 00:00:00 2001 From: PucikaBiubiubiu <101719124+PucikaBiubiubiu@users.noreply.github.com> Date: Wed, 16 Mar 2022 20:56:19 +0800 Subject: [PATCH] Add check to Makefile, check compile test_utempter.c --- libutempter/Makefile | 13 +++ libutempter/libutempter.spec | 8 +- libutempter/run_test.sh | 16 ++++ libutempter/test/test_utempter.c | 145 +++++++++++++++++++++++++++++++ 4 files changed, 180 insertions(+), 2 deletions(-) create mode 100755 libutempter/run_test.sh create mode 100644 libutempter/test/test_utempter.c diff --git a/libutempter/Makefile b/libutempter/Makefile index 8c90121..eb19a80 100644 --- a/libutempter/Makefile +++ b/libutempter/Makefile @@ -55,6 +55,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 +97,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) $(BIN) *.o *.gcda *.gcno *.xml app.info diff --git a/libutempter/libutempter.spec b/libutempter/libutempter.spec index 6360072..6014961 100644 --- a/libutempter/libutempter.spec +++ b/libutempter/libutempter.spec @@ -1,6 +1,6 @@ Name: libutempter -Version: 1.2.1 -Release: alt1 +Version: 1.2.2 +Release: zc1 Summary: A privileged helper for utmp/wtmp updates License: LGPLv2+ @@ -69,6 +69,10 @@ statically linked utempter-based software. %_libdir/*.a %changelog +* Wed Mar 16 2022 Ziyang Chen 1.2.2-zc1 +- utempter: add cunit test for libutempter. Specifically add 2 tests + to verify function of libutempter interface. + * Mon Jul 06 2020 Dmitry V. Levin 1.2.1-alt1 - utempter: relaxed host argument validation: it is now allowed to contain spaces except in the first character. diff --git a/libutempter/run_test.sh b/libutempter/run_test.sh new file mode 100755 index 0000000..951fd62 --- /dev/null +++ b/libutempter/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/libutempter/test/test_utempter.c b/libutempter/test/test_utempter.c new file mode 100644 index 0000000..5cc88ab --- /dev/null +++ b/libutempter/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; +}