Skip to content

Commit 41fd086

Browse files
justinemariechanglan
authored andcommitted
Add testing for BPF module.
1 parent aaf0d91 commit 41fd086

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# CRASH TEST ##
2+
3+
filters = [
4+
"tcp src port 92",
5+
"len <= 1000",
6+
"ether proto 0x800",
7+
"ip proto 47 or ip6 proto 47",
8+
"ip host 22.22.22.22"
9+
]
10+
11+
filter0 = {"priority": 0, "filter": filters[0], "gate": 1}
12+
bpf0::BPF()
13+
bpf0.add(filters=[filter0])
14+
CRASH_TEST_INPUTS.append([bpf0, 1, 2])
15+
16+
bpf1::BPF()
17+
for i, exp in enumerate(filters):
18+
bpf1.add(filters=[{"priority": i, "filter": exp, "gate": i}])
19+
20+
CRASH_TEST_INPUTS.append([bpf1, 1, len(filters)])
21+
22+
# OUTPUT TEST ##
23+
24+
# Test basic output/steering with single rule
25+
bpf2::BPF()
26+
bpf2.add(filters=[filter0])
27+
packet1 = gen_packet(scapy.UDP, '12.34.56.78', '12.34.56.78')
28+
packet2 = gen_packet(scapy.TCP, '12.34.56.78', '12.34.56.78', srcport=92)
29+
30+
OUTPUT_TEST_INPUTS.append([bpf2, 1, 2,
31+
[{'input_port': 0,
32+
'input_packet': packet1,
33+
'output_port': 0,
34+
'output_packet': packet1},
35+
{'input_port': 0,
36+
'input_packet': packet2,
37+
'output_port': 1,
38+
'output_packet': packet2}]])
39+
40+
# Test multiple rules with priorities
41+
bpf3::BPF()
42+
bpf3.add(filters=[{"priority": 2, "filter": filters[0], "gate": 1}])
43+
bpf3.add(filters=[{"priority": 1, "filter": filters[4], "gate": 2}])
44+
packet1 = gen_packet(scapy.UDP, '22.22.22.22', '12.34.56.78', srcport=700)
45+
packet2 = gen_packet(scapy.TCP, '12.34.56.78', '22.22.22.22', srcport=92)
46+
packet3 = gen_packet(scapy.TCP, '12.34.56.78', '12.34.56.78', srcport=700)
47+
48+
OUTPUT_TEST_INPUTS.append([bpf3, 1, 3,
49+
[{'input_port': 0,
50+
'input_packet': packet1,
51+
'output_port': 2,
52+
'output_packet': packet1},
53+
{'input_port': 0,
54+
'input_packet': packet2,
55+
'output_port': 1,
56+
'output_packet': packet2},
57+
{'input_port': 0,
58+
'input_packet': packet3,
59+
'output_port': 0,
60+
'output_packet': packet3}]])

bessctl/conf/testing/run_module_tests.bess

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ def gen_socket_and_port(sockname):
3131
# All the other fields -- Ether, ports -- are dummy values.
3232

3333

34-
def gen_packet(proto, src_ip, dst_ip, ip_ttl=64):
34+
def gen_packet(proto, src_ip, dst_ip, ip_ttl=64, srcport=1001, dstport=1002):
3535
eth = scapy.Ether(src='02:1e:67:9f:4d:ae', dst='06:16:3e:1b:72:32')
3636
ip = scapy.IP(src=src_ip, dst=dst_ip, ttl=ip_ttl)
37-
udp = proto(sport=10001, dport=10002)
37+
udp = proto(sport=srcport, dport=dstport)
3838
payload = 'helloworld'
3939
pkt = eth / ip / udp / payload
4040
return pkt

0 commit comments

Comments
 (0)