Skip to content

Commit 20de792

Browse files
committed
Add more test coverage to auparse
Added a new C test exercising lesser used API pieces such as buffer replacement, feed handling, normalization and event comparisons. Integrated the new test binary into the automake rules and test list Updated the test runner script to execute the extra coverage test during make check. These additions cover previously untested areas like auparse_new_buffer, feed status helpers, normalization routines and timestamp comparison, guarding against regressions in those complex functions.
1 parent b8e09e0 commit 20de792

File tree

3 files changed

+91
-3
lines changed

3 files changed

+91
-3
lines changed

auparse/test/Makefile.am

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
#
2222

2323
CONFIG_CLEAN_FILES = *.loT *.rej *.orig *.cur
24-
noinst_PROGRAMS = auparse_test auparselol_test lookup_test databuf_test
25-
TESTS = run_auparse_tests.sh run_auparselol_test.sh lookup_test databuf_test
24+
noinst_PROGRAMS = auparse_test auparselol_test lookup_test databuf_test \
25+
auparse_extra_test
26+
TESTS = run_auparse_tests.sh run_auparselol_test.sh lookup_test \
27+
databuf_test auparse_extra_test
2628
dist_check_SCRIPTS = run_auparse_tests.sh run_auparselol_test.sh
2729
EXTRA_DIST = auparse_test.ref auparse_test.ref.py test.log test2.log test3.log test4.log auditd_raw.sed run_auparse_tests.sh auparse_test.py run_auparselol_test.sh
2830
CLEANFILES = run_auparse_tests.sh run_auparselol_test.sh
@@ -53,6 +55,12 @@ databuf_test_LDADD = ${top_builddir}/auparse/libauparse.la \
5355
${top_builddir}/lib/libaudit.la ${top_builddir}/common/libaucommon.la
5456
databuf_test_DEPENDENCIES = ${top_builddir}/auparse/libauparse.la ${top_builddir}/lib/libaudit.la ${top_builddir}/common/libaucommon.la
5557

58+
auparse_extra_test_SOURCES = auparse_extra_test.c
59+
auparse_extra_test_LDFLAGS = -static
60+
auparse_extra_test_LDADD = ${top_builddir}/auparse/libauparse.la \
61+
${top_builddir}/lib/libaudit.la ${top_builddir}/common/libaucommon.la
62+
auparse_extra_test_DEPENDENCIES = ${top_builddir}/auparse/libauparse.la ${top_builddir}/lib/libaudit.la ${top_builddir}/common/libaucommon.la
63+
5664
drop_srcdir = sed 's,$(srcdir)/test,test,'
5765

5866

auparse/test/auparse_extra_test.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "config.h"
2+
#include <assert.h>
3+
#include <stdio.h>
4+
#include <string.h>
5+
#include "libaudit.h"
6+
#include "auparse.h"
7+
8+
static void test_new_buffer(void)
9+
{
10+
const char buf1[] = "type=LOGIN msg=audit(1143146623.787:142): pid=1\n";
11+
const char buf2[] = "type=USER_LOGIN msg=audit(1143146623.879:146): pid=2\n";
12+
auparse_state_t *au = auparse_init(AUSOURCE_BUFFER, buf1);
13+
assert(au != NULL);
14+
assert(auparse_next_event(au) > 0);
15+
assert(auparse_get_type(au) == AUDIT_LOGIN);
16+
assert(auparse_new_buffer(au, buf2, strlen(buf2)) == 0);
17+
assert(auparse_next_event(au) > 0);
18+
assert(auparse_get_type(au) == AUDIT_USER_LOGIN);
19+
auparse_destroy(au);
20+
}
21+
22+
static int cb_count;
23+
static void ready_cb(auparse_state_t *au, auparse_cb_event_t e, void *d)
24+
{
25+
if (e == AUPARSE_CB_EVENT_READY)
26+
cb_count++;
27+
}
28+
29+
static void test_feed_state(void)
30+
{
31+
const char buf[] = "type=LOGIN msg=audit(1143146623.787:142): pid=1\n";
32+
auparse_state_t *au = auparse_init(AUSOURCE_FEED, NULL);
33+
assert(au != NULL);
34+
auparse_add_callback(au, ready_cb, NULL, NULL);
35+
assert(auparse_feed_has_data(au) == 0);
36+
assert(auparse_feed(au, buf, strlen(buf)) == 0);
37+
assert(auparse_feed_has_data(au) == 1);
38+
auparse_flush_feed(au);
39+
assert(cb_count == 1);
40+
auparse_destroy(au);
41+
}
42+
43+
static void test_normalize(void)
44+
{
45+
auparse_state_t *au = auparse_init(AUSOURCE_FILE, "./test.log");
46+
assert(au != NULL);
47+
assert(auparse_next_event(au) > 0);
48+
assert(auparse_normalize(au, NORM_OPT_ALL) == 0);
49+
const char *kind = auparse_normalize_get_event_kind(au);
50+
assert(kind && strcmp(kind, "mac-decision") == 0);
51+
assert(auparse_normalize_subject_primary(au) == 1);
52+
assert(auparse_get_field_str(au) != NULL);
53+
auparse_normalize_object_primary(au);
54+
auparse_interpret_realpath(au);
55+
auparse_destroy(au);
56+
}
57+
58+
static void test_compare(void)
59+
{
60+
auparse_state_t *au = auparse_init(AUSOURCE_FILE, "./test.log");
61+
assert(au != NULL);
62+
assert(auparse_next_event(au) > 0);
63+
const au_event_t *e1 = auparse_get_timestamp(au);
64+
assert(auparse_next_event(au) > 0);
65+
const au_event_t *e2 = auparse_get_timestamp(au);
66+
assert(auparse_node_compare(e1, e2) == 0);
67+
assert(auparse_timestamp_compare(e1, e2) < 0);
68+
auparse_destroy(au);
69+
}
70+
71+
int main(void)
72+
{
73+
test_new_buffer();
74+
test_feed_state();
75+
test_normalize();
76+
test_compare();
77+
printf("extra auparse tests: all passed\n");
78+
return 0;
79+
}
80+

lib/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# Steve Grubb <[email protected]>
2222
#
2323

24-
SUBDIRS = test
24+
SUBDIRS = . test
2525
CLEANFILES = $(BUILT_SOURCES)
2626
CONFIG_CLEAN_FILES = *.loT *.rej *.orig
2727
EXTRA_DIST = syscall-update.txt gen_tables64.c

0 commit comments

Comments
 (0)