backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / tests / unit / packet / test_arp.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 struct
21 from struct import *
22 from nose.tools import *
23 from ryu.ofproto import ether
24 from ryu.lib.packet.ethernet import ethernet
25 from ryu.lib.packet.packet import Packet
26 from ryu.lib.packet.arp import arp
27 from ryu.lib.packet.vlan import vlan
28 from ryu.lib import addrconv
29
30
31 LOG = logging.getLogger('test_arp')
32
33
34 class Test_arp(unittest.TestCase):
35     """ Test case for arp
36     """
37
38     hwtype = 1
39     proto = 0x0800
40     hlen = 6
41     plen = 4
42     opcode = 1
43     src_mac = '00:07:0d:af:f4:54'
44     src_ip = '24.166.172.1'
45     dst_mac = '00:00:00:00:00:00'
46     dst_ip = '24.166.173.159'
47
48     fmt = arp._PACK_STR
49     buf = pack(fmt, hwtype, proto, hlen, plen, opcode,
50                addrconv.mac.text_to_bin(src_mac),
51                addrconv.ipv4.text_to_bin(src_ip),
52                addrconv.mac.text_to_bin(dst_mac),
53                addrconv.ipv4.text_to_bin(dst_ip))
54
55     a = arp(hwtype, proto, hlen, plen, opcode, src_mac, src_ip, dst_mac,
56             dst_ip)
57
58     def setUp(self):
59         pass
60
61     def tearDown(self):
62         pass
63
64     def find_protocol(self, pkt, name):
65         for p in pkt.protocols:
66             if p.protocol_name == name:
67                 return p
68
69     def test_init(self):
70         eq_(self.hwtype, self.a.hwtype)
71         eq_(self.proto, self.a.proto)
72         eq_(self.hlen, self.a.hlen)
73         eq_(self.plen, self.a.plen)
74         eq_(self.opcode, self.a.opcode)
75         eq_(self.src_mac, self.a.src_mac)
76         eq_(self.src_ip, self.a.src_ip)
77         eq_(self.dst_mac, self.a.dst_mac)
78         eq_(self.dst_ip, self.a.dst_ip)
79
80     def test_parser(self):
81         _res = self.a.parser(self.buf)
82         if type(_res) is tuple:
83             res = _res[0]
84         else:
85             res = _res
86
87         eq_(res.hwtype, self.hwtype)
88         eq_(res.proto, self.proto)
89         eq_(res.hlen, self.hlen)
90         eq_(res.plen, self.plen)
91         eq_(res.opcode, self.opcode)
92         eq_(res.src_mac, self.src_mac)
93         eq_(res.src_ip, self.src_ip)
94         eq_(res.dst_mac, self.dst_mac)
95         eq_(res.dst_ip, self.dst_ip)
96
97     def test_serialize(self):
98         data = bytearray()
99         prev = None
100         buf = self.a.serialize(data, prev)
101
102         fmt = arp._PACK_STR
103         res = struct.unpack(fmt, buf)
104
105         eq_(res[0], self.hwtype)
106         eq_(res[1], self.proto)
107         eq_(res[2], self.hlen)
108         eq_(res[3], self.plen)
109         eq_(res[4], self.opcode)
110         eq_(res[5], addrconv.mac.text_to_bin(self.src_mac))
111         eq_(res[6], addrconv.ipv4.text_to_bin(self.src_ip))
112         eq_(res[7], addrconv.mac.text_to_bin(self.dst_mac))
113         eq_(res[8], addrconv.ipv4.text_to_bin(self.dst_ip))
114
115     def _build_arp(self, vlan_enabled):
116         if vlan_enabled is True:
117             ethertype = ether.ETH_TYPE_8021Q
118             v = vlan(1, 1, 3, ether.ETH_TYPE_ARP)
119         else:
120             ethertype = ether.ETH_TYPE_ARP
121         e = ethernet(self.dst_mac, self.src_mac, ethertype)
122         p = Packet()
123
124         p.add_protocol(e)
125         if vlan_enabled is True:
126             p.add_protocol(v)
127         p.add_protocol(self.a)
128         p.serialize()
129         return p
130
131     def test_build_arp_vlan(self):
132         p = self._build_arp(True)
133
134         e = self.find_protocol(p, "ethernet")
135         ok_(e)
136         eq_(e.ethertype, ether.ETH_TYPE_8021Q)
137
138         v = self.find_protocol(p, "vlan")
139         ok_(v)
140         eq_(v.ethertype, ether.ETH_TYPE_ARP)
141
142         a = self.find_protocol(p, "arp")
143         ok_(a)
144
145         eq_(a.hwtype, self.hwtype)
146         eq_(a.proto, self.proto)
147         eq_(a.hlen, self.hlen)
148         eq_(a.plen, self.plen)
149         eq_(a.opcode, self.opcode)
150         eq_(a.src_mac, self.src_mac)
151         eq_(a.src_ip, self.src_ip)
152         eq_(a.dst_mac, self.dst_mac)
153         eq_(a.dst_ip, self.dst_ip)
154
155     def test_build_arp_novlan(self):
156         p = self._build_arp(False)
157
158         e = self.find_protocol(p, "ethernet")
159         ok_(e)
160         eq_(e.ethertype, ether.ETH_TYPE_ARP)
161
162         a = self.find_protocol(p, "arp")
163         ok_(a)
164
165         eq_(a.hwtype, self.hwtype)
166         eq_(a.proto, self.proto)
167         eq_(a.hlen, self.hlen)
168         eq_(a.plen, self.plen)
169         eq_(a.opcode, self.opcode)
170         eq_(a.src_mac, self.src_mac)
171         eq_(a.src_ip, self.src_ip)
172         eq_(a.dst_mac, self.dst_mac)
173         eq_(a.dst_ip, self.dst_ip)
174
175     @raises(Exception)
176     def test_malformed_arp(self):
177         m_short_buf = self.buf[1:arp._MIN_LEN]
178         arp.parser(m_short_buf)
179
180     def test_json(self):
181         jsondict = self.a.to_jsondict()
182         a = arp.from_jsondict(jsondict['arp'])
183         eq_(str(self.a), str(a))