diff --git a/src/wormhole/test/test_transit.py b/src/wormhole/test/test_transit.py index e04d9f1..de9d6df 100644 --- a/src/wormhole/test/test_transit.py +++ b/src/wormhole/test/test_transit.py @@ -132,6 +132,8 @@ class Misc(unittest.TestCase): portno = transit.allocate_tcp_port() self.assertIsInstance(portno, int) +UnknownHint = namedtuple("UnknownHint", ["stuff"]) + class Hints(unittest.TestCase): def test_endpoint_from_hint_obj(self): c = transit.Common("") @@ -153,7 +155,6 @@ class Hints(unittest.TestCase): ("tor_ep", "host2.onion", 1234)) self.assertEqual(efho(transit.DirectTCPV1Hint("non-public", 1234)), None) - UnknownHint = namedtuple("UnknownHint", ["stuff"]) self.assertEqual(efho(UnknownHint("foo")), None) def test_comparable(self): @@ -185,6 +186,37 @@ class Hints(unittest.TestCase): "port": "not a number"}), None) # invalid port + def test_parse_hint_argv(self): + def p(hint): + stderr = io.StringIO() + value = transit.parse_hint_argv(hint, stderr=stderr) + return value, stderr.getvalue() + h,stderr = p("tcp:host:1234") + self.assertEqual(h, transit.DirectTCPV1Hint("host", 1234)) + self.assertEqual(stderr, "") + + h,stderr = p("$!@#^") + self.assertEqual(h, None) + self.assertEqual(stderr, "unparseable hint '$!@#^'\n") + + h,stderr = p("unknown:stuff") + self.assertEqual(h, None) + self.assertEqual(stderr, + "unknown hint type 'unknown' in 'unknown:stuff'\n") + + h,stderr = p("tcp:host:number") + self.assertEqual(h, None) + self.assertEqual(stderr, + "unparseable TCP hint 'tcp:host:number'\n") + + def test_describe_hint_obj(self): + d = transit.describe_hint_obj + self.assertEqual(d(transit.DirectTCPV1Hint("host", 1234)), + "tcp:host:1234") + self.assertEqual(d(transit.TorTCPV1Hint("host", 1234)), + "tor:host:1234") + self.assertEqual(d(UnknownHint("stuff")), str(UnknownHint("stuff"))) + class Basic(unittest.TestCase): @inlineCallbacks def test_relay_hints(self): diff --git a/src/wormhole/transit.py b/src/wormhole/transit.py index 3eb8272..284fb1b 100644 --- a/src/wormhole/transit.py +++ b/src/wormhole/transit.py @@ -103,27 +103,27 @@ def describe_hint_obj(hint): else: return str(hint) -def parse_hint_argv(hint): +def parse_hint_argv(hint, stderr=sys.stderr): assert isinstance(hint, type(u"")) # return tuple or None for an unparseable hint mo = re.search(r'^([a-zA-Z0-9]+):(.*)$', hint) if not mo: - print("unparseable hint '%s'" % (hint,)) + print("unparseable hint '%s'" % (hint,), file=stderr) return None hint_type = mo.group(1) if hint_type != "tcp": - print("unknown hint type '%s' in '%s'" % (hint_type, hint)) + print("unknown hint type '%s' in '%s'" % (hint_type, hint), file=stderr) return None hint_value = mo.group(2) mo = re.search(r'^(.*):(\d+)$', hint_value) if not mo: - print("unparseable TCP hint '%s'" % (hint,)) + print("unparseable TCP hint '%s'" % (hint,), file=stderr) return None hint_host = mo.group(1) try: hint_port = int(mo.group(2)) except ValueError: - print("non-numeric port in TCP hint '%s'" % (hint,)) + print("non-numeric port in TCP hint '%s'" % (hint,), file=stderr) return None return DirectTCPV1Hint(hint_host, hint_port)