2016-06-04 06:07:50 +00:00
|
|
|
from __future__ import print_function, unicode_literals
|
2017-04-16 14:56:17 +00:00
|
|
|
import io, re
|
2016-05-22 18:31:00 +00:00
|
|
|
import mock
|
|
|
|
from twisted.trial import unittest
|
|
|
|
from twisted.internet import reactor
|
2017-04-16 16:25:37 +00:00
|
|
|
from twisted.internet.defer import gatherResults, inlineCallbacks, returnValue
|
|
|
|
from twisted.internet.error import ConnectionRefusedError
|
add w.when_key(), fix w.when_verified() to fire later
Previously, w.when_verified() was documented to fire only after a valid
encrypted message was received, but in fact it fired as soon as the shared
key was derived (before any encrypted messages are seen, so no actual
"verification" could occur yet).
This fixes that, and also adds a new w.when_key() API call which fires at the
earlier point. Having something which fires early is useful for the CLI
commands that want to print a pacifier message when the peer is responding
slowly. In particular it helps detect the case where 'wormhole send' has quit
early (after depositing the PAKE message on the server, but before the
receiver has started). In this case, the receiver will compute the shared
key, but then wait forever hoping for a VERSION that will never come. By
starting a timer when w.when_key() fires, and cancelling it when
w.when_verified() fires, we have a good place to tell the user that something
is taking longer than it should have.
This shifts responsibility for notifying Boss.got_verifier, out of Key and
into Receive, since Receive is what notices the first valid encrypted
message. It also shifts the Boss's ordering expectations: it now receives
B.happy() before B.got_verifier(), and consequently got_verifier ought to
arrive in the S2_happy state rather than S1_lonely.
2017-04-07 01:27:41 +00:00
|
|
|
from .common import ServerBase, poll_until, pause_one_tick
|
2017-03-07 11:34:36 +00:00
|
|
|
from .. import wormhole, _rendezvous
|
2017-04-16 16:25:37 +00:00
|
|
|
from ..errors import (WrongPasswordError, ServerConnectionError,
|
2017-03-08 07:45:11 +00:00
|
|
|
KeyFormatError, WormholeClosed, LonelyError,
|
|
|
|
NoKeyError, OnlyOneCodeError)
|
2017-04-16 16:25:37 +00:00
|
|
|
from ..transit import allocate_tcp_port
|
2016-05-22 18:31:00 +00:00
|
|
|
|
2016-06-04 20:16:09 +00:00
|
|
|
APPID = "appid"
|
2016-05-22 18:31:00 +00:00
|
|
|
|
2016-05-23 01:40:44 +00:00
|
|
|
# event orderings to exercise:
|
|
|
|
#
|
|
|
|
# * normal sender: set_code, send_phase1, connected, claimed, learn_msg2,
|
|
|
|
# learn_phase1
|
|
|
|
# * normal receiver (argv[2]=code): set_code, connected, learn_msg1,
|
|
|
|
# learn_phase1, send_phase1,
|
|
|
|
# * normal receiver (readline): connected, input_code
|
|
|
|
# *
|
|
|
|
# * set_code, then connected
|
|
|
|
# * connected, receive_pake, send_phase, set_code
|
|
|
|
|
2017-04-04 07:05:28 +00:00
|
|
|
class Delegate:
|
|
|
|
def __init__(self):
|
2017-05-12 20:12:36 +00:00
|
|
|
self.welcome = None
|
2017-04-04 07:05:28 +00:00
|
|
|
self.code = None
|
2017-04-19 02:50:09 +00:00
|
|
|
self.key = None
|
2017-04-04 07:05:28 +00:00
|
|
|
self.verifier = None
|
2017-05-12 20:12:36 +00:00
|
|
|
self.versions = None
|
2017-04-04 07:05:28 +00:00
|
|
|
self.messages = []
|
|
|
|
self.closed = None
|
2017-05-12 20:12:36 +00:00
|
|
|
def wormhole_got_welcome(self, welcome):
|
|
|
|
self.welcome = welcome
|
|
|
|
def wormhole_got_code(self, code):
|
2017-04-04 07:05:28 +00:00
|
|
|
self.code = code
|
2017-05-12 20:12:36 +00:00
|
|
|
def wormhole_got_unverified_key(self, key):
|
2017-04-19 02:50:09 +00:00
|
|
|
self.key = key
|
2017-05-12 20:12:36 +00:00
|
|
|
def wormhole_got_verifier(self, verifier):
|
2017-04-04 07:05:28 +00:00
|
|
|
self.verifier = verifier
|
2017-05-12 20:12:36 +00:00
|
|
|
def wormhole_got_versions(self, versions):
|
|
|
|
self.versions = versions
|
|
|
|
def wormhole_got_message(self, data):
|
2017-04-04 07:05:28 +00:00
|
|
|
self.messages.append(data)
|
|
|
|
def wormhole_closed(self, result):
|
|
|
|
self.closed = result
|
|
|
|
|
|
|
|
class Delegated(ServerBase, unittest.TestCase):
|
|
|
|
|
2017-04-19 02:50:09 +00:00
|
|
|
@inlineCallbacks
|
2017-04-04 07:05:28 +00:00
|
|
|
def test_delegated(self):
|
|
|
|
dg = Delegate()
|
2017-04-19 02:50:09 +00:00
|
|
|
w1 = wormhole.create(APPID, self.relayurl, reactor, delegate=dg)
|
|
|
|
#w1.debug_set_trace("W1")
|
|
|
|
with self.assertRaises(NoKeyError):
|
|
|
|
w1.derive_key("purpose", 12)
|
|
|
|
w1.set_code("1-abc")
|
|
|
|
self.assertEqual(dg.code, "1-abc")
|
|
|
|
w2 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
w2.set_code(dg.code)
|
|
|
|
yield poll_until(lambda: dg.key is not None)
|
|
|
|
yield poll_until(lambda: dg.verifier is not None)
|
2017-05-12 20:12:36 +00:00
|
|
|
yield poll_until(lambda: dg.versions is not None)
|
2017-04-19 02:50:09 +00:00
|
|
|
|
2017-05-12 20:12:36 +00:00
|
|
|
w1.send_message(b"ping")
|
|
|
|
got = yield w2.get_message()
|
2017-04-19 02:50:09 +00:00
|
|
|
self.assertEqual(got, b"ping")
|
2017-05-12 20:12:36 +00:00
|
|
|
w2.send_message(b"pong")
|
2017-04-19 02:50:09 +00:00
|
|
|
yield poll_until(lambda: dg.messages)
|
|
|
|
self.assertEqual(dg.messages[0], b"pong")
|
|
|
|
|
|
|
|
key1 = w1.derive_key("purpose", 16)
|
|
|
|
self.assertEqual(len(key1), 16)
|
|
|
|
self.assertEqual(type(key1), type(b""))
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
w1.derive_key(b"not unicode", 16)
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
w1.derive_key(12345, 16)
|
|
|
|
|
|
|
|
w1.close()
|
|
|
|
yield w2.close()
|
|
|
|
|
|
|
|
@inlineCallbacks
|
|
|
|
def test_allocate_code(self):
|
|
|
|
dg = Delegate()
|
|
|
|
w1 = wormhole.create(APPID, self.relayurl, reactor, delegate=dg)
|
|
|
|
w1.allocate_code()
|
|
|
|
yield poll_until(lambda: dg.code is not None)
|
|
|
|
w1.close()
|
|
|
|
|
|
|
|
@inlineCallbacks
|
|
|
|
def test_input_code(self):
|
|
|
|
dg = Delegate()
|
|
|
|
w1 = wormhole.create(APPID, self.relayurl, reactor, delegate=dg)
|
|
|
|
h = w1.input_code()
|
|
|
|
h.choose_nameplate("123")
|
|
|
|
h.choose_words("purple-elephant")
|
|
|
|
yield poll_until(lambda: dg.code is not None)
|
|
|
|
w1.close()
|
2017-04-04 07:05:28 +00:00
|
|
|
|
2016-05-23 01:40:44 +00:00
|
|
|
class Wormholes(ServerBase, unittest.TestCase):
|
|
|
|
# integration test, with a real server
|
|
|
|
|
2017-06-26 13:34:07 +00:00
|
|
|
def setUp(self):
|
|
|
|
# test_welcome wants to see [current_cli_version]
|
|
|
|
self._setup_relay(None, advertise_version="advertised.version")
|
|
|
|
|
2016-05-23 01:40:44 +00:00
|
|
|
def doBoth(self, d1, d2):
|
|
|
|
return gatherResults([d1, d2], True)
|
|
|
|
|
2017-04-04 07:05:28 +00:00
|
|
|
@inlineCallbacks
|
|
|
|
def test_allocate_default(self):
|
|
|
|
w1 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
w1.allocate_code()
|
2017-05-12 20:12:36 +00:00
|
|
|
code = yield w1.get_code()
|
2017-04-04 07:05:28 +00:00
|
|
|
mo = re.search(r"^\d+-\w+-\w+$", code)
|
|
|
|
self.assert_(mo, code)
|
|
|
|
# w.close() fails because we closed before connecting
|
|
|
|
yield self.assertFailure(w1.close(), LonelyError)
|
|
|
|
|
|
|
|
@inlineCallbacks
|
|
|
|
def test_allocate_more_words(self):
|
|
|
|
w1 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
w1.allocate_code(3)
|
2017-05-12 20:12:36 +00:00
|
|
|
code = yield w1.get_code()
|
2017-04-04 07:05:28 +00:00
|
|
|
mo = re.search(r"^\d+-\w+-\w+-\w+$", code)
|
|
|
|
self.assert_(mo, code)
|
|
|
|
yield self.assertFailure(w1.close(), LonelyError)
|
|
|
|
|
2016-05-23 01:40:44 +00:00
|
|
|
@inlineCallbacks
|
|
|
|
def test_basic(self):
|
2017-03-07 11:34:36 +00:00
|
|
|
w1 = wormhole.create(APPID, self.relayurl, reactor)
|
2017-04-04 07:05:28 +00:00
|
|
|
#w1.debug_set_trace("W1")
|
2017-04-19 02:50:59 +00:00
|
|
|
with self.assertRaises(NoKeyError):
|
|
|
|
w1.derive_key("purpose", 12)
|
|
|
|
|
2017-03-07 11:34:36 +00:00
|
|
|
w2 = wormhole.create(APPID, self.relayurl, reactor)
|
2017-04-04 07:05:28 +00:00
|
|
|
#w2.debug_set_trace(" W2")
|
2017-03-07 11:34:36 +00:00
|
|
|
w1.allocate_code()
|
2017-05-12 20:12:36 +00:00
|
|
|
code = yield w1.get_code()
|
2016-05-23 01:40:44 +00:00
|
|
|
w2.set_code(code)
|
2017-04-04 07:05:28 +00:00
|
|
|
|
2017-05-12 20:12:36 +00:00
|
|
|
yield w1.get_unverified_key()
|
|
|
|
yield w2.get_unverified_key()
|
add w.when_key(), fix w.when_verified() to fire later
Previously, w.when_verified() was documented to fire only after a valid
encrypted message was received, but in fact it fired as soon as the shared
key was derived (before any encrypted messages are seen, so no actual
"verification" could occur yet).
This fixes that, and also adds a new w.when_key() API call which fires at the
earlier point. Having something which fires early is useful for the CLI
commands that want to print a pacifier message when the peer is responding
slowly. In particular it helps detect the case where 'wormhole send' has quit
early (after depositing the PAKE message on the server, but before the
receiver has started). In this case, the receiver will compute the shared
key, but then wait forever hoping for a VERSION that will never come. By
starting a timer when w.when_key() fires, and cancelling it when
w.when_verified() fires, we have a good place to tell the user that something
is taking longer than it should have.
This shifts responsibility for notifying Boss.got_verifier, out of Key and
into Receive, since Receive is what notices the first valid encrypted
message. It also shifts the Boss's ordering expectations: it now receives
B.happy() before B.got_verifier(), and consequently got_verifier ought to
arrive in the S2_happy state rather than S1_lonely.
2017-04-07 01:27:41 +00:00
|
|
|
|
2017-04-19 02:50:59 +00:00
|
|
|
key1 = w1.derive_key("purpose", 16)
|
|
|
|
self.assertEqual(len(key1), 16)
|
|
|
|
self.assertEqual(type(key1), type(b""))
|
2017-04-17 21:15:35 +00:00
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
w1.derive_key(b"not unicode", 16)
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
w1.derive_key(12345, 16)
|
|
|
|
|
2017-05-12 20:12:36 +00:00
|
|
|
verifier1 = yield w1.get_verifier()
|
|
|
|
verifier2 = yield w2.get_verifier()
|
2017-04-04 07:05:28 +00:00
|
|
|
self.assertEqual(verifier1, verifier2)
|
|
|
|
|
2017-05-12 20:12:36 +00:00
|
|
|
self.successResultOf(w1.get_unverified_key())
|
|
|
|
self.successResultOf(w2.get_unverified_key())
|
add w.when_key(), fix w.when_verified() to fire later
Previously, w.when_verified() was documented to fire only after a valid
encrypted message was received, but in fact it fired as soon as the shared
key was derived (before any encrypted messages are seen, so no actual
"verification" could occur yet).
This fixes that, and also adds a new w.when_key() API call which fires at the
earlier point. Having something which fires early is useful for the CLI
commands that want to print a pacifier message when the peer is responding
slowly. In particular it helps detect the case where 'wormhole send' has quit
early (after depositing the PAKE message on the server, but before the
receiver has started). In this case, the receiver will compute the shared
key, but then wait forever hoping for a VERSION that will never come. By
starting a timer when w.when_key() fires, and cancelling it when
w.when_verified() fires, we have a good place to tell the user that something
is taking longer than it should have.
This shifts responsibility for notifying Boss.got_verifier, out of Key and
into Receive, since Receive is what notices the first valid encrypted
message. It also shifts the Boss's ordering expectations: it now receives
B.happy() before B.got_verifier(), and consequently got_verifier ought to
arrive in the S2_happy state rather than S1_lonely.
2017-04-07 01:27:41 +00:00
|
|
|
|
2017-05-12 20:12:36 +00:00
|
|
|
versions1 = yield w1.get_versions()
|
|
|
|
versions2 = yield w2.get_versions()
|
2017-04-07 02:44:27 +00:00
|
|
|
# app-versions are exercised properly in test_versions, this just
|
|
|
|
# tests the defaults
|
2017-05-12 20:12:36 +00:00
|
|
|
self.assertEqual(versions1, {})
|
|
|
|
self.assertEqual(versions2, {})
|
2017-04-04 07:05:28 +00:00
|
|
|
|
2017-05-12 20:12:36 +00:00
|
|
|
w1.send_message(b"data1")
|
|
|
|
w2.send_message(b"data2")
|
|
|
|
dataX = yield w1.get_message()
|
|
|
|
dataY = yield w2.get_message()
|
2016-05-23 01:40:44 +00:00
|
|
|
self.assertEqual(dataX, b"data2")
|
|
|
|
self.assertEqual(dataY, b"data1")
|
2017-04-04 07:05:28 +00:00
|
|
|
|
2017-05-12 20:12:36 +00:00
|
|
|
versions1_again = yield w1.get_versions()
|
|
|
|
self.assertEqual(versions1, versions1_again)
|
2017-04-04 07:05:28 +00:00
|
|
|
|
|
|
|
c1 = yield w1.close()
|
|
|
|
self.assertEqual(c1, "happy")
|
|
|
|
c2 = yield w2.close()
|
|
|
|
self.assertEqual(c2, "happy")
|
|
|
|
|
|
|
|
@inlineCallbacks
|
2017-05-12 20:12:36 +00:00
|
|
|
def test_get_code_early(self):
|
2017-04-04 07:05:28 +00:00
|
|
|
w1 = wormhole.create(APPID, self.relayurl, reactor)
|
2017-05-12 20:12:36 +00:00
|
|
|
d = w1.get_code()
|
2017-04-04 07:05:28 +00:00
|
|
|
w1.set_code("1-abc")
|
|
|
|
code = self.successResultOf(d)
|
|
|
|
self.assertEqual(code, "1-abc")
|
|
|
|
yield self.assertFailure(w1.close(), LonelyError)
|
|
|
|
|
|
|
|
@inlineCallbacks
|
2017-05-12 20:12:36 +00:00
|
|
|
def test_get_code_late(self):
|
2017-04-04 07:05:28 +00:00
|
|
|
w1 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
w1.set_code("1-abc")
|
2017-05-12 20:12:36 +00:00
|
|
|
d = w1.get_code()
|
2017-04-04 07:05:28 +00:00
|
|
|
code = self.successResultOf(d)
|
|
|
|
self.assertEqual(code, "1-abc")
|
|
|
|
yield self.assertFailure(w1.close(), LonelyError)
|
2016-05-23 01:40:44 +00:00
|
|
|
|
|
|
|
@inlineCallbacks
|
|
|
|
def test_same_message(self):
|
|
|
|
# the two sides use random nonces for their messages, so it's ok for
|
|
|
|
# both to try and send the same body: they'll result in distinct
|
|
|
|
# encrypted messages
|
2017-03-07 11:34:36 +00:00
|
|
|
w1 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
w2 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
w1.allocate_code()
|
2017-05-12 20:12:36 +00:00
|
|
|
code = yield w1.get_code()
|
2016-05-23 01:40:44 +00:00
|
|
|
w2.set_code(code)
|
2017-05-12 20:12:36 +00:00
|
|
|
w1.send_message(b"data")
|
|
|
|
w2.send_message(b"data")
|
|
|
|
dataX = yield w1.get_message()
|
|
|
|
dataY = yield w2.get_message()
|
2016-05-23 01:40:44 +00:00
|
|
|
self.assertEqual(dataX, b"data")
|
|
|
|
self.assertEqual(dataY, b"data")
|
2016-05-24 06:59:49 +00:00
|
|
|
yield w1.close()
|
|
|
|
yield w2.close()
|
2016-05-23 01:40:44 +00:00
|
|
|
|
|
|
|
@inlineCallbacks
|
|
|
|
def test_interleaved(self):
|
2017-03-07 11:34:36 +00:00
|
|
|
w1 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
w2 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
w1.allocate_code()
|
2017-05-12 20:12:36 +00:00
|
|
|
code = yield w1.get_code()
|
2016-05-23 01:40:44 +00:00
|
|
|
w2.set_code(code)
|
2017-05-12 20:12:36 +00:00
|
|
|
w1.send_message(b"data1")
|
|
|
|
dataY = yield w2.get_message()
|
2016-05-23 01:40:44 +00:00
|
|
|
self.assertEqual(dataY, b"data1")
|
2017-05-12 20:12:36 +00:00
|
|
|
d = w1.get_message()
|
|
|
|
w2.send_message(b"data2")
|
2016-05-24 05:53:00 +00:00
|
|
|
dataX = yield d
|
2016-05-23 01:40:44 +00:00
|
|
|
self.assertEqual(dataX, b"data2")
|
2016-05-24 06:59:49 +00:00
|
|
|
yield w1.close()
|
|
|
|
yield w2.close()
|
2016-05-24 05:53:00 +00:00
|
|
|
|
|
|
|
@inlineCallbacks
|
|
|
|
def test_unidirectional(self):
|
2017-03-07 11:34:36 +00:00
|
|
|
w1 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
w2 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
w1.allocate_code()
|
2017-05-12 20:12:36 +00:00
|
|
|
code = yield w1.get_code()
|
2016-05-24 05:53:00 +00:00
|
|
|
w2.set_code(code)
|
2017-05-12 20:12:36 +00:00
|
|
|
w1.send_message(b"data1")
|
|
|
|
dataY = yield w2.get_message()
|
2016-05-24 05:53:00 +00:00
|
|
|
self.assertEqual(dataY, b"data1")
|
2016-05-24 06:59:49 +00:00
|
|
|
yield w1.close()
|
|
|
|
yield w2.close()
|
2016-05-24 05:53:00 +00:00
|
|
|
|
|
|
|
@inlineCallbacks
|
|
|
|
def test_early(self):
|
2017-03-07 11:34:36 +00:00
|
|
|
w1 = wormhole.create(APPID, self.relayurl, reactor)
|
2017-05-12 20:12:36 +00:00
|
|
|
w1.send_message(b"data1")
|
2017-03-07 11:34:36 +00:00
|
|
|
w2 = wormhole.create(APPID, self.relayurl, reactor)
|
2017-05-12 20:12:36 +00:00
|
|
|
d = w2.get_message()
|
2016-06-04 20:16:09 +00:00
|
|
|
w1.set_code("123-abc-def")
|
|
|
|
w2.set_code("123-abc-def")
|
2016-05-24 05:53:00 +00:00
|
|
|
dataY = yield d
|
|
|
|
self.assertEqual(dataY, b"data1")
|
2016-05-24 06:59:49 +00:00
|
|
|
yield w1.close()
|
|
|
|
yield w2.close()
|
2016-05-23 01:40:44 +00:00
|
|
|
|
|
|
|
@inlineCallbacks
|
|
|
|
def test_fixed_code(self):
|
2017-03-07 11:34:36 +00:00
|
|
|
w1 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
w2 = wormhole.create(APPID, self.relayurl, reactor)
|
2016-06-04 20:16:09 +00:00
|
|
|
w1.set_code("123-purple-elephant")
|
|
|
|
w2.set_code("123-purple-elephant")
|
2017-05-12 20:12:36 +00:00
|
|
|
w1.send_message(b"data1"), w2.send_message(b"data2")
|
|
|
|
dl = yield self.doBoth(w1.get_message(), w2.get_message())
|
2016-05-23 01:40:44 +00:00
|
|
|
(dataX, dataY) = dl
|
|
|
|
self.assertEqual(dataX, b"data2")
|
|
|
|
self.assertEqual(dataY, b"data1")
|
2016-05-24 06:59:49 +00:00
|
|
|
yield w1.close()
|
|
|
|
yield w2.close()
|
2016-05-23 01:40:44 +00:00
|
|
|
|
2017-04-06 17:44:04 +00:00
|
|
|
@inlineCallbacks
|
|
|
|
def test_input_code(self):
|
|
|
|
w1 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
w2 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
w1.set_code("123-purple-elephant")
|
|
|
|
h = w2.input_code()
|
|
|
|
h.choose_nameplate("123")
|
|
|
|
# Pause to allow some messages to get delivered. Specifically we want
|
|
|
|
# to wait until w2 claims the nameplate, opens the mailbox, and
|
|
|
|
# receives the PAKE message, to exercise the PAKE-before-CODE path in
|
|
|
|
# Key.
|
|
|
|
yield poll_until(lambda: w2._boss._K._debug_pake_stashed)
|
|
|
|
h.choose_words("purple-elephant")
|
|
|
|
|
2017-05-12 20:12:36 +00:00
|
|
|
w1.send_message(b"data1"), w2.send_message(b"data2")
|
|
|
|
dl = yield self.doBoth(w1.get_message(), w2.get_message())
|
2017-04-06 17:44:04 +00:00
|
|
|
(dataX, dataY) = dl
|
|
|
|
self.assertEqual(dataX, b"data2")
|
|
|
|
self.assertEqual(dataY, b"data1")
|
|
|
|
yield w1.close()
|
|
|
|
yield w2.close()
|
|
|
|
|
2016-05-23 01:40:44 +00:00
|
|
|
|
|
|
|
@inlineCallbacks
|
|
|
|
def test_multiple_messages(self):
|
2017-03-07 11:34:36 +00:00
|
|
|
w1 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
w2 = wormhole.create(APPID, self.relayurl, reactor)
|
2016-06-04 20:16:09 +00:00
|
|
|
w1.set_code("123-purple-elephant")
|
|
|
|
w2.set_code("123-purple-elephant")
|
2017-05-12 20:12:36 +00:00
|
|
|
w1.send_message(b"data1"), w2.send_message(b"data2")
|
|
|
|
w1.send_message(b"data3"), w2.send_message(b"data4")
|
|
|
|
dl = yield self.doBoth(w1.get_message(), w2.get_message())
|
2016-05-23 01:40:44 +00:00
|
|
|
(dataX, dataY) = dl
|
|
|
|
self.assertEqual(dataX, b"data2")
|
|
|
|
self.assertEqual(dataY, b"data1")
|
2017-05-12 20:12:36 +00:00
|
|
|
dl = yield self.doBoth(w1.get_message(), w2.get_message())
|
2016-05-23 01:40:44 +00:00
|
|
|
(dataX, dataY) = dl
|
|
|
|
self.assertEqual(dataX, b"data4")
|
|
|
|
self.assertEqual(dataY, b"data3")
|
2016-05-24 06:59:49 +00:00
|
|
|
yield w1.close()
|
|
|
|
yield w2.close()
|
2016-05-23 01:40:44 +00:00
|
|
|
|
2017-03-07 11:34:36 +00:00
|
|
|
|
|
|
|
@inlineCallbacks
|
|
|
|
def test_closed(self):
|
|
|
|
w1 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
w2 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
w1.set_code("123-foo")
|
|
|
|
w2.set_code("123-foo")
|
|
|
|
|
|
|
|
# let it connect and become HAPPY
|
2017-05-12 20:12:36 +00:00
|
|
|
yield w1.get_versions()
|
|
|
|
yield w2.get_versions()
|
2017-03-07 11:34:36 +00:00
|
|
|
|
|
|
|
yield w1.close()
|
|
|
|
yield w2.close()
|
|
|
|
|
add w.when_key(), fix w.when_verified() to fire later
Previously, w.when_verified() was documented to fire only after a valid
encrypted message was received, but in fact it fired as soon as the shared
key was derived (before any encrypted messages are seen, so no actual
"verification" could occur yet).
This fixes that, and also adds a new w.when_key() API call which fires at the
earlier point. Having something which fires early is useful for the CLI
commands that want to print a pacifier message when the peer is responding
slowly. In particular it helps detect the case where 'wormhole send' has quit
early (after depositing the PAKE message on the server, but before the
receiver has started). In this case, the receiver will compute the shared
key, but then wait forever hoping for a VERSION that will never come. By
starting a timer when w.when_key() fires, and cancelling it when
w.when_verified() fires, we have a good place to tell the user that something
is taking longer than it should have.
This shifts responsibility for notifying Boss.got_verifier, out of Key and
into Receive, since Receive is what notices the first valid encrypted
message. It also shifts the Boss's ordering expectations: it now receives
B.happy() before B.got_verifier(), and consequently got_verifier ought to
arrive in the S2_happy state rather than S1_lonely.
2017-04-07 01:27:41 +00:00
|
|
|
# once closed, all Deferred-yielding API calls get an immediate error
|
2017-05-15 23:10:22 +00:00
|
|
|
self.failureResultOf(w1.get_welcome(), WormholeClosed)
|
2017-05-12 20:12:36 +00:00
|
|
|
f = self.failureResultOf(w1.get_code(), WormholeClosed)
|
add w.when_key(), fix w.when_verified() to fire later
Previously, w.when_verified() was documented to fire only after a valid
encrypted message was received, but in fact it fired as soon as the shared
key was derived (before any encrypted messages are seen, so no actual
"verification" could occur yet).
This fixes that, and also adds a new w.when_key() API call which fires at the
earlier point. Having something which fires early is useful for the CLI
commands that want to print a pacifier message when the peer is responding
slowly. In particular it helps detect the case where 'wormhole send' has quit
early (after depositing the PAKE message on the server, but before the
receiver has started). In this case, the receiver will compute the shared
key, but then wait forever hoping for a VERSION that will never come. By
starting a timer when w.when_key() fires, and cancelling it when
w.when_verified() fires, we have a good place to tell the user that something
is taking longer than it should have.
This shifts responsibility for notifying Boss.got_verifier, out of Key and
into Receive, since Receive is what notices the first valid encrypted
message. It also shifts the Boss's ordering expectations: it now receives
B.happy() before B.got_verifier(), and consequently got_verifier ought to
arrive in the S2_happy state rather than S1_lonely.
2017-04-07 01:27:41 +00:00
|
|
|
self.assertEqual(f.value.args[0], "happy")
|
2017-05-12 20:12:36 +00:00
|
|
|
self.failureResultOf(w1.get_unverified_key(), WormholeClosed)
|
|
|
|
self.failureResultOf(w1.get_verifier(), WormholeClosed)
|
|
|
|
self.failureResultOf(w1.get_versions(), WormholeClosed)
|
|
|
|
self.failureResultOf(w1.get_message(), WormholeClosed)
|
2017-03-07 11:34:36 +00:00
|
|
|
|
2017-05-15 23:10:22 +00:00
|
|
|
@inlineCallbacks
|
|
|
|
def test_closed_idle(self):
|
|
|
|
yield self._relay_server.disownServiceParent()
|
|
|
|
w1 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
# without a relay server, this won't ever connect
|
|
|
|
|
|
|
|
d_welcome = w1.get_welcome()
|
|
|
|
self.assertNoResult(d_welcome)
|
|
|
|
d_code = w1.get_code()
|
|
|
|
d_key = w1.get_unverified_key()
|
|
|
|
d_verifier = w1.get_verifier()
|
|
|
|
d_versions = w1.get_versions()
|
|
|
|
d_message = w1.get_message()
|
|
|
|
|
|
|
|
yield self.assertFailure(w1.close(), LonelyError)
|
|
|
|
|
|
|
|
self.failureResultOf(d_welcome, LonelyError)
|
|
|
|
self.failureResultOf(d_code, LonelyError)
|
|
|
|
self.failureResultOf(d_key, LonelyError)
|
|
|
|
self.failureResultOf(d_verifier, LonelyError)
|
|
|
|
self.failureResultOf(d_versions, LonelyError)
|
|
|
|
self.failureResultOf(d_message, LonelyError)
|
2017-03-07 11:34:36 +00:00
|
|
|
|
2016-05-23 01:40:44 +00:00
|
|
|
@inlineCallbacks
|
|
|
|
def test_wrong_password(self):
|
2017-03-07 11:34:36 +00:00
|
|
|
w1 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
w2 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
w1.allocate_code()
|
2017-05-12 20:12:36 +00:00
|
|
|
code = yield w1.get_code()
|
2016-05-23 01:40:44 +00:00
|
|
|
w2.set_code(code+"not")
|
2017-05-12 20:12:36 +00:00
|
|
|
code2 = yield w2.get_code()
|
2017-04-04 07:05:28 +00:00
|
|
|
self.assertNotEqual(code, code2)
|
2016-05-24 05:53:00 +00:00
|
|
|
# That's enough to allow both sides to discover the mismatch, but
|
|
|
|
# only after the confirmation message gets through. API calls that
|
|
|
|
# don't wait will appear to work until the mismatched confirmation
|
|
|
|
# message arrives.
|
2017-05-12 20:12:36 +00:00
|
|
|
w1.send_message(b"should still work")
|
|
|
|
w2.send_message(b"should still work")
|
2016-05-24 05:53:00 +00:00
|
|
|
|
2017-05-12 20:12:36 +00:00
|
|
|
key2 = yield w2.get_unverified_key() # should work
|
add w.when_key(), fix w.when_verified() to fire later
Previously, w.when_verified() was documented to fire only after a valid
encrypted message was received, but in fact it fired as soon as the shared
key was derived (before any encrypted messages are seen, so no actual
"verification" could occur yet).
This fixes that, and also adds a new w.when_key() API call which fires at the
earlier point. Having something which fires early is useful for the CLI
commands that want to print a pacifier message when the peer is responding
slowly. In particular it helps detect the case where 'wormhole send' has quit
early (after depositing the PAKE message on the server, but before the
receiver has started). In this case, the receiver will compute the shared
key, but then wait forever hoping for a VERSION that will never come. By
starting a timer when w.when_key() fires, and cancelling it when
w.when_verified() fires, we have a good place to tell the user that something
is taking longer than it should have.
This shifts responsibility for notifying Boss.got_verifier, out of Key and
into Receive, since Receive is what notices the first valid encrypted
message. It also shifts the Boss's ordering expectations: it now receives
B.happy() before B.got_verifier(), and consequently got_verifier ought to
arrive in the S2_happy state rather than S1_lonely.
2017-04-07 01:27:41 +00:00
|
|
|
# w2 has just received w1.PAKE, and is about to send w2.VERSION
|
2017-05-12 20:12:36 +00:00
|
|
|
key1 = yield w1.get_unverified_key() # should work
|
add w.when_key(), fix w.when_verified() to fire later
Previously, w.when_verified() was documented to fire only after a valid
encrypted message was received, but in fact it fired as soon as the shared
key was derived (before any encrypted messages are seen, so no actual
"verification" could occur yet).
This fixes that, and also adds a new w.when_key() API call which fires at the
earlier point. Having something which fires early is useful for the CLI
commands that want to print a pacifier message when the peer is responding
slowly. In particular it helps detect the case where 'wormhole send' has quit
early (after depositing the PAKE message on the server, but before the
receiver has started). In this case, the receiver will compute the shared
key, but then wait forever hoping for a VERSION that will never come. By
starting a timer when w.when_key() fires, and cancelling it when
w.when_verified() fires, we have a good place to tell the user that something
is taking longer than it should have.
This shifts responsibility for notifying Boss.got_verifier, out of Key and
into Receive, since Receive is what notices the first valid encrypted
message. It also shifts the Boss's ordering expectations: it now receives
B.happy() before B.got_verifier(), and consequently got_verifier ought to
arrive in the S2_happy state rather than S1_lonely.
2017-04-07 01:27:41 +00:00
|
|
|
# w1 has just received w2.PAKE, and is about to send w1.VERSION, and
|
|
|
|
# then will receive w2.VERSION. When it sees w2.VERSION, it will
|
|
|
|
# learn about the WrongPasswordError.
|
|
|
|
self.assertNotEqual(key1, key2)
|
|
|
|
|
|
|
|
# API calls that wait (i.e. get) will errback. We collect all these
|
|
|
|
# Deferreds early to exercise the wait-then-fail path
|
2017-05-12 20:12:36 +00:00
|
|
|
d1_verified = w1.get_verifier()
|
|
|
|
d1_versions = w1.get_versions()
|
|
|
|
d1_received = w1.get_message()
|
|
|
|
d2_verified = w2.get_verifier()
|
|
|
|
d2_versions = w2.get_versions()
|
|
|
|
d2_received = w2.get_message()
|
add w.when_key(), fix w.when_verified() to fire later
Previously, w.when_verified() was documented to fire only after a valid
encrypted message was received, but in fact it fired as soon as the shared
key was derived (before any encrypted messages are seen, so no actual
"verification" could occur yet).
This fixes that, and also adds a new w.when_key() API call which fires at the
earlier point. Having something which fires early is useful for the CLI
commands that want to print a pacifier message when the peer is responding
slowly. In particular it helps detect the case where 'wormhole send' has quit
early (after depositing the PAKE message on the server, but before the
receiver has started). In this case, the receiver will compute the shared
key, but then wait forever hoping for a VERSION that will never come. By
starting a timer when w.when_key() fires, and cancelling it when
w.when_verified() fires, we have a good place to tell the user that something
is taking longer than it should have.
This shifts responsibility for notifying Boss.got_verifier, out of Key and
into Receive, since Receive is what notices the first valid encrypted
message. It also shifts the Boss's ordering expectations: it now receives
B.happy() before B.got_verifier(), and consequently got_verifier ought to
arrive in the S2_happy state rather than S1_lonely.
2017-04-07 01:27:41 +00:00
|
|
|
|
|
|
|
# wait for each side to notice the failure
|
2017-05-12 20:12:36 +00:00
|
|
|
yield self.assertFailure(w1.get_verifier(), WrongPasswordError)
|
|
|
|
yield self.assertFailure(w2.get_verifier(), WrongPasswordError)
|
add w.when_key(), fix w.when_verified() to fire later
Previously, w.when_verified() was documented to fire only after a valid
encrypted message was received, but in fact it fired as soon as the shared
key was derived (before any encrypted messages are seen, so no actual
"verification" could occur yet).
This fixes that, and also adds a new w.when_key() API call which fires at the
earlier point. Having something which fires early is useful for the CLI
commands that want to print a pacifier message when the peer is responding
slowly. In particular it helps detect the case where 'wormhole send' has quit
early (after depositing the PAKE message on the server, but before the
receiver has started). In this case, the receiver will compute the shared
key, but then wait forever hoping for a VERSION that will never come. By
starting a timer when w.when_key() fires, and cancelling it when
w.when_verified() fires, we have a good place to tell the user that something
is taking longer than it should have.
This shifts responsibility for notifying Boss.got_verifier, out of Key and
into Receive, since Receive is what notices the first valid encrypted
message. It also shifts the Boss's ordering expectations: it now receives
B.happy() before B.got_verifier(), and consequently got_verifier ought to
arrive in the S2_happy state rather than S1_lonely.
2017-04-07 01:27:41 +00:00
|
|
|
# and then wait for the rest of the loops to fire. if we had+used
|
|
|
|
# eventual-send, this wouldn't be a problem
|
|
|
|
yield pause_one_tick()
|
|
|
|
|
|
|
|
# now all the rest should have fired already
|
|
|
|
self.failureResultOf(d1_verified, WrongPasswordError)
|
2017-05-12 20:12:36 +00:00
|
|
|
self.failureResultOf(d1_versions, WrongPasswordError)
|
add w.when_key(), fix w.when_verified() to fire later
Previously, w.when_verified() was documented to fire only after a valid
encrypted message was received, but in fact it fired as soon as the shared
key was derived (before any encrypted messages are seen, so no actual
"verification" could occur yet).
This fixes that, and also adds a new w.when_key() API call which fires at the
earlier point. Having something which fires early is useful for the CLI
commands that want to print a pacifier message when the peer is responding
slowly. In particular it helps detect the case where 'wormhole send' has quit
early (after depositing the PAKE message on the server, but before the
receiver has started). In this case, the receiver will compute the shared
key, but then wait forever hoping for a VERSION that will never come. By
starting a timer when w.when_key() fires, and cancelling it when
w.when_verified() fires, we have a good place to tell the user that something
is taking longer than it should have.
This shifts responsibility for notifying Boss.got_verifier, out of Key and
into Receive, since Receive is what notices the first valid encrypted
message. It also shifts the Boss's ordering expectations: it now receives
B.happy() before B.got_verifier(), and consequently got_verifier ought to
arrive in the S2_happy state rather than S1_lonely.
2017-04-07 01:27:41 +00:00
|
|
|
self.failureResultOf(d1_received, WrongPasswordError)
|
|
|
|
self.failureResultOf(d2_verified, WrongPasswordError)
|
2017-05-12 20:12:36 +00:00
|
|
|
self.failureResultOf(d2_versions, WrongPasswordError)
|
add w.when_key(), fix w.when_verified() to fire later
Previously, w.when_verified() was documented to fire only after a valid
encrypted message was received, but in fact it fired as soon as the shared
key was derived (before any encrypted messages are seen, so no actual
"verification" could occur yet).
This fixes that, and also adds a new w.when_key() API call which fires at the
earlier point. Having something which fires early is useful for the CLI
commands that want to print a pacifier message when the peer is responding
slowly. In particular it helps detect the case where 'wormhole send' has quit
early (after depositing the PAKE message on the server, but before the
receiver has started). In this case, the receiver will compute the shared
key, but then wait forever hoping for a VERSION that will never come. By
starting a timer when w.when_key() fires, and cancelling it when
w.when_verified() fires, we have a good place to tell the user that something
is taking longer than it should have.
This shifts responsibility for notifying Boss.got_verifier, out of Key and
into Receive, since Receive is what notices the first valid encrypted
message. It also shifts the Boss's ordering expectations: it now receives
B.happy() before B.got_verifier(), and consequently got_verifier ought to
arrive in the S2_happy state rather than S1_lonely.
2017-04-07 01:27:41 +00:00
|
|
|
self.failureResultOf(d2_received, WrongPasswordError)
|
|
|
|
|
|
|
|
# and at this point, with the failure safely noticed by both sides,
|
2017-05-12 20:12:36 +00:00
|
|
|
# new get_unverified_key() calls should signal the failure, even
|
|
|
|
# before we close
|
add w.when_key(), fix w.when_verified() to fire later
Previously, w.when_verified() was documented to fire only after a valid
encrypted message was received, but in fact it fired as soon as the shared
key was derived (before any encrypted messages are seen, so no actual
"verification" could occur yet).
This fixes that, and also adds a new w.when_key() API call which fires at the
earlier point. Having something which fires early is useful for the CLI
commands that want to print a pacifier message when the peer is responding
slowly. In particular it helps detect the case where 'wormhole send' has quit
early (after depositing the PAKE message on the server, but before the
receiver has started). In this case, the receiver will compute the shared
key, but then wait forever hoping for a VERSION that will never come. By
starting a timer when w.when_key() fires, and cancelling it when
w.when_verified() fires, we have a good place to tell the user that something
is taking longer than it should have.
This shifts responsibility for notifying Boss.got_verifier, out of Key and
into Receive, since Receive is what notices the first valid encrypted
message. It also shifts the Boss's ordering expectations: it now receives
B.happy() before B.got_verifier(), and consequently got_verifier ought to
arrive in the S2_happy state rather than S1_lonely.
2017-04-07 01:27:41 +00:00
|
|
|
|
|
|
|
# any new calls in the error state should immediately fail
|
2017-05-12 20:12:36 +00:00
|
|
|
self.failureResultOf(w1.get_unverified_key(), WrongPasswordError)
|
|
|
|
self.failureResultOf(w1.get_verifier(), WrongPasswordError)
|
|
|
|
self.failureResultOf(w1.get_versions(), WrongPasswordError)
|
|
|
|
self.failureResultOf(w1.get_message(), WrongPasswordError)
|
|
|
|
self.failureResultOf(w2.get_unverified_key(), WrongPasswordError)
|
|
|
|
self.failureResultOf(w2.get_verifier(), WrongPasswordError)
|
|
|
|
self.failureResultOf(w2.get_versions(), WrongPasswordError)
|
|
|
|
self.failureResultOf(w2.get_message(), WrongPasswordError)
|
2017-03-07 11:34:36 +00:00
|
|
|
|
|
|
|
yield self.assertFailure(w1.close(), WrongPasswordError)
|
|
|
|
yield self.assertFailure(w2.close(), WrongPasswordError)
|
2016-05-23 01:40:44 +00:00
|
|
|
|
add w.when_key(), fix w.when_verified() to fire later
Previously, w.when_verified() was documented to fire only after a valid
encrypted message was received, but in fact it fired as soon as the shared
key was derived (before any encrypted messages are seen, so no actual
"verification" could occur yet).
This fixes that, and also adds a new w.when_key() API call which fires at the
earlier point. Having something which fires early is useful for the CLI
commands that want to print a pacifier message when the peer is responding
slowly. In particular it helps detect the case where 'wormhole send' has quit
early (after depositing the PAKE message on the server, but before the
receiver has started). In this case, the receiver will compute the shared
key, but then wait forever hoping for a VERSION that will never come. By
starting a timer when w.when_key() fires, and cancelling it when
w.when_verified() fires, we have a good place to tell the user that something
is taking longer than it should have.
This shifts responsibility for notifying Boss.got_verifier, out of Key and
into Receive, since Receive is what notices the first valid encrypted
message. It also shifts the Boss's ordering expectations: it now receives
B.happy() before B.got_verifier(), and consequently got_verifier ought to
arrive in the S2_happy state rather than S1_lonely.
2017-04-07 01:27:41 +00:00
|
|
|
# API calls should still get the error, not WormholeClosed
|
2017-05-12 20:12:36 +00:00
|
|
|
self.failureResultOf(w1.get_unverified_key(), WrongPasswordError)
|
|
|
|
self.failureResultOf(w1.get_verifier(), WrongPasswordError)
|
|
|
|
self.failureResultOf(w1.get_versions(), WrongPasswordError)
|
|
|
|
self.failureResultOf(w1.get_message(), WrongPasswordError)
|
|
|
|
self.failureResultOf(w2.get_unverified_key(), WrongPasswordError)
|
|
|
|
self.failureResultOf(w2.get_verifier(), WrongPasswordError)
|
|
|
|
self.failureResultOf(w2.get_versions(), WrongPasswordError)
|
|
|
|
self.failureResultOf(w2.get_message(), WrongPasswordError)
|
2016-05-23 01:40:44 +00:00
|
|
|
|
2016-06-02 21:07:27 +00:00
|
|
|
@inlineCallbacks
|
|
|
|
def test_wrong_password_with_spaces(self):
|
2017-03-07 11:34:36 +00:00
|
|
|
w = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
badcode = "4 oops spaces"
|
2016-06-02 21:21:29 +00:00
|
|
|
with self.assertRaises(KeyFormatError) as ex:
|
2017-03-07 11:34:36 +00:00
|
|
|
w.set_code(badcode)
|
|
|
|
expected_msg = "code (%s) contains spaces." % (badcode,)
|
2016-06-02 21:21:29 +00:00
|
|
|
self.assertEqual(expected_msg, str(ex.exception))
|
2017-03-07 11:34:36 +00:00
|
|
|
yield self.assertFailure(w.close(), LonelyError)
|
2016-06-02 21:07:27 +00:00
|
|
|
|
2017-05-15 23:10:22 +00:00
|
|
|
@inlineCallbacks
|
|
|
|
def test_welcome(self):
|
|
|
|
w1 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
wel1 = yield w1.get_welcome() # early: before connection established
|
|
|
|
wel2 = yield w1.get_welcome() # late: already received welcome
|
|
|
|
self.assertEqual(wel1, wel2)
|
|
|
|
self.assertIn("current_cli_version", wel1)
|
|
|
|
|
|
|
|
# cause an error, so a later get_welcome will return the error
|
|
|
|
w1.set_code("123-foo")
|
|
|
|
w2 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
w2.set_code("123-NOT")
|
|
|
|
yield self.assertFailure(w1.get_verifier(), WrongPasswordError)
|
|
|
|
|
|
|
|
yield self.assertFailure(w1.get_welcome(), WrongPasswordError) # late
|
|
|
|
|
|
|
|
yield self.assertFailure(w1.close(), WrongPasswordError)
|
|
|
|
yield self.assertFailure(w2.close(), WrongPasswordError)
|
|
|
|
|
2016-05-23 01:40:44 +00:00
|
|
|
@inlineCallbacks
|
|
|
|
def test_verifier(self):
|
2017-03-07 11:34:36 +00:00
|
|
|
w1 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
w2 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
w1.allocate_code()
|
2017-05-12 20:12:36 +00:00
|
|
|
code = yield w1.get_code()
|
2016-05-23 01:40:44 +00:00
|
|
|
w2.set_code(code)
|
2017-05-12 20:12:36 +00:00
|
|
|
v1 = yield w1.get_verifier() # early
|
|
|
|
v2 = yield w2.get_verifier()
|
2016-05-23 01:40:44 +00:00
|
|
|
self.failUnlessEqual(type(v1), type(b""))
|
|
|
|
self.failUnlessEqual(v1, v2)
|
2017-05-12 20:12:36 +00:00
|
|
|
w1.send_message(b"data1")
|
|
|
|
w2.send_message(b"data2")
|
|
|
|
dataX = yield w1.get_message()
|
|
|
|
dataY = yield w2.get_message()
|
2016-05-23 01:40:44 +00:00
|
|
|
self.assertEqual(dataX, b"data2")
|
|
|
|
self.assertEqual(dataY, b"data1")
|
2017-04-06 19:26:52 +00:00
|
|
|
|
2017-05-12 20:12:36 +00:00
|
|
|
# calling get_verifier() this late should fire right away
|
|
|
|
v1_late = self.successResultOf(w2.get_verifier())
|
2017-04-06 19:26:52 +00:00
|
|
|
self.assertEqual(v1_late, v1)
|
|
|
|
|
2016-05-24 06:59:49 +00:00
|
|
|
yield w1.close()
|
|
|
|
yield w2.close()
|
2016-05-23 01:40:44 +00:00
|
|
|
|
2016-05-26 01:27:37 +00:00
|
|
|
@inlineCallbacks
|
|
|
|
def test_versions(self):
|
|
|
|
# there's no API for this yet, but make sure the internals work
|
2017-03-07 11:34:36 +00:00
|
|
|
w1 = wormhole.create(APPID, self.relayurl, reactor,
|
|
|
|
versions={"w1": 123})
|
|
|
|
w2 = wormhole.create(APPID, self.relayurl, reactor,
|
|
|
|
versions={"w2": 456})
|
|
|
|
w1.allocate_code()
|
2017-05-12 20:12:36 +00:00
|
|
|
code = yield w1.get_code()
|
2016-05-26 01:27:37 +00:00
|
|
|
w2.set_code(code)
|
2017-05-12 20:12:36 +00:00
|
|
|
w1_versions = yield w2.get_versions()
|
2017-03-07 11:34:36 +00:00
|
|
|
self.assertEqual(w1_versions, {"w1": 123})
|
2017-05-12 20:12:36 +00:00
|
|
|
w2_versions = yield w1.get_versions()
|
2017-03-07 11:34:36 +00:00
|
|
|
self.assertEqual(w2_versions, {"w2": 456})
|
2016-05-26 01:27:37 +00:00
|
|
|
yield w1.close()
|
|
|
|
yield w2.close()
|
|
|
|
|
2016-12-26 20:21:45 +00:00
|
|
|
@inlineCallbacks
|
|
|
|
def test_rx_dedup(self):
|
|
|
|
# Future clients will handle losing/reestablishing the Rendezvous
|
|
|
|
# Server connection by retransmitting messages, which will sometimes
|
|
|
|
# cause duplicate messages. Make sure this client can tolerate them.
|
|
|
|
# The first place this would fail was when the second copy of the
|
|
|
|
# incoming PAKE message was received, which would cause
|
|
|
|
# SPAKE2.finish() to be called a second time, which throws an error
|
|
|
|
# (which, being somewhat unexpected, caused a hang rather than a
|
2017-03-07 11:34:36 +00:00
|
|
|
# clear exception). The Mailbox object is responsible for
|
|
|
|
# deduplication, so we must patch the RendezvousConnector to simulate
|
|
|
|
# duplicated messages.
|
|
|
|
with mock.patch("wormhole._boss.RendezvousConnector", MessageDoubler):
|
2017-03-04 11:44:07 +00:00
|
|
|
w1 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
w2 = wormhole.create(APPID, self.relayurl, reactor)
|
2016-12-26 20:21:45 +00:00
|
|
|
w1.set_code("123-purple-elephant")
|
|
|
|
w2.set_code("123-purple-elephant")
|
2017-05-12 20:12:36 +00:00
|
|
|
w1.send_message(b"data1"), w2.send_message(b"data2")
|
|
|
|
dl = yield self.doBoth(w1.get_message(), w2.get_message())
|
2016-12-26 20:21:45 +00:00
|
|
|
(dataX, dataY) = dl
|
|
|
|
self.assertEqual(dataX, b"data2")
|
|
|
|
self.assertEqual(dataY, b"data1")
|
|
|
|
yield w1.close()
|
|
|
|
yield w2.close()
|
|
|
|
|
2017-03-07 11:34:36 +00:00
|
|
|
class MessageDoubler(_rendezvous.RendezvousConnector):
|
2016-12-26 20:21:45 +00:00
|
|
|
# we could double messages on the sending side, but a future server will
|
|
|
|
# strip those duplicates, so to really exercise the receiver, we must
|
|
|
|
# double them on the inbound side instead
|
|
|
|
#def _msg_send(self, phase, body):
|
|
|
|
# wormhole._Wormhole._msg_send(self, phase, body)
|
|
|
|
# self._ws_send_command("add", phase=phase, body=bytes_to_hexstr(body))
|
2017-03-07 11:34:36 +00:00
|
|
|
def _response_handle_message(self, msg):
|
|
|
|
_rendezvous.RendezvousConnector._response_handle_message(self, msg)
|
|
|
|
_rendezvous.RendezvousConnector._response_handle_message(self, msg)
|
2016-12-26 20:21:45 +00:00
|
|
|
|
2016-05-24 05:53:00 +00:00
|
|
|
class Errors(ServerBase, unittest.TestCase):
|
2016-05-23 01:40:44 +00:00
|
|
|
@inlineCallbacks
|
2017-03-08 07:45:11 +00:00
|
|
|
def test_derive_key_early(self):
|
2017-03-07 11:34:36 +00:00
|
|
|
w = wormhole.create(APPID, self.relayurl, reactor)
|
2016-05-24 05:53:00 +00:00
|
|
|
# definitely too early
|
2017-04-19 02:50:26 +00:00
|
|
|
with self.assertRaises(NoKeyError):
|
|
|
|
w.derive_key("purpose", 12)
|
2017-03-08 07:45:11 +00:00
|
|
|
yield self.assertFailure(w.close(), LonelyError)
|
2016-05-24 05:53:00 +00:00
|
|
|
|
2017-03-08 07:45:11 +00:00
|
|
|
@inlineCallbacks
|
|
|
|
def test_multiple_set_code(self):
|
|
|
|
w = wormhole.create(APPID, self.relayurl, reactor)
|
2016-06-04 20:16:09 +00:00
|
|
|
w.set_code("123-purple-elephant")
|
2016-05-24 05:53:00 +00:00
|
|
|
# code can only be set once
|
2017-04-19 02:50:26 +00:00
|
|
|
with self.assertRaises(OnlyOneCodeError):
|
|
|
|
w.set_code("123-nope")
|
2017-03-08 07:45:11 +00:00
|
|
|
yield self.assertFailure(w.close(), LonelyError)
|
2016-05-24 05:53:00 +00:00
|
|
|
|
|
|
|
@inlineCallbacks
|
2017-03-08 07:45:11 +00:00
|
|
|
def test_allocate_and_set_code(self):
|
2017-03-07 11:34:36 +00:00
|
|
|
w = wormhole.create(APPID, self.relayurl, reactor)
|
2017-03-08 07:45:11 +00:00
|
|
|
w.allocate_code()
|
2017-05-12 20:12:36 +00:00
|
|
|
yield w.get_code()
|
2017-04-19 02:50:26 +00:00
|
|
|
with self.assertRaises(OnlyOneCodeError):
|
|
|
|
w.set_code("123-nope")
|
2017-03-08 07:45:11 +00:00
|
|
|
yield self.assertFailure(w.close(), LonelyError)
|
2017-04-07 16:48:48 +00:00
|
|
|
|
|
|
|
class Reconnection(ServerBase, unittest.TestCase):
|
|
|
|
@inlineCallbacks
|
|
|
|
def test_basic(self):
|
|
|
|
w1 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
w1_in = []
|
|
|
|
w1._boss._RC._debug_record_inbound_f = w1_in.append
|
|
|
|
#w1.debug_set_trace("W1")
|
|
|
|
w1.allocate_code()
|
2017-05-12 20:12:36 +00:00
|
|
|
code = yield w1.get_code()
|
|
|
|
w1.send_message(b"data1") # queued until wormhole is established
|
2017-04-07 16:48:48 +00:00
|
|
|
|
|
|
|
# now wait until we've deposited all our messages on the server
|
|
|
|
def seen_our_pake():
|
|
|
|
for m in w1_in:
|
|
|
|
if m["type"] == "message" and m["phase"] == "pake":
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
yield poll_until(seen_our_pake)
|
|
|
|
|
|
|
|
w1_in[:] = []
|
|
|
|
# drop the connection
|
|
|
|
w1._boss._RC._ws.transport.loseConnection()
|
|
|
|
# wait for it to reconnect and redeliver all the messages. The server
|
|
|
|
# sends mtype=message messages in random order, but we've only sent
|
|
|
|
# one of them, so it's safe to wait for just the PAKE phase.
|
|
|
|
yield poll_until(seen_our_pake)
|
|
|
|
|
|
|
|
# now let the second side proceed. this simulates the most common
|
|
|
|
# case: the server is bounced while the sender is waiting, before the
|
|
|
|
# receiver has started
|
|
|
|
|
|
|
|
w2 = wormhole.create(APPID, self.relayurl, reactor)
|
|
|
|
#w2.debug_set_trace(" W2")
|
|
|
|
w2.set_code(code)
|
|
|
|
|
2017-05-12 20:12:36 +00:00
|
|
|
dataY = yield w2.get_message()
|
2017-04-07 16:48:48 +00:00
|
|
|
self.assertEqual(dataY, b"data1")
|
|
|
|
|
2017-05-12 20:12:36 +00:00
|
|
|
w2.send_message(b"data2")
|
|
|
|
dataX = yield w1.get_message()
|
2017-04-07 16:48:48 +00:00
|
|
|
self.assertEqual(dataX, b"data2")
|
|
|
|
|
|
|
|
c1 = yield w1.close()
|
|
|
|
self.assertEqual(c1, "happy")
|
|
|
|
c2 = yield w2.close()
|
|
|
|
self.assertEqual(c2, "happy")
|
2017-04-19 02:49:17 +00:00
|
|
|
|
2017-04-16 16:25:37 +00:00
|
|
|
class InitialFailure(unittest.TestCase):
|
|
|
|
def assertSCEResultOf(self, d, innerType):
|
|
|
|
f = self.failureResultOf(d, ServerConnectionError)
|
|
|
|
inner = f.value.reason
|
|
|
|
self.assertIsInstance(inner, innerType)
|
|
|
|
return inner
|
|
|
|
|
|
|
|
@inlineCallbacks
|
|
|
|
def test_bad_dns(self):
|
|
|
|
# point at a URL that will never connect
|
|
|
|
w = wormhole.create(APPID, "ws://%%%.example.org:4000/v1", reactor)
|
|
|
|
# that should have already received an error, when it tried to
|
|
|
|
# resolve the bogus DNS name. All API calls will return an error.
|
|
|
|
e = yield self.assertFailure(w.get_unverified_key(),
|
|
|
|
ServerConnectionError)
|
|
|
|
self.assertIsInstance(e.reason, ValueError)
|
|
|
|
self.assertEqual(str(e), "invalid hostname: %%%.example.org")
|
|
|
|
self.assertSCEResultOf(w.get_code(), ValueError)
|
|
|
|
self.assertSCEResultOf(w.get_verifier(), ValueError)
|
|
|
|
self.assertSCEResultOf(w.get_versions(), ValueError)
|
|
|
|
self.assertSCEResultOf(w.get_message(), ValueError)
|
|
|
|
|
|
|
|
@inlineCallbacks
|
|
|
|
def assertSCE(self, d, innerType):
|
|
|
|
e = yield self.assertFailure(d, ServerConnectionError)
|
|
|
|
inner = e.reason
|
|
|
|
self.assertIsInstance(inner, innerType)
|
|
|
|
returnValue(inner)
|
|
|
|
|
|
|
|
@inlineCallbacks
|
|
|
|
def test_no_connection(self):
|
|
|
|
# point at a URL that will never connect
|
|
|
|
port = allocate_tcp_port()
|
|
|
|
w = wormhole.create(APPID, "ws://127.0.0.1:%d/v1" % port, reactor)
|
|
|
|
# nothing is listening, but it will take a turn to discover that
|
|
|
|
d1 = w.get_code()
|
|
|
|
d2 = w.get_unverified_key()
|
|
|
|
d3 = w.get_verifier()
|
|
|
|
d4 = w.get_versions()
|
|
|
|
d5 = w.get_message()
|
|
|
|
yield self.assertSCE(d1, ConnectionRefusedError)
|
|
|
|
yield self.assertSCE(d2, ConnectionRefusedError)
|
|
|
|
yield self.assertSCE(d3, ConnectionRefusedError)
|
|
|
|
yield self.assertSCE(d4, ConnectionRefusedError)
|
|
|
|
yield self.assertSCE(d5, ConnectionRefusedError)
|
|
|
|
|
|
|
|
@inlineCallbacks
|
|
|
|
def test_all_deferreds(self):
|
|
|
|
# point at a URL that will never connect
|
|
|
|
port = allocate_tcp_port()
|
|
|
|
w = wormhole.create(APPID, "ws://127.0.0.1:%d/v1" % port, reactor)
|
|
|
|
# nothing is listening, but it will take a turn to discover that
|
|
|
|
w.allocate_code()
|
|
|
|
d1 = w.get_code()
|
|
|
|
d2 = w.get_unverified_key()
|
|
|
|
d3 = w.get_verifier()
|
|
|
|
d4 = w.get_versions()
|
|
|
|
d5 = w.get_message()
|
|
|
|
yield self.assertSCE(d1, ConnectionRefusedError)
|
|
|
|
yield self.assertSCE(d2, ConnectionRefusedError)
|
|
|
|
yield self.assertSCE(d3, ConnectionRefusedError)
|
|
|
|
yield self.assertSCE(d4, ConnectionRefusedError)
|
|
|
|
yield self.assertSCE(d5, ConnectionRefusedError)
|
|
|
|
|
2017-04-19 02:49:17 +00:00
|
|
|
class Trace(unittest.TestCase):
|
|
|
|
def test_basic(self):
|
|
|
|
w1 = wormhole.create(APPID, "ws://localhost:1", reactor)
|
|
|
|
stderr = io.StringIO()
|
|
|
|
w1.debug_set_trace("W1", file=stderr)
|
|
|
|
# if Automat doesn't have the tracing API, then we won't actually
|
|
|
|
# exercise the tracing function, so exercise the RendezvousConnector
|
|
|
|
# function manually (it isn't a state machine, so it will always wire
|
|
|
|
# up the tracer)
|
|
|
|
w1._boss._RC._debug("what")
|
|
|
|
|
|
|
|
stderr = io.StringIO()
|
|
|
|
out = w1._boss._print_trace("OLD", "IN", "NEW", "C1", "M1", stderr)
|
|
|
|
self.assertEqual(stderr.getvalue().splitlines(),
|
|
|
|
["C1.M1[OLD].IN -> [NEW]"])
|
|
|
|
out("OUT1")
|
|
|
|
self.assertEqual(stderr.getvalue().splitlines(),
|
|
|
|
["C1.M1[OLD].IN -> [NEW]",
|
|
|
|
" C1.M1.OUT1()"])
|
|
|
|
w1._boss._print_trace("", "R.connected", "", "C1", "RC1", stderr)
|
|
|
|
self.assertEqual(stderr.getvalue().splitlines(),
|
|
|
|
["C1.M1[OLD].IN -> [NEW]",
|
|
|
|
" C1.M1.OUT1()",
|
|
|
|
"C1.RC1.R.connected"])
|
|
|
|
|
|
|
|
def test_delegated(self):
|
|
|
|
dg = Delegate()
|
|
|
|
w1 = wormhole.create(APPID, "ws://localhost:1", reactor, delegate=dg)
|
|
|
|
stderr = io.StringIO()
|
|
|
|
w1.debug_set_trace("W1", file=stderr)
|
|
|
|
w1._boss._RC._debug("what")
|