more coverage: parse_hint_argv
This commit is contained in:
parent
fe6ff466d8
commit
1abe733a81
|
@ -132,6 +132,8 @@ class Misc(unittest.TestCase):
|
||||||
portno = transit.allocate_tcp_port()
|
portno = transit.allocate_tcp_port()
|
||||||
self.assertIsInstance(portno, int)
|
self.assertIsInstance(portno, int)
|
||||||
|
|
||||||
|
UnknownHint = namedtuple("UnknownHint", ["stuff"])
|
||||||
|
|
||||||
class Hints(unittest.TestCase):
|
class Hints(unittest.TestCase):
|
||||||
def test_endpoint_from_hint_obj(self):
|
def test_endpoint_from_hint_obj(self):
|
||||||
c = transit.Common("")
|
c = transit.Common("")
|
||||||
|
@ -153,7 +155,6 @@ class Hints(unittest.TestCase):
|
||||||
("tor_ep", "host2.onion", 1234))
|
("tor_ep", "host2.onion", 1234))
|
||||||
self.assertEqual(efho(transit.DirectTCPV1Hint("non-public", 1234)),
|
self.assertEqual(efho(transit.DirectTCPV1Hint("non-public", 1234)),
|
||||||
None)
|
None)
|
||||||
UnknownHint = namedtuple("UnknownHint", ["stuff"])
|
|
||||||
self.assertEqual(efho(UnknownHint("foo")), None)
|
self.assertEqual(efho(UnknownHint("foo")), None)
|
||||||
|
|
||||||
def test_comparable(self):
|
def test_comparable(self):
|
||||||
|
@ -185,6 +186,37 @@ class Hints(unittest.TestCase):
|
||||||
"port": "not a number"}),
|
"port": "not a number"}),
|
||||||
None) # invalid port
|
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):
|
class Basic(unittest.TestCase):
|
||||||
@inlineCallbacks
|
@inlineCallbacks
|
||||||
def test_relay_hints(self):
|
def test_relay_hints(self):
|
||||||
|
|
|
@ -103,27 +103,27 @@ def describe_hint_obj(hint):
|
||||||
else:
|
else:
|
||||||
return str(hint)
|
return str(hint)
|
||||||
|
|
||||||
def parse_hint_argv(hint):
|
def parse_hint_argv(hint, stderr=sys.stderr):
|
||||||
assert isinstance(hint, type(u""))
|
assert isinstance(hint, type(u""))
|
||||||
# return tuple or None for an unparseable hint
|
# return tuple or None for an unparseable hint
|
||||||
mo = re.search(r'^([a-zA-Z0-9]+):(.*)$', hint)
|
mo = re.search(r'^([a-zA-Z0-9]+):(.*)$', hint)
|
||||||
if not mo:
|
if not mo:
|
||||||
print("unparseable hint '%s'" % (hint,))
|
print("unparseable hint '%s'" % (hint,), file=stderr)
|
||||||
return None
|
return None
|
||||||
hint_type = mo.group(1)
|
hint_type = mo.group(1)
|
||||||
if hint_type != "tcp":
|
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
|
return None
|
||||||
hint_value = mo.group(2)
|
hint_value = mo.group(2)
|
||||||
mo = re.search(r'^(.*):(\d+)$', hint_value)
|
mo = re.search(r'^(.*):(\d+)$', hint_value)
|
||||||
if not mo:
|
if not mo:
|
||||||
print("unparseable TCP hint '%s'" % (hint,))
|
print("unparseable TCP hint '%s'" % (hint,), file=stderr)
|
||||||
return None
|
return None
|
||||||
hint_host = mo.group(1)
|
hint_host = mo.group(1)
|
||||||
try:
|
try:
|
||||||
hint_port = int(mo.group(2))
|
hint_port = int(mo.group(2))
|
||||||
except ValueError:
|
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 None
|
||||||
return DirectTCPV1Hint(hint_host, hint_port)
|
return DirectTCPV1Hint(hint_host, hint_port)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user