From 8ba0ee2f1de89f85ff71ec015c741b5b300dc5f8 Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Wed, 17 Jul 2019 18:11:17 +0000 Subject: [PATCH] robustify the auth test: use the new challenge-handlers argument with client commands to specify the handler more stricly, delete temp files as soon as possible, parse session and env options using binary strings git-svn-id: https://xpra.org/svn/Xpra/trunk@23188 3bb7dfac-3a0b-4e04-842a-767bc560f471 --- src/unittests/unit/process_test_util.py | 10 ++++++++ src/unittests/unit/server/server_auth_test.py | 25 +++++++++++-------- src/xpra/server/auth/multifile_auth.py | 8 +++--- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/unittests/unit/process_test_util.py b/src/unittests/unit/process_test_util.py index 0cea27ad46..a3a7f6a8f9 100755 --- a/src/unittests/unit/process_test_util.py +++ b/src/unittests/unit/process_test_util.py @@ -201,6 +201,16 @@ def _temp_file(self, data=None, prefix="xpra-"): self.temp_files.append(f.name) return f + def delete_temp_file(self, f): + try: + self.temp_files.remove(f.name) + except ValueError: + pass + try: + os.unlink(f.name) + except OSError as e: + log.error("Error deleting temp file '%s': %s", f.name, e) + @classmethod def find_X11_display_numbers(cls): diff --git a/src/unittests/unit/server/server_auth_test.py b/src/unittests/unit/server/server_auth_test.py index 604e0a182d..54447cff35 100755 --- a/src/unittests/unit/server/server_auth_test.py +++ b/src/unittests/unit/server/server_auth_test.py @@ -6,7 +6,8 @@ import os import unittest -from xpra.os_util import pollwait, OSX, POSIX + +from xpra.os_util import pollwait, strtobytes, OSX, POSIX from xpra.exit_codes import EXIT_OK, EXIT_FAILURE, EXIT_PASSWORD_REQUIRED, EXIT_NO_AUTHENTICATION from unit.server_test_util import ServerTestUtil, log @@ -25,13 +26,17 @@ def _test_auth(self, auth="fail", uri_prefix="", exit_code=0, password=None): #try to connect cmd = ["info", uri_prefix+display] f = None - if password: - f = self._temp_file(password) - cmd += ["--password-file=%s" % f.name] - client = self.run_xpra(cmd) - r = pollwait(client, 5) - if f: - f.close() + try: + if password: + f = self._temp_file(strtobytes(password)) + cmd += ["--password-file=%s" % f.name] + cmd += ["--challenge-handlers=file:filename=%s" % f.name] + client = self.run_xpra(cmd) + r = pollwait(client, 5) + finally: + if f: + f.close() + self.delete_temp_file(f) if client.poll() is None: client.terminate() server.terminate() @@ -54,7 +59,7 @@ def test_allow(self): def test_file(self): from xpra.os_util import get_hex_uuid password = get_hex_uuid() - f = self._temp_file(password) + f = self._temp_file(strtobytes(password)) self._test_auth("file", "", EXIT_PASSWORD_REQUIRED) self._test_auth("file:filename=%s" % f.name, "", EXIT_PASSWORD_REQUIRED) self._test_auth("file:filename=%s" % f.name, "", EXIT_OK, password) @@ -68,7 +73,7 @@ def test_multifile(self): password = get_hex_uuid() displays = "" data = "%s|%s|%i|%i|%s||" % (username, password, os.getuid(), os.getgid(), displays) - f = self._temp_file(data) + f = self._temp_file(strtobytes(data)) self._test_auth("multifile", "", EXIT_PASSWORD_REQUIRED) self._test_auth("multifile:filename=%s" % f.name, "", EXIT_PASSWORD_REQUIRED) self._test_auth("multifile:filename=%s" % f.name, "", EXIT_OK, password) diff --git a/src/xpra/server/auth/multifile_auth.py b/src/xpra/server/auth/multifile_auth.py index ff891277e1..8a9ed8b632 100644 --- a/src/xpra/server/auth/multifile_auth.py +++ b/src/xpra/server/auth/multifile_auth.py @@ -8,7 +8,7 @@ from xpra.server.auth.sys_auth_base import parse_uid, parse_gid from xpra.server.auth.file_auth_base import log, FileAuthenticatorBase, init -from xpra.os_util import strtobytes, hexstr +from xpra.os_util import strtobytes, bytestostr, hexstr from xpra.util import parse_simple_dict from xpra.net.digest import verify_digest assert init and log #tests will disable logging from here @@ -33,9 +33,9 @@ def parse_auth_line(line): env_options = {} session_options = {} if len(ldata)>=6: - env_options = parse_simple_dict(ldata[5], ";") + env_options = parse_simple_dict(ldata[5], b";") if len(ldata)>=7: - session_options = parse_simple_dict(ldata[6], ";") + session_options = parse_simple_dict(ldata[6], b";") return username, password, uid, gid, displays, env_options, session_options @@ -66,7 +66,7 @@ def parse_filedata(self, data): except Exception as e: log("parsing error", exc_info=True) log.error("Error parsing password file '%s' at line %i:", self.password_filename, i) - log.error(" '%s'", line) + log.error(" '%s'", bytestostr(line)) log.error(" %s", e) continue log("parsed auth data from file %s: %s", self.password_filename, auth_data)