From f94df3c0b0f8a290a07da83e055af0602dffe8cf Mon Sep 17 00:00:00 2001 From: Masashi Honma Date: Sat, 2 Feb 2019 16:25:57 +0200 Subject: [PATCH] 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 Signed-off-by: Jouni Malinen --- tests/hwsim/test_ap_psk.py | 2 +- tests/hwsim/test_ap_wps.py | 18 +++++++++--------- tests/hwsim/test_eap_proto.py | 4 ++-- tests/hwsim/test_fils.py | 4 ++-- tests/hwsim/test_gas.py | 20 ++++++++++---------- tests/hwsim/test_p2p_messages.py | 6 +++--- 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/tests/hwsim/test_ap_psk.py b/tests/hwsim/test_ap_psk.py index a82f51af0..7c04d6408 100644 --- a/tests/hwsim/test_ap_psk.py +++ b/tests/hwsim/test_ap_psk.py @@ -1248,7 +1248,7 @@ def sha1_prf(key, label, data, outlen): res = '' counter = 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(data) m.update(struct.pack('B', counter)) diff --git a/tests/hwsim/test_ap_wps.py b/tests/hwsim/test_ap_wps.py index 640a8e87c..de2039572 100644 --- a/tests/hwsim/test_ap_wps.py +++ b/tests/hwsim/test_ap_wps.py @@ -4049,7 +4049,7 @@ def gen_upnp_info(eventSubURL='wps_event', controlURL='wps_control', 'Connection: close\r\n' + \ 'Content-Length: ' + str(len(payload)) + '\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): payload = ''' @@ -4077,7 +4077,7 @@ AAYANyoAASA= 'Connection: close\r\n' + \ 'Content-Length: ' + str(len(payload)) + '\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'): payload = "" @@ -4090,7 +4090,7 @@ def gen_wps_event(sid='uuid:7eb3342a-8a5f-47fe-a585-0785bfec6d8a'): hdr += 'SID: ' + sid + '\r\n' hdr += 'Timeout: Second-1801\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): 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' + \ 'Timeout: Second-1801\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) 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' + \ 'Timeout: Second-1801\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) 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' + \ 'Content-Length: ' + str(len(payload)) + '\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) 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' + \ 'Content-Length: ' + str(len(payload)) + '\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) 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 = '' i = 1 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) result += m.digest() i += 1 @@ -6290,7 +6290,7 @@ def wsc_keys(kdk): return authkey,keywrapkey,emsk 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] def wsc_dev_pw_psk(authkey, dev_pw): diff --git a/tests/hwsim/test_eap_proto.py b/tests/hwsim/test_eap_proto.py index a6881177e..c07694f49 100644 --- a/tests/hwsim/test_eap_proto.py +++ b/tests/hwsim/test_eap_proto.py @@ -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\nA-ID-Info=1\nEND\n" ] 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].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412", 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", "wpa_supplicant EAP-FAST PAC file - version 1\nSTART\nEND\nSTART\nEND\nSTART\nEND\n" ] 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].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412", eap="FAST", anonymous_identity="FAST", diff --git a/tests/hwsim/test_fils.py b/tests/hwsim/test_fils.py index 3276de38b..c3de79be8 100644 --- a/tests/hwsim/test_fils.py +++ b/tests/hwsim/test_fils.py @@ -603,7 +603,7 @@ def test_fils_sk_multiple_realms(dev, apdev, params): expected = '' count = 0 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() count += 1 if count == 7: @@ -616,7 +616,7 @@ def test_fils_sk_multiple_realms(dev, apdev, params): info = bss['anqp_fils_realm_info'] expected = '' 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() if info != expected: raise Exception("Unexpected FILS Realm Info ANQP-element: " + info) diff --git a/tests/hwsim/test_gas.py b/tests/hwsim/test_gas.py index 5b6a3e068..21a02dfe3 100644 --- a/tests/hwsim/test_gas.py +++ b/tests/hwsim/test_gas.py @@ -1193,9 +1193,9 @@ def test_gas_anqp_extra_elements(dev, apdev): geo_loc = "001052834d12efd2b08b9b4bf1cc2c00004104050000000000060100" civic_loc = "0000f9555302f50102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5" 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 = 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() params = { "ssid": "gas/anqp", "interworking": "1", @@ -1602,8 +1602,8 @@ def test_gas_anqp_venue_url(dev, apdev): name1= "Example venue" lang2 = "fin" name2 = "Esimerkkipaikka" - venue1 = struct.pack('B', len(lang1 + name1)) + lang1 + name1 - venue2 = struct.pack('B', len(lang2 + name2)) + lang2 + name2 + venue1 = struct.pack('B', len(lang1 + name1)) + lang1.encode() + name1.encode() + venue2 = struct.pack('B', len(lang2 + name2)) + lang2.encode() + name2.encode() venue_name = binascii.hexlify(venue_info + venue1 + venue2).decode() url1 = "http://example.com/venue" @@ -1659,14 +1659,14 @@ def test_gas_anqp_venue_url2(dev, apdev): name1= "Example venue" lang2 = "fin" name2 = "Esimerkkipaikka" - venue1 = struct.pack('B', len(lang1 + name1)) + lang1 + name1 - venue2 = struct.pack('B', len(lang2 + name2)) + lang2 + name2 + venue1 = struct.pack('B', len(lang1 + name1)) + lang1.encode() + name1.encode() + venue2 = struct.pack('B', len(lang2 + name2)) + lang2.encode() + name2.encode() venue_name = binascii.hexlify(venue_info + venue1 + venue2).decode() url1 = "http://example.com/venue" url2 = "https://example.org/venue-info/" - duple1 = struct.pack('BB', 1 + len(url1), 1) + url1 - duple2 = struct.pack('BB', 1 + len(url2), 2) + url2 + duple1 = struct.pack('BB', 1 + len(url1.encode()), 1) + url1.encode() + duple2 = struct.pack('BB', 1 + len(url2.encode()), 2) + url2.encode() venue_url = binascii.hexlify(duple1 + duple2).decode() params = { "ssid": "gas/anqp", @@ -1713,8 +1713,8 @@ def test_gas_anqp_venue_url_pmf(dev, apdev): name1= "Example venue" lang2 = "fin" name2 = "Esimerkkipaikka" - venue1 = struct.pack('B', len(lang1 + name1)) + lang1 + name1 - venue2 = struct.pack('B', len(lang2 + name2)) + lang2 + name2 + venue1 = struct.pack('B', len(lang1 + name1)) + lang1.encode() + name1.encode() + venue2 = struct.pack('B', len(lang2 + name2)) + lang2.encode() + name2.encode() venue_name = binascii.hexlify(venue_info + venue1 + venue2) url1 = "http://example.com/venue" diff --git a/tests/hwsim/test_p2p_messages.py b/tests/hwsim/test_p2p_messages.py index d4c39b82d..c8fea1eab 100644 --- a/tests/hwsim/test_p2p_messages.py +++ b/tests/hwsim/test_p2p_messages.py @@ -16,7 +16,7 @@ from p2p_utils import * from test_gas import anqp_adv_proto def ie_ssid(ssid): - return struct.pack("H", config_methods) + struct.pack("8BB", *t2) + struct.pack('>HH', 0x1011, len(name)) +name + return struct.pack("H", config_methods) + struct.pack("8BB", *t2) + struct.pack('>HH', 0x1011, len(name)) + name.encode() def p2p_attr_group_id(addr, ssid): val = struct.unpack('6B', binascii.unhexlify(addr.replace(':',''))) t = (P2P_ATTR_GROUP_ID, 6 + len(ssid)) + val - return struct.pack('