diff --git a/ryu/lib/pack_utils.py b/ryu/lib/pack_utils.py new file mode 100644 index 00000000..a84d14a7 --- /dev/null +++ b/ryu/lib/pack_utils.py @@ -0,0 +1,32 @@ +# Copyright (C) 2011, 2012 Nippon Telegraph and Telephone Corporation. +# Copyright (C) 2011 Isaku Yamahata +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import struct + + +def msg_pack_into(fmt, buf, offset, *args): + if len(buf) < offset: + buf += bytearray(offset - len(buf)) + + if len(buf) == offset: + buf += struct.pack(fmt, *args) + return + + needed_len = offset + struct.calcsize(fmt) + if len(buf) < needed_len: + buf += bytearray(needed_len - len(buf)) + + struct.pack_into(fmt, buf, offset, *args) diff --git a/ryu/lib/packet/bgp.py b/ryu/lib/packet/bgp.py index 12808c9d..2f507f0c 100644 --- a/ryu/lib/packet/bgp.py +++ b/ryu/lib/packet/bgp.py @@ -29,13 +29,13 @@ import copy import netaddr import numbers -from ryu.ofproto.ofproto_parser import msg_pack_into from ryu.lib.stringify import StringifyMixin from ryu.lib.packet import afi as addr_family from ryu.lib.packet import safi as subaddr_family from ryu.lib.packet import packet_base from ryu.lib.packet import stream_parser from ryu.lib import addrconv +from ryu.lib.pack_utils import msg_pack_into BGP_MSG_OPEN = 1 BGP_MSG_UPDATE = 2 diff --git a/ryu/ofproto/nx_actions.py b/ryu/ofproto/nx_actions.py index 0bc4a1bd..b097a232 100644 --- a/ryu/ofproto/nx_actions.py +++ b/ryu/ofproto/nx_actions.py @@ -20,7 +20,7 @@ from ryu import utils from ryu.lib import type_desc from ryu.ofproto import nicira_ext from ryu.ofproto import ofproto_common -from ryu.ofproto.ofproto_parser import msg_pack_into +from ryu.lib.pack_utils import msg_pack_into from ryu.ofproto.ofproto_parser import StringifyMixin diff --git a/ryu/ofproto/nx_match.py b/ryu/ofproto/nx_match.py index 5cfeb874..38ce02b5 100644 --- a/ryu/ofproto/nx_match.py +++ b/ryu/ofproto/nx_match.py @@ -20,7 +20,7 @@ import itertools from ryu import exception from ryu.lib import mac -from . import ofproto_parser +from ryu.lib.pack_utils import msg_pack_into from . import ofproto_v1_0 from . import inet @@ -440,7 +440,7 @@ class MFField(object): return cls(header, value, mask) def _put(self, buf, offset, value): - ofproto_parser.msg_pack_into(self.pack_str, buf, offset, value) + msg_pack_into(self.pack_str, buf, offset, value) return self.n_bytes def putw(self, buf, offset, value, mask): @@ -459,8 +459,7 @@ class MFField(object): return self.putw(buf, offset, value, mask) def _putv6(self, buf, offset, value): - ofproto_parser.msg_pack_into(self.pack_str, buf, offset, - *value) + msg_pack_into(self.pack_str, buf, offset, *value) return self.n_bytes def putv6(self, buf, offset, value, mask): @@ -1092,7 +1091,7 @@ def serialize_nxm_match(rule, buf, offset): # Pad pad_len = round_up(offset) - offset - ofproto_parser.msg_pack_into("%dx" % pad_len, buf, offset) + msg_pack_into("%dx" % pad_len, buf, offset) # The returned length, the match_len, does not include the pad return offset - old_offset @@ -1146,6 +1145,6 @@ class NXMatch(object): self.hasmask(), self.length())) def put_header(self, buf, offset): - ofproto_parser.msg_pack_into(ofproto_v1_0.NXM_HEADER_PACK_STRING, - buf, offset, self.header) + msg_pack_into(ofproto_v1_0.NXM_HEADER_PACK_STRING, + buf, offset, self.header) return struct.calcsize(ofproto_v1_0.NXM_HEADER_PACK_STRING) diff --git a/ryu/ofproto/ofproto_parser.py b/ryu/ofproto/ofproto_parser.py index b883addd..9625cd8b 100644 --- a/ryu/ofproto/ofproto_parser.py +++ b/ryu/ofproto/ofproto_parser.py @@ -212,21 +212,6 @@ class MsgInMsgBase(MsgBase): **additional_args) -def msg_pack_into(fmt, buf, offset, *args): - if len(buf) < offset: - buf += bytearray(offset - len(buf)) - - if len(buf) == offset: - buf += struct.pack(fmt, *args) - return - - needed_len = offset + struct.calcsize(fmt) - if len(buf) < needed_len: - buf += bytearray(needed_len - len(buf)) - - struct.pack_into(fmt, buf, offset, *args) - - def namedtuple(typename, fields, **kwargs): class _namedtuple(StringifyMixin, collections.namedtuple(typename, fields, **kwargs)): diff --git a/ryu/ofproto/ofproto_v1_0_parser.py b/ryu/ofproto/ofproto_v1_0_parser.py index 5d5044ca..477082a3 100644 --- a/ryu/ofproto/ofproto_v1_0_parser.py +++ b/ryu/ofproto/ofproto_v1_0_parser.py @@ -21,9 +21,10 @@ Decoder/Encoder implementations of OpenFlow 1.0. import struct import binascii -from ofproto_parser import StringifyMixin, MsgBase, msg_pack_into, msg_str_attr +from ofproto_parser import StringifyMixin, MsgBase, msg_str_attr from ryu.lib import addrconv from ryu.lib import mac +from ryu.lib.pack_utils import msg_pack_into from . import ofproto_common from . import ofproto_parser from . import ofproto_v1_0 as ofproto diff --git a/ryu/ofproto/ofproto_v1_2_parser.py b/ryu/ofproto/ofproto_v1_2_parser.py index 20f3b572..7d00dc42 100644 --- a/ryu/ofproto/ofproto_v1_2_parser.py +++ b/ryu/ofproto/ofproto_v1_2_parser.py @@ -23,8 +23,9 @@ import itertools from ryu.lib import addrconv from ryu.lib import mac +from ryu.lib.pack_utils import msg_pack_into from ryu import utils -from ofproto_parser import StringifyMixin, MsgBase, msg_pack_into, msg_str_attr +from ofproto_parser import StringifyMixin, MsgBase, msg_str_attr from . import ether from . import ofproto_parser from . import ofproto_v1_2 as ofproto @@ -1082,7 +1083,7 @@ class OFPInstructionActions(OFPInstruction): self.len = action_offset - offset pad_len = utils.round_up(self.len, 8) - self.len - ofproto_parser.msg_pack_into("%dx" % pad_len, buf, action_offset) + msg_pack_into("%dx" % pad_len, buf, action_offset) self.len += pad_len msg_pack_into(ofproto.OFP_INSTRUCTION_ACTIONS_PACK_STR, @@ -1508,7 +1509,7 @@ class OFPActionSetField(OFPAction): self.len = utils.round_up(4 + len_, 8) msg_pack_into('!HH', buf, offset, self.type, self.len) pad_len = self.len - (4 + len_) - ofproto_parser.msg_pack_into("%dx" % pad_len, buf, offset + 4 + len_) + msg_pack_into("%dx" % pad_len, buf, offset + 4 + len_) # XXX old api compat def serialize_old(self, buf, offset): @@ -1519,7 +1520,7 @@ class OFPActionSetField(OFPAction): msg_pack_into('!HH', buf, offset, self.type, self.len) self.field.serialize(buf, offset + 4) offset += len_ - ofproto_parser.msg_pack_into("%dx" % pad_len, buf, offset) + msg_pack_into("%dx" % pad_len, buf, offset) # XXX old api compat def _composed_with_old_api(self): @@ -3561,7 +3562,7 @@ class OFPMatch(StringifyMixin): self.length = length pad_len = utils.round_up(length, 8) - length - ofproto_parser.msg_pack_into("%dx" % pad_len, buf, field_offset) + msg_pack_into("%dx" % pad_len, buf, field_offset) return length + pad_len @@ -3766,7 +3767,7 @@ class OFPMatch(StringifyMixin): msg_pack_into('!HH', buf, offset, ofproto.OFPMT_OXM, length) pad_len = utils.round_up(length, 8) - length - ofproto_parser.msg_pack_into("%dx" % pad_len, buf, field_offset) + msg_pack_into("%dx" % pad_len, buf, field_offset) return length + pad_len @@ -4073,11 +4074,11 @@ class OFPMatchField(StringifyMixin): self.put(buf, offset, self.value) def _put_header(self, buf, offset): - ofproto_parser.msg_pack_into('!I', buf, offset, self.header) + msg_pack_into('!I', buf, offset, self.header) self.length = 4 def _put(self, buf, offset, value): - ofproto_parser.msg_pack_into(self.pack_str, buf, offset, value) + msg_pack_into(self.pack_str, buf, offset, value) self.length += self.n_bytes def put_w(self, buf, offset, value, mask): @@ -4090,8 +4091,7 @@ class OFPMatchField(StringifyMixin): self._put(buf, offset + self.length, value) def _putv6(self, buf, offset, value): - ofproto_parser.msg_pack_into(self.pack_str, buf, offset, - *value) + msg_pack_into(self.pack_str, buf, offset, *value) self.length += self.n_bytes def putv6(self, buf, offset, value, mask=None): diff --git a/ryu/ofproto/ofproto_v1_3_parser.py b/ryu/ofproto/ofproto_v1_3_parser.py index e9b4b293..ea69c8cd 100644 --- a/ryu/ofproto/ofproto_v1_3_parser.py +++ b/ryu/ofproto/ofproto_v1_3_parser.py @@ -45,8 +45,9 @@ import itertools from ryu.lib import addrconv from ryu.lib import mac +from ryu.lib.pack_utils import msg_pack_into from ryu import utils -from ofproto_parser import StringifyMixin, MsgBase, msg_pack_into, msg_str_attr +from ofproto_parser import StringifyMixin, MsgBase, msg_str_attr from . import ether from . import nicira_ext from . import ofproto_parser @@ -998,7 +999,7 @@ class OFPMatch(StringifyMixin): self.length = length pad_len = utils.round_up(length, 8) - length - ofproto_parser.msg_pack_into("%dx" % pad_len, buf, field_offset) + msg_pack_into("%dx" % pad_len, buf, field_offset) return length + pad_len @@ -1231,7 +1232,7 @@ class OFPMatch(StringifyMixin): msg_pack_into('!HH', buf, offset, ofproto.OFPMT_OXM, length) pad_len = utils.round_up(length, 8) - length - ofproto_parser.msg_pack_into("%dx" % pad_len, buf, field_offset) + msg_pack_into("%dx" % pad_len, buf, field_offset) return length + pad_len @@ -1570,11 +1571,11 @@ class OFPMatchField(StringifyMixin): self.put(buf, offset, self.value) def _put_header(self, buf, offset): - ofproto_parser.msg_pack_into('!I', buf, offset, self.header) + msg_pack_into('!I', buf, offset, self.header) self.length = 4 def _put(self, buf, offset, value): - ofproto_parser.msg_pack_into(self.pack_str, buf, offset, value) + msg_pack_into(self.pack_str, buf, offset, value) self.length += self.n_bytes def put_w(self, buf, offset, value, mask): @@ -1587,8 +1588,7 @@ class OFPMatchField(StringifyMixin): self._put(buf, offset + self.length, value) def _putv6(self, buf, offset, value): - ofproto_parser.msg_pack_into(self.pack_str, buf, offset, - *value) + msg_pack_into(self.pack_str, buf, offset, *value) self.length += self.n_bytes def putv6(self, buf, offset, value, mask=None): @@ -2038,10 +2038,10 @@ class MTPbbIsid(OFPMatchField): return cls(header, value, mask) def _put(self, buf, offset, value): - ofproto_parser.msg_pack_into(self.pack_str, buf, offset, - (value >> 16) & 0xff, - (value >> 8) & 0xff, - (value >> 0) & 0xff) + msg_pack_into(self.pack_str, buf, offset, + (value >> 16) & 0xff, + (value >> 8) & 0xff, + (value >> 0) & 0xff) self.length += self.n_bytes @@ -2637,7 +2637,7 @@ class OFPInstructionActions(OFPInstruction): self.len = action_offset - offset pad_len = utils.round_up(self.len, 8) - self.len - ofproto_parser.msg_pack_into("%dx" % pad_len, buf, action_offset) + msg_pack_into("%dx" % pad_len, buf, action_offset) self.len += pad_len msg_pack_into(ofproto.OFP_INSTRUCTION_ACTIONS_PACK_STR, @@ -3092,7 +3092,7 @@ class OFPActionSetField(OFPAction): self.len = utils.round_up(4 + len_, 8) msg_pack_into('!HH', buf, offset, self.type, self.len) pad_len = self.len - (4 + len_) - ofproto_parser.msg_pack_into("%dx" % pad_len, buf, offset + 4 + len_) + msg_pack_into("%dx" % pad_len, buf, offset + 4 + len_) # XXX old api compat def serialize_old(self, buf, offset): @@ -3103,7 +3103,7 @@ class OFPActionSetField(OFPAction): msg_pack_into('!HH', buf, offset, self.type, self.len) self.field.serialize(buf, offset + 4) offset += len_ - ofproto_parser.msg_pack_into("%dx" % pad_len, buf, offset) + msg_pack_into("%dx" % pad_len, buf, offset) # XXX old api compat def _composed_with_old_api(self): @@ -5295,8 +5295,7 @@ class OFPTableFeaturePropExperimenter(OFPTableFeatureProp): # data if len(self.data): - ofproto_parser.msg_pack_into('!%dI' % len(self.data), - buf, len(buf), *self.data) + msg_pack_into('!%dI' % len(self.data), buf, len(buf), *self.data) return buf diff --git a/ryu/ofproto/ofproto_v1_4_parser.py b/ryu/ofproto/ofproto_v1_4_parser.py index 1363908d..c4c9054c 100644 --- a/ryu/ofproto/ofproto_v1_4_parser.py +++ b/ryu/ofproto/ofproto_v1_4_parser.py @@ -23,9 +23,9 @@ import itertools from ryu.lib import addrconv from ryu.lib import mac +from ryu.lib.pack_utils import msg_pack_into from ryu import utils -from ofproto_parser import (StringifyMixin, MsgBase, MsgInMsgBase, - msg_pack_into, msg_str_attr) +from ofproto_parser import StringifyMixin, MsgBase, MsgInMsgBase, msg_str_attr from . import ether from . import ofproto_parser from . import ofproto_common @@ -769,7 +769,7 @@ class OFPMatch(StringifyMixin): self.length = length pad_len = utils.round_up(length, 8) - length - ofproto_parser.msg_pack_into("%dx" % pad_len, buf, field_offset) + msg_pack_into("%dx" % pad_len, buf, field_offset) return length + pad_len @@ -880,7 +880,7 @@ class OFPPropCommonExperimenter4ByteData(StringifyMixin): # Pad pad_len = utils.round_up(self.length, 8) - self.length - ofproto_parser.msg_pack_into("%dx" % pad_len, buf, len(buf)) + msg_pack_into("%dx" % pad_len, buf, len(buf)) return buf @@ -1124,11 +1124,11 @@ class OFPMatchField(StringifyMixin): self.put(buf, offset, self.value) def _put_header(self, buf, offset): - ofproto_parser.msg_pack_into('!I', buf, offset, self.header) + msg_pack_into('!I', buf, offset, self.header) self.length = 4 def _put(self, buf, offset, value): - ofproto_parser.msg_pack_into(self.pack_str, buf, offset, value) + msg_pack_into(self.pack_str, buf, offset, value) self.length += self.n_bytes def put_w(self, buf, offset, value, mask): @@ -1141,8 +1141,7 @@ class OFPMatchField(StringifyMixin): self._put(buf, offset + self.length, value) def _putv6(self, buf, offset, value): - ofproto_parser.msg_pack_into(self.pack_str, buf, offset, - *value) + msg_pack_into(self.pack_str, buf, offset, *value) self.length += self.n_bytes def putv6(self, buf, offset, value, mask=None): @@ -1596,10 +1595,10 @@ class MTPbbIsid(OFPMatchField): return cls(header, value, mask) def _put(self, buf, offset, value): - ofproto_parser.msg_pack_into(self.pack_str, buf, offset, - (value >> 16) & 0xff, - (value >> 8) & 0xff, - (value >> 0) & 0xff) + msg_pack_into(self.pack_str, buf, offset, + (value >> 16) & 0xff, + (value >> 8) & 0xff, + (value >> 0) & 0xff) self.length += self.n_bytes @@ -2249,7 +2248,7 @@ class OFPTableFeatureProp(OFPPropBase): # Pad pad_len = utils.round_up(self.length, 8) - self.length - ofproto_parser.msg_pack_into("%dx" % pad_len, buf, len(buf)) + msg_pack_into("%dx" % pad_len, buf, len(buf)) return buf @@ -4943,7 +4942,7 @@ class OFPInstructionActions(OFPInstruction): self.len = action_offset - offset pad_len = utils.round_up(self.len, 8) - self.len - ofproto_parser.msg_pack_into("%dx" % pad_len, buf, action_offset) + msg_pack_into("%dx" % pad_len, buf, action_offset) self.len += pad_len msg_pack_into(ofproto.OFP_INSTRUCTION_ACTIONS_PACK_STR, @@ -5379,7 +5378,7 @@ class OFPActionSetField(OFPAction): self.len = utils.round_up(4 + len_, 8) msg_pack_into('!HH', buf, offset, self.type, self.len) pad_len = self.len - (4 + len_) - ofproto_parser.msg_pack_into("%dx" % pad_len, buf, offset + 4 + len_) + msg_pack_into("%dx" % pad_len, buf, offset + 4 + len_) def to_jsondict(self): return { @@ -6068,8 +6067,7 @@ class OFPBundleAddMsg(MsgInMsgBase): if len(self.properties) > 0: message_len = len(tail_buf) pad_len = utils.round_up(message_len, 8) - message_len - ofproto_parser.msg_pack_into("%dx" % pad_len, tail_buf, - message_len) + msg_pack_into("%dx" % pad_len, tail_buf, message_len) # Properties for p in self.properties: diff --git a/ryu/ofproto/ofproto_v1_5_parser.py b/ryu/ofproto/ofproto_v1_5_parser.py index 4bd9db6b..3c9d9983 100644 --- a/ryu/ofproto/ofproto_v1_5_parser.py +++ b/ryu/ofproto/ofproto_v1_5_parser.py @@ -23,9 +23,9 @@ import itertools from ryu.lib import addrconv from ryu.lib import mac +from ryu.lib.pack_utils import msg_pack_into from ryu import utils -from ofproto_parser import (StringifyMixin, MsgBase, MsgInMsgBase, - msg_pack_into, msg_str_attr) +from ofproto_parser import StringifyMixin, MsgBase, MsgInMsgBase, msg_str_attr from . import ether from . import ofproto_parser from . import ofproto_common @@ -769,7 +769,7 @@ class OFPMatch(StringifyMixin): self.length = length pad_len = utils.round_up(length, 8) - length - ofproto_parser.msg_pack_into("%dx" % pad_len, buf, field_offset) + msg_pack_into("%dx" % pad_len, buf, field_offset) return length + pad_len @@ -880,7 +880,7 @@ class OFPPropCommonExperimenter4ByteData(StringifyMixin): # Pad pad_len = utils.round_up(self.length, 8) - self.length - ofproto_parser.msg_pack_into("%dx" % pad_len, buf, len(buf)) + msg_pack_into("%dx" % pad_len, buf, len(buf)) return buf @@ -1124,11 +1124,11 @@ class OFPMatchField(StringifyMixin): self.put(buf, offset, self.value) def _put_header(self, buf, offset): - ofproto_parser.msg_pack_into('!I', buf, offset, self.header) + msg_pack_into('!I', buf, offset, self.header) self.length = 4 def _put(self, buf, offset, value): - ofproto_parser.msg_pack_into(self.pack_str, buf, offset, value) + msg_pack_into(self.pack_str, buf, offset, value) self.length += self.n_bytes def put_w(self, buf, offset, value, mask): @@ -1141,8 +1141,7 @@ class OFPMatchField(StringifyMixin): self._put(buf, offset + self.length, value) def _putv6(self, buf, offset, value): - ofproto_parser.msg_pack_into(self.pack_str, buf, offset, - *value) + msg_pack_into(self.pack_str, buf, offset, *value) self.length += self.n_bytes def putv6(self, buf, offset, value, mask=None): @@ -1596,10 +1595,10 @@ class MTPbbIsid(OFPMatchField): return cls(header, value, mask) def _put(self, buf, offset, value): - ofproto_parser.msg_pack_into(self.pack_str, buf, offset, - (value >> 16) & 0xff, - (value >> 8) & 0xff, - (value >> 0) & 0xff) + msg_pack_into(self.pack_str, buf, offset, + (value >> 16) & 0xff, + (value >> 8) & 0xff, + (value >> 0) & 0xff) self.length += self.n_bytes @@ -2249,7 +2248,7 @@ class OFPTableFeatureProp(OFPPropBase): # Pad pad_len = utils.round_up(self.length, 8) - self.length - ofproto_parser.msg_pack_into("%dx" % pad_len, buf, len(buf)) + msg_pack_into("%dx" % pad_len, buf, len(buf)) return buf @@ -4943,7 +4942,7 @@ class OFPInstructionActions(OFPInstruction): self.len = action_offset - offset pad_len = utils.round_up(self.len, 8) - self.len - ofproto_parser.msg_pack_into("%dx" % pad_len, buf, action_offset) + msg_pack_into("%dx" % pad_len, buf, action_offset) self.len += pad_len msg_pack_into(ofproto.OFP_INSTRUCTION_ACTIONS_PACK_STR, @@ -5348,7 +5347,7 @@ class OFPActionSetField(OFPAction): self.len = utils.round_up(4 + len_, 8) msg_pack_into('!HH', buf, offset, self.type, self.len) pad_len = self.len - (4 + len_) - ofproto_parser.msg_pack_into("%dx" % pad_len, buf, offset + 4 + len_) + msg_pack_into("%dx" % pad_len, buf, offset + 4 + len_) def to_jsondict(self): return { @@ -6067,8 +6066,7 @@ class OFPBundleAddMsg(MsgInMsgBase): if len(self.properties) > 0: message_len = len(tail_buf) pad_len = utils.round_up(message_len, 8) - message_len - ofproto_parser.msg_pack_into("%dx" % pad_len, tail_buf, - message_len) + msg_pack_into("%dx" % pad_len, tail_buf, message_len) # Properties for p in self.properties: diff --git a/ryu/ofproto/oxm_fields.py b/ryu/ofproto/oxm_fields.py index d8ea8fda..b7936b7d 100644 --- a/ryu/ofproto/oxm_fields.py +++ b/ryu/ofproto/oxm_fields.py @@ -63,7 +63,7 @@ import itertools import struct import ofproto_common -from ofproto_parser import msg_pack_into +from ryu.lib.pack_utils import msg_pack_into from ryu.lib import type_desc diff --git a/ryu/tests/unit/lib/test_pack_utils.py b/ryu/tests/unit/lib/test_pack_utils.py new file mode 100644 index 00000000..3bf476f9 --- /dev/null +++ b/ryu/tests/unit/lib/test_pack_utils.py @@ -0,0 +1,58 @@ +# Copyright (C) 2012 Nippon Telegraph and Telephone Corporation. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest +import struct + +from nose.tools import ok_, eq_ + +from ryu.lib import pack_utils + + +class TestMsgPackInto(unittest.TestCase): + """ Test case for msg_pack_into + """ + + def _test_msg_pack_into(self, offset_type='e'): + fmt = '!HH' + len_ = struct.calcsize(fmt) + buf = bytearray(len_) + offset = len_ + arg1 = 1 + arg2 = 2 + + if offset_type == 'l': + offset += 1 + elif offset_type == 'g': + offset -= 1 + + pack_utils.msg_pack_into(fmt, buf, offset, arg1, arg2) + + check_offset = len(buf) - len_ + res = struct.unpack_from(fmt, buffer(buf), check_offset) + + eq_(arg1, res[0]) + eq_(arg2, res[1]) + + return True + + def test_msg_pack_into(self): + ok_(self._test_msg_pack_into()) + + def test_msg_pack_into_less(self): + ok_(self._test_msg_pack_into('l')) + + def test_msg_pack_into_greater(self): + ok_(self._test_msg_pack_into('g')) diff --git a/ryu/tests/unit/ofproto/test_ofproto_parser.py b/ryu/tests/unit/ofproto/test_ofproto_parser.py index efc99e9a..e31cf201 100644 --- a/ryu/tests/unit/ofproto/test_ofproto_parser.py +++ b/ryu/tests/unit/ofproto/test_ofproto_parser.py @@ -223,43 +223,6 @@ class TestMsgBase(unittest.TestCase): ok_(self._test_serialize()) -class TestMsgPackInto(unittest.TestCase): - """ Test case for ofproto_parser.msg_pack_into - """ - - def _test_msg_pack_into(self, offset_type='e'): - fmt = '!HH' - len_ = struct.calcsize(fmt) - buf = bytearray(len_) - offset = len_ - arg1 = 1 - arg2 = 2 - - if offset_type == 'l': - offset += 1 - elif offset_type == 'g': - offset -= 1 - - ofproto_parser.msg_pack_into(fmt, buf, offset, arg1, arg2) - - check_offset = len(buf) - len_ - res = struct.unpack_from(fmt, buffer(buf), check_offset) - - eq_(arg1, res[0]) - eq_(arg2, res[1]) - - return True - - def test_msg_pack_into(self): - ok_(self._test_msg_pack_into()) - - def test_msg_pack_into_less(self): - ok_(self._test_msg_pack_into('l')) - - def test_msg_pack_into_greater(self): - ok_(self._test_msg_pack_into('g')) - - class TestMsgStrAttr(unittest.TestCase): """ Test case for ofproto_parser.msg_str_attr """ diff --git a/ryu/tests/unit/ofproto/test_parser_v12.py b/ryu/tests/unit/ofproto/test_parser_v12.py index 13caa526..1ea9b272 100644 --- a/ryu/tests/unit/ofproto/test_parser_v12.py +++ b/ryu/tests/unit/ofproto/test_parser_v12.py @@ -29,6 +29,7 @@ from ryu.ofproto import ether from ryu.ofproto.ofproto_parser import MsgBase from ryu import utils from ryu.lib import addrconv +from ryu.lib import pack_utils LOG = logging.getLogger('test_ofproto_v12') @@ -6673,12 +6674,11 @@ class TestOFPMatch(unittest.TestCase): def test_parse_unknown_field(self): buf = bytearray() - ofproto_parser.msg_pack_into('!HH', buf, 0, ofproto.OFPMT_OXM, - 4 + 6) + pack_utils.msg_pack_into('!HH', buf, 0, ofproto.OFPMT_OXM, 4 + 6) header = ofproto.oxm_tlv_header(36, 2) - ofproto_parser.msg_pack_into('!IH', buf, 4, header, 1) + pack_utils.msg_pack_into('!IH', buf, 4, header, 1) header = ofproto.OXM_OF_ETH_TYPE - ofproto_v1_2_parser.msg_pack_into('!IH', buf, 10, header, 1) + pack_utils.msg_pack_into('!IH', buf, 10, header, 1) match = OFPMatch() res = match.parser(str(buf), 0)