Raise exception instead of using assert

This commit is contained in:
shortcutme 2019-07-03 18:35:55 +02:00
parent 80bfccd9d3
commit ff32f822ba
No known key found for this signature in database
GPG key ID: 5B63BAE6CB9613AE
7 changed files with 27 additions and 12 deletions

View file

@ -52,7 +52,8 @@ class CryptConnectionManager:
sock_wrapped = ssl.wrap_socket(sock, ciphers=ciphers)
if cert_pin:
cert_hash = hashlib.sha256(sock_wrapped.getpeercert(True)).hexdigest()
assert cert_hash == cert_pin, "Socket certificate does not match (%s != %s)" % (cert_hash, cert_pin)
if cert_hash != cert_pin:
raise Exception("Socket certificate does not match (%s != %s)" % (cert_hash, cert_pin))
return sock_wrapped
else:
return sock

View file

@ -33,7 +33,7 @@ class TestHelper:
with pytest.raises(socket.error):
helper.packAddress("999.1.1.1", 1)
with pytest.raises(AssertionError):
with pytest.raises(Exception):
helper.unpackAddress("X")
def testGetDirname(self):

View file

@ -159,12 +159,14 @@ class TorManager(object):
else:
res_auth = self.send("AUTHENTICATE", conn)
assert "250 OK" in res_auth, "Authenticate error %s" % res_auth
if "250 OK" not in res_auth:
raise Exception("Authenticate error %s" % res_auth)
# Version 0.2.7.5 required because ADD_ONION support
res_version = self.send("GETINFO version", conn)
version = re.search(r'version=([0-9\.]+)', res_version).group(1)
assert float(version.replace(".", "0", 2)) >= 207.5, "Tor version >=0.2.7.5 required, found: %s" % version
if float(version.replace(".", "0", 2)) < 207.5:
raise Exception("Tor version >=0.2.7.5 required, found: %s" % version)
self.setStatus("Connected (%s)" % res_auth)
self.event_started.set(True)

View file

@ -110,7 +110,8 @@ def unpackAddress(packed):
if len(packed) == 18:
return socket.inet_ntop(socket.AF_INET6, packed[0:16]), struct.unpack_from("H", packed, 16)[0]
else:
assert len(packed) == 6, "Invalid length ip4 packed address: %s" % len(packed)
if len(packed) != 6:
raise Exception("Invalid length ip4 packed address: %s" % len(packed))
return socket.inet_ntoa(packed[0:4]), struct.unpack_from("H", packed, 4)[0]