python3: Use bytes type for binary data

Signed-off-by: IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
IWAMOTO Toshihiro 2015-06-20 00:10:27 +09:00 committed by FUJITA Tomonori
parent c0590ea903
commit 75e8c58916
10 changed files with 75 additions and 75 deletions

View File

@ -511,8 +511,7 @@ class KeyedMD5(BFDAuth):
auth_hdr_bin = self.serialize_hdr()
auth_data_bin = struct.pack(self._PACK_STR, self.auth_key_id, 0,
self.seq, self.auth_key +
''.join(['\x00' *
(len(self.auth_key) - 16)]))
(b'\x00' * (len(self.auth_key) - 16)))
h = hashlib.md5()
h.update(bfd_bin + auth_hdr_bin + auth_data_bin)
@ -551,7 +550,7 @@ class KeyedMD5(BFDAuth):
auth_hdr_bin = self.serialize_hdr()
auth_data_bin = struct.pack(self._PACK_STR, self.auth_key_id, 0,
self.seq, auth_key +
''.join(['\x00' * (len(auth_key) - 16)]))
(b'\x00' * (len(auth_key) - 16)))
h = hashlib.md5()
h.update(bfd_bin + auth_hdr_bin + auth_data_bin)
@ -662,8 +661,7 @@ class KeyedSHA1(BFDAuth):
auth_hdr_bin = self.serialize_hdr()
auth_data_bin = struct.pack(self._PACK_STR, self.auth_key_id, 0,
self.seq, self.auth_key +
''.join(['\x00' *
(len(self.auth_key) - 20)]))
(b'\x00' * (len(self.auth_key) - 20)))
h = hashlib.sha1()
h.update(bfd_bin + auth_hdr_bin + auth_data_bin)
@ -702,7 +700,7 @@ class KeyedSHA1(BFDAuth):
auth_hdr_bin = self.serialize_hdr()
auth_data_bin = struct.pack(self._PACK_STR, self.auth_key_id, 0,
self.seq, auth_key +
''.join(['\x00' * (len(auth_key) - 20)]))
(b'\x00' * (len(auth_key) - 20)))
h = hashlib.sha1()
h.update(bfd_bin + auth_hdr_bin + auth_data_bin)

View File

@ -51,7 +51,7 @@ BGP_MSG_KEEPALIVE = 4
BGP_MSG_ROUTE_REFRESH = 5 # RFC 2918
_VERSION = 4
_MARKER = 16 * '\xff'
_MARKER = 16 * b'\xff'
BGP_OPT_CAPABILITY = 2 # RFC 5492

View File

@ -260,9 +260,9 @@ class cc_message(operation):
def __init__(self, md_lv=0, version=CFM_VERSION,
rdi=0, interval=_INTERVAL_1_SEC, seq_num=0, mep_id=1,
md_name_format=_MD_FMT_CHARACTER_STRING,
md_name_length=0, md_name="0",
md_name_length=0, md_name=b"0",
short_ma_name_format=_SHORT_MA_FMT_CHARACTER_STRING,
short_ma_name_length=0, short_ma_name="1",
short_ma_name_length=0, short_ma_name=b"1",
tlvs=None):
super(cc_message, self).__init__(md_lv, version, tlvs)
self._opcode = CFM_CC_MESSAGE
@ -285,7 +285,7 @@ class cc_message(operation):
(md_lv_version, opcode, flags, tlv_offset, seq_num, mep_id,
md_name_format) = struct.unpack_from(cls._PACK_STR, buf)
md_name_length = 0
md_name = ""
md_name = b""
md_lv = int(md_lv_version >> 5)
version = int(md_lv_version & 0x1f)
rdi = int(flags >> 7)
@ -310,9 +310,9 @@ class cc_message(operation):
# ascii to text
if md_name_format == cls._MD_FMT_DOMAIN_NAME_BASED_STRING or \
md_name_format == cls._MD_FMT_CHARACTER_STRING:
md_name = "".join(map(chr, md_name))
md_name = b"".join(map(six.int2byte, md_name))
if short_ma_name_format == cls._SHORT_MA_FMT_CHARACTER_STRING:
short_ma_name = "".join(map(chr, short_ma_name))
short_ma_name = b"".join(map(six.int2byte, short_ma_name))
return cls(md_lv, version, rdi, interval, seq_num, mep_id,
md_name_format, md_name_length,
md_name,
@ -925,7 +925,7 @@ class data_tlv(tlv):
_PACK_STR = '!BH'
_MIN_LEN = struct.calcsize(_PACK_STR)
def __init__(self, length=0, data_value=""
def __init__(self, length=0, data_value=b""
):
super(data_tlv, self).__init__(length)
self._type = CFM_DATA_TLV
@ -1171,9 +1171,9 @@ class organization_specific_tlv(tlv):
def __init__(self,
length=0,
oui="\x00\x00\x00",
oui=b"\x00\x00\x00",
subtype=0,
value=""
value=b""
):
super(organization_specific_tlv, self).__init__(length)
self._type = CFM_ORGANIZATION_SPECIFIC_TLV
@ -1184,7 +1184,7 @@ class organization_specific_tlv(tlv):
@classmethod
def parser(cls, buf):
(type_, length, oui, subtype) = struct.unpack_from(cls._PACK_STR, buf)
value = ""
value = b""
if length > cls._OUI_AND_SUBTYPE_LEN:
form = "%ds" % (length - cls._OUI_AND_SUBTYPE_LEN)
(value,) = struct.unpack_from(form, buf, cls._MIN_LEN)

View File

@ -149,7 +149,7 @@ class dhcp(packet_base.PacketBase):
def __init__(self, op, chaddr, options, htype=_HARDWARE_TYPE_ETHERNET,
hlen=0, hops=0, xid=None, secs=0, flags=0,
ciaddr='0.0.0.0', yiaddr='0.0.0.0', siaddr='0.0.0.0',
giaddr='0.0.0.0', sname='', boot_file=''):
giaddr='0.0.0.0', sname=b'', boot_file=b''):
super(dhcp, self).__init__()
self.op = op
self.htype = htype

View File

@ -206,7 +206,7 @@ class opt_header(header):
buf = bytearray(buf)
if self.data is None:
self.data = [option(type_=1, len_=4,
data='\x00\x00\x00\x00')]
data=b'\x00\x00\x00\x00')]
for opt in self.data:
buf.extend(opt.serialize())
return buf
@ -565,7 +565,7 @@ class auth(header):
_MIN_LEN = struct.calcsize(_PACK_STR)
def __init__(self, nxt=inet.IPPROTO_TCP, size=2, spi=0, seq=0,
data='\x00\x00\x00\x00'):
data=b'\x00\x00\x00\x00'):
super(auth, self).__init__(nxt)
assert data is not None
self.size = size

View File

@ -15,6 +15,8 @@
# limitations under the License.
import six
from ryu.lib import addrconv
@ -35,9 +37,9 @@ class IntDescr(TypeDescr):
return i
def from_user(self, i):
bin = ''
bin = b''
for x in range(self.size):
bin = chr(i & 255) + bin
bin = six.int2byte(i & 255) + bin
i //= 256
return bin

View File

@ -85,7 +85,7 @@ class TestBFD(unittest.TestCase):
+ '\x3c\xc1\x6a\x00\x69\x23'
# BFD Key chain {auth_key_id: auth_key/password}
self.auth_keys = {2: "secret"}
self.auth_keys = {2: b"secret"}
def tearDown(self):
pass

View File

@ -44,10 +44,10 @@ class Test_cfm(unittest.TestCase):
self.cc_message_mep_id = 4
self.cc_message_md_name_format = 4
self.cc_message_md_name_length = 0
self.cc_message_md_name = "hoge"
self.cc_message_md_name = b"hoge"
self.cc_message_short_ma_name_format = 2
self.cc_message_short_ma_name_length = 0
self.cc_message_short_ma_name = "pakeratta"
self.cc_message_short_ma_name = b"pakeratta"
self.cc_message_md_name_txfcf = 11
self.cc_message_md_name_rxfcb = 22
self.cc_message_md_name_txfcb = 33
@ -397,10 +397,10 @@ class Test_cc_message(unittest.TestCase):
self.mep_id = 2
self.md_name_format = cfm.cc_message._MD_FMT_CHARACTER_STRING
self.md_name_length = 3
self.md_name = "foo"
self.md_name = b"foo"
self.short_ma_name_format = 2
self.short_ma_name_length = 8
self.short_ma_name = "hogehoge"
self.short_ma_name = b"hogehoge"
self.tlvs = [
]
self.end_tlv = 0
@ -984,11 +984,11 @@ class Test_sender_id_tlv(unittest.TestCase):
self.length = 10
self.chassis_id_length = 1
self.chassis_id_subtype = 3
self.chassis_id = "\x0a"
self.chassis_id = b"\x0a"
self.ma_domain_length = 2
self.ma_domain = "\x04\x05"
self.ma_domain = b"\x04\x05"
self.ma_length = 3
self.ma = "\x01\x02\x03"
self.ma = b"\x01\x02\x03"
self.ins = cfm.sender_id_tlv(
self.length,
self.chassis_id_length,
@ -1212,7 +1212,7 @@ class Test_data_tlv(unittest.TestCase):
def setUp(self):
self._type = cfm.CFM_DATA_TLV
self.length = 3
self.data_value = "\x01\x02\x03"
self.data_value = b"\x01\x02\x03"
self.ins = cfm.data_tlv(
self.length,
self.data_value
@ -1495,9 +1495,9 @@ class Test_organization_specific_tlv(unittest.TestCase):
def setUp(self):
self._type = cfm.CFM_ORGANIZATION_SPECIFIC_TLV
self.length = 10
self.oui = "\xff\x12\x34"
self.oui = b"\xff\x12\x34"
self.subtype = 3
self.value = "\x01\x02\x0f\x0e\x0d\x0c"
self.value = b"\x01\x02\x0f\x0e\x0d\x0c"
self.ins = cfm.organization_specific_tlv(self.length,
self.oui,
self.subtype,
@ -1566,7 +1566,7 @@ class Test_organization_specific_tlv(unittest.TestCase):
str(buf))
eq_(res[0], cfm.CFM_ORGANIZATION_SPECIFIC_TLV)
eq_(res[1], 4)
eq_(res[2], "\x00\x00\x00")
eq_(res[2], b"\x00\x00\x00")
eq_(res[3], 0)
@ -1579,7 +1579,7 @@ class Test_reply_ingress_tlv(unittest.TestCase):
self.mac_address = 'aa:bb:cc:56:34:12'
self.port_id_length = 3
self.port_id_subtype = 2
self.port_id = "\x01\x04\x09"
self.port_id = b"\x01\x04\x09"
self.ins = cfm.reply_ingress_tlv(self.length, self.action,
self.mac_address,
self.port_id_length,
@ -1673,7 +1673,7 @@ class Test_reply_egress_tlv(unittest.TestCase):
self.mac_address = 'aa:bb:cc:56:34:12'
self.port_id_length = 3
self.port_id_subtype = 2
self.port_id = "\x01\x04\x09"
self.port_id = b"\x01\x04\x09"
self.ins = cfm.reply_egress_tlv(self.length,
self.action,
self.mac_address,

View File

@ -40,18 +40,18 @@ class Test_dhcp_offer(unittest.TestCase):
yiaddr = '192.168.20.20'
siaddr = '192.168.30.30'
giaddr = '192.168.40.40'
sname = 'abc'
boot_file = ''
sname = b'abc'
boot_file = b''
option_list = [
dhcp.option(dhcp.DHCP_MESSAGE_TYPE_OPT, '\x02', 1),
dhcp.option(dhcp.DHCP_SUBNET_MASK_OPT, '\xff\xff\xff\x00', 4),
dhcp.option(dhcp.DHCP_GATEWAY_ADDR_OPT, '\xc0\xa8\x0a\x09', 4),
dhcp.option(dhcp.DHCP_DNS_SERVER_ADDR_OPT, '\xc0\xa8\x0a\x09', 4),
dhcp.option(dhcp.DHCP_IP_ADDR_LEASE_TIME_OPT, '\x00\x03\xf4\x80', 4),
dhcp.option(dhcp.DHCP_RENEWAL_TIME_OPT, '\x00\x01\xfa\x40', 4),
dhcp.option(dhcp.DHCP_REBINDING_TIME_OPT, '\x00\x03\x75\xf0', 4),
dhcp.option(dhcp.DHCP_SERVER_IDENTIFIER_OPT, '\xc0\xa8\x0a\x09', 4)]
dhcp.option(dhcp.DHCP_MESSAGE_TYPE_OPT, b'\x02', 1),
dhcp.option(dhcp.DHCP_SUBNET_MASK_OPT, b'\xff\xff\xff\x00', 4),
dhcp.option(dhcp.DHCP_GATEWAY_ADDR_OPT, b'\xc0\xa8\x0a\x09', 4),
dhcp.option(dhcp.DHCP_DNS_SERVER_ADDR_OPT, b'\xc0\xa8\x0a\x09', 4),
dhcp.option(dhcp.DHCP_IP_ADDR_LEASE_TIME_OPT, b'\x00\x03\xf4\x80', 4),
dhcp.option(dhcp.DHCP_RENEWAL_TIME_OPT, b'\x00\x01\xfa\x40', 4),
dhcp.option(dhcp.DHCP_REBINDING_TIME_OPT, b'\x00\x03\x75\xf0', 4),
dhcp.option(dhcp.DHCP_SERVER_IDENTIFIER_OPT, b'\xc0\xa8\x0a\x09', 4)]
magic_cookie = '99.130.83.99'
options = dhcp.options(option_list=option_list, options_len=50,
magic_cookie=magic_cookie)
@ -61,24 +61,24 @@ class Test_dhcp_offer(unittest.TestCase):
ciaddr=ciaddr, yiaddr=yiaddr, siaddr=siaddr,
giaddr=giaddr, sname=sname, boot_file=boot_file)
buf = "\x02\x01\x06\x00\x00\x00\x00\x01\x00\x00\x00\x01\xc0\xa8\x0a\x0a"\
+ "\xc0\xa8\x14\x14\xc0\xa8\x1e\x1e\xc0\xa8\x28\x28\xaa\xaa\xaa\xaa"\
+ "\xaa\xaa\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x61\x62\x63\x00"\
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"\
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"\
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"\
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"\
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"\
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"\
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"\
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"\
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"\
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"\
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"\
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x63\x82\x53\x63"\
+ "\x35\x01\x02\x01\x04\xff\xff\xff\x00\x03\x04\xc0\xa8\x0a\x09\x06"\
+ "\x04\xc0\xa8\x0a\x09\x33\x04\x00\x03\xf4\x80\x3a\x04\x00\x01\xfa"\
+ "\x40\x3b\x04\x00\x03\x75\xf0\x36\x04\xc0\xa8\x0a\x09\xff"
buf = b"\x02\x01\x06\x00\x00\x00\x00\x01\x00\x00\x00\x01\xc0\xa8\x0a\x0a"\
+ b"\xc0\xa8\x14\x14\xc0\xa8\x1e\x1e\xc0\xa8\x28\x28\xaa\xaa\xaa\xaa"\
+ b"\xaa\xaa\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x61\x62\x63\x00"\
+ b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"\
+ b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"\
+ b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"\
+ b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"\
+ b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"\
+ b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"\
+ b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"\
+ b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"\
+ b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"\
+ b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"\
+ b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"\
+ b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x63\x82\x53\x63"\
+ b"\x35\x01\x02\x01\x04\xff\xff\xff\x00\x03\x04\xc0\xa8\x0a\x09\x06"\
+ b"\x04\xc0\xa8\x0a\x09\x33\x04\x00\x03\xf4\x80\x3a\x04\x00\x01\xfa"\
+ b"\x40\x3b\x04\x00\x03\x75\xf0\x36\x04\xc0\xa8\x0a\x09\xff"
def setUp(self):
pass

View File

@ -57,7 +57,7 @@ class Test_ipv6(unittest.TestCase):
def setUp_with_hop_opts(self):
self.opt1_type = 5
self.opt1_len = 2
self.opt1_data = '\x00\x00'
self.opt1_data = b'\x00\x00'
self.opt2_type = 1
self.opt2_len = 0
self.opt2_data = None
@ -86,7 +86,7 @@ class Test_ipv6(unittest.TestCase):
def setUp_with_dst_opts(self):
self.opt1_type = 5
self.opt1_len = 2
self.opt1_data = '\x00\x00'
self.opt1_data = b'\x00\x00'
self.opt2_type = 1
self.opt2_len = 0
self.opt2_data = None
@ -167,7 +167,7 @@ class Test_ipv6(unittest.TestCase):
self.auth_size = 4
self.auth_spi = 256
self.auth_seq = 1
self.auth_data = '\xa0\xe7\xf8\xab\xf9\x69\x1a\x8b\xf3\x9f\x7c\xae'
self.auth_data = b'\xa0\xe7\xf8\xab\xf9\x69\x1a\x8b\xf3\x9f\x7c\xae'
self.auth = ipv6.auth(
self.auth_nxt, self.auth_size, self.auth_spi, self.auth_seq,
self.auth_data)
@ -188,7 +188,7 @@ class Test_ipv6(unittest.TestCase):
def setUp_with_multi_headers(self):
self.opt1_type = 5
self.opt1_len = 2
self.opt1_data = '\x00\x00'
self.opt1_data = b'\x00\x00'
self.opt2_type = 1
self.opt2_len = 0
self.opt2_data = None
@ -204,7 +204,7 @@ class Test_ipv6(unittest.TestCase):
self.auth_size = 4
self.auth_spi = 256
self.auth_seq = 1
self.auth_data = '\xa0\xe7\xf8\xab\xf9\x69\x1a\x8b\xf3\x9f\x7c\xae'
self.auth_data = b'\xa0\xe7\xf8\xab\xf9\x69\x1a\x8b\xf3\x9f\x7c\xae'
self.auth = ipv6.auth(
self.auth_nxt, self.auth_size, self.auth_spi, self.auth_seq,
self.auth_data)
@ -510,9 +510,9 @@ class Test_hop_opts(unittest.TestCase):
self.nxt = 0
self.size = 8
self.data = [
ipv6.option(5, 2, '\x00\x00'),
ipv6.option(5, 2, b'\x00\x00'),
ipv6.option(1, 0, None),
ipv6.option(0xc2, 4, '\x00\x01\x00\x00'),
ipv6.option(0xc2, 4, b'\x00\x01\x00\x00'),
ipv6.option(1, 0, None),
]
self.hop = ipv6.hop_opts(self.nxt, self.size, self.data)
@ -591,9 +591,9 @@ class Test_dst_opts(unittest.TestCase):
self.nxt = 60
self.size = 8
self.data = [
ipv6.option(5, 2, '\x00\x00'),
ipv6.option(5, 2, b'\x00\x00'),
ipv6.option(1, 0, None),
ipv6.option(0xc2, 4, '\x00\x01\x00\x00'),
ipv6.option(0xc2, 4, b'\x00\x01\x00\x00'),
ipv6.option(1, 0, None),
]
self.dst = ipv6.dst_opts(self.nxt, self.size, self.data)
@ -662,7 +662,7 @@ class Test_dst_opts(unittest.TestCase):
eq_(res[0], 6)
eq_(res[1], 0)
opt = ipv6.option(type_=1, len_=4, data='\x00\x00\x00\x00')
opt = ipv6.option(type_=1, len_=4, data=b'\x00\x00\x00\x00')
eq_(str(buf[2:]), opt.serialize())
@ -670,7 +670,7 @@ class Test_option(unittest.TestCase):
def setUp(self):
self.type_ = 5
self.data = '\x00\x00'
self.data = b'\x00\x00'
self.len_ = len(self.data)
self.opt = ipv6.option(self.type_, self.len_, self.data)
self.form = '!BB%ds' % self.len_
@ -1068,7 +1068,7 @@ class Test_auth(unittest.TestCase):
self.size = 4
self.spi = 256
self.seq = 1
self.data = '\x21\xd3\xa9\x5c\x5f\xfd\x4d\x18\x46\x22\xb9\xf8'
self.data = b'\x21\xd3\xa9\x5c\x5f\xfd\x4d\x18\x46\x22\xb9\xf8'
self.auth = ipv6.auth(
self.nxt, self.size, self.spi, self.seq, self.data)
self.form = '!BB2xII12s'