diff options
author | Thomas Graf <tgraf@suug.ch> | 2013-03-14 15:25:17 (GMT) |
---|---|---|
committer | Thomas Graf <tgraf@suug.ch> | 2013-03-14 15:25:17 (GMT) |
commit | e09e7f1035fc2a8c918822cbfcfac61c77153497 (patch) | |
tree | cd3d8a92106201f5782d5ae7f7086177defed4fc /tests | |
parent | 780a0423dc7a7f248d6a71a7ae6f531fe95ba911 (diff) | |
download | libnl-e09e7f1035fc2a8c918822cbfcfac61c77153497.zip libnl-e09e7f1035fc2a8c918822cbfcfac61c77153497.tar.gz libnl-e09e7f1035fc2a8c918822cbfcfac61c77153497.tar.bz2 |
tests: Add check based unit test system
Introduces new unit tests based on `check`. Each subsystem/module can
have its own unit tests in tests/check-<name>.c that will be linked
together in tests/check-all.c.
Running 'make check' will compile and run the unit tests automatically.
A reference unit test implementation has been done for the abstract
address module.
Signed-off-by: Thomas Graf <tgraf@suug.ch>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/.gitignore | 1 | ||||
-rw-r--r-- | tests/Makefile.am | 22 | ||||
-rw-r--r-- | tests/check-addr.c | 212 | ||||
-rw-r--r-- | tests/check-all.c | 42 |
4 files changed, 271 insertions, 6 deletions
diff --git a/tests/.gitignore b/tests/.gitignore index b386c45..151e9b5 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -6,3 +6,4 @@ test-delete-link test-genl test-nf-cache-mngr test-socket-creation +check-all diff --git a/tests/Makefile.am b/tests/Makefile.am index 9c0ab8d..6a5606a 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -6,14 +6,23 @@ LDADD = \ ${top_builddir}/lib/libnl-3.la \ ${top_builddir}/lib/libnl-nf-3.la \ ${top_builddir}/lib/libnl-genl-3.la \ - ${top_builddir}/lib/libnl-route-3.la + ${top_builddir}/lib/libnl-route-3.la \ + @CHECK_LIBS@ + +CFLAGS += @CHECK_CFLAGS@ + +UNIT_TESTS = check-all check_PROGRAMS = \ test-create-bond \ test-create-vlan \ test-delete-link \ test-socket-creation \ - test-complex-HTB-with-hash-filters + test-complex-HTB-with-hash-filters \ + ${UNIT_TESTS} + +TESTS = \ + ${UNIT_TESTS} if ENABLE_CLI LDADD += ${top_builddir}/src/lib/libnl-cli-3.la @@ -23,10 +32,6 @@ check_PROGRAMS += \ test-nf-cache-mngr endif -# Eventually add these to TESTS once converted to be real -# test programs -# TESTS = $(check_PROGRAMS) - test_cache_mngr_SOURCES = test-cache-mngr.c test_create_bond_SOURCES = test-create-bond.c test_create_vlan_SOURCES = test-create-vlan.c @@ -35,3 +40,8 @@ test_genl_SOURCES = test-genl.c test_nf_cache_mngr_SOURCES = test-nf-cache-mngr.c test_socket_creation_SOURCES = test-socket-creation.c test_complex_HTB_with_hash_filters_SOURCES = test-complex-HTB-with-hash-filters.c + +# Unit tests +check_all_SOURCES = \ + check-all.c \ + check-addr.c diff --git a/tests/check-addr.c b/tests/check-addr.c new file mode 100644 index 0000000..39f3ede --- /dev/null +++ b/tests/check-addr.c @@ -0,0 +1,212 @@ +/* + * tests/check-addr.c nl_addr unit tests + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * Copyright (c) 2013 Thomas Graf <tgraf@suug.ch> + */ + +#include <check.h> +#include <netlink/addr.h> + +START_TEST(addr_alloc) +{ + struct nl_addr *addr; + + addr = nl_addr_alloc(16); + fail_if(addr == NULL, + "Allocation should not return NULL"); + + fail_if(nl_addr_iszero(addr) == 0, + "New empty address should be all zeros"); + + fail_if(nl_addr_get_family(addr) != AF_UNSPEC, + "New empty address should have family AF_UNSPEC"); + + fail_if(nl_addr_get_prefixlen(addr) != 0, + "New empty address should have prefix length 0"); + + fail_if(nl_addr_shared(addr), + "New empty address should not be shared"); + + fail_if(nl_addr_get(addr) != addr, + "nl_addr_get() should return pointer to address"); + + fail_if(nl_addr_shared(addr) == 0, + "Address should be shared after call to nl_addr_get()"); + + nl_addr_put(addr); + + fail_if(nl_addr_shared(addr), + "Address should not be shared after call to nl_addr_put()"); + + fail_if(nl_addr_fill_sockaddr(addr, NULL, 0) == 0, + "Socket address filling should fail for empty address"); + + nl_addr_put(addr); +} +END_TEST + +START_TEST(addr_binary_addr) +{ + struct nl_addr *addr, *addr2; + char baddr[4] = { 0x1, 0x2, 0x3, 0x4 }; + char baddr2[6] = { 0x1, 0x2, 0x3, 0x4, 0x5, 0x6 }; + + addr = nl_addr_alloc(4); + fail_if(addr == NULL, + "Allocation should not return NULL"); + + fail_if(nl_addr_set_binary_addr(addr, baddr, 4) < 0, + "Valid binary address should be settable"); + + fail_if(nl_addr_get_prefixlen(addr) != 0, + "Prefix length should be unchanged after nl_addr_set_binary_addr()"); + + fail_if(nl_addr_get_len(addr) != 4, + "Address length should be 4"); + + fail_if(nl_addr_set_binary_addr(addr, baddr2, 6) == 0, + "Should not be able to set binary address exceeding maximum length"); + + fail_if(nl_addr_get_len(addr) != 4, + "Address length should still be 4"); + + fail_if(nl_addr_guess_family(addr) != AF_INET, + "Binary address of length 4 should be guessed as AF_INET"); + + fail_if(memcmp(baddr, nl_addr_get_binary_addr(addr), 4) != 0, + "Binary address mismatches"); + + addr2 = nl_addr_build(AF_UNSPEC, baddr, 4); + fail_if(addr2 == NULL, + "Building of address should not fail"); + + nl_addr_set_prefixlen(addr, 32); + fail_if(nl_addr_get_prefixlen(addr) != 32, + "Prefix length should be successful changed after nl_addr_set_prefixlen()"); + + fail_if(nl_addr_cmp(addr, addr2), + "Addresses built from same binary address should match"); + + nl_addr_put(addr); + nl_addr_put(addr2); +} +END_TEST + +START_TEST(addr_parse4) +{ + struct nl_addr *addr4, *clone; + struct sockaddr_in sin; + socklen_t len = sizeof(sin); + char *addr_str = "10.0.0.1/16"; + char buf[128]; + + fail_if(nl_addr_parse(addr_str, AF_INET6, &addr4) == 0, + "Should not be able to parse IPv4 address in IPv6 mode"); + + fail_if(nl_addr_parse(addr_str, AF_UNSPEC, &addr4) != 0, + "Should be able to parse \"%s\"", addr_str); + + fail_if(nl_addr_get_family(addr4) != AF_INET, + "Address family should be AF_INET"); + + fail_if(nl_addr_get_prefixlen(addr4) != 16, + "Prefix length should be 16"); + + fail_if(nl_addr_iszero(addr4), + "Address should not be all zeroes"); + + clone = nl_addr_clone(addr4); + fail_if(clone == NULL, + "Cloned address should not be NULL"); + + fail_if(nl_addr_cmp(addr4, clone) != 0, + "Cloned address should not mismatch original"); + + fail_if(nl_addr_fill_sockaddr(addr4, (struct sockaddr *) &sin, &len) != 0, + "Should be able to fill socketaddr"); + + fail_if(strcmp(nl_addr2str(addr4, buf, sizeof(buf)), addr_str), + "Address translated back to string does not match original"); + + nl_addr_put(addr4); + nl_addr_put(clone); +} +END_TEST + +START_TEST(addr_parse6) +{ + struct nl_addr *addr6, *clone; + struct sockaddr_in6 sin; + socklen_t len = sizeof(sin); + char *addr_str = "2001:1:2::3/64"; + char buf[128]; + + fail_if(nl_addr_parse(addr_str, AF_INET, &addr6) == 0, + "Should not be able to parse IPv6 address in IPv4 mode"); + + fail_if(nl_addr_parse(addr_str, AF_UNSPEC, &addr6) != 0, + "Should be able to parse \"%s\"", addr_str); + + fail_if(nl_addr_get_family(addr6) != AF_INET6, + "Address family should be AF_INET6"); + + fail_if(nl_addr_get_prefixlen(addr6) != 64, + "Prefix length should be 64"); + + fail_if(nl_addr_iszero(addr6), + "Address should not be all zeroes"); + + clone = nl_addr_clone(addr6); + fail_if(clone == NULL, + "Cloned address should not be NULL"); + + fail_if(nl_addr_cmp(addr6, clone) != 0, + "Cloned address should not mismatch original"); + + fail_if(nl_addr_fill_sockaddr(addr6, (struct sockaddr *) &sin, &len) != 0, + "Should be able to fill socketaddr"); + + fail_if(strcmp(nl_addr2str(addr6, buf, sizeof(buf)), addr_str), + "Address translated back to string does not match original"); + + nl_addr_put(addr6); + nl_addr_put(clone); +} +END_TEST + +START_TEST(addr_info) +{ + struct nl_addr *addr; + char *addr_str = "127.0.0.1"; + struct addrinfo *result; + + fail_if(nl_addr_parse(addr_str, AF_UNSPEC, &addr) != 0, + "Parsing of valid address should not fail"); + + fail_if(nl_addr_info(addr, &result) != 0, + "getaddrinfo() on loopback address should work"); + + freeaddrinfo(result); + nl_addr_put(addr); +} +END_TEST + +Suite *make_nl_addr_suite(void) +{ + Suite *suite = suite_create("Abstract addresses"); + + TCase *tc_addr = tcase_create("Core"); + tcase_add_test(tc_addr, addr_alloc); + tcase_add_test(tc_addr, addr_binary_addr); + tcase_add_test(tc_addr, addr_parse4); + tcase_add_test(tc_addr, addr_parse6); + tcase_add_test(tc_addr, addr_info); + suite_add_tcase(suite, tc_addr); + + return suite; +} diff --git a/tests/check-all.c b/tests/check-all.c new file mode 100644 index 0000000..e801003 --- /dev/null +++ b/tests/check-all.c @@ -0,0 +1,42 @@ +/* + * tests/check-all.c overall unit test program + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * Copyright (c) 2013 Thomas Graf <tgraf@suug.ch> + */ + +#include <check.h> + +extern Suite *make_nl_addr_suite(void); + +static Suite *main_suite(void) +{ + Suite *suite = suite_create("main"); + + return suite; +} + +int main(int argc, char *argv[]) +{ + SRunner *runner; + int nfailed; + + runner = srunner_create(main_suite()); + + /* Add testsuites below */ + + srunner_add_suite(runner, make_nl_addr_suite()); + + /* Do not add testsuites below this line */ + + srunner_run_all(runner, CK_ENV); + + nfailed = srunner_ntests_failed(runner); + srunner_free(runner); + + return nfailed != 0; +} |