Skip to content

Commit 062e172

Browse files
committed
Add tests
Signed-off-by: Jonas Johansson <[email protected]>
1 parent 218b151 commit 062e172

File tree

4 files changed

+100
-2
lines changed

4 files changed

+100
-2
lines changed

Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ if ENABLE_EXAMPLES
55
EXAMPLES += examples
66
endif
77

8-
SUBDIRS = src $(EXAMPLES)
8+
SUBDIRS = src $(EXAMPLES) tests
99

1010
EXTRA_DIST = LICENSE README.md autogen.sh
1111

configure.ac

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ AC_INIT([libnsh], [0.2], [https://github.com/jonasj76/libnsh/issues])
33

44
AM_INIT_AUTOMAKE([foreign -Wall -Werror])
55

6+
PKG_CHECK_MODULES([CHECK], [check >= 0.9.4], [have_check=yes], [have_check=no])
7+
68
AC_CONFIG_MACRO_DIR([m4])
79

810
AC_PROG_CC
@@ -13,9 +15,11 @@ LT_INIT
1315
AC_ARG_ENABLE([examples],
1416
[AC_HELP_STRING([--enable-examples], [build examples in examples])],
1517
[enable_examples=yes], [])
18+
1619
AM_CONDITIONAL([ENABLE_EXAMPLES], [test "$enable_examples" = yes])
20+
AM_CONDITIONAL([HAVE_CHECK], [test "$have_check" = yes])
1721

1822
AC_CONFIG_HEADERS([config.h])
19-
AC_CONFIG_FILES([Makefile src/Makefile examples/Makefile])
23+
AC_CONFIG_FILES([Makefile src/Makefile examples/Makefile tests/Makefile])
2024

2125
AC_OUTPUT

tests/Makefile.am

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
TESTS = check_register
2+
AM_CFLAGS = -I$(top_srcdir)/src @CHECK_CFLAGS@
3+
AM_LDFLAGS = -L$(top_srcdir)/src
4+
LIBS = -lnetsnmp -lnetsnmpagent -lnetsnmpmibs
5+
LDADD = $(top_srcdir)/src/libnsh.la @CHECK_LIBS@
6+
7+
if HAVE_CHECK
8+
9+
check_register_SOURCES = check_register.c
10+
11+
check_PROGRAMS = $(TESTS)
12+
13+
endif

tests/check_register.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <stdlib.h>
2+
#include <check.h>
3+
#include <nsh.h>
4+
5+
#define oid_netSnmpPlaypen 1, 3, 6, 1, 4, 1, 8072, 9999, 9999
6+
#define oid_exampleOid oid_netSnmpPlaypen, 1
7+
8+
nsh_scalar_handler_const(exampleOid, ASN_INTEGER, 10);
9+
10+
START_TEST (test_register_scalar)
11+
{
12+
int ret;
13+
14+
ret = nsh_register_scalar_ro(exampleOid);
15+
ck_assert_int_eq(ret, MIB_REGISTERED_OK);
16+
17+
ret = nsh_register_scalar_ro(exampleOid);
18+
ck_assert_int_eq(ret, MIB_DUPLICATE_REGISTRATION);
19+
}
20+
END_TEST
21+
22+
START_TEST (test_register_table_ok)
23+
{
24+
oid table_oid[] = { oid_exampleOid };
25+
int index[] = { ASN_INTEGER };
26+
int ret;
27+
28+
ret = nsh_register_table("exampleOid",
29+
table_oid, OID_LENGTH (table_oid),
30+
0, 0, NULL, 0,
31+
NULL, NULL, NULL, NULL, NULL,
32+
HANDLER_CAN_RONLY);
33+
34+
ck_assert_int_eq(ret, MIB_REGISTERED_OK);
35+
}
36+
END_TEST
37+
38+
START_TEST (test_register_table_fail)
39+
{
40+
oid table_oid[] = { oid_exampleOid };
41+
int index[] = { ASN_INTEGER };
42+
int ret;
43+
44+
ret = nsh_register_table("exampleOid",
45+
NULL, 0,
46+
0, 0, NULL, 0,
47+
NULL, NULL, NULL, NULL, NULL,
48+
HANDLER_CAN_RONLY);
49+
50+
ck_assert_int_eq(ret, MIB_REGISTRATION_FAILED);
51+
}
52+
END_TEST
53+
54+
Suite* register_suite(void)
55+
{
56+
Suite *s = suite_create("register");
57+
TCase *tc_register_scalar = tcase_create("register scalar");
58+
TCase *tc_register_table = tcase_create("register table");
59+
60+
tcase_add_test(tc_register_scalar, test_register_scalar);
61+
suite_add_tcase(s, tc_register_scalar);
62+
63+
tcase_add_test(tc_register_table, test_register_table_ok);
64+
tcase_add_test(tc_register_table, test_register_table_fail);
65+
suite_add_tcase(s, tc_register_table);
66+
67+
return s;
68+
}
69+
70+
int main(void)
71+
{
72+
int number_failed;
73+
Suite *s = register_suite();
74+
SRunner *sr = srunner_create(s);
75+
76+
srunner_run_all(sr, CK_VERBOSE);
77+
number_failed = srunner_ntests_failed(sr);
78+
srunner_free(sr);
79+
80+
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
81+
}

0 commit comments

Comments
 (0)