tests: Explicitly encode str to bytes when needed for python3

Avoid implicit conversion errors when constructing bytes objects or
passing a str object to a function that needs a bytes object.

Signed-off-by: Masashi Honma <masashi.honma@gmail.com>
Signed-off-by: Jouni Malinen <j@w1.fi>
This commit is contained in:
Masashi Honma 2019-02-02 16:25:57 +02:00 committed by Jouni Malinen
parent a444a6d262
commit f94df3c0b0
6 changed files with 27 additions and 27 deletions

View file

@ -1248,7 +1248,7 @@ def sha1_prf(key, label, data, outlen):
res = '' res = ''
counter = 0 counter = 0
while outlen > 0: while outlen > 0:
m = hmac.new(key, label, hashlib.sha1) m = hmac.new(key, label.encode(), hashlib.sha1)
m.update(struct.pack('B', 0)) m.update(struct.pack('B', 0))
m.update(data) m.update(data)
m.update(struct.pack('B', counter)) m.update(struct.pack('B', counter))

View file

@ -4049,7 +4049,7 @@ def gen_upnp_info(eventSubURL='wps_event', controlURL='wps_control',
'Connection: close\r\n' + \ 'Connection: close\r\n' + \
'Content-Length: ' + str(len(payload)) + '\r\n' + \ 'Content-Length: ' + str(len(payload)) + '\r\n' + \
'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n' 'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n'
return hdr + payload return (hdr + payload).encode()
def gen_wps_control(payload_override=None): def gen_wps_control(payload_override=None):
payload = '''<?xml version="1.0"?> payload = '''<?xml version="1.0"?>
@ -4077,7 +4077,7 @@ AAYANyoAASA=
'Connection: close\r\n' + \ 'Connection: close\r\n' + \
'Content-Length: ' + str(len(payload)) + '\r\n' + \ 'Content-Length: ' + str(len(payload)) + '\r\n' + \
'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n' 'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n'
return hdr + payload return (hdr + payload).encode()
def gen_wps_event(sid='uuid:7eb3342a-8a5f-47fe-a585-0785bfec6d8a'): def gen_wps_event(sid='uuid:7eb3342a-8a5f-47fe-a585-0785bfec6d8a'):
payload = "" payload = ""
@ -4090,7 +4090,7 @@ def gen_wps_event(sid='uuid:7eb3342a-8a5f-47fe-a585-0785bfec6d8a'):
hdr += 'SID: ' + sid + '\r\n' hdr += 'SID: ' + sid + '\r\n'
hdr += 'Timeout: Second-1801\r\n' + \ hdr += 'Timeout: Second-1801\r\n' + \
'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n' 'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n'
return hdr + payload return (hdr + payload).encode()
class WPSAPHTTPServer(StreamRequestHandler): class WPSAPHTTPServer(StreamRequestHandler):
def handle(self): def handle(self):
@ -4723,7 +4723,7 @@ def test_ap_wps_er_http_proto_subscribe_failing(dev, apdev):
'Content-Length: ' + str(len(payload)) + '\r\n' + \ 'Content-Length: ' + str(len(payload)) + '\r\n' + \
'Timeout: Second-1801\r\n' + \ 'Timeout: Second-1801\r\n' + \
'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n' 'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n'
self.wfile.write(hdr + payload) self.wfile.write((hdr + payload).encode())
run_wps_er_proto_test(dev[0], WPSAPHTTPServer_fail_subscribe) run_wps_er_proto_test(dev[0], WPSAPHTTPServer_fail_subscribe)
def test_ap_wps_er_http_proto_subscribe_invalid_response(dev, apdev): def test_ap_wps_er_http_proto_subscribe_invalid_response(dev, apdev):
@ -4738,7 +4738,7 @@ def test_ap_wps_er_http_proto_subscribe_invalid_response(dev, apdev):
'Content-Length: ' + str(len(payload)) + '\r\n' + \ 'Content-Length: ' + str(len(payload)) + '\r\n' + \
'Timeout: Second-1801\r\n' + \ 'Timeout: Second-1801\r\n' + \
'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n' 'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n'
self.wfile.write(hdr + payload) self.wfile.write((hdr + payload).encode())
run_wps_er_proto_test(dev[0], WPSAPHTTPServer_subscribe_invalid_response) run_wps_er_proto_test(dev[0], WPSAPHTTPServer_subscribe_invalid_response)
def test_ap_wps_er_http_proto_subscribe_invalid_response(dev, apdev): def test_ap_wps_er_http_proto_subscribe_invalid_response(dev, apdev):
@ -4775,7 +4775,7 @@ def test_ap_wps_er_http_proto_upnp_info_no_device(dev, apdev):
'Connection: close\r\n' + \ 'Connection: close\r\n' + \
'Content-Length: ' + str(len(payload)) + '\r\n' + \ 'Content-Length: ' + str(len(payload)) + '\r\n' + \
'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n' 'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n'
self.wfile.write(hdr + payload) self.wfile.write((hdr + payload).encode())
run_wps_er_proto_test(dev[0], WPSAPHTTPServer_no_device, no_event_url=True) run_wps_er_proto_test(dev[0], WPSAPHTTPServer_no_device, no_event_url=True)
def test_ap_wps_er_http_proto_upnp_info_no_device_type(dev, apdev): def test_ap_wps_er_http_proto_upnp_info_no_device_type(dev, apdev):
@ -4798,7 +4798,7 @@ def test_ap_wps_er_http_proto_upnp_info_no_device_type(dev, apdev):
'Connection: close\r\n' + \ 'Connection: close\r\n' + \
'Content-Length: ' + str(len(payload)) + '\r\n' + \ 'Content-Length: ' + str(len(payload)) + '\r\n' + \
'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n' 'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n'
self.wfile.write(hdr + payload) self.wfile.write((hdr + payload).encode())
run_wps_er_proto_test(dev[0], WPSAPHTTPServer_no_device, no_event_url=True) run_wps_er_proto_test(dev[0], WPSAPHTTPServer_no_device, no_event_url=True)
def test_ap_wps_er_http_proto_upnp_info_invalid_udn_uuid(dev, apdev): def test_ap_wps_er_http_proto_upnp_info_invalid_udn_uuid(dev, apdev):
@ -6276,7 +6276,7 @@ def wsc_kdf(key, label, bits):
result = '' result = ''
i = 1 i = 1
while len(result) * 8 < bits: while len(result) * 8 < bits:
data = struct.pack('>L', i) + label + struct.pack('>L', bits) data = struct.pack('>L', i) + label.encode() + struct.pack('>L', bits)
m = hmac.new(key, data, hashlib.sha256) m = hmac.new(key, data, hashlib.sha256)
result += m.digest() result += m.digest()
i += 1 i += 1
@ -6290,7 +6290,7 @@ def wsc_keys(kdk):
return authkey,keywrapkey,emsk return authkey,keywrapkey,emsk
def wsc_dev_pw_half_psk(authkey, dev_pw): def wsc_dev_pw_half_psk(authkey, dev_pw):
m = hmac.new(authkey, dev_pw, hashlib.sha256) m = hmac.new(authkey, dev_pw.encode(), hashlib.sha256)
return m.digest()[0:16] return m.digest()[0:16]
def wsc_dev_pw_psk(authkey, dev_pw): def wsc_dev_pw_psk(authkey, dev_pw):

View file

@ -6830,7 +6830,7 @@ def test_eap_proto_fast_errors(dev, apdev):
"wpa_supplicant EAP-FAST PAC file - version 1\nSTART\nI-ID=1\nEND\n", "wpa_supplicant EAP-FAST PAC file - version 1\nSTART\nI-ID=1\nEND\n",
"wpa_supplicant EAP-FAST PAC file - version 1\nSTART\nA-ID-Info=1\nEND\n" ] "wpa_supplicant EAP-FAST PAC file - version 1\nSTART\nA-ID-Info=1\nEND\n" ]
for pac in tests: for pac in tests:
blob = binascii.hexlify(pac).decode() blob = binascii.hexlify(pac.encode()).decode()
dev[0].request("SET blob fast_pac_errors " + blob) dev[0].request("SET blob fast_pac_errors " + blob)
dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412", dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
eap="FAST", anonymous_identity="FAST", eap="FAST", anonymous_identity="FAST",
@ -6849,7 +6849,7 @@ def test_eap_proto_fast_errors(dev, apdev):
tests = [ "wpa_supplicant EAP-FAST PAC file - version 1\nSTART\nEND\n", tests = [ "wpa_supplicant EAP-FAST PAC file - version 1\nSTART\nEND\n",
"wpa_supplicant EAP-FAST PAC file - version 1\nSTART\nEND\nSTART\nEND\nSTART\nEND\n" ] "wpa_supplicant EAP-FAST PAC file - version 1\nSTART\nEND\nSTART\nEND\nSTART\nEND\n" ]
for pac in tests: for pac in tests:
blob = binascii.hexlify(pac).decode() blob = binascii.hexlify(pac.encode()).decode()
dev[0].request("SET blob fast_pac_errors " + blob) dev[0].request("SET blob fast_pac_errors " + blob)
dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412", dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
eap="FAST", anonymous_identity="FAST", eap="FAST", anonymous_identity="FAST",

View file

@ -603,7 +603,7 @@ def test_fils_sk_multiple_realms(dev, apdev, params):
expected = '' expected = ''
count = 0 count = 0
for realm in fils_realms: for realm in fils_realms:
hash = hashlib.sha256(realm.lower()).digest() hash = hashlib.sha256(realm.lower().encode()).digest()
expected += binascii.hexlify(hash[0:2]).decode() expected += binascii.hexlify(hash[0:2]).decode()
count += 1 count += 1
if count == 7: if count == 7:
@ -616,7 +616,7 @@ def test_fils_sk_multiple_realms(dev, apdev, params):
info = bss['anqp_fils_realm_info'] info = bss['anqp_fils_realm_info']
expected = '' expected = ''
for realm in fils_realms: for realm in fils_realms:
hash = hashlib.sha256(realm.lower()).digest() hash = hashlib.sha256(realm.lower().encode()).digest()
expected += binascii.hexlify(hash[0:2]).decode() expected += binascii.hexlify(hash[0:2]).decode()
if info != expected: if info != expected:
raise Exception("Unexpected FILS Realm Info ANQP-element: " + info) raise Exception("Unexpected FILS Realm Info ANQP-element: " + info)

View file

@ -1193,9 +1193,9 @@ def test_gas_anqp_extra_elements(dev, apdev):
geo_loc = "001052834d12efd2b08b9b4bf1cc2c00004104050000000000060100" geo_loc = "001052834d12efd2b08b9b4bf1cc2c00004104050000000000060100"
civic_loc = "0000f9555302f50102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5" civic_loc = "0000f9555302f50102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5"
held_uri = "https://held.example.com/location" held_uri = "https://held.example.com/location"
held = struct.pack('BBB', 0, 1 + len(held_uri), 1) + held_uri held = struct.pack('BBB', 0, 1 + len(held_uri), 1) + held_uri.encode()
supl_fqdn = "supl.example.com" supl_fqdn = "supl.example.com"
supl = struct.pack('BBB', 0, 1 + len(supl_fqdn), 1) + supl_fqdn supl = struct.pack('BBB', 0, 1 + len(supl_fqdn), 1) + supl_fqdn.encode()
public_id = binascii.hexlify(held + supl).decode() public_id = binascii.hexlify(held + supl).decode()
params = { "ssid": "gas/anqp", params = { "ssid": "gas/anqp",
"interworking": "1", "interworking": "1",
@ -1602,8 +1602,8 @@ def test_gas_anqp_venue_url(dev, apdev):
name1= "Example venue" name1= "Example venue"
lang2 = "fin" lang2 = "fin"
name2 = "Esimerkkipaikka" name2 = "Esimerkkipaikka"
venue1 = struct.pack('B', len(lang1 + name1)) + lang1 + name1 venue1 = struct.pack('B', len(lang1 + name1)) + lang1.encode() + name1.encode()
venue2 = struct.pack('B', len(lang2 + name2)) + lang2 + name2 venue2 = struct.pack('B', len(lang2 + name2)) + lang2.encode() + name2.encode()
venue_name = binascii.hexlify(venue_info + venue1 + venue2).decode() venue_name = binascii.hexlify(venue_info + venue1 + venue2).decode()
url1 = "http://example.com/venue" url1 = "http://example.com/venue"
@ -1659,14 +1659,14 @@ def test_gas_anqp_venue_url2(dev, apdev):
name1= "Example venue" name1= "Example venue"
lang2 = "fin" lang2 = "fin"
name2 = "Esimerkkipaikka" name2 = "Esimerkkipaikka"
venue1 = struct.pack('B', len(lang1 + name1)) + lang1 + name1 venue1 = struct.pack('B', len(lang1 + name1)) + lang1.encode() + name1.encode()
venue2 = struct.pack('B', len(lang2 + name2)) + lang2 + name2 venue2 = struct.pack('B', len(lang2 + name2)) + lang2.encode() + name2.encode()
venue_name = binascii.hexlify(venue_info + venue1 + venue2).decode() venue_name = binascii.hexlify(venue_info + venue1 + venue2).decode()
url1 = "http://example.com/venue" url1 = "http://example.com/venue"
url2 = "https://example.org/venue-info/" url2 = "https://example.org/venue-info/"
duple1 = struct.pack('BB', 1 + len(url1), 1) + url1 duple1 = struct.pack('BB', 1 + len(url1.encode()), 1) + url1.encode()
duple2 = struct.pack('BB', 1 + len(url2), 2) + url2 duple2 = struct.pack('BB', 1 + len(url2.encode()), 2) + url2.encode()
venue_url = binascii.hexlify(duple1 + duple2).decode() venue_url = binascii.hexlify(duple1 + duple2).decode()
params = { "ssid": "gas/anqp", params = { "ssid": "gas/anqp",
@ -1713,8 +1713,8 @@ def test_gas_anqp_venue_url_pmf(dev, apdev):
name1= "Example venue" name1= "Example venue"
lang2 = "fin" lang2 = "fin"
name2 = "Esimerkkipaikka" name2 = "Esimerkkipaikka"
venue1 = struct.pack('B', len(lang1 + name1)) + lang1 + name1 venue1 = struct.pack('B', len(lang1 + name1)) + lang1.encode() + name1.encode()
venue2 = struct.pack('B', len(lang2 + name2)) + lang2 + name2 venue2 = struct.pack('B', len(lang2 + name2)) + lang2.encode() + name2.encode()
venue_name = binascii.hexlify(venue_info + venue1 + venue2) venue_name = binascii.hexlify(venue_info + venue1 + venue2)
url1 = "http://example.com/venue" url1 = "http://example.com/venue"

View file

@ -16,7 +16,7 @@ from p2p_utils import *
from test_gas import anqp_adv_proto from test_gas import anqp_adv_proto
def ie_ssid(ssid): def ie_ssid(ssid):
return struct.pack("<BB", WLAN_EID_SSID, len(ssid)) + ssid return struct.pack("<BB", WLAN_EID_SSID, len(ssid)) + ssid.encode()
def ie_supp_rates(): def ie_supp_rates():
return struct.pack("<BBBBBBBBBB", WLAN_EID_SUPP_RATES, 8, return struct.pack("<BBBBBBBBBB", WLAN_EID_SUPP_RATES, 8,
@ -85,12 +85,12 @@ def p2p_attr_device_info(addr, name="Test", config_methods=0, dev_type="00010050
val2 = struct.unpack('8B', binascii.unhexlify(dev_type)) val2 = struct.unpack('8B', binascii.unhexlify(dev_type))
t = (P2P_ATTR_DEVICE_INFO, 6 + 2 + 8 + 1 + 4 + len(name)) + val t = (P2P_ATTR_DEVICE_INFO, 6 + 2 + 8 + 1 + 4 + len(name)) + val
t2 = val2 + (0,) t2 = val2 + (0,)
return struct.pack("<BH6B", *t) + struct.pack(">H", config_methods) + struct.pack("8BB", *t2) + struct.pack('>HH', 0x1011, len(name)) +name return struct.pack("<BH6B", *t) + struct.pack(">H", config_methods) + struct.pack("8BB", *t2) + struct.pack('>HH', 0x1011, len(name)) + name.encode()
def p2p_attr_group_id(addr, ssid): def p2p_attr_group_id(addr, ssid):
val = struct.unpack('6B', binascii.unhexlify(addr.replace(':',''))) val = struct.unpack('6B', binascii.unhexlify(addr.replace(':','')))
t = (P2P_ATTR_GROUP_ID, 6 + len(ssid)) + val t = (P2P_ATTR_GROUP_ID, 6 + len(ssid)) + val
return struct.pack('<BH6B', *t) + ssid return struct.pack('<BH6B', *t) + ssid.encode()
def p2p_attr_operating_channel(op_class=81, chan=1): def p2p_attr_operating_channel(op_class=81, chan=1):
return struct.pack("<BHBBBBB", P2P_ATTR_OPERATING_CHANNEL, 5, return struct.pack("<BHBBBBB", P2P_ATTR_OPERATING_CHANNEL, 5,