[PATCH v2 04/38] tests: Modify core functions for python3

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Masashi Honma <masashi.honma@xxxxxxxxx>
---
 tests/hwsim/hostapd.py       |  9 ++++++---
 tests/hwsim/hwsim.py         |  2 +-
 tests/hwsim/netlink.py       | 11 +++++------
 tests/hwsim/nl80211.py       |  4 ++--
 tests/hwsim/rfkill.py        | 12 +++++++-----
 tests/hwsim/run-tests.py     |  2 +-
 tests/hwsim/tshark.py        | 12 ++++++++----
 tests/hwsim/wpasupplicant.py | 11 +++++++----
 wpaspy/wpaspy.py             | 13 +++++++------
 9 files changed, 44 insertions(+), 32 deletions(-)

diff --git a/tests/hwsim/hostapd.py b/tests/hwsim/hostapd.py
index 9deab15aa..3d6ca1d19 100644
--- a/tests/hwsim/hostapd.py
+++ b/tests/hwsim/hostapd.py
@@ -162,7 +162,7 @@ class Hostapd:
                                     stdout=subprocess.PIPE, shell=shell)
             out = proc.communicate()[0]
             ret = proc.returncode
-            return ret, out
+            return ret, out.decode()
         else:
             return self.host.execute(cmd_array)
 
@@ -180,7 +180,10 @@ class Hostapd:
         return self.bssid
 
     def request(self, cmd):
-        logger.debug(self.dbg + ": CTRL: " + cmd)
+        try:
+            logger.debug(self.dbg + ": CTRL: " + cmd)
+        except UnicodeEncodeError:
+            pass
         return self.ctrl.request(cmd)
 
     def ping(self):
@@ -329,7 +332,7 @@ class Hostapd:
     def mgmt_tx(self, msg):
         t = (msg['fc'], 0) + mac2tuple(msg['da']) + mac2tuple(msg['sa']) + mac2tuple(msg['bssid']) + (0,)
         hdr = struct.pack('<HH6B6B6BH', *t)
-        if "OK" not in self.request("MGMT_TX " + binascii.hexlify(hdr + msg['payload'])):
+        if "OK" not in self.request("MGMT_TX " + binascii.hexlify(hdr + msg['payload']).decode()):
             raise Exception("MGMT_TX command to hostapd failed")
 
     def get_sta(self, addr, info=None, next=False):
diff --git a/tests/hwsim/hwsim.py b/tests/hwsim/hwsim.py
index 9300922a8..bc1be8542 100644
--- a/tests/hwsim/hwsim.py
+++ b/tests/hwsim/hwsim.py
@@ -22,7 +22,7 @@ HWSIM_ATTR_USE_CHANCTX		= 15
 class HWSimController(object):
     def __init__(self):
         self._conn = netlink.Connection(netlink.NETLINK_GENERIC)
-        self._fid = netlink.genl_controller.get_family_id('MAC80211_HWSIM')
+        self._fid = netlink.genl_controller.get_family_id(b'MAC80211_HWSIM')
 
     def create_radio(self, n_channels=None, use_chanctx=False,
                      use_p2p_device=False):
diff --git a/tests/hwsim/netlink.py b/tests/hwsim/netlink.py
index eef79090c..2773f2d8b 100644
--- a/tests/hwsim/netlink.py
+++ b/tests/hwsim/netlink.py
@@ -6,7 +6,7 @@
 # This software may be distributed under the terms of the BSD license.
 # See README for more details.
 
-import struct, socket
+import struct, socket, binascii
 
 # flags
 NLM_F_REQUEST = 1
@@ -33,7 +33,7 @@ class Attr(object):
         hdr = struct.pack("HH", len(self._data) + 4, self._type)
         length = len(self._data)
         pad = ((length + 4 - 1) & ~3 ) - length
-        return hdr + self._data + '\0' * pad
+        return hdr + self._data + b'\x00' * pad
 
     def __repr__(self):
         return '<Attr type %d, data "%s">' % (self._type, repr(self._data))
@@ -71,7 +71,7 @@ class U8Attr(Attr):
 
 class FlagAttr(Attr):
     def __init__(self, attr_type):
-        Attr.__init__(self, attr_type, "")
+        Attr.__init__(self, attr_type, bytes())
 
 class Nested(Attr):
     def __init__(self, attr_type, attrs):
@@ -113,10 +113,9 @@ class Message(object):
         self.pid = -1
         payload = payload or []
         if isinstance(payload, list):
-            contents = []
+            self.payload = bytes()
             for attr in payload:
-                contents.append(attr._dump())
-            self.payload = ''.join(contents)
+                self.payload = self.payload + attr._dump()
         else:
             self.payload = payload
 
diff --git a/tests/hwsim/nl80211.py b/tests/hwsim/nl80211.py
index 910381639..daba2371b 100644
--- a/tests/hwsim/nl80211.py
+++ b/tests/hwsim/nl80211.py
@@ -322,7 +322,7 @@ nl80211_attr = {
 def build_nl80211_attr(id, val):
     attr = struct.pack("@HH", 4 + len(val), nl80211_attr[id]) + val
     if len(attr) % 4 != 0:
-        attr += '\0' * (4 - (len(attr) % 4))
+        attr += b'\x00' * (4 - (len(attr) % 4))
     return attr
 
 def build_nl80211_attr_u32(id, val):
@@ -335,7 +335,7 @@ def build_nl80211_attr_u8(id, val):
     return build_nl80211_attr(id, struct.pack("@B", val))
 
 def build_nl80211_attr_flag(id):
-    return build_nl80211_attr(id, '')
+    return build_nl80211_attr(id, bytes())
 
 def build_nl80211_attr_mac(id, val):
     addr = struct.unpack('6B', binascii.unhexlify(val.replace(':','')))
diff --git a/tests/hwsim/rfkill.py b/tests/hwsim/rfkill.py
index a5e6f523a..f08cf50c5 100755
--- a/tests/hwsim/rfkill.py
+++ b/tests/hwsim/rfkill.py
@@ -97,20 +97,20 @@ class RFKill(object):
         return self.blocked[1]
 
     def block(self):
-        rfk = open('/dev/rfkill', 'w')
+        rfk = open('/dev/rfkill', 'wb')
         s = struct.pack(_event_struct, self.idx, TYPE_ALL, _OP_CHANGE, 1, 0)
         rfk.write(s)
         rfk.close()
 
     def unblock(self):
-        rfk = open('/dev/rfkill', 'w')
+        rfk = open('/dev/rfkill', 'wb')
         s = struct.pack(_event_struct, self.idx, TYPE_ALL, _OP_CHANGE, 0, 0)
         rfk.write(s)
         rfk.close()
 
     @classmethod
     def block_all(cls, t=TYPE_ALL):
-        rfk = open('/dev/rfkill', 'w')
+        rfk = open('/dev/rfkill', 'wb')
         print(rfk)
         s = struct.pack(_event_struct, 0, t, _OP_CHANGE_ALL, 1, 0)
         rfk.write(s)
@@ -118,7 +118,7 @@ class RFKill(object):
 
     @classmethod
     def unblock_all(cls, t=TYPE_ALL):
-        rfk = open('/dev/rfkill', 'w')
+        rfk = open('/dev/rfkill', 'wb')
         s = struct.pack(_event_struct, 0, t, _OP_CHANGE_ALL, 0, 0)
         rfk.write(s)
         rfk.close()
@@ -126,13 +126,15 @@ class RFKill(object):
     @classmethod
     def list(cls):
         res = []
-        rfk = open('/dev/rfkill', 'r')
+        rfk = open('/dev/rfkill', 'rb')
         fd = rfk.fileno()
         flgs = fcntl.fcntl(fd, fcntl.F_GETFL)
         fcntl.fcntl(fd, fcntl.F_SETFL, flgs | os.O_NONBLOCK)
         while True:
             try:
                 d = rfk.read(_event_sz)
+                if d == None:
+                    break
                 _idx, _t, _op, _s, _h = struct.unpack(_event_struct, d)
                 if _op != _OP_ADD:
                     continue
diff --git a/tests/hwsim/run-tests.py b/tests/hwsim/run-tests.py
index be0edcb7a..49e42cd78 100755
--- a/tests/hwsim/run-tests.py
+++ b/tests/hwsim/run-tests.py
@@ -84,7 +84,7 @@ def add_log_file(conn, test, run, type, path):
     if not os.path.exists(path):
         return
     contents = None
-    with open(path, 'r') as f:
+    with open(path, 'rb') as f:
         contents = f.read()
     if contents is None:
         return
diff --git a/tests/hwsim/tshark.py b/tests/hwsim/tshark.py
index 81aee6c4b..042c10575 100644
--- a/tests/hwsim/tshark.py
+++ b/tests/hwsim/tshark.py
@@ -7,6 +7,7 @@
 # This software may be distributed under the terms of the BSD license.
 # See README for more details.
 
+import sys
 import time
 import subprocess
 import logging
@@ -46,11 +47,14 @@ def _run_tshark(filename, filter, display=None, wait=True):
 
     output = cmd.communicate()
     out = output[0]
+    if sys.version_info[0] > 2:
+        out = out.decode()
     res = cmd.wait()
+    _output = output[1].decode()
     if res == 1:
         errmsg = "Some fields aren't valid"
-        if errmsg in output[1]:
-            errors = output[1].split('\n')
+        if errmsg in _output:
+            errors = _output.split('\n')
             fields = []
             collect = False
             for f in errors:
@@ -71,8 +75,8 @@ def _run_tshark(filename, filter, display=None, wait=True):
         out = cmd.communicate()[0]
         cmd.wait()
     if res == 2:
-        if "tshark: Neither" in output[1] and "are field or protocol names" in output[1]:
-            errors = output[1].split('\n')
+        if "tshark: Neither" in _output and "are field or protocol names" in _output:
+            errors = _output.split('\n')
             fields = []
             for f in errors:
                 if f.startswith("tshark: Neither "):
diff --git a/tests/hwsim/wpasupplicant.py b/tests/hwsim/wpasupplicant.py
index fed9a5eff..02cd0e93d 100644
--- a/tests/hwsim/wpasupplicant.py
+++ b/tests/hwsim/wpasupplicant.py
@@ -62,7 +62,7 @@ class WpaSupplicant:
                 cmd = cmd_array
             proc = subprocess.Popen(cmd, stderr=subprocess.STDOUT,
                                     stdout=subprocess.PIPE, shell=shell)
-            out = proc.communicate()[0]
+            out = proc.communicate()[0].decode()
             ret = proc.returncode
             return ret, out
         else:
@@ -169,7 +169,10 @@ class WpaSupplicant:
         self.global_request("INTERFACE_REMOVE " + ifname)
 
     def request(self, cmd, timeout=10):
-        logger.debug(self.dbg + ": CTRL: " + cmd)
+        try:
+            logger.debug(self.dbg + ": CTRL: " + cmd)
+        except UnicodeEncodeError:
+            pass
         return self.ctrl.request(cmd, timeout=timeout)
 
     def global_request(self, cmd):
@@ -1151,8 +1154,8 @@ class WpaSupplicant:
         self.dump_monitor()
         if new_ssid:
             self.request("WPS_REG " + bssid + " " + pin + " " +
-                         new_ssid.encode("hex") + " " + key_mgmt + " " +
-                         cipher + " " + new_passphrase.encode("hex"))
+                         binascii.hexlify(new_ssid.encode()).decode() + " " + key_mgmt + " " +
+                         cipher + " " + binascii.hexlify(new_passphrase.encode()).decode())
             if no_wait:
                 return
             ev = self.wait_event(["WPS-SUCCESS"], timeout=15)
diff --git a/wpaspy/wpaspy.py b/wpaspy/wpaspy.py
index 99b565ad2..05e2fa445 100644
--- a/wpaspy/wpaspy.py
+++ b/wpaspy/wpaspy.py
@@ -52,9 +52,9 @@ class Ctrl:
                     break
                 self.s = socket.socket(af, socktype)
                 self.s.settimeout(5)
-                self.s.sendto("GET_COOKIE", sockaddr)
+                self.s.sendto("GET_COOKIE".encode(), sockaddr)
                 reply, server = self.s.recvfrom(4096)
-                self.cookie = reply
+                self.cookie = reply.decode()
                 self.port = port
             except:
                 print("connect exception ", path, str(port))
@@ -82,12 +82,13 @@ class Ctrl:
 
     def request(self, cmd, timeout=10):
         if self.udp:
-            self.s.sendto(self.cookie + cmd, self.sockaddr)
+            self.s.sendto((self.cookie + cmd).encode(), self.sockaddr)
         else:
-            self.s.send(cmd)
+            # we specify utf-8 for utf-8 SSID
+            self.s.send(cmd.encode('utf-8'))
         [r, w, e] = select.select([self.s], [], [], timeout)
         if r:
-            return self.s.recv(4096)
+            return self.s.recv(4096).decode()
         raise Exception("Timeout on waiting response")
 
     def attach(self):
@@ -127,5 +128,5 @@ class Ctrl:
         return False
 
     def recv(self):
-        res = self.s.recv(4096)
+        res = self.s.recv(4096).decode()
         return res
-- 
2.17.1


_______________________________________________
Hostap mailing list
Hostap@xxxxxxxxxxxxxxxxxxx
http://lists.infradead.org/mailman/listinfo/hostap



[Index of Archives]     [Linux Wireless]     [Linux Kernel]     [ATH6KL]     [Linux Bluetooth]     [Linux Netdev]     [Kernel Newbies]     [IDE]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux ATA RAID]     [Samba]     [Device Mapper]

  Powered by Linux