Skip to content

Commit 535cc03

Browse files
committed
Add tests for re-entrant functions
Created fgets_r_test.c, which verifies initialization of state, custom buffer handling with auplugin_setvbuf_r, and EOF detection.
1 parent 52591f7 commit 535cc03

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

auplugin/test/Makefile.am

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
AM_CPPFLAGS = -I${top_srcdir}/auplugin -I${top_srcdir}/lib -I${top_srcdir}/auparse
2424
AM_CFLAGS = -D_GNU_SOURCE -Wno-pointer-sign ${WFLAGS}
25-
check_PROGRAMS = fgets_test metrics_test
25+
check_PROGRAMS = fgets_test metrics_test fgets_r_test
2626
TESTS = $(check_PROGRAMS)
2727

2828
fgets_test_LDADD = ${top_builddir}/auplugin/libauplugin.la
@@ -31,3 +31,6 @@ fgets_test_DEPENDENCIES = ${top_builddir}/auplugin/libauplugin.la
3131
metrics_test_LDADD = ${top_builddir}/auplugin/libauplugin.la
3232
metrics_test_DEPENDENCIES = ${top_builddir}/auplugin/libauplugin.la
3333

34+
fgets_r_test_LDADD = ${top_builddir}/auplugin/libauplugin.la
35+
fgets_r_test_DEPENDENCIES = ${top_builddir}/auplugin/libauplugin.la
36+

auplugin/test/fgets_r_test.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <assert.h>
2+
#include <string.h>
3+
#include <unistd.h>
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <auplugin.h>
7+
8+
static void test_basic_state(void)
9+
{
10+
int fds[2];
11+
char buf[16];
12+
char custom[32];
13+
auplugin_fgets_state_t *st;
14+
15+
assert(pipe(fds) == 0);
16+
17+
st = auplugin_fgets_init();
18+
assert(st);
19+
assert(auplugin_setvbuf_r(st, custom, sizeof(custom), MEM_SELF_MANAGED) == 0);
20+
21+
/* no data yet */
22+
assert(auplugin_fgets_more_r(st, sizeof(buf)) == 0);
23+
assert(auplugin_fgets_eof_r(st) == 0);
24+
25+
const char *input = "hi\n";
26+
assert(write(fds[1], input, strlen(input)));
27+
close(fds[1]);
28+
29+
/* read the line */
30+
int len = auplugin_fgets_r(st, buf, sizeof(buf), fds[0]);
31+
assert(len == 3);
32+
assert(strcmp(buf, "hi\n") == 0);
33+
34+
/* EOF on next call */
35+
len = auplugin_fgets_r(st, buf, sizeof(buf), fds[0]);
36+
assert(len == 0);
37+
assert(auplugin_fgets_eof_r(st) == 1);
38+
39+
auplugin_fgets_clear_r(st);
40+
close(fds[0]);
41+
auplugin_fgets_destroy(st);
42+
}
43+
44+
int main(void)
45+
{
46+
test_basic_state();
47+
printf("audit-fgets_r tests: all passed\n");
48+
return 0;
49+
}
50+

0 commit comments

Comments
 (0)