backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / tests / unit / packet / test_ethernet.py
1 # Copyright (C) 2012 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 # vim: tabstop=4 shiftwidth=4 softtabstop=4
17
18 import unittest
19 import logging
20 import six
21 import struct
22 import netaddr
23 from struct import *
24 from nose.tools import *
25 from ryu.ofproto import ether, inet
26 from ryu.lib.packet.ethernet import ethernet
27 from ryu.lib.packet.packet import Packet
28 from ryu.lib.packet.arp import arp
29 from ryu.lib import addrconv
30
31
32 LOG = logging.getLogger('test_ethernet')
33
34
35 class Test_ethernet(unittest.TestCase):
36     """ Test case for ethernet
37     """
38
39     dst = 'aa:aa:aa:aa:aa:aa'
40     src = 'bb:bb:bb:bb:bb:bb'
41     ethertype = ether.ETH_TYPE_ARP
42
43     buf = pack(ethernet._PACK_STR,
44                addrconv.mac.text_to_bin(dst),
45                addrconv.mac.text_to_bin(src), ethertype)
46
47     e = ethernet(dst, src, ethertype)
48
49     def setUp(self):
50         pass
51
52     def tearDown(self):
53         pass
54
55     def find_protocol(self, pkt, name):
56         for p in pkt.protocols:
57             if p.protocol_name == name:
58                 return p
59
60     def test_init(self):
61         eq_(self.dst, self.e.dst)
62         eq_(self.src, self.e.src)
63         eq_(self.ethertype, self.e.ethertype)
64
65     def test_parser(self):
66         res, ptype, _ = self.e.parser(self.buf)
67         LOG.debug((res, ptype))
68
69         eq_(res.dst, self.dst)
70         eq_(res.src, self.src)
71         eq_(res.ethertype, self.ethertype)
72         eq_(ptype, arp)
73
74     def test_serialize(self):
75         data = bytearray()
76         prev = None
77         buf = self.e.serialize(data, prev)
78
79         fmt = ethernet._PACK_STR
80         res = struct.unpack(fmt, buf)
81
82         eq_(res[0], addrconv.mac.text_to_bin(self.dst))
83         eq_(res[1], addrconv.mac.text_to_bin(self.src))
84         eq_(res[2], self.ethertype)
85
86     @raises(Exception)
87     def test_malformed_ethernet(self):
88         m_short_buf = self.buf[1:ethernet._MIN_LEN]
89         ethernet.parser(m_short_buf)
90
91     def test_default_args(self):
92         e = ethernet()
93         buf = e.serialize(bytearray(), None)
94         res = struct.unpack(e._PACK_STR, six.binary_type(buf))
95
96         eq_(res[0], addrconv.mac.text_to_bin('ff:ff:ff:ff:ff:ff'))
97         eq_(res[1], addrconv.mac.text_to_bin('00:00:00:00:00:00'))
98         eq_(res[2], ether.ETH_TYPE_IP)
99
100     def test_json(self):
101         jsondict = self.e.to_jsondict()
102         e = ethernet.from_jsondict(jsondict['ethernet'])
103         eq_(str(self.e), str(e))