Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.sbin/npf/npftest npftest:
details: https://anonhg.NetBSD.org/src/rev/0e2ff31cba1c
branches: trunk
changeset: 781134:0e2ff31cba1c
user: rmind <rmind%NetBSD.org@localhost>
date: Tue Aug 21 20:52:11 2012 +0000
description:
npftest:
- Do not stop running other tests, if some tests fail.
- Fix some endianness bugs in the test cases.
Tested on sparc64 by martin@, all tests pass.
diffstat:
usr.sbin/npf/npftest/libnpftest/npf_nbuf_test.c | 13 +-
usr.sbin/npf/npftest/libnpftest/npf_processor_test.c | 123 +++++++-----------
usr.sbin/npf/npftest/libnpftest/npf_rule_test.c | 12 +-
usr.sbin/npf/npftest/libnpftest/npf_state_test.c | 7 +-
usr.sbin/npf/npftest/libnpftest/npf_table_test.c | 97 ++++++++------
usr.sbin/npf/npftest/npftest.c | 25 +--
6 files changed, 133 insertions(+), 144 deletions(-)
diffs (truncated from 661 to 300 lines):
diff -r ba665e50f731 -r 0e2ff31cba1c usr.sbin/npf/npftest/libnpftest/npf_nbuf_test.c
--- a/usr.sbin/npf/npftest/libnpftest/npf_nbuf_test.c Tue Aug 21 15:53:07 2012 +0000
+++ b/usr.sbin/npf/npftest/libnpftest/npf_nbuf_test.c Tue Aug 21 20:52:11 2012 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: npf_nbuf_test.c,v 1.1 2012/04/14 21:57:29 rmind Exp $ */
+/* $NetBSD: npf_nbuf_test.c,v 1.2 2012/08/21 20:52:11 rmind Exp $ */
/*
* NPF nbuf interface test.
@@ -157,20 +157,17 @@
{
struct mbuf *m1, *m2;
char *bufa, *bufb;
+ bool fail = false;
m1 = mbuf_random_len(MBUF_CHAIN_LEN);
bufa = mbuf_getstring(m1);
bufb = parse_nbuf_chain(m1, m1->m_data);
- if (!validate_mbuf_data(m1, verbose, bufa, bufb)) {
- return false;
- }
+ fail |= !validate_mbuf_data(m1, verbose, bufa, bufb);
m2 = mbuf_bytesize(MBUF_CHAIN_LEN);
bufa = mbuf_getstring(m2);
bufb = parse_nbuf_chain(m2, m2->m_data);
- if (!validate_mbuf_data(m2, verbose, bufa, bufb)) {
- return false;
- }
+ fail |= !validate_mbuf_data(m2, verbose, bufa, bufb);
- return true;
+ return !fail;
}
diff -r ba665e50f731 -r 0e2ff31cba1c usr.sbin/npf/npftest/libnpftest/npf_processor_test.c
--- a/usr.sbin/npf/npftest/libnpftest/npf_processor_test.c Tue Aug 21 15:53:07 2012 +0000
+++ b/usr.sbin/npf/npftest/libnpftest/npf_processor_test.c Tue Aug 21 20:52:11 2012 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: npf_processor_test.c,v 1.2 2012/07/01 23:21:07 rmind Exp $ */
+/* $NetBSD: npf_processor_test.c,v 1.3 2012/08/21 20:52:11 rmind Exp $ */
/*
* NPF n-code processor test.
@@ -7,87 +7,77 @@
*/
#include <sys/types.h>
+#include <sys/endian.h>
#include "npf_impl.h"
#include "npf_ncode.h"
#include "npf_test.h"
-/*
- * In network byte order:
- * 192.168.2.0 == 0x0002a8c0
- * 192.168.2.1 == 0x0102a8c0
- * 192.168.2.100 == 0x6402a8c0
- * fe80:: == 0x000080fe
- * 0x00000000
- * 0x00000000
- * 0x00000000
- * fe80::2a0:c0ff:fe10:1234 == 0x000080fe
- * 0x00000000
- * 0xffc0a002
- * 0x341210fe
- * htons(ETHERTYPE_IP) == 0x08
- * (htons(80) << 16) | htons(80) == 0x50005000
- * (htons(80) << 16) | htons(15000) == 0x5000983a
- */
+#if BYTE_ORDER == LITTLE_ENDIAN
+#define IP4(a, b, c, d) ((a << 0) | (b << 8) | (c << 16) | (d << 24))
+#elif BYTE_ORDER == BIG_ENDIAN
+#define IP4(a, b, c, d) ((a << 24) | (b << 16) | (c << 8) | (d << 0))
+#endif
-static uint32_t nc_match[ ] __aligned(4) = {
+#define PORTS(a, b) ((htons(a) << 16) | htons(b))
+
+static const uint32_t nc_match[] = {
NPF_OPCODE_CMP, NPF_LAYER_3, 0,
NPF_OPCODE_BEQ, 0x0c,
- NPF_OPCODE_ETHER, 0x00, 0x00, 0x08,
+ NPF_OPCODE_ETHER, 0x00, 0x00, htons(ETHERTYPE_IP),
NPF_OPCODE_BEQ, 0x04,
NPF_OPCODE_RET, 0xff,
NPF_OPCODE_ADVR, 3,
- NPF_OPCODE_IP4MASK, 0x01, 0x0002a8c0, 24,
+ NPF_OPCODE_IP4MASK, 0x01, IP4(192,168,2,0), 24,
NPF_OPCODE_BEQ, 0x04,
NPF_OPCODE_RET, 0xff,
- NPF_OPCODE_TCP_PORTS, 0x00, 0x50005000,
+ NPF_OPCODE_TCP_PORTS, 0x00, PORTS(80, 80),
NPF_OPCODE_BEQ, 0x04,
NPF_OPCODE_RET, 0xff,
NPF_OPCODE_RET, 0x00
};
-static uint32_t nc_nmatch[ ] __aligned(4) = {
+static const uint32_t nc_nmatch[] = {
NPF_OPCODE_CMP, NPF_LAYER_3, 0,
NPF_OPCODE_BEQ, 0x0c,
- NPF_OPCODE_ETHER, 0x00, 0x00, 0x08,
+ NPF_OPCODE_ETHER, 0x00, 0x00, htons(ETHERTYPE_IP),
NPF_OPCODE_BEQ, 0x04,
NPF_OPCODE_RET, 0xff,
NPF_OPCODE_ADVR, 3,
- NPF_OPCODE_IP4MASK, 0x01, 0x0102a8c0, 32,
+ NPF_OPCODE_IP4MASK, 0x01, IP4(192,168,2,1), 32,
NPF_OPCODE_BEQ, 0x04,
NPF_OPCODE_RET, 0xff,
NPF_OPCODE_RET, 0x00
};
-static uint32_t nc_rmatch[ ] __aligned(4) = {
+static const uint32_t nc_rmatch[] = {
NPF_OPCODE_MOVE, offsetof(struct ip, ip_src), 1,
NPF_OPCODE_ADVR, 1,
NPF_OPCODE_LW, sizeof(in_addr_t), 0,
- NPF_OPCODE_CMP, 0x6402a8c0, 0,
+ NPF_OPCODE_CMP, IP4(192,168,2,100), 0,
NPF_OPCODE_BEQ, 0x04,
NPF_OPCODE_RET, 0xff,
NPF_OPCODE_MOVE, sizeof(struct ip) - offsetof(struct ip, ip_src)
+ offsetof(struct tcphdr, th_sport), 1,
NPF_OPCODE_ADVR, 1,
NPF_OPCODE_LW, 2 * sizeof(in_port_t), 0,
- NPF_OPCODE_CMP, 0x5000983a, 0,
+ NPF_OPCODE_CMP, htonl((15000 << 16) | 80), 0,
NPF_OPCODE_BEQ, 0x04,
NPF_OPCODE_RET, 0xff,
NPF_OPCODE_RET, 0x01
};
-static uint32_t nc_inval[ ] __aligned(4) = {
+static const uint32_t nc_inval[] = {
NPF_OPCODE_BEQ, 0x05,
NPF_OPCODE_RET, 0xff,
NPF_OPCODE_RET, 0x01
};
-static uint32_t nc_match6[ ] __aligned(4) = {
- NPF_OPCODE_IP6MASK, 0x01,
- 0x000080fe, 0x00000000, 0x00000000, 0x00000000, 10,
+static const uint32_t nc_match6[] = {
+ NPF_OPCODE_IP6MASK, 0x01, htonl(0xfe80 << 16), 0x0, 0x0, 0x0, 10,
NPF_OPCODE_BEQ, 0x04,
NPF_OPCODE_RET, 0xff,
- NPF_OPCODE_TCP_PORTS, 0x00, 0x50005000,
+ NPF_OPCODE_TCP_PORTS, 0x00, PORTS(80, 80),
NPF_OPCODE_BEQ, 0x04,
NPF_OPCODE_RET, 0xff,
NPF_OPCODE_RET, 0x00
@@ -116,8 +106,14 @@
static struct mbuf *
fill_packet6(int proto)
{
- uint32_t src[] = { 0x000080fe, 0x00000000, 0xffc0a002, 0x341210fe };
- uint32_t dst[] = { 0x000080fe, 0x00000000, 0xffc0a002, 0x111110fe };
+ uint16_t src[] = {
+ htons(0xfe80), 0x0, 0x0, 0x0,
+ htons(0x2a0), htons(0xc0ff), htons(0xfe10), htons(0x1234)
+ };
+ uint16_t dst[] = {
+ htons(0xfe80), 0x0, 0x0, 0x0,
+ htons(0x2a0), htons(0xc0ff), htons(0xfe10), htons(0x1111)
+ };
struct mbuf *m;
struct ip6_hdr *ip;
struct tcphdr *th;
@@ -134,15 +130,15 @@
}
static bool
-validate_retcode(const char *msg, bool verbose, int ret, int expected)
+retcode_fail_p(const char *msg, bool verbose, int ret, int expected)
{
- bool ok = (ret == expected);
+ bool fail = (ret != expected);
if (verbose) {
printf("%-25s\t%-4d == %4d\t-> %s\n",
- msg, ret, expected, ok ? "ok" : "fail");
+ msg, ret, expected, fail ? "fail" : "ok");
}
- return ok;
+ return fail;
}
bool
@@ -151,71 +147,54 @@
npf_cache_t npc;
struct mbuf *m;
int errat, ret;
+ bool fail = false;
/* Layer 2 (Ethernet + IP + TCP). */
m = fill_packet(IPPROTO_TCP, true);
ret = npf_ncode_validate(nc_match, sizeof(nc_match), &errat);
- if (!validate_retcode("Ether validation", verbose, ret, 0)) {
- return false;
- }
+ fail |= retcode_fail_p("Ether validation", verbose, ret, 0);
+
memset(&npc, 0, sizeof(npf_cache_t));
ret = npf_ncode_process(&npc, nc_match, m, NPF_LAYER_2);
- if (!validate_retcode("Ether", verbose, ret, 0)) {
- return false;
- }
+ fail |= retcode_fail_p("Ether", verbose, ret, 0);
m_freem(m);
/* Layer 3 (IP + TCP). */
m = fill_packet(IPPROTO_TCP, false);
memset(&npc, 0, sizeof(npf_cache_t));
ret = npf_ncode_process(&npc, nc_match, m, NPF_LAYER_3);
- if (!validate_retcode("IPv4 mask 1", verbose, ret, 0)) {
- return false;
- }
+ fail |= retcode_fail_p("IPv4 mask 1", verbose, ret, 0);
/* Non-matching IPv4 case. */
ret = npf_ncode_validate(nc_nmatch, sizeof(nc_nmatch), &errat);
- if (!validate_retcode("IPv4 mask 2 validation", verbose, ret, 0)) {
- return false;
- }
+ fail |= retcode_fail_p("IPv4 mask 2 validation", verbose, ret, 0);
+
memset(&npc, 0, sizeof(npf_cache_t));
ret = npf_ncode_process(&npc, nc_nmatch, m, NPF_LAYER_3);
- if (!validate_retcode("IPv4 mask 2", verbose, ret, 255)) {
- return false;
- }
+ fail |= retcode_fail_p("IPv4 mask 2", verbose, ret, 255);
/* Invalid n-code case. */
ret = npf_ncode_validate(nc_inval, sizeof(nc_inval), &errat);
- if (!validate_retcode("Invalid n-code", verbose, ret, NPF_ERR_JUMP)) {
- return false;
- }
+ fail |= retcode_fail_p("Invalid n-code", verbose, ret, NPF_ERR_JUMP);
/* RISC-like insns. */
ret = npf_ncode_validate(nc_rmatch, sizeof(nc_rmatch), &errat);
- if (!validate_retcode("RISC-like n-code validation", verbose, ret, 0)) {
- return false;
- }
+ fail |= retcode_fail_p("RISC-like n-code validation", verbose, ret, 0);
+
memset(&npc, 0, sizeof(npf_cache_t));
ret = npf_ncode_process(&npc, nc_rmatch, m, NPF_LAYER_3);
- if (!validate_retcode("RISC-like n-code", verbose, ret, 1)) {
- return false;
- }
-
+ fail |= retcode_fail_p("RISC-like n-code", verbose, ret, 1);
m_freem(m);
/* IPv6 matching. */
ret = npf_ncode_validate(nc_match6, sizeof(nc_match6), &errat);
- if (!validate_retcode("IPv6 mask validation", verbose, ret, 0)) {
- return false;
- }
+ fail |= retcode_fail_p("IPv6 mask validation", verbose, ret, 0);
+
m = fill_packet6(IPPROTO_TCP);
memset(&npc, 0, sizeof(npf_cache_t));
ret = npf_ncode_process(&npc, nc_match6, m, NPF_LAYER_3);
- if (!validate_retcode("IPv6 mask", verbose, ret, 0)) {
- return false;
- }
-
+ fail |= retcode_fail_p("IPv6 mask", verbose, ret, 0);
m_freem(m);
- return true;
+ return !fail;
}
diff -r ba665e50f731 -r 0e2ff31cba1c usr.sbin/npf/npftest/libnpftest/npf_rule_test.c
--- a/usr.sbin/npf/npftest/libnpftest/npf_rule_test.c Tue Aug 21 15:53:07 2012 +0000
+++ b/usr.sbin/npf/npftest/libnpftest/npf_rule_test.c Tue Aug 21 20:52:11 2012 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: npf_rule_test.c,v 1.1 2012/08/12 03:35:14 rmind Exp $ */
+/* $NetBSD: npf_rule_test.c,v 1.2 2012/08/21 20:52:11 rmind Exp $ */
/*
* NPF ruleset test.
@@ -99,10 +99,11 @@
bool
npf_rule_test(bool verbose)
{
+ bool fail = false;
+
for (unsigned i = 0; i < __arraycount(test_cases); i++) {
const struct test_case *t = &test_cases[i];
ifnet_t *ifp = ifunit(t->ifname);
- struct mbuf *m = fill_packet(t);
int serror, error;
if (ifp == NULL) {
Home |
Main Index |
Thread Index |
Old Index