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

convert sockaddr_test

parent 7ab8c5b5
......@@ -21,7 +21,7 @@ tap_test_program{name='radix_test'}
atf_test_program{name='regex_test'}
tap_test_program{name='result_test'}
tap_test_program{name='safe_test'}
atf_test_program{name='sockaddr_test'}
tap_test_program{name='sockaddr_test'}
atf_test_program{name='socket_test'}
tap_test_program{name='symtab_test'}
atf_test_program{name='task_test'}
......
......@@ -159,8 +159,9 @@ socket_test@EXEEXT@: socket_test.@O@ isctest.@O@ ${ISCDEPLIBS}
socket_test.@O@ isctest.@O@ ${ISCLIBS} ${LIBS}
sockaddr_test@EXEEXT@: sockaddr_test.@O@ isctest.@O@ ${ISCDEPLIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \
sockaddr_test.@O@ isctest.@O@ ${ISCLIBS} ${LIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${CMOCKA_CFLAGS} \
${LDFLAGS} -o $@ sockaddr_test.@O@ isctest.@O@ \
${ISCLIBS} ${LIBS} ${CMOCKA_LIBS}
symtab_test@EXEEXT@: symtab_test.@O@ isctest.@O@ ${ISCDEPLIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${CMOCKA_CFLAGS} \
......
......@@ -9,64 +9,79 @@
* 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 <stdlib.h>
#include <string.h>
#include <unistd.h>
#define UNIT_TESTING
#include <cmocka.h>
#include <isc/netaddr.h>
#include <isc/sockaddr.h>
#include <isc/print.h>
#include <isc/util.h>
#include "isctest.h"
/*
* Individual unit tests
*/
static int
_setup(void **state) {
isc_result_t result;
UNUSED(state);
result = isc_test_begin(NULL, true, 0);
assert_int_equal(result, ISC_R_SUCCESS);
ATF_TC(sockaddr_hash);
ATF_TC_HEAD(sockaddr_hash, tc) {
atf_tc_set_md_var(tc, "descr", "sockaddr hash");
return (0);
}
ATF_TC_BODY(sockaddr_hash, tc) {
isc_result_t result;
static int
_teardown(void **state) {
UNUSED(state);
isc_test_end();
return (0);
}
/* test sockaddr hash */
static void
sockaddr_hash(void **state) {
isc_sockaddr_t addr;
struct in_addr in;
struct in6_addr in6;
unsigned int h1, h2, h3, h4;
int ret;
UNUSED(tc);
result = isc_test_begin(NULL, true, 0);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
UNUSED(state);
in.s_addr = inet_addr("127.0.0.1");
isc_sockaddr_fromin(&addr, &in, 1);
h1 = isc_sockaddr_hash(&addr, true);
h2 = isc_sockaddr_hash(&addr, false);
ATF_CHECK(h1 != h2);
assert_int_not_equal(h1, h2);
ret = inet_pton(AF_INET6, "::ffff:127.0.0.1", &in6);
ATF_CHECK(ret == 1);
assert_int_equal(ret, 1);
isc_sockaddr_fromin6(&addr, &in6, 1);
h3 = isc_sockaddr_hash(&addr, true);
h4 = isc_sockaddr_hash(&addr, false);
ATF_CHECK(h1 == h3);
ATF_CHECK(h2 == h4);
isc_test_end();
assert_int_equal(h1, h3);
assert_int_equal(h2, h4);
}
ATF_TC(sockaddr_isnetzero);
ATF_TC_HEAD(sockaddr_isnetzero, tc) {
atf_tc_set_md_var(tc, "descr", "sockaddr is net zero");
}
ATF_TC_BODY(sockaddr_isnetzero, tc) {
/* test isc_sockaddr_isnetzero() */
static void
sockaddr_isnetzero(void **state) {
isc_sockaddr_t addr;
struct in_addr in;
struct in6_addr in6;
......@@ -102,32 +117,30 @@ ATF_TC_BODY(sockaddr_isnetzero, tc) {
{ "::ffff:255.255.255.255", false },
};
UNUSED(tc);
UNUSED(state);
for (i = 0; i < sizeof(data4)/sizeof(data4[0]); i++) {
in.s_addr = inet_addr(data4[i].string);
isc_sockaddr_fromin(&addr, &in, 1);
r = isc_sockaddr_isnetzero(&addr);
ATF_CHECK_EQ_MSG(r, data4[i].expect, "%s", data4[i].string);
assert_int_equal(r, data4[i].expect);
}
for (i = 0; i < sizeof(data6)/sizeof(data6[0]); i++) {
ret = inet_pton(AF_INET6, data6[i].string, &in6);
ATF_CHECK_EQ(ret, 1);
assert_int_equal(ret, 1);
isc_sockaddr_fromin6(&addr, &in6, 1);
r = isc_sockaddr_isnetzero(&addr);
ATF_CHECK_EQ_MSG(r, data6[i].expect, "%s", data6[i].string);
assert_int_equal(r, data6[i].expect);
}
}
ATF_TC(sockaddr_eqaddrprefix);
ATF_TC_HEAD(sockaddr_eqaddrprefix, tc) {
atf_tc_set_md_var(tc, "descr",
"isc_sockaddr_eqaddrprefix() returns true when "
"prefixes of a and b are equal, and false when "
"they are not equal");
}
ATF_TC_BODY(sockaddr_eqaddrprefix, tc) {
/*
* test that isc_sockaddr_eqaddrprefix() returns true when prefixes of a
* and b are equal, and false when they are not equal
*/
static void
sockaddr_eqaddrprefix(void **state) {
struct in_addr ina_a;
struct in_addr ina_b;
struct in_addr ina_c;
......@@ -135,33 +148,45 @@ ATF_TC_BODY(sockaddr_eqaddrprefix, tc) {
isc_sockaddr_t isa_b;
isc_sockaddr_t isa_c;
UNUSED(tc);
UNUSED(state);
ATF_CHECK(inet_pton(AF_INET, "194.100.32.87", &ina_a) >= 0);
ATF_CHECK(inet_pton(AF_INET, "194.100.32.80", &ina_b) >= 0);
ATF_CHECK(inet_pton(AF_INET, "194.101.32.87", &ina_c) >= 0);
assert_true(inet_pton(AF_INET, "194.100.32.87", &ina_a) >= 0);
assert_true(inet_pton(AF_INET, "194.100.32.80", &ina_b) >= 0);
assert_true(inet_pton(AF_INET, "194.101.32.87", &ina_c) >= 0);
isc_sockaddr_fromin(&isa_a, &ina_a, 0);
isc_sockaddr_fromin(&isa_b, &ina_b, 42);
isc_sockaddr_fromin(&isa_c, &ina_c, 0);
ATF_CHECK(isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 0));
ATF_CHECK(isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 29));
ATF_CHECK(isc_sockaddr_eqaddrprefix(&isa_a, &isa_c, 8));
assert_true(isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 0));
assert_true(isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 29));
assert_true(isc_sockaddr_eqaddrprefix(&isa_a, &isa_c, 8));
ATF_CHECK(! isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 30));
ATF_CHECK(! isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 32));
ATF_CHECK(! isc_sockaddr_eqaddrprefix(&isa_a, &isa_c, 16));
assert_false(isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 30));
assert_false(isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 32));
assert_false(isc_sockaddr_eqaddrprefix(&isa_a, &isa_c, 16));
}
/*
* Main
*/
ATF_TP_ADD_TCS(tp) {
ATF_TP_ADD_TC(tp, sockaddr_hash);
ATF_TP_ADD_TC(tp, sockaddr_isnetzero);
ATF_TP_ADD_TC(tp, sockaddr_eqaddrprefix);
int
main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup_teardown(sockaddr_hash,
_setup, _teardown),
cmocka_unit_test(sockaddr_isnetzero),
cmocka_unit_test(sockaddr_eqaddrprefix),
};
return (cmocka_run_group_tests(tests, NULL, NULL));
}
#else /* HAVE_CMOCKA */
#include <stdio.h>
return (atf_no_error());
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