Zsolt Haraszti | 313c4be | 2016-12-27 11:06:53 -0800 | [diff] [blame] | 1 | from unittest import TestCase, main |
| 2 | |
| 3 | from scapy.layers.l2 import Ether, Dot1Q |
| 4 | |
| 5 | from common.frameio.frameio import BpfProgramFilter |
| 6 | |
| 7 | |
| 8 | class TestBpf(TestCase): |
| 9 | |
| 10 | def test_bpf1(self): |
| 11 | vid = 4090 |
| 12 | pcp = 7 |
| 13 | frame_match = 'ether[14:2] = 0x{:01x}{:03x}'.format(pcp << 1, vid) |
| 14 | filter = BpfProgramFilter(frame_match) |
| 15 | self.assertTrue(filter(str(Ether()/Dot1Q(prio=pcp, vlan=vid)))) |
| 16 | self.assertFalse(filter(str(Ether()/Dot1Q(prio=pcp, vlan=4000)))) |
| 17 | |
| 18 | def test_bpf2(self): |
| 19 | vid1 = 4090 |
| 20 | pcp1 = 7 |
| 21 | frame_match_case1 = 'ether[14:2] = 0x{:01x}{:03x}'.format( |
| 22 | pcp1 << 1, vid1) |
| 23 | |
| 24 | vid2 = 4000 |
| 25 | frame_match_case2 = '(ether[14:2] & 0xfff) = 0x{:03x}'.format(vid2) |
| 26 | |
| 27 | filter = BpfProgramFilter('{} or {}'.format( |
| 28 | frame_match_case1, frame_match_case2)) |
| 29 | self.assertTrue(filter(str(Ether()/Dot1Q(prio=pcp1, vlan=vid1)))) |
| 30 | self.assertTrue(filter(str(Ether()/Dot1Q(vlan=vid2)))) |
| 31 | self.assertFalse(filter(str(Ether()/Dot1Q(vlan=4001)))) |
| 32 | |
| 33 | |
| 34 | if __name__ == '__main__': |
| 35 | main() |