Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/lib/libbpfjit Add libbpfjit test.



details:   https://anonhg.NetBSD.org/src/rev/b1ba89722d23
branches:  trunk
changeset: 782645:b1ba89722d23
user:      alnsn <alnsn%NetBSD.org@localhost>
date:      Sun Nov 11 17:37:34 2012 +0000

description:
Add libbpfjit test.

diffstat:

 tests/lib/libbpfjit/Makefile   |    21 +
 tests/lib/libbpfjit/t_bpfjit.c |  3374 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 3395 insertions(+), 0 deletions(-)

diffs (truncated from 3403 to 300 lines):

diff -r 7c6d5482a67e -r b1ba89722d23 tests/lib/libbpfjit/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libbpfjit/Makefile      Sun Nov 11 17:37:34 2012 +0000
@@ -0,0 +1,21 @@
+# $NetBSD: Makefile,v 1.1 2012/11/11 17:37:34 alnsn Exp $
+
+.include <bsd.own.mk>
+.include <../../../external/bsd/sljit/Makefile.inc>
+
+TESTSDIR=      ${TESTSBASE}/lib/libbpfjit
+
+TESTS_C+=      t_bpfjit
+
+# XXX this variable doesn't belong to here
+LIBBPFJITDIR!= cd ${NETBSDSRCDIR}/lib/libbpfjit && ${PRINTOBJDIR}
+
+LDADD+=                -L${LIBBPFJITDIR} -lbpfjit
+DPADD+=                ${LIBBPFJITDIR}/libbpfjit.a
+
+LDADD+=                -L${LIBSLJITDIR} -lsljit
+DPADD+=                ${LIBSLJITDIR}/libsljit.a
+
+LDADD+=                ${LIBPCAP}
+
+.include <bsd.test.mk>
diff -r 7c6d5482a67e -r b1ba89722d23 tests/lib/libbpfjit/t_bpfjit.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libbpfjit/t_bpfjit.c    Sun Nov 11 17:37:34 2012 +0000
@@ -0,0 +1,3374 @@
+/*     $NetBSD: t_bpfjit.c,v 1.1 2012/11/11 17:37:34 alnsn Exp $ */
+
+/*-
+ * Copyright (c) 2011-2012 Alexander Nasonov.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: t_bpfjit.c,v 1.1 2012/11/11 17:37:34 alnsn Exp $");
+
+#include <net/bpfjit.h>
+
+#include <atf-c.h>
+#include <stdint.h>
+#include <string.h>
+
+static uint8_t deadbeef_at_5[16] = {
+       0, 0xf1, 2, 0xf3, 4, 0xde, 0xad, 0xbe, 0xef, 0xff
+};
+
+ATF_TC(bpfjit_empty);
+ATF_TC_HEAD(bpfjit_empty, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Test that JIT compilation for an empty bpf program fails");
+}
+
+ATF_TC_BODY(bpfjit_empty, tc)
+{
+       struct bpf_insn dummy;
+
+       ATF_CHECK(bpfjit_generate_code(&dummy, 0) == NULL);
+}
+
+ATF_TC(bpfjit_alu_add_k);
+ATF_TC_HEAD(bpfjit_alu_add_k, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Test JIT compilation of BPF_ALU+BPF_ADD+BPF_K");
+}
+
+ATF_TC_BODY(bpfjit_alu_add_k, tc)
+{
+       static struct bpf_insn insns[] = {
+               BPF_STMT(BPF_LD+BPF_IMM, 3),
+               BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, 2),
+               BPF_STMT(BPF_RET+BPF_A, 0)
+       };
+
+       bpfjit_function_t code;
+       uint8_t pkt[1]; /* the program doesn't read any data */
+
+       size_t insn_count = sizeof(insns) / sizeof(insns[0]);
+
+       ATF_CHECK(bpf_validate(insns, insn_count));
+
+       code = bpfjit_generate_code(insns, insn_count);
+       ATF_REQUIRE(code != NULL);
+
+       ATF_CHECK(code(pkt, 1, 1) == 5);
+
+       bpfjit_free_code(code);
+}
+
+ATF_TC(bpfjit_alu_sub_k);
+ATF_TC_HEAD(bpfjit_alu_sub_k, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Test JIT compilation of BPF_ALU+BPF_SUB+BPF_K");
+}
+
+ATF_TC_BODY(bpfjit_alu_sub_k, tc)
+{
+       static struct bpf_insn insns[] = {
+               BPF_STMT(BPF_LD+BPF_IMM, 1),
+               BPF_STMT(BPF_ALU+BPF_SUB+BPF_K, 2),
+               BPF_STMT(BPF_RET+BPF_A, 0)
+       };
+
+       bpfjit_function_t code;
+       uint8_t pkt[1]; /* the program doesn't read any data */
+
+       size_t insn_count = sizeof(insns) / sizeof(insns[0]);
+
+       ATF_CHECK(bpf_validate(insns, insn_count));
+
+       code = bpfjit_generate_code(insns, insn_count);
+       ATF_REQUIRE(code != NULL);
+
+       ATF_CHECK(code(pkt, 1, 1) == UINT32_MAX);
+
+       bpfjit_free_code(code);
+}
+
+ATF_TC(bpfjit_alu_mul_k);
+ATF_TC_HEAD(bpfjit_alu_mul_k, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Test JIT compilation of BPF_ALU+BPF_MUL+BPF_K");
+}
+
+ATF_TC_BODY(bpfjit_alu_mul_k, tc)
+{
+       static struct bpf_insn insns[] = {
+               BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffff)),
+               BPF_STMT(BPF_ALU+BPF_MUL+BPF_K, 3),
+               BPF_STMT(BPF_RET+BPF_A, 0)
+       };
+
+       bpfjit_function_t code;
+       uint8_t pkt[1]; /* the program doesn't read any data */
+
+       size_t insn_count = sizeof(insns) / sizeof(insns[0]);
+
+       ATF_CHECK(bpf_validate(insns, insn_count));
+
+       code = bpfjit_generate_code(insns, insn_count);
+       ATF_REQUIRE(code != NULL);
+
+       ATF_CHECK(code(pkt, 1, 1) == UINT32_C(0xfffffffd));
+
+       bpfjit_free_code(code);
+}
+
+ATF_TC(bpfjit_alu_div0_k);
+ATF_TC_HEAD(bpfjit_alu_div0_k, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_K with k=0");
+}
+
+ATF_TC_BODY(bpfjit_alu_div0_k, tc)
+{
+       static struct bpf_insn insns[] = {
+               BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, 0),
+               BPF_STMT(BPF_RET+BPF_A, 0)
+       };
+
+       bpfjit_function_t code;
+       uint8_t pkt[1]; /* the program doesn't read any data */
+
+       size_t insn_count = sizeof(insns) / sizeof(insns[0]);
+
+       //ATF_CHECK(bpf_validate(insns, insn_count));
+
+       code = bpfjit_generate_code(insns, insn_count);
+       ATF_REQUIRE(code != NULL);
+
+       ATF_CHECK(code(pkt, 1, 1) == 0);
+
+       bpfjit_free_code(code);
+}
+
+ATF_TC(bpfjit_alu_div1_k);
+ATF_TC_HEAD(bpfjit_alu_div1_k, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_K with k=1");
+}
+
+ATF_TC_BODY(bpfjit_alu_div1_k, tc)
+{
+       static struct bpf_insn insns[] = {
+               BPF_STMT(BPF_LD+BPF_IMM, 7),
+               BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, 1),
+               BPF_STMT(BPF_RET+BPF_A, 0)
+       };
+
+       bpfjit_function_t code;
+       uint8_t pkt[1]; /* the program doesn't read any data */
+
+       size_t insn_count = sizeof(insns) / sizeof(insns[0]);
+
+       ATF_CHECK(bpf_validate(insns, insn_count));
+
+       code = bpfjit_generate_code(insns, insn_count);
+       ATF_REQUIRE(code != NULL);
+
+       ATF_CHECK(code(pkt, 1, 1) == 7);
+
+       bpfjit_free_code(code);
+}
+
+ATF_TC(bpfjit_alu_div2_k);
+ATF_TC_HEAD(bpfjit_alu_div2_k, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_K with k=2");
+}
+
+ATF_TC_BODY(bpfjit_alu_div2_k, tc)
+{
+       static struct bpf_insn insns[] = {
+               BPF_STMT(BPF_LD+BPF_IMM, 7),
+               BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, 2),
+               BPF_STMT(BPF_RET+BPF_A, 0)
+       };
+
+       bpfjit_function_t code;
+       uint8_t pkt[1]; /* the program doesn't read any data */
+
+       size_t insn_count = sizeof(insns) / sizeof(insns[0]);
+
+       ATF_CHECK(bpf_validate(insns, insn_count));
+
+       code = bpfjit_generate_code(insns, insn_count);
+       ATF_REQUIRE(code != NULL);
+
+       ATF_CHECK(code(pkt, 1, 1) == 3);
+
+       bpfjit_free_code(code);
+}
+
+ATF_TC(bpfjit_alu_div4_k);
+ATF_TC_HEAD(bpfjit_alu_div4_k, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_K with k=4");
+}
+
+ATF_TC_BODY(bpfjit_alu_div4_k, tc)
+{
+       static struct bpf_insn insns[] = {
+               BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffff)),
+               BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, 4),
+               BPF_STMT(BPF_RET+BPF_A, 0)
+       };
+
+       bpfjit_function_t code;
+       uint8_t pkt[1]; /* the program doesn't read any data */
+
+       size_t insn_count = sizeof(insns) / sizeof(insns[0]);
+
+       ATF_CHECK(bpf_validate(insns, insn_count));
+
+       code = bpfjit_generate_code(insns, insn_count);
+       ATF_REQUIRE(code != NULL);
+
+       ATF_CHECK(code(pkt, 1, 1) == UINT32_C(0x3fffffff));
+
+       bpfjit_free_code(code);
+}
+
+ATF_TC(bpfjit_alu_div10_k);
+ATF_TC_HEAD(bpfjit_alu_div10_k, tc)
+{
+       atf_tc_set_md_var(tc, "descr",



Home | Main Index | Thread Index | Old Index