Skip to content

Commit

Permalink
Add check to Makefile, check compile test_utempter.c
Browse files Browse the repository at this point in the history
  • Loading branch information
PucikaBiubiubiu authored and ziyangc2 committed Mar 22, 2022
1 parent c26cb88 commit f7d7584
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 2 deletions.
13 changes: 13 additions & 0 deletions libutempter/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
8 changes: 6 additions & 2 deletions libutempter/libutempter.spec
Original file line number Diff line number Diff line change
@@ -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+
Expand Down Expand Up @@ -69,6 +69,10 @@ statically linked utempter-based software.
%_libdir/*.a

%changelog
* Wed Mar 16 2022 Ziyang Chen <[email protected]> 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 <[email protected]> 1.2.1-alt1
- utempter: relaxed host argument validation: it is now allowed
to contain spaces except in the first character.
Expand Down
16 changes: 16 additions & 0 deletions libutempter/run_test.sh
Original file line number Diff line number Diff line change
@@ -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
145 changes: 145 additions & 0 deletions libutempter/test/test_utempter.c
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit f7d7584

Please sign in to comment.