Skip to content

Commit f7d7584

Browse files
PucikaBiubiubiuziyangc2
authored andcommitted
Add check to Makefile, check compile test_utempter.c
1 parent c26cb88 commit f7d7584

File tree

4 files changed

+180
-2
lines changed

4 files changed

+180
-2
lines changed

libutempter/Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ override CFLAGS := $(WARNINGS) $(CFLAGS) $(RPM_OPT_FLAGS)
5555
override LDFLAGS := $(LINK_RELRO) $(LINK_STATS) $(LDFLAGS)
5656
LDLIBS =
5757

58+
INC = -I /usr/include/CUnit
59+
LIBS = -L -dynamics /usr/lib64/libcunit.so ./libutempter.so iface.o
60+
TEST_FLAGS = -g -lutil
61+
TEST_FLAGS += $(INC)
62+
BIN = test_utempter
63+
test_utempter = ./test/test_utempter.c
5864
all: $(TARGETS)
5965

6066
%.os: %.c
@@ -91,5 +97,12 @@ install:
9197
ln -s $(PROJECT).3 $(DESTDIR)$(man3dir)/$$n.3; \
9298
done
9399

100+
check:
101+
echo "run check"
102+
$(CC) $(test_utempter) -o $(BIN) $(TEST_FLAGS) $(LIBS)
103+
chmod +x run_test.sh
104+
./run_test.sh
105+
94106
clean:
95107
$(RM) $(TARGETS) iface.o iface.os core *~
108+
$(RM) $(BIN) *.o *.gcda *.gcno *.xml app.info

libutempter/libutempter.spec

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Name: libutempter
2-
Version: 1.2.1
3-
Release: alt1
2+
Version: 1.2.2
3+
Release: zc1
44

55
Summary: A privileged helper for utmp/wtmp updates
66
License: LGPLv2+
@@ -69,6 +69,10 @@ statically linked utempter-based software.
6969
%_libdir/*.a
7070

7171
%changelog
72+
* Wed Mar 16 2022 Ziyang Chen <chenziyang76@163.com> 1.2.2-zc1
73+
- utempter: add cunit test for libutempter. Specifically add 2 tests
74+
to verify function of libutempter interface.
75+
7276
* Mon Jul 06 2020 Dmitry V. Levin <ldv@altlinux.org> 1.2.1-alt1
7377
- utempter: relaxed host argument validation: it is now allowed
7478
to contain spaces except in the first character.

libutempter/run_test.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/sh
2+
# prepare environment
3+
mkdir -p /usr/lib/utempter
4+
cp utempter /usr/lib/utempter/
5+
6+
# run test
7+
./test_utempter
8+
9+
# clean environment
10+
tmp_file="wtmp.txt"
11+
utmpdump /var/log/wtmp > "${tmp_file}"
12+
sed -i '/test_libutempter/,$d' "${tmp_file}"
13+
14+
# restore wtmp file
15+
utmpdump -r < "${tmp_file}" > /var/log/wtmp
16+
rm -rf "${tmp_file}" /usr/lib/utempter

libutempter/test/test_utempter.c

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <pty.h>
4+
#include <unistd.h>
5+
#include <string.h>
6+
#include <sys/types.h>
7+
#include <sys/stat.h>
8+
#include <fcntl.h>
9+
#include "../utempter.h"
10+
#include "CUnit.h"
11+
#include "Automated.h"
12+
13+
int check_update_utempter_success(const char * command)
14+
{
15+
FILE* wtmp_fd;
16+
char buff[255];
17+
18+
system("utmpdump /var/log/wtmp > wtmp.txt");
19+
wtmp_fd = fopen("wtmp.txt", "r");
20+
if (wtmp_fd == NULL) {
21+
printf("open wtmp failed, err = %d\n", errno);
22+
return -1;
23+
}
24+
25+
while((fgets(buff, 255, wtmp_fd)) != NULL) {
26+
continue;
27+
}
28+
fclose(wtmp_fd);
29+
30+
if (strstr(buff, "test_libutempter") != NULL) {
31+
// find test hostname, print success
32+
if (strcmp(command, "add") == 0) {
33+
printf("utempter add success\n");
34+
return 0;
35+
} else {
36+
printf("utempter add failed\n");
37+
return -1;
38+
}
39+
} else {
40+
if (strcmp(command, "del") == 0) {
41+
printf("utempter remove success\n");
42+
return 0;
43+
} else {
44+
printf("utempter remove failed\n");
45+
return -1;
46+
}
47+
}
48+
}
49+
50+
void utcase_test_add_remove()
51+
{
52+
int master;
53+
int slave;
54+
int err;
55+
int SUCCESS = 1;
56+
char pid[32];
57+
58+
err = openpty(&master, &slave, NULL, NULL, NULL);
59+
if(0 > err) {
60+
printf("Error: %s\n", strerror(err));
61+
return;
62+
}
63+
64+
err = utempter_add_record(master, "test_libutempter");
65+
printf("add_record result = %d\n", err);
66+
CU_ASSERT(err == SUCCESS)
67+
68+
err = check_update_utempter_success("add");
69+
CU_ASSERT(err == 0);
70+
71+
err = utempter_remove_added_record();
72+
printf("remove_record result = %d\n", err);
73+
74+
CU_ASSERT(err == SUCCESS);
75+
err = check_update_utempter_success("del");
76+
CU_ASSERT(err == 0);
77+
78+
close(slave);
79+
close(master);
80+
}
81+
82+
void utcase_test_set_path()
83+
{
84+
utempter_set_helper("/usr/lib/utempter/utempter");
85+
return;
86+
}
87+
88+
static CU_TestInfo ut_cases[] =
89+
{
90+
{"case:test_set_path", utcase_test_set_path},
91+
{"case:test_add_remove", utcase_test_add_remove},
92+
CU_TEST_INFO_NULL,
93+
};
94+
95+
int suite_init(void)
96+
{
97+
return 0;
98+
}
99+
100+
int suite_clean(void)
101+
{
102+
return 0;
103+
}
104+
105+
static CU_SuiteInfo ut_suites[] =
106+
{
107+
{"my_first_suite", suite_init, suite_clean, NULL, NULL, ut_cases},
108+
CU_SUITE_INFO_NULL,
109+
};
110+
111+
int main() {
112+
int rc = 0;
113+
CU_ErrorCode err = CUE_SUCCESS;
114+
115+
err = CU_initialize_registry();
116+
if (err != CUE_SUCCESS) {
117+
fprintf(stderr, "failed to initialize registry, error %d", err);
118+
rc = 1;
119+
goto l_out;
120+
}
121+
122+
err = CU_register_suites(ut_suites);
123+
if (err != CUE_SUCCESS) {
124+
fprintf(stderr, "failed to register suites, error %d, %s", err, CU_get_error_msg());
125+
rc = 1;
126+
goto l_clean_register;
127+
}
128+
129+
CU_set_output_filename("cunit_sample");
130+
131+
err = CU_list_tests_to_file();
132+
if (err != CUE_SUCCESS) {
133+
fprintf(stderr, "failed to list tests to file, error %d, %s", err, CU_get_error_msg());
134+
rc = 1;
135+
goto l_clean_register;
136+
}
137+
138+
CU_automated_run_tests();
139+
140+
l_clean_register:
141+
CU_cleanup_registry();
142+
143+
l_out:
144+
return rc;
145+
}

0 commit comments

Comments
 (0)