1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
# Copyright (C) 2012 Nippon Telegraph and Telephone Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# vim: tabstop=4 shiftwidth=4 softtabstop=4
import unittest
import logging
import struct
from struct import *
from nose.tools import *
from ryu.ofproto import ether, inet
from ryu.lib.packet.ethernet import ethernet
from ryu.lib.packet.packet import Packet
from ryu.lib.packet.ipv4 import ipv4
from ryu.lib.packet.vlan import vlan
from ryu.lib.packet.vlan import svlan
LOG = logging.getLogger('test_vlan')
class Test_vlan(unittest.TestCase):
""" Test case for vlan
"""
pcp = 0
cfi = 0
vid = 32
tci = pcp << 15 | cfi << 12 | vid
ethertype = ether.ETH_TYPE_IP
buf = pack(vlan._PACK_STR, tci, ethertype)
v = vlan(pcp, cfi, vid, ethertype)
def setUp(self):
pass
def tearDown(self):
pass
def find_protocol(self, pkt, name):
for p in pkt.protocols:
if p.protocol_name == name:
return p
def test_init(self):
eq_(self.pcp, self.v.pcp)
eq_(self.cfi, self.v.cfi)
eq_(self.vid, self.v.vid)
eq_(self.ethertype, self.v.ethertype)
def test_parser(self):
res, ptype, _ = self.v.parser(self.buf)
eq_(res.pcp, self.pcp)
eq_(res.cfi, self.cfi)
eq_(res.vid, self.vid)
eq_(res.ethertype, self.ethertype)
eq_(ptype, ipv4)
def test_serialize(self):
data = bytearray()
prev = None
buf = self.v.serialize(data, prev)
fmt = vlan._PACK_STR
res = struct.unpack(fmt, buf)
eq_(res[0], self.tci)
eq_(res[1], self.ethertype)
def _build_vlan(self):
src_mac = '00:07:0d:af:f4:54'
dst_mac = '00:00:00:00:00:00'
ethertype = ether.ETH_TYPE_8021Q
e = ethernet(dst_mac, src_mac, ethertype)
version = 4
header_length = 20
tos = 0
total_length = 24
identification = 0x8a5d
flags = 0
offset = 1480
ttl = 64
proto = inet.IPPROTO_ICMP
csum = 0xa7f2
src = '131.151.32.21'
dst = '131.151.32.129'
option = b'TEST'
ip = ipv4(version, header_length, tos, total_length, identification,
flags, offset, ttl, proto, csum, src, dst, option)
p = Packet()
p.add_protocol(e)
p.add_protocol(self.v)
p.add_protocol(ip)
p.serialize()
return p
def test_build_vlan(self):
p = self._build_vlan()
e = self.find_protocol(p, "ethernet")
ok_(e)
eq_(e.ethertype, ether.ETH_TYPE_8021Q)
v = self.find_protocol(p, "vlan")
ok_(v)
eq_(v.ethertype, ether.ETH_TYPE_IP)
ip = self.find_protocol(p, "ipv4")
ok_(ip)
eq_(v.pcp, self.pcp)
eq_(v.cfi, self.cfi)
eq_(v.vid, self.vid)
eq_(v.ethertype, self.ethertype)
@raises(Exception)
def test_malformed_vlan(self):
m_short_buf = self.buf[1:vlan._MIN_LEN]
vlan.parser(m_short_buf)
def test_json(self):
jsondict = self.v.to_jsondict()
v = vlan.from_jsondict(jsondict['vlan'])
eq_(str(self.v), str(v))
class Test_svlan(unittest.TestCase):
pcp = 0
cfi = 0
vid = 32
tci = pcp << 15 | cfi << 12 | vid
ethertype = ether.ETH_TYPE_8021Q
buf = pack(svlan._PACK_STR, tci, ethertype)
sv = svlan(pcp, cfi, vid, ethertype)
def setUp(self):
pass
def tearDown(self):
pass
def find_protocol(self, pkt, name):
for p in pkt.protocols:
if p.protocol_name == name:
return p
def test_init(self):
eq_(self.pcp, self.sv.pcp)
eq_(self.cfi, self.sv.cfi)
eq_(self.vid, self.sv.vid)
eq_(self.ethertype, self.sv.ethertype)
def test_parser(self):
res, ptype, _ = self.sv.parser(self.buf)
eq_(res.pcp, self.pcp)
eq_(res.cfi, self.cfi)
eq_(res.vid, self.vid)
eq_(res.ethertype, self.ethertype)
eq_(ptype, vlan)
def test_serialize(self):
data = bytearray()
prev = None
buf = self.sv.serialize(data, prev)
fmt = svlan._PACK_STR
res = struct.unpack(fmt, buf)
eq_(res[0], self.tci)
eq_(res[1], self.ethertype)
def _build_svlan(self):
src_mac = '00:07:0d:af:f4:54'
dst_mac = '00:00:00:00:00:00'
ethertype = ether.ETH_TYPE_8021AD
e = ethernet(dst_mac, src_mac, ethertype)
pcp = 0
cfi = 0
vid = 32
tci = pcp << 15 | cfi << 12 | vid
ethertype = ether.ETH_TYPE_IP
v = vlan(pcp, cfi, vid, ethertype)
version = 4
header_length = 20
tos = 0
total_length = 24
identification = 0x8a5d
flags = 0
offset = 1480
ttl = 64
proto = inet.IPPROTO_ICMP
csum = 0xa7f2
src = '131.151.32.21'
dst = '131.151.32.129'
option = b'TEST'
ip = ipv4(version, header_length, tos, total_length, identification,
flags, offset, ttl, proto, csum, src, dst, option)
p = Packet()
p.add_protocol(e)
p.add_protocol(self.sv)
p.add_protocol(v)
p.add_protocol(ip)
p.serialize()
return p
def test_build_svlan(self):
p = self._build_svlan()
e = self.find_protocol(p, "ethernet")
ok_(e)
eq_(e.ethertype, ether.ETH_TYPE_8021AD)
sv = self.find_protocol(p, "svlan")
ok_(sv)
eq_(sv.ethertype, ether.ETH_TYPE_8021Q)
v = self.find_protocol(p, "vlan")
ok_(v)
eq_(v.ethertype, ether.ETH_TYPE_IP)
ip = self.find_protocol(p, "ipv4")
ok_(ip)
eq_(sv.pcp, self.pcp)
eq_(sv.cfi, self.cfi)
eq_(sv.vid, self.vid)
eq_(sv.ethertype, self.ethertype)
@raises(Exception)
def test_malformed_svlan(self):
m_short_buf = self.buf[1:svlan._MIN_LEN]
svlan.parser(m_short_buf)
def test_json(self):
jsondict = self.sv.to_jsondict()
sv = svlan.from_jsondict(jsondict['svlan'])
eq_(str(self.sv), str(sv))
|