From b7df5e21eb0764cf1adfe277c2007a235bb8c1a5 Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Thu, 23 Feb 2017 18:23:55 -0800 Subject: [PATCH] more tests, still failing --- docs/code.dot | 6 ++-- docs/machines.dot | 4 +-- src/wormhole/_boss.py | 11 ++++---- src/wormhole/_code.py | 13 +++++---- src/wormhole/test/test_wormhole_new.py | 33 ++++++++++++++++++---- src/wormhole/wormhole.py | 38 +++++++++++++++++++++----- 6 files changed, 75 insertions(+), 30 deletions(-) diff --git a/docs/code.dot b/docs/code.dot index b6fb71c..2b76493 100644 --- a/docs/code.dot +++ b/docs/code.dot @@ -4,12 +4,12 @@ digraph { {rank=same; start S0} start -> S0 [style="invis"] S0 [label="S0:\nunknown\ndisconnected"] - S0 -> P0_got_code [label="set"] + S0 -> P0_got_code [label="set_code"] P0_got_code [shape="box" label="B.got_code"] P0_got_code -> S4 S4 [label="S4: known" color="green"] - S0 -> P0_list_nameplates [label="input"] + S0 -> P0_list_nameplates [label="input_code"] S2 [label="S2: typing\nnameplate"] S2 -> P2_completion [label=""] @@ -32,7 +32,7 @@ digraph { S3 -> P0_got_code [label="" color="orange" fontcolor="orange"] - S0 -> S1A [label="allocate"] + S0 -> S1A [label="allocate_code"] S1A [label="S1A:\nconnecting"] S1A -> P1_allocate [label="connected"] P1_allocate [shape="box" label="RC.tx_allocate"] diff --git a/docs/machines.dot b/docs/machines.dot index c6004ee..4c75515 100644 --- a/docs/machines.dot +++ b/docs/machines.dot @@ -18,7 +18,7 @@ digraph { Connection -> websocket [color="blue"] #Connection -> Order [color="blue"] - Wormhole -> Boss [style="dashed" label="allocate\ninput\nset_code\nsend\nclose\n(once)"] + Wormhole -> Boss [style="dashed" label="allocate_code\ninput_code\nset_code\nsend\nclose\n(once)"] #Wormhole -> Boss [color="blue"] Boss -> Wormhole [style="dashed" label="got_code\ngot_verifier\nreceived (seq)\nclosed\n(once)"] @@ -74,7 +74,7 @@ digraph { label="refresh_nameplates" ] Boss -> Code [style="dashed" - label="allocate\ninput\nset_code"] + label="allocate_code\ninput_code\nset_code_code"] Code -> Boss [style="dashed" label="got_code"] diff --git a/src/wormhole/_boss.py b/src/wormhole/_boss.py index 7ba8978..e6b9238 100644 --- a/src/wormhole/_boss.py +++ b/src/wormhole/_boss.py @@ -81,10 +81,10 @@ class Boss(object): # would require the Wormhole to be aware of Code (whereas right now # Wormhole only knows about this Boss instance, and everything else is # hidden away). - def input(self, stdio): - self._C.input(stdio) - def allocate(self, code_length): - self._C.allocate(code_length) + def input_code(self, stdio): + self._C.input_code(stdio) + def allocate_code(self, code_length): + self._C.allocate_code(code_length) def set_code(self, code): self._C.set_code(code) @@ -165,8 +165,7 @@ class Boss(object): # we call Wormhole.received() in strict phase order, with no gaps self._rx_phases[phase] = plaintext while self._next_rx_phase in self._rx_phases: - self._W.received(self._next_rx_phase, - self._rx_phases.pop(self._next_rx_phase)) + self._W.received(self._rx_phases.pop(self._next_rx_phase)) self._next_rx_phase += 1 @m.output() diff --git a/src/wormhole/_code.py b/src/wormhole/_code.py index 7f4a248..544685b 100644 --- a/src/wormhole/_code.py +++ b/src/wormhole/_code.py @@ -46,11 +46,11 @@ class Code(object): # from App @m.input() - def allocate(self, code_length): pass + def allocate_code(self, code_length): pass @m.input() - def input(self, stdio): pass + def input_code(self, stdio): pass @m.input() - def set(self, code): pass + def set_code(self, code): pass # from RendezvousConnector @m.input() @@ -111,16 +111,17 @@ class Code(object): def _B_got_code(self): self._B.got_code(self._code) - S0_unknown.upon(set, enter=S4_known, outputs=[B_got_code]) + S0_unknown.upon(set_code, enter=S4_known, outputs=[B_got_code]) - S0_unknown.upon(allocate, enter=S1A_connecting, outputs=[stash_code_length]) + S0_unknown.upon(allocate_code, enter=S1A_connecting, + outputs=[stash_code_length]) S1A_connecting.upon(connected, enter=S1B_allocating, outputs=[RC_tx_allocate]) S1B_allocating.upon(lost, enter=S1A_connecting, outputs=[]) S1B_allocating.upon(rx_allocated, enter=S4_known, outputs=[generate_and_B_got_code]) - S0_unknown.upon(input, enter=S2_typing_nameplate, + S0_unknown.upon(input_code, enter=S2_typing_nameplate, outputs=[start_input_and_NL_refresh_nameplates]) S2_typing_nameplate.upon(tab, enter=S2_typing_nameplate, outputs=[do_completion_nameplates]) diff --git a/src/wormhole/test/test_wormhole_new.py b/src/wormhole/test/test_wormhole_new.py index 9710ae7..6e32a24 100644 --- a/src/wormhole/test/test_wormhole_new.py +++ b/src/wormhole/test/test_wormhole_new.py @@ -23,15 +23,36 @@ class Delegate: self.closed = result class New(ServerBase, unittest.TestCase): - def test_basic(self): + @inlineCallbacks + def test_allocate(self): + w = wormhole.deferred_wormhole(APPID, self.relayurl, reactor) + w.allocate_code(2) + code = yield w.when_code() + print("code:", code) + yield w.close() + test_allocate.timeout = 2 + + def test_delegated(self): dg = Delegate() w = wormhole.delegated_wormhole(APPID, self.relayurl, reactor, dg) w.close() @inlineCallbacks - def test_allocate(self): - w = wormhole.deferred_wormhole(APPID, self.relayurl, reactor) - code = yield w.when_code() + def test_basic(self): + w1 = wormhole.deferred_wormhole(APPID, self.relayurl, reactor) + w1.allocate_code(2) + code = yield w1.when_code() print("code:", code) - yield w.close() - test_allocate.timeout = 2 + w2 = wormhole.deferred_wormhole(APPID, self.relayurl, reactor) + w2.set_code(code) + code2 = yield w2.when_code() + self.assertEqual(code, code2) + + w1.send(b"data") + + data = yield w2.when_received() + self.assertEqual(data, b"data") + + yield w1.close() + yield w2.close() + test_basic.timeout = 2 diff --git a/src/wormhole/wormhole.py b/src/wormhole/wormhole.py index c37bad8..d965d72 100644 --- a/src/wormhole/wormhole.py +++ b/src/wormhole/wormhole.py @@ -38,6 +38,14 @@ class _DelegatedWormhole(object): self._boss = boss # from above + + def allocate_code(self, code_length=2): + self._boss.allocate_code(code_length) + def input_code(self, stdio): + self._boss.input_code(stdio) + def set_code(self, code): + self._boss.set_code(code) + def send(self, plaintext): self._boss.send(plaintext) def close(self): @@ -48,9 +56,8 @@ class _DelegatedWormhole(object): self._delegate.wormhole_got_code(code) def got_verifier(self, verifier): self._delegate.wormhole_got_verifier(verifier) - def received(self, phase, plaintext): - # TODO: deliver phases in order - self._delegate.wormhole_received(phase, plaintext) + def received(self, plaintext): + self._delegate.wormhole_received(plaintext) def closed(self, result): self._delegate.wormhole_closed(result) @@ -61,6 +68,8 @@ class _DeferredWormhole(object): self._code_observers = [] self._verifier = None self._verifier_observers = [] + self._received_data = [] + self._received_observers = [] def _set_boss(self, boss): self._boss = boss @@ -80,6 +89,20 @@ class _DeferredWormhole(object): self._verifier_observers.append(d) return d + def when_received(self): + if self._received_data: + return defer.succeed(self._received_data.pop(0)) + d = defer.Deferred() + self._received_observers.append(d) + return d + + def allocate_code(self, code_length=2): + self._boss.allocate_code(code_length) + def input_code(self, stdio): + self._boss.input_code(stdio) + def set_code(self, code): + self._boss.set_code(code) + def send(self, plaintext): self._boss.send(plaintext) def close(self): @@ -97,8 +120,11 @@ class _DeferredWormhole(object): d.callback(verifier) self._verifier_observers[:] = [] - def received(self, phase, plaintext): - print(phase, plaintext) + def received(self, plaintext): + if self._received_observers: + self._received_observers.pop(0).callback(plaintext) + return + self._received_data.append(plaintext) def closed(self, result): print("closed", result) @@ -109,7 +135,6 @@ def _wormhole(appid, relay_url, reactor, delegate=None, stderr=sys.stderr, ): timing = timing or DebugTiming() - code_length = 2 side = bytes_to_hexstr(os.urandom(5)) journal = journal or ImmediateJournal() if delegate: @@ -120,7 +145,6 @@ def _wormhole(appid, relay_url, reactor, delegate=None, w._set_boss(b) # force allocate for now b.start() - b.allocate(code_length) return w def delegated_wormhole(appid, relay_url, reactor, delegate,