From d1698eab2d2fd89a988a2eaeea30567b6f664c4c Mon Sep 17 00:00:00 2001 From: Renato Alves Date: Tue, 20 Jan 2015 11:23:04 +0000 Subject: [PATCH] Fix usage of taskd.trust=strict on the test suite * This reverts commit 67cb30fdce08768a13776a592d404caf1b901aac. * Code also now tests if taskd is listening on IPv4 or IPv6 interfaces. --- test/basetest/task.py | 1 - test/basetest/taskd.py | 6 +++--- test/basetest/utils.py | 34 +++++++++++++++++++++++++++++----- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/test/basetest/task.py b/test/basetest/task.py index 36d858dbc..a6921f7e3 100644 --- a/test/basetest/task.py +++ b/test/basetest/task.py @@ -99,7 +99,6 @@ class Task(object): self.config("taskd.certificate", cert) self.config("taskd.key", key) self.config("taskd.ca", self.taskd.ca_cert) - self.config("taskd.trust", "ignore hostname") address = ":".join((self.taskd.address, str(self.taskd.port))) self.config("taskd.server", address) diff --git a/test/basetest/taskd.py b/test/basetest/taskd.py index d5add67dc..209ae7da8 100644 --- a/test/basetest/taskd.py +++ b/test/basetest/taskd.py @@ -33,7 +33,7 @@ class Taskd(object): DEFAULT_TASKD = binary_location("taskd") def __init__(self, taskd=DEFAULT_TASKD, certpath=None, - address="127.0.0.1"): + address="localhost"): """Initialize a Task server that runs in the background and stores data in a temporary folder @@ -60,7 +60,7 @@ class Taskd(object): self.certpath = certpath self.address = address - self.port = find_unused_port() + self.port = find_unused_port(self.address) # Keep all certificate paths public for access by TaskClients self.client_cert = os.path.join(self.certpath, "client.cert.pem") @@ -182,7 +182,7 @@ class Taskd(object): if self.proc.poll() is not None: return False - if not port_used(port=self.port): + if not port_used(addr=self.address, port=self.port): return False return True diff --git a/test/basetest/utils.py b/test/basetest/utils.py index 482430caa..6caec070f 100644 --- a/test/basetest/utils.py +++ b/test/basetest/utils.py @@ -164,16 +164,40 @@ def run_cmd_wait_nofail(*args, **kwargs): return e.code, e.out, e.err +def get_IPs(hostname): + output = {} + addrs = socket.getaddrinfo(hostname, 0, 0, 0, socket.IPPROTO_TCP) + + for family, socktype, proto, canonname, sockaddr in addrs: + addr = sockaddr[0] + output[family] = addr + + return output + + def port_used(addr="localhost", port=None): "Return True if port is in use, False otherwise" if port is None: raise TypeError("Argument 'port' may not be None") - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - result = s.connect_ex((addr, port)) - s.close() - # result == 0 if connection was successful - return result == 0 + # If we got an address name, resolve it both to IPv6 and IPv4. + IPs = get_IPs(addr) + + # Taskd seems to prefer IPv6 so we do it first + for family in (socket.AF_INET6, socket.AF_INET): + try: + addr = IPs[family] + except KeyError: + continue + + s = socket.socket(family, socket.SOCK_STREAM) + result = s.connect_ex((addr, port)) + s.close() + if result == 0: + # connection was successful + return True + else: + return False def find_unused_port(addr="localhost", start=53589, track=True):