Skip to content

Commit f2a471f

Browse files
Create unit test for gateway functions
This patch add some unit test. It uses CMOCKA library that is unit test framework for C language. In addtion, this patch add some shell scripts that enables interface creation and clean up, clean up in case of test is failed, and create coveradge report.
1 parent 884d3cb commit f2a471f

10 files changed

Lines changed: 497 additions & 62 deletions

File tree

GNUmakefile.am

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ SUBDIRS = \
33
config \
44
src
55

6+
if ENABLE_TEST
7+
SUBDIRS += test
8+
endif
9+
610
EXTRA_DIST = \
711
autogen.sh \
812
config/m4/.secret-world-domination-project

configure.ac

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ if test "${CONFIG_DEBUG}" = "yes"; then
9595
CFLAGS="${CFLAGS} -Werror -Wsign-compare -Wfloat-equal -Wformat-security -g -O1"
9696
AC_DEFINE(DEBUG, 1, [debugging])
9797
else
98-
CFLAGS="${CFLAGS} -O2"
98+
CFLAGS="${CFLAGS} -g -O2"
9999
fi
100100

101101

@@ -141,12 +141,25 @@ if test "$enable_address_sanitizer" = "yes"; then
141141
CFLAGS="${CFLAGS} -fsanitize=address"
142142
fi
143143

144+
#
145+
# Enable unit test
146+
#
147+
AC_ARG_ENABLE([test],
148+
[AS_HELP_STRING([--enable-test], [Enable unit test build (cmocka is required, default is no])],
149+
[:],
150+
[enable_test=no])
151+
AM_CONDITIONAL([ENABLE_TEST], [test "$enable_test" = "yes"])
152+
AS_CASE(
153+
["$enable_test"],
154+
[yes], [PKG_CHECK_MODULES([CMOCKA], [cmocka])],[])
155+
144156
AC_CONFIG_FILES([
145157
GNUmakefile
146158
config/libsocketcan.pc
147159
config/GNUmakefile
148160
include/GNUmakefile
149161
src/GNUmakefile
162+
test/GNUmakefile
150163
])
151164
AC_OUTPUT
152165

src/GNUmakefile.am

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,4 @@ libsocketcan_la_LDFLAGS = \
1919
MAINTAINERCLEANFILES = \
2020
GNUmakefile.in
2121

22-
bin_PROGRAMS = gwtwst
23-
24-
gwtwst_SOURCES = test/gwtest.c
25-
2622
CLEANFILES = *.gcda *.gcno

src/libcangw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ int cangw_get_rules(socketcan_gw_rules_t **gw_rules)
520520
if (parse_listing_data(pgw_rules, rxbuf, ret))
521521
break;
522522
}
523-
print_gw_rules(pgw_rules);
523+
524524
(*gw_rules) = pgw_rules;
525525

526526
do_return:

src/test/gwtest.c

Lines changed: 0 additions & 56 deletions
This file was deleted.

test/GNUmakefile.am

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
bin_PROGRAMS = gwtest
2+
3+
gwtest_SOURCES = gwtest.c
4+
5+
gwtest_LDADD = \
6+
@CMOCKA_LIBS@
7+
8+
gwtest_CFLAGS = \
9+
-g \
10+
-I$(top_srcdir)/src \
11+
-I$(top_srcdir)/include \
12+
@CMOCKA_CFLAGS@ \
13+
-D_GNU_SOURCE
14+
15+
CLEANFILES = *.gcda *.gcno

test/clean-test.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
ip link del vxcan0
3+
ip link del vcan0
4+
ip link del vcan1
5+

test/gen-test-report.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
3+
rm -Rf ./coverage
4+
mkdir -p ./coverage
5+
mkdir -p ./coverage/report
6+
7+
lcov -c --rc lcov_branch_coverage=1 -d . -o ./coverage/testcoverage.info
8+
genhtml --rc lcov_branch_coverage=1 -o ./coverage/report -f ./coverage/testcoverage.info --num-spaces 4
9+

0 commit comments

Comments
 (0)