backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / tests / unit / packet / test_vxlan.py
1 # Copyright (C) 2016 Nippon Telegraph and Telephone Corporation.
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
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import logging
17 import unittest
18
19 from nose.tools import eq_
20 from nose.tools import raises
21
22 from ryu.lib.packet import ethernet
23 from ryu.lib.packet import vxlan
24
25
26 LOG = logging.getLogger(__name__)
27
28
29 class Test_vxlan(unittest.TestCase):
30     """
31     Test case for VXLAN (RFC 7348) header encoder/decoder class.
32     """
33
34     vni = 0x123456
35     buf = (
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
39     )
40     pkt = vxlan.vxlan(vni)
41     jsondict = {
42         'vxlan': {
43             'vni': vni
44         }
45     }
46
47     def test_init(self):
48         eq_(self.vni, self.pkt.vni)
49
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)
55
56     @raises(AssertionError)
57     def test_invalid_flags(self):
58         invalid_flags_bug = (
59             b'\x00\x00\x00\x00'  # all bits are set to zero
60             b'\x12\x34\x56\x00'  # vni = 0x123456 (24 bits)
61         )
62         vxlan.vxlan.parser(invalid_flags_bug)
63
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)
67
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)
72
73     def test_to_jsondict(self):
74         jsondict_from_pkt = self.pkt.to_jsondict()
75         eq_(self.jsondict, jsondict_from_pkt)
76
77     def test_vni_from_bin(self):
78         vni = vxlan.vni_from_bin(b'\x12\x34\x56')
79         eq_(self.vni, vni)
80
81     def test_vni_to_bin(self):
82         eq_(b'\x12\x34\x56', vxlan.vni_to_bin(self.vni))