Projects
Eulaceura:Factory
libutempter
_service:obs_scm:test-libutempter.patch
Sign Up
Log In
Username
Password
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File _service:obs_scm:test-libutempter.patch of Package libutempter
From b42bfd09d2877edf563d1c80589b661c044ce82e Mon Sep 17 00:00:00 2001 From: ziyangc2 <chenziyang4@huawei.com> 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 <stdio.h> +#include <stdlib.h> +#include <pty.h> +#include <unistd.h> +#include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#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
Locations
Projects
Search
Status Monitor
Help
Open Build Service
OBS Manuals
API Documentation
OBS Portal
Reporting a Bug
Contact
Mailing List
Forums
Chat (IRC)
Twitter
Open Build Service (OBS)
is an
openSUSE project
.
浙ICP备2022010568号-2