2015-09-27 17:43:25 +00:00
|
|
|
import json
|
|
|
|
from twisted.trial import unittest
|
2015-09-28 23:44:00 +00:00
|
|
|
from twisted.internet.defer import gatherResults
|
2015-09-27 17:43:25 +00:00
|
|
|
from twisted.internet.threads import deferToThread
|
|
|
|
from ..blocking.transcribe import Wormhole as BlockingWormhole, UsageError
|
|
|
|
from .common import ServerBase
|
|
|
|
|
2015-10-07 00:02:52 +00:00
|
|
|
APPID = u"appid"
|
|
|
|
|
2015-09-27 17:43:25 +00:00
|
|
|
class Blocking(ServerBase, unittest.TestCase):
|
|
|
|
# we need Twisted to run the server, but we run the sender and receiver
|
|
|
|
# with deferToThread()
|
|
|
|
|
2015-10-04 05:03:27 +00:00
|
|
|
def doBoth(self, call1, call2):
|
|
|
|
f1 = call1[0]
|
|
|
|
f1args = call1[1:]
|
|
|
|
f2 = call2[0]
|
|
|
|
f2args = call2[1:]
|
|
|
|
return gatherResults([deferToThread(f1, *f1args),
|
|
|
|
deferToThread(f2, *f2args)], True)
|
|
|
|
|
2015-09-27 17:43:25 +00:00
|
|
|
def test_basic(self):
|
2015-10-07 00:02:52 +00:00
|
|
|
w1 = BlockingWormhole(APPID, self.relayurl)
|
|
|
|
w2 = BlockingWormhole(APPID, self.relayurl)
|
2015-09-27 17:43:25 +00:00
|
|
|
d = deferToThread(w1.get_code)
|
|
|
|
def _got_code(code):
|
|
|
|
w2.set_code(code)
|
2015-10-04 05:03:27 +00:00
|
|
|
return self.doBoth([w1.send_data, b"data1"],
|
|
|
|
[w2.send_data, b"data2"])
|
2015-09-27 17:43:25 +00:00
|
|
|
d.addCallback(_got_code)
|
2015-10-04 05:03:27 +00:00
|
|
|
def _sent(res):
|
|
|
|
return self.doBoth([w1.get_data], [w2.get_data])
|
|
|
|
d.addCallback(_sent)
|
2015-09-27 17:43:25 +00:00
|
|
|
def _done(dl):
|
2015-09-28 23:44:00 +00:00
|
|
|
(dataX, dataY) = dl
|
2015-09-28 06:09:51 +00:00
|
|
|
self.assertEqual(dataX, b"data2")
|
|
|
|
self.assertEqual(dataY, b"data1")
|
2015-10-04 05:03:27 +00:00
|
|
|
return self.doBoth([w1.close], [w2.close])
|
|
|
|
d.addCallback(_done)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def test_interleaved(self):
|
2015-10-07 00:02:52 +00:00
|
|
|
w1 = BlockingWormhole(APPID, self.relayurl)
|
|
|
|
w2 = BlockingWormhole(APPID, self.relayurl)
|
2015-10-04 05:03:27 +00:00
|
|
|
d = deferToThread(w1.get_code)
|
|
|
|
def _got_code(code):
|
|
|
|
w2.set_code(code)
|
|
|
|
return self.doBoth([w1.send_data, b"data1"],
|
|
|
|
[w2.get_data])
|
|
|
|
d.addCallback(_got_code)
|
|
|
|
def _sent(res):
|
|
|
|
(_, dataY) = res
|
|
|
|
self.assertEqual(dataY, b"data1")
|
|
|
|
return self.doBoth([w1.get_data], [w2.send_data, b"data2"])
|
|
|
|
d.addCallback(_sent)
|
|
|
|
def _done(dl):
|
|
|
|
(dataX, _) = dl
|
|
|
|
self.assertEqual(dataX, b"data2")
|
|
|
|
return self.doBoth([w1.close], [w2.close])
|
2015-09-27 17:43:25 +00:00
|
|
|
d.addCallback(_done)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def test_fixed_code(self):
|
2015-10-07 00:02:52 +00:00
|
|
|
w1 = BlockingWormhole(APPID, self.relayurl)
|
|
|
|
w2 = BlockingWormhole(APPID, self.relayurl)
|
2015-09-27 17:43:25 +00:00
|
|
|
w1.set_code("123-purple-elephant")
|
|
|
|
w2.set_code("123-purple-elephant")
|
2015-10-04 05:03:27 +00:00
|
|
|
d = self.doBoth([w1.send_data, b"data1"], [w2.send_data, b"data2"])
|
|
|
|
def _sent(res):
|
|
|
|
return self.doBoth([w1.get_data], [w2.get_data])
|
|
|
|
d.addCallback(_sent)
|
2015-09-27 17:43:25 +00:00
|
|
|
def _done(dl):
|
2015-09-28 23:44:00 +00:00
|
|
|
(dataX, dataY) = dl
|
2015-09-28 06:09:51 +00:00
|
|
|
self.assertEqual(dataX, b"data2")
|
|
|
|
self.assertEqual(dataY, b"data1")
|
2015-10-04 05:03:27 +00:00
|
|
|
return self.doBoth([w1.close], [w2.close])
|
2015-09-27 17:43:25 +00:00
|
|
|
d.addCallback(_done)
|
|
|
|
return d
|
|
|
|
|
2015-09-28 23:49:36 +00:00
|
|
|
def test_verifier(self):
|
2015-10-07 00:02:52 +00:00
|
|
|
w1 = BlockingWormhole(APPID, self.relayurl)
|
|
|
|
w2 = BlockingWormhole(APPID, self.relayurl)
|
2015-09-28 23:49:36 +00:00
|
|
|
d = deferToThread(w1.get_code)
|
|
|
|
def _got_code(code):
|
|
|
|
w2.set_code(code)
|
2015-10-04 05:03:27 +00:00
|
|
|
return self.doBoth([w1.get_verifier], [w2.get_verifier])
|
2015-09-28 23:49:36 +00:00
|
|
|
d.addCallback(_got_code)
|
|
|
|
def _check_verifier(res):
|
|
|
|
v1, v2 = res
|
|
|
|
self.failUnlessEqual(type(v1), type(b""))
|
|
|
|
self.failUnlessEqual(v1, v2)
|
2015-10-04 05:03:27 +00:00
|
|
|
return self.doBoth([w1.send_data, b"data1"],
|
|
|
|
[w2.send_data, b"data2"])
|
2015-09-28 23:49:36 +00:00
|
|
|
d.addCallback(_check_verifier)
|
2015-10-04 05:03:27 +00:00
|
|
|
def _sent(res):
|
|
|
|
return self.doBoth([w1.get_data], [w2.get_data])
|
|
|
|
d.addCallback(_sent)
|
2015-09-28 23:49:36 +00:00
|
|
|
def _done(dl):
|
|
|
|
(dataX, dataY) = dl
|
|
|
|
self.assertEqual(dataX, b"data2")
|
|
|
|
self.assertEqual(dataY, b"data1")
|
2015-10-04 05:03:27 +00:00
|
|
|
return self.doBoth([w1.close], [w2.close])
|
2015-09-28 23:49:36 +00:00
|
|
|
d.addCallback(_done)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def test_verifier_mismatch(self):
|
2015-10-07 00:02:52 +00:00
|
|
|
w1 = BlockingWormhole(APPID, self.relayurl)
|
|
|
|
w2 = BlockingWormhole(APPID, self.relayurl)
|
2015-09-28 23:49:36 +00:00
|
|
|
d = deferToThread(w1.get_code)
|
|
|
|
def _got_code(code):
|
|
|
|
w2.set_code(code+"not")
|
2015-10-04 05:03:27 +00:00
|
|
|
return self.doBoth([w1.get_verifier], [w2.get_verifier])
|
2015-09-28 23:49:36 +00:00
|
|
|
d.addCallback(_got_code)
|
|
|
|
def _check_verifier(res):
|
|
|
|
v1, v2 = res
|
|
|
|
self.failUnlessEqual(type(v1), type(b""))
|
|
|
|
self.failIfEqual(v1, v2)
|
2015-10-04 05:03:27 +00:00
|
|
|
return self.doBoth([w1.close], [w2.close])
|
2015-09-28 23:49:36 +00:00
|
|
|
d.addCallback(_check_verifier)
|
|
|
|
return d
|
|
|
|
|
2015-09-27 17:43:25 +00:00
|
|
|
def test_errors(self):
|
2015-10-07 00:02:52 +00:00
|
|
|
w1 = BlockingWormhole(APPID, self.relayurl)
|
2015-09-27 17:43:25 +00:00
|
|
|
self.assertRaises(UsageError, w1.get_verifier)
|
2015-10-04 05:03:27 +00:00
|
|
|
self.assertRaises(UsageError, w1.get_data)
|
|
|
|
self.assertRaises(UsageError, w1.send_data, b"data")
|
2015-09-27 17:43:25 +00:00
|
|
|
w1.set_code("123-purple-elephant")
|
|
|
|
self.assertRaises(UsageError, w1.set_code, "123-nope")
|
|
|
|
self.assertRaises(UsageError, w1.get_code)
|
2015-10-07 00:02:52 +00:00
|
|
|
w2 = BlockingWormhole(APPID, self.relayurl)
|
2015-09-27 17:43:25 +00:00
|
|
|
d = deferToThread(w2.get_code)
|
|
|
|
def _done(code):
|
|
|
|
self.assertRaises(UsageError, w2.get_code)
|
2015-10-04 05:03:27 +00:00
|
|
|
return self.doBoth([w1.close], [w2.close])
|
2015-09-27 17:43:25 +00:00
|
|
|
d.addCallback(_done)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def test_serialize(self):
|
2015-10-07 00:02:52 +00:00
|
|
|
w1 = BlockingWormhole(APPID, self.relayurl)
|
2015-09-27 17:43:25 +00:00
|
|
|
self.assertRaises(UsageError, w1.serialize) # too early
|
2015-10-07 00:02:52 +00:00
|
|
|
w2 = BlockingWormhole(APPID, self.relayurl)
|
2015-09-27 17:43:25 +00:00
|
|
|
d = deferToThread(w1.get_code)
|
|
|
|
def _got_code(code):
|
|
|
|
self.assertRaises(UsageError, w2.serialize) # too early
|
|
|
|
w2.set_code(code)
|
|
|
|
w2.serialize() # ok
|
|
|
|
s = w1.serialize()
|
|
|
|
self.assertEqual(type(s), type(""))
|
|
|
|
unpacked = json.loads(s) # this is supposed to be JSON
|
|
|
|
self.assertEqual(type(unpacked), dict)
|
2015-10-04 05:03:27 +00:00
|
|
|
self.new_w1 = BlockingWormhole.from_serialized(s)
|
|
|
|
return self.doBoth([self.new_w1.send_data, b"data1"],
|
|
|
|
[w2.send_data, b"data2"])
|
2015-09-27 17:43:25 +00:00
|
|
|
d.addCallback(_got_code)
|
2015-10-04 05:03:27 +00:00
|
|
|
def _sent(res):
|
|
|
|
return self.doBoth(self.new_w1.get_data(), w2.get_data())
|
|
|
|
d.addCallback(_sent)
|
2015-09-27 17:43:25 +00:00
|
|
|
def _done(dl):
|
2015-09-28 23:44:00 +00:00
|
|
|
(dataX, dataY) = dl
|
2015-09-28 06:09:51 +00:00
|
|
|
self.assertEqual(dataX, b"data2")
|
|
|
|
self.assertEqual(dataY, b"data1")
|
2015-09-27 17:43:25 +00:00
|
|
|
self.assertRaises(UsageError, w2.serialize) # too late
|
2015-10-04 05:03:27 +00:00
|
|
|
return self.doBoth([w1.close], [w2.close])
|
2015-09-27 17:43:25 +00:00
|
|
|
d.addCallback(_done)
|
|
|
|
return d
|
|
|
|
test_serialize.skip = "not yet implemented for the blocking flavor"
|