blob: 38f10f67c378bae81c5288dbc687805563117cd1 [file] [log] [blame]
Zack Williams41513bf2018-07-07 20:08:35 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
Zsolt Haraszti313c4be2016-12-27 11:06:53 -080014from unittest import TestCase, main
15
16from scapy.layers.l2 import Ether, Dot1Q
17
18from common.frameio.frameio import BpfProgramFilter
19
20
21class TestBpf(TestCase):
22
23 def test_bpf1(self):
24 vid = 4090
25 pcp = 7
26 frame_match = 'ether[14:2] = 0x{:01x}{:03x}'.format(pcp << 1, vid)
27 filter = BpfProgramFilter(frame_match)
28 self.assertTrue(filter(str(Ether()/Dot1Q(prio=pcp, vlan=vid))))
29 self.assertFalse(filter(str(Ether()/Dot1Q(prio=pcp, vlan=4000))))
30
31 def test_bpf2(self):
32 vid1 = 4090
33 pcp1 = 7
34 frame_match_case1 = 'ether[14:2] = 0x{:01x}{:03x}'.format(
35 pcp1 << 1, vid1)
36
37 vid2 = 4000
38 frame_match_case2 = '(ether[14:2] & 0xfff) = 0x{:03x}'.format(vid2)
39
40 filter = BpfProgramFilter('{} or {}'.format(
41 frame_match_case1, frame_match_case2))
42 self.assertTrue(filter(str(Ether()/Dot1Q(prio=pcp1, vlan=vid1))))
43 self.assertTrue(filter(str(Ether()/Dot1Q(vlan=vid2))))
44 self.assertFalse(filter(str(Ether()/Dot1Q(vlan=4001))))
45
46
47if __name__ == '__main__':
48 main()