1 # Copyright (C) 2016 Nippon Telegraph and Telephone Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
19 from nose.tools import eq_
20 from nose.tools import raises
22 from ryu.lib.packet import ethernet
23 from ryu.lib.packet import vxlan
26 LOG = logging.getLogger(__name__)
29 class Test_vxlan(unittest.TestCase):
31 Test case for VXLAN (RFC 7348) header encoder/decoder class.
36 b'\x08\x00\x00\x00' # flags = R|R|R|R|I|R|R|R (8 bits)
37 b'\x12\x34\x56\x00' # vni = 0x123456 (24 bits)
38 b'test_payload' # for test
40 pkt = vxlan.vxlan(vni)
48 eq_(self.vni, self.pkt.vni)
50 def test_parser(self):
51 parsed_pkt, next_proto_cls, rest_buf = vxlan.vxlan.parser(self.buf)
52 eq_(self.vni, parsed_pkt.vni)
53 eq_(ethernet.ethernet, next_proto_cls)
54 eq_(b'test_payload', rest_buf)
56 @raises(AssertionError)
57 def test_invalid_flags(self):
59 b'\x00\x00\x00\x00' # all bits are set to zero
60 b'\x12\x34\x56\x00' # vni = 0x123456 (24 bits)
62 vxlan.vxlan.parser(invalid_flags_bug)
64 def test_serialize(self):
65 serialized_buf = self.pkt.serialize(payload=None, prev=None)
66 eq_(self.buf[:vxlan.vxlan._MIN_LEN], serialized_buf)
68 def test_from_jsondict(self):
69 pkt_from_json = vxlan.vxlan.from_jsondict(
70 self.jsondict[vxlan.vxlan.__name__])
71 eq_(self.vni, pkt_from_json.vni)
73 def test_to_jsondict(self):
74 jsondict_from_pkt = self.pkt.to_jsondict()
75 eq_(self.jsondict, jsondict_from_pkt)
77 def test_vni_from_bin(self):
78 vni = vxlan.vni_from_bin(b'\x12\x34\x56')
81 def test_vni_to_bin(self):
82 eq_(b'\x12\x34\x56', vxlan.vni_to_bin(self.vni))