Index: tests/sys/netinet/libalias/2_natout.c =================================================================== --- /dev/null +++ tests/sys/netinet/libalias/2_natout.c @@ -0,0 +1,100 @@ +#include +#include +#include +#include + +#include +#include + +static struct ip * +ip_packet(struct in_addr src, struct in_addr dst, u_char protocol, size_t len) { + struct ip * p; + + ATF_REQUIRE(len >= 64 && len <= IP_MAXPACKET); + + p = malloc(len); + ATF_REQUIRE(p != NULL); + + p->ip_v = IPVERSION; + p->ip_hl = sizeof(*p)/4; + p->ip_len = htons(len); + p->ip_ttl = IPDEFTTL; + p->ip_src = src; + p->ip_dst = dst; + p->ip_p = protocol; + ATF_REQUIRE(p->ip_hl == 5); + + return (p); +} + +static inline bool +addr_eq(struct in_addr a, struct in_addr b) { + return a.s_addr == b.s_addr; +} + +#define a2h(a) ntohl(a.s_addr) + +#define NAT_CHECK(pip, src, dst) do { \ + int res; \ + res = LibAliasOut(la, pip, 64); \ + ATF_CHECK(res == PKT_ALIAS_OK); \ + ATF_CHECK(addr_eq(src, pip->ip_src)); \ + ATF_CHECK(addr_eq(dst, pip->ip_dst)); \ +} while(0) + +ATF_TC_WITHOUT_HEAD(1_simplemasq); +ATF_TC_BODY(1_simplemasq, dummy) +{ + struct libalias *la = LibAliasInit(NULL); + struct in_addr masq = { htonl(0x01020304) }; + struct in_addr src = { htonl(0x0a010203) }; + struct in_addr dst = { htonl(0x12345678) }; + struct ip *pip; + + ATF_REQUIRE(la != NULL); + LibAliasSetAddress(la, masq); + + pip = ip_packet(src, dst, 254, 64); + NAT_CHECK(pip, masq, dst); + free(pip); + + LibAliasUninit(la); +} + +ATF_TC_WITHOUT_HEAD(2_unregistered); +ATF_TC_BODY(2_unregistered, dummy) +{ + struct libalias *la = LibAliasInit(NULL); + struct in_addr masq = { htonl(0x01020304) }; + struct in_addr src1 = { htonl(0x0a010203) }; + struct in_addr src2 = { htonl(0xa0102030) }; + struct in_addr dst = { htonl(0x12345678) }; + struct ip *pip; + + ATF_REQUIRE(la != NULL); + LibAliasSetAddress(la, masq); + LibAliasSetMode(la, PKT_ALIAS_UNREGISTERED_ONLY, ~0); + + pip = ip_packet(src1, dst, 254, 64); + NAT_CHECK(pip, masq, dst); + + pip->ip_src = src2; + NAT_CHECK(pip, src2, dst); + + LibAliasSetMode(la, 0, PKT_ALIAS_UNREGISTERED_ONLY); + NAT_CHECK(pip, masq, dst); + + free(pip); + LibAliasUninit(la); +} + +ATF_TP_ADD_TCS(natout) +{ + /* Use "dd if=/dev/random bs=2 count=1 | od -x" to reproduce */ + srand(0x0b61); + + ATF_TP_ADD_TC(natout, 1_simplemasq); + ATF_TP_ADD_TC(natout, 2_unregistered); + + return atf_no_error(); +} Index: tests/sys/netinet/libalias/Makefile =================================================================== --- tests/sys/netinet/libalias/Makefile +++ tests/sys/netinet/libalias/Makefile @@ -7,7 +7,8 @@ TESTSDIR= ${TESTSBASE}/sys/netinet/libalias BINDIR= ${TESTSDIR} -ATF_TESTS_C+= 1_instance +ATF_TESTS_C+= 1_instance \ + 2_natout \ LIBADD+= alias