get tests to work on py2.7

only install 'noiseprotocol' (which is necessary for dilation to work) if the
"dilate" feature is requested (e.g. `pip install magic-wormhole[dilate]`)
This commit is contained in:
Brian Warner 2018-12-24 00:07:06 -05:00
parent a458fe9ab9
commit 29c269ac8d
7 changed files with 26 additions and 10 deletions

View File

@ -48,13 +48,13 @@ setup(name="magic-wormhole",
"click", "click",
"humanize", "humanize",
"txtorcon >= 18.0.2", # 18.0.2 fixes py3.4 support "txtorcon >= 18.0.2", # 18.0.2 fixes py3.4 support
"noiseprotocol",
], ],
extras_require={ extras_require={
':sys_platform=="win32"': ["pywin32"], ':sys_platform=="win32"': ["pywin32"],
"dev": ["mock", "tox", "pyflakes", "dev": ["mock", "tox", "pyflakes",
"magic-wormhole-transit-relay==0.1.2", "magic-wormhole-transit-relay==0.1.2",
"magic-wormhole-mailbox-server==0.3.1"], "magic-wormhole-mailbox-server==0.3.1"],
"dilate": ["noiseprotocol"],
}, },
test_suite="wormhole.test", test_suite="wormhole.test",
cmdclass=commands, cmdclass=commands,

View File

@ -0,0 +1,11 @@
try:
from noise.exceptions import NoiseInvalidMessage
except ImportError:
class NoiseInvalidMessage(Exception):
pass
try:
from noise.connection import NoiseConnection
except ImportError:
# allow imports to work on py2.7, even if dilation doesn't
NoiseConnection = None

View File

@ -12,6 +12,7 @@ from .._interfaces import IDilationConnector
from ..observer import OneShotObserver from ..observer import OneShotObserver
from .encode import to_be4, from_be4 from .encode import to_be4, from_be4
from .roles import FOLLOWER from .roles import FOLLOWER
from ._noise import NoiseInvalidMessage
# InboundFraming is given data and returns Frames (Noise wire-side # InboundFraming is given data and returns Frames (Noise wire-side
# bytestrings). It handles the relay handshake and the prologue. The Frames it # bytestrings). It handles the relay handshake and the prologue. The Frames it
@ -346,7 +347,6 @@ class _Record(object):
@n.output() @n.output()
def process_handshake(self, frame): def process_handshake(self, frame):
from noise.exceptions import NoiseInvalidMessage
try: try:
payload = self._noise.read_message(frame) payload = self._noise.read_message(frame)
# Noise can include unencrypted data in the handshake, but we don't # Noise can include unencrypted data in the handshake, but we don't
@ -359,7 +359,6 @@ class _Record(object):
@n.output() @n.output()
def decrypt_message(self, frame): def decrypt_message(self, frame):
from noise.exceptions import NoiseInvalidMessage
try: try:
message = self._noise.decrypt(frame) message = self._noise.decrypt(frame)
except NoiseInvalidMessage as e: except NoiseInvalidMessage as e:

View File

@ -1,7 +1,6 @@
from __future__ import print_function, unicode_literals from __future__ import print_function, unicode_literals
from collections import defaultdict from collections import defaultdict
from binascii import hexlify from binascii import hexlify
import six
from attr import attrs, attrib from attr import attrs, attrib
from attr.validators import instance_of, provides, optional from attr.validators import instance_of, provides, optional
from automat import MethodicalMachine from automat import MethodicalMachine
@ -15,13 +14,14 @@ from .. import ipaddrs # TODO: move into _dilation/
from .._interfaces import IDilationConnector, IDilationManager from .._interfaces import IDilationConnector, IDilationManager
from ..timing import DebugTiming from ..timing import DebugTiming
from ..observer import EmptyableSet from ..observer import EmptyableSet
from ..util import HKDF from ..util import HKDF, to_unicode
from .connection import DilatedConnectionProtocol, KCM from .connection import DilatedConnectionProtocol, KCM
from .roles import LEADER from .roles import LEADER
from .._hints import (DirectTCPV1Hint, TorTCPV1Hint, RelayV1Hint, from .._hints import (DirectTCPV1Hint, TorTCPV1Hint, RelayV1Hint,
parse_hint_argv, describe_hint_obj, endpoint_from_hint_obj, parse_hint_argv, describe_hint_obj, endpoint_from_hint_obj,
encode_hint) encode_hint)
from ._noise import NoiseConnection
def build_sided_relay_handshake(key, side): def build_sided_relay_handshake(key, side):
@ -37,14 +37,13 @@ PROLOGUE_FOLLOWER = b"Magic-Wormhole Dilation Handshake v1 Follower\n\n"
NOISEPROTO = b"Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s" NOISEPROTO = b"Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s"
def build_noise(): def build_noise():
from noise.connection import NoiseConnection
return NoiseConnection.from_name(NOISEPROTO) return NoiseConnection.from_name(NOISEPROTO)
@attrs @attrs
@implementer(IDilationConnector) @implementer(IDilationConnector)
class Connector(object): class Connector(object):
_dilation_key = attrib(validator=instance_of(type(b""))) _dilation_key = attrib(validator=instance_of(type(b"")))
_transit_relay_location = attrib(validator=optional(instance_of(str))) _transit_relay_location = attrib(validator=optional(instance_of(type(u""))))
_manager = attrib(validator=provides(IDilationManager)) _manager = attrib(validator=provides(IDilationManager))
_reactor = attrib() _reactor = attrib()
_eventual_queue = attrib() _eventual_queue = attrib()
@ -265,7 +264,7 @@ class Connector(object):
# lp is an IListeningPort # lp is an IListeningPort
self._listeners.add(lp) # for shutdown and tests self._listeners.add(lp) # for shutdown and tests
portnum = lp.getHost().port portnum = lp.getHost().port
direct_hints = [DirectTCPV1Hint(six.u(addr), portnum, 0.0) direct_hints = [DirectTCPV1Hint(to_unicode(addr), portnum, 0.0)
for addr in addresses] for addr in addresses]
self.listener_ready(direct_hints) self.listener_ready(direct_hints)
d.addCallback(_listening) d.addCallback(_listening)

View File

@ -7,8 +7,9 @@ from twisted.internet.task import Clock
from twisted.internet.defer import Deferred from twisted.internet.defer import Deferred
from ...eventual import EventualQueue from ...eventual import EventualQueue
from ..._interfaces import IDilationManager, IDilationConnector from ..._interfaces import IDilationManager, IDilationConnector
from ..._dilation import roles
from ..._hints import DirectTCPV1Hint, RelayV1Hint, TorTCPV1Hint from ..._hints import DirectTCPV1Hint, RelayV1Hint, TorTCPV1Hint
from ..._dilation import roles
from ..._dilation._noise import NoiseConnection
from ..._dilation.connection import KCM from ..._dilation.connection import KCM
from ..._dilation.connector import (Connector, from ..._dilation.connector import (Connector,
build_sided_relay_handshake, build_sided_relay_handshake,
@ -101,6 +102,8 @@ class TestConnector(unittest.TestCase):
]) ])
def test_build_noise(self): def test_build_noise(self):
if not NoiseConnection:
raise unittest.SkipTest("noiseprotocol unavailable")
build_noise() build_noise()
def test_build_protocol_leader(self): def test_build_protocol_leader(self):

View File

@ -2,7 +2,7 @@ from __future__ import print_function, unicode_literals
import mock import mock
from zope.interface import alsoProvides from zope.interface import alsoProvides
from twisted.trial import unittest from twisted.trial import unittest
from noise.exceptions import NoiseInvalidMessage from ..._dilation._noise import NoiseInvalidMessage
from ..._dilation.connection import (IFramer, Frame, Prologue, from ..._dilation.connection import (IFramer, Frame, Prologue,
_Record, Handshake, _Record, Handshake,
Disconnect, Ping) Disconnect, Ping)

View File

@ -12,6 +12,10 @@ def HKDF(skm, outlen, salt=None, CTXinfo=b""):
def to_bytes(u): def to_bytes(u):
return unicodedata.normalize("NFC", u).encode("utf-8") return unicodedata.normalize("NFC", u).encode("utf-8")
def to_unicode(any):
if isinstance(any, type(u"")):
return any
return any.decode("ascii")
def bytes_to_hexstr(b): def bytes_to_hexstr(b):
assert isinstance(b, type(b"")) assert isinstance(b, type(b""))