xref: /netbsd-src/tests/net/bpf/t_div-by-zero.c (revision 513ca2875bd4ed3518c25c80f56c6ccedecc3022)
1 #include <sys/types.h>
2 #include <sys/ioctl.h>
3 
4 #include <net/bpf.h>
5 
6 #include <atf-c.h>
7 #include <fcntl.h>
8 
9 #include <rump/rump.h>
10 #include <rump/rumpuser.h>
11 
12 ATF_TC(div_by_zero);
13 ATF_TC_HEAD(div_by_zero, tc)
14 {
15 
16 	atf_tc_set_md_var(tc, "descr", "Check that BPF rejects a filter "
17 	    "which divides by 0");
18 }
19 
20 ATF_TC_BODY(div_by_zero, tc)
21 {
22 	struct bpf_program bp;
23 	struct bpf_insn bins[] = {
24 	    { 0x20, 0, 0, 0x00000000 },
25 	    { 0x34, 0, 0, 0x00000000 },
26 	    { 0x15, 0, 1, 0x00000002 },
27 	    { 0x6, 0, 0, 0x00000060 },
28 	    { 0x6, 0, 0, 0x00000000 },
29 	};
30 	int fd;
31 
32 	bp.bf_len = __arraycount(bins);
33 	bp.bf_insns = bins;
34 
35 	rump_init();
36 	fd = rump_sys_open("/dev/bpf", O_RDWR);
37 	if (fd == -1)
38 		err(1, "open bpf");
39 	ATF_REQUIRE_EQ_MSG(rump_sys_ioctl(fd, BIOCSETF, &bp), -1,
40 	    "bpf accepted program with division by zero");
41 }
42 
43 ATF_TP_ADD_TCS(tp)
44 {
45 
46 	ATF_TP_ADD_TC(tp, div_by_zero);
47 	return atf_no_error();
48 }
49