add test for Transit (blocking only)

This commit is contained in:
Brian Warner 2015-12-03 16:24:52 -06:00
parent 229c702ccc
commit 568903ac48
2 changed files with 71 additions and 7 deletions

View File

@ -16,7 +16,7 @@ class ServerBase:
s.setServiceParent(self.sp) s.setServiceParent(self.sp)
self._relay_server = s.relay self._relay_server = s.relay
self.relayurl = u"http://127.0.0.1:%d/wormhole-relay/" % relayport self.relayurl = u"http://127.0.0.1:%d/wormhole-relay/" % relayport
self.transit = "tcp:127.0.0.1:%d" % transitport self.transit = u"tcp:127.0.0.1:%d" % transitport
d.addCallback(_got_ports) d.addCallback(_got_ports)
return d return d

View File

@ -1,11 +1,12 @@
from __future__ import print_function from __future__ import print_function
import json import json
from twisted.trial import unittest from twisted.trial import unittest
from twisted.internet.defer import gatherResults, succeed from twisted.internet.defer import gatherResults, succeed, inlineCallbacks
from twisted.internet.threads import deferToThread from twisted.internet.threads import deferToThread
from ..blocking.transcribe import (Wormhole, UsageError, ChannelManager, from ..blocking.transcribe import (Wormhole, UsageError, ChannelManager,
WrongPasswordError) WrongPasswordError)
from ..blocking.eventsource import EventSourceFollower from ..blocking.eventsource import EventSourceFollower
from ..blocking.transit import TransitSender, TransitReceiver
from .common import ServerBase from .common import ServerBase
APPID = u"appid" APPID = u"appid"
@ -125,11 +126,7 @@ class Channel(ServerBase, unittest.TestCase):
d.addCallback(lambda msg: self.failUnlessEqual(msg, b"msg1b")) d.addCallback(lambda msg: self.failUnlessEqual(msg, b"msg1b"))
return d return d
class _DoBothMixin:
class Blocking(ServerBase, unittest.TestCase):
# we need Twisted to run the server, but we run the sender and receiver
# with deferToThread()
def doBoth(self, call1, call2): def doBoth(self, call1, call2):
f1 = call1[0] f1 = call1[0]
f1args = call1[1:] f1args = call1[1:]
@ -138,6 +135,10 @@ class Blocking(ServerBase, unittest.TestCase):
return gatherResults([deferToThread(f1, *f1args), return gatherResults([deferToThread(f1, *f1args),
deferToThread(f2, *f2args)], True) deferToThread(f2, *f2args)], True)
class Blocking(_DoBothMixin, ServerBase, unittest.TestCase):
# we need Twisted to run the server, but we run the sender and receiver
# with deferToThread()
def test_basic(self): def test_basic(self):
w1 = Wormhole(APPID, self.relayurl) w1 = Wormhole(APPID, self.relayurl)
w2 = Wormhole(APPID, self.relayurl) w2 = Wormhole(APPID, self.relayurl)
@ -444,3 +445,66 @@ class EventSourceClient(unittest.TestCase):
(u"message", u"three"), (u"message", u"three"),
(u"e2", u"four"), (u"e2", u"four"),
]) ])
class Transit(_DoBothMixin, ServerBase, unittest.TestCase):
@inlineCallbacks
def test_direct_to_receiver(self):
s = TransitSender(self.transit)
r = TransitReceiver(self.transit)
key = b"\x00"*32
# force the connection to be sender->receiver
s.set_transit_key(key)
s.add_their_direct_hints(r.get_direct_hints())
s.add_their_relay_hints([])
r.set_transit_key(key)
r.add_their_direct_hints([])
r.add_their_relay_hints([])
(sp, rp) = yield self.doBoth([s.connect], [r.connect])
yield deferToThread(sp.send_record, b"01234")
rec = yield deferToThread(rp.receive_record)
self.assertEqual(rec, b"01234")
yield deferToThread(sp.close)
yield deferToThread(rp.close)
@inlineCallbacks
def test_direct_to_sender(self):
s = TransitSender(self.transit)
r = TransitReceiver(self.transit)
key = b"\x00"*32
# force the connection to be receiver->sender
s.set_transit_key(key)
s.add_their_direct_hints([])
s.add_their_relay_hints([])
r.set_transit_key(key)
r.add_their_direct_hints(s.get_direct_hints())
r.add_their_relay_hints([])
(sp, rp) = yield self.doBoth([s.connect], [r.connect])
yield deferToThread(sp.send_record, b"01234")
rec = yield deferToThread(rp.receive_record)
self.assertEqual(rec, b"01234")
yield deferToThread(sp.close)
yield deferToThread(rp.close)
@inlineCallbacks
def test_relay(self):
s = TransitSender(self.transit)
r = TransitReceiver(self.transit)
key = b"\x00"*32
# force the connection to use the relay by not revealing direct hints
s.set_transit_key(key)
s.add_their_direct_hints([])
s.add_their_relay_hints(r.get_relay_hints())
r.set_transit_key(key)
r.add_their_direct_hints([])
r.add_their_relay_hints(s.get_relay_hints())
(sp, rp) = yield self.doBoth([s.connect], [r.connect])
yield deferToThread(sp.send_record, b"01234")
rec = yield deferToThread(rp.receive_record)
self.assertEqual(rec, b"01234")
yield deferToThread(sp.close)
yield deferToThread(rp.close)