1 # Copyright (C) 2012 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.
16 # vim: tabstop=4 shiftwidth=4 softtabstop=4
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
32 LOG = logging.getLogger('test_ethernet')
35 class Test_ethernet(unittest.TestCase):
36 """ Test case for ethernet
39 dst = 'aa:aa:aa:aa:aa:aa'
40 src = 'bb:bb:bb:bb:bb:bb'
41 ethertype = ether.ETH_TYPE_ARP
43 buf = pack(ethernet._PACK_STR,
44 addrconv.mac.text_to_bin(dst),
45 addrconv.mac.text_to_bin(src), ethertype)
47 e = ethernet(dst, src, ethertype)
55 def find_protocol(self, pkt, name):
56 for p in pkt.protocols:
57 if p.protocol_name == name:
61 eq_(self.dst, self.e.dst)
62 eq_(self.src, self.e.src)
63 eq_(self.ethertype, self.e.ethertype)
65 def test_parser(self):
66 res, ptype, _ = self.e.parser(self.buf)
67 LOG.debug((res, ptype))
69 eq_(res.dst, self.dst)
70 eq_(res.src, self.src)
71 eq_(res.ethertype, self.ethertype)
74 def test_serialize(self):
77 buf = self.e.serialize(data, prev)
79 fmt = ethernet._PACK_STR
80 res = struct.unpack(fmt, buf)
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)
87 def test_malformed_ethernet(self):
88 m_short_buf = self.buf[1:ethernet._MIN_LEN]
89 ethernet.parser(m_short_buf)
91 def test_default_args(self):
93 buf = e.serialize(bytearray(), None)
94 res = struct.unpack(e._PACK_STR, six.binary_type(buf))
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)
101 jsondict = self.e.to_jsondict()
102 e = ethernet.from_jsondict(jsondict['ethernet'])
103 eq_(str(self.e), str(e))