backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / tests / unit / ofproto / test_parser_v12.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 socket
22 from struct import *
23 from nose.tools import *
24 from ryu.ofproto.ofproto_v1_2_parser import *
25 from ryu.ofproto import ofproto_v1_2_parser
26 from ryu.ofproto import ofproto_v1_2
27 from ryu.ofproto import ofproto_protocol
28 from ryu.ofproto import ether
29 from ryu.ofproto.ofproto_parser import MsgBase
30 from ryu import utils
31 from ryu.lib import addrconv
32 from ryu.lib import pack_utils
33
34 LOG = logging.getLogger('test_ofproto_v12')
35
36
37 _Datapath = ofproto_protocol.ProtocolDesc(version=ofproto_v1_2.OFP_VERSION)
38
39
40 class TestRegisterParser(unittest.TestCase):
41     """ Test case for ofproto_v1_2_parser._register_parser
42     """
43
44     class _OFPDummy(MsgBase):
45         def __init__(self, datapath):
46             self.dummy = 'dummy'
47
48         def parser(self):
49             return self.dummy
50
51     def test_cls_msg_type(self):
52         msg_type = 0xff
53         cls = self._OFPDummy(_Datapath)
54         cls.cls_msg_type = msg_type
55
56         res = ofproto_v1_2_parser._register_parser(cls)
57         res_parser = ofproto_v1_2_parser._MSG_PARSERS[msg_type]
58         del ofproto_v1_2_parser._MSG_PARSERS[msg_type]
59
60         eq_(res.cls_msg_type, msg_type)
61         ok_(res.dummy)
62         eq_(res_parser(), 'dummy')
63
64     @raises(AssertionError)
65     def test_cls_msg_type_none(self):
66         cls = OFPHello(_Datapath)
67         cls.cls_msg_type = None
68         ofproto_v1_2_parser._register_parser(cls)
69
70     @raises(AssertionError)
71     def test_cls_msg_type_already_registed(self):
72         cls = OFPHello(_Datapath)
73         ofproto_v1_2_parser._register_parser(cls)
74
75
76 class TestMsgParser(unittest.TestCase):
77     """ Test case for ofproto_v1_2_parser.msg_parser
78     """
79
80     def _test_msg_parser(self, xid, msg_len):
81         # OFP_HEADER_PACK_STR
82         # '!BBHI'...version, msg_type, msg_len, xid
83         version = ofproto.OFP_VERSION
84         msg_type = ofproto.OFPT_HELLO
85
86         fmt = ofproto.OFP_HEADER_PACK_STR
87         buf = pack(fmt, version, msg_type, msg_len, xid)
88
89         c = msg_parser(_Datapath, version, msg_type, msg_len, xid, buf)
90
91         eq_(version, c.version)
92         eq_(msg_type, c.msg_type)
93         eq_(msg_len, c.msg_len)
94         eq_(xid, c.xid)
95
96         # buf
97         fmt = ofproto.OFP_HEADER_PACK_STR
98         res = struct.unpack(fmt, c.buf)
99
100         eq_(version, res[0])
101         eq_(msg_type, res[1])
102         eq_(msg_len, res[2])
103         eq_(xid, res[3])
104
105     def test_parser_mid(self):
106         xid = 2147483648
107         msg_len = 8
108         self._test_msg_parser(xid, msg_len)
109
110     def test_parser_max(self):
111         xid = 4294967295
112         msg_len = 65535
113         self._test_msg_parser(xid, msg_len)
114
115     def test_parser_min(self):
116         xid = 0
117         msg_len = 0
118         self._test_msg_parser(xid, msg_len)
119
120
121 class TestOFPHello(unittest.TestCase):
122     """ Test case for ofproto_v1_2_parser.OFPHello
123     """
124
125     def _test_parser(self, xid):
126         version = ofproto.OFP_VERSION
127         msg_type = ofproto.OFPT_HELLO
128         msg_len = ofproto.OFP_HEADER_SIZE
129
130         fmt = ofproto.OFP_HEADER_PACK_STR
131         buf = pack(fmt, version, msg_type, msg_len, xid)
132
133         res = OFPHello.parser(object, version, msg_type, msg_len, xid,
134                               bytearray(buf))
135
136         eq_(version, res.version)
137         eq_(msg_type, res.msg_type)
138         eq_(msg_len, res.msg_len)
139         eq_(xid, res.xid)
140         eq_(six.binary_type(buf), six.binary_type(res.buf))
141
142     def test_parser_xid_min(self):
143         xid = 0
144         self._test_parser(xid)
145
146     def test_parser_xid_mid(self):
147         xid = 2183948390
148         self._test_parser(xid)
149
150     def test_parser_xid_max(self):
151         xid = 4294967295
152         self._test_parser(xid)
153
154     def test_serialize(self):
155         c = OFPHello(_Datapath)
156         c.serialize()
157         eq_(ofproto.OFP_VERSION, c.version)
158         eq_(ofproto.OFPT_HELLO, c.msg_type)
159         eq_(0, c.xid)
160
161
162 class TestOFPErrorMsg(unittest.TestCase):
163     """ Test case for ofproto_v1_2_parser.OFPErrorMsg
164     """
165
166     # OFP_HEADER_PACK_STR
167     # '!BBHI'...version, msg_type, msg_len, xid
168     version = ofproto.OFP_VERSION
169     msg_type = ofproto.OFPT_ERROR
170     msg_len = ofproto.OFP_ERROR_MSG_SIZE
171     xid = 2495926989
172
173     fmt = ofproto.OFP_HEADER_PACK_STR
174     buf = pack(fmt, version, msg_type, msg_len, xid)
175
176     def test_init(self):
177         c = OFPErrorMsg(_Datapath)
178         eq_(c.code, None)
179         eq_(c.type, None)
180         eq_(c.data, None)
181
182     def _test_parser(self, type_, code, data=None):
183
184         # OFP_ERROR_MSG_PACK_STR = '!HH'
185         fmt = ofproto.OFP_ERROR_MSG_PACK_STR
186         buf = self.buf + pack(fmt, type_, code)
187
188         if data is not None:
189             buf += data
190
191         res = OFPErrorMsg.parser(object, self.version, self.msg_type,
192                                  self.msg_len, self.xid, buf)
193
194         eq_(res.version, self.version)
195         eq_(res.msg_type, self.msg_type)
196         eq_(res.msg_len, self.msg_len)
197         eq_(res.xid, self.xid)
198         eq_(res.type, type_)
199         eq_(res.code, code)
200
201         if data is not None:
202             eq_(res.data, data)
203
204     def test_parser_mid(self):
205         type_ = 32768
206         code = 32768
207         data = b'Error Message.'
208         self._test_parser(type_, code, data)
209
210     def test_parser_max(self):
211         type_ = 65534
212         code = 65535
213         data = b'Error Message.'.ljust(65523)
214         self._test_parser(type_, code, data)
215
216     def test_parser_min(self):
217         type_ = 0
218         code = 0
219         data = None
220         self._test_parser(type_, code, data)
221
222     def test_parser_p0_1(self):
223         type_ = ofproto.OFPET_HELLO_FAILED
224         code = ofproto.OFPHFC_EPERM
225         data = b'Error Message.'
226         self._test_parser(type_, code, data)
227
228     def test_parser_p1_0(self):
229         type_ = ofproto.OFPET_BAD_REQUEST
230         code = ofproto.OFPBRC_BAD_VERSION
231         data = b'Error Message.'
232         self._test_parser(type_, code, data)
233
234     def test_parser_p1_1(self):
235         type_ = ofproto.OFPET_BAD_REQUEST
236         code = ofproto.OFPBRC_BAD_TYPE
237         data = b'Error Message.'
238         self._test_parser(type_, code, data)
239
240     def test_parser_p1_2(self):
241         type_ = ofproto.OFPET_BAD_REQUEST
242         code = ofproto.OFPBRC_BAD_STAT
243         data = b'Error Message.'
244         self._test_parser(type_, code, data)
245
246     def test_parser_p1_3(self):
247         type_ = ofproto.OFPET_BAD_REQUEST
248         code = ofproto.OFPBRC_BAD_EXPERIMENTER
249         data = b'Error Message.'
250         self._test_parser(type_, code, data)
251
252     def test_parser_p1_4(self):
253         type_ = ofproto.OFPET_BAD_REQUEST
254         code = ofproto.OFPBRC_BAD_EXP_TYPE
255         data = b'Error Message.'
256         self._test_parser(type_, code, data)
257
258     def test_parser_p1_5(self):
259         type_ = ofproto.OFPET_BAD_REQUEST
260         code = ofproto.OFPBRC_EPERM
261         data = b'Error Message.'
262         self._test_parser(type_, code, data)
263
264     def test_parser_p1_6(self):
265         type_ = ofproto.OFPET_BAD_REQUEST
266         code = ofproto.OFPBRC_BAD_LEN
267         data = b'Error Message.'
268         self._test_parser(type_, code, data)
269
270     def test_parser_p1_7(self):
271         type_ = ofproto.OFPET_BAD_REQUEST
272         code = ofproto.OFPBRC_BUFFER_EMPTY
273         data = b'Error Message.'
274         self._test_parser(type_, code, data)
275
276     def test_parser_p1_8(self):
277         type_ = ofproto.OFPET_BAD_REQUEST
278         code = ofproto.OFPBRC_BUFFER_UNKNOWN
279         data = b'Error Message.'
280         self._test_parser(type_, code, data)
281
282     def test_parser_p1_9(self):
283         type_ = ofproto.OFPET_BAD_REQUEST
284         code = ofproto.OFPBRC_BAD_TABLE_ID
285         data = b'Error Message.'
286         self._test_parser(type_, code, data)
287
288     def test_parser_p1_10(self):
289         type_ = ofproto.OFPET_BAD_REQUEST
290         code = ofproto.OFPBRC_IS_SLAVE
291         data = b'Error Message.'
292         self._test_parser(type_, code, data)
293
294     def test_parser_p1_11(self):
295         type_ = ofproto.OFPET_BAD_REQUEST
296         code = ofproto.OFPBRC_BAD_PORT
297         data = b'Error Message.'
298         self._test_parser(type_, code, data)
299
300     def test_parser_p1_12(self):
301         type_ = ofproto.OFPET_BAD_REQUEST
302         code = ofproto.OFPBRC_BAD_PACKET
303         data = b'Error Message.'
304         self._test_parser(type_, code, data)
305
306     def test_parser_p2_0(self):
307         type_ = ofproto.OFPET_BAD_ACTION
308         code = ofproto.OFPBAC_BAD_TYPE
309         data = b'Error Message.'
310         self._test_parser(type_, code, data)
311
312     def test_parser_p2_1(self):
313         type_ = ofproto.OFPET_BAD_ACTION
314         code = ofproto.OFPBAC_BAD_LEN
315         data = b'Error Message.'
316         self._test_parser(type_, code, data)
317
318     def test_parser_p2_2(self):
319         type_ = ofproto.OFPET_BAD_ACTION
320         code = ofproto.OFPBAC_BAD_EXPERIMENTER
321         data = b'Error Message.'
322         self._test_parser(type_, code, data)
323
324     def test_parser_p2_3(self):
325         type_ = ofproto.OFPET_BAD_ACTION
326         code = ofproto.OFPBAC_BAD_EXP_TYPE
327         data = b'Error Message.'
328         self._test_parser(type_, code, data)
329
330     def test_parser_p2_4(self):
331         type_ = ofproto.OFPET_BAD_ACTION
332         code = ofproto.OFPBAC_BAD_OUT_PORT
333         data = b'Error Message.'
334         self._test_parser(type_, code, data)
335
336     def test_parser_p2_5(self):
337         type_ = ofproto.OFPET_BAD_ACTION
338         code = ofproto.OFPBAC_BAD_ARGUMENT
339         data = b'Error Message.'
340         self._test_parser(type_, code, data)
341
342     def test_parser_p2_6(self):
343         type_ = ofproto.OFPET_BAD_ACTION
344         code = ofproto.OFPBAC_EPERM
345         data = b'Error Message.'
346         self._test_parser(type_, code, data)
347
348     def test_parser_p2_7(self):
349         type_ = ofproto.OFPET_BAD_ACTION
350         code = ofproto.OFPBAC_TOO_MANY
351         data = b'Error Message.'
352         self._test_parser(type_, code, data)
353
354     def test_parser_p2_8(self):
355         type_ = ofproto.OFPET_BAD_ACTION
356         code = ofproto.OFPBAC_BAD_QUEUE
357         data = b'Error Message.'
358         self._test_parser(type_, code, data)
359
360     def test_parser_p2_9(self):
361         type_ = ofproto.OFPET_BAD_ACTION
362         code = ofproto.OFPBAC_BAD_OUT_GROUP
363         data = b'Error Message.'
364         self._test_parser(type_, code, data)
365
366     def test_parser_p2_10(self):
367         type_ = ofproto.OFPET_BAD_ACTION
368         code = ofproto.OFPBAC_MATCH_INCONSISTENT
369         data = b'Error Message.'
370         self._test_parser(type_, code, data)
371
372     def test_parser_p2_11(self):
373         type_ = ofproto.OFPET_BAD_ACTION
374         code = ofproto.OFPBAC_UNSUPPORTED_ORDER
375         data = b'Error Message.'
376         self._test_parser(type_, code, data)
377
378     def test_parser_p2_12(self):
379         type_ = ofproto.OFPET_BAD_ACTION
380         code = ofproto.OFPBAC_BAD_TAG
381         data = b'Error Message.'
382         self._test_parser(type_, code, data)
383
384     def test_parser_p2_13(self):
385         type_ = ofproto.OFPET_BAD_ACTION
386         code = ofproto.OFPBAC_BAD_SET_TYPE
387         data = b'Error Message.'
388         self._test_parser(type_, code, data)
389
390     def test_parser_p2_14(self):
391         type_ = ofproto.OFPET_BAD_ACTION
392         code = ofproto.OFPBAC_BAD_SET_LEN
393         data = b'Error Message.'
394         self._test_parser(type_, code, data)
395
396     def test_parser_p2_15(self):
397         type_ = ofproto.OFPET_BAD_ACTION
398         code = ofproto.OFPBAC_BAD_SET_ARGUMENT
399         data = b'Error Message.'
400         self._test_parser(type_, code, data)
401
402     def test_parser_p3_0(self):
403         type_ = ofproto.OFPET_BAD_INSTRUCTION
404         code = ofproto.OFPBIC_UNKNOWN_INST
405         data = b'Error Message.'
406         self._test_parser(type_, code, data)
407
408     def test_parser_p3_1(self):
409         type_ = ofproto.OFPET_BAD_INSTRUCTION
410         code = ofproto.OFPBIC_UNSUP_INST
411         data = b'Error Message.'
412         self._test_parser(type_, code, data)
413
414     def test_parser_p3_2(self):
415         type_ = ofproto.OFPET_BAD_INSTRUCTION
416         code = ofproto.OFPBIC_BAD_TABLE_ID
417         data = b'Error Message.'
418         self._test_parser(type_, code, data)
419
420     def test_parser_p3_3(self):
421         type_ = ofproto.OFPET_BAD_INSTRUCTION
422         code = ofproto.OFPBIC_UNSUP_METADATA
423         data = b'Error Message.'
424         self._test_parser(type_, code, data)
425
426     def test_parser_p3_4(self):
427         type_ = ofproto.OFPET_BAD_INSTRUCTION
428         code = ofproto.OFPBIC_UNSUP_METADATA_MASK
429         data = b'Error Message.'
430         self._test_parser(type_, code, data)
431
432     def test_parser_p3_5(self):
433         type_ = ofproto.OFPET_BAD_INSTRUCTION
434         code = ofproto.OFPBIC_BAD_EXPERIMENTER
435         data = b'Error Message.'
436         self._test_parser(type_, code, data)
437
438     def test_parser_p3_6(self):
439         type_ = ofproto.OFPET_BAD_INSTRUCTION
440         code = ofproto.OFPBIC_BAD_EXP_TYPE
441         data = b'Error Message.'
442         self._test_parser(type_, code, data)
443
444     def test_parser_p3_7(self):
445         type_ = ofproto.OFPET_BAD_INSTRUCTION
446         code = ofproto.OFPBIC_BAD_LEN
447         data = b'Error Message.'
448         self._test_parser(type_, code, data)
449
450     def test_parser_p3_8(self):
451         type_ = ofproto.OFPET_BAD_INSTRUCTION
452         code = ofproto.OFPBIC_EPERM
453         data = b'Error Message.'
454         self._test_parser(type_, code, data)
455
456     def test_parser_p4_0(self):
457         type_ = ofproto.OFPET_BAD_MATCH
458         code = ofproto.OFPBMC_BAD_TYPE
459         data = b'Error Message.'
460         self._test_parser(type_, code, data)
461
462     def test_parser_p4_1(self):
463         type_ = ofproto.OFPET_BAD_MATCH
464         code = ofproto.OFPBMC_BAD_LEN
465         data = b'Error Message.'
466         self._test_parser(type_, code, data)
467
468     def test_parser_p4_2(self):
469         type_ = ofproto.OFPET_BAD_MATCH
470         code = ofproto.OFPBMC_BAD_TAG
471         data = b'Error Message.'
472         self._test_parser(type_, code, data)
473
474     def test_parser_p4_3(self):
475         type_ = ofproto.OFPET_BAD_MATCH
476         code = ofproto.OFPBMC_BAD_DL_ADDR_MASK
477         data = b'Error Message.'
478         self._test_parser(type_, code, data)
479
480     def test_parser_p4_4(self):
481         type_ = ofproto.OFPET_BAD_MATCH
482         code = ofproto.OFPBMC_BAD_NW_ADDR_MASK
483         data = b'Error Message.'
484         self._test_parser(type_, code, data)
485
486     def test_parser_p4_5(self):
487         type_ = ofproto.OFPET_BAD_MATCH
488         code = ofproto.OFPBMC_BAD_WILDCARDS
489         data = b'Error Message.'
490         self._test_parser(type_, code, data)
491
492     def test_parser_p4_6(self):
493         type_ = ofproto.OFPET_BAD_MATCH
494         code = ofproto.OFPBMC_BAD_FIELD
495         data = b'Error Message.'
496         self._test_parser(type_, code, data)
497
498     def test_parser_p4_7(self):
499         type_ = ofproto.OFPET_BAD_MATCH
500         code = ofproto.OFPBMC_BAD_VALUE
501         data = b'Error Message.'
502         self._test_parser(type_, code, data)
503
504     def test_parser_p4_8(self):
505         type_ = ofproto.OFPET_BAD_MATCH
506         code = ofproto.OFPBMC_BAD_MASK
507         data = b'Error Message.'
508         self._test_parser(type_, code, data)
509
510     def test_parser_p4_9(self):
511         type_ = ofproto.OFPET_BAD_MATCH
512         code = ofproto.OFPBMC_BAD_PREREQ
513         data = b'Error Message.'
514         self._test_parser(type_, code, data)
515
516     def test_parser_p4_10(self):
517         type_ = ofproto.OFPET_BAD_MATCH
518         code = ofproto.OFPBMC_DUP_FIELD
519         data = b'Error Message.'
520         self._test_parser(type_, code, data)
521
522     def test_parser_p4_11(self):
523         type_ = ofproto.OFPET_BAD_MATCH
524         code = ofproto.OFPBMC_EPERM
525         data = b'Error Message.'
526         self._test_parser(type_, code, data)
527
528     def test_parser_p5_0(self):
529         type_ = ofproto.OFPET_FLOW_MOD_FAILED
530         code = ofproto.OFPFMFC_UNKNOWN
531         data = b'Error Message.'
532         self._test_parser(type_, code, data)
533
534     def test_parser_p5_1(self):
535         type_ = ofproto.OFPET_FLOW_MOD_FAILED
536         code = ofproto.OFPFMFC_TABLE_FULL
537         data = b'Error Message.'
538         self._test_parser(type_, code, data)
539
540     def test_parser_p5_2(self):
541         type_ = ofproto.OFPET_FLOW_MOD_FAILED
542         code = ofproto.OFPFMFC_BAD_TABLE_ID
543         data = b'Error Message.'
544         self._test_parser(type_, code, data)
545
546     def test_parser_p5_3(self):
547         type_ = ofproto.OFPET_FLOW_MOD_FAILED
548         code = ofproto.OFPFMFC_OVERLAP
549         data = b'Error Message.'
550         self._test_parser(type_, code, data)
551
552     def test_parser_p5_4(self):
553         type_ = ofproto.OFPET_FLOW_MOD_FAILED
554         code = ofproto.OFPFMFC_EPERM
555         data = b'Error Message.'
556         self._test_parser(type_, code, data)
557
558     def test_parser_p5_5(self):
559         type_ = ofproto.OFPET_FLOW_MOD_FAILED
560         code = ofproto.OFPFMFC_BAD_TIMEOUT
561         data = b'Error Message.'
562         self._test_parser(type_, code, data)
563
564     def test_parser_p5_6(self):
565         type_ = ofproto.OFPET_FLOW_MOD_FAILED
566         code = ofproto.OFPFMFC_BAD_COMMAND
567         data = b'Error Message.'
568         self._test_parser(type_, code, data)
569
570     def test_parser_p5_7(self):
571         type_ = ofproto.OFPET_FLOW_MOD_FAILED
572         code = ofproto.OFPFMFC_BAD_FLAGS
573         data = b'Error Message.'
574         self._test_parser(type_, code, data)
575
576     def test_parser_p6_0(self):
577         type_ = ofproto.OFPET_GROUP_MOD_FAILED
578         code = ofproto.OFPGMFC_GROUP_EXISTS
579         data = b'Error Message.'
580         self._test_parser(type_, code, data)
581
582     def test_parser_p6_1(self):
583         type_ = ofproto.OFPET_GROUP_MOD_FAILED
584         code = ofproto.OFPGMFC_INVALID_GROUP
585         data = b'Error Message.'
586         self._test_parser(type_, code, data)
587
588     def test_parser_p6_2(self):
589         type_ = ofproto.OFPET_GROUP_MOD_FAILED
590         code = ofproto.OFPGMFC_WEIGHT_UNSUPPORTED
591         data = b'Error Message.'
592         self._test_parser(type_, code, data)
593
594     def test_parser_p6_3(self):
595         type_ = ofproto.OFPET_GROUP_MOD_FAILED
596         code = ofproto.OFPGMFC_OUT_OF_GROUPS
597         data = b'Error Message.'
598         self._test_parser(type_, code, data)
599
600     def test_parser_p6_4(self):
601         type_ = ofproto.OFPET_GROUP_MOD_FAILED
602         code = ofproto.OFPGMFC_OUT_OF_BUCKETS
603         data = b'Error Message.'
604         self._test_parser(type_, code, data)
605
606     def test_parser_p6_5(self):
607         type_ = ofproto.OFPET_GROUP_MOD_FAILED
608         code = ofproto.OFPGMFC_CHAINING_UNSUPPORTED
609         data = b'Error Message.'
610         self._test_parser(type_, code, data)
611
612     def test_parser_p6_6(self):
613         type_ = ofproto.OFPET_GROUP_MOD_FAILED
614         code = ofproto.OFPGMFC_WATCH_UNSUPPORTED
615         data = b'Error Message.'
616         self._test_parser(type_, code, data)
617
618     def test_parser_p6_7(self):
619         type_ = ofproto.OFPET_GROUP_MOD_FAILED
620         code = ofproto.OFPGMFC_LOOP
621         data = b'Error Message.'
622         self._test_parser(type_, code, data)
623
624     def test_parser_p6_8(self):
625         type_ = ofproto.OFPET_GROUP_MOD_FAILED
626         code = ofproto.OFPGMFC_UNKNOWN_GROUP
627         data = b'Error Message.'
628         self._test_parser(type_, code, data)
629
630     def test_parser_p6_9(self):
631         type_ = ofproto.OFPET_GROUP_MOD_FAILED
632         code = ofproto.OFPGMFC_CHAINED_GROUP
633         data = b'Error Message.'
634         self._test_parser(type_, code, data)
635
636     def test_parser_p6_10(self):
637         type_ = ofproto.OFPET_GROUP_MOD_FAILED
638         code = ofproto.OFPGMFC_BAD_TYPE
639         data = b'Error Message.'
640         self._test_parser(type_, code, data)
641
642     def test_parser_p6_11(self):
643         type_ = ofproto.OFPET_GROUP_MOD_FAILED
644         code = ofproto.OFPGMFC_BAD_COMMAND
645         data = b'Error Message.'
646         self._test_parser(type_, code, data)
647
648     def test_parser_p6_12(self):
649         type_ = ofproto.OFPET_GROUP_MOD_FAILED
650         code = ofproto.OFPGMFC_BAD_BUCKET
651         data = b'Error Message.'
652         self._test_parser(type_, code, data)
653
654     def test_parser_p6_13(self):
655         type_ = ofproto.OFPET_GROUP_MOD_FAILED
656         code = ofproto.OFPGMFC_BAD_WATCH
657         data = b'Error Message.'
658         self._test_parser(type_, code, data)
659
660     def test_parser_p6_14(self):
661         type_ = ofproto.OFPET_GROUP_MOD_FAILED
662         code = ofproto.OFPGMFC_EPERM
663         data = b'Error Message.'
664         self._test_parser(type_, code, data)
665
666     def test_parser_p7_0(self):
667         type_ = ofproto.OFPET_PORT_MOD_FAILED
668         code = ofproto.OFPPMFC_BAD_PORT
669         data = b'Error Message.'
670         self._test_parser(type_, code, data)
671
672     def test_parser_p7_1(self):
673         type_ = ofproto.OFPET_PORT_MOD_FAILED
674         code = ofproto.OFPPMFC_BAD_HW_ADDR
675         data = b'Error Message.'
676         self._test_parser(type_, code, data)
677
678     def test_parser_p7_2(self):
679         type_ = ofproto.OFPET_PORT_MOD_FAILED
680         code = ofproto.OFPPMFC_BAD_CONFIG
681         data = b'Error Message.'
682         self._test_parser(type_, code, data)
683
684     def test_parser_p7_3(self):
685         type_ = ofproto.OFPET_PORT_MOD_FAILED
686         code = ofproto.OFPPMFC_BAD_ADVERTISE
687         data = b'Error Message.'
688         self._test_parser(type_, code, data)
689
690     def test_parser_p7_4(self):
691         type_ = ofproto.OFPET_PORT_MOD_FAILED
692         code = ofproto.OFPPMFC_EPERM
693         data = b'Error Message.'
694         self._test_parser(type_, code, data)
695
696     def test_parser_p8_0(self):
697         type_ = ofproto.OFPET_TABLE_MOD_FAILED
698         code = ofproto.OFPTMFC_BAD_TABLE
699         data = b'Error Message.'
700         self._test_parser(type_, code, data)
701
702     def test_parser_p8_1(self):
703         type_ = ofproto.OFPET_TABLE_MOD_FAILED
704         code = ofproto.OFPTMFC_BAD_CONFIG
705         data = b'Error Message.'
706         self._test_parser(type_, code, data)
707
708     def test_parser_p8_2(self):
709         type_ = ofproto.OFPET_TABLE_MOD_FAILED
710         code = ofproto.OFPTMFC_EPERM
711         data = b'Error Message.'
712         self._test_parser(type_, code, data)
713
714     def test_parser_p9_0(self):
715         type_ = ofproto.OFPET_QUEUE_OP_FAILED
716         code = ofproto.OFPQOFC_BAD_PORT
717         data = b'Error Message.'
718         self._test_parser(type_, code, data)
719
720     def test_parser_p9_1(self):
721         type_ = ofproto.OFPET_QUEUE_OP_FAILED
722         code = ofproto.OFPQOFC_BAD_QUEUE
723         data = b'Error Message.'
724         self._test_parser(type_, code, data)
725
726     def test_parser_p9_2(self):
727         type_ = ofproto.OFPET_QUEUE_OP_FAILED
728         code = ofproto.OFPQOFC_EPERM
729         data = b'Error Message.'
730         self._test_parser(type_, code, data)
731
732     def test_parser_p10_0(self):
733         type_ = ofproto.OFPET_SWITCH_CONFIG_FAILED
734         code = ofproto.OFPSCFC_BAD_FLAGS
735         data = b'Error Message.'
736         self._test_parser(type_, code, data)
737
738     def test_parser_p10_1(self):
739         type_ = ofproto.OFPET_SWITCH_CONFIG_FAILED
740         code = ofproto.OFPSCFC_BAD_LEN
741         data = b'Error Message.'
742         self._test_parser(type_, code, data)
743
744     def test_parser_p10_2(self):
745         type_ = ofproto.OFPET_SWITCH_CONFIG_FAILED
746         code = ofproto.OFPSCFC_EPERM
747         data = b'Error Message.'
748         self._test_parser(type_, code, data)
749
750     def test_parser_p11_0(self):
751         type_ = ofproto.OFPET_ROLE_REQUEST_FAILED
752         code = ofproto.OFPRRFC_STALE
753         data = b'Error Message.'
754         self._test_parser(type_, code, data)
755
756     def test_parser_p11_1(self):
757         type_ = ofproto.OFPET_ROLE_REQUEST_FAILED
758         code = ofproto.OFPRRFC_UNSUP
759         data = b'Error Message.'
760         self._test_parser(type_, code, data)
761
762     def test_parser_p11_2(self):
763         type_ = ofproto.OFPET_ROLE_REQUEST_FAILED
764         code = ofproto.OFPRRFC_BAD_ROLE
765         data = b'Error Message.'
766         self._test_parser(type_, code, data)
767
768     def test_parser_experimenter(self):
769         type_ = 0xffff
770         exp_type = 1
771         experimenter = 1
772         data = b'Error Experimenter Message.'
773
774         # OFP_ERROR_EXPERIMENTER_MSG_PACK_STR = '!HHI'
775         fmt = ofproto.OFP_ERROR_EXPERIMENTER_MSG_PACK_STR
776         buf = self.buf + pack(fmt, type_, exp_type, experimenter) \
777             + data
778
779         res = OFPErrorMsg.parser(object, self.version, self.msg_type,
780                                  self.msg_len, self.xid, buf)
781
782         eq_(res.version, self.version)
783         eq_(res.msg_type, self.msg_type)
784         eq_(res.msg_len, self.msg_len)
785         eq_(res.xid, self.xid)
786         eq_(res.type, type_)
787         eq_(res.exp_type, exp_type)
788         eq_(res.experimenter, experimenter)
789         eq_(res.data, data)
790
791     def _test_serialize(self, type_, code, data):
792         # OFP_ERROR_MSG_PACK_STR = '!HH'
793         fmt = ofproto.OFP_ERROR_MSG_PACK_STR
794         buf = self.buf + pack(fmt, type_, code) + data
795
796         # initialization
797         c = OFPErrorMsg(_Datapath)
798         c.type = type_
799         c.code = code
800         c.data = data
801
802         c.serialize()
803
804         eq_(ofproto.OFP_VERSION, c.version)
805         eq_(ofproto.OFPT_ERROR, c.msg_type)
806         eq_(0, c.xid)
807         eq_(len(buf), c.msg_len)
808
809         fmt = '!' \
810             + ofproto.OFP_HEADER_PACK_STR.replace('!', '') \
811             + ofproto.OFP_ERROR_MSG_PACK_STR.replace('!', '') \
812             + str(len(c.data)) + 's'
813
814         res = struct.unpack(fmt, six.binary_type(c.buf))
815
816         eq_(res[0], ofproto.OFP_VERSION)
817         eq_(res[1], ofproto.OFPT_ERROR)
818         eq_(res[2], len(buf))
819         eq_(res[3], 0)
820         eq_(res[4], type_)
821         eq_(res[5], code)
822         eq_(res[6], data)
823
824     def test_serialize_mid(self):
825         type_ = 32768
826         code = 32768
827         data = b'Error Message.'
828         self._test_serialize(type_, code, data)
829
830     def test_serialize_max(self):
831         type_ = 65534  # 65535 collides OFPET_EXPERIMENTER
832         code = 65535
833         data = b'Error Message.'.ljust(65523)
834         self._test_serialize(type_, code, data)
835
836     def test_serialize_min_except_data(self):
837         type_ = ofproto.OFPET_HELLO_FAILED
838         code = ofproto.OFPHFC_INCOMPATIBLE
839         data = b'Error Message.'
840         self._test_serialize(type_, code, data)
841
842     @raises(AssertionError)
843     def test_serialize_check_data(self):
844         c = OFPErrorMsg(_Datapath)
845         c.serialize()
846
847     def _test_serialize_p(self, type_, code):
848         self._test_serialize(type_, code, b'Error Message.')
849
850     def test_serialize_p0_1(self):
851         self._test_serialize_p(ofproto.OFPET_HELLO_FAILED,
852                                ofproto.OFPHFC_EPERM)
853
854     def test_serialize_p1_0(self):
855         self._test_serialize_p(ofproto.OFPET_BAD_REQUEST,
856                                ofproto.OFPBRC_BAD_VERSION)
857
858     def test_serialize_p1_1(self):
859         self._test_serialize_p(ofproto.OFPET_BAD_REQUEST,
860                                ofproto.OFPBRC_BAD_TYPE)
861
862     def test_serialize_p1_2(self):
863         self._test_serialize_p(ofproto.OFPET_BAD_REQUEST,
864                                ofproto.OFPBRC_BAD_STAT)
865
866     def test_serialize_p1_3(self):
867         self._test_serialize_p(ofproto.OFPET_BAD_REQUEST,
868                                ofproto.OFPBRC_BAD_EXPERIMENTER)
869
870     def test_serialize_p1_4(self):
871         self._test_serialize_p(ofproto.OFPET_BAD_REQUEST,
872                                ofproto.OFPBRC_BAD_EXP_TYPE)
873
874     def test_serialize_p1_5(self):
875         self._test_serialize_p(ofproto.OFPET_BAD_REQUEST,
876                                ofproto.OFPBRC_EPERM)
877
878     def test_serialize_p1_6(self):
879         self._test_serialize_p(ofproto.OFPET_BAD_REQUEST,
880                                ofproto.OFPBRC_BAD_LEN)
881
882     def test_serialize_p1_7(self):
883         self._test_serialize_p(ofproto.OFPET_BAD_REQUEST,
884                                ofproto.OFPBRC_BUFFER_EMPTY)
885
886     def test_serialize_p1_8(self):
887         self._test_serialize_p(ofproto.OFPET_BAD_REQUEST,
888                                ofproto.OFPBRC_BUFFER_UNKNOWN)
889
890     def test_serialize_p1_9(self):
891         self._test_serialize_p(ofproto.OFPET_BAD_REQUEST,
892                                ofproto.OFPBRC_BAD_TABLE_ID)
893
894     def test_serialize_p1_10(self):
895         self._test_serialize_p(ofproto.OFPET_BAD_REQUEST,
896                                ofproto.OFPBRC_IS_SLAVE)
897
898     def test_serialize_p1_11(self):
899         self._test_serialize_p(ofproto.OFPET_BAD_REQUEST,
900                                ofproto.OFPBRC_BAD_PORT)
901
902     def test_serialize_p1_12(self):
903         self._test_serialize_p(ofproto.OFPET_BAD_REQUEST,
904                                ofproto.OFPBRC_BAD_PACKET)
905
906     def test_serialize_p2_0(self):
907         self._test_serialize_p(ofproto.OFPET_BAD_ACTION,
908                                ofproto.OFPBAC_BAD_TYPE)
909
910     def test_serialize_p2_1(self):
911         self._test_serialize_p(ofproto.OFPET_BAD_ACTION,
912                                ofproto.OFPBAC_BAD_LEN)
913
914     def test_serialize_p2_2(self):
915         self._test_serialize_p(ofproto.OFPET_BAD_ACTION,
916                                ofproto.OFPBAC_BAD_EXPERIMENTER)
917
918     def test_serialize_p2_3(self):
919         self._test_serialize_p(ofproto.OFPET_BAD_ACTION,
920                                ofproto.OFPBAC_BAD_EXP_TYPE)
921
922     def test_serialize_p2_4(self):
923         self._test_serialize_p(ofproto.OFPET_BAD_ACTION,
924                                ofproto.OFPBAC_BAD_OUT_PORT)
925
926     def test_serialize_p2_5(self):
927         self._test_serialize_p(ofproto.OFPET_BAD_ACTION,
928                                ofproto.OFPBAC_BAD_ARGUMENT)
929
930     def test_serialize_p2_6(self):
931         self._test_serialize_p(ofproto.OFPET_BAD_ACTION,
932                                ofproto.OFPBAC_EPERM)
933
934     def test_serialize_p2_7(self):
935         self._test_serialize_p(ofproto.OFPET_BAD_ACTION,
936                                ofproto.OFPBAC_TOO_MANY)
937
938     def test_serialize_p2_8(self):
939         self._test_serialize_p(ofproto.OFPET_BAD_ACTION,
940                                ofproto.OFPBAC_BAD_QUEUE)
941
942     def test_serialize_p2_9(self):
943         self._test_serialize_p(ofproto.OFPET_BAD_ACTION,
944                                ofproto.OFPBAC_BAD_OUT_GROUP)
945
946     def test_serialize_p2_10(self):
947         self._test_serialize_p(ofproto.OFPET_BAD_ACTION,
948                                ofproto.OFPBAC_MATCH_INCONSISTENT)
949
950     def test_serialize_p2_11(self):
951         self._test_serialize_p(ofproto.OFPET_BAD_ACTION,
952                                ofproto.OFPBAC_UNSUPPORTED_ORDER)
953
954     def test_serialize_p2_12(self):
955         self._test_serialize_p(ofproto.OFPET_BAD_ACTION,
956                                ofproto.OFPBAC_BAD_TAG)
957
958     def test_serialize_p2_13(self):
959         self._test_serialize_p(ofproto.OFPET_BAD_ACTION,
960                                ofproto.OFPBAC_BAD_SET_TYPE)
961
962     def test_serialize_p2_14(self):
963         self._test_serialize_p(ofproto.OFPET_BAD_ACTION,
964                                ofproto.OFPBAC_BAD_SET_LEN)
965
966     def test_serialize_p2_15(self):
967         self._test_serialize_p(ofproto.OFPET_BAD_ACTION,
968                                ofproto.OFPBAC_BAD_SET_ARGUMENT)
969
970     def test_serialize_p3_0(self):
971         self._test_serialize_p(ofproto.OFPET_BAD_INSTRUCTION,
972                                ofproto.OFPBIC_UNKNOWN_INST)
973
974     def test_serialize_p3_1(self):
975         self._test_serialize_p(ofproto.OFPET_BAD_INSTRUCTION,
976                                ofproto.OFPBIC_UNSUP_INST)
977
978     def test_serialize_p3_2(self):
979         self._test_serialize_p(ofproto.OFPET_BAD_INSTRUCTION,
980                                ofproto.OFPBIC_BAD_TABLE_ID)
981
982     def test_serialize_p3_3(self):
983         self._test_serialize_p(ofproto.OFPET_BAD_INSTRUCTION,
984                                ofproto.OFPBIC_UNSUP_METADATA)
985
986     def test_serialize_p3_4(self):
987         self._test_serialize_p(ofproto.OFPET_BAD_INSTRUCTION,
988                                ofproto.OFPBIC_UNSUP_METADATA_MASK)
989
990     def test_serialize_p3_5(self):
991         self._test_serialize_p(ofproto.OFPET_BAD_INSTRUCTION,
992                                ofproto.OFPBIC_BAD_EXPERIMENTER)
993
994     def test_serialize_p3_6(self):
995         self._test_serialize_p(ofproto.OFPET_BAD_INSTRUCTION,
996                                ofproto.OFPBIC_BAD_EXP_TYPE)
997
998     def test_serialize_p3_7(self):
999         self._test_serialize_p(ofproto.OFPET_BAD_INSTRUCTION,
1000                                ofproto.OFPBIC_BAD_LEN)
1001
1002     def test_serialize_p3_8(self):
1003         self._test_serialize_p(ofproto.OFPET_BAD_INSTRUCTION,
1004                                ofproto.OFPBIC_EPERM)
1005
1006     def test_serialize_p4_0(self):
1007         self._test_serialize_p(ofproto.OFPET_BAD_MATCH,
1008                                ofproto.OFPBMC_BAD_TYPE)
1009
1010     def test_serialize_p4_1(self):
1011         self._test_serialize_p(ofproto.OFPET_BAD_MATCH,
1012                                ofproto.OFPBMC_BAD_LEN)
1013
1014     def test_serialize_p4_2(self):
1015         self._test_serialize_p(ofproto.OFPET_BAD_MATCH,
1016                                ofproto.OFPBMC_BAD_TAG)
1017
1018     def test_serialize_p4_3(self):
1019         self._test_serialize_p(ofproto.OFPET_BAD_MATCH,
1020                                ofproto.OFPBMC_BAD_DL_ADDR_MASK)
1021
1022     def test_serialize_p4_4(self):
1023         self._test_serialize_p(ofproto.OFPET_BAD_MATCH,
1024                                ofproto.OFPBMC_BAD_NW_ADDR_MASK)
1025
1026     def test_serialize_p4_5(self):
1027         self._test_serialize_p(ofproto.OFPET_BAD_MATCH,
1028                                ofproto.OFPBMC_BAD_WILDCARDS)
1029
1030     def test_serialize_p4_6(self):
1031         self._test_serialize_p(ofproto.OFPET_BAD_MATCH,
1032                                ofproto.OFPBMC_BAD_FIELD)
1033
1034     def test_serialize_p4_7(self):
1035         self._test_serialize_p(ofproto.OFPET_BAD_MATCH,
1036                                ofproto.OFPBMC_BAD_VALUE)
1037
1038     def test_serialize_p4_8(self):
1039         self._test_serialize_p(ofproto.OFPET_BAD_MATCH,
1040                                ofproto.OFPBMC_BAD_MASK)
1041
1042     def test_serialize_p4_9(self):
1043         self._test_serialize_p(ofproto.OFPET_BAD_MATCH,
1044                                ofproto.OFPBMC_BAD_PREREQ)
1045
1046     def test_serialize_p4_10(self):
1047         self._test_serialize_p(ofproto.OFPET_BAD_MATCH,
1048                                ofproto.OFPBMC_DUP_FIELD)
1049
1050     def test_serialize_p4_11(self):
1051         self._test_serialize_p(ofproto.OFPET_BAD_MATCH,
1052                                ofproto.OFPBMC_EPERM)
1053
1054     def test_serialize_p5_0(self):
1055         self._test_serialize_p(ofproto.OFPET_FLOW_MOD_FAILED,
1056                                ofproto.OFPFMFC_UNKNOWN)
1057
1058     def test_serialize_p5_1(self):
1059         self._test_serialize_p(ofproto.OFPET_FLOW_MOD_FAILED,
1060                                ofproto.OFPFMFC_TABLE_FULL)
1061
1062     def test_serialize_p5_2(self):
1063         self._test_serialize_p(ofproto.OFPET_FLOW_MOD_FAILED,
1064                                ofproto.OFPFMFC_BAD_TABLE_ID)
1065
1066     def test_serialize_p5_3(self):
1067         self._test_serialize_p(ofproto.OFPET_FLOW_MOD_FAILED,
1068                                ofproto.OFPFMFC_OVERLAP)
1069
1070     def test_serialize_p5_4(self):
1071         self._test_serialize_p(ofproto.OFPET_FLOW_MOD_FAILED,
1072                                ofproto.OFPFMFC_EPERM)
1073
1074     def test_serialize_p5_5(self):
1075         self._test_serialize_p(ofproto.OFPET_FLOW_MOD_FAILED,
1076                                ofproto.OFPFMFC_BAD_TIMEOUT)
1077
1078     def test_serialize_p5_6(self):
1079         self._test_serialize_p(ofproto.OFPET_FLOW_MOD_FAILED,
1080                                ofproto.OFPFMFC_BAD_COMMAND)
1081
1082     def test_serialize_p5_7(self):
1083         self._test_serialize_p(ofproto.OFPET_FLOW_MOD_FAILED,
1084                                ofproto.OFPFMFC_BAD_FLAGS)
1085
1086     def test_serialize_p6_0(self):
1087         self._test_serialize_p(ofproto.OFPET_GROUP_MOD_FAILED,
1088                                ofproto.OFPGMFC_GROUP_EXISTS)
1089
1090     def test_serialize_p6_1(self):
1091         self._test_serialize_p(ofproto.OFPET_GROUP_MOD_FAILED,
1092                                ofproto.OFPGMFC_INVALID_GROUP)
1093
1094     def test_serialize_p6_2(self):
1095         self._test_serialize_p(ofproto.OFPET_GROUP_MOD_FAILED,
1096                                ofproto.OFPGMFC_WEIGHT_UNSUPPORTED)
1097
1098     def test_serialize_p6_3(self):
1099         self._test_serialize_p(ofproto.OFPET_GROUP_MOD_FAILED,
1100                                ofproto.OFPGMFC_OUT_OF_GROUPS)
1101
1102     def test_serialize_p6_4(self):
1103         self._test_serialize_p(ofproto.OFPET_GROUP_MOD_FAILED,
1104                                ofproto.OFPGMFC_OUT_OF_BUCKETS)
1105
1106     def test_serialize_p6_5(self):
1107         self._test_serialize_p(ofproto.OFPET_GROUP_MOD_FAILED,
1108                                ofproto.OFPGMFC_CHAINING_UNSUPPORTED)
1109
1110     def test_serialize_p6_6(self):
1111         self._test_serialize_p(ofproto.OFPET_GROUP_MOD_FAILED,
1112                                ofproto.OFPGMFC_WATCH_UNSUPPORTED)
1113
1114     def test_serialize_p6_7(self):
1115         self._test_serialize_p(ofproto.OFPET_GROUP_MOD_FAILED,
1116                                ofproto.OFPGMFC_LOOP)
1117
1118     def test_serialize_p6_8(self):
1119         self._test_serialize_p(ofproto.OFPET_GROUP_MOD_FAILED,
1120                                ofproto.OFPGMFC_UNKNOWN_GROUP)
1121
1122     def test_serialize_p6_9(self):
1123         self._test_serialize_p(ofproto.OFPET_GROUP_MOD_FAILED,
1124                                ofproto.OFPGMFC_CHAINED_GROUP)
1125
1126     def test_serialize_p6_10(self):
1127         self._test_serialize_p(ofproto.OFPET_GROUP_MOD_FAILED,
1128                                ofproto.OFPGMFC_BAD_TYPE)
1129
1130     def test_serialize_p6_11(self):
1131         self._test_serialize_p(ofproto.OFPET_GROUP_MOD_FAILED,
1132                                ofproto.OFPGMFC_BAD_COMMAND)
1133
1134     def test_serialize_p6_12(self):
1135         self._test_serialize_p(ofproto.OFPET_GROUP_MOD_FAILED,
1136                                ofproto.OFPGMFC_BAD_BUCKET)
1137
1138     def test_serialize_p6_13(self):
1139         self._test_serialize_p(ofproto.OFPET_GROUP_MOD_FAILED,
1140                                ofproto.OFPGMFC_BAD_WATCH)
1141
1142     def test_serialize_p6_14(self):
1143         self._test_serialize_p(ofproto.OFPET_GROUP_MOD_FAILED,
1144                                ofproto.OFPGMFC_EPERM)
1145
1146     def test_serialize_p7_0(self):
1147         self._test_serialize_p(ofproto.OFPET_PORT_MOD_FAILED,
1148                                ofproto.OFPPMFC_BAD_PORT)
1149
1150     def test_serialize_p7_1(self):
1151         self._test_serialize_p(ofproto.OFPET_PORT_MOD_FAILED,
1152                                ofproto.OFPPMFC_BAD_HW_ADDR)
1153
1154     def test_serialize_p7_2(self):
1155         self._test_serialize_p(ofproto.OFPET_PORT_MOD_FAILED,
1156                                ofproto.OFPPMFC_BAD_CONFIG)
1157
1158     def test_serialize_p7_3(self):
1159         self._test_serialize_p(ofproto.OFPET_PORT_MOD_FAILED,
1160                                ofproto.OFPPMFC_BAD_ADVERTISE)
1161
1162     def test_serialize_p7_4(self):
1163         self._test_serialize_p(ofproto.OFPET_PORT_MOD_FAILED,
1164                                ofproto.OFPPMFC_EPERM)
1165
1166     def test_serialize_p8_0(self):
1167         self._test_serialize_p(ofproto.OFPET_TABLE_MOD_FAILED,
1168                                ofproto.OFPTMFC_BAD_TABLE)
1169
1170     def test_serialize_p8_1(self):
1171         self._test_serialize_p(ofproto.OFPET_TABLE_MOD_FAILED,
1172                                ofproto.OFPTMFC_BAD_CONFIG)
1173
1174     def test_serialize_p8_2(self):
1175         self._test_serialize_p(ofproto.OFPET_TABLE_MOD_FAILED,
1176                                ofproto.OFPTMFC_EPERM)
1177
1178     def test_serialize_p9_0(self):
1179         self._test_serialize_p(ofproto.OFPET_QUEUE_OP_FAILED,
1180                                ofproto.OFPQOFC_BAD_PORT)
1181
1182     def test_serialize_p9_1(self):
1183         self._test_serialize_p(ofproto.OFPET_QUEUE_OP_FAILED,
1184                                ofproto.OFPQOFC_BAD_QUEUE)
1185
1186     def test_serialize_p9_2(self):
1187         self._test_serialize_p(ofproto.OFPET_QUEUE_OP_FAILED,
1188                                ofproto.OFPQOFC_EPERM)
1189
1190     def test_serialize_p10_0(self):
1191         self._test_serialize_p(ofproto.OFPET_SWITCH_CONFIG_FAILED,
1192                                ofproto.OFPSCFC_BAD_FLAGS)
1193
1194     def test_serialize_p10_1(self):
1195         self._test_serialize_p(ofproto.OFPET_SWITCH_CONFIG_FAILED,
1196                                ofproto.OFPSCFC_BAD_LEN)
1197
1198     def test_serialize_p10_2(self):
1199         self._test_serialize_p(ofproto.OFPET_SWITCH_CONFIG_FAILED,
1200                                ofproto.OFPSCFC_EPERM)
1201
1202     def test_serialize_p11_0(self):
1203         self._test_serialize_p(ofproto.OFPET_ROLE_REQUEST_FAILED,
1204                                ofproto.OFPRRFC_STALE)
1205
1206     def test_serialize_p11_1(self):
1207         self._test_serialize_p(ofproto.OFPET_ROLE_REQUEST_FAILED,
1208                                ofproto.OFPRRFC_UNSUP)
1209
1210     def test_serialize_p11_2(self):
1211         self._test_serialize_p(ofproto.OFPET_ROLE_REQUEST_FAILED,
1212                                ofproto.OFPRRFC_BAD_ROLE)
1213
1214
1215 class TestOFPErrorExperimenterMsg(unittest.TestCase):
1216     """ Test case for ofproto_v1_2_parser.OFPErrorExperimenterMsg
1217     """
1218
1219     def test_init(self):
1220         c = OFPErrorExperimenterMsg(_Datapath)
1221         eq_(c.type, 65535)
1222         eq_(c.exp_type, None)
1223         eq_(c.experimenter, None)
1224         eq_(c.data, None)
1225
1226     def _test_parser(self, exp_type, experimenter, data=None):
1227         # OFP_HEADER_PACK_STR
1228         # '!BBHI'...version, msg_type, msg_len, xid
1229         version = ofproto.OFP_VERSION
1230         msg_type = ofproto.OFPT_ERROR
1231         msg_len = ofproto.OFP_ERROR_MSG_SIZE
1232         xid = 2495926989
1233
1234         fmt = ofproto.OFP_HEADER_PACK_STR
1235         buf = pack(fmt, version, msg_type, msg_len, xid)
1236
1237         # OFP_ERROR_EXPERIMENTER_MSG_PACK_STR = '!HHI'
1238         type_ = 0xffff
1239         fmt = ofproto.OFP_ERROR_EXPERIMENTER_MSG_PACK_STR
1240         buf += pack(fmt, type_, exp_type, experimenter)
1241
1242         if data is not None:
1243             buf += data
1244
1245         res = OFPErrorMsg.parser(
1246             object, version, msg_type, msg_len, xid, buf)
1247
1248         eq_(res.version, version)
1249         eq_(res.msg_type, msg_type)
1250         eq_(res.msg_len, msg_len)
1251         eq_(res.xid, xid)
1252         eq_(res.type, type_)
1253         eq_(res.exp_type, exp_type)
1254         eq_(res.experimenter, experimenter)
1255
1256         if data is not None:
1257             eq_(res.data, data)
1258
1259     def test_parser_mid(self):
1260         exp_type = 32768
1261         experimenter = 2147483648
1262         data = b'Error Experimenter Message.'
1263         self._test_parser(exp_type, experimenter, data)
1264
1265     def test_parser_max(self):
1266         exp_type = 65535
1267         experimenter = 4294967295
1268         data = b'Error Experimenter Message.'.ljust(65519)
1269         self._test_parser(exp_type, experimenter, data)
1270
1271     def test_parser_min(self):
1272         exp_type = 0
1273         experimenter = 0
1274         self._test_parser(exp_type, experimenter)
1275
1276
1277 class TestOFPEchoRequest(unittest.TestCase):
1278     """ Test case for ofproto_v1_2_parser.OFPEchoRequest
1279     """
1280     # OFP_HEADER_PACK_STR
1281     # '!BBHI'...version, msg_type, msg_len, xid
1282     version = ofproto.OFP_VERSION
1283     msg_type = ofproto.OFPT_ECHO_REQUEST
1284     msg_len = ofproto.OFP_HEADER_SIZE
1285     xid = 2495926989
1286
1287     def test_init(self):
1288         c = OFPEchoRequest(_Datapath)
1289         eq_(c.data, None)
1290
1291     def _test_parser(self, data=None):
1292         fmt = ofproto.OFP_HEADER_PACK_STR
1293         buf = pack(fmt, self.version, self.msg_type,
1294                    self.msg_len, self.xid)
1295
1296         if data is not None:
1297             buf += data
1298
1299         res = OFPEchoRequest.parser(object, self.version, self.msg_type,
1300                                     self.msg_len, self.xid, buf)
1301
1302         eq_(res.version, self.version)
1303         eq_(res.msg_type, self.msg_type)
1304         eq_(res.msg_len, self.msg_len)
1305         eq_(res.xid, self.xid)
1306
1307         if data is not None:
1308             eq_(res.data, data)
1309
1310     def test_parser_mid(self):
1311         data = b'Request Message.'
1312         self._test_parser(data)
1313
1314     def test_parser_max(self):
1315         data = b'Request Message.'.ljust(65527)
1316         self._test_parser(data)
1317
1318     def test_parser_min(self):
1319         data = None
1320         self._test_parser(data)
1321
1322     def _test_serialize(self, data):
1323         c = OFPEchoRequest(_Datapath)
1324         c.data = data
1325         c.serialize()
1326
1327         eq_(ofproto.OFP_VERSION, c.version)
1328         eq_(ofproto.OFPT_ECHO_REQUEST, c.msg_type)
1329         eq_(0, c.xid)
1330
1331         fmt = ofproto.OFP_HEADER_PACK_STR
1332
1333         if data is not None:
1334             fmt += str(len(c.data)) + 's'
1335
1336         res = struct.unpack(fmt, six.binary_type(c.buf))
1337
1338         eq_(res[0], ofproto.OFP_VERSION)
1339         eq_(res[1], ofproto.OFPT_ECHO_REQUEST)
1340         eq_(res[2], len(c.buf))
1341         eq_(res[3], 0)
1342
1343         if data is not None:
1344             eq_(res[4], data)
1345
1346     def test_serialize_mid(self):
1347         data = b'Request Message.'
1348         self._test_serialize(data)
1349
1350     def test_serialize_max(self):
1351         data = b'Request Message.'.ljust(65527)
1352         self._test_serialize(data)
1353
1354     def test_serialize_min(self):
1355         data = None
1356         self._test_serialize(data)
1357
1358
1359 class TestOFPEchoReply(unittest.TestCase):
1360     """ Test case for ofproto_v1_2_parser.OFPEchoReply
1361     """
1362
1363     # OFP_HEADER_PACK_STR
1364     # '!BBHI'...version, msg_type, msg_len, xid
1365     version = ofproto.OFP_VERSION
1366     msg_type = ofproto.OFPT_ECHO_REPLY
1367     msg_len = ofproto.OFP_HEADER_SIZE
1368     xid = 2495926989
1369
1370     def test_init(self):
1371         c = OFPEchoReply(_Datapath)
1372         eq_(c.data, None)
1373
1374     def _test_parser(self, data):
1375         fmt = ofproto.OFP_HEADER_PACK_STR
1376         buf = pack(fmt, self.version, self.msg_type,
1377                    self.msg_len, self.xid)
1378
1379         if data is not None:
1380             buf += data
1381
1382         res = OFPEchoReply.parser(object, self.version, self.msg_type,
1383                                   self.msg_len, self.xid, buf)
1384
1385         eq_(res.version, self.version)
1386         eq_(res.msg_type, self.msg_type)
1387         eq_(res.msg_len, self.msg_len)
1388         eq_(res.xid, self.xid)
1389
1390         if data is not None:
1391             eq_(res.data, data)
1392
1393     def test_parser_mid(self):
1394         data = b'Reply Message.'
1395         self._test_parser(data)
1396
1397     def test_parser_max(self):
1398         data = b'Reply Message.'.ljust(65527)
1399         self._test_parser(data)
1400
1401     def test_parser_min(self):
1402         data = None
1403         self._test_parser(data)
1404
1405     def _test_serialize(self, data):
1406         fmt = ofproto.OFP_HEADER_PACK_STR
1407         buf = pack(fmt, self.version, self.msg_type,
1408                    self.msg_len, self.xid) + data
1409
1410         c = OFPEchoReply(_Datapath)
1411         c.data = data
1412         c.serialize()
1413
1414         eq_(ofproto.OFP_VERSION, c.version)
1415         eq_(ofproto.OFPT_ECHO_REPLY, c.msg_type)
1416         eq_(0, c.xid)
1417
1418         fmt = '!' \
1419             + ofproto.OFP_HEADER_PACK_STR.replace('!', '') \
1420             + str(len(c.data)) + 's'
1421
1422         res = struct.unpack(fmt, six.binary_type(c.buf))
1423
1424         eq_(res[0], ofproto.OFP_VERSION)
1425         eq_(res[1], ofproto.OFPT_ECHO_REPLY)
1426         eq_(res[2], len(buf))
1427         eq_(res[3], 0)
1428         eq_(res[4], data)
1429
1430     def test_serialize_mid(self):
1431         data = b'Reply Message.'
1432         self._test_serialize(data)
1433
1434     def test_serialize_max(self):
1435         data = b'Reply Message.'.ljust(65527)
1436         self._test_serialize(data)
1437
1438     @raises(AssertionError)
1439     def test_serialize_check_data(self):
1440         c = OFPEchoReply(_Datapath)
1441         c.serialize()
1442
1443
1444 class TestOFPExperimenter(unittest.TestCase):
1445     """ Test case for ofproto_v1_2_parser.OFPExperimenter
1446     """
1447
1448     c = OFPExperimenter(_Datapath)
1449
1450     def _test_parser(self, xid, experimenter, exp_type):
1451         # OFP_HEADER_PACK_STR
1452         # '!BBHI'...version, msg_type, msg_len, xid
1453         version = ofproto.OFP_VERSION
1454         msg_type = ofproto.OFPT_EXPERIMENTER
1455         msg_len = ofproto.OFP_EXPERIMENTER_HEADER_SIZE
1456
1457         fmt = ofproto.OFP_HEADER_PACK_STR
1458         buf = pack(fmt, version, msg_type, msg_len, xid)
1459
1460         # OFP_EXPERIMENTER_HEADER_PACK_STR
1461         # '!II'...experimenter, exp_type
1462         fmt = ofproto.OFP_EXPERIMENTER_HEADER_PACK_STR
1463         buf += pack(fmt, experimenter, exp_type)
1464
1465         res = OFPExperimenter.parser(object, version, msg_type,
1466                                      msg_len, xid, buf)
1467
1468         eq_(version, res.version)
1469         eq_(msg_type, res.msg_type)
1470         eq_(msg_len, res.msg_len)
1471         eq_(xid, res.xid)
1472         eq_(experimenter, res.experimenter)
1473         eq_(exp_type, res.exp_type)
1474
1475     def test_parser_mid(self):
1476         xid = 2495926989
1477         experimenter = 2147483648
1478         exp_type = 1
1479         self._test_parser(xid, experimenter, exp_type)
1480
1481     def test_parser_max(self):
1482         xid = 4294967295
1483         experimenter = 4294967295
1484         exp_type = 65535
1485         self._test_parser(xid, experimenter, exp_type)
1486
1487     def test_parser_min(self):
1488         xid = 0
1489         experimenter = 0
1490         exp_type = 0
1491         self._test_parser(xid, experimenter, exp_type)
1492
1493
1494 class TestOFPPort(unittest.TestCase):
1495     """ Test case for ofproto_v1_2_parser.OFPPort
1496     """
1497
1498     def test_init(self):
1499         # OFP_PORT_PACK_STR
1500         # '!I4x6s2x16sIIIIIIII'... port_no, pad(4), hw_addr, pad(2),
1501         #                          name, config, state, curr, advertised,
1502         #                          peer, curr_speed, max_speed
1503         port_no = 1119692796
1504         hw_addr = 'c0:26:53:c4:29:e2'
1505         name = b'name'.ljust(16)
1506         config = 2226555987
1507         state = 1678244809
1508         curr = 2850556459
1509         advertised = 2025421682
1510         supported = 2120575149
1511         peer = 2757463021
1512         curr_speed = 2641353507
1513         max_speed = 1797291672
1514
1515         fmt = ofproto.OFP_PORT_PACK_STR
1516
1517         c = OFPPort(port_no, hw_addr, name, config, state, curr,
1518                     advertised, supported, peer, curr_speed, max_speed)
1519
1520         eq_(port_no, c.port_no)
1521         eq_(hw_addr, c.hw_addr)
1522         eq_(name, c.name)
1523         eq_(config, c.config)
1524         eq_(state, c.state)
1525         eq_(curr, c.curr)
1526         eq_(advertised, c.advertised)
1527         eq_(supported, c.supported)
1528         eq_(peer, c.peer)
1529         eq_(curr_speed, c.curr_speed)
1530         eq_(max_speed, c.max_speed)
1531
1532     def _test_parser(self, port_no, hw_addr, config, state, curr, advertised,
1533                      supported, peer, curr_speed, max_speed):
1534         name = b'name'.ljust(16)
1535         fmt = ofproto.OFP_PORT_PACK_STR
1536         buf = pack(fmt, port_no, addrconv.mac.text_to_bin(hw_addr), name,
1537                    config, state, curr,
1538                    advertised, supported, peer, curr_speed, max_speed)
1539
1540         res = OFPPort.parser(buf, 0)
1541
1542         eq_(port_no, res.port_no)
1543         eq_(hw_addr, res.hw_addr)
1544         eq_(name, res.name)
1545         eq_(config, res.config)
1546         eq_(state, res.state)
1547         eq_(curr, res.curr)
1548         eq_(advertised, res.advertised)
1549         eq_(supported, res.supported)
1550         eq_(peer, res.peer)
1551         eq_(curr_speed, res.curr_speed)
1552         eq_(max_speed, res.max_speed)
1553
1554     def test_parser_mid(self):
1555         port_no = 1119692796
1556         hw_addr = 'c0:26:53:c4:29:e2'
1557         config = 2226555987
1558         state = 1678244809
1559         curr = 2850556459
1560         advertised = 2025421682
1561         supported = 2120575149
1562         peer = 2757463021
1563         curr_speed = 2641353507
1564         max_speed = 1797291672
1565         self._test_parser(port_no, hw_addr, config, state, curr, advertised,
1566                           supported, peer, curr_speed, max_speed)
1567
1568     def test_parser_max(self):
1569         port_no = ofproto.OFPP_ANY
1570         hw_addr = 'ff:ff:ff:ff:ff:ff'
1571         config = 4294967295
1572         state = 4294967295
1573         curr = 4294967295
1574         advertised = 4294967295
1575         supported = 4294967295
1576         peer = 4294967295
1577         curr_speed = 4294967295
1578         max_speed = 4294967295
1579         self._test_parser(port_no, hw_addr, config, state, curr, advertised,
1580                           supported, peer, curr_speed, max_speed)
1581
1582     def test_parser_min(self):
1583         port_no = 0
1584         hw_addr = '00:00:00:00:00:00'
1585         config = 0
1586         state = 0
1587         curr = 0
1588         advertised = 0
1589         supported = 0
1590         peer = 0
1591         curr_speed = 0
1592         max_speed = 0
1593         self._test_parser(port_no, hw_addr, config, state, curr, advertised,
1594                           supported, peer, curr_speed, max_speed)
1595
1596     def test_parser_p1(self):
1597         port_no = ofproto.OFPP_MAX
1598         hw_addr = 'c0:26:53:c4:29:e2'
1599         config = ofproto.OFPPC_PORT_DOWN
1600         state = ofproto.OFPPS_LINK_DOWN
1601         curr = advertised = supported \
1602              = peer = curr_speed = max_speed \
1603              = ofproto.OFPPF_10MB_HD
1604         self._test_parser(port_no, hw_addr, config, state, curr, advertised,
1605                           supported, peer, curr_speed, max_speed)
1606
1607     def test_parser_p2(self):
1608         port_no = ofproto.OFPP_IN_PORT
1609         hw_addr = 'c0:26:53:c4:29:e2'
1610         config = ofproto.OFPPC_NO_RECV
1611         state = ofproto.OFPPS_BLOCKED
1612         curr = advertised = supported \
1613              = peer = curr_speed = max_speed \
1614              = ofproto.OFPPF_10MB_FD
1615         self._test_parser(port_no, hw_addr, config, state, curr, advertised,
1616                           supported, peer, curr_speed, max_speed)
1617
1618     def test_parser_p3(self):
1619         port_no = ofproto.OFPP_TABLE
1620         hw_addr = 'c0:26:53:c4:29:e2'
1621         config = ofproto.OFPPC_NO_FWD
1622         state = ofproto.OFPPS_LIVE
1623         curr = advertised = supported \
1624              = peer = curr_speed = max_speed \
1625              = ofproto.OFPPF_100MB_HD
1626         self._test_parser(port_no, hw_addr, config, state, curr, advertised,
1627                           supported, peer, curr_speed, max_speed)
1628
1629     def test_parser_p4(self):
1630         port_no = ofproto.OFPP_NORMAL
1631         hw_addr = 'c0:26:53:c4:29:e2'
1632         config = ofproto.OFPPC_NO_PACKET_IN
1633         state = ofproto.OFPPS_LIVE
1634         curr = advertised = supported \
1635              = peer = curr_speed = max_speed \
1636              = ofproto.OFPPF_100MB_FD
1637         self._test_parser(port_no, hw_addr, config, state, curr, advertised,
1638                           supported, peer, curr_speed, max_speed)
1639
1640     def test_parser_p5(self):
1641         port_no = ofproto.OFPP_FLOOD
1642         hw_addr = 'c0:26:53:c4:29:e2'
1643         config = ofproto.OFPPC_NO_PACKET_IN
1644         state = ofproto.OFPPS_LIVE
1645         curr = advertised = supported \
1646              = peer = curr_speed = max_speed \
1647              = ofproto.OFPPF_1GB_HD
1648         self._test_parser(port_no, hw_addr, config, state, curr, advertised,
1649                           supported, peer, curr_speed, max_speed)
1650
1651     def test_parser_p6(self):
1652         port_no = ofproto.OFPP_ALL
1653         hw_addr = 'c0:26:53:c4:29:e2'
1654         config = ofproto.OFPPC_NO_PACKET_IN
1655         state = ofproto.OFPPS_LIVE
1656         curr = advertised = supported \
1657              = peer = curr_speed = max_speed \
1658              = ofproto.OFPPF_1GB_FD
1659         self._test_parser(port_no, hw_addr, config, state, curr, advertised,
1660                           supported, peer, curr_speed, max_speed)
1661
1662     def test_parser_p7(self):
1663         port_no = ofproto.OFPP_CONTROLLER
1664         hw_addr = 'c0:26:53:c4:29:e2'
1665         config = ofproto.OFPPC_NO_PACKET_IN
1666         state = ofproto.OFPPS_LIVE
1667         curr = advertised = supported \
1668              = peer = curr_speed = max_speed \
1669              = ofproto.OFPPF_10GB_FD
1670         self._test_parser(port_no, hw_addr, config, state, curr, advertised,
1671                           supported, peer, curr_speed, max_speed)
1672
1673     def test_parser_p8(self):
1674         port_no = ofproto.OFPP_LOCAL
1675         hw_addr = 'c0:26:53:c4:29:e2'
1676         config = ofproto.OFPPC_NO_PACKET_IN
1677         state = ofproto.OFPPS_LIVE
1678         curr = advertised = supported \
1679              = peer = curr_speed = max_speed \
1680              = ofproto.OFPPF_40GB_FD
1681         self._test_parser(port_no, hw_addr, config, state, curr, advertised,
1682                           supported, peer, curr_speed, max_speed)
1683
1684     def test_parser_p9(self):
1685         port_no = ofproto.OFPP_LOCAL
1686         hw_addr = 'c0:26:53:c4:29:e2'
1687         config = ofproto.OFPPC_NO_PACKET_IN
1688         state = ofproto.OFPPS_LIVE
1689         curr = advertised = supported \
1690              = peer = curr_speed = max_speed \
1691              = ofproto.OFPPF_100GB_FD
1692         self._test_parser(port_no, hw_addr, config, state, curr, advertised,
1693                           supported, peer, curr_speed, max_speed)
1694
1695     def test_parser_p10(self):
1696         port_no = ofproto.OFPP_LOCAL
1697         hw_addr = 'c0:26:53:c4:29:e2'
1698         config = ofproto.OFPPC_NO_PACKET_IN
1699         state = ofproto.OFPPS_LIVE
1700         curr = advertised = supported \
1701              = peer = curr_speed = max_speed \
1702              = ofproto.OFPPF_1TB_FD
1703         self._test_parser(port_no, hw_addr, config, state, curr, advertised,
1704                           supported, peer, curr_speed, max_speed)
1705
1706     def test_parser_p11(self):
1707         port_no = ofproto.OFPP_LOCAL
1708         hw_addr = 'c0:26:53:c4:29:e2'
1709         config = ofproto.OFPPC_NO_PACKET_IN
1710         state = ofproto.OFPPS_LIVE
1711         curr = advertised = supported \
1712              = peer = curr_speed = max_speed \
1713              = ofproto.OFPPF_OTHER
1714         self._test_parser(port_no, hw_addr, config, state, curr, advertised,
1715                           supported, peer, curr_speed, max_speed)
1716
1717     def test_parser_p12(self):
1718         port_no = ofproto.OFPP_LOCAL
1719         hw_addr = 'c0:26:53:c4:29:e2'
1720         config = ofproto.OFPPC_NO_PACKET_IN
1721         state = ofproto.OFPPS_LIVE
1722         curr = advertised = supported \
1723              = peer = curr_speed = max_speed \
1724              = ofproto.OFPPF_COPPER
1725         self._test_parser(port_no, hw_addr, config, state, curr, advertised,
1726                           supported, peer, curr_speed, max_speed)
1727
1728     def test_parser_p13(self):
1729         port_no = ofproto.OFPP_LOCAL
1730         hw_addr = 'c0:26:53:c4:29:e2'
1731         config = ofproto.OFPPC_NO_PACKET_IN
1732         state = ofproto.OFPPS_LIVE
1733         curr = advertised = supported \
1734              = peer = curr_speed = max_speed \
1735              = ofproto.OFPPF_FIBER
1736         self._test_parser(port_no, hw_addr, config, state, curr, advertised,
1737                           supported, peer, curr_speed, max_speed)
1738
1739     def test_parser_p14(self):
1740         port_no = ofproto.OFPP_LOCAL
1741         hw_addr = 'c0:26:53:c4:29:e2'
1742         config = ofproto.OFPPC_NO_PACKET_IN
1743         state = ofproto.OFPPS_LIVE
1744         curr = advertised = supported \
1745              = peer = curr_speed = max_speed \
1746              = ofproto.OFPPF_AUTONEG
1747         self._test_parser(port_no, hw_addr, config, state, curr, advertised,
1748                           supported, peer, curr_speed, max_speed)
1749
1750     def test_parser_p15(self):
1751         port_no = ofproto.OFPP_LOCAL
1752         hw_addr = 'c0:26:53:c4:29:e2'
1753         config = ofproto.OFPPC_NO_PACKET_IN
1754         state = ofproto.OFPPS_LIVE
1755         curr = advertised = supported \
1756              = peer = curr_speed = max_speed \
1757              = ofproto.OFPPF_PAUSE
1758         self._test_parser(port_no, hw_addr, config, state, curr, advertised,
1759                           supported, peer, curr_speed, max_speed)
1760
1761     def test_parser_p16(self):
1762         port_no = ofproto.OFPP_LOCAL
1763         hw_addr = 'c0:26:53:c4:29:e2'
1764         config = ofproto.OFPPC_NO_PACKET_IN
1765         state = ofproto.OFPPS_LIVE
1766         curr = advertised = supported \
1767              = peer = curr_speed = max_speed \
1768              = ofproto.OFPPF_PAUSE_ASYM
1769         self._test_parser(port_no, hw_addr, config, state, curr, advertised,
1770                           supported, peer, curr_speed, max_speed)
1771
1772
1773 class TestOFPFeaturesRequest(unittest.TestCase):
1774     """ Test case for ofproto_v1_2_parser.OFPFeaturesRequest
1775     """
1776
1777     def test_serialize(self):
1778         c = OFPFeaturesRequest(_Datapath)
1779         c.serialize()
1780
1781         eq_(ofproto.OFP_VERSION, c.version)
1782         eq_(ofproto.OFPT_FEATURES_REQUEST, c.msg_type)
1783         eq_(0, c.xid)
1784
1785         fmt = ofproto.OFP_HEADER_PACK_STR
1786
1787         res = struct.unpack(fmt, six.binary_type(c.buf))
1788
1789         eq_(res[0], ofproto.OFP_VERSION)
1790         eq_(res[1], ofproto.OFPT_FEATURES_REQUEST)
1791         eq_(res[2], len(c.buf))
1792         eq_(res[3], 0)
1793
1794
1795 class TestOFPSwitchFeatures(unittest.TestCase):
1796     """ Test case for ofproto_v1_2_parser.OFPSwitchFeatures
1797     """
1798
1799     def _test_parser(self, xid, datapath_id, n_buffers,
1800                      n_tables, capabilities, reserved, port_cnt=0):
1801
1802         # OFP_HEADER_PACK_STR
1803         # '!BBHI'...version, msg_type, msg_len, xid
1804         version = ofproto.OFP_VERSION
1805         msg_type = ofproto.OFPT_FEATURES_REPLY
1806         msg_len = ofproto.OFP_SWITCH_FEATURES_SIZE \
1807             + ofproto.OFP_PORT_SIZE * port_cnt
1808
1809         fmt = ofproto.OFP_HEADER_PACK_STR
1810         buf = pack(fmt, version, msg_type, msg_len, xid)
1811
1812         # OFP_SWITCH_FEATURES_PACK_STR
1813         # '!QIB3xII'...datapath_id, n_buffers, n_tables,
1814         #              pad(3), capabilities, reserved
1815
1816         fmt = ofproto.OFP_SWITCH_FEATURES_PACK_STR
1817         buf += pack(fmt, datapath_id, n_buffers, n_tables,
1818                     capabilities, reserved)
1819
1820         for i in range(port_cnt):
1821             # OFP_PORT_PACK_STR
1822             # '!I4x6s2x16sIIIIIIII'... port_no, pad(4), hw_addr, pad(2),
1823             #                          name, config, state, curr, advertised,
1824             #                          peer, curr_speed, max_speed
1825             port_no = i
1826
1827             fmt = ofproto.OFP_PORT_PACK_STR
1828             buf += pack(fmt, port_no, b'\x00' * 6, b'\x00' * 16, 0, 0, 0,
1829                         0, 0, 0, 0, 0)
1830
1831         res = OFPSwitchFeatures.parser(object, version, msg_type,
1832                                        msg_len, xid, buf)
1833
1834         eq_(res.version, version)
1835         eq_(res.msg_type, msg_type)
1836         eq_(res.msg_len, msg_len)
1837         eq_(res.xid, xid)
1838
1839         eq_(res.datapath_id, datapath_id)
1840         eq_(res.n_buffers, n_buffers)
1841         eq_(res.n_tables, n_tables)
1842         eq_(res.capabilities, capabilities)
1843         eq_(res._reserved, reserved)
1844
1845         for i in range(port_cnt):
1846             eq_(res.ports[i].port_no, i)
1847
1848     def test_parser_mid(self):
1849         xid = 2495926989
1850         datapath_id = 1270985291017894273
1851         n_buffers = 2148849654
1852         n_tables = 228
1853         capabilities = 1766843586
1854         reserved = 2013714700
1855         port_cnt = 1
1856
1857         self._test_parser(xid, datapath_id, n_buffers, n_tables,
1858                           capabilities, reserved, port_cnt)
1859
1860     def test_parser_max(self):
1861         xid = 4294967295
1862         datapath_id = 18446744073709551615
1863         n_buffers = 4294967295
1864         n_tables = 255
1865         capabilities = 4294967295
1866         reserved = 4294967295
1867         port_cnt = 1023
1868
1869         self._test_parser(xid, datapath_id, n_buffers, n_tables,
1870                           capabilities, reserved, port_cnt)
1871
1872     def test_parser_min(self):
1873         xid = 0
1874         datapath_id = 0
1875         n_buffers = 0
1876         n_tables = 0
1877         capabilities = 0
1878         reserved = 0
1879         port_cnt = 0
1880
1881         self._test_parser(xid, datapath_id, n_buffers, n_tables,
1882                           capabilities, reserved, port_cnt)
1883
1884
1885 class TestOFPGetConfigRequest(unittest.TestCase):
1886     """ Test case for ofproto_v1_2_parser.OFPGetConfigRequest
1887     """
1888
1889     def test_serialize(self):
1890         c = OFPGetConfigRequest(_Datapath)
1891         c.serialize()
1892
1893         eq_(ofproto.OFP_VERSION, c.version)
1894         eq_(ofproto.OFPT_GET_CONFIG_REQUEST, c.msg_type)
1895         eq_(0, c.xid)
1896
1897         fmt = ofproto.OFP_HEADER_PACK_STR
1898
1899         res = struct.unpack(fmt, six.binary_type(c.buf))
1900         eq_(res[0], ofproto.OFP_VERSION)
1901         eq_(res[1], ofproto.OFPT_GET_CONFIG_REQUEST)
1902         eq_(res[2], len(c.buf))
1903         eq_(res[3], 0)
1904
1905
1906 class TestOFPGetConfigReply(unittest.TestCase):
1907     """ Test case for ofproto_v1_2_parser.OFPGetConfigReply
1908     """
1909
1910     def _test_parser(self, xid, flags, miss_send_len):
1911         # OFP_HEADER_PACK_STR
1912         # '!BBHI'...version, msg_type, msg_len, xid
1913         version = ofproto.OFP_VERSION
1914         msg_type = ofproto.OFPT_GET_CONFIG_REPLY
1915         msg_len = ofproto.OFP_SWITCH_CONFIG_SIZE
1916
1917         fmt = ofproto.OFP_HEADER_PACK_STR
1918         buf = pack(fmt, version, msg_type, msg_len, xid)
1919
1920         # OFP_SWITCH_CONFIG_PACK_STR
1921         # '!HH'...flags, miss_send_len
1922         fmt = ofproto.OFP_SWITCH_CONFIG_PACK_STR
1923         buf += pack(fmt, flags, miss_send_len)
1924
1925         res = OFPGetConfigReply.parser(object, version, msg_type,
1926                                        msg_len, xid, buf)
1927
1928         eq_(version, res.version)
1929         eq_(msg_type, res.msg_type)
1930         eq_(msg_len, res.msg_len)
1931         eq_(xid, res.xid)
1932         eq_(flags, res.flags)
1933         eq_(miss_send_len, res.miss_send_len)
1934
1935     def test_parser_mid(self):
1936         xid = 3423224276
1937         flags = 41186
1938         miss_send_len = 13838
1939         self._test_parser(xid, flags, miss_send_len)
1940
1941     def test_parser_max(self):
1942         xid = 4294967295
1943         flags = 65535
1944         miss_send_len = 65535
1945         self._test_parser(xid, flags, miss_send_len)
1946
1947     def test_parser_min(self):
1948         xid = 0
1949         flags = ofproto.OFPC_FRAG_NORMAL
1950         miss_send_len = 0
1951         self._test_parser(xid, flags, miss_send_len)
1952
1953     def test_parser_p1(self):
1954         xid = 3423224276
1955         flags = ofproto.OFPC_FRAG_DROP
1956         miss_send_len = 13838
1957         self._test_parser(xid, flags, miss_send_len)
1958
1959     def test_parser_p2(self):
1960         xid = 3423224276
1961         flags = ofproto.OFPC_FRAG_REASM
1962         miss_send_len = 13838
1963         self._test_parser(xid, flags, miss_send_len)
1964
1965     def test_parser_p3(self):
1966         xid = 3423224276
1967         flags = ofproto.OFPC_FRAG_MASK
1968         miss_send_len = 13838
1969         self._test_parser(xid, flags, miss_send_len)
1970
1971     def test_parser_p4(self):
1972         xid = 3423224276
1973         flags = ofproto.OFPC_INVALID_TTL_TO_CONTROLLER
1974         miss_send_len = 13838
1975         self._test_parser(xid, flags, miss_send_len)
1976
1977
1978 class TestOFPSetConfig(unittest.TestCase):
1979     """ Test case for ofproto_v1_2_parser.OFPSetConfig
1980     """
1981
1982     def test_init(self):
1983         # OFP_SWITCH_CONFIG_PACK_STR
1984         # '!HH'...flags, miss_send_len
1985         flags = 41186
1986         miss_send_len = 13838
1987
1988         c = OFPSetConfig(_Datapath, flags, miss_send_len)
1989
1990         eq_(flags, c.flags)
1991         eq_(miss_send_len, c.miss_send_len)
1992
1993     def _test_serialize(self, flags, miss_send_len):
1994         c = OFPSetConfig(_Datapath, flags, miss_send_len)
1995         c.serialize()
1996
1997         eq_(ofproto.OFP_VERSION, c.version)
1998         eq_(ofproto.OFPT_SET_CONFIG, c.msg_type)
1999         eq_(0, c.xid)
2000
2001         fmt = '!' \
2002             + ofproto.OFP_HEADER_PACK_STR.replace('!', '') \
2003             + ofproto.OFP_SWITCH_CONFIG_PACK_STR.replace('!', '')
2004
2005         res = struct.unpack(fmt, six.binary_type(c.buf))
2006
2007         eq_(res[0], ofproto.OFP_VERSION)
2008         eq_(res[1], ofproto.OFPT_SET_CONFIG)
2009         eq_(res[2], len(c.buf))
2010         eq_(res[3], 0)
2011         eq_(res[4], flags)
2012         eq_(res[5], miss_send_len)
2013
2014     def test_serialize_mid(self):
2015         flags = 41186
2016         miss_send_len = 13838
2017         self._test_serialize(flags, miss_send_len)
2018
2019     def test_serialize_max(self):
2020         flags = 65535
2021         miss_send_len = 65535
2022         self._test_serialize(flags, miss_send_len)
2023
2024     def test_serialize_min(self):
2025         flags = ofproto.OFPC_FRAG_NORMAL
2026         miss_send_len = 0
2027         self._test_serialize(flags, miss_send_len)
2028
2029     def test_serialize_p1(self):
2030         flags = ofproto.OFPC_FRAG_DROP
2031         miss_send_len = 13838
2032         self._test_serialize(flags, miss_send_len)
2033
2034     def test_serialize_p2(self):
2035         flags = ofproto.OFPC_FRAG_REASM
2036         miss_send_len = 13838
2037         self._test_serialize(flags, miss_send_len)
2038
2039     def test_serialize_p3(self):
2040         flags = ofproto.OFPC_FRAG_MASK
2041         miss_send_len = 13838
2042         self._test_serialize(flags, miss_send_len)
2043
2044     def test_serialize_p4(self):
2045         flags = ofproto.OFPC_INVALID_TTL_TO_CONTROLLER
2046         miss_send_len = 13838
2047         self._test_serialize(flags, miss_send_len)
2048
2049     @raises(AssertionError)
2050     def test_serialize_check_flags(self):
2051         flags = None
2052         miss_send_len = 13838
2053         c = OFPSetConfig(_Datapath, flags, miss_send_len)
2054         c.serialize()
2055
2056     @raises(AssertionError)
2057     def test_serialize_check_miss_send_len(self):
2058         flags = 41186
2059         miss_send_len = None
2060         c = OFPSetConfig(_Datapath, flags, miss_send_len)
2061         c.serialize()
2062
2063
2064 class TestOFPPacketIn(unittest.TestCase):
2065     """ Test case for ofproto_v1_2_parser.OFPPacketIn
2066     """
2067
2068     def _test_parser(self, xid, buffer_id, total_len=0,
2069                      reason=0, table_id=0, data=None):
2070         if data is None:
2071             data = b''
2072
2073         # OFP_HEADER_PACK_STR
2074         # '!BBHI'...version, msg_type, msg_len, xid
2075         version = ofproto.OFP_VERSION
2076         msg_type = ofproto.OFPT_PACKET_IN
2077         msg_len = ofproto.OFP_PACKET_IN_SIZE + len(data)
2078
2079         fmt = ofproto.OFP_HEADER_PACK_STR
2080         buf = pack(fmt, version, msg_type, msg_len, xid)
2081
2082         # OFP_PACKET_IN_PACK_STR
2083         fmt = ofproto.OFP_PACKET_IN_PACK_STR
2084         buf += pack(fmt, buffer_id, total_len, reason, table_id)
2085
2086         # match
2087         buf_match = bytearray()
2088         match = OFPMatch()
2089         match.serialize(buf_match, 0)
2090         buf += six.binary_type(buf_match)
2091
2092         # data
2093         buf += b'\x00' * 2
2094         buf += data
2095
2096         res = OFPPacketIn.parser(object, version, msg_type, msg_len,
2097                                  xid, buf)
2098
2099         eq_(version, res.version)
2100         eq_(msg_type, res.msg_type)
2101         eq_(msg_len, res.msg_len)
2102         eq_(xid, res.xid)
2103         eq_(buffer_id, res.buffer_id)
2104         eq_(total_len, res.total_len)
2105         eq_(reason, res.reason)
2106         eq_(table_id, res.table_id)
2107         ok_(hasattr(res, 'match'))
2108         eq_(ofproto.OFPMT_OXM, res.match.type)
2109
2110         if data:
2111             eq_(data[:total_len], res.data)
2112
2113     def test_data_is_total_len(self):
2114         xid = 3423224276
2115         buffer_id = 2926809324
2116         reason = 128
2117         table_id = 3
2118         data = b'PacketIn'
2119         total_len = len(data)
2120         self._test_parser(xid, buffer_id, total_len, reason, table_id, data)
2121
2122     def test_data_is_not_total_len(self):
2123         xid = 3423224276
2124         buffer_id = 2926809324
2125         reason = 128
2126         table_id = 3
2127         data = b'PacketIn'
2128         total_len = len(data) - 1
2129         self._test_parser(xid, buffer_id, total_len, reason, table_id, data)
2130
2131     def test_parser_max(self):
2132         # 65535(!H max) - 24(without data) = 65511
2133         xid = 4294967295
2134         buffer_id = 4294967295
2135         reason = 255
2136         table_id = 255
2137         data = b'data'.ljust(65511)
2138         total_len = len(data)
2139         self._test_parser(xid, buffer_id, total_len, reason, table_id, data)
2140
2141     def test_parser_min(self):
2142         xid = 0
2143         buffer_id = 0
2144         reason = ofproto.OFPR_NO_MATCH
2145         table_id = 0
2146         total_len = 0
2147         self._test_parser(xid, buffer_id, total_len, reason, table_id)
2148
2149     def test_parser_p1(self):
2150         data = b'data'.ljust(8)
2151         xid = 3423224276
2152         buffer_id = 2926809324
2153         total_len = len(data)
2154         reason = ofproto.OFPR_ACTION
2155         table_id = 3
2156         self._test_parser(xid, buffer_id, total_len, reason, table_id, data)
2157
2158     def test_parser_p2(self):
2159         data = b'data'.ljust(8)
2160         xid = 3423224276
2161         buffer_id = 2926809324
2162         total_len = len(data)
2163         reason = ofproto.OFPR_INVALID_TTL
2164         table_id = 3
2165         self._test_parser(xid, buffer_id, total_len, reason, table_id, data)
2166
2167
2168 class TestOFPFlowRemoved(unittest.TestCase):
2169     """ Test case for ofproto_v1_2_parser.OFPFlowRemoved
2170     """
2171
2172     def _test_parser(self, xid, cookie, priority,
2173                      reason, table_id, duration_sec,
2174                      duration_nsec, idle_timeout, hard_timeout,
2175                      packet_count, byte_count):
2176         # OFP_HEADER_PACK_STR
2177         # '!BBHI'...version, msg_type, msg_len, xid
2178         version = ofproto.OFP_VERSION
2179         msg_type = ofproto.OFPT_FLOW_REMOVED
2180         msg_len = ofproto.OFP_FLOW_REMOVED_SIZE
2181
2182         fmt = ofproto.OFP_HEADER_PACK_STR
2183         buf = pack(fmt, version, msg_type, msg_len, xid)
2184
2185         # OFP_FLOW_REMOVED_PACK_STR0
2186         # '!QHBBIIHHQQ' ...cookie, priority, reason, table_id,
2187         #                  duration_sec, duration_nsec, idle_timeout,
2188         #                  hard_timeout, packet_count, byte_count
2189
2190         fmt = ofproto.OFP_FLOW_REMOVED_PACK_STR0
2191         buf += pack(fmt, cookie, priority, reason, table_id,
2192                     duration_sec, duration_nsec, idle_timeout,
2193                     hard_timeout, packet_count, byte_count)
2194
2195         # OFP_MATCH_PACK_STR
2196         match = OFPMatch()
2197         buf_match = bytearray()
2198         match.serialize(buf_match, 0)
2199
2200         buf += six.binary_type(buf_match)
2201
2202         res = OFPFlowRemoved.parser(object, version, msg_type,
2203                                     msg_len, xid, buf)
2204
2205         eq_(version, res.version)
2206         eq_(msg_type, res.msg_type)
2207         eq_(msg_len, res.msg_len)
2208         eq_(xid, res.xid)
2209         eq_(cookie, res.cookie)
2210         eq_(priority, res.priority)
2211         eq_(reason, res.reason)
2212         eq_(table_id, res.table_id)
2213         eq_(duration_sec, res.duration_sec)
2214         eq_(duration_nsec, res.duration_nsec)
2215         eq_(idle_timeout, res.idle_timeout)
2216         eq_(hard_timeout, res.hard_timeout)
2217         eq_(packet_count, res.packet_count)
2218         eq_(byte_count, res.byte_count)
2219         ok_(hasattr(res, 'match'))
2220         eq_(ofproto.OFPMT_OXM, res.match.type)
2221
2222     def test_parser_mid(self):
2223         xid = 3423224276
2224         cookie = 178378173441633860
2225         priority = 718
2226         reason = 128
2227         table_id = 169
2228         duration_sec = 2250548154
2229         duration_nsec = 2492776995
2230         idle_timeout = 60284
2231         hard_timeout = 60285
2232         packet_count = 6489108735192644493
2233         byte_count = 7334344481123449724
2234         self._test_parser(xid, cookie, priority,
2235                           reason, table_id, duration_sec,
2236                           duration_nsec, idle_timeout, hard_timeout,
2237                           packet_count, byte_count)
2238
2239     def test_parser_max(self):
2240         xid = 4294967295
2241         cookie = 18446744073709551615
2242         priority = 65535
2243         reason = 255
2244         table_id = 255
2245         duration_sec = 4294967295
2246         duration_nsec = 4294967295
2247         idle_timeout = 65535
2248         hard_timeout = 65535
2249         packet_count = 18446744073709551615
2250         byte_count = 18446744073709551615
2251         self._test_parser(xid, cookie, priority,
2252                           reason, table_id, duration_sec,
2253                           duration_nsec, idle_timeout, hard_timeout,
2254                           packet_count, byte_count)
2255
2256     def test_parser_min(self):
2257         xid = 0
2258         cookie = 0
2259         priority = 0
2260         reason = ofproto.OFPRR_IDLE_TIMEOUT
2261         table_id = 0
2262         duration_sec = 0
2263         duration_nsec = 0
2264         idle_timeout = 0
2265         hard_timeout = 0
2266         packet_count = 0
2267         byte_count = 0
2268         self._test_parser(xid, cookie, priority,
2269                           reason, table_id, duration_sec,
2270                           duration_nsec, idle_timeout, hard_timeout,
2271                           packet_count, byte_count)
2272
2273     def test_parser_p1(self):
2274         xid = 3423224276
2275         cookie = 178378173441633860
2276         priority = 718
2277         reason = ofproto.OFPRR_HARD_TIMEOUT
2278         table_id = 169
2279         duration_sec = 2250548154
2280         duration_nsec = 2492776995
2281         idle_timeout = 60284
2282         hard_timeout = 60285
2283         packet_count = 6489108735192644493
2284         byte_count = 7334344481123449724
2285         self._test_parser(xid, cookie, priority,
2286                           reason, table_id, duration_sec,
2287                           duration_nsec, idle_timeout, hard_timeout,
2288                           packet_count, byte_count)
2289
2290     def test_parser_p2(self):
2291         xid = 3423224276
2292         cookie = 178378173441633860
2293         priority = 718
2294         reason = ofproto.OFPRR_DELETE
2295         table_id = 169
2296         duration_sec = 2250548154
2297         duration_nsec = 2492776995
2298         idle_timeout = 60284
2299         hard_timeout = 60285
2300         packet_count = 6489108735192644493
2301         byte_count = 7334344481123449724
2302         self._test_parser(xid, cookie, priority,
2303                           reason, table_id, duration_sec,
2304                           duration_nsec, idle_timeout, hard_timeout,
2305                           packet_count, byte_count)
2306
2307     def test_parser_p3(self):
2308         xid = 3423224276
2309         cookie = 178378173441633860
2310         priority = 718
2311         reason = ofproto.OFPRR_GROUP_DELETE
2312         table_id = 169
2313         duration_sec = 2250548154
2314         duration_nsec = 2492776995
2315         idle_timeout = 60284
2316         hard_timeout = 60285
2317         packet_count = 6489108735192644493
2318         byte_count = 7334344481123449724
2319         self._test_parser(xid, cookie, priority,
2320                           reason, table_id, duration_sec,
2321                           duration_nsec, idle_timeout, hard_timeout,
2322                           packet_count, byte_count)
2323
2324
2325 class TestOFPPortStatus(unittest.TestCase):
2326     """ Test case for ofproto_v1_2_parser.OFPPortStatus
2327     """
2328
2329     def _test_parser(self, xid, reason,
2330                      port_no, config, state, curr, advertised,
2331                      supported, peer, curr_speed, max_speed):
2332
2333         # OFP_HEADER_PACK_STR
2334         # '!BBHI'...version, msg_type, msg_len, xid
2335         version = ofproto.OFP_VERSION
2336         msg_type = ofproto.OFPT_PORT_STATUS
2337         msg_len = ofproto.OFP_PORT_STATUS_SIZE
2338
2339         fmt = ofproto.OFP_HEADER_PACK_STR
2340         buf = pack(fmt, version, msg_type, msg_len, xid)
2341
2342         # OFP_PORT_STATUS_PACK_STR = '!B7x' + _OFP_PORT_PACK_STR
2343         # '!B7x'...reason, pad(7)
2344         # OFP_PORT_PACK_STR
2345         # '!I4x6s2x16sIIIIIIII'... port_no, pad(4), hw_addr, pad(2),
2346         #                          name, config, state, curr, advertised,
2347         #                          peer, curr_speed, max_speed
2348         hw_addr = '80:ff:9a:e3:72:85'
2349         name = b'name'.ljust(16)
2350
2351         fmt = ofproto.OFP_PORT_STATUS_PACK_STR
2352         buf += pack(fmt, reason, port_no, addrconv.mac.text_to_bin(hw_addr),
2353                     name, config, state, curr, advertised,
2354                     supported, peer, curr_speed, max_speed)
2355
2356         res = OFPPortStatus.parser(object, version, msg_type, msg_len,
2357                                    xid, buf)
2358
2359         eq_(reason, res.reason)
2360         eq_(port_no, res.desc.port_no)
2361         eq_(hw_addr, res.desc.hw_addr)
2362         eq_(name, res.desc.name)
2363         eq_(config, res.desc.config)
2364         eq_(state, res.desc.state)
2365         eq_(curr, res.desc.curr)
2366         eq_(advertised, res.desc.advertised)
2367         eq_(supported, res.desc.supported)
2368         eq_(peer, res.desc.peer)
2369         eq_(curr_speed, res.desc.curr_speed)
2370         eq_(max_speed, res.desc.max_speed)
2371
2372     def test_parser_mid(self):
2373         xid = 3423224276
2374         reason = 128
2375         port_no = 1119692796
2376         config = 2226555987
2377         state = 1678244809
2378         curr = 2850556459
2379         advertised = 2025421682
2380         supported = 2120575149
2381         peer = 2757463021
2382         curr_speed = 2641353507
2383         max_speed = 1797291672
2384         self._test_parser(xid, reason,
2385                           port_no, config, state, curr, advertised,
2386                           supported, peer, curr_speed, max_speed)
2387
2388     def test_parser_max(self):
2389         xid = 4294967295
2390         reason = 255
2391         port_no = ofproto.OFPP_ANY
2392         config = 4294967295
2393         state = 4294967295
2394         curr = 4294967295
2395         advertised = 4294967295
2396         supported = 4294967295
2397         peer = 4294967295
2398         curr_speed = 4294967295
2399         max_speed = 4294967295
2400         self._test_parser(xid, reason,
2401                           port_no, config, state, curr, advertised,
2402                           supported, peer, curr_speed, max_speed)
2403
2404     def test_parser_min(self):
2405         xid = 0
2406         reason = 0
2407         port_no = 0
2408         config = 0
2409         state = 0
2410         curr = 0
2411         advertised = 0
2412         supported = 0
2413         peer = 0
2414         curr_speed = 0
2415         max_speed = 0
2416         self._test_parser(xid, reason,
2417                           port_no, config, state, curr, advertised,
2418                           supported, peer, curr_speed, max_speed)
2419
2420     def test_parser_p1(self):
2421         xid = 3423224276
2422         reason = ofproto.OFPPR_DELETE
2423         port_no = ofproto.OFPP_MAX
2424         config = ofproto.OFPPC_PORT_DOWN
2425         state = ofproto.OFPPS_LINK_DOWN
2426         curr = advertised = supported \
2427              = peer = curr_speed = max_speed \
2428              = ofproto.OFPPF_10MB_HD
2429         self._test_parser(xid, reason,
2430                           port_no, config, state, curr, advertised,
2431                           supported, peer, curr_speed, max_speed)
2432
2433     def test_parser_p2(self):
2434         xid = 3423224276
2435         reason = ofproto.OFPPR_MODIFY
2436         port_no = ofproto.OFPP_MAX
2437         config = ofproto.OFPPC_PORT_DOWN
2438         state = ofproto.OFPPS_LINK_DOWN
2439         curr = advertised = supported \
2440              = peer = curr_speed = max_speed \
2441              = ofproto.OFPPF_10MB_HD
2442         self._test_parser(xid, reason,
2443                           port_no, config, state, curr, advertised,
2444                           supported, peer, curr_speed, max_speed)
2445
2446
2447 class TestOFPPacketOut(unittest.TestCase):
2448     """ Test case for ofproto_v1_2_parser.OFPPacketOut
2449     """
2450
2451     def _test_init(self, in_port):
2452         buffer_id = 0xffffffff
2453         data = b'Message'
2454         out_port = 0x00002ae0
2455         actions = [OFPActionOutput(out_port, 0)]
2456
2457         c = OFPPacketOut(_Datapath, buffer_id, in_port, actions, data)
2458
2459         eq_(buffer_id, c.buffer_id)
2460         eq_(in_port, c.in_port)
2461         eq_(0, c.actions_len)
2462         eq_(data, c.data)
2463         eq_(actions, c.actions)
2464
2465     def test_init(self):
2466         in_port = 0x00040455
2467         self._test_init(in_port)
2468
2469     @raises(AssertionError)
2470     def test_init_check_in_port(self):
2471         in_port = None
2472         self._test_init(in_port)
2473
2474     def _test_serialize(self, buffer_id, in_port, action_cnt=0, data=None):
2475         actions = []
2476         for i in range(action_cnt):
2477             actions.append(ofproto_v1_2_parser.OFPActionOutput(i, 0))
2478
2479         c = OFPPacketOut(_Datapath, buffer_id, in_port, actions, data)
2480         c.serialize()
2481
2482         eq_(ofproto.OFP_VERSION, c.version)
2483         eq_(ofproto.OFPT_PACKET_OUT, c.msg_type)
2484         eq_(0, c.xid)
2485
2486         fmt = ofproto.OFP_HEADER_PACK_STR \
2487             + ofproto.OFP_PACKET_OUT_PACK_STR[1:] \
2488             + ofproto.OFP_ACTION_OUTPUT_PACK_STR[1:] * action_cnt
2489
2490         if data is not None:
2491             fmt += str(len(data)) + 's'
2492
2493         res = struct.unpack(fmt, six.binary_type(c.buf))
2494
2495         eq_(res[0], ofproto.OFP_VERSION)
2496         eq_(res[1], ofproto.OFPT_PACKET_OUT)
2497         eq_(res[2], len(c.buf))
2498         eq_(res[3], 0)
2499         eq_(res[4], buffer_id)
2500         eq_(res[5], in_port)
2501         eq_(res[6], ofproto.OFP_ACTION_OUTPUT_SIZE * action_cnt)
2502
2503         for i in range(action_cnt):
2504             index = 7 + i * 4
2505             eq_(res[index], ofproto.OFPAT_OUTPUT)
2506             eq_(res[index + 1], ofproto.OFP_ACTION_OUTPUT_SIZE)
2507             eq_(res[index + 2], i)
2508             eq_(res[index + 3], 0)
2509
2510         if data:
2511             eq_(res[-1], data)
2512
2513     def test_serialize_true(self):
2514         buffer_id = 0xffffffff
2515         in_port = 0x00040455
2516         action_cnt = 2
2517         data = b'Message'
2518         self._test_serialize(buffer_id, in_port, action_cnt, data)
2519
2520     def test_serialize_none(self):
2521         buffer_id = 0xffffffff
2522         in_port = 0x00040455
2523         self._test_serialize(buffer_id, in_port)
2524
2525     def test_serialize_max(self):
2526         buffer_id = 0xffffffff
2527         in_port = 4294967295
2528         action_cnt = 1
2529         data = b'Message'.ljust(65495)
2530         self._test_serialize(buffer_id, in_port, action_cnt, data)
2531
2532     def test_serialize_min(self):
2533         buffer_id = 0
2534         in_port = 0
2535         self._test_serialize(buffer_id, in_port)
2536
2537     def test_serialize_p1(self):
2538         buffer_id = 2147483648
2539         in_port = ofproto.OFPP_CONTROLLER
2540         self._test_serialize(buffer_id, in_port)
2541
2542     @raises(AssertionError)
2543     def test_serialize_check_buffer_id(self):
2544         buffer_id = 2147483648
2545         in_port = 1
2546         action_cnt = 0
2547         data = b'DATA'
2548         self._test_serialize(buffer_id, in_port, action_cnt, data)
2549
2550
2551 class TestOFPFlowMod(unittest.TestCase):
2552     """ Test case for ofproto_v1_2_parser.OFPFlowMod
2553     """
2554
2555     def test_init(self):
2556         # OFP_FLOW_MOD_PACK_STR0
2557         # '!QQBBHHHIIIH2x'...cookie, cookie_mask, table_id, command,
2558         #                    idle_timeout, hard_timeout, priority, buffer_id,
2559         #                    out_port, out_group, flags
2560         cookie = 2127614848199081640
2561         cookie_mask = 2127614848199081641
2562         table_id = 3
2563         command = 0
2564         idle_timeout = 62317
2565         hard_timeout = 7365
2566         priority = 40163
2567         buffer_id = 4037115955
2568         out_port = 65037
2569         out_group = 6606
2570         flags = 135
2571         instructions = [OFPInstructionGotoTable(table_id)]
2572
2573         in_port = 1
2574         match = OFPMatch()
2575         match.set_in_port(in_port)
2576
2577         c = OFPFlowMod(_Datapath, cookie, cookie_mask, table_id, command,
2578                        idle_timeout, hard_timeout, priority, buffer_id,
2579                        out_port, out_group, flags, match, instructions)
2580
2581         eq_(cookie, c.cookie)
2582         eq_(cookie_mask, c.cookie_mask)
2583         eq_(table_id, c.table_id)
2584         eq_(command, c.command)
2585         eq_(idle_timeout, c.idle_timeout)
2586         eq_(hard_timeout, c.hard_timeout)
2587         eq_(priority, c.priority)
2588         eq_(buffer_id, c.buffer_id)
2589         eq_(out_port, c.out_port)
2590         eq_(out_group, c.out_group)
2591         eq_(flags, c.flags)
2592         eq_(in_port, c.match._flow.in_port)
2593         eq_(instructions[0], c.instructions[0])
2594
2595     def _test_serialize(self, cookie, cookie_mask, table_id,
2596                         command, idle_timeout, hard_timeout,
2597                         priority, buffer_id, out_port,
2598                         out_group, flags, inst_cnt=0):
2599         dl_type = 0x0800
2600         match = OFPMatch()
2601         match.set_dl_type(dl_type)
2602
2603         insts = []
2604         for i in range(inst_cnt):
2605             insts.append(OFPInstructionGotoTable(i))
2606
2607         c = OFPFlowMod(_Datapath, cookie, cookie_mask, table_id, command,
2608                        idle_timeout, hard_timeout, priority, buffer_id,
2609                        out_port, out_group, flags, match, insts)
2610         c.serialize()
2611
2612         eq_(ofproto.OFP_VERSION, c.version)
2613         eq_(ofproto.OFPT_FLOW_MOD, c.msg_type)
2614         eq_(0, c.xid)
2615
2616         fmt = ofproto.OFP_HEADER_PACK_STR \
2617             + ofproto.OFP_FLOW_MOD_PACK_STR0[1:] \
2618             + 'HHHBB' \
2619             + MTEthType.pack_str[1:] + '6x' \
2620             + ofproto.OFP_INSTRUCTION_GOTO_TABLE_PACK_STR[1:] * inst_cnt
2621
2622         res = struct.unpack(fmt, six.binary_type(c.buf))
2623
2624         eq_(res[0], ofproto.OFP_VERSION)
2625         eq_(res[1], ofproto.OFPT_FLOW_MOD)
2626         eq_(res[2], len(c.buf))
2627         eq_(res[3], 0)
2628         eq_(res[4], cookie)
2629         eq_(res[5], cookie_mask)
2630         eq_(res[6], table_id)
2631         eq_(res[7], command)
2632         eq_(res[8], idle_timeout)
2633         eq_(res[9], hard_timeout)
2634         eq_(res[10], priority)
2635         eq_(res[11], buffer_id)
2636         eq_(res[12], out_port)
2637         eq_(res[13], out_group)
2638         eq_(res[14], flags)
2639
2640         # OFP_MATCH (type, length, class, [field, hashmask], n_byte, ip_proto)
2641         eq_(res[15], ofproto.OFPMT_OXM)
2642         eq_(res[16], 10)  # OFP_MATCH_STR + MTEthType.pack_str
2643         eq_(res[17], ofproto.OFPXMC_OPENFLOW_BASIC)
2644         eq_(res[18] >> 1, ofproto.OFPXMT_OFB_ETH_TYPE)
2645         eq_(res[18] & 0b0001, 0)
2646         eq_(res[19], calcsize(MTEthType.pack_str))
2647         eq_(res[20], dl_type)
2648
2649         # insts (type, length, table_id)
2650         for i in range(inst_cnt):
2651             index = 21 + 3 * i
2652             eq_(res[index], ofproto.OFPIT_GOTO_TABLE)
2653             eq_(res[index + 1], ofproto.OFP_INSTRUCTION_GOTO_TABLE_SIZE)
2654             eq_(res[index + 2], i)
2655
2656     def test_serialize_mid(self):
2657         cookie = 2127614848199081640
2658         cookie_mask = 2127614848199081641
2659         table_id = 3
2660         command = 128
2661         idle_timeout = 62317
2662         hard_timeout = 7365
2663         priority = 40163
2664         buffer_id = 4037115955
2665         out_port = 65037
2666         out_group = 6606
2667         flags = 135
2668         inst_cnt = 1
2669         self._test_serialize(cookie, cookie_mask, table_id,
2670                              command, idle_timeout, hard_timeout,
2671                              priority, buffer_id, out_port,
2672                              out_group, flags, inst_cnt)
2673
2674     def test_serialize_max(self):
2675         cookie = 18446744073709551615
2676         cookie_mask = 18446744073709551615
2677         table_id = 255
2678         command = 255
2679         idle_timeout = 65535
2680         hard_timeout = 65535
2681         priority = 65535
2682         buffer_id = 0xffffffff
2683         out_port = 0xffffffff
2684         out_group = 0xffffffff
2685         flags = 65535
2686         inst_cnt = 0xff
2687         self._test_serialize(cookie, cookie_mask, table_id,
2688                              command, idle_timeout, hard_timeout,
2689                              priority, buffer_id, out_port,
2690                              out_group, flags, inst_cnt)
2691
2692     def test_serialize_min(self):
2693         cookie = 0
2694         cookie_mask = 0
2695         table_id = 0
2696         command = ofproto.OFPFC_ADD
2697         idle_timeout = 0
2698         hard_timeout = 0
2699         priority = 0
2700         buffer_id = 0
2701         out_port = 0
2702         out_group = 0
2703         flags = 0
2704         self._test_serialize(cookie, cookie_mask, table_id,
2705                              command, idle_timeout, hard_timeout,
2706                              priority, buffer_id, out_port,
2707                              out_group, flags)
2708
2709     def test_serialize_p1(self):
2710         cookie = 2127614848199081640
2711         cookie_mask = 2127614848199081641
2712         table_id = 3
2713         command = 1
2714         idle_timeout = 62317
2715         hard_timeout = 7365
2716         priority = 40163
2717         buffer_id = 4037115955
2718         out_port = 65037
2719         out_group = 6606
2720         flags = 1 << 0
2721         self._test_serialize(cookie, cookie_mask, table_id,
2722                              command, idle_timeout, hard_timeout,
2723                              priority, buffer_id, out_port,
2724                              out_group, flags)
2725
2726     def test_serialize_p2(self):
2727         cookie = 2127614848199081640
2728         cookie_mask = 2127614848199081641
2729         table_id = 3
2730         command = 2
2731         idle_timeout = 62317
2732         hard_timeout = 7365
2733         priority = 40163
2734         buffer_id = 4037115955
2735         out_port = 65037
2736         out_group = 6606
2737         flags = 1 << 0
2738         self._test_serialize(cookie, cookie_mask, table_id,
2739                              command, idle_timeout, hard_timeout,
2740                              priority, buffer_id, out_port,
2741                              out_group, flags)
2742
2743     def test_serialize_p3(self):
2744         cookie = 2127614848199081640
2745         cookie_mask = 2127614848199081641
2746         table_id = 3
2747         command = 3
2748         idle_timeout = 62317
2749         hard_timeout = 7365
2750         priority = 40163
2751         buffer_id = 4037115955
2752         out_port = 65037
2753         out_group = 6606
2754         flags = 1 << 1
2755         self._test_serialize(cookie, cookie_mask, table_id,
2756                              command, idle_timeout, hard_timeout,
2757                              priority, buffer_id, out_port,
2758                              out_group, flags)
2759
2760     def test_serialize_p4(self):
2761         cookie = 2127614848199081640
2762         cookie_mask = 2127614848199081641
2763         table_id = 3
2764         command = 4
2765         idle_timeout = 62317
2766         hard_timeout = 7365
2767         priority = 40163
2768         buffer_id = 4037115955
2769         out_port = 65037
2770         out_group = 6606
2771         flags = 1 << 2
2772         self._test_serialize(cookie, cookie_mask, table_id,
2773                              command, idle_timeout, hard_timeout,
2774                              priority, buffer_id, out_port,
2775                              out_group, flags)
2776
2777
2778 class TestOFPInstructionGotoTable(unittest.TestCase):
2779     """ Test case for ofproto_v1_2_parser.OFPInstructionGotoTable
2780     """
2781
2782     # OFP_INSTRUCTION_GOTO_TABLE_PACK_STR
2783     # '!HHB3x'...type, len, table_id, pad(3)
2784     type_ = ofproto.OFPIT_GOTO_TABLE
2785     len_ = ofproto.OFP_INSTRUCTION_GOTO_TABLE_SIZE
2786
2787     fmt = ofproto.OFP_INSTRUCTION_GOTO_TABLE_PACK_STR
2788
2789     def test_init(self):
2790         table_id = 3
2791         c = OFPInstructionGotoTable(table_id)
2792
2793         eq_(self.type_, c.type)
2794         eq_(self.len_, c.len)
2795         eq_(table_id, c.table_id)
2796
2797     def _test_parser(self, table_id):
2798         buf = pack(self.fmt, self.type_, self.len_, table_id)
2799         res = OFPInstructionGotoTable.parser(buf, 0)
2800
2801         eq_(res.len, self.len_)
2802         eq_(res.type, self.type_)
2803         eq_(res.table_id, table_id)
2804
2805     def test_parser_mid(self):
2806         self._test_parser(3)
2807
2808     def test_parser_max(self):
2809         self._test_parser(255)
2810
2811     def test_parser_min(self):
2812         self._test_parser(0)
2813
2814     def _test_serialize(self, table_id):
2815         c = OFPInstructionGotoTable(table_id)
2816
2817         buf = bytearray()
2818         c.serialize(buf, 0)
2819
2820         res = struct.unpack(self.fmt, six.binary_type(buf))
2821         eq_(res[0], self.type_)
2822         eq_(res[1], self.len_)
2823         eq_(res[2], table_id)
2824
2825     def test_serialize_mid(self):
2826         self._test_serialize(3)
2827
2828     def test_serialize_max(self):
2829         self._test_serialize(255)
2830
2831     def test_serialize_min(self):
2832         self._test_serialize(0)
2833
2834
2835 class TestOFPInstructionWriteMetadata(unittest.TestCase):
2836     """ Test case for ofproto_v1_2_parser.OFPInstructionWriteMetadata
2837     """
2838
2839     # OFP_INSTRUCTION_WRITE_METADATA_PACK_STR
2840     # '!HH4xQQ'...type, len, pad(4), metadata, metadata_mask
2841     type_ = ofproto.OFPIT_WRITE_METADATA
2842     len_ = ofproto.OFP_INSTRUCTION_WRITE_METADATA_SIZE
2843     metadata = 0x1212121212121212
2844     metadata_mask = 0xff00ff00ff00ff00
2845
2846     fmt = ofproto.OFP_INSTRUCTION_WRITE_METADATA_PACK_STR
2847
2848     def test_init(self):
2849         c = OFPInstructionWriteMetadata(self.metadata,
2850                                         self.metadata_mask)
2851
2852         eq_(self.type_, c.type)
2853         eq_(self.len_, c.len)
2854         eq_(self.metadata, c.metadata)
2855         eq_(self.metadata_mask, c.metadata_mask)
2856
2857     def _test_parser(self, metadata, metadata_mask):
2858         buf = pack(self.fmt, self.type_, self.len_,
2859                    metadata, metadata_mask)
2860
2861         res = OFPInstructionWriteMetadata.parser(buf, 0)
2862         eq_(res.len, self.len_)
2863         eq_(res.type, self.type_)
2864         eq_(res.metadata, metadata)
2865         eq_(res.metadata_mask, metadata_mask)
2866
2867     def test_parser_metadata_mid(self):
2868         self._test_parser(self.metadata, self.metadata_mask)
2869
2870     def test_parser_metadata_max(self):
2871         metadata = 0xffffffffffffffff
2872         self._test_parser(metadata, self.metadata_mask)
2873
2874     def test_parser_metadata_min(self):
2875         metadata = 0
2876         self._test_parser(metadata, self.metadata_mask)
2877
2878     def test_parser_metadata_mask_max(self):
2879         metadata_mask = 0xffffffffffffffff
2880         self._test_parser(self.metadata, metadata_mask)
2881
2882     def test_parser_metadata_mask_min(self):
2883         metadata_mask = 0
2884         self._test_parser(self.metadata, metadata_mask)
2885
2886     def _test_serialize(self, metadata, metadata_mask):
2887         c = OFPInstructionWriteMetadata(metadata,
2888                                         metadata_mask)
2889
2890         buf = bytearray()
2891         c.serialize(buf, 0)
2892
2893         res = struct.unpack(self.fmt, six.binary_type(buf))
2894         eq_(res[0], self.type_)
2895         eq_(res[1], self.len_)
2896         eq_(res[2], metadata)
2897         eq_(res[3], metadata_mask)
2898
2899     def test_serialize_metadata_mid(self):
2900         self._test_serialize(self.metadata, self.metadata_mask)
2901
2902     def test_serialize_metadata_max(self):
2903         metadata = 0xffffffffffffffff
2904         self._test_serialize(metadata, self.metadata_mask)
2905
2906     def test_serialize_metadata_min(self):
2907         metadata = 0
2908         self._test_serialize(metadata, self.metadata_mask)
2909
2910     def test_serialize_metadata_mask_max(self):
2911         metadata_mask = 0xffffffffffffffff
2912         self._test_serialize(self.metadata, metadata_mask)
2913
2914     def test_serialize_metadata_mask_min(self):
2915         metadata_mask = 0
2916         self._test_serialize(self.metadata, metadata_mask)
2917
2918
2919 class TestOFPInstructionActions(unittest.TestCase):
2920     """ Test case for ofproto_v1_2_parser.OFPInstructionActions
2921     """
2922     # OFP_INSTRUCTION_ACTIONS_PACK_STR
2923     # '!HH4x'...type, len, pad(4)
2924     type_ = ofproto.OFPIT_WRITE_ACTIONS
2925     len_ = ofproto.OFP_INSTRUCTION_ACTIONS_SIZE \
2926         + ofproto.OFP_ACTION_OUTPUT_SIZE
2927
2928     fmt = ofproto.OFP_INSTRUCTION_ACTIONS_PACK_STR
2929     buf = pack(fmt, type_, len_)
2930
2931     # OFP_ACTION (OFP_ACTION_OUTPUT)
2932     port = 0x00002ae0
2933     max_len = ofproto.OFP_ACTION_OUTPUT_SIZE
2934     actions = [OFPActionOutput(port, max_len)]
2935     buf_actions = bytearray()
2936     actions[0].serialize(buf_actions, 0)
2937
2938     buf += six.binary_type(buf_actions)
2939
2940     def test_init(self):
2941         c = OFPInstructionActions(self.type_, self.actions)
2942
2943         eq_(self.type_, c.type)
2944         eq_(self.actions, c.actions)
2945
2946     def _test_parser(self, action_cnt):
2947         # OFP_INSTRUCTION_ACTIONS_PACK_STR
2948         # '!HH4x'...type, len, pad(4)
2949         len_ = ofproto.OFP_INSTRUCTION_ACTIONS_SIZE \
2950             + (ofproto.OFP_ACTION_OUTPUT_SIZE * action_cnt)
2951
2952         fmt = ofproto.OFP_INSTRUCTION_ACTIONS_PACK_STR
2953         buf = pack(fmt, self.type_, len_)
2954
2955         actions = []
2956         for a in range(action_cnt):
2957             # OFP_ACTION (OFP_ACTION_OUTPUT)
2958             port = a
2959             action = OFPActionOutput(port, self.max_len)
2960             actions.append(action)
2961             buf_actions = bytearray()
2962             actions[a].serialize(buf_actions, 0)
2963             buf += six.binary_type(buf_actions)
2964
2965         res = OFPInstructionActions.parser(buf, 0)
2966
2967         # 8
2968         eq_(res.len, len_)
2969         eq_(res.type, self.type_)
2970
2971         # 8 + 16 * action_cnt < 65535 byte
2972         # action_cnt <= 4095
2973         for a in range(action_cnt):
2974             eq_(res.actions[a].type, actions[a].type)
2975             eq_(res.actions[a].len, actions[a].len)
2976             eq_(res.actions[a].port, actions[a].port)
2977             eq_(res.actions[a].max_len, actions[a].max_len)
2978
2979     def test_parser_mid(self):
2980         self._test_parser(2047)
2981
2982     def test_parser_max(self):
2983         self._test_parser(4095)
2984
2985     def test_parser_min(self):
2986         self._test_parser(0)
2987
2988     def _test_serialize(self, action_cnt):
2989         # OFP_INSTRUCTION_ACTIONS_PACK_STR
2990         # '!HH4x'...type, len, pad(4)
2991         len_ = ofproto.OFP_INSTRUCTION_ACTIONS_SIZE \
2992             + (ofproto.OFP_ACTION_OUTPUT_SIZE * action_cnt)
2993
2994         actions = []
2995         for a in range(action_cnt):
2996             # OFP_ACTION (OFP_ACTION_OUTPUT)
2997             port = a
2998             action = OFPActionOutput(port, self.max_len)
2999             actions.append(action)
3000
3001         c = OFPInstructionActions(self.type_, actions)
3002
3003         buf = bytearray()
3004         c.serialize(buf, 0)
3005
3006         fmt = '!' \
3007             + ofproto.OFP_INSTRUCTION_ACTIONS_PACK_STR.replace('!', '')
3008
3009         for a in range(action_cnt):
3010             fmt += ofproto.OFP_ACTION_OUTPUT_PACK_STR.replace('!', '')
3011         res = struct.unpack(fmt, six.binary_type(buf))
3012
3013         eq_(res[0], self.type_)
3014         eq_(res[1], len_)
3015
3016         for a in range(action_cnt):
3017             d = 2 + a * 4
3018             eq_(res[d], actions[a].type)
3019             eq_(res[d + 1], actions[a].len)
3020             eq_(res[d + 2], actions[a].port)
3021             eq_(res[d + 3], actions[a].max_len)
3022
3023     def test_serialize_mid(self):
3024         self._test_serialize(2047)
3025
3026     def test_serialize_max(self):
3027         self._test_serialize(4095)
3028
3029     def test_serialize_min(self):
3030         self._test_serialize(0)
3031
3032
3033 class TestOFPActionHeader(unittest.TestCase):
3034     """ Test case for ofproto_v1_2_parser.OFPActionHeader
3035     """
3036
3037     def test_init(self):
3038         # OFP_ACTION_HEADER_PACK_STR
3039         # '!HH4x'...type, len, pad(4)
3040         type_ = ofproto.OFPAT_OUTPUT
3041         len_ = ofproto.OFP_ACTION_HEADER_SIZE
3042
3043         fmt = ofproto.OFP_ACTION_HEADER_PACK_STR
3044         buf = pack(fmt, type_, len_)
3045
3046         c = OFPActionHeader(type_, len_)
3047
3048         eq_(type_, c.type)
3049         eq_(len_, c.len)
3050
3051     def _test_serialize(self, type_, len_):
3052         # OFP_ACTION_HEADER_PACK_STR
3053         # '!HH4x'...type, len, pad(4)
3054
3055         fmt = ofproto.OFP_ACTION_HEADER_PACK_STR
3056         buf = pack(fmt, type_, len_)
3057
3058         c = OFPActionHeader(type_, len_)
3059
3060         buf = bytearray()
3061         c.serialize(buf, 0)
3062
3063         fmt = ofproto.OFP_ACTION_HEADER_PACK_STR
3064         res = struct.unpack(fmt, six.binary_type(buf))
3065
3066         eq_(res[0], type_)
3067         eq_(res[1], len_)
3068
3069     def test_serialize_mid(self):
3070         type_ = 11
3071         len_ = 8
3072         self._test_serialize(type_, len_)
3073
3074     def test_serialize_max(self):
3075         type_ = 0xffff
3076         len_ = 0xffff
3077         self._test_serialize(type_, len_)
3078
3079     def test_serialize_min(self):
3080         type_ = 0
3081         len_ = 0
3082         self._test_serialize(type_, len_)
3083
3084
3085 class TestOFPActionOutput(unittest.TestCase):
3086     """ Test case for ofproto_v1_2_parser.OFPActionOutput
3087     """
3088
3089     # OFP_ACTION_OUTPUT_PACK_STR
3090     # '!HHIH6x'...type, len, port, max_len, pad(6)
3091     type_ = ofproto.OFPAT_OUTPUT
3092     len_ = ofproto.OFP_ACTION_OUTPUT_SIZE
3093
3094     def test_init(self):
3095         port = 6606
3096         max_len = 1500
3097         fmt = ofproto.OFP_ACTION_OUTPUT_PACK_STR
3098         c = OFPActionOutput(port, max_len)
3099         eq_(port, c.port)
3100         eq_(max_len, c.max_len)
3101
3102     def _test_parser(self, port, max_len):
3103         fmt = ofproto.OFP_ACTION_OUTPUT_PACK_STR
3104         buf = pack(fmt, self.type_, self.len_, port, max_len)
3105
3106         c = OFPActionOutput(port, max_len)
3107
3108         res = c.parser(buf, 0)
3109
3110         eq_(res.len, self.len_)
3111         eq_(res.type, self.type_)
3112         eq_(res.port, port)
3113         eq_(res.max_len, max_len)
3114
3115     def test_parser_mid(self):
3116         port = 6606
3117         max_len = 16
3118         self._test_parser(port, max_len)
3119
3120     def test_parser_max(self):
3121         port = 4294967295
3122         max_len = 0xffff
3123         self._test_parser(port, max_len)
3124
3125     def test_parser_min(self):
3126         port = 0
3127         max_len = 0
3128         self._test_parser(port, max_len)
3129
3130     def test_parser_p1(self):
3131         port = 6606
3132         max_len = 0xffe5
3133         self._test_parser(port, max_len)
3134
3135     def _test_serialize(self, port, max_len):
3136         c = OFPActionOutput(port, max_len)
3137
3138         buf = bytearray()
3139         c.serialize(buf, 0)
3140
3141         fmt = ofproto.OFP_ACTION_OUTPUT_PACK_STR
3142         res = struct.unpack(fmt, six.binary_type(buf))
3143         eq_(res[0], self.type_)
3144         eq_(res[1], self.len_)
3145         eq_(res[2], port)
3146         eq_(res[3], max_len)
3147
3148     def test_serialize_mid(self):
3149         port = 6606
3150         max_len = 16
3151         self._test_serialize(port, max_len)
3152
3153     def test_serialize_max(self):
3154         port = 4294967295
3155         max_len = 0xffff
3156         self._test_serialize(port, max_len)
3157
3158     def test_serialize_min(self):
3159         port = 0
3160         max_len = 0
3161         self._test_serialize(port, max_len)
3162
3163     def test_serialize_p1(self):
3164         port = 6606
3165         max_len = 0xffe5
3166         self._test_serialize(port, max_len)
3167
3168
3169 class TestOFPActionGroup(unittest.TestCase):
3170     """ Test case for ofproto_v1_2_parser.OFPActionGroup
3171     """
3172
3173     # OFP_ACTION_GROUP_PACK_STR
3174     # '!HHI'...type, len, group_id
3175     type_ = ofproto.OFPAT_GROUP
3176     len_ = ofproto.OFP_ACTION_GROUP_SIZE
3177     group_id = 6606
3178
3179     fmt = ofproto.OFP_ACTION_GROUP_PACK_STR
3180
3181     def test_init(self):
3182         c = OFPActionGroup(self.group_id)
3183         eq_(self.group_id, c.group_id)
3184
3185     def _test_parser(self, group_id):
3186         buf = pack(self.fmt, self.type_, self.len_, group_id)
3187
3188         res = OFPActionGroup.parser(buf, 0)
3189         eq_(res.len, self.len_)
3190         eq_(res.type, self.type_)
3191         eq_(res.group_id, group_id)
3192
3193     def test_parser_mid(self):
3194         self._test_parser(self.group_id)
3195
3196     def test_parser_max(self):
3197         self._test_parser(4294967295)
3198
3199     def test_parser_min(self):
3200         self._test_parser(0)
3201
3202     def _test_serialize(self, group_id):
3203         c = OFPActionGroup(group_id)
3204
3205         buf = bytearray()
3206         c.serialize(buf, 0)
3207
3208         res = struct.unpack(self.fmt, six.binary_type(buf))
3209         eq_(res[0], self.type_)
3210         eq_(res[1], self.len_)
3211         eq_(res[2], group_id)
3212
3213     def test_serialize_mid(self):
3214         self._test_serialize(self.group_id)
3215
3216     def test_serialize_max(self):
3217         self._test_serialize(4294967295)
3218
3219     def test_serialize_min(self):
3220         self._test_serialize(0)
3221
3222
3223 class TestOFPActionSetQueue(unittest.TestCase):
3224     """ Test case for ofproto_v1_2_parser.OFPActionSetQueue
3225     """
3226
3227     # OFP_ACTION_SET_QUEUE_PACK_STR
3228     # '!HHI'...type, len, queue_id
3229     type_ = ofproto.OFPAT_SET_QUEUE
3230     len_ = ofproto.OFP_ACTION_SET_QUEUE_SIZE
3231     queue_id = 6606
3232
3233     fmt = ofproto.OFP_ACTION_SET_QUEUE_PACK_STR
3234
3235     def test_init(self):
3236         c = OFPActionSetQueue(self.queue_id)
3237         eq_(self.queue_id, c.queue_id)
3238
3239     def _test_parser(self, queue_id):
3240         buf = pack(self.fmt, self.type_, self.len_, queue_id)
3241
3242         res = OFPActionSetQueue.parser(buf, 0)
3243         eq_(res.len, self.len_)
3244         eq_(res.type, self.type_)
3245         eq_(res.queue_id, queue_id)
3246
3247     def test_parser_mid(self):
3248         self._test_parser(self.queue_id)
3249
3250     def test_parser_max(self):
3251         self._test_parser(4294967295)
3252
3253     def test_parser_min(self):
3254         self._test_parser(0)
3255
3256     def _test_serialize(self, queue_id):
3257         c = OFPActionSetQueue(queue_id)
3258
3259         buf = bytearray()
3260         c.serialize(buf, 0)
3261
3262         res = struct.unpack(self.fmt, six.binary_type(buf))
3263         eq_(res[0], self.type_)
3264         eq_(res[1], self.len_)
3265         eq_(res[2], queue_id)
3266
3267     def test_serialize_mid(self):
3268         self._test_serialize(self.queue_id)
3269
3270     def test_serialize_max(self):
3271         self._test_serialize(4294967295)
3272
3273     def test_serialize_min(self):
3274         self._test_serialize(0)
3275
3276
3277 class TestOFPActionSetMplsTtl(unittest.TestCase):
3278     """ Test case for ofproto_v1_2_parser.OFPActionSetMplsTtl
3279     """
3280
3281     # OFP_ACTION_MPLS_TTL_PACK_STR
3282     # '!HHB3x'...type, len, mpls_ttl, pad(3)
3283     type_ = ofproto.OFPAT_SET_MPLS_TTL
3284     len_ = ofproto.OFP_ACTION_MPLS_TTL_SIZE
3285     mpls_ttl = 254
3286
3287     fmt = ofproto.OFP_ACTION_MPLS_TTL_PACK_STR
3288
3289     def test_init(self):
3290         c = OFPActionSetMplsTtl(self.mpls_ttl)
3291         eq_(self.mpls_ttl, c.mpls_ttl)
3292
3293     def _test_parser(self, mpls_ttl):
3294         buf = pack(self.fmt, self.type_, self.len_, mpls_ttl)
3295
3296         res = OFPActionSetMplsTtl.parser(buf, 0)
3297         eq_(res.len, self.len_)
3298         eq_(res.type, self.type_)
3299         eq_(res.mpls_ttl, mpls_ttl)
3300
3301     def test_parser_mid(self):
3302         self._test_parser(self.mpls_ttl)
3303
3304     def test_parser_max(self):
3305         self._test_parser(255)
3306
3307     def test_parser_min(self):
3308         self._test_parser(0)
3309
3310     def _test_serialize(self, mpls_ttl):
3311         c = OFPActionSetMplsTtl(mpls_ttl)
3312
3313         buf = bytearray()
3314         c.serialize(buf, 0)
3315
3316         res = struct.unpack(self.fmt, six.binary_type(buf))
3317         eq_(res[0], self.type_)
3318         eq_(res[1], self.len_)
3319         eq_(res[2], mpls_ttl)
3320
3321     def test_serialize_mid(self):
3322         self._test_serialize(self.mpls_ttl)
3323
3324     def test_serialize_max(self):
3325         self._test_serialize(255)
3326
3327     def test_serialize_min(self):
3328         self._test_serialize(0)
3329
3330
3331 class TestOFPActionDecMplsTtl(unittest.TestCase):
3332     """ Test case for ofproto_v1_2_parser.OFPActionDecMplsTtl
3333     """
3334
3335     type_ = ofproto.OFPAT_DEC_MPLS_TTL
3336     len_ = ofproto.OFP_ACTION_MPLS_TTL_SIZE
3337     fmt = ofproto.OFP_ACTION_HEADER_PACK_STR
3338     buf = pack(fmt, type_, len_)
3339     c = OFPActionDecMplsTtl()
3340
3341     def test_parser(self):
3342         res = self.c.parser(self.buf, 0)
3343
3344         eq_(res.len, self.len_)
3345         eq_(res.type, self.type_)
3346
3347     def test_serialize(self):
3348         buf = bytearray()
3349         self.c.serialize(buf, 0)
3350
3351         res = struct.unpack(self.fmt, six.binary_type(buf))
3352         eq_(res[0], self.type_)
3353         eq_(res[1], self.len_)
3354
3355
3356 class TestOFPActionSetNwTtl(unittest.TestCase):
3357     """ Test case for ofproto_v1_2_parser.OFPActionSetNwTtl
3358     """
3359
3360     # OFP_ACTION_NW_TTL_PACK_STR
3361     # '!HHB3x'...type, len, nw_ttl, pad(3)
3362     type_ = ofproto.OFPAT_SET_NW_TTL
3363     len_ = ofproto.OFP_ACTION_NW_TTL_SIZE
3364     nw_ttl = 240
3365
3366     fmt = ofproto.OFP_ACTION_NW_TTL_PACK_STR
3367
3368     def test_init(self):
3369         c = OFPActionSetNwTtl(self.nw_ttl)
3370         eq_(self.nw_ttl, c.nw_ttl)
3371
3372     def _test_parser(self, nw_ttl):
3373         buf = pack(self.fmt, self.type_, self.len_, nw_ttl)
3374
3375         res = OFPActionSetNwTtl.parser(buf, 0)
3376         eq_(res.type, self.type_)
3377         eq_(res.len, self.len_)
3378         eq_(res.nw_ttl, nw_ttl)
3379
3380     def test_parser_mid(self):
3381         self._test_parser(self.nw_ttl)
3382
3383     def test_parser_max(self):
3384         self._test_parser(255)
3385
3386     def test_parser_min(self):
3387         self._test_parser(0)
3388
3389     def _test_serialize(self, nw_ttl):
3390         c = OFPActionSetNwTtl(nw_ttl)
3391
3392         buf = bytearray()
3393         c.serialize(buf, 0)
3394
3395         res = struct.unpack(self.fmt, six.binary_type(buf))
3396         eq_(res[0], self.type_)
3397         eq_(res[1], self.len_)
3398         eq_(res[2], nw_ttl)
3399
3400     def test_serialize_mid(self):
3401         self._test_serialize(self.nw_ttl)
3402
3403     def test_serialize_max(self):
3404         self._test_serialize(255)
3405
3406     def test_serialize_min(self):
3407         self._test_serialize(0)
3408
3409
3410 class TestOFPActionDecNwTtl(unittest.TestCase):
3411     """ Test case for ofproto_v1_2_parser.OFPActionDecNwTtl
3412     """
3413
3414     type_ = ofproto.OFPAT_DEC_NW_TTL
3415     len_ = ofproto.OFP_ACTION_NW_TTL_SIZE
3416     fmt = ofproto.OFP_ACTION_HEADER_PACK_STR
3417     buf = pack(fmt, type_, len_)
3418     c = OFPActionDecNwTtl()
3419
3420     def test_parser(self):
3421         res = self.c.parser(self.buf, 0)
3422
3423         eq_(res.len, self.len_)
3424         eq_(res.type, self.type_)
3425
3426     def test_serialize(self):
3427         buf = bytearray()
3428         self.c.serialize(buf, 0)
3429
3430         res = struct.unpack(self.fmt, six.binary_type(buf))
3431         eq_(res[0], self.type_)
3432         eq_(res[1], self.len_)
3433
3434
3435 class TestOFPActionCopyTtlOut(unittest.TestCase):
3436     """ Test case for ofproto_v1_2_parser.OFPActionCopyTtlOut
3437     """
3438
3439     type_ = ofproto.OFPAT_COPY_TTL_OUT
3440     len_ = ofproto.OFP_ACTION_HEADER_SIZE
3441     fmt = ofproto.OFP_ACTION_HEADER_PACK_STR
3442     buf = pack(fmt, type_, len_)
3443     c = OFPActionCopyTtlOut()
3444
3445     def test_parser(self):
3446         res = self.c.parser(self.buf, 0)
3447         eq_(res.len, self.len_)
3448         eq_(res.type, self.type_)
3449
3450     def test_serialize(self):
3451         buf = bytearray()
3452         self.c.serialize(buf, 0)
3453
3454         res = struct.unpack(self.fmt, six.binary_type(buf))
3455         eq_(res[0], self.type_)
3456         eq_(res[1], self.len_)
3457
3458
3459 class TestOFPActionCopyTtlIn(unittest.TestCase):
3460     """ Test case for ofproto_v1_2_parser.OFPActionCopyTtlIn
3461     """
3462
3463     # OFP_ACTION_HEADER_PACK_STR
3464     # '!HH'...type, len
3465     type_ = ofproto.OFPAT_COPY_TTL_IN
3466     len_ = ofproto.OFP_ACTION_HEADER_SIZE
3467     fmt = ofproto.OFP_ACTION_HEADER_PACK_STR
3468     buf = pack(fmt, type_, len_)
3469     c = OFPActionCopyTtlIn()
3470
3471     def test_parser(self):
3472         res = self.c.parser(self.buf, 0)
3473
3474         eq_(res.len, self.len_)
3475         eq_(res.type, self.type_)
3476
3477     def test_serialize(self):
3478         buf = bytearray()
3479         self.c.serialize(buf, 0)
3480
3481         res = struct.unpack(self.fmt, six.binary_type(buf))
3482         eq_(res[0], self.type_)
3483         eq_(res[1], self.len_)
3484
3485
3486 class TestOFPActionPushVlan(unittest.TestCase):
3487     """ Test case for ofproto_v1_2_parser.OFPActionPushVlan
3488     """
3489
3490     # OFP_ACTION_PUSH_PACK_STR
3491     # '!HHH2x'...type, len, ethertype, pad(2)
3492     type_ = ofproto.OFPAT_PUSH_VLAN
3493     len_ = ofproto.OFP_ACTION_PUSH_SIZE
3494     fmt = ofproto.OFP_ACTION_PUSH_PACK_STR
3495
3496     def test_init(self):
3497         ethertype = 0x8100
3498         c = OFPActionPushVlan(ethertype)
3499         eq_(ethertype, c.ethertype)
3500
3501     def _test_parser(self, ethertype):
3502         buf = pack(self.fmt, self.type_, self.len_, ethertype)
3503
3504         res = OFPActionPushVlan.parser(buf, 0)
3505         eq_(res.type, self.type_)
3506         eq_(res.len, self.len_)
3507         eq_(res.ethertype, ethertype)
3508
3509     def test_parser_mid(self):
3510         self._test_parser(0x8100)
3511
3512     def test_parser_max(self):
3513         self._test_parser(0xffff)
3514
3515     def test_parser_min(self):
3516         self._test_parser(0)
3517
3518     def _test_serialize(self, ethertype):
3519         c = OFPActionPushVlan(ethertype)
3520         buf = bytearray()
3521         c.serialize(buf, 0)
3522
3523         res = struct.unpack(self.fmt, six.binary_type(buf))
3524         eq_(res[0], self.type_)
3525         eq_(res[1], self.len_)
3526         eq_(res[2], ethertype)
3527
3528     def test_serialize_mid(self):
3529         self._test_serialize(0x8100)
3530
3531     def test_serialize_max(self):
3532         self._test_serialize(0xffff)
3533
3534     def test_serialize_min(self):
3535         self._test_serialize(0)
3536
3537
3538 class TestOFPActionPushMpls(unittest.TestCase):
3539     """ Test case for ofproto_v1_2_parser.OFPActionPushMpls
3540     """
3541
3542     # OFP_ACTION_PUSH_PACK_STR
3543     # '!HHH2x'...type, len, ethertype, pad(2)
3544     type_ = ofproto.OFPAT_PUSH_MPLS
3545     len_ = ofproto.OFP_ACTION_PUSH_SIZE
3546     fmt = ofproto.OFP_ACTION_PUSH_PACK_STR
3547
3548     def test_init(self):
3549         ethertype = 0x8100
3550         c = OFPActionPushMpls(ethertype)
3551         eq_(ethertype, c.ethertype)
3552
3553     def _test_parser(self, ethertype):
3554         buf = pack(self.fmt, self.type_, self.len_, ethertype)
3555
3556         res = OFPActionPushMpls.parser(buf, 0)
3557         eq_(res.type, self.type_)
3558         eq_(res.len, self.len_)
3559         eq_(res.ethertype, ethertype)
3560
3561     def test_parser_mid(self):
3562         self._test_parser(0x8100)
3563
3564     def test_parser_max(self):
3565         self._test_parser(0xffff)
3566
3567     def test_parser_min(self):
3568         self._test_parser(0)
3569
3570     def _test_serialize(self, ethertype):
3571         c = OFPActionPushMpls(ethertype)
3572         buf = bytearray()
3573         c.serialize(buf, 0)
3574
3575         res = struct.unpack(self.fmt, six.binary_type(buf))
3576         eq_(res[0], self.type_)
3577         eq_(res[1], self.len_)
3578         eq_(res[2], ethertype)
3579
3580     def test_serialize_mid(self):
3581         self._test_serialize(0x8100)
3582
3583     def test_serialize_max(self):
3584         self._test_serialize(0xffff)
3585
3586     def test_serialize_min(self):
3587         self._test_serialize(0)
3588
3589
3590 class TestOFPActionPopVlan(unittest.TestCase):
3591     """ Test case for ofproto_v1_2_parser.OFPActionPopVlan
3592     """
3593
3594     # OFP_ACTION_HEADER_PACK_STR
3595     # '!HH'...type, len
3596     type_ = ofproto.OFPAT_POP_VLAN
3597     len_ = ofproto.OFP_ACTION_HEADER_SIZE
3598     fmt = ofproto.OFP_ACTION_HEADER_PACK_STR
3599     buf = pack(fmt, type_, len_)
3600     c = OFPActionPopVlan()
3601
3602     def test_parser(self):
3603         res = self.c.parser(self.buf, 0)
3604         eq_(self.type_, res.type)
3605         eq_(self.len_, res.len)
3606
3607     def test_serialize(self):
3608         buf = bytearray()
3609         self.c.serialize(buf, 0)
3610
3611         res = struct.unpack(self.fmt, six.binary_type(buf))
3612         eq_(res[0], self.type_)
3613         eq_(res[1], self.len_)
3614
3615
3616 class TestOFPActionPopMpls(unittest.TestCase):
3617     """ Test case for ofproto_v1_2_parser.OFPActionPopMpls
3618     """
3619
3620     # OFP_ACTION_POP_MPLS_PACK_STR
3621     # '!HHH2x'...type, len, ethertype, pad(2)
3622     type_ = ofproto.OFPAT_POP_MPLS
3623     len_ = ofproto.OFP_ACTION_POP_MPLS_SIZE
3624     fmt = ofproto.OFP_ACTION_POP_MPLS_PACK_STR
3625
3626     def test_init(self):
3627         ethertype = 0x8100
3628         c = OFPActionPopMpls(ethertype)
3629         eq_(ethertype, c.ethertype)
3630
3631     def _test_parser(self, ethertype):
3632         buf = pack(self.fmt, self.type_, self.len_, ethertype)
3633
3634         res = OFPActionPopMpls.parser(buf, 0)
3635         eq_(res.type, self.type_)
3636         eq_(res.len, self.len_)
3637         eq_(res.ethertype, ethertype)
3638
3639     def test_parser_mid(self):
3640         self._test_parser(0x8100)
3641
3642     def test_parser_max(self):
3643         self._test_parser(0xffff)
3644
3645     def test_parser_min(self):
3646         self._test_parser(0)
3647
3648     def _test_serialize(self, ethertype):
3649         c = OFPActionPopMpls(ethertype)
3650         buf = bytearray()
3651         c.serialize(buf, 0)
3652
3653         res = struct.unpack(self.fmt, six.binary_type(buf))
3654         eq_(res[0], self.type_)
3655         eq_(res[1], self.len_)
3656         eq_(res[2], ethertype)
3657
3658     def test_serialize_mid(self):
3659         self._test_serialize(0x8100)
3660
3661     def test_serialize_max(self):
3662         self._test_serialize(0xffff)
3663
3664     def test_serialize_min(self):
3665         self._test_serialize(0)
3666
3667
3668 class TestOFPActionSetField(unittest.TestCase):
3669     """ Test case for ofproto_v1_2_parser.OFPActionSetField
3670     """
3671
3672     type_ = ofproto.OFPAT_SET_FIELD
3673     header = ofproto.OXM_OF_IN_PORT
3674     in_port = 6606
3675
3676     field = MTInPort(header, in_port)
3677     length = ofproto.OFP_ACTION_SET_FIELD_SIZE + field.oxm_len()
3678     len_ = utils.round_up(length, 8)
3679
3680     fmt = '!HHII4x'
3681     buf = pack(fmt, type_, len_, header, in_port)
3682
3683     c = OFPActionSetField(field)
3684
3685     def test_init(self):
3686         eq_(self.field, self.c.field)
3687
3688     def test_parser(self):
3689         res = self.c.parser(self.buf, 0)
3690
3691         eq_(res.type, self.type_)
3692         eq_(res.len, self.len_)
3693         eq_(res.field.header, self.header)
3694         eq_(res.field.value, self.in_port)
3695
3696     def test_serialize(self):
3697         buf = bytearray()
3698         self.c.serialize(buf, 0)
3699
3700         res = struct.unpack(self.fmt, six.binary_type(buf))
3701
3702         eq_(res[0], self.type_)
3703         eq_(res[1], self.len_)
3704         eq_(res[2], self.header)
3705         eq_(res[3], self.in_port)
3706
3707
3708 class TestOFPActionExperimenter(unittest.TestCase):
3709     """ Test case for ofproto_v1_2_parser.OFPActionExperimenter
3710     """
3711
3712     # OFP_ACTION_EXPERIMENTER_HEADER_PACK_STR v1.2
3713     # '!HHI'...type, len, experimenter
3714     type_ = ofproto.OFPAT_EXPERIMENTER
3715     len_ = ofproto.OFP_ACTION_EXPERIMENTER_HEADER_SIZE
3716     fmt = ofproto.OFP_ACTION_EXPERIMENTER_HEADER_PACK_STR
3717
3718     def test_init(self):
3719         experimenter = 4294967295
3720         c = OFPActionExperimenter(experimenter)
3721         eq_(experimenter, c.experimenter)
3722
3723     def _test_parser(self, experimenter):
3724         buf = pack(self.fmt, self.type_, self.len_, experimenter)
3725
3726         res = OFPActionExperimenter.parser(buf, 0)
3727         eq_(res.type, self.type_)
3728         eq_(res.len, self.len_)
3729         eq_(res.experimenter, experimenter)
3730
3731     def test_parser_mid(self):
3732         experimenter = 2147483648
3733         self._test_parser(experimenter)
3734
3735     def test_parser_max(self):
3736         experimenter = 4294967295
3737         self._test_parser(experimenter)
3738
3739     def test_parser_min(self):
3740         experimenter = 0
3741         self._test_parser(experimenter)
3742
3743     def _test_serialize(self, experimenter):
3744         c = OFPActionExperimenter(experimenter)
3745
3746         buf = bytearray()
3747         c.serialize(buf, 0)
3748
3749         res = struct.unpack(self.fmt, six.binary_type(buf))
3750         eq_(res[0], self.type_)
3751         eq_(res[1], self.len_)
3752         eq_(res[2], experimenter)
3753
3754     def test_serialize_mid(self):
3755         experimenter = 2147483648
3756         self._test_serialize(experimenter)
3757
3758     def test_serialize_max(self):
3759         experimenter = 4294967295
3760         self._test_serialize(experimenter)
3761
3762     def test_serialize_min(self):
3763         experimenter = 0
3764         self._test_serialize(experimenter)
3765
3766
3767 class TestOFPBucket(unittest.TestCase):
3768     """ Test case for ofproto_v1_2_parser.OFPBucket
3769     """
3770
3771     def test_init(self):
3772         # OFP_BUCKET_PACK_STR
3773         # '!HHII4x'...len, weight, watch_port, watch_group, pad(4)
3774         weight = 4386
3775         watch_port = 6606
3776         watch_group = 3
3777
3778         # OFP_ACTION (OFP_ACTION_OUTPUT)
3779         port = 3
3780         max_len = 1500
3781         actions = [OFPActionOutput(port, max_len)]
3782
3783         c = OFPBucket(weight, watch_port, watch_group, actions)
3784         eq_(weight, c.weight)
3785         eq_(watch_port, c.watch_port)
3786         eq_(watch_group, c.watch_group)
3787         eq_(1, len(c.actions))
3788         eq_(port, c.actions[0].port)
3789         eq_(max_len, c.actions[0].max_len)
3790
3791     def _test_parser(self, weight, watch_port, watch_group, action_cnt):
3792         # OFP_BUCKET_PACK_STR
3793         # '!HHII4x'...len, weight, watch_port, watch_group, pad(4)
3794         len_ = ofproto.OFP_BUCKET_SIZE \
3795             + (ofproto.OFP_ACTION_OUTPUT_SIZE * action_cnt)
3796
3797         fmt = ofproto.OFP_BUCKET_PACK_STR
3798         buf = pack(fmt, len_, weight, watch_port, watch_group)
3799
3800         actions = []
3801         for a in range(action_cnt):
3802             # OFP_ACTION (OFP_ACTION_OUTPUT)
3803             port = a
3804             max_len = ofproto.OFP_ACTION_OUTPUT_SIZE
3805             action = OFPActionOutput(port, max_len)
3806             actions.append(action)
3807             buf_actions = bytearray()
3808             actions[a].serialize(buf_actions, 0)
3809             buf += six.binary_type(buf_actions)
3810
3811         res = OFPBucket.parser(buf, 0)
3812
3813         # 16
3814         eq_(weight, res.weight)
3815         eq_(watch_port, res.watch_port)
3816         eq_(watch_group, res.watch_group)
3817
3818         # 16 + 16 * action_cnt < 65535 byte
3819         # action_cnt <= 4094
3820         for a in range(action_cnt):
3821             eq_(actions[a].type, res.actions[a].type)
3822             eq_(actions[a].len, res.actions[a].len)
3823             eq_(actions[a].port, res.actions[a].port)
3824             eq_(actions[a].max_len, res.actions[a].max_len)
3825
3826     def test_parser_mid(self):
3827         weight = 4386
3828         watch_port = 6606
3829         watch_group = 3
3830         action_cnt = 2047
3831         self._test_parser(weight, watch_port,
3832                           watch_group, action_cnt)
3833
3834     def test_parser_max(self):
3835         weight = 65535
3836         watch_port = 4294967295
3837         watch_group = 4294967295
3838         action_cnt = 4094
3839         self._test_parser(weight, watch_port,
3840                           watch_group, action_cnt)
3841
3842     def test_parser_min(self):
3843         weight = 0
3844         watch_port = 0
3845         watch_group = 0
3846         action_cnt = 0
3847         self._test_parser(weight, watch_port,
3848                           watch_group, action_cnt)
3849
3850     def _test_serialize(self, weight, watch_port, watch_group,
3851                         action_cnt):
3852         # OFP_BUCKET_PACK_STR
3853         # '!HHII4x'...len, weight, watch_port, watch_group, pad(4)
3854         len_ = ofproto.OFP_BUCKET_SIZE \
3855             + (ofproto.OFP_ACTION_OUTPUT_SIZE * action_cnt)
3856
3857         actions = []
3858         for a in range(action_cnt):
3859             # OFP_ACTION (OFP_ACTION_OUTPUT)
3860             port = a
3861             max_len = ofproto.OFP_ACTION_OUTPUT_SIZE
3862             action = OFPActionOutput(port, max_len)
3863             actions.append(action)
3864
3865         c = OFPBucket(weight, watch_port, watch_group, actions)
3866
3867         buf = bytearray()
3868         c.serialize(buf, 0)
3869
3870         fmt = ofproto.OFP_BUCKET_PACK_STR
3871         for a in range(action_cnt):
3872             fmt += ofproto.OFP_ACTION_OUTPUT_PACK_STR[1:]
3873         res = struct.unpack(fmt, six.binary_type(buf))
3874
3875         eq_(res[0], len_)
3876         eq_(res[1], weight)
3877         eq_(res[2], watch_port)
3878         eq_(res[3], watch_group)
3879
3880         for a in range(action_cnt):
3881             d = 4 + a * 4
3882             eq_(res[d], actions[a].type)
3883             eq_(res[d + 1], actions[a].len)
3884             eq_(res[d + 2], actions[a].port)
3885             eq_(res[d + 3], actions[a].max_len)
3886
3887     def test_serialize_mid(self):
3888         weight = 4386
3889         watch_port = 6606
3890         watch_group = 3
3891         action_cnt = 2047
3892         self._test_serialize(weight, watch_port,
3893                              watch_group, action_cnt)
3894
3895     def test_serialize_max(self):
3896         weight = 65535
3897         watch_port = 4294967295
3898         watch_group = 4294967295
3899         action_cnt = 4094
3900         self._test_serialize(weight, watch_port,
3901                              watch_group, action_cnt)
3902
3903     def test_serialize_min(self):
3904         weight = 0
3905         watch_port = 0
3906         watch_group = 0
3907         action_cnt = 0
3908         self._test_serialize(weight, watch_port,
3909                              watch_group, action_cnt)
3910
3911
3912 class TestOFPGroupMod(unittest.TestCase):
3913     """ Test case for ofproto_v1_2_parser.OFPGroupMod
3914     """
3915
3916     def test_init(self):
3917         # OFP_GROUP_MOD_PACK_STR
3918         # '!HBBI'...command, type, pad, group_id
3919         command = ofproto.OFPFC_ADD
3920         type_ = ofproto.OFPGT_SELECT
3921         group_id = 6606
3922
3923         # OFP_BUCKET
3924         weight = 4386
3925         watch_port = 8006
3926         watch_group = 3
3927
3928         # OFP_ACTION (OFP_ACTION_OUTPUT)
3929         port = 10
3930         max_len = 2000
3931         actions = [OFPActionOutput(port, max_len)]
3932
3933         buckets = [OFPBucket(weight, watch_port, watch_group, actions)]
3934
3935         c = OFPGroupMod(_Datapath, command, type_, group_id, buckets)
3936         eq_(command, c.command)
3937         eq_(type_, c.type)
3938         eq_(group_id, c.group_id)
3939         eq_(1, len(c.buckets))
3940         eq_(1, len(c.buckets[0].actions))
3941         eq_(port, c.buckets[0].actions[0].port)
3942         eq_(max_len, c.buckets[0].actions[0].max_len)
3943
3944     def _test_serialize(self, command, type_, group_id, bucket_cnt):
3945         len_ = ofproto.OFP_BUCKET_SIZE \
3946             + ofproto.OFP_ACTION_OUTPUT_SIZE
3947
3948         buckets = []
3949         for b in range(bucket_cnt):
3950             # OFP_BUCKET
3951             weight = watch_port = watch_group = port = b
3952             actions = [OFPActionOutput(port, 0)]
3953             bucket = OFPBucket(weight, watch_port, watch_group, actions)
3954             buckets.append(bucket)
3955
3956         c = OFPGroupMod(_Datapath, command, type_, group_id, buckets)
3957
3958         c.serialize()
3959
3960         eq_(ofproto.OFP_VERSION, c.version)
3961         eq_(ofproto.OFPT_GROUP_MOD, c.msg_type)
3962         eq_(0, c.xid)
3963         eq_(len(c.buf), c.msg_len)
3964
3965         # 16 byte
3966         fmt = ofproto.OFP_HEADER_PACK_STR \
3967             + ofproto.OFP_GROUP_MOD_PACK_STR[1:]
3968
3969         # 16 + (16 + 16) * bucket_cnt < 65535 byte
3970         # bucket_cnt <= 2047
3971         for b in range(bucket_cnt):
3972             fmt += ofproto.OFP_BUCKET_PACK_STR[1:] \
3973                 + ofproto.OFP_ACTION_OUTPUT_PACK_STR[1:]
3974
3975         res = struct.unpack(fmt, six.binary_type(c.buf))
3976
3977         msg_len = ofproto.OFP_GROUP_MOD_SIZE \
3978             + (len_ * bucket_cnt)
3979
3980         eq_(res[0], ofproto.OFP_VERSION)
3981         eq_(res[1], ofproto.OFPT_GROUP_MOD)
3982         eq_(res[2], msg_len)
3983         eq_(res[3], 0)
3984         eq_(res[4], command)
3985         eq_(res[5], type_)
3986         eq_(res[6], group_id)
3987
3988         for d in range(bucket_cnt):
3989             e = 7 + d * 8
3990             eq_(res[e + 1], buckets[d].weight)
3991             eq_(res[e + 2], buckets[d].watch_port)
3992             eq_(res[e + 3], buckets[d].watch_group)
3993             eq_(res[e + 4], buckets[d].actions[0].type)
3994             eq_(res[e + 5], buckets[d].actions[0].len)
3995             eq_(res[e + 6], buckets[d].actions[0].port)
3996             eq_(res[e + 7], buckets[d].actions[0].max_len)
3997
3998     def test_serialize_mid(self):
3999         command = 32768
4000         type_ = 128
4001         group_id = 6606
4002         bucket_cnt = 1023
4003         self._test_serialize(command, type_, group_id, bucket_cnt)
4004
4005     def test_serialize_max(self):
4006         command = 65535
4007         type_ = 255
4008         group_id = 4294967295
4009         bucket_cnt = 2047
4010         self._test_serialize(command, type_, group_id, bucket_cnt)
4011
4012     def test_serialize_min(self):
4013         command = 0
4014         type_ = 0
4015         group_id = 0
4016         bucket_cnt = 0
4017         self._test_serialize(command, type_, group_id, bucket_cnt)
4018
4019     def test_serialize_p1(self):
4020         command = 1
4021         type_ = 1
4022         group_id = 6606
4023         bucket_cnt = 1023
4024         self._test_serialize(command, type_, group_id, bucket_cnt)
4025
4026     def test_serialize_p2(self):
4027         command = 1
4028         type_ = 2
4029         group_id = 6606
4030         bucket_cnt = 1023
4031         self._test_serialize(command, type_, group_id, bucket_cnt)
4032
4033     def test_serialize_p3(self):
4034         command = 2
4035         type_ = 3
4036         group_id = 6606
4037         bucket_cnt = 1023
4038         self._test_serialize(command, type_, group_id, bucket_cnt)
4039
4040
4041 class TestOFPPortMod(unittest.TestCase):
4042     """ Test case for ofproto_v1_2_parser.OFPPortMod
4043     """
4044
4045     # OFP_PORT_MOD_PACK_STR v1.2
4046     # '!I4xs2xIII4x'...port_no, pad(4), hw_addr, pad(2),
4047     #                  config, mask, advertise, pad(4)
4048     port_no = 1119692796
4049     hw_addr = 'e8:fe:5e:a9:68:6c'
4050     config = 2226555987
4051     mask = 1678244809
4052     advertise = 2025421682
4053
4054     def test_init(self):
4055         c = OFPPortMod(_Datapath, self.port_no, self.hw_addr,
4056                        self.config, self.mask, self.advertise)
4057         eq_(self.port_no, c.port_no)
4058         eq_(self.hw_addr, c.hw_addr)
4059         eq_(self.config, c.config)
4060         eq_(self.mask, c.mask)
4061         eq_(self.advertise, c.advertise)
4062
4063     def _test_serialize(self, port_no, hw_addr, config, mask, advertise):
4064         c = OFPPortMod(_Datapath, port_no, hw_addr, config,
4065                        mask, advertise)
4066         c.serialize()
4067
4068         eq_(ofproto.OFP_VERSION, c.version)
4069         eq_(ofproto.OFPT_PORT_MOD, c.msg_type)
4070         eq_(0, c.xid)
4071
4072         fmt = '!' \
4073             + ofproto.OFP_HEADER_PACK_STR.replace('!', '') \
4074             + ofproto.OFP_PORT_MOD_PACK_STR.replace('!', '')
4075
4076         res = struct.unpack(fmt, six.binary_type(c.buf))
4077
4078         eq_(res[0], ofproto.OFP_VERSION)
4079         eq_(res[1], ofproto.OFPT_PORT_MOD)
4080         eq_(res[2], len(c.buf))
4081         eq_(res[3], 0)
4082         eq_(res[4], port_no)
4083         eq_(res[5], addrconv.mac.text_to_bin(hw_addr))
4084         eq_(res[6], config)
4085         eq_(res[7], mask)
4086         eq_(res[8], advertise)
4087
4088     def test_serialize_mid(self):
4089         self._test_serialize(self.port_no, self.hw_addr,
4090                              self.config, self.mask, self.advertise)
4091
4092     def test_serialize_max(self):
4093         port_no = ofproto.OFPP_ANY
4094         hw_addr = 'ff:ff:ff:ff:ff:ff'
4095         config = 0xffffffff
4096         mask = 0xffffffff
4097         advertise = 0xffffffff
4098         self._test_serialize(port_no, hw_addr, config, mask, advertise)
4099
4100     def test_serialize_min(self):
4101         port_no = 0
4102         hw_addr = '00:00:00:00:00:00'
4103         config = 0
4104         mask = 0
4105         advertise = 0
4106         self._test_serialize(port_no, hw_addr, config, mask, advertise)
4107
4108     def test_serialize_p1(self):
4109         port_no = ofproto.OFPP_MAX
4110         hw_addr = self.hw_addr
4111         config = ofproto.OFPPC_PORT_DOWN
4112         mask = ofproto.OFPPC_PORT_DOWN
4113         advertise = ofproto.OFPPF_10MB_HD
4114         self._test_serialize(port_no, hw_addr, config, mask, advertise)
4115
4116     def test_serialize_p2(self):
4117         port_no = ofproto.OFPP_IN_PORT
4118         hw_addr = self.hw_addr
4119         config = ofproto.OFPPC_NO_RECV
4120         mask = ofproto.OFPPC_NO_RECV
4121         advertise = ofproto.OFPPF_10MB_FD
4122         self._test_serialize(port_no, hw_addr, config, mask, advertise)
4123
4124     def test_serialize_p3(self):
4125         port_no = ofproto.OFPP_TABLE
4126         hw_addr = self.hw_addr
4127         config = ofproto.OFPPC_NO_FWD
4128         mask = ofproto.OFPPC_NO_FWD
4129         advertise = ofproto.OFPPF_100MB_HD
4130         self._test_serialize(port_no, hw_addr, config, mask, advertise)
4131
4132     def test_serialize_p4(self):
4133         port_no = ofproto.OFPP_NORMAL
4134         hw_addr = self.hw_addr
4135         config = ofproto.OFPPC_NO_PACKET_IN
4136         mask = ofproto.OFPPC_NO_PACKET_IN
4137         advertise = ofproto.OFPPF_100MB_FD
4138         self._test_serialize(port_no, hw_addr, config, mask, advertise)
4139
4140     def test_serialize_p5(self):
4141         port_no = ofproto.OFPP_FLOOD
4142         hw_addr = self.hw_addr
4143         config = ofproto.OFPPC_NO_PACKET_IN
4144         mask = ofproto.OFPPC_NO_PACKET_IN
4145         advertise = ofproto.OFPPF_1GB_HD
4146         self._test_serialize(port_no, hw_addr, config, mask, advertise)
4147
4148     def test_serialize_p6(self):
4149         port_no = ofproto.OFPP_ALL
4150         hw_addr = self.hw_addr
4151         config = ofproto.OFPPC_NO_PACKET_IN
4152         mask = ofproto.OFPPC_NO_PACKET_IN
4153         advertise = ofproto.OFPPF_1GB_FD
4154         self._test_serialize(port_no, hw_addr, config, mask, advertise)
4155
4156     def test_serialize_p7(self):
4157         port_no = ofproto.OFPP_CONTROLLER
4158         hw_addr = self.hw_addr
4159         config = ofproto.OFPPC_NO_PACKET_IN
4160         mask = ofproto.OFPPC_NO_PACKET_IN
4161         advertise = ofproto.OFPPF_10GB_FD
4162         self._test_serialize(port_no, hw_addr, config, mask, advertise)
4163
4164     def test_serialize_p8(self):
4165         port_no = ofproto.OFPP_LOCAL
4166         hw_addr = self.hw_addr
4167         config = ofproto.OFPPC_NO_PACKET_IN
4168         mask = ofproto.OFPPC_NO_PACKET_IN
4169         advertise = ofproto.OFPPF_40GB_FD
4170         self._test_serialize(port_no, hw_addr, config, mask, advertise)
4171
4172     def test_serialize_p9(self):
4173         port_no = ofproto.OFPP_LOCAL
4174         hw_addr = self.hw_addr
4175         config = ofproto.OFPPC_NO_PACKET_IN
4176         mask = ofproto.OFPPC_NO_PACKET_IN
4177         advertise = ofproto.OFPPF_100GB_FD
4178         self._test_serialize(port_no, hw_addr, config, mask, advertise)
4179
4180     def test_serialize_p10(self):
4181         port_no = ofproto.OFPP_LOCAL
4182         hw_addr = self.hw_addr
4183         config = ofproto.OFPPC_NO_PACKET_IN
4184         mask = ofproto.OFPPC_NO_PACKET_IN
4185         advertise = ofproto.OFPPF_1TB_FD
4186         self._test_serialize(port_no, hw_addr, config, mask, advertise)
4187
4188     def test_serialize_p11(self):
4189         port_no = ofproto.OFPP_LOCAL
4190         hw_addr = self.hw_addr
4191         config = ofproto.OFPPC_NO_PACKET_IN
4192         mask = ofproto.OFPPC_NO_PACKET_IN
4193         advertise = ofproto.OFPPF_OTHER
4194         self._test_serialize(port_no, hw_addr, config, mask, advertise)
4195
4196     def test_serialize_p12(self):
4197         port_no = ofproto.OFPP_LOCAL
4198         hw_addr = self.hw_addr
4199         config = ofproto.OFPPC_NO_PACKET_IN
4200         mask = ofproto.OFPPC_NO_PACKET_IN
4201         advertise = ofproto.OFPPF_COPPER
4202         self._test_serialize(port_no, hw_addr, config, mask, advertise)
4203
4204     def test_serialize_p13(self):
4205         port_no = ofproto.OFPP_LOCAL
4206         hw_addr = self.hw_addr
4207         config = ofproto.OFPPC_NO_PACKET_IN
4208         mask = ofproto.OFPPC_NO_PACKET_IN
4209         advertise = ofproto.OFPPF_FIBER
4210         self._test_serialize(port_no, hw_addr, config, mask, advertise)
4211
4212     def test_serialize_p14(self):
4213         port_no = ofproto.OFPP_LOCAL
4214         hw_addr = self.hw_addr
4215         config = ofproto.OFPPC_NO_PACKET_IN
4216         mask = ofproto.OFPPC_NO_PACKET_IN
4217         advertise = ofproto.OFPPF_AUTONEG
4218         self._test_serialize(port_no, hw_addr, config, mask, advertise)
4219
4220     def test_serialize_p15(self):
4221         port_no = ofproto.OFPP_LOCAL
4222         hw_addr = self.hw_addr
4223         config = ofproto.OFPPC_NO_PACKET_IN
4224         mask = ofproto.OFPPC_NO_PACKET_IN
4225         advertise = ofproto.OFPPF_PAUSE
4226         self._test_serialize(port_no, hw_addr, config, mask, advertise)
4227
4228     def test_serialize_p16(self):
4229         port_no = ofproto.OFPP_LOCAL
4230         hw_addr = self.hw_addr
4231         config = ofproto.OFPPC_NO_PACKET_IN
4232         mask = ofproto.OFPPC_NO_PACKET_IN
4233         advertise = ofproto.OFPPF_PAUSE_ASYM
4234         self._test_serialize(port_no, hw_addr, config, mask, advertise)
4235
4236
4237 class TestOFPTableMod(unittest.TestCase):
4238     """ Test case for ofproto_v1_2_parser.OFPTableMod
4239     """
4240
4241     # OFP_PORT_TABLE_PACK_STR v1.2
4242     # '!B3xI'...table_id, pad(3), config
4243     table_id = 3
4244     config = 2226555987
4245
4246     def test_init(self):
4247         c = OFPTableMod(_Datapath, self.table_id, self.config)
4248         eq_(self.table_id, c.table_id)
4249         eq_(self.config, c.config)
4250
4251     def _test_serialize(self, table_id, config):
4252         c = OFPTableMod(_Datapath, table_id, config)
4253         c.serialize()
4254
4255         eq_(ofproto.OFP_VERSION, c.version)
4256         eq_(ofproto.OFPT_TABLE_MOD, c.msg_type)
4257         eq_(0, c.xid)
4258
4259         fmt = '!' \
4260             + ofproto.OFP_HEADER_PACK_STR.replace('!', '') \
4261             + ofproto.OFP_TABLE_MOD_PACK_STR.replace('!', '')
4262
4263         res = struct.unpack(fmt, six.binary_type(c.buf))
4264
4265         eq_(res[0], ofproto.OFP_VERSION)
4266         eq_(res[1], ofproto.OFPT_TABLE_MOD)
4267         eq_(res[2], len(c.buf))
4268         eq_(res[3], 0)
4269         eq_(res[4], table_id)
4270         eq_(res[5], config)
4271
4272     def test_serialize_mid(self):
4273         self._test_serialize(self.table_id, self.config)
4274
4275     def test_serialize_max(self):
4276         table_id = ofproto.OFPTT_ALL
4277         config = 0xffffffff
4278         self._test_serialize(table_id, config)
4279
4280     def test_serialize_min(self):
4281         table_id = 0
4282         config = 0
4283         self._test_serialize(table_id, config)
4284
4285     def test_serialize_p1(self):
4286         table_id = ofproto.OFPTT_MAX
4287         config = ofproto.OFPTC_TABLE_MISS_CONTINUE
4288         self._test_serialize(table_id, config)
4289
4290     def test_serialize_p2(self):
4291         table_id = ofproto.OFPTT_MAX
4292         config = ofproto.OFPTC_TABLE_MISS_DROP
4293         self._test_serialize(table_id, config)
4294
4295     def test_serialize_p3(self):
4296         table_id = ofproto.OFPTT_MAX
4297         config = ofproto.OFPTC_TABLE_MISS_MASK
4298         self._test_serialize(table_id, config)
4299
4300
4301 class TestOFPStatsRequest(unittest.TestCase):
4302     """ Test case for ofproto_v1_2_parser.OFPStatsRequest
4303     """
4304
4305     type_ = ofproto.OFPST_DESC
4306     c = OFPStatsRequest(_Datapath, type_)
4307
4308     def test_init(self):
4309         eq_(self.type_, self.c.type)
4310         eq_(0, self.c.flags)
4311
4312     def test_serialize_body(self):
4313         len_ = ofproto.OFP_HEADER_SIZE \
4314             + ofproto.OFP_STATS_REQUEST_SIZE
4315         self.c.buf = bytearray(len_)
4316         self.c._serialize_body()
4317
4318         fmt = ofproto.OFP_STATS_REQUEST_PACK_STR
4319         res = struct.unpack_from(fmt, six.binary_type(self.c.buf),
4320                                  ofproto.OFP_HEADER_SIZE)
4321
4322         eq_(res[0], self.type_)
4323         eq_(res[1], 0)
4324
4325
4326 class TestOFPStatsReply(unittest.TestCase):
4327     """ Test case for ofproto_v1_2_parser.OFPStatsReply
4328     """
4329
4330     c = OFPStatsReply(_Datapath)
4331
4332     def test_parser_single_struct_true(self):
4333         # OFP_HEADER_PACK_STR
4334         # '!BBHI'...version, msg_type, msg_len, xid
4335         version = ofproto.OFP_VERSION
4336         msg_type = ofproto.OFPT_STATS_REPLY
4337         msg_len = ofproto.OFP_STATS_REPLY_SIZE \
4338             + ofproto.OFP_AGGREGATE_STATS_REPLY_SIZE
4339         xid = 2495926989
4340
4341         fmt = ofproto.OFP_HEADER_PACK_STR
4342         buf = pack(fmt, version, msg_type, msg_len, xid)
4343
4344         # OFP_STATS_REPLY_PACK_STR
4345         # '!HH4x'...type, flags, pad(4)
4346         type_ = ofproto.OFPST_AGGREGATE
4347         flags = 41802
4348
4349         fmt = ofproto.OFP_STATS_REPLY_PACK_STR
4350         buf += pack(fmt, type_, flags)
4351
4352         # OFP_AGGREGATE_STATS_REPLY_PACK_STR
4353         packet_count = 5142202600015232219
4354         byte_count = 2659740543924820419
4355         flow_count = 1344694860
4356         body = OFPAggregateStatsReply(packet_count, byte_count, flow_count)
4357
4358         fmt = ofproto.OFP_AGGREGATE_STATS_REPLY_PACK_STR
4359         buf += pack(fmt, packet_count, byte_count, flow_count)
4360
4361         res = self.c.parser(object, version, msg_type, msg_len, xid, buf)
4362
4363         eq_(version, res.version)
4364         eq_(msg_type, res.msg_type)
4365         eq_(msg_len, res.msg_len)
4366         eq_(xid, res.xid)
4367         eq_(type_, res.type)
4368         eq_(flags, res.flags)
4369         eq_(packet_count, res.body.packet_count)
4370         eq_(byte_count, res.body.byte_count)
4371         eq_(flow_count, res.body.flow_count)
4372
4373     def test_parser_single_struct_flase(self):
4374         # OFP_HEADER_PACK_STR
4375         # '!BBHI'...version, msg_type, msg_len, xid
4376         version = ofproto.OFP_VERSION
4377         msg_type = ofproto.OFPT_STATS_REPLY
4378         msg_len = ofproto.OFP_STATS_REPLY_SIZE \
4379             + ofproto.OFP_QUEUE_STATS_SIZE
4380         xid = 2495926989
4381
4382         fmt = ofproto.OFP_HEADER_PACK_STR
4383         buf = pack(fmt, version, msg_type, msg_len, xid)
4384
4385         # OFP_STATS_REPLY_PACK_STR
4386         # '!HH4x'...type, flags, pad(4)
4387         type_ = ofproto.OFPST_QUEUE
4388         flags = 11884
4389
4390         fmt = ofproto.OFP_STATS_REPLY_PACK_STR
4391         buf += pack(fmt, type_, flags)
4392
4393         # OFP_QUEUE_STATS_PACK_STR
4394         port_no = 41186
4395         queue_id = 6606
4396         tx_bytes = 8638420181865882538
4397         tx_packets = 2856480458895760962
4398         tx_errors = 6283093430376743019
4399         body = [OFPQueueStats(port_no, queue_id, tx_bytes, tx_packets,
4400                               tx_errors)]
4401
4402         fmt = ofproto.OFP_QUEUE_STATS_PACK_STR
4403         buf += pack(fmt, port_no, queue_id, tx_bytes, tx_packets, tx_errors)
4404
4405         res = self.c.parser(object, version, msg_type, msg_len, xid, buf)
4406
4407         eq_(version, res.version)
4408         eq_(msg_type, res.msg_type)
4409         eq_(msg_len, res.msg_len)
4410         eq_(xid, res.xid)
4411         eq_(type_, res.type)
4412         eq_(flags, res.flags)
4413         eq_(port_no, res.body[0].port_no)
4414         eq_(queue_id, res.body[0].queue_id)
4415         eq_(tx_bytes, res.body[0].tx_bytes)
4416         eq_(tx_packets, res.body[0].tx_packets)
4417         eq_(tx_errors, res.body[0].tx_errors)
4418
4419     def test_parser_max(self):
4420         # OFP_HEADER_PACK_STR
4421         # '!BBHI'...version, msg_type, msg_len, xid
4422         version = ofproto.OFP_VERSION
4423         msg_type = ofproto.OFPT_STATS_REPLY
4424         msg_len = ofproto.OFP_STATS_REPLY_SIZE
4425         xid = 0xffffffff
4426
4427         fmt = ofproto.OFP_HEADER_PACK_STR
4428         buf = pack(fmt, version, msg_type, msg_len, xid)
4429
4430         # OFP_STATS_REPLY_PACK_STR
4431         # '!HH4x'...type, flags, pad(4)
4432         type_ = ofproto.OFPST_QUEUE
4433         flags = 0xffff
4434
4435         fmt = ofproto.OFP_STATS_REPLY_PACK_STR
4436         buf += pack(fmt, type_, flags)
4437         res = self.c.parser(object, version, msg_type, msg_len, xid, buf)
4438
4439         eq_(version, res.version)
4440         eq_(msg_type, res.msg_type)
4441         eq_(msg_len, res.msg_len)
4442         eq_(xid, res.xid)
4443         eq_(type_, res.type)
4444         eq_(flags, res.flags)
4445
4446     def test_parser_min(self):
4447         # OFP_HEADER_PACK_STR
4448         # '!BBHI'...version, msg_type, msg_len, xid
4449         version = ofproto.OFP_VERSION
4450         msg_type = ofproto.OFPT_STATS_REPLY
4451         msg_len = ofproto.OFP_STATS_REPLY_SIZE
4452         xid = 0
4453
4454         fmt = ofproto.OFP_HEADER_PACK_STR
4455         buf = pack(fmt, version, msg_type, msg_len, xid)
4456
4457         # OFP_STATS_REPLY_PACK_STR
4458         # '!HH4x'...type, flags, pad(4)
4459         type_ = ofproto.OFPST_QUEUE
4460         flags = 0
4461
4462         fmt = ofproto.OFP_STATS_REPLY_PACK_STR
4463         buf += pack(fmt, type_, flags)
4464         res = self.c.parser(object, version, msg_type, msg_len, xid, buf)
4465
4466         eq_(version, res.version)
4467         eq_(msg_type, res.msg_type)
4468         eq_(msg_len, res.msg_len)
4469         eq_(xid, res.xid)
4470         eq_(type_, res.type)
4471         eq_(flags, res.flags)
4472
4473
4474 class TestOFPDescStatsRequest(unittest.TestCase):
4475     """ Test case for ofproto_v1_2_parser.OFPDescStatsRequest
4476     """
4477
4478     def test_serialize(self):
4479         c = OFPDescStatsRequest(_Datapath)
4480         c.serialize()
4481
4482         fmt = '!' \
4483             + ofproto.OFP_HEADER_PACK_STR.replace('!', '') \
4484             + ofproto.OFP_STATS_REQUEST_PACK_STR.replace('!', '')
4485
4486         res = struct.unpack(fmt, six.binary_type(c.buf))
4487
4488         eq_(res[0], ofproto.OFP_VERSION)
4489         eq_(res[1], ofproto.OFPT_STATS_REQUEST)
4490         eq_(res[2], len(c.buf))
4491         eq_(res[3], 0)
4492         eq_(res[4], ofproto.OFPST_DESC)
4493         eq_(res[5], 0)
4494
4495
4496 class TestOFPDescStats(unittest.TestCase):
4497     """ Test case for ofproto_v1_2_parser.OFPDescStats
4498     """
4499
4500     # OFP_DESC_STATS_PACK_STR
4501     # '!256s256s256s32s256s'...mfr_desc, hw_desc, sw_desc, serial_num, dp_desc
4502     mfr_desc = b'mfr_desc'.ljust(256)
4503     hw_desc = b'hw_desc'.ljust(256)
4504     sw_desc = b'sw_desc'.ljust(256)
4505     serial_num = b'serial_num'.ljust(32)
4506     dp_desc = b'dp_desc'.ljust(256)
4507
4508     buf = mfr_desc \
4509         + hw_desc \
4510         + sw_desc \
4511         + serial_num \
4512         + dp_desc
4513
4514     c = OFPDescStats(mfr_desc, hw_desc, sw_desc, serial_num, dp_desc)
4515
4516     def test_init(self):
4517         eq_(self.mfr_desc, self.c.mfr_desc)
4518         eq_(self.hw_desc, self.c.hw_desc)
4519         eq_(self.sw_desc, self.c.sw_desc)
4520         eq_(self.serial_num, self.c.serial_num)
4521         eq_(self.dp_desc, self.c.dp_desc)
4522
4523     def test_parser(self):
4524         res = self.c.parser(self.buf, 0)
4525
4526         eq_(self.mfr_desc, res.mfr_desc)
4527         eq_(self.hw_desc, res.hw_desc)
4528         eq_(self.sw_desc, res.sw_desc)
4529         eq_(self.serial_num, res.serial_num)
4530         eq_(self.dp_desc, res.dp_desc)
4531
4532
4533 class TestOFPFlowStatsRequest(unittest.TestCase):
4534     """ Test case for ofproto_v1_2_parser.OFPFlowStatsRequest
4535     """
4536
4537     # OFP_FLOW_STATS_REQUEST_PACK_STR
4538     # '!B3xII4xQQ'...table_id, pad(3), out_port, out_group, pad(4),
4539     #                cookie, cookie_mask
4540     table_id = 3
4541     out_port = 65037
4542     out_group = 6606
4543     cookie = 2127614848199081640
4544     cookie_mask = 2127614848199081641
4545
4546     def test_init(self):
4547         match = OFPMatch()
4548         in_port = 3
4549         match.set_in_port(in_port)
4550
4551         c = OFPFlowStatsRequest(_Datapath, self.table_id, self.out_port,
4552                                 self.out_group, self.cookie, self.cookie_mask,
4553                                 match)
4554
4555         eq_(self.table_id, c.table_id)
4556         eq_(self.out_port, c.out_port)
4557         eq_(self.out_group, c.out_group)
4558         eq_(self.cookie, c.cookie)
4559         eq_(self.cookie_mask, c.cookie_mask)
4560         eq_(in_port, c.match._flow.in_port)
4561
4562     def _test_serialize(self, table_id, out_port, out_group,
4563                         cookie, cookie_mask):
4564         match = OFPMatch()
4565         dl_type = 0x800
4566         match.set_dl_type(dl_type)
4567
4568         c = OFPFlowStatsRequest(_Datapath, table_id, out_port,
4569                                 out_group, cookie, cookie_mask, match)
4570         c.serialize()
4571
4572         eq_(ofproto.OFP_VERSION, c.version)
4573         eq_(ofproto.OFPT_STATS_REQUEST, c.msg_type)
4574         eq_(0, c.xid)
4575
4576         fmt = ofproto.OFP_HEADER_PACK_STR \
4577             + ofproto.OFP_STATS_REQUEST_PACK_STR[1:] \
4578             + ofproto.OFP_FLOW_STATS_REQUEST_PACK_STR[1:] \
4579             + 'HHHBB' \
4580             + MTEthType.pack_str[1:] + '6x'
4581
4582         res = struct.unpack(fmt, six.binary_type(c.buf))
4583
4584         eq_(res[0], ofproto.OFP_VERSION)
4585         eq_(res[1], ofproto.OFPT_STATS_REQUEST)
4586         size = ofproto.OFP_STATS_REPLY_SIZE \
4587             + ofproto.OFP_FLOW_STATS_REQUEST_SIZE \
4588             + calcsize(MTEthType.pack_str + '6x')
4589         eq_(res[2], size)
4590         eq_(res[3], 0)
4591         eq_(res[4], ofproto.OFPST_FLOW)
4592         eq_(res[5], 0)
4593         eq_(res[6], table_id)
4594         eq_(res[7], out_port)
4595         eq_(res[8], out_group)
4596         eq_(res[9], cookie)
4597         eq_(res[10], cookie_mask)
4598         # match
4599         eq_(res[11], ofproto.OFPMT_OXM)
4600         eq_(res[12], 10)
4601         eq_(res[13], ofproto.OFPXMC_OPENFLOW_BASIC)
4602         eq_(res[14] >> 1, ofproto.OFPXMT_OFB_ETH_TYPE)
4603         eq_(res[14] & 0b0001, 0)
4604         eq_(res[15], calcsize(MTEthType.pack_str))
4605         eq_(res[16], dl_type)
4606
4607     def test_serialize_mid(self):
4608         self._test_serialize(self.table_id, self.out_port, self.out_group,
4609                              self.cookie, self.cookie_mask)
4610
4611     def test_serialize_max(self):
4612         table_id = 0xff
4613         out_port = 0xffff
4614         out_group = 0xffff
4615         cookie = 0xffffffff
4616         cookie_mask = 0xffffffff
4617         self._test_serialize(table_id, out_port, out_group,
4618                              cookie, cookie_mask)
4619
4620     def test_serialize_min(self):
4621         table_id = 0
4622         out_port = 0
4623         out_group = 0
4624         cookie = 0
4625         cookie_mask = 0
4626         self._test_serialize(table_id, out_port, out_group,
4627                              cookie, cookie_mask)
4628
4629     def test_serialize_p1(self):
4630         table_id = ofproto.OFPTT_MAX
4631         self._test_serialize(table_id, self.out_port, self.out_group,
4632                              self.cookie, self.cookie_mask)
4633
4634
4635 class TestOFPFlowStats(unittest.TestCase):
4636     """ Test case for ofproto_v1_2_parser.OFPFlowStats
4637     """
4638
4639     def test_init(self):
4640         length = ofproto.OFP_FLOW_STATS_SIZE
4641         table_id = 81
4642         duration_sec = 2484712402
4643         duration_nsec = 3999715196
4644         priority = 57792
4645         idle_timeout = 36368
4646         hard_timeout = 54425
4647         cookie = 793171083674290912
4648         packet_count = 5142202600015232219
4649         byte_count = 2659740543924820419
4650
4651         match = OFPMatch()
4652         in_port = 2
4653         match.set_in_port(in_port)
4654
4655         goto_table = 3
4656         instructions = [OFPInstructionGotoTable(goto_table)]
4657         c = OFPFlowStats(table_id, duration_sec, duration_nsec,
4658                          priority, idle_timeout, hard_timeout, cookie,
4659                          packet_count, byte_count, match, instructions)
4660
4661         eq_(table_id, c.table_id)
4662         eq_(duration_sec, c.duration_sec)
4663         eq_(duration_nsec, c.duration_nsec)
4664         eq_(priority, c.priority)
4665         eq_(idle_timeout, c.idle_timeout)
4666         eq_(hard_timeout, c.hard_timeout)
4667         eq_(cookie, c.cookie)
4668         eq_(packet_count, c.packet_count)
4669         eq_(byte_count, c.byte_count)
4670         eq_(in_port, c.match._flow.in_port)
4671         eq_(goto_table, c.instructions[0].table_id)
4672
4673     def _test_parser(self, table_id, duration_sec, duration_nsec,
4674                      priority, idle_timeout, hard_timeout, cookie,
4675                      packet_count, byte_count, inst_cnt=0):
4676
4677         length = ofproto.OFP_FLOW_STATS_SIZE \
4678             + calcsize(MTEthType.pack_str[1:] + '6x') \
4679             + ofproto.OFP_INSTRUCTION_GOTO_TABLE_SIZE * inst_cnt
4680
4681         # OFP_FLOW_STATS_PACK_STR
4682         buf = pack(ofproto.OFP_FLOW_STATS_PACK_STR,
4683                    length, table_id, duration_sec, duration_nsec,
4684                    priority, idle_timeout, hard_timeout, cookie,
4685                    packet_count, byte_count)
4686
4687         # match
4688         match = OFPMatch()
4689         dl_type = 0x0800
4690         match.set_dl_type(dl_type)
4691         match_buf = bytearray()
4692         match.serialize(match_buf, 0)
4693         buf += six.binary_type(match_buf)
4694
4695         # instructions
4696         # 56 + 8 + 8 * inst_cnt <= 65535
4697         # inst_cnt <= 8183
4698         for i in range(inst_cnt):
4699             inst = OFPInstructionGotoTable(1)
4700             inst_buf = bytearray()
4701             inst.serialize(inst_buf, 0)
4702             buf += six.binary_type(inst_buf)
4703
4704         # parse
4705         res = OFPFlowStats.parser(buf, 0)
4706         eq_(length, res.length)
4707         eq_(table_id, res.table_id)
4708         eq_(duration_sec, res.duration_sec)
4709         eq_(duration_nsec, res.duration_nsec)
4710         eq_(priority, res.priority)
4711         eq_(idle_timeout, res.idle_timeout)
4712         eq_(hard_timeout, res.hard_timeout)
4713         eq_(cookie, res.cookie)
4714         eq_(packet_count, res.packet_count)
4715         eq_(byte_count, res.byte_count)
4716         eq_(dl_type, res.match.fields[0].value)
4717         for i in range(inst_cnt):
4718             eq_(1, res.instructions[i].table_id)
4719
4720     def test_parser_mid(self):
4721         table_id = 81
4722         duration_sec = 2484712402
4723         duration_nsec = 3999715196
4724         priority = 57792
4725         idle_timeout = 36368
4726         hard_timeout = 54425
4727         cookie = 793171083674290912
4728         packet_count = 5142202600015232219
4729         byte_count = 2659740543924820419
4730         inst_cnt = 2
4731
4732         self._test_parser(table_id, duration_sec, duration_nsec,
4733                           priority, idle_timeout, hard_timeout, cookie,
4734                           packet_count, byte_count, inst_cnt)
4735
4736     def test_parser_max(self):
4737         table_id = 0xff
4738         duration_sec = 0xffff
4739         duration_nsec = 0xffff
4740         priority = 0xffff
4741         idle_timeout = 0xff
4742         hard_timeout = 0xff
4743         cookie = 0xffffffffffffffff
4744         packet_count = 0xffffffffffffffff
4745         byte_count = 0xffffffffffffffff
4746         inst_cnt = 8183
4747
4748         self._test_parser(table_id, duration_sec, duration_nsec,
4749                           priority, idle_timeout, hard_timeout, cookie,
4750                           packet_count, byte_count, inst_cnt)
4751
4752     def test_parser_min(self):
4753         self._test_parser(0, 0, 0, 0, 0, 0, 0, 0, 0)
4754
4755
4756 class TestOFPAggregateStatsRequest(unittest.TestCase):
4757     """ Test case for ofproto_v1_2_parser.OFPAggregateStatsRequest
4758     """
4759
4760     # OFP_AGGREGATE_STATS_REQUEST_PACK_STR
4761     # '!B3xII4xQQ'...table_id, pad(3), out_port, out_group, pad(4),
4762     #                cookie, cookie_mask
4763     table_id = 3
4764     out_port = 65037
4765     out_group = 6606
4766     cookie = 2127614848199081640
4767     cookie_mask = 2127614848199081641
4768
4769     def test_init(self):
4770         match = OFPMatch()
4771         dl_type = 0x800
4772         match.set_dl_type(dl_type)
4773         c = OFPAggregateStatsRequest(_Datapath, self.table_id,
4774                                      self.out_port, self.out_group,
4775                                      self.cookie, self.cookie_mask,
4776                                      match)
4777
4778         eq_(self.table_id, c.table_id)
4779         eq_(self.out_port, c.out_port)
4780         eq_(self.out_group, c.out_group)
4781         eq_(self.cookie, c.cookie)
4782         eq_(self.cookie_mask, c.cookie_mask)
4783         eq_(dl_type, c.match._flow.dl_type)
4784
4785     def _test_serialize(self, table_id, out_port, out_group,
4786                         cookie, cookie_mask):
4787         match = OFPMatch()
4788         dl_type = 0x800
4789         match.set_dl_type(dl_type)
4790         c = OFPAggregateStatsRequest(_Datapath, table_id,
4791                                      out_port, out_group, cookie,
4792                                      cookie_mask, match)
4793         c.serialize()
4794
4795         eq_(ofproto.OFP_VERSION, c.version)
4796         eq_(ofproto.OFPT_STATS_REQUEST, c.msg_type)
4797         eq_(0, c.xid)
4798
4799         fmt = ofproto.OFP_HEADER_PACK_STR \
4800             + ofproto.OFP_STATS_REQUEST_PACK_STR[1:] \
4801             + ofproto.OFP_AGGREGATE_STATS_REQUEST_PACK_STR[1:] \
4802             + 'HHHBB' \
4803             + MTEthType.pack_str[1:] + '6x'
4804
4805         res = struct.unpack(fmt, six.binary_type(c.buf))
4806         eq_(res[0], ofproto.OFP_VERSION)
4807         eq_(res[1], ofproto.OFPT_STATS_REQUEST)
4808         eq_(res[2], len(c.buf))
4809         eq_(res[3], 0)
4810         eq_(res[4], ofproto.OFPST_AGGREGATE)
4811         eq_(res[5], 0)
4812         eq_(res[6], table_id)
4813         eq_(res[7], out_port)
4814         eq_(res[8], out_group)
4815         eq_(res[9], cookie)
4816         eq_(res[10], cookie_mask)
4817         # match
4818         eq_(res[11], ofproto.OFPMT_OXM)
4819         eq_(res[12], 10)
4820         eq_(res[13], ofproto.OFPXMC_OPENFLOW_BASIC)
4821         eq_(res[14] >> 1, ofproto.OFPXMT_OFB_ETH_TYPE)
4822         eq_(res[14] & 0b0001, 0)
4823         eq_(res[15], calcsize(MTEthType.pack_str))
4824         eq_(res[16], dl_type)
4825
4826     def test_serialize_mid(self):
4827         self._test_serialize(self.table_id, self.out_port, self.out_group,
4828                              self.cookie, self.cookie_mask)
4829
4830     def test_serialize_max(self):
4831         table_id = 0xff
4832         out_port = 0xffffffff
4833         out_group = 0xffffffff
4834         cookie = 0xffffffff
4835         cookie_mask = 0xffffffff
4836         self._test_serialize(table_id, out_port, out_group,
4837                              cookie, cookie_mask)
4838
4839     def test_serialize_min(self):
4840         table_id = 0
4841         out_port = 0
4842         out_group = 0
4843         cookie = 0
4844         cookie_mask = 0
4845         self._test_serialize(table_id, out_port, out_group,
4846                              cookie, cookie_mask)
4847
4848     def test_serialize_p1(self):
4849         table_id = ofproto.OFPTT_MAX
4850         self._test_serialize(table_id, self.out_port, self.out_group,
4851                              self.cookie, self.cookie_mask)
4852
4853
4854 class TestOFPAggregateStatsReply(unittest.TestCase):
4855     """ Test case for ofproto_v1_2_parser.OFPAggregateStatsReply
4856     """
4857
4858     # OFP_AGGREGATE_STATS_REPLY_PACK_STR
4859     # '!QQI4x'...packet_count, byte_count, flow_count, pad(4)
4860     packet_count = 5142202600015232219
4861     byte_count = 2659740543924820419
4862     flow_count = 1344694860
4863
4864     def test_init(self):
4865         c = OFPAggregateStatsReply(self.packet_count, self.byte_count,
4866                                    self.flow_count)
4867
4868         eq_(c.packet_count, self.packet_count)
4869         eq_(c.byte_count, self.byte_count)
4870         eq_(c.flow_count, self.flow_count)
4871
4872     def _test_parser(self, packet_count, byte_count, flow_count):
4873         fmt = ofproto.OFP_AGGREGATE_STATS_REPLY_PACK_STR
4874         buf = pack(fmt, packet_count, byte_count, flow_count)
4875
4876         res = OFPAggregateStatsReply.parser(buf, 0)
4877         eq_(packet_count, res.packet_count)
4878         eq_(byte_count, res.byte_count)
4879         eq_(flow_count, res.flow_count)
4880
4881     def test_parser_mid(self):
4882         self._test_parser(self.packet_count, self.byte_count,
4883                           self.flow_count)
4884
4885     def test_parser_max(self):
4886         packet_count = 18446744073709551615
4887         byte_count = 18446744073709551615
4888         flow_count = 4294967295
4889         self._test_parser(packet_count, byte_count,
4890                           flow_count)
4891
4892     def test_parser_min(self):
4893         packet_count = 0
4894         byte_count = 0
4895         flow_count = 0
4896         self._test_parser(packet_count, byte_count,
4897                           flow_count)
4898
4899
4900 class TestOFPTableStatsRequest(unittest.TestCase):
4901     """ Test case for ofproto_v1_2_parser.OFPTableStatsRequest
4902     """
4903
4904     def test_serialize(self):
4905         c = OFPTableStatsRequest(_Datapath)
4906         c.serialize()
4907
4908         fmt = '!' \
4909             + ofproto.OFP_HEADER_PACK_STR.replace('!', '') \
4910             + ofproto.OFP_STATS_REQUEST_PACK_STR.replace('!', '')
4911
4912         res = struct.unpack(fmt, six.binary_type(c.buf))
4913
4914         eq_(res[0], ofproto.OFP_VERSION)
4915         eq_(res[1], ofproto.OFPT_STATS_REQUEST)
4916         eq_(res[2], len(c.buf))
4917         eq_(res[3], 0)
4918         eq_(res[4], ofproto.OFPST_TABLE)
4919         eq_(res[5], 0)
4920
4921
4922 class TestOFPTableStats(unittest.TestCase):
4923     """ Test case for ofproto_v1_2_parser.OFPTableStats
4924     """
4925
4926     def test_init(self):
4927         table_id = 91
4928         name = 'name'
4929         match = 1270985291017894273
4930         wildcards = 3316608530
4931         write_actions = 2484712402
4932         apply_actions = 3999715196
4933         write_setfields = 5142202600015232219
4934         apply_setfields = 2659740543924820419
4935         metadata_match = 2127614848199081640
4936         metadata_write = 2127614848199081641
4937         instructions = 1119692796
4938         config = 2226555987
4939         max_entries = 2506913869
4940         active_count = 2024581150
4941         lookup_count = 4620020561814017052
4942         matched_count = 2825167325263435621
4943
4944         res = OFPTableStats(table_id, name, match, wildcards, write_actions,
4945                             apply_actions, write_setfields, apply_setfields,
4946                             metadata_match, metadata_write, instructions,
4947                             config, max_entries, active_count, lookup_count,
4948                             matched_count)
4949
4950         eq_(table_id, res.table_id)
4951         eq_(name, res.name)
4952         eq_(match, res.match)
4953         eq_(wildcards, res.wildcards)
4954         eq_(write_actions, res.write_actions)
4955         eq_(apply_actions, res.apply_actions)
4956         eq_(write_setfields, res.write_setfields)
4957         eq_(apply_setfields, res.apply_setfields)
4958         eq_(metadata_match, res.metadata_match)
4959         eq_(metadata_write, res.metadata_write)
4960         eq_(instructions, res.instructions)
4961         eq_(config, res.config)
4962         eq_(max_entries, res.max_entries)
4963         eq_(active_count, res.active_count)
4964         eq_(lookup_count, res.lookup_count)
4965         eq_(matched_count, res.matched_count)
4966
4967     def _test_parser(self, table_id, name, match, wildcards, write_actions,
4968                      apply_actions, write_setfields, apply_setfields,
4969                      metadata_match, metadata_write, instructions, config,
4970                      max_entries, active_count, lookup_count, matched_count):
4971         # OFP_TABLE_STATS_PACK_STR
4972         # '!B7x32sQQIIQQQQIIIIQQ'
4973         # ...table_id, name, match, wildcards, write_actions, apply_actions,
4974         #    write_setfields, apply_setfields', metadata_match, metadata_write,
4975         #    instructions, config, max_entries,
4976         #    active_count, lookup_count, matched_count
4977         fmt = ofproto.OFP_TABLE_STATS_PACK_STR
4978         buf = pack(fmt, table_id, name,
4979                    match, wildcards, write_actions,
4980                    apply_actions, write_setfields, apply_setfields,
4981                    metadata_match, metadata_write, instructions, config,
4982                    max_entries, active_count, lookup_count, matched_count)
4983
4984         res = OFPTableStats.parser(buf, 0)
4985
4986         eq_(table_id, res.table_id)
4987         eq_(name, res.name.replace(b'\x00', b''))
4988         eq_(match, res.match)
4989         eq_(wildcards, res.wildcards)
4990         eq_(write_actions, res.write_actions)
4991         eq_(apply_actions, res.apply_actions)
4992         eq_(write_setfields, res.write_setfields)
4993         eq_(apply_setfields, res.apply_setfields)
4994         eq_(metadata_match, res.metadata_match)
4995         eq_(metadata_write, res.metadata_write)
4996         eq_(instructions, res.instructions)
4997         eq_(config, res.config)
4998         eq_(max_entries, res.max_entries)
4999         eq_(active_count, res.active_count)
5000         eq_(lookup_count, res.lookup_count)
5001         eq_(matched_count, res.matched_count)
5002
5003     def test_parser_mid(self):
5004         table_id = 91
5005         name = b'name'
5006         match = 1270985291017894273
5007         wildcards = 3316608530
5008         write_actions = 2484712402
5009         apply_actions = 3999715196
5010         write_setfields = 5142202600015232219
5011         apply_setfields = 2659740543924820419
5012         metadata_match = 2127614848199081640
5013         metadata_write = 2127614848199081641
5014         instructions = 1119692796
5015         config = 2226555987
5016         max_entries = 2506913869
5017         active_count = 2024581150
5018         lookup_count = 4620020561814017052
5019         matched_count = 2825167325263435621
5020
5021         self._test_parser(table_id, name, match, wildcards, write_actions,
5022                           apply_actions, write_setfields, apply_setfields,
5023                           metadata_match, metadata_write, instructions, config,
5024                           max_entries, active_count, lookup_count,
5025                           matched_count)
5026
5027     def test_parser_max(self):
5028         # '!B7x32sQQIIQQQQIIIIQQ'
5029         table_id = 0xff
5030         name = b'a' * 32
5031         match = 0xffffffffffffffff
5032         wildcards = 0xffffffffffffffff
5033         write_actions = 0xffffffff
5034         apply_actions = 0xffffffff
5035         write_setfields = 0xffffffffffffffff
5036         apply_setfields = 0xffffffffffffffff
5037         metadata_match = 0xffffffffffffffff
5038         metadata_write = 0xffffffffffffffff
5039         instructions = 0xffffffff
5040         config = 0xffffffff
5041         max_entries = 0xffffffff
5042         active_count = 0xffffffff
5043         lookup_count = 0xffffffffffffffff
5044         matched_count = 0xffffffffffffffff
5045
5046         self._test_parser(table_id, name, match, wildcards, write_actions,
5047                           apply_actions, write_setfields, apply_setfields,
5048                           metadata_match, metadata_write, instructions, config,
5049                           max_entries, active_count, lookup_count,
5050                           matched_count)
5051
5052     def test_parser_min(self):
5053         table_id = 0
5054         name = b''
5055         match = 0
5056         wildcards = 0
5057         write_actions = 0
5058         apply_actions = 0
5059         write_setfields = 0
5060         apply_setfields = 0
5061         metadata_match = 0
5062         metadata_write = 0
5063         instructions = 0
5064         config = 0
5065         max_entries = 0
5066         active_count = 0
5067         lookup_count = 0
5068         matched_count = 0
5069
5070         self._test_parser(table_id, name, match, wildcards, write_actions,
5071                           apply_actions, write_setfields, apply_setfields,
5072                           metadata_match, metadata_write, instructions, config,
5073                           max_entries, active_count, lookup_count,
5074                           matched_count)
5075
5076     def _test_parser_p(self, ofpxmt, ofpit, ofptc):
5077         table_id = 91
5078         name = b'name'
5079         match = ofpxmt
5080         wildcards = ofpxmt
5081         write_actions = 2484712402
5082         apply_actions = 3999715196
5083         write_setfields = ofpxmt
5084         apply_setfields = ofpxmt
5085         metadata_match = 2127614848199081640
5086         metadata_write = 2127614848199081641
5087         instructions = ofpit
5088         config = ofptc
5089         max_entries = 2506913869
5090         active_count = 2024581150
5091         lookup_count = 4620020561814017052
5092         matched_count = 2825167325263435621
5093
5094         self._test_parser(table_id, name, match, wildcards, write_actions,
5095                           apply_actions, write_setfields, apply_setfields,
5096                           metadata_match, metadata_write, instructions, config,
5097                           max_entries, active_count, lookup_count,
5098                           matched_count)
5099
5100     def test_parser_p1(self):
5101         self._test_parser_p(ofproto.OFPXMT_OFB_IN_PORT,
5102                             ofproto.OFPIT_GOTO_TABLE,
5103                             ofproto.OFPTC_TABLE_MISS_CONTINUE)
5104
5105     def test_parser_p2(self):
5106         self._test_parser_p(ofproto.OFPXMT_OFB_IN_PHY_PORT,
5107                             ofproto.OFPIT_WRITE_METADATA,
5108                             ofproto.OFPTC_TABLE_MISS_DROP)
5109
5110     def test_parser_p3(self):
5111         self._test_parser_p(ofproto.OFPXMT_OFB_METADATA,
5112                             ofproto.OFPIT_WRITE_ACTIONS,
5113                             ofproto.OFPTC_TABLE_MISS_MASK)
5114
5115     def test_parser_p4(self):
5116         self._test_parser_p(ofproto.OFPXMT_OFB_ETH_DST,
5117                             ofproto.OFPIT_APPLY_ACTIONS,
5118                             ofproto.OFPTC_TABLE_MISS_MASK)
5119
5120     def test_parser_p5(self):
5121         self._test_parser_p(ofproto.OFPXMT_OFB_ETH_SRC,
5122                             ofproto.OFPIT_CLEAR_ACTIONS,
5123                             ofproto.OFPTC_TABLE_MISS_MASK)
5124
5125     def test_parser_p6(self):
5126         self._test_parser_p(ofproto.OFPXMT_OFB_ETH_TYPE,
5127                             ofproto.OFPIT_EXPERIMENTER,
5128                             ofproto.OFPTC_TABLE_MISS_MASK)
5129
5130     def test_parser_p7(self):
5131         self._test_parser_p(ofproto.OFPXMT_OFB_VLAN_VID,
5132                             ofproto.OFPIT_EXPERIMENTER,
5133                             ofproto.OFPTC_TABLE_MISS_MASK)
5134
5135     def test_parser_p8(self):
5136         self._test_parser_p(ofproto.OFPXMT_OFB_VLAN_PCP,
5137                             ofproto.OFPIT_EXPERIMENTER,
5138                             ofproto.OFPTC_TABLE_MISS_MASK)
5139
5140     def test_parser_p9(self):
5141         self._test_parser_p(ofproto.OFPXMT_OFB_IP_DSCP,
5142                             ofproto.OFPIT_EXPERIMENTER,
5143                             ofproto.OFPTC_TABLE_MISS_MASK)
5144
5145     def test_parser_p10(self):
5146         self._test_parser_p(ofproto.OFPXMT_OFB_IP_ECN,
5147                             ofproto.OFPIT_EXPERIMENTER,
5148                             ofproto.OFPTC_TABLE_MISS_MASK)
5149
5150     def test_parser_p11(self):
5151         self._test_parser_p(ofproto.OFPXMT_OFB_IP_PROTO,
5152                             ofproto.OFPIT_EXPERIMENTER,
5153                             ofproto.OFPTC_TABLE_MISS_MASK)
5154
5155     def test_parser_p12(self):
5156         self._test_parser_p(ofproto.OFPXMT_OFB_IPV4_SRC,
5157                             ofproto.OFPIT_EXPERIMENTER,
5158                             ofproto.OFPTC_TABLE_MISS_MASK)
5159
5160     def test_parser_p13(self):
5161         self._test_parser_p(ofproto.OFPXMT_OFB_IPV4_DST,
5162                             ofproto.OFPIT_EXPERIMENTER,
5163                             ofproto.OFPTC_TABLE_MISS_MASK)
5164
5165     def test_parser_p14(self):
5166         self._test_parser_p(ofproto.OFPXMT_OFB_TCP_SRC,
5167                             ofproto.OFPIT_EXPERIMENTER,
5168                             ofproto.OFPTC_TABLE_MISS_MASK)
5169
5170     def test_parser_p15(self):
5171         self._test_parser_p(ofproto.OFPXMT_OFB_TCP_DST,
5172                             ofproto.OFPIT_EXPERIMENTER,
5173                             ofproto.OFPTC_TABLE_MISS_MASK)
5174
5175     def test_parser_p16(self):
5176         self._test_parser_p(ofproto.OFPXMT_OFB_UDP_SRC,
5177                             ofproto.OFPIT_EXPERIMENTER,
5178                             ofproto.OFPTC_TABLE_MISS_MASK)
5179
5180     def test_parser_p17(self):
5181         self._test_parser_p(ofproto.OFPXMT_OFB_UDP_DST,
5182                             ofproto.OFPIT_EXPERIMENTER,
5183                             ofproto.OFPTC_TABLE_MISS_MASK)
5184
5185     def test_parser_p18(self):
5186         self._test_parser_p(ofproto.OFPXMT_OFB_SCTP_SRC,
5187                             ofproto.OFPIT_EXPERIMENTER,
5188                             ofproto.OFPTC_TABLE_MISS_MASK)
5189
5190     def test_parser_p19(self):
5191         self._test_parser_p(ofproto.OFPXMT_OFB_SCTP_DST,
5192                             ofproto.OFPIT_EXPERIMENTER,
5193                             ofproto.OFPTC_TABLE_MISS_MASK)
5194
5195     def test_parser_p20(self):
5196         self._test_parser_p(ofproto.OFPXMT_OFB_ICMPV4_TYPE,
5197                             ofproto.OFPIT_EXPERIMENTER,
5198                             ofproto.OFPTC_TABLE_MISS_MASK)
5199
5200     def test_parser_p21(self):
5201         self._test_parser_p(ofproto.OFPXMT_OFB_ICMPV4_CODE,
5202                             ofproto.OFPIT_EXPERIMENTER,
5203                             ofproto.OFPTC_TABLE_MISS_MASK)
5204
5205     def test_parser_p22(self):
5206         self._test_parser_p(ofproto.OFPXMT_OFB_ARP_OP,
5207                             ofproto.OFPIT_EXPERIMENTER,
5208                             ofproto.OFPTC_TABLE_MISS_MASK)
5209
5210     def test_parser_p23(self):
5211         self._test_parser_p(ofproto.OFPXMT_OFB_ARP_SPA,
5212                             ofproto.OFPIT_EXPERIMENTER,
5213                             ofproto.OFPTC_TABLE_MISS_MASK)
5214
5215     def test_parser_p24(self):
5216         self._test_parser_p(ofproto.OFPXMT_OFB_ARP_TPA,
5217                             ofproto.OFPIT_EXPERIMENTER,
5218                             ofproto.OFPTC_TABLE_MISS_MASK)
5219
5220     def test_parser_p25(self):
5221         self._test_parser_p(ofproto.OFPXMT_OFB_ARP_SHA,
5222                             ofproto.OFPIT_EXPERIMENTER,
5223                             ofproto.OFPTC_TABLE_MISS_MASK)
5224
5225     def test_parser_p26(self):
5226         self._test_parser_p(ofproto.OFPXMT_OFB_ARP_THA,
5227                             ofproto.OFPIT_EXPERIMENTER,
5228                             ofproto.OFPTC_TABLE_MISS_MASK)
5229
5230     def test_parser_p27(self):
5231         self._test_parser_p(ofproto.OFPXMT_OFB_IPV6_SRC,
5232                             ofproto.OFPIT_EXPERIMENTER,
5233                             ofproto.OFPTC_TABLE_MISS_MASK)
5234
5235     def test_parser_p28(self):
5236         self._test_parser_p(ofproto.OFPXMT_OFB_IPV6_DST,
5237                             ofproto.OFPIT_EXPERIMENTER,
5238                             ofproto.OFPTC_TABLE_MISS_MASK)
5239
5240     def test_parser_p29(self):
5241         self._test_parser_p(ofproto.OFPXMT_OFB_IPV6_FLABEL,
5242                             ofproto.OFPIT_EXPERIMENTER,
5243                             ofproto.OFPTC_TABLE_MISS_MASK)
5244
5245     def test_parser_p30(self):
5246         self._test_parser_p(ofproto.OFPXMT_OFB_ICMPV6_TYPE,
5247                             ofproto.OFPIT_EXPERIMENTER,
5248                             ofproto.OFPTC_TABLE_MISS_MASK)
5249
5250     def test_parser_p31(self):
5251         self._test_parser_p(ofproto.OFPXMT_OFB_ICMPV6_CODE,
5252                             ofproto.OFPIT_EXPERIMENTER,
5253                             ofproto.OFPTC_TABLE_MISS_MASK)
5254
5255     def test_parser_p32(self):
5256         self._test_parser_p(ofproto.OFPXMT_OFB_IPV6_ND_TARGET,
5257                             ofproto.OFPIT_EXPERIMENTER,
5258                             ofproto.OFPTC_TABLE_MISS_MASK)
5259
5260     def test_parser_p33(self):
5261         self._test_parser_p(ofproto.OFPXMT_OFB_IPV6_ND_SLL,
5262                             ofproto.OFPIT_EXPERIMENTER,
5263                             ofproto.OFPTC_TABLE_MISS_MASK)
5264
5265     def test_parser_p34(self):
5266         self._test_parser_p(ofproto.OFPXMT_OFB_IPV6_ND_TLL,
5267                             ofproto.OFPIT_EXPERIMENTER,
5268                             ofproto.OFPTC_TABLE_MISS_MASK)
5269
5270     def test_parser_p35(self):
5271         self._test_parser_p(ofproto.OFPXMT_OFB_MPLS_LABEL,
5272                             ofproto.OFPIT_EXPERIMENTER,
5273                             ofproto.OFPTC_TABLE_MISS_MASK)
5274
5275     def test_parser_p36(self):
5276         self._test_parser_p(ofproto.OFPXMT_OFB_MPLS_TC,
5277                             ofproto.OFPIT_EXPERIMENTER,
5278                             ofproto.OFPTC_TABLE_MISS_MASK)
5279
5280
5281 class TestOFPPortStatsRequest(unittest.TestCase):
5282     """ Test case for ofproto_v1_2_parser.OFPPortStatsRequest
5283     """
5284
5285     # OFP_PORT_STATS_REQUEST_PACK_STR
5286     # '!I4x'...port_no, pad(4)
5287     port_no = 41186
5288
5289     def test_init(self):
5290         c = OFPPortStatsRequest(_Datapath, self.port_no)
5291         eq_(self.port_no, c.port_no)
5292
5293     def _test_serialize(self, port_no):
5294         c = OFPPortStatsRequest(_Datapath, port_no)
5295         c.serialize()
5296
5297         eq_(ofproto.OFP_VERSION, c.version)
5298         eq_(ofproto.OFPT_STATS_REQUEST, c.msg_type)
5299         eq_(0, c.xid)
5300
5301         fmt = '!' \
5302             + ofproto.OFP_HEADER_PACK_STR.replace('!', '') \
5303             + ofproto.OFP_STATS_REQUEST_PACK_STR.replace('!', '') \
5304             + ofproto.OFP_PORT_STATS_REQUEST_PACK_STR.replace('!', '')
5305         res = struct.unpack(fmt, six.binary_type(c.buf))
5306
5307         eq_(res[0], ofproto.OFP_VERSION)
5308         eq_(res[1], ofproto.OFPT_STATS_REQUEST)
5309         eq_(res[2], len(c.buf))
5310         eq_(res[3], 0)
5311         eq_(res[4], ofproto.OFPST_PORT)
5312         eq_(res[5], 0)
5313         eq_(res[6], port_no)
5314
5315     def test_serialize_mid(self):
5316         self._test_serialize(self.port_no)
5317
5318     def test_serialize_max(self):
5319         self._test_serialize(ofproto.OFPP_ANY)
5320
5321     def test_serialize_min(self):
5322         self._test_serialize(0)
5323
5324     def test_serialize_p1(self):
5325         self._test_serialize(ofproto.OFPP_MAX)
5326
5327     def test_serialize_p2(self):
5328         self._test_serialize(ofproto.OFPP_IN_PORT)
5329
5330     def test_serialize_p3(self):
5331         self._test_serialize(ofproto.OFPP_TABLE)
5332
5333     def test_serialize_p4(self):
5334         self._test_serialize(ofproto.OFPP_NORMAL)
5335
5336     def test_serialize_p5(self):
5337         self._test_serialize(ofproto.OFPP_FLOOD)
5338
5339     def test_serialize_p6(self):
5340         self._test_serialize(ofproto.OFPP_ALL)
5341
5342     def test_serialize_p7(self):
5343         self._test_serialize(ofproto.OFPP_CONTROLLER)
5344
5345     def test_serialize_p8(self):
5346         self._test_serialize(ofproto.OFPP_LOCAL)
5347
5348
5349 class TestOFPPortStats(unittest.TestCase):
5350     """ Test case for ofproto_v1_2_parser.OFPPortStats
5351     """
5352
5353     def test_init(self):
5354         port_no = 6606
5355         rx_packets = 5999980397101236279
5356         tx_packets = 2856480458895760962
5357         rx_bytes = 6170274950576278921
5358         tx_bytes = 8638420181865882538
5359         rx_dropped = 6982303461569875546
5360         tx_dropped = 661287462113808071
5361         rx_errors = 3422231811478788365
5362         tx_errors = 6283093430376743019
5363         rx_frame_err = 876072919806406283
5364         rx_over_err = 6525873760178941600
5365         rx_crc_err = 8303073210207070535
5366         collisions = 3409801584220270201
5367
5368         res = OFPPortStats(port_no, rx_packets, tx_packets,
5369                            rx_bytes, tx_bytes, rx_dropped, tx_dropped,
5370                            rx_errors, tx_errors, rx_frame_err,
5371                            rx_over_err, rx_crc_err, collisions)
5372
5373         eq_(port_no, res.port_no)
5374         eq_(rx_packets, res.rx_packets)
5375         eq_(tx_packets, res.tx_packets)
5376         eq_(rx_bytes, res.rx_bytes)
5377         eq_(tx_bytes, res.tx_bytes)
5378         eq_(rx_dropped, res.rx_dropped)
5379         eq_(tx_dropped, res.tx_dropped)
5380         eq_(rx_errors, res.rx_errors)
5381         eq_(tx_errors, res.tx_errors)
5382         eq_(rx_frame_err, res.rx_frame_err)
5383         eq_(rx_over_err, res.rx_over_err)
5384         eq_(rx_crc_err, res.rx_crc_err)
5385         eq_(collisions, res.collisions)
5386
5387     def _test_parser(self, port_no, rx_packets, tx_packets,
5388                      rx_bytes, tx_bytes, rx_dropped, tx_dropped,
5389                      rx_errors, tx_errors, rx_frame_err,
5390                      rx_over_err, rx_crc_err, collisions):
5391
5392         # OFP_PORT_STATS_PACK_STR = '!H6xQQQQQQQQQQQQ'
5393         fmt = ofproto.OFP_PORT_STATS_PACK_STR
5394         buf = pack(fmt, port_no, rx_packets, tx_packets, rx_bytes, tx_bytes,
5395                    rx_dropped, tx_dropped, rx_errors, tx_errors, rx_frame_err,
5396                    rx_over_err, rx_crc_err, collisions)
5397
5398         res = OFPPortStats.parser(buf, 0)
5399
5400         eq_(port_no, res.port_no)
5401         eq_(rx_packets, res.rx_packets)
5402         eq_(tx_packets, res.tx_packets)
5403         eq_(rx_bytes, res.rx_bytes)
5404         eq_(tx_bytes, res.tx_bytes)
5405         eq_(rx_dropped, res.rx_dropped)
5406         eq_(tx_dropped, res.tx_dropped)
5407         eq_(rx_errors, res.rx_errors)
5408         eq_(tx_errors, res.tx_errors)
5409         eq_(rx_frame_err, res.rx_frame_err)
5410         eq_(rx_over_err, res.rx_over_err)
5411         eq_(rx_crc_err, res.rx_crc_err)
5412         eq_(collisions, res.collisions)
5413
5414     def test_parser_mid(self):
5415         port_no = 6606
5416         rx_packets = 5999980397101236279
5417         tx_packets = 2856480458895760962
5418         rx_bytes = 6170274950576278921
5419         tx_bytes = 8638420181865882538
5420         rx_dropped = 6982303461569875546
5421         tx_dropped = 661287462113808071
5422         rx_errors = 3422231811478788365
5423         tx_errors = 6283093430376743019
5424         rx_frame_err = 876072919806406283
5425         rx_over_err = 6525873760178941600
5426         rx_crc_err = 8303073210207070535
5427         collisions = 3409801584220270201
5428
5429         self._test_parser(port_no, rx_packets, tx_packets, rx_bytes, tx_bytes,
5430                           rx_dropped, tx_dropped, rx_errors, tx_errors,
5431                           rx_frame_err, rx_over_err, rx_crc_err, collisions)
5432
5433     def test_parser_max(self):
5434         port_no = 0xffffffff
5435         rx_packets = 0xffffffffffffffff
5436         tx_packets = 0xffffffffffffffff
5437         rx_bytes = 0xffffffffffffffff
5438         tx_bytes = 0xffffffffffffffff
5439         rx_dropped = 0xffffffffffffffff
5440         tx_dropped = 0xffffffffffffffff
5441         rx_errors = 0xffffffffffffffff
5442         tx_errors = 0xffffffffffffffff
5443         rx_frame_err = 0xffffffffffffffff
5444         rx_over_err = 0xffffffffffffffff
5445         rx_crc_err = 0xffffffffffffffff
5446         collisions = 0xffffffffffffffff
5447
5448         self._test_parser(port_no, rx_packets, tx_packets, rx_bytes, tx_bytes,
5449                           rx_dropped, tx_dropped, rx_errors, tx_errors,
5450                           rx_frame_err, rx_over_err, rx_crc_err, collisions)
5451
5452     def test_parser_min(self):
5453         port_no = 0
5454         rx_packets = 0
5455         tx_packets = 0
5456         rx_bytes = 0
5457         tx_bytes = 0
5458         rx_dropped = 0
5459         tx_dropped = 0
5460         rx_errors = 0
5461         tx_errors = 0
5462         rx_frame_err = 0
5463         rx_over_err = 0
5464         rx_crc_err = 0
5465         collisions = 0
5466
5467         self._test_parser(port_no, rx_packets, tx_packets, rx_bytes, tx_bytes,
5468                           rx_dropped, tx_dropped, rx_errors, tx_errors,
5469                           rx_frame_err, rx_over_err, rx_crc_err, collisions)
5470
5471     def _test_parser_p(self, port_no):
5472         port_no = port_no
5473         rx_packets = 5999980397101236279
5474         tx_packets = 2856480458895760962
5475         rx_bytes = 6170274950576278921
5476         tx_bytes = 8638420181865882538
5477         rx_dropped = 6982303461569875546
5478         tx_dropped = 661287462113808071
5479         rx_errors = 3422231811478788365
5480         tx_errors = 6283093430376743019
5481         rx_frame_err = 876072919806406283
5482         rx_over_err = 6525873760178941600
5483         rx_crc_err = 8303073210207070535
5484         collisions = 3409801584220270201
5485
5486         self._test_parser(port_no, rx_packets, tx_packets, rx_bytes, tx_bytes,
5487                           rx_dropped, tx_dropped, rx_errors, tx_errors,
5488                           rx_frame_err, rx_over_err, rx_crc_err, collisions)
5489
5490     def test_parser_p1(self):
5491         self._test_parser_p(ofproto.OFPP_MAX)
5492
5493     def test_parser_p2(self):
5494         self._test_parser_p(ofproto.OFPP_IN_PORT)
5495
5496     def test_parser_p3(self):
5497         self._test_parser_p(ofproto.OFPP_TABLE)
5498
5499     def test_parser_p4(self):
5500         self._test_parser_p(ofproto.OFPP_NORMAL)
5501
5502     def test_parser_p5(self):
5503         self._test_parser_p(ofproto.OFPP_FLOOD)
5504
5505     def test_parser_p6(self):
5506         self._test_parser_p(ofproto.OFPP_ALL)
5507
5508     def test_parser_p7(self):
5509         self._test_parser_p(ofproto.OFPP_CONTROLLER)
5510
5511     def test_parser_p8(self):
5512         self._test_parser_p(ofproto.OFPP_LOCAL)
5513
5514
5515 class TestOFPQueueStatsRequest(unittest.TestCase):
5516     """ Test case for ofproto_v1_2_parser.OFPQueueStatsRequest
5517     """
5518
5519     # OFP_QUEUE_STATS_REQUEST_PACK_STR
5520     # '!II'...port_no, queue_id
5521     port_no = 41186
5522     queue_id = 6606
5523
5524     def test_init(self):
5525         c = OFPQueueStatsRequest(_Datapath, self.port_no, self.queue_id)
5526
5527         eq_(self.port_no, c.port_no)
5528         eq_(self.queue_id, c.queue_id)
5529
5530     def _test_serialize(self, port_no, queue_id):
5531         c = OFPQueueStatsRequest(_Datapath, port_no, queue_id)
5532         c.serialize()
5533
5534         eq_(ofproto.OFP_VERSION, c.version)
5535         eq_(ofproto.OFPT_STATS_REQUEST, c.msg_type)
5536         eq_(0, c.xid)
5537
5538         fmt = '!' \
5539             + ofproto.OFP_HEADER_PACK_STR.replace('!', '') \
5540             + ofproto.OFP_STATS_REQUEST_PACK_STR.replace('!', '') \
5541             + ofproto.OFP_QUEUE_STATS_REQUEST_PACK_STR.replace('!', '')
5542         res = struct.unpack(fmt, six.binary_type(c.buf))
5543
5544         eq_(res[0], ofproto.OFP_VERSION)
5545         eq_(res[1], ofproto.OFPT_STATS_REQUEST)
5546         eq_(res[2], len(c.buf))
5547         eq_(res[3], 0)
5548         eq_(res[4], ofproto.OFPST_QUEUE)
5549         eq_(res[5], 0)
5550         eq_(res[6], port_no)
5551         eq_(res[7], queue_id)
5552
5553     def test_serialize_mid(self):
5554         self._test_serialize(self.port_no, self.queue_id)
5555
5556     def test_serialize_max(self):
5557         self._test_serialize(0xffffffff, 0xffffffff)
5558
5559     def test_serialize_min(self):
5560         self._test_serialize(0, 0)
5561
5562     def test_serialize_p1(self):
5563         self._test_serialize(ofproto.OFPP_MAX, self.queue_id)
5564
5565     def test_serialize_p2(self):
5566         self._test_serialize(ofproto.OFPP_IN_PORT, self.queue_id)
5567
5568     def test_serialize_p3(self):
5569         self._test_serialize(ofproto.OFPP_NORMAL, self.queue_id)
5570
5571     def test_serialize_p4(self):
5572         self._test_serialize(ofproto.OFPP_TABLE, self.queue_id)
5573
5574     def test_serialize_p5(self):
5575         self._test_serialize(ofproto.OFPP_FLOOD, self.queue_id)
5576
5577     def test_serialize_p6(self):
5578         self._test_serialize(ofproto.OFPP_ALL, self.queue_id)
5579
5580     def test_serialize_p7(self):
5581         self._test_serialize(ofproto.OFPP_CONTROLLER, self.queue_id)
5582
5583     def test_serialize_p8(self):
5584         self._test_serialize(ofproto.OFPP_LOCAL, self.queue_id)
5585
5586
5587 class TestOFPQueueStats(unittest.TestCase):
5588     """ Test case for ofproto_v1_2_parser.OFPQueueStats
5589     """
5590
5591     def test_init(self):
5592         port_no = 41186
5593         queue_id = 6606
5594         tx_bytes = 8638420181865882538
5595         tx_packets = 2856480458895760962
5596         tx_errors = 6283093430376743019
5597
5598         res = OFPQueueStats(port_no, queue_id, tx_bytes,
5599                             tx_packets, tx_errors)
5600
5601         eq_(port_no, res.port_no)
5602         eq_(queue_id, res.queue_id)
5603         eq_(tx_bytes, res.tx_bytes)
5604         eq_(tx_packets, res.tx_packets)
5605         eq_(tx_errors, res.tx_errors)
5606
5607     def _test_parser(self, port_no, queue_id, tx_bytes,
5608                      tx_packets, tx_errors):
5609
5610         # OFP_QUEUE_STATS_PACK_STR = '!IIQQQ'
5611         fmt = ofproto.OFP_QUEUE_STATS_PACK_STR
5612         buf = pack(fmt, port_no, queue_id, tx_bytes, tx_packets, tx_errors)
5613         res = OFPQueueStats.parser(buf, 0)
5614
5615         eq_(port_no, res.port_no)
5616         eq_(queue_id, res.queue_id)
5617         eq_(tx_bytes, res.tx_bytes)
5618         eq_(tx_packets, res.tx_packets)
5619         eq_(tx_errors, res.tx_errors)
5620
5621     def test_parser_mid(self):
5622         port_no = 41186
5623         queue_id = 6606
5624         tx_bytes = 8638420181865882538
5625         tx_packets = 2856480458895760962
5626         tx_errors = 6283093430376743019
5627
5628         self._test_parser(port_no, queue_id, tx_bytes,
5629                           tx_packets, tx_errors)
5630
5631     def test_parser_max(self):
5632         port_no = 0xffffffff
5633         queue_id = 0xffffffff
5634         tx_bytes = 0xffffffffffffffff
5635         tx_packets = 0xffffffffffffffff
5636         tx_errors = 0xffffffffffffffff
5637
5638         self._test_parser(port_no, queue_id, tx_bytes,
5639                           tx_packets, tx_errors)
5640
5641     def test_parser_min(self):
5642         port_no = 0
5643         queue_id = 0
5644         tx_bytes = 0
5645         tx_packets = 0
5646         tx_errors = 0
5647
5648         self._test_parser(port_no, queue_id, tx_bytes,
5649                           tx_packets, tx_errors)
5650
5651     def _test_parser_p(self, port_no):
5652         queue_id = 6606
5653         tx_bytes = 8638420181865882538
5654         tx_packets = 2856480458895760962
5655         tx_errors = 6283093430376743019
5656
5657         self._test_parser(port_no, queue_id, tx_bytes,
5658                           tx_packets, tx_errors)
5659
5660     def test_parser_p1(self):
5661         self._test_parser_p(ofproto.OFPP_MAX)
5662
5663     def test_parser_p2(self):
5664         self._test_parser_p(ofproto.OFPP_IN_PORT)
5665
5666     def test_parser_p3(self):
5667         self._test_parser_p(ofproto.OFPP_TABLE)
5668
5669     def test_parser_p4(self):
5670         self._test_parser_p(ofproto.OFPP_NORMAL)
5671
5672     def test_parser_p5(self):
5673         self._test_parser_p(ofproto.OFPP_FLOOD)
5674
5675     def test_parser_p6(self):
5676         self._test_parser_p(ofproto.OFPP_ALL)
5677
5678     def test_parser_p7(self):
5679         self._test_parser_p(ofproto.OFPP_CONTROLLER)
5680
5681     def test_parser_p8(self):
5682         self._test_parser_p(ofproto.OFPP_LOCAL)
5683
5684
5685 class TestOFPBucketCounter(unittest.TestCase):
5686     """ Test case for ofproto_v1_2_parser.OFPBucketCounter
5687     """
5688
5689     # OFP_BUCKET_COUNTER_PACK_STR = '!QQ'
5690     packet_count = 6489108735192644493
5691     byte_count = 7334344481123449724
5692
5693     def test_init(self):
5694         c = OFPBucketCounter(self.packet_count, self.byte_count)
5695
5696         eq_(self.packet_count, c.packet_count)
5697         eq_(self.byte_count, c.byte_count)
5698
5699     def _test_parser(self, packet_count, byte_count):
5700         fmt = ofproto.OFP_BUCKET_COUNTER_PACK_STR
5701         buf = pack(fmt, packet_count, byte_count)
5702
5703         res = OFPBucketCounter.parser(buf, 0)
5704         eq_(packet_count, res.packet_count)
5705         eq_(byte_count, res.byte_count)
5706
5707     def test_parser_mid(self):
5708         self._test_parser(self.packet_count, self.byte_count)
5709
5710     def test_parser_max(self):
5711         packet_count = 18446744073709551615
5712         byte_count = 18446744073709551615
5713         self._test_parser(packet_count, byte_count)
5714
5715     def test_parser_min(self):
5716         packet_count = 0
5717         byte_count = 0
5718         self._test_parser(packet_count, byte_count)
5719
5720
5721 class TestOFPGroupStatsRequest(unittest.TestCase):
5722     """ Test case for ofproto_v1_2_parser.OFPGroupStatsRequest
5723     """
5724
5725     # OFP_GROUP_STATS_REQUEST_PACK_STR
5726     # '!I4x'...group_id, pad(4)
5727     group_id = 6606
5728
5729     def test_init(self):
5730         c = OFPGroupStatsRequest(_Datapath, self.group_id)
5731         eq_(self.group_id, c.group_id)
5732
5733     def _test_serialize(self, group_id):
5734         c = OFPGroupStatsRequest(_Datapath, group_id)
5735         c.serialize()
5736
5737         eq_(ofproto.OFP_VERSION, c.version)
5738         eq_(ofproto.OFPT_STATS_REQUEST, c.msg_type)
5739         eq_(0, c.xid)
5740
5741         fmt = '!' \
5742             + ofproto.OFP_HEADER_PACK_STR.replace('!', '') \
5743             + ofproto.OFP_STATS_REQUEST_PACK_STR.replace('!', '') \
5744             + ofproto.OFP_GROUP_STATS_REQUEST_PACK_STR.replace('!', '')
5745         res = struct.unpack(fmt, six.binary_type(c.buf))
5746
5747         eq_(res[0], ofproto.OFP_VERSION)
5748         eq_(res[1], ofproto.OFPT_STATS_REQUEST)
5749         eq_(res[2], len(c.buf))
5750         eq_(res[3], 0)
5751         eq_(res[4], ofproto.OFPST_GROUP)
5752         eq_(res[5], 0)
5753         eq_(res[6], group_id)
5754
5755     def test_serialize_mid(self):
5756         self._test_serialize(self.group_id)
5757
5758     def test_serialize_max(self):
5759         self._test_serialize(0xffffffff)
5760
5761     def test_serialize_min(self):
5762         self._test_serialize(0)
5763
5764     def test_serialize_p1(self):
5765         self._test_serialize(ofproto.OFPG_MAX)
5766
5767     def test_serialize_p2(self):
5768         self._test_serialize(ofproto.OFPG_ALL)
5769
5770
5771 class TestOFPGroupStats(unittest.TestCase):
5772     """ Test case for ofproto_v1_2_parser.OFPGroupStats
5773     """
5774
5775     # OFP_GROUP_STATS_PACK_STR = '!H2xII4xQQ'
5776     length = ofproto.OFP_GROUP_STATS_SIZE \
5777         + ofproto.OFP_BUCKET_COUNTER_SIZE
5778     group_id = 6606
5779     ref_count = 2102
5780     packet_count = 6489108735192644493
5781     byte_count = 7334344481123449724
5782
5783     # OFP_BUCKET_COUNTER_PACK_STR = '!QQ'
5784     buck_packet_count = 3519264449364891087
5785     buck_byte_count = 3123449724733434448
5786     bucket_counters = [OFPBucketCounter(buck_packet_count, buck_byte_count)]
5787     buf_bucket_counters = pack(ofproto.OFP_BUCKET_COUNTER_PACK_STR,
5788                                buck_packet_count, buck_byte_count)
5789
5790     fmt = ofproto.OFP_GROUP_STATS_PACK_STR
5791     buf = pack(fmt, length, group_id, ref_count, packet_count, byte_count) \
5792         + buf_bucket_counters
5793
5794     def test_init(self):
5795         c = OFPGroupStats(self.group_id, self.ref_count,
5796                           self.packet_count, self.byte_count,
5797                           self.bucket_counters)
5798
5799         eq_(self.group_id, c.group_id)
5800         eq_(self.ref_count, c.ref_count)
5801         eq_(self.packet_count, c.packet_count)
5802         eq_(self.byte_count, c.byte_count)
5803         eq_(self.bucket_counters, c.bucket_counters)
5804
5805     def _test_parser(self, group_id, ref_count, packet_count,
5806                      byte_count, bucket_counter_cnt):
5807         # OFP_GROUP_STATS_PACK_STR = '!H2xII4xQQ'
5808         length = ofproto.OFP_GROUP_STATS_SIZE \
5809             + (ofproto.OFP_BUCKET_COUNTER_SIZE * bucket_counter_cnt)
5810         fmt = ofproto.OFP_GROUP_STATS_PACK_STR
5811         buf = pack(fmt, length, group_id, ref_count,
5812                    packet_count, byte_count)
5813
5814         bucket_counters = []
5815         for b in range(bucket_counter_cnt):
5816             # OFP_BUCKET_COUNTER_PACK_STR = '!QQ'
5817             buck_packet_count = b
5818             buck_byte_count = b
5819             bucket_counter = OFPBucketCounter(buck_packet_count,
5820                                               buck_byte_count)
5821             bucket_counters.append(bucket_counter)
5822             buf_bucket_counters = \
5823                 pack(ofproto.OFP_BUCKET_COUNTER_PACK_STR,
5824                      buck_packet_count, buck_byte_count)
5825             buf += buf_bucket_counters
5826
5827         res = OFPGroupStats.parser(buf, 0)
5828
5829         # 32
5830         eq_(length, res.length)
5831         eq_(group_id, res.group_id)
5832         eq_(ref_count, res.ref_count)
5833         eq_(packet_count, res.packet_count)
5834         eq_(byte_count, res.byte_count)
5835
5836         # 32 + 16 * bucket_counter_cnt < 65535 byte
5837         # bucket_counter_cnt <= 4093
5838         for b in range(bucket_counter_cnt):
5839             eq_(bucket_counters[b].packet_count,
5840                 res.bucket_counters[b].packet_count)
5841             eq_(bucket_counters[b].byte_count,
5842                 res.bucket_counters[b].byte_count)
5843
5844     def test_parser_mid(self):
5845         bucket_counter_cnt = 2046
5846         self._test_parser(self.group_id, self.ref_count,
5847                           self.packet_count, self.byte_count,
5848                           bucket_counter_cnt)
5849
5850     def test_parser_max(self):
5851         group_id = 4294967295
5852         ref_count = 4294967295
5853         packet_count = 18446744073709551615
5854         byte_count = 18446744073709551615
5855         bucket_counter_cnt = 4093
5856         self._test_parser(group_id, ref_count,
5857                           packet_count, byte_count,
5858                           bucket_counter_cnt)
5859
5860     def test_parser_min(self):
5861         group_id = 0
5862         ref_count = 0
5863         packet_count = 0
5864         byte_count = 0
5865         bucket_counter_cnt = 0
5866         self._test_parser(group_id, ref_count,
5867                           packet_count, byte_count,
5868                           bucket_counter_cnt)
5869
5870
5871 class TestOFPGroupDescStatsRequest(unittest.TestCase):
5872     """ Test case for ofproto_v1_2_parser.OFPGroupDescStatsRequest
5873     """
5874
5875     def test_serialize(self):
5876         c = OFPGroupDescStatsRequest(_Datapath)
5877         c.serialize()
5878
5879         fmt = '!' \
5880             + ofproto.OFP_HEADER_PACK_STR.replace('!', '') \
5881             + ofproto.OFP_STATS_REQUEST_PACK_STR.replace('!', '')
5882
5883         res = struct.unpack(fmt, six.binary_type(c.buf))
5884
5885         eq_(res[0], ofproto.OFP_VERSION)
5886         eq_(res[1], ofproto.OFPT_STATS_REQUEST)
5887         eq_(res[2], len(c.buf))
5888         eq_(res[3], 0)
5889         eq_(res[4], ofproto.OFPST_GROUP_DESC)
5890         eq_(res[5], 0)
5891
5892
5893 class TestOFPGroupDescStats(unittest.TestCase):
5894     """ Test case for ofproto_v1_2_parser.OFPGroupDescStats
5895     """
5896
5897     # OFP_GROUP_DESC_STATS_PACK_STR = '!HBxI'
5898     length = ofproto.OFP_GROUP_DESC_STATS_SIZE \
5899         + ofproto.OFP_BUCKET_SIZE \
5900         + ofproto.OFP_ACTION_OUTPUT_SIZE
5901     type_ = 128
5902     group_id = 6606
5903
5904     # OFP_ACTION (OFP_ACTION_OUTPUT)
5905     port = 0x00002ae0
5906     max_len = ofproto.OFP_ACTION_OUTPUT_SIZE
5907     actions = [OFPActionOutput(port, max_len)]
5908     buf_actions = bytearray()
5909     actions[0].serialize(buf_actions, 0)
5910
5911     # OFP_BUCKET
5912     weight = 4386
5913     watch_port = 8006
5914     watch_group = 3
5915     buckets = [OFPBucket(weight, watch_port, watch_group, actions)]
5916
5917     bucket_cnt = 1024
5918
5919     def test_init(self):
5920         c = OFPGroupDescStats(self.type_, self.group_id, self.buckets)
5921
5922         eq_(self.type_, c.type)
5923         eq_(self.group_id, c.group_id)
5924         eq_(self.buckets, c.buckets)
5925
5926     def _test_parser(self, type_, group_id, bucket_cnt):
5927         # OFP_GROUP_DESC_STATS_PACK_STR = '!HBxI'
5928         length = ofproto.OFP_GROUP_DESC_STATS_SIZE \
5929             + (ofproto.OFP_BUCKET_SIZE
5930                + ofproto.OFP_ACTION_OUTPUT_SIZE) * bucket_cnt
5931
5932         fmt = ofproto.OFP_GROUP_DESC_STATS_PACK_STR
5933         buf = pack(fmt, length, type_, group_id)
5934
5935         buckets = []
5936         for b in range(bucket_cnt):
5937             # OFP_BUCKET
5938             weight = watch_port = watch_group = b
5939             bucket = OFPBucket(weight,
5940                                watch_port, watch_group,
5941                                self.actions)
5942             buckets.append(bucket)
5943             buf_buckets = bytearray()
5944             buckets[b].serialize(buf_buckets, 0)
5945             buf += six.binary_type(buf_buckets)
5946
5947         res = OFPGroupDescStats.parser(buf, 0)
5948
5949         # 8 byte
5950         eq_(type_, res.type)
5951         eq_(group_id, res.group_id)
5952
5953         # 8 + ( 16 + 16 ) * b < 65535 byte
5954         # b <= 2047 byte
5955         for b in range(bucket_cnt):
5956             eq_(buckets[b].weight, res.buckets[b].weight)
5957             eq_(buckets[b].watch_port, res.buckets[b].watch_port)
5958             eq_(buckets[b].watch_group, res.buckets[b].watch_group)
5959             eq_(buckets[b].actions[0].port,
5960                 res.buckets[b].actions[0].port)
5961             eq_(buckets[b].actions[0].max_len,
5962                 res.buckets[b].actions[0].max_len)
5963
5964     def test_parser_mid(self):
5965         self._test_parser(self.type_, self.group_id, self.bucket_cnt)
5966
5967     def test_parser_max(self):
5968         group_id = 4294967295
5969         type_ = 255
5970         bucket_cnt = 2047
5971         self._test_parser(type_, group_id, bucket_cnt)
5972
5973     def test_parser_min(self):
5974         group_id = 0
5975         type_ = ofproto.OFPGT_ALL
5976         bucket_cnt = 0
5977         self._test_parser(type_, group_id, bucket_cnt)
5978
5979     def test_parser_p1(self):
5980         type_ = ofproto.OFPGT_SELECT
5981         self._test_parser(type_, self.group_id, self.bucket_cnt)
5982
5983     def test_parser_p2(self):
5984         type_ = ofproto.OFPGT_INDIRECT
5985         self._test_parser(type_, self.group_id, self.bucket_cnt)
5986
5987     def test_parser_p3(self):
5988         type_ = ofproto.OFPGT_FF
5989         self._test_parser(type_, self.group_id, self.bucket_cnt)
5990
5991
5992 class TestOFPGroupFeaturesStatsRequest(unittest.TestCase):
5993     """ Test case for ofproto_v1_2_parser.OFPGroupFeaturesStatsRequest
5994     """
5995
5996     def test_serialize(self):
5997         c = OFPGroupFeaturesStatsRequest(_Datapath)
5998         c.serialize()
5999
6000         fmt = '!' \
6001             + ofproto.OFP_HEADER_PACK_STR.replace('!', '') \
6002             + ofproto.OFP_STATS_REQUEST_PACK_STR.replace('!', '')
6003
6004         res = struct.unpack(fmt, six.binary_type(c.buf))
6005
6006         eq_(res[0], ofproto.OFP_VERSION)
6007         eq_(res[1], ofproto.OFPT_STATS_REQUEST)
6008         eq_(res[2], len(c.buf))
6009         eq_(res[3], 0)
6010         eq_(res[4], ofproto.OFPST_GROUP_FEATURES)
6011         eq_(res[5], 0)
6012
6013
6014 class TestOFPGroupFeaturesStats(unittest.TestCase):
6015     """ Test case for ofproto_v1_2_parser.OFPGroupFeaturesStats
6016     """
6017
6018     # OFP_GROUP_FEATURES_STATS_PACK_STR = '!II4I4I'
6019     types = ofproto.OFPGT_ALL
6020     capabilities = ofproto.OFPGFC_SELECT_WEIGHT
6021     max_groups = [1, 2, 3, 4]
6022     actions = [1 << ofproto.OFPAT_OUTPUT,
6023                1 << ofproto.OFPAT_COPY_TTL_OUT,
6024                1 << ofproto.OFPAT_SET_MPLS_TTL,
6025                1 << ofproto.OFPAT_PUSH_VLAN]
6026
6027     def test_init(self):
6028         c = OFPGroupFeaturesStats(self.types, self.capabilities,
6029                                   self.max_groups, self.actions)
6030         eq_(self.types, c.types)
6031         eq_(self.capabilities, c.capabilities)
6032         eq_(self.max_groups, c.max_groups)
6033         eq_(self.actions, c.actions)
6034
6035     def _test_parser(self, types, capabilities, max_groups, actions):
6036
6037         buf = pack('!I', types) \
6038             + pack('!I', capabilities) \
6039             + pack('!I', max_groups[0]) \
6040             + pack('!I', max_groups[1]) \
6041             + pack('!I', max_groups[2]) \
6042             + pack('!I', max_groups[3]) \
6043             + pack('!I', actions[0]) \
6044             + pack('!I', actions[1]) \
6045             + pack('!I', actions[2]) \
6046             + pack('!I', actions[3])
6047
6048         res = OFPGroupFeaturesStats.parser(buf, 0)
6049
6050         # max_groups and actions after the parser is tuple
6051         eq_(types, res.types)
6052         eq_(capabilities, res.capabilities)
6053         eq_(max_groups, res.max_groups)
6054         eq_(actions, res.actions)
6055
6056     def test_parser_mid(self):
6057         self._test_parser(self.types, self.capabilities,
6058                           self.max_groups, self.actions)
6059
6060     def test_parser_max(self):
6061         types = 0b11111111111111111111111111111111
6062         capabilities = 0b11111111111111111111111111111111
6063         max_groups = [4294967295] * 4
6064         actions = [0b11111111111111111111111111111111] * 4
6065         self._test_parser(types, capabilities,
6066                           max_groups, actions)
6067
6068     def test_parser_min(self):
6069         types = 0b00000000000000000000000000000000
6070         capabilities = 0b00000000000000000000000000000000
6071         max_groups = [0] * 4
6072         actions = [0b00000000000000000000000000000000] * 4
6073         self._test_parser(types, capabilities,
6074                           max_groups, actions)
6075
6076     def _test_parser_p(self, types, capabilities, actions):
6077         self._test_parser(types, capabilities,
6078                           self.max_groups, actions)
6079
6080     def test_parser_p1(self):
6081         actions = [1 << ofproto.OFPAT_COPY_TTL_IN,
6082                    1 << ofproto.OFPAT_DEC_MPLS_TTL,
6083                    1 << ofproto.OFPAT_POP_VLAN,
6084                    1 << ofproto.OFPAT_PUSH_MPLS]
6085         self._test_parser_p(1 << ofproto.OFPGT_ALL,
6086                             ofproto.OFPGFC_CHAINING,
6087                             actions)
6088
6089     def test_parser_p2(self):
6090         actions = [1 << ofproto.OFPAT_POP_MPLS,
6091                    1 << ofproto.OFPAT_SET_QUEUE,
6092                    1 << ofproto.OFPAT_GROUP,
6093                    1 << ofproto.OFPAT_SET_NW_TTL]
6094         self._test_parser_p(1 << ofproto.OFPGT_SELECT,
6095                             ofproto.OFPGFC_SELECT_WEIGHT,
6096                             actions)
6097
6098     def test_parser_p3(self):
6099         actions = [1 << ofproto.OFPAT_DEC_NW_TTL,
6100                    1 << ofproto.OFPAT_SET_FIELD,
6101                    1 << ofproto.OFPAT_GROUP,
6102                    1 << ofproto.OFPAT_SET_NW_TTL]
6103         self._test_parser_p(1 << ofproto.OFPGT_SELECT,
6104                             ofproto.OFPGFC_SELECT_LIVENESS,
6105                             actions)
6106
6107     def test_parser_p4(self):
6108         self._test_parser_p(1 << ofproto.OFPGT_INDIRECT,
6109                             ofproto.OFPGFC_CHAINING,
6110                             self.actions)
6111
6112     def test_parser_p5(self):
6113         self._test_parser_p(1 << ofproto.OFPGT_FF,
6114                             ofproto.OFPGFC_CHAINING_CHECKS,
6115                             self.actions)
6116
6117
6118 class TestOFPQueueGetConfigRequest(unittest.TestCase):
6119     """ Test case for ofproto_v1_2_parser.OFPQueueGetConfigRequest
6120     """
6121
6122     # OFP_QUEUE_GET_CONFIG_REQUEST_PACK_STR v1.2
6123     # '!I4x'...port, pad(4)
6124     port = 41186
6125
6126     def test_init(self):
6127         c = OFPQueueGetConfigRequest(_Datapath, self.port)
6128         eq_(self.port, c.port)
6129
6130     def _test_serialize(self, port):
6131         c = OFPQueueGetConfigRequest(_Datapath, port)
6132         c.serialize()
6133
6134         eq_(ofproto.OFP_VERSION, c.version)
6135         eq_(ofproto.OFPT_QUEUE_GET_CONFIG_REQUEST, c.msg_type)
6136         eq_(0, c.xid)
6137
6138         fmt = ofproto.OFP_HEADER_PACK_STR \
6139             + ofproto.OFP_QUEUE_GET_CONFIG_REQUEST_PACK_STR[1:]
6140
6141         res = struct.unpack(fmt, six.binary_type(c.buf))
6142         eq_(res[0], ofproto.OFP_VERSION)
6143         eq_(res[1], ofproto.OFPT_QUEUE_GET_CONFIG_REQUEST)
6144         eq_(res[2], len(c.buf))
6145         eq_(res[3], 0)
6146         eq_(res[4], port)
6147
6148     def test_serialize_mid(self):
6149         self._test_serialize(self.port)
6150
6151     def test_serialize_max(self):
6152         self._test_serialize(0xffffffff)
6153
6154     def test_serialize_min(self):
6155         self._test_serialize(0)
6156
6157     def test_serialize_p1(self):
6158         self._test_serialize(ofproto.OFPP_MAX)
6159
6160
6161 class TestOFPQueuePropHeader(unittest.TestCase):
6162     """ Test case for ofproto_v1_2_parser.OFPQueuePropHeader
6163     """
6164
6165     # OFP_QUEUE_PROP_HEADER_PACK_STR = '!HH4x'
6166     property_ = 1
6167     len_ = 10
6168
6169     def test_init(self):
6170         c = OFPQueuePropHeader(self.property_, self.len_)
6171         eq_(self.property_, c.property)
6172         eq_(self.len_, c.len)
6173
6174     def _test_serialize(self, property_, len_):
6175         c = OFPQueuePropHeader(property_, len_)
6176         buf = bytearray()
6177         c.serialize(buf, 0)
6178
6179         fmt = ofproto.OFP_QUEUE_PROP_HEADER_PACK_STR
6180         res = struct.unpack(fmt, six.binary_type(buf))
6181
6182         eq_(res[0], property_)
6183         eq_(res[1], len_)
6184
6185     def test_serialize_mid(self):
6186         self._test_serialize(self.property_, self.len_)
6187
6188     def test_serialize_max(self):
6189         self._test_serialize(0xffff, 0xffff)
6190
6191     def test_serialize_min(self):
6192         self._test_serialize(0, 0)
6193
6194
6195 class TestOFPPacketQueue(unittest.TestCase):
6196     """ Test case for ofproto_v1_2_parser.OFPPacketQueue
6197     """
6198
6199     def test_init(self):
6200         queue_id = 1
6201         port = 2
6202         len_ = 3
6203         properties = [4, 5, 6]
6204         c = OFPPacketQueue(queue_id, port, properties)
6205
6206         eq_(queue_id, c.queue_id)
6207         eq_(port, c.port)
6208         eq_(properties, c.properties)
6209
6210     def _test_parser(self, queue_id, port, prop_cnt):
6211         # OFP_PACKET_QUEUE_PACK_STR = '!IIH6x'
6212         fmt = ofproto.OFP_PACKET_QUEUE_PACK_STR
6213         queue_len = ofproto.OFP_PACKET_QUEUE_SIZE \
6214             + ofproto.OFP_QUEUE_PROP_MIN_RATE_SIZE * prop_cnt
6215
6216         buf = pack(fmt, queue_id, port, queue_len)
6217
6218         for rate in range(prop_cnt):
6219             # OFP_QUEUE_PROP_HEADER_PACK_STR = '!HH4x'
6220             fmt = ofproto.OFP_QUEUE_PROP_HEADER_PACK_STR
6221             prop_type = ofproto.OFPQT_MIN_RATE
6222             prop_len = ofproto.OFP_QUEUE_PROP_MIN_RATE_SIZE
6223             buf += pack(fmt, prop_type, prop_len)
6224
6225             # OFP_QUEUE_PROP_MIN_RATE_PACK_STR = '!H6x'
6226             fmt = ofproto.OFP_QUEUE_PROP_MIN_RATE_PACK_STR
6227             prop_rate = rate
6228             buf += pack(fmt, prop_rate)
6229
6230         res = OFPPacketQueue.parser(buf, 0)
6231
6232         eq_(queue_id, res.queue_id)
6233         eq_(port, res.port)
6234         eq_(queue_len, res.len)
6235         eq_(prop_cnt, len(res.properties))
6236
6237         for rate, p in enumerate(res.properties):
6238             eq_(prop_type, p.property)
6239             eq_(prop_len, p.len)
6240             eq_(rate, p.rate)
6241
6242     def test_parser_mid(self):
6243         queue_id = 1
6244         port = 2
6245         prop_cnt = 2
6246         self._test_parser(queue_id, port, prop_cnt)
6247
6248     def test_parser_max(self):
6249         # queue_len format is 'H' < number 65535
6250         #
6251         # queue_len = OFP_PACKET_QUEUE_SIZE(16)
6252         #     + OFP_QUEUE_PROP_MIN_RATE_SIZE(16) * N
6253         # max_prop_cnt = (65535 - 16) / 16 = 4094
6254         queue_id = 0xffffffff
6255         port = 0xffffffff
6256         prop_cnt = 4094
6257         self._test_parser(queue_id, port, prop_cnt)
6258
6259     def test_parser_min(self):
6260         queue_id = 0
6261         port = 0
6262         prop_cnt = 0
6263         self._test_parser(queue_id, port, prop_cnt)
6264
6265
6266 class TestOFPQueuePropMinRate(unittest.TestCase):
6267     """ Test case for ofproto_v1_2_parser.OFPQueuePropMinRate
6268     """
6269
6270     def _test_parser(self, rate):
6271         # OFP_QUEUE_PROP_MIN_RATE_PACK_STR...H6x
6272         buf = pack(ofproto.OFP_QUEUE_PROP_MIN_RATE_PACK_STR, rate)
6273         res = OFPQueuePropMinRate.parser(buf, 0)
6274         eq_(rate, res.rate)
6275
6276     def test_parser_mid(self):
6277         self._test_parser(32768)
6278
6279     def test_parser_max(self):
6280         self._test_parser(0xffff)
6281
6282     def test_parser_min(self):
6283         self._test_parser(0)
6284
6285
6286 class TestOFPQueuePropMaxRate(unittest.TestCase):
6287     """ Test case for ofproto_v1_2_parser.OFPQueuePropMaxRate
6288     """
6289
6290     rate = 100
6291     buf = pack(ofproto.OFP_QUEUE_PROP_MAX_RATE_PACK_STR, rate)
6292     c = OFPQueuePropMaxRate(rate)
6293
6294     def _test_parser(self, rate):
6295         # OFP_QUEUE_PROP_MAX_RATE_PACK_STR...H6x
6296         buf = pack(ofproto.OFP_QUEUE_PROP_MAX_RATE_PACK_STR, rate)
6297         res = OFPQueuePropMaxRate.parser(buf, 0)
6298         eq_(rate, res.rate)
6299
6300     def test_parser_mid(self):
6301         self._test_parser(100)
6302
6303     def test_parser_max(self):
6304         self._test_parser(0xffff)
6305
6306     def test_parser_min(self):
6307         self._test_parser(0)
6308
6309
6310 class TestOFPQueueGetConfigReply(unittest.TestCase):
6311     """ Test case for ofproto_v1_2_parser.OFPQueueGetConfigReply
6312     """
6313
6314     def _test_parser(self, xid, port, queue_cnt):
6315         version = ofproto.OFP_VERSION
6316         msg_type = ofproto.OFPT_QUEUE_GET_CONFIG_REPLY
6317
6318         queues_len = 0
6319         for q in range(queue_cnt):
6320             queues_len += ofproto.OFP_PACKET_QUEUE_SIZE
6321             queues_len += ofproto.OFP_QUEUE_PROP_MIN_RATE_SIZE
6322
6323         msg_len = ofproto.OFP_QUEUE_GET_CONFIG_REPLY_SIZE \
6324             + queues_len
6325
6326         # OFP_HEADER_PACK_STR = '!BBHI'
6327         fmt = ofproto.OFP_HEADER_PACK_STR
6328         buf = pack(fmt, version, msg_type, msg_len, xid)
6329
6330         # OFP_QUEUE_GET_CONFIG_REPLY_PACK_STR = '!I4x'
6331         fmt = ofproto.OFP_QUEUE_GET_CONFIG_REPLY_PACK_STR
6332         buf += pack(fmt, port)
6333
6334         queues = []
6335         for q in range(1, queue_cnt + 1):
6336             # OFP_PACKET_QUEUE_PACK_STR = '!IIH6x'
6337             fmt = ofproto.OFP_PACKET_QUEUE_PACK_STR
6338             queue_id = q * 100
6339             queue_port = q
6340             queue_len = ofproto.OFP_PACKET_QUEUE_SIZE \
6341                 + ofproto.OFP_QUEUE_PROP_MIN_RATE_SIZE
6342             buf += pack(fmt, queue_id, queue_port, queue_len)
6343
6344             # OFP_QUEUE_PROP_HEADER_PACK_STR = '!HH4x'
6345             fmt = ofproto.OFP_QUEUE_PROP_HEADER_PACK_STR
6346             prop_type = ofproto.OFPQT_MIN_RATE
6347             prop_len = ofproto.OFP_QUEUE_PROP_MIN_RATE_SIZE
6348             buf += pack(fmt, prop_type, prop_len)
6349
6350             # OFP_QUEUE_PROP_MIN_RATE_PACK_STR = '!H6x'
6351             fmt = ofproto.OFP_QUEUE_PROP_MIN_RATE_PACK_STR
6352             prop_rate = q * 10
6353             buf += pack(fmt, prop_rate)
6354
6355             queue = {'queue_id': queue_id, 'queue_port': queue_port,
6356                      'queue_len': queue_len, 'prop_type': prop_type,
6357                      'prop_len': prop_len, 'prop_rate': prop_rate}
6358             queues.append(queue)
6359
6360         res = OFPQueueGetConfigReply.parser(object, version, msg_type,
6361                                             msg_len, xid, buf)
6362         eq_(version, res.version)
6363         eq_(msg_type, res.msg_type)
6364         eq_(msg_len, res.msg_len)
6365         eq_(xid, res.xid)
6366         eq_(port, res.port)
6367         eq_(queue_cnt, len(res.queues))
6368
6369         for i, val in enumerate(res.queues):
6370             c = queues[i]
6371             eq_(c['queue_id'], val.queue_id)
6372             eq_(c['queue_port'], val.port)
6373             eq_(c['queue_len'], val.len)
6374             eq_(1, len(val.properties))
6375
6376             prop = val.properties[0]
6377             eq_(c['prop_type'], prop.property)
6378             eq_(c['prop_len'], prop.len)
6379             eq_(c['prop_rate'], prop.rate)
6380
6381     def test_parser_mid(self):
6382         self._test_parser(2495926989, 65037, 2)
6383
6384     def test_parser_max(self):
6385         # total msg_len = 65520
6386         self._test_parser(0xffffffff, 0xffffffff, 2047)
6387
6388     def test_parser_min(self):
6389         self._test_parser(0, 0, 0)
6390
6391
6392 class TestOFPBarrierRequest(unittest.TestCase):
6393     """ Test case for ofproto_v1_2_parser.OFPBarrierRequest
6394     """
6395
6396     def test_serialize(self):
6397         c = OFPBarrierRequest(_Datapath)
6398         c.serialize()
6399
6400         eq_(ofproto.OFP_VERSION, c.version)
6401         eq_(ofproto.OFPT_BARRIER_REQUEST, c.msg_type)
6402         eq_(ofproto.OFP_HEADER_SIZE, c.msg_len)
6403         eq_(0, c.xid)
6404
6405         fmt = ofproto.OFP_HEADER_PACK_STR
6406         res = unpack(fmt, six.binary_type(c.buf))
6407         eq_(ofproto.OFP_VERSION, res[0])
6408         eq_(ofproto.OFPT_BARRIER_REQUEST, res[1])
6409         eq_(len(c.buf), res[2])
6410         eq_(0, c.xid)
6411
6412
6413 class TestOFPBarrierReply(unittest.TestCase):
6414     """ Test case for ofproto_v1_2_parser.OFPBarrierReply
6415     """
6416
6417     def _test_parser(self, xid):
6418         version = ofproto.OFP_VERSION
6419         msg_type = ofproto.OFPT_BARRIER_REPLY
6420         msg_len = ofproto.OFP_HEADER_SIZE
6421
6422         fmt = ofproto.OFP_HEADER_PACK_STR
6423         buf = pack(fmt, version, msg_type, msg_len, xid)
6424
6425         res = OFPBarrierReply.parser(object, version, msg_type,
6426                                      msg_len, xid, buf)
6427         eq_(version, res.version)
6428         eq_(msg_type, res.msg_type)
6429         eq_(msg_len, res.msg_len)
6430         eq_(xid, res.xid)
6431
6432     def test_parser_mid(self):
6433         self._test_parser(2147483648)
6434
6435     def test_parser_max(self):
6436         self._test_parser(0xffffffff)
6437
6438     def test_parser_min(self):
6439         self._test_parser(0)
6440
6441
6442 class TestOFPRoleRequest(unittest.TestCase):
6443     """ Test case for ofproto_v1_2_parser.OFPRoleRequest
6444     """
6445
6446     # OFP_ROLE_REQUEST_PACK_STR
6447     # '!I4xQ'...role, pad(4), generation_id
6448     role = 2147483648
6449     generation_id = 1270985291017894273
6450
6451     def test_init(self):
6452         c = OFPRoleRequest(_Datapath, self.role, self.generation_id)
6453         eq_(self.role, c.role)
6454         eq_(self.generation_id, c.generation_id)
6455
6456     def _test_serialize(self, role, generation_id):
6457         c = OFPRoleRequest(_Datapath, role, generation_id)
6458         c.serialize()
6459
6460         eq_(ofproto.OFP_VERSION, c.version)
6461         eq_(ofproto.OFPT_ROLE_REQUEST, c.msg_type)
6462         eq_(0, c.xid)
6463
6464         fmt = '!' \
6465             + ofproto.OFP_HEADER_PACK_STR.replace('!', '') \
6466             + ofproto.OFP_ROLE_REQUEST_PACK_STR.replace('!', '')
6467
6468         res = struct.unpack(fmt, six.binary_type(c.buf))
6469
6470         eq_(ofproto.OFP_VERSION, res[0])
6471         eq_(ofproto.OFPT_ROLE_REQUEST, res[1])
6472         eq_(len(c.buf), res[2])
6473         eq_(0, res[3])
6474         eq_(role, res[4])
6475         eq_(generation_id, res[5])
6476
6477     def test_serialize_mid(self):
6478         self._test_serialize(self.role, self.generation_id)
6479
6480     def test_serialize_max(self):
6481         role = 0xffffffff
6482         generation_id = 0xffffffffffffffff
6483         self._test_serialize(role, generation_id)
6484
6485     def test_serialize_min(self):
6486         role = 0
6487         generation_id = 0
6488         self._test_serialize(role, generation_id)
6489
6490     def test_serialize_p1(self):
6491         role = ofproto.OFPCR_ROLE_EQUAL
6492         self._test_serialize(role, self.generation_id)
6493
6494     def test_serialize_p2(self):
6495         role = ofproto.OFPCR_ROLE_MASTER
6496         self._test_serialize(role, self.generation_id)
6497
6498     def test_serialize_p3(self):
6499         role = ofproto.OFPCR_ROLE_SLAVE
6500         self._test_serialize(role, self.generation_id)
6501
6502
6503 class TestOFPRoleReply(unittest.TestCase):
6504     """ Test case for ofproto_v1_2_parser.OFPRoleReply
6505     """
6506
6507     # OFP_ROLE_REQUEST_PACK_STR
6508     # '!I4xQ'...role, pad(4), generation_id
6509     # role = ofproto.OFPCR_ROLE_NOCHANGE
6510     role = 2147483648
6511     generation_id = 1270985291017894273
6512
6513     def _test_parser(self, role, generation_id):
6514         # OFP_HEADER_PACK_STR
6515         version = ofproto.OFP_VERSION
6516         msg_type = ofproto.OFPT_ROLE_REPLY
6517         msg_len = ofproto.OFP_ROLE_REQUEST_SIZE
6518         xid = 2495926989
6519
6520         fmt = ofproto.OFP_HEADER_PACK_STR
6521         buf = pack(fmt, version, msg_type, msg_len, xid)
6522
6523         fmt = ofproto.OFP_ROLE_REQUEST_PACK_STR
6524         buf += pack(fmt, role, generation_id)
6525
6526         res = OFPRoleReply.parser(object, version, msg_type, msg_len, xid, buf)
6527
6528         # OFP_HEADER_PACK_STR
6529         eq_(version, res.version)
6530         eq_(msg_type, res.msg_type)
6531         eq_(msg_len, res.msg_len)
6532         eq_(xid, res.xid)
6533
6534         # OFP_ROLE_REQUEST_PACK_STR
6535         eq_(role, res.role)
6536         eq_(generation_id, res.generation_id)
6537
6538     def test_parser_mid(self):
6539         self._test_parser(self.role, self.generation_id)
6540
6541     def test_parser_max(self):
6542         role = 0xffffffff
6543         generation_id = 0xffffffffffffffff
6544         self._test_parser(role, generation_id)
6545
6546     def test_parser_min(self):
6547         role = ofproto.OFPCR_ROLE_NOCHANGE
6548         generation_id = 0
6549         self._test_parser(role, generation_id)
6550
6551     def test_parser_p1(self):
6552         role = ofproto.OFPCR_ROLE_EQUAL
6553         self._test_parser(role, self.generation_id)
6554
6555     def test_parser_p2(self):
6556         role = ofproto.OFPCR_ROLE_MASTER
6557         self._test_parser(role, self.generation_id)
6558
6559     def test_parser_p3(self):
6560         role = ofproto.OFPCR_ROLE_SLAVE
6561         self._test_parser(role, self.generation_id)
6562
6563
6564 class TestOFPMatch(unittest.TestCase):
6565     """ Test case for ofproto_v1_2_parser.OFPMatch
6566     """
6567
6568     def test_init(self):
6569         res = OFPMatch()
6570
6571         # wc check
6572         eq_(res._wc.metadata_mask, 0)
6573         eq_(res._wc.dl_dst_mask, 0)
6574         eq_(res._wc.dl_src_mask, 0)
6575         eq_(res._wc.vlan_vid_mask, 0)
6576         eq_(res._wc.ipv4_src_mask, 0)
6577         eq_(res._wc.ipv4_dst_mask, 0)
6578         eq_(res._wc.arp_spa_mask, 0)
6579         eq_(res._wc.arp_tpa_mask, 0)
6580         eq_(res._wc.arp_sha_mask, 0)
6581         eq_(res._wc.arp_tha_mask, 0)
6582         eq_(res._wc.ipv6_src_mask, [])
6583         eq_(res._wc.ipv6_dst_mask, [])
6584         eq_(res._wc.ipv6_flabel_mask, 0)
6585         eq_(res._wc.wildcards, (1 << 64) - 1)
6586
6587         # flow check
6588         eq_(res._flow.in_port, 0)
6589         eq_(res._flow.in_phy_port, 0)
6590         eq_(res._flow.metadata, 0)
6591         eq_(res._flow.dl_dst, mac.DONTCARE)
6592         eq_(res._flow.dl_src, mac.DONTCARE)
6593         eq_(res._flow.dl_type, 0)
6594         eq_(res._flow.vlan_vid, 0)
6595         eq_(res._flow.vlan_pcp, 0)
6596         eq_(res._flow.ip_dscp, 0)
6597         eq_(res._flow.ip_ecn, 0)
6598         eq_(res._flow.ip_proto, 0)
6599         eq_(res._flow.ipv4_src, 0)
6600         eq_(res._flow.ipv4_dst, 0)
6601         eq_(res._flow.tcp_src, 0)
6602         eq_(res._flow.tcp_dst, 0)
6603         eq_(res._flow.udp_src, 0)
6604         eq_(res._flow.udp_dst, 0)
6605         eq_(res._flow.sctp_src, 0)
6606         eq_(res._flow.sctp_dst, 0)
6607         eq_(res._flow.icmpv4_type, 0)
6608         eq_(res._flow.icmpv4_code, 0)
6609         eq_(res._flow.arp_op, 0)
6610         eq_(res._flow.arp_spa, 0)
6611         eq_(res._flow.arp_tpa, 0)
6612         eq_(res._flow.arp_sha, 0)
6613         eq_(res._flow.arp_tha, 0)
6614         eq_(res._flow.ipv6_src, [])
6615         eq_(res._flow.ipv6_dst, [])
6616         eq_(res._flow.ipv6_flabel, 0)
6617         eq_(res._flow.icmpv6_type, 0)
6618         eq_(res._flow.icmpv6_code, 0)
6619         eq_(res._flow.ipv6_nd_target, [])
6620         eq_(res._flow.ipv6_nd_sll, 0)
6621         eq_(res._flow.ipv6_nd_tll, 0)
6622         eq_(res._flow.mpls_label, 0)
6623         eq_(res._flow.mpls_tc, 0)
6624
6625         # flow check
6626         eq_(res.fields, [])
6627
6628     def _test_serialize_and_parser(self, match, header, value, mask=None):
6629         cls_ = OFPMatchField._FIELDS_HEADERS.get(header)
6630         pack_str = cls_.pack_str.replace('!', '')
6631         fmt = '!HHI' + pack_str
6632
6633         # serialize
6634         buf = bytearray()
6635         length = match.serialize(buf, 0)
6636         eq_(length, len(buf))
6637         if mask and len(buf) > calcsize(fmt):
6638             fmt += pack_str
6639
6640         res = list(unpack_from(fmt, six.binary_type(buf), 0)[3:])
6641         if type(value) is list:
6642             res_value = res[:calcsize(pack_str) // 2]
6643             eq_(res_value, value)
6644             if mask:
6645                 res_mask = res[calcsize(pack_str) // 2:]
6646                 eq_(res_mask, mask)
6647         else:
6648             res_value = res.pop(0)
6649             if cls_.__name__ == 'MTVlanVid':
6650                 eq_(res_value, value | ofproto.OFPVID_PRESENT)
6651             else:
6652                 eq_(res_value, value)
6653             if mask and res and res[0]:
6654                 res_mask = res[0]
6655                 eq_(res_mask, mask)
6656
6657         # parser
6658         res = match.parser(six.binary_type(buf), 0)
6659         eq_(res.type, ofproto.OFPMT_OXM)
6660         eq_(res.fields[0].header, header)
6661         eq_(res.fields[0].value, value)
6662         if mask and res.fields[0].mask is not None:
6663             eq_(res.fields[0].mask, mask)
6664
6665         # to_jsondict
6666         jsondict = match.to_jsondict()
6667
6668         # from_jsondict
6669         match2 = match.from_jsondict(jsondict["OFPMatch"])
6670         buf2 = bytearray()
6671         match2.serialize(buf2, 0)
6672         eq_(str(match), str(match2))
6673         eq_(buf, buf2)
6674
6675     def test_parse_unknown_field(self):
6676         buf = bytearray()
6677         pack_utils.msg_pack_into('!HH', buf, 0, ofproto.OFPMT_OXM, 4 + 6)
6678         header = ofproto.oxm_tlv_header(36, 2)
6679         pack_utils.msg_pack_into('!IH', buf, 4, header, 1)
6680         header = ofproto.OXM_OF_ETH_TYPE
6681         pack_utils.msg_pack_into('!IH', buf, 10, header, 1)
6682
6683         match = OFPMatch()
6684         res = match.parser(six.binary_type(buf), 0)
6685
6686     # set_in_port
6687     def _test_set_in_port(self, in_port):
6688         header = ofproto.OXM_OF_IN_PORT
6689         match = OFPMatch()
6690         match.set_in_port(in_port)
6691         self._test_serialize_and_parser(match, header, in_port)
6692
6693     def test_set_in_port_mid(self):
6694         self._test_set_in_port(0xff8)
6695
6696     def test_set_in_port_max(self):
6697         self._test_set_in_port(0xffffffff)
6698
6699     def test_set_in_port_min(self):
6700         self._test_set_in_port(0)
6701
6702     # set_in_phy_port
6703     def _test_set_in_phy_port(self, phy_port):
6704         header = ofproto.OXM_OF_IN_PHY_PORT
6705         match = OFPMatch()
6706         match.set_in_phy_port(phy_port)
6707         self._test_serialize_and_parser(match, header, phy_port)
6708
6709     def test_set_in_phy_port_mid(self):
6710         self._test_set_in_phy_port(1)
6711
6712     def test_set_in_phy_port_max(self):
6713         self._test_set_in_phy_port(0xffffffff)
6714
6715     def test_set_in_phy_port_min(self):
6716         self._test_set_in_phy_port(0)
6717
6718     # set_metadata
6719     def _test_set_metadata(self, metadata, mask=None):
6720         header = ofproto.OXM_OF_METADATA
6721         match = OFPMatch()
6722         if mask is None:
6723             match.set_metadata(metadata)
6724         else:
6725             if (mask + 1) >> 64 != 1:
6726                 header = ofproto.OXM_OF_METADATA_W
6727             match.set_metadata_masked(metadata, mask)
6728             metadata &= mask
6729         self._test_serialize_and_parser(match, header, metadata, mask)
6730
6731     def test_set_metadata_mid(self):
6732         self._test_set_metadata(0x1212121212121212)
6733
6734     def test_set_metadata_max(self):
6735         self._test_set_metadata(0xffffffffffffffff)
6736
6737     def test_set_metadata_min(self):
6738         self._test_set_metadata(0)
6739
6740     def test_set_metadata_masked_mid(self):
6741         self._test_set_metadata(0x1212121212121212, 0xff00ff00ff00ff00)
6742
6743     def test_set_metadata_masked_max(self):
6744         self._test_set_metadata(0x1212121212121212, 0xffffffffffffffff)
6745
6746     def test_set_metadata_masked_min(self):
6747         self._test_set_metadata(0x1212121212121212, 0)
6748
6749     # set_dl_dst
6750     def _test_set_dl_dst(self, dl_dst, mask=None):
6751         header = ofproto.OXM_OF_ETH_DST
6752         match = OFPMatch()
6753         dl_dst = mac.haddr_to_bin(dl_dst)
6754         if mask is None:
6755             match.set_dl_dst(dl_dst)
6756         else:
6757             header = ofproto.OXM_OF_ETH_DST_W
6758             mask = mac.haddr_to_bin(mask)
6759             match.set_dl_dst_masked(dl_dst, mask)
6760             dl_dst = mac.haddr_bitand(dl_dst, mask)
6761         self._test_serialize_and_parser(match, header, dl_dst, mask)
6762
6763     def test_set_dl_dst_mid(self):
6764         self._test_set_dl_dst('e2:7a:09:79:0b:0f')
6765
6766     def test_set_dl_dst_max(self):
6767         self._test_set_dl_dst('ff:ff:ff:ff:ff:ff')
6768
6769     def test_set_dl_dst_min(self):
6770         self._test_set_dl_dst('00:00:00:00:00:00')
6771
6772     def test_set_dl_dst_masked_mid(self):
6773         self._test_set_dl_dst('e2:7a:09:79:0b:0f', 'ff:00:ff:00:ff:00')
6774
6775     def test_set_dl_dst_masked_max(self):
6776         self._test_set_dl_dst('e2:7a:09:79:0b:0f', 'ff:ff:ff:ff:ff:ff')
6777
6778     def test_set_dl_dst_masked_min(self):
6779         self._test_set_dl_dst('e2:7a:09:79:0b:0f', '00:00:00:00:00:00')
6780
6781     # set_dl_src
6782     def _test_set_dl_src(self, dl_src, mask=None):
6783         header = ofproto.OXM_OF_ETH_SRC
6784         match = OFPMatch()
6785         dl_src = mac.haddr_to_bin(dl_src)
6786         if mask is None:
6787             match.set_dl_src(dl_src)
6788         else:
6789             header = ofproto.OXM_OF_ETH_SRC_W
6790             mask = mac.haddr_to_bin(mask)
6791             match.set_dl_src_masked(dl_src, mask)
6792             dl_src = mac.haddr_bitand(dl_src, mask)
6793         self._test_serialize_and_parser(match, header, dl_src, mask)
6794
6795     def test_set_dl_src_mid(self):
6796         self._test_set_dl_src('d0:98:79:b4:75:b5')
6797
6798     def test_set_dl_src_max(self):
6799         self._test_set_dl_src('ff:ff:ff:ff:ff:ff')
6800
6801     def test_set_dl_src_min(self):
6802         self._test_set_dl_src('00:00:00:00:00:00')
6803
6804     def test_set_dl_src_masked_mid(self):
6805         self._test_set_dl_src('d0:98:79:b4:75:b5', 'f0:f0:f0:f0:f0:f0')
6806
6807     def test_set_dl_src_masked_max(self):
6808         self._test_set_dl_src('d0:98:79:b4:75:b5', 'ff:ff:ff:ff:ff:ff')
6809
6810     def test_set_dl_src_masked_min(self):
6811         self._test_set_dl_src('d0:98:79:b4:75:b5', '00:00:00:00:00:00')
6812
6813     # set_dl_type
6814     def _test_set_dl_type(self, value):
6815         header = ofproto.OXM_OF_ETH_TYPE
6816         match = OFPMatch()
6817         match.set_dl_type(value)
6818         self._test_serialize_and_parser(match, header, value)
6819
6820     def test_set_dl_type_mid(self):
6821         self._test_set_dl_type(0x7fb6)
6822
6823     def test_set_dl_type_max(self):
6824         self._test_set_dl_type(0xffff)
6825
6826     def test_set_dl_type_min(self):
6827         self._test_set_dl_type(0)
6828
6829     def test_set_dl_type_ip(self):
6830         value = ether.ETH_TYPE_IP
6831         self._test_set_dl_type(value)
6832
6833     def test_set_dl_type_arp(self):
6834         value = ether.ETH_TYPE_ARP
6835         self._test_set_dl_type(value)
6836
6837     def test_set_dl_type_ipv6(self):
6838         value = ether.ETH_TYPE_IPV6
6839         self._test_set_dl_type(value)
6840
6841     def test_set_dl_type_slow(self):
6842         value = ether.ETH_TYPE_SLOW
6843         self._test_set_dl_type(value)
6844
6845     # set_vlan_vid
6846     def _test_set_vlan_vid(self, vid, mask=None):
6847         header = ofproto.OXM_OF_VLAN_VID
6848         match = OFPMatch()
6849         if mask is None:
6850             match.set_vlan_vid(vid)
6851         else:
6852             header = ofproto.OXM_OF_VLAN_VID_W
6853             match.set_vlan_vid_masked(vid, mask)
6854         self._test_serialize_and_parser(match, header, vid, mask)
6855
6856     def _test_set_vlan_vid_none(self):
6857         header = ofproto.OXM_OF_VLAN_VID
6858         match = OFPMatch()
6859         match.set_vlan_vid_none()
6860         value = ofproto.OFPVID_NONE
6861         cls_ = OFPMatchField._FIELDS_HEADERS.get(header)
6862         pack_str = cls_.pack_str.replace('!', '')
6863         fmt = '!HHI' + pack_str
6864
6865         # serialize
6866         buf = bytearray()
6867         length = match.serialize(buf, 0)
6868         eq_(length, len(buf))
6869
6870         res = list(unpack_from(fmt, six.binary_type(buf), 0)[3:])
6871         res_value = res.pop(0)
6872         eq_(res_value, value)
6873
6874         # parser
6875         res = match.parser(six.binary_type(buf), 0)
6876         eq_(res.type, ofproto.OFPMT_OXM)
6877         eq_(res.fields[0].header, header)
6878         eq_(res.fields[0].value, value)
6879
6880         # to_jsondict
6881         jsondict = match.to_jsondict()
6882
6883         # from_jsondict
6884         match2 = match.from_jsondict(jsondict["OFPMatch"])
6885         buf2 = bytearray()
6886         match2.serialize(buf2, 0)
6887         eq_(str(match), str(match2))
6888         eq_(buf, buf2)
6889
6890     def test_set_vlan_vid_mid(self):
6891         self._test_set_vlan_vid(2047)
6892
6893     def test_set_vlan_vid_max(self):
6894         self._test_set_vlan_vid(0xfff)
6895
6896     def test_set_vlan_vid_min(self):
6897         self._test_set_vlan_vid(0)
6898
6899     def test_set_vlan_vid_masked_mid(self):
6900         self._test_set_vlan_vid(2047, 0xf0f)
6901
6902     def test_set_vlan_vid_masked_max(self):
6903         self._test_set_vlan_vid(2047, 0xfff)
6904
6905     def test_set_vlan_vid_masked_min(self):
6906         self._test_set_vlan_vid(2047, 0)
6907
6908     def test_set_vlan_vid_none(self):
6909         self._test_set_vlan_vid_none()
6910
6911     # set_vlan_pcp
6912     def _test_set_vlan_pcp(self, pcp):
6913         header = ofproto.OXM_OF_VLAN_PCP
6914         match = OFPMatch()
6915         match.set_vlan_pcp(pcp)
6916         self._test_serialize_and_parser(match, header, pcp)
6917
6918     def test_set_vlan_pcp_mid(self):
6919         self._test_set_vlan_pcp(5)
6920
6921     def test_set_vlan_pcp_max(self):
6922         self._test_set_vlan_pcp(7)
6923
6924     def test_set_vlan_pcp_min(self):
6925         self._test_set_vlan_pcp(0)
6926
6927     # set_ip_dscp
6928     def _test_set_ip_dscp(self, ip_dscp):
6929         header = ofproto.OXM_OF_IP_DSCP
6930         match = OFPMatch()
6931         match.set_ip_dscp(ip_dscp)
6932         self._test_serialize_and_parser(match, header, ip_dscp)
6933
6934     def test_set_ip_dscp_mid(self):
6935         self._test_set_ip_dscp(36)
6936
6937     def test_set_ip_dscp_max(self):
6938         self._test_set_ip_dscp(63)
6939
6940     def test_set_ip_dscp_min(self):
6941         self._test_set_ip_dscp(0)
6942
6943     # set_ip_ecn
6944     def _test_set_ip_ecn(self, ip_ecn):
6945         header = ofproto.OXM_OF_IP_ECN
6946         match = OFPMatch()
6947         match.set_ip_ecn(ip_ecn)
6948         self._test_serialize_and_parser(match, header, ip_ecn)
6949
6950     def test_set_ip_ecn_mid(self):
6951         self._test_set_ip_ecn(1)
6952
6953     def test_set_ip_ecn_max(self):
6954         self._test_set_ip_ecn(3)
6955
6956     def test_set_ip_ecn_min(self):
6957         self._test_set_ip_ecn(0)
6958
6959     # set_ip_proto
6960     def _test_set_ip_proto(self, ip_proto):
6961         header = ofproto.OXM_OF_IP_PROTO
6962         match = OFPMatch()
6963         match.set_ip_proto(ip_proto)
6964         self._test_serialize_and_parser(match, header, ip_proto)
6965
6966     def test_set_ip_proto_mid(self):
6967         self._test_set_ip_proto(6)
6968
6969     def test_set_ip_proto_max(self):
6970         self._test_set_ip_proto(0xff)
6971
6972     def test_set_ip_proto_min(self):
6973         self._test_set_ip_proto(0)
6974
6975     # set_ipv4_src
6976     def _test_set_ipv4_src(self, ip, mask=None):
6977         header = ofproto.OXM_OF_IPV4_SRC
6978         match = OFPMatch()
6979         ip = unpack('!I', socket.inet_aton(ip))[0]
6980         if mask is None:
6981             match.set_ipv4_src(ip)
6982         else:
6983             mask = unpack('!I', socket.inet_aton(mask))[0]
6984             if (mask + 1) >> 32 != 1:
6985                 header = ofproto.OXM_OF_IPV4_SRC_W
6986             match.set_ipv4_src_masked(ip, mask)
6987         self._test_serialize_and_parser(match, header, ip, mask)
6988
6989     def test_set_ipv4_src_mid(self):
6990         self._test_set_ipv4_src('192.168.196.250')
6991
6992     def test_set_ipv4_src_max(self):
6993         self._test_set_ipv4_src('255.255.255.255')
6994
6995     def test_set_ipv4_src_min(self):
6996         self._test_set_ipv4_src('0.0.0.0')
6997
6998     def test_set_ipv4_src_masked_mid(self):
6999         self._test_set_ipv4_src('192.168.196.250', '255.255.0.0')
7000
7001     def test_set_ipv4_src_masked_max(self):
7002         self._test_set_ipv4_src('192.168.196.250', '255.255.255.255')
7003
7004     def test_set_ipv4_src_masked_min(self):
7005         self._test_set_ipv4_src('192.168.196.250', '0.0.0.0')
7006
7007     # set_ipv4_dst
7008     def _test_set_ipv4_dst(self, ip, mask=None):
7009         header = ofproto.OXM_OF_IPV4_DST
7010         match = OFPMatch()
7011         ip = unpack('!I', socket.inet_aton(ip))[0]
7012         if mask is None:
7013             match.set_ipv4_dst(ip)
7014         else:
7015             mask = unpack('!I', socket.inet_aton(mask))[0]
7016             if (mask + 1) >> 32 != 1:
7017                 header = ofproto.OXM_OF_IPV4_DST_W
7018             match.set_ipv4_dst_masked(ip, mask)
7019         self._test_serialize_and_parser(match, header, ip, mask)
7020
7021     def test_set_ipv4_dst_mid(self):
7022         self._test_set_ipv4_dst('192.168.196.250')
7023
7024     def test_set_ipv4_dst_max(self):
7025         self._test_set_ipv4_dst('255.255.255.255')
7026
7027     def test_set_ipv4_dst_min(self):
7028         self._test_set_ipv4_dst('0.0.0.0')
7029
7030     def test_set_ipv4_dst_masked_mid(self):
7031         self._test_set_ipv4_dst('192.168.196.250', '255.255.0.0')
7032
7033     def test_set_ipv4_dst_masked_max(self):
7034         self._test_set_ipv4_dst('192.168.196.250', '255.255.255.255')
7035
7036     def test_set_ipv4_dst_masked_min(self):
7037         self._test_set_ipv4_dst('192.168.196.250', '0.0.0.0')
7038
7039     # set_tcp_src
7040     def _test_set_tcp_src(self, tcp_src):
7041         header = ofproto.OXM_OF_TCP_SRC
7042         match = OFPMatch()
7043         match.set_tcp_src(tcp_src)
7044         self._test_serialize_and_parser(match, header, tcp_src)
7045
7046     def test_set_tcp_src_mid(self):
7047         self._test_set_tcp_src(1103)
7048
7049     def test_set_tcp_src_max(self):
7050         self._test_set_tcp_src(0xffff)
7051
7052     def test_set_tcp_src_min(self):
7053         self._test_set_tcp_src(0)
7054
7055     # set_tcp_dst
7056     def _test_set_tcp_dst(self, tcp_dst):
7057         header = ofproto.OXM_OF_TCP_DST
7058         match = OFPMatch()
7059         match.set_tcp_dst(tcp_dst)
7060         self._test_serialize_and_parser(match, header, tcp_dst)
7061
7062     def test_set_tcp_dst_mid(self):
7063         self._test_set_tcp_dst(236)
7064
7065     def test_set_tcp_dst_max(self):
7066         self._test_set_tcp_dst(0xffff)
7067
7068     def test_set_tcp_dst_min(self):
7069         self._test_set_tcp_dst(0)
7070
7071     # set_udp_src
7072     def _test_set_udp_src(self, udp_src):
7073         header = ofproto.OXM_OF_UDP_SRC
7074         match = OFPMatch()
7075         match.set_udp_src(udp_src)
7076         self._test_serialize_and_parser(match, header, udp_src)
7077
7078     def test_set_udp_src_mid(self):
7079         self._test_set_udp_src(56617)
7080
7081     def test_set_udp_src_max(self):
7082         self._test_set_udp_src(0xffff)
7083
7084     def test_set_udp_src_min(self):
7085         self._test_set_udp_src(0)
7086
7087     # set_udp_dst
7088     def _test_set_udp_dst(self, udp_dst):
7089         header = ofproto.OXM_OF_UDP_DST
7090         match = OFPMatch()
7091         match.set_udp_dst(udp_dst)
7092         self._test_serialize_and_parser(match, header, udp_dst)
7093
7094     def test_set_udp_dst_mid(self):
7095         self._test_set_udp_dst(61278)
7096
7097     def test_set_udp_dst_max(self):
7098         self._test_set_udp_dst(0xffff)
7099
7100     def test_set_udp_dst_min(self):
7101         self._test_set_udp_dst(0)
7102
7103     # set_sctp_src
7104     def _test_set_sctp_src(self, sctp_src):
7105         header = ofproto.OXM_OF_SCTP_SRC
7106         match = OFPMatch()
7107         match.set_sctp_src(sctp_src)
7108         self._test_serialize_and_parser(match, header, sctp_src)
7109
7110     def test_set_sctp_src_mid(self):
7111         self._test_set_sctp_src(9999)
7112
7113     def test_set_sctp_src_max(self):
7114         self._test_set_sctp_src(0xffff)
7115
7116     def test_set_sctp_src_min(self):
7117         self._test_set_sctp_src(0)
7118
7119     # set_sctp_dst
7120     def _test_set_sctp_dst(self, sctp_dst):
7121         header = ofproto.OXM_OF_SCTP_DST
7122         match = OFPMatch()
7123         match.set_sctp_dst(sctp_dst)
7124         self._test_serialize_and_parser(match, header, sctp_dst)
7125
7126     def test_set_sctp_dst_mid(self):
7127         self._test_set_sctp_dst(1234)
7128
7129     def test_set_sctp_dst_max(self):
7130         self._test_set_sctp_dst(0xffff)
7131
7132     def test_set_sctp_dst_min(self):
7133         self._test_set_sctp_dst(0)
7134
7135     # set_icmpv4_type
7136     def _test_set_icmpv4_type(self, icmpv4_type):
7137         header = ofproto.OXM_OF_ICMPV4_TYPE
7138         match = OFPMatch()
7139         match.set_icmpv4_type(icmpv4_type)
7140         self._test_serialize_and_parser(match, header, icmpv4_type)
7141
7142     def test_set_icmpv4_type_mid(self):
7143         self._test_set_icmpv4_type(8)
7144
7145     def test_set_icmpv4_type_max(self):
7146         self._test_set_icmpv4_type(0xff)
7147
7148     def test_set_icmpv4_type_min(self):
7149         self._test_set_icmpv4_type(0)
7150
7151     # set_icmpv4_code
7152     def _test_set_icmpv4_code(self, icmpv4_code):
7153         header = ofproto.OXM_OF_ICMPV4_CODE
7154         match = OFPMatch()
7155         match.set_icmpv4_code(icmpv4_code)
7156         self._test_serialize_and_parser(match, header, icmpv4_code)
7157
7158     def test_set_icmpv4_code_mid(self):
7159         self._test_set_icmpv4_code(1)
7160
7161     def test_set_icmpv4_code_max(self):
7162         self._test_set_icmpv4_code(0xff)
7163
7164     def test_set_icmpv4_code_min(self):
7165         self._test_set_icmpv4_code(0)
7166
7167     # set_arp_opcode
7168     def _test_set_arp_opcode(self, arp_op):
7169         header = ofproto.OXM_OF_ARP_OP
7170         match = OFPMatch()
7171         match.set_arp_opcode(arp_op)
7172         self._test_serialize_and_parser(match, header, arp_op)
7173
7174     def test_set_arp_opcode_mid(self):
7175         self._test_set_arp_opcode(1)
7176
7177     def test_set_arp_opcode_max(self):
7178         self._test_set_arp_opcode(0xffff)
7179
7180     def test_set_arp_opcode_min(self):
7181         self._test_set_arp_opcode(0)
7182
7183     # set_arp_spa
7184     def _test_set_arp_spa(self, ip, mask=None):
7185         header = ofproto.OXM_OF_ARP_SPA
7186         match = OFPMatch()
7187         ip = unpack('!I', socket.inet_aton(ip))[0]
7188         if mask is None:
7189             match.set_arp_spa(ip)
7190         else:
7191             mask = unpack('!I', socket.inet_aton(mask))[0]
7192             if (mask + 1) >> 32 != 1:
7193                 header = ofproto.OXM_OF_ARP_SPA_W
7194             match.set_arp_spa_masked(ip, mask)
7195         self._test_serialize_and_parser(match, header, ip, mask)
7196
7197     def test_set_arp_spa_mid(self):
7198         self._test_set_arp_spa('192.168.227.57')
7199
7200     def test_set_arp_spa_max(self):
7201         self._test_set_arp_spa('255.255.255.255')
7202
7203     def test_set_arp_spa_min(self):
7204         self._test_set_arp_spa('0.0.0.0')
7205
7206     def test_set_arp_spa_masked_mid(self):
7207         self._test_set_arp_spa('192.168.227.57', '255.255.0.0')
7208
7209     def test_set_arp_spa_masked_max(self):
7210         self._test_set_arp_spa('192.168.227.57', '255.255.255.255')
7211
7212     def test_set_arp_spa_masked_min(self):
7213         self._test_set_arp_spa('192.168.227.57', '0.0.0.0')
7214
7215     # set_arp_tpa
7216     def _test_set_arp_tpa(self, ip, mask=None):
7217         header = ofproto.OXM_OF_ARP_TPA
7218         match = OFPMatch()
7219         ip = unpack('!I', socket.inet_aton(ip))[0]
7220         if mask is None:
7221             match.set_arp_tpa(ip)
7222         else:
7223             mask = unpack('!I', socket.inet_aton(mask))[0]
7224             if (mask + 1) >> 32 != 1:
7225                 header = ofproto.OXM_OF_ARP_TPA_W
7226             match.set_arp_tpa_masked(ip, mask)
7227         self._test_serialize_and_parser(match, header, ip, mask)
7228
7229     def test_set_arp_tpa_mid(self):
7230         self._test_set_arp_tpa('192.168.227.57')
7231
7232     def test_set_arp_tpa_max(self):
7233         self._test_set_arp_tpa('255.255.255.255')
7234
7235     def test_set_arp_tpa_min(self):
7236         self._test_set_arp_tpa('0.0.0.0')
7237
7238     def test_set_arp_tpa_masked_mid(self):
7239         self._test_set_arp_tpa('192.168.227.57', '255.255.0.0')
7240
7241     def test_set_arp_tpa_masked_max(self):
7242         self._test_set_arp_tpa('192.168.227.57', '255.255.255.255')
7243
7244     def test_set_arp_tpa_masked_min(self):
7245         self._test_set_arp_tpa('192.168.227.57', '0.0.0.0')
7246
7247     # set_arp_sha
7248     def _test_set_arp_sha(self, arp_sha, mask=None):
7249         header = ofproto.OXM_OF_ARP_SHA
7250         match = OFPMatch()
7251         arp_sha = mac.haddr_to_bin(arp_sha)
7252         if mask is None:
7253             match.set_arp_sha(arp_sha)
7254         else:
7255             header = ofproto.OXM_OF_ARP_SHA_W
7256             mask = mac.haddr_to_bin(mask)
7257             match.set_arp_sha_masked(arp_sha, mask)
7258             arp_sha = mac.haddr_bitand(arp_sha, mask)
7259         self._test_serialize_and_parser(match, header, arp_sha, mask)
7260
7261     def test_set_arp_sha_mid(self):
7262         self._test_set_arp_sha('3e:ec:13:9b:f3:0b')
7263
7264     def test_set_arp_sha_max(self):
7265         self._test_set_arp_sha('ff:ff:ff:ff:ff:ff')
7266
7267     def test_set_arp_sha_min(self):
7268         self._test_set_arp_sha('00:00:00:00:00:00')
7269
7270     def test_set_arp_sha_masked_mid(self):
7271         self._test_set_arp_sha('3e:ec:13:9b:f3:0b', 'ff:ff:ff:00:00:00')
7272
7273     def test_set_arp_sha_masked_max(self):
7274         self._test_set_arp_sha('3e:ec:13:9b:f3:0b', 'ff:ff:ff:ff:ff:ff')
7275
7276     def test_set_arp_sha_masked_min(self):
7277         self._test_set_arp_sha('3e:ec:13:9b:f3:0b', '00:00:00:00:00:00')
7278
7279     # set_arp_tha
7280     def _test_set_arp_tha(self, arp_tha, mask=None):
7281         header = ofproto.OXM_OF_ARP_THA
7282         match = OFPMatch()
7283         arp_tha = mac.haddr_to_bin(arp_tha)
7284         if mask is None:
7285             match.set_arp_tha(arp_tha)
7286         else:
7287             header = ofproto.OXM_OF_ARP_THA_W
7288             mask = mac.haddr_to_bin(mask)
7289             match.set_arp_tha_masked(arp_tha, mask)
7290             arp_tha = mac.haddr_bitand(arp_tha, mask)
7291         self._test_serialize_and_parser(match, header, arp_tha, mask)
7292
7293     def test_set_arp_tha_mid(self):
7294         self._test_set_arp_tha('83:6c:21:52:49:68')
7295
7296     def test_set_arp_tha_max(self):
7297         self._test_set_arp_tha('ff:ff:ff:ff:ff:ff')
7298
7299     def test_set_arp_tha_min(self):
7300         self._test_set_arp_tha('00:00:00:00:00:00')
7301
7302     def test_set_arp_tha_masked_mid(self):
7303         self._test_set_arp_tha('83:6c:21:52:49:68', 'ff:ff:ff:00:00:00')
7304
7305     def test_set_arp_tha_masked_max(self):
7306         self._test_set_arp_tha('83:6c:21:52:49:68', 'ff:ff:ff:ff:ff:ff')
7307
7308     def test_set_arp_tha_masked_min(self):
7309         self._test_set_arp_tha('83:6c:21:52:49:68', '00:00:00:00:00:00')
7310
7311     # set_ipv6_src
7312     def _test_set_ipv6_src(self, ipv6, mask=None):
7313         header = ofproto.OXM_OF_IPV6_SRC
7314         match = OFPMatch()
7315         ipv6 = [int(x, 16) for x in ipv6.split(":")]
7316         if mask is None:
7317             match.set_ipv6_src(ipv6)
7318         else:
7319             header = ofproto.OXM_OF_IPV6_SRC_W
7320             mask = [int(x, 16) for x in mask.split(":")]
7321             match.set_ipv6_src_masked(ipv6, mask)
7322             ipv6 = [x & y for (x, y) in zip(ipv6, mask)]
7323         self._test_serialize_and_parser(match, header, ipv6, mask)
7324
7325     def test_set_ipv6_src_mid(self):
7326         ipv6 = '2001:db8:bd05:1d2:288a:1fc0:1:10ee'
7327         self._test_set_ipv6_src(ipv6)
7328
7329     def test_set_ipv6_src_max(self):
7330         ipv6 = 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'
7331         self._test_set_ipv6_src(ipv6)
7332
7333     def test_set_ipv6_src_min(self):
7334         ipv6 = '0:0:0:0:0:0:0:0'
7335         self._test_set_ipv6_src(ipv6)
7336
7337     def test_set_ipv6_src_masked_mid(self):
7338         ipv6 = '2001:db8:bd05:1d2:288a:1fc0:1:10ee'
7339         mask = 'ffff:ffff:ffff:ffff:0:0:0:0'
7340         self._test_set_ipv6_src(ipv6, mask)
7341
7342     def test_set_ipv6_src_masked_max(self):
7343         ipv6 = '2001:db8:bd05:1d2:288a:1fc0:1:10ee'
7344         mask = 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'
7345         self._test_set_ipv6_src(ipv6, mask)
7346
7347     def test_set_ipv6_src_masked_min(self):
7348         ipv6 = '2001:db8:bd05:1d2:288a:1fc0:1:10ee'
7349         mask = '0:0:0:0:0:0:0:0'
7350         self._test_set_ipv6_src(ipv6, mask)
7351
7352     # set_ipv6_dst
7353     def _test_set_ipv6_dst(self, ipv6, mask=None):
7354         header = ofproto.OXM_OF_IPV6_DST
7355         match = OFPMatch()
7356         ipv6 = [int(x, 16) for x in ipv6.split(":")]
7357         if mask is None:
7358             match.set_ipv6_dst(ipv6)
7359         else:
7360             header = ofproto.OXM_OF_IPV6_DST_W
7361             mask = [int(x, 16) for x in mask.split(":")]
7362             match.set_ipv6_dst_masked(ipv6, mask)
7363             ipv6 = [x & y for (x, y) in zip(ipv6, mask)]
7364         self._test_serialize_and_parser(match, header, ipv6, mask)
7365
7366     def test_set_ipv6_dst_mid(self):
7367         ipv6 = 'e9e8:9ea5:7d67:82cc:ca54:1fc0:2d24:f038'
7368         self._test_set_ipv6_dst(ipv6)
7369
7370     def test_set_ipv6_dst_max(self):
7371         ipv6 = ':'.join(['ffff'] * 8)
7372         self._test_set_ipv6_dst(ipv6)
7373
7374     def test_set_ipv6_dst_min(self):
7375         ipv6 = ':'.join(['0'] * 8)
7376         self._test_set_ipv6_dst(ipv6)
7377
7378     def test_set_ipv6_dst_mask_mid(self):
7379         ipv6 = 'e9e8:9ea5:7d67:82cc:ca54:1fc0:2d24:f038'
7380         mask = ':'.join(['ffff'] * 4 + ['0'] * 4)
7381         self._test_set_ipv6_dst(ipv6, mask)
7382
7383     def test_set_ipv6_dst_mask_max(self):
7384         ipv6 = 'e9e8:9ea5:7d67:82cc:ca54:1fc0:2d24:f038'
7385         mask = ':'.join(['ffff'] * 8)
7386         self._test_set_ipv6_dst(ipv6, mask)
7387
7388     def test_set_ipv6_dst_mask_min(self):
7389         ipv6 = 'e9e8:9ea5:7d67:82cc:ca54:1fc0:2d24:f038'
7390         mask = ':'.join(['0'] * 8)
7391         self._test_set_ipv6_dst(ipv6, mask)
7392
7393     # set_ipv6_flabel
7394     def _test_set_ipv6_flabel(self, flabel, mask=None):
7395         header = ofproto.OXM_OF_IPV6_FLABEL
7396         match = OFPMatch()
7397         if mask is None:
7398             match.set_ipv6_flabel(flabel)
7399         else:
7400             header = ofproto.OXM_OF_IPV6_FLABEL_W
7401             match.set_ipv6_flabel_masked(flabel, mask)
7402         self._test_serialize_and_parser(match, header, flabel, mask)
7403
7404     def test_set_ipv6_flabel_mid(self):
7405         self._test_set_ipv6_flabel(0xc5384)
7406
7407     def test_set_ipv6_flabel_max(self):
7408         self._test_set_ipv6_flabel(0xfffff)
7409
7410     def test_set_ipv6_flabel_min(self):
7411         self._test_set_ipv6_flabel(0)
7412
7413     def test_set_ipv6_flabel_masked_mid(self):
7414         self._test_set_ipv6_flabel(0xc5384, 0xfff00)
7415
7416     def test_set_ipv6_flabel_masked_max(self):
7417         self._test_set_ipv6_flabel(0xc5384, 0xfffff)
7418
7419     def test_set_ipv6_flabel_masked_min(self):
7420         self._test_set_ipv6_flabel(0xc5384, 0)
7421
7422     # set_icmpv6_type
7423     def _test_set_icmpv6_type(self, icmpv6_type):
7424         header = ofproto.OXM_OF_ICMPV6_TYPE
7425         match = OFPMatch()
7426         match.set_icmpv6_type(icmpv6_type)
7427         self._test_serialize_and_parser(match, header, icmpv6_type)
7428
7429     def test_set_icmpv6_type_mid(self):
7430         self._test_set_icmpv6_type(129)
7431
7432     def test_set_icmpv6_type_max(self):
7433         self._test_set_icmpv6_type(0xff)
7434
7435     def test_set_icmpv6_type_min(self):
7436         self._test_set_icmpv6_type(0)
7437
7438     # set_icmpv6_code
7439     def _test_set_icmpv6_code(self, icmpv6_code):
7440         header = ofproto.OXM_OF_ICMPV6_CODE
7441         match = OFPMatch()
7442         match.set_icmpv6_code(icmpv6_code)
7443         self._test_serialize_and_parser(match, header, icmpv6_code)
7444
7445     def test_set_icmpv6_code_mid(self):
7446         self._test_set_icmpv6_code(1)
7447
7448     def test_set_icmpv6_code_max(self):
7449         self._test_set_icmpv6_code(0xff)
7450
7451     def test_set_icmpv6_code_min(self):
7452         self._test_set_icmpv6_code(0)
7453
7454     # set_ipv6_nd_target
7455     def _test_set_ipv6_nd_target(self, ipv6):
7456         header = ofproto.OXM_OF_IPV6_ND_TARGET
7457         match = OFPMatch()
7458         ipv6 = [int(x, 16) for x in ipv6.split(":")]
7459         match.set_ipv6_nd_target(ipv6)
7460         self._test_serialize_and_parser(match, header, ipv6)
7461
7462     def test_set_ipv6_nd_target_mid(self):
7463         ip = '5420:db3f:921b:3e33:2791:98f:dd7f:2e19'
7464         self._test_set_ipv6_nd_target(ip)
7465
7466     def test_set_ipv6_nd_target_max(self):
7467         ip = ':'.join(['ffff'] * 8)
7468         self._test_set_ipv6_nd_target(ip)
7469
7470     def test_set_ipv6_nd_target_min(self):
7471         ip = ':'.join(['0'] * 8)
7472         self._test_set_ipv6_nd_target(ip)
7473
7474     # set_ipv6_nd_sll
7475     def _test_set_ipv6_nd_sll(self, nd_sll):
7476         header = ofproto.OXM_OF_IPV6_ND_SLL
7477         match = OFPMatch()
7478         nd_sll = mac.haddr_to_bin(nd_sll)
7479         match.set_ipv6_nd_sll(nd_sll)
7480         self._test_serialize_and_parser(match, header, nd_sll)
7481
7482     def test_set_ipv6_nd_sll_mid(self):
7483         self._test_set_ipv6_nd_sll('93:6d:d0:d4:e8:36')
7484
7485     def test_set_ipv6_nd_sll_max(self):
7486         self._test_set_ipv6_nd_sll('ff:ff:ff:ff:ff:ff')
7487
7488     def test_set_ipv6_nd_sll_min(self):
7489         self._test_set_ipv6_nd_sll('00:00:00:00:00:00')
7490
7491     # set_ipv6_nd_tll
7492     def _test_set_ipv6_nd_tll(self, nd_tll):
7493         header = ofproto.OXM_OF_IPV6_ND_TLL
7494         match = OFPMatch()
7495         nd_tll = mac.haddr_to_bin(nd_tll)
7496         match.set_ipv6_nd_tll(nd_tll)
7497         self._test_serialize_and_parser(match, header, nd_tll)
7498
7499     def test_set_ipv6_nd_tll_mid(self):
7500         self._test_set_ipv6_nd_tll('18:f6:66:b6:f1:b3')
7501
7502     def test_set_ipv6_nd_tll_max(self):
7503         self._test_set_ipv6_nd_tll('ff:ff:ff:ff:ff:ff')
7504
7505     def test_set_ipv6_nd_tll_min(self):
7506         self._test_set_ipv6_nd_tll('00:00:00:00:00:00')
7507
7508     # set_mpls_label
7509     def _test_set_mpls_label(self, mpls_label):
7510         header = ofproto.OXM_OF_MPLS_LABEL
7511         match = OFPMatch()
7512         match.set_mpls_label(mpls_label)
7513         self._test_serialize_and_parser(match, header, mpls_label)
7514
7515     def test_set_mpls_label_mid(self):
7516         self._test_set_mpls_label(2144)
7517
7518     def test_set_mpls_label_max(self):
7519         self._test_set_mpls_label(0xfffff)
7520
7521     def test_set_mpls_label_min(self):
7522         self._test_set_mpls_label(0)
7523
7524     # set_mpls_tc
7525     def _test_set_mpls_tc(self, mpls_tc):
7526         header = ofproto.OXM_OF_MPLS_TC
7527         match = OFPMatch()
7528         match.set_mpls_tc(mpls_tc)
7529         self._test_serialize_and_parser(match, header, mpls_tc)
7530
7531     def test_set_mpls_tc_mid(self):
7532         self._test_set_mpls_tc(3)
7533
7534     def test_set_mpls_tc_max(self):
7535         self._test_set_mpls_tc(7)
7536
7537     def test_set_mpls_tc_min(self):
7538         self._test_set_mpls_tc(0)
7539
7540
7541 class TestOFPMatchField(unittest.TestCase):
7542     """ Test case for ofproto_v1_2_parser.OFPMatchField
7543     """
7544
7545     def test_init_hasmask_true(self):
7546         header = 0x0100
7547
7548         res = OFPMatchField(header)
7549
7550         eq_(res.header, header)
7551         eq_(res.n_bytes, (header & 0xff) // 2)
7552         eq_(res.length, 0)
7553
7554     def test_init_hasmask_false(self):
7555         header = 0x0000
7556
7557         res = OFPMatchField(header)
7558
7559         eq_(res.header, header)
7560         eq_(res.n_bytes, header & 0xff)
7561         eq_(res.length, 0)