Commit ba226291 authored by Evan Hunt's avatar Evan Hunt
Browse files

convert netaddr_test

(cherry picked from commit 7ab8c5b5)
parent 7b228848
......@@ -13,7 +13,7 @@ atf_test_program{name='ht_test'}
atf_test_program{name='inet_ntop_test'}
tap_test_program{name='lex_test'}
atf_test_program{name='mem_test'}
atf_test_program{name='netaddr_test'}
tap_test_program{name='netaddr_test'}
tap_test_program{name='parse_test'}
atf_test_program{name='pool_test'}
atf_test_program{name='print_test'}
......
......@@ -107,8 +107,9 @@ mem_test@EXEEXT@: mem_test.@O@ isctest.@O@ ${ISCDEPLIBS}
mem_test.@O@ isctest.@O@ ${ISCLIBS} ${LIBS}
netaddr_test@EXEEXT@: netaddr_test.@O@ ${ISCDEPLIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \
netaddr_test.@O@ ${ISCLIBS} ${LIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${CMOCKA_CFLAGS} \
${LDFLAGS} -o $@ netaddr_test.@O@ \
${ISCLIBS} ${LIBS} ${CMOCKA_LIBS}
parse_test@EXEEXT@: parse_test.@O@ isctest.@O@ ${ISCDEPLIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${CMOCKA_CFLAGS} \
......
......@@ -9,25 +9,28 @@
* information regarding copyright ownership.
*/
/* ! \file */
#include <config.h>
#include <atf-c.h>
#if HAVE_CMOCKA
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#define UNIT_TESTING
#include <cmocka.h>
#include <isc/netaddr.h>
#include <isc/sockaddr.h>
#include <isc/util.h>
ATF_TC(netaddr_isnetzero);
ATF_TC_HEAD(netaddr_isnetzero, tc) {
atf_tc_set_md_var(tc, "descr", "test netaddr_isnetzero");
}
ATF_TC_BODY(netaddr_isnetzero, tc) {
/* test isc_netaddr_isnetzero() */
static void
netaddr_isnetzero(void **state) {
unsigned int i;
struct in_addr ina;
struct {
......@@ -48,22 +51,19 @@ ATF_TC_BODY(netaddr_isnetzero, tc) {
bool result;
isc_netaddr_t netaddr;
UNUSED(state);
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
ina.s_addr = inet_addr(tests[i].address);
isc_netaddr_fromin(&netaddr, &ina);
result = isc_netaddr_isnetzero(&netaddr);
ATF_CHECK_EQ_MSG(result, tests[i].expect,
"%s", tests[i].address);
assert_int_equal(result, tests[i].expect);
}
}
ATF_TC(netaddr_masktoprefixlen);
ATF_TC_HEAD(netaddr_masktoprefixlen, tc) {
atf_tc_set_md_var(tc, "descr",
"isc_netaddr_masktoprefixlen() "
"calculates correct prefix lengths ");
}
ATF_TC_BODY(netaddr_masktoprefixlen, tc) {
/* test isc_netaddr_masktoprefixlen() calculates correct prefix lengths */
static void
netaddr_masktoprefixlen(void **state) {
struct in_addr na_a;
struct in_addr na_b;
struct in_addr na_c;
......@@ -74,41 +74,38 @@ ATF_TC_BODY(netaddr_masktoprefixlen, tc) {
isc_netaddr_t ina_d;
unsigned int plen;
UNUSED(tc);
UNUSED(state);
ATF_CHECK(inet_pton(AF_INET, "0.0.0.0", &na_a) >= 0);
ATF_CHECK(inet_pton(AF_INET, "255.255.255.254", &na_b) >= 0);
ATF_CHECK(inet_pton(AF_INET, "255.255.255.255", &na_c) >= 0);
ATF_CHECK(inet_pton(AF_INET, "255.255.255.0", &na_d) >= 0);
assert_true(inet_pton(AF_INET, "0.0.0.0", &na_a) >= 0);
assert_true(inet_pton(AF_INET, "255.255.255.254", &na_b) >= 0);
assert_true(inet_pton(AF_INET, "255.255.255.255", &na_c) >= 0);
assert_true(inet_pton(AF_INET, "255.255.255.0", &na_d) >= 0);
isc_netaddr_fromin(&ina_a, &na_a);
isc_netaddr_fromin(&ina_b, &na_b);
isc_netaddr_fromin(&ina_c, &na_c);
isc_netaddr_fromin(&ina_d, &na_d);
ATF_CHECK_EQ(isc_netaddr_masktoprefixlen(&ina_a, &plen),
ISC_R_SUCCESS);
ATF_CHECK_EQ(plen, 0);
assert_int_equal(isc_netaddr_masktoprefixlen(&ina_a, &plen),
ISC_R_SUCCESS);
assert_int_equal(plen, 0);
ATF_CHECK_EQ(isc_netaddr_masktoprefixlen(&ina_b, &plen),
assert_int_equal(isc_netaddr_masktoprefixlen(&ina_b, &plen),
ISC_R_SUCCESS);
ATF_CHECK_EQ(plen, 31);
assert_int_equal(plen, 31);
ATF_CHECK_EQ(isc_netaddr_masktoprefixlen(&ina_c, &plen),
ISC_R_SUCCESS);
ATF_CHECK_EQ(plen, 32);
assert_int_equal(isc_netaddr_masktoprefixlen(&ina_c, &plen),
ISC_R_SUCCESS);
assert_int_equal(plen, 32);
ATF_CHECK_EQ(isc_netaddr_masktoprefixlen(&ina_d, &plen),
ISC_R_SUCCESS);
ATF_CHECK_EQ(plen, 24);
assert_int_equal(isc_netaddr_masktoprefixlen(&ina_d, &plen),
ISC_R_SUCCESS);
assert_int_equal(plen, 24);
}
ATF_TC(netaddr_multicast);
ATF_TC_HEAD(netaddr_multicast, tc) {
atf_tc_set_md_var(tc, "descr",
"check multicast addresses are detected properly");
}
ATF_TC_BODY(netaddr_multicast, tc) {
/* check multicast addresses are detected properly */
static void
netaddr_multicast(void **state) {
unsigned int i;
struct {
int family;
......@@ -123,6 +120,8 @@ ATF_TC_BODY(netaddr_multicast, tc) {
{ AF_INET6, "ff02::1", true }
};
UNUSED(state);
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
isc_netaddr_t na;
struct in_addr in;
......@@ -132,26 +131,39 @@ ATF_TC_BODY(netaddr_multicast, tc) {
if (tests[i].family == AF_INET) {
r = inet_pton(AF_INET, tests[i].addr,
(unsigned char *)&in);
ATF_REQUIRE_EQ(r, 1);
assert_int_equal(r, 1);
isc_netaddr_fromin(&na, &in);
} else {
r = inet_pton(AF_INET6, tests[i].addr,
(unsigned char *)&in6);
ATF_REQUIRE_EQ(r, 1);
assert_int_equal(r, 1);
isc_netaddr_fromin6(&na, &in6);
}
ATF_CHECK_EQ(isc_netaddr_ismulticast(&na),
tests[i].is_multicast);
assert_int_equal(isc_netaddr_ismulticast(&na),
tests[i].is_multicast);
}
}
/*
* Main
*/
ATF_TP_ADD_TCS(tp) {
ATF_TP_ADD_TC(tp, netaddr_isnetzero);
ATF_TP_ADD_TC(tp, netaddr_masktoprefixlen);
ATF_TP_ADD_TC(tp, netaddr_multicast);
return (atf_no_error());
int
main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(netaddr_isnetzero),
cmocka_unit_test(netaddr_masktoprefixlen),
cmocka_unit_test(netaddr_multicast),
};
return (cmocka_run_group_tests(tests, NULL, NULL));
}
#else /* HAVE_CMOCKA */
#include <stdio.h>
int
main(void) {
printf("1..0 # Skipped: cmocka not available\n");
return (0);
}
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment