rename send_data/get_data to just send/get
This commit is contained in:
parent
49785008bb
commit
501af4b4ec
|
@ -90,7 +90,7 @@ class TwistedReceiver:
|
|||
raise RespondError("unknown offer type")
|
||||
except RespondError as r:
|
||||
data = json.dumps({"error": r.response}).encode("utf-8")
|
||||
yield w.send_data(data)
|
||||
yield w.send(data)
|
||||
raise TransferError(r.response)
|
||||
returnValue(None)
|
||||
|
||||
|
@ -113,7 +113,7 @@ class TwistedReceiver:
|
|||
@inlineCallbacks
|
||||
def get_data(self, w):
|
||||
# this may raise WrongPasswordError
|
||||
them_bytes = yield w.get_data()
|
||||
them_bytes = yield w.get()
|
||||
them_d = json.loads(them_bytes.decode("utf-8"))
|
||||
if "error" in them_d:
|
||||
raise TransferError(them_d["error"])
|
||||
|
@ -124,7 +124,7 @@ class TwistedReceiver:
|
|||
# we're receiving a text message
|
||||
self.msg(them_d["message"])
|
||||
data = json.dumps({"message_ack": "ok"}).encode("utf-8")
|
||||
yield w.send_data(data, wait=True)
|
||||
yield w.send(data, wait=True)
|
||||
|
||||
def handle_file(self, them_d):
|
||||
file_data = them_d["file"]
|
||||
|
@ -199,7 +199,7 @@ class TwistedReceiver:
|
|||
"relay_connection_hints": relay_hints,
|
||||
},
|
||||
}).encode("utf-8")
|
||||
yield w.send_data(data)
|
||||
yield w.send(data)
|
||||
|
||||
# now receive the rest of the owl
|
||||
tdata = them_d["transit"]
|
||||
|
|
|
@ -94,17 +94,17 @@ def _send(reactor, w, args, phase1, fd_to_send, tor_manager):
|
|||
if ok.lower() == "no":
|
||||
err = "sender rejected verification check, abandoned transfer"
|
||||
reject_data = json.dumps({"error": err}).encode("utf-8")
|
||||
yield w.send_data(reject_data)
|
||||
yield w.send(reject_data)
|
||||
raise TransferError(err)
|
||||
if fd_to_send is not None:
|
||||
transit_key = w.derive_key(APPID+"/transit-key")
|
||||
transit_sender.set_transit_key(transit_key)
|
||||
|
||||
my_phase1_bytes = json.dumps(phase1).encode("utf-8")
|
||||
yield w.send_data(my_phase1_bytes)
|
||||
yield w.send(my_phase1_bytes)
|
||||
|
||||
# this may raise WrongPasswordError
|
||||
them_phase1_bytes = yield w.get_data()
|
||||
them_phase1_bytes = yield w.get()
|
||||
|
||||
them_phase1 = json.loads(them_phase1_bytes.decode("utf-8"))
|
||||
|
||||
|
|
|
@ -18,8 +18,8 @@ class Basic(ServerBase, unittest.TestCase):
|
|||
w2 = Wormhole(APPID, self.relayurl)
|
||||
code = yield w1.get_code()
|
||||
w2.set_code(code)
|
||||
yield self.doBoth(w1.send_data(b"data1"), w2.send_data(b"data2"))
|
||||
dl = yield self.doBoth(w1.get_data(), w2.get_data())
|
||||
yield self.doBoth(w1.send(b"data1"), w2.send(b"data2"))
|
||||
dl = yield self.doBoth(w1.get(), w2.get())
|
||||
(dataX, dataY) = dl
|
||||
self.assertEqual(dataX, b"data2")
|
||||
self.assertEqual(dataY, b"data1")
|
||||
|
@ -34,8 +34,8 @@ class Basic(ServerBase, unittest.TestCase):
|
|||
w2 = Wormhole(APPID, self.relayurl)
|
||||
code = yield w1.get_code()
|
||||
w2.set_code(code)
|
||||
yield self.doBoth(w1.send_data(b"data"), w2.send_data(b"data"))
|
||||
dl = yield self.doBoth(w1.get_data(), w2.get_data())
|
||||
yield self.doBoth(w1.send(b"data"), w2.send(b"data"))
|
||||
dl = yield self.doBoth(w1.get(), w2.get())
|
||||
(dataX, dataY) = dl
|
||||
self.assertEqual(dataX, b"data")
|
||||
self.assertEqual(dataY, b"data")
|
||||
|
@ -47,10 +47,10 @@ class Basic(ServerBase, unittest.TestCase):
|
|||
w2 = Wormhole(APPID, self.relayurl)
|
||||
code = yield w1.get_code()
|
||||
w2.set_code(code)
|
||||
res = yield self.doBoth(w1.send_data(b"data1"), w2.get_data())
|
||||
res = yield self.doBoth(w1.send(b"data1"), w2.get())
|
||||
(_, dataY) = res
|
||||
self.assertEqual(dataY, b"data1")
|
||||
dl = yield self.doBoth(w1.get_data(), w2.send_data(b"data2"))
|
||||
dl = yield self.doBoth(w1.get(), w2.send(b"data2"))
|
||||
(dataX, _) = dl
|
||||
self.assertEqual(dataX, b"data2")
|
||||
yield self.doBoth(w1.close(), w2.close())
|
||||
|
@ -61,8 +61,8 @@ class Basic(ServerBase, unittest.TestCase):
|
|||
w2 = Wormhole(APPID, self.relayurl)
|
||||
w1.set_code(u"123-purple-elephant")
|
||||
w2.set_code(u"123-purple-elephant")
|
||||
yield self.doBoth(w1.send_data(b"data1"), w2.send_data(b"data2"))
|
||||
dl = yield self.doBoth(w1.get_data(), w2.get_data())
|
||||
yield self.doBoth(w1.send(b"data1"), w2.send(b"data2"))
|
||||
dl = yield self.doBoth(w1.get(), w2.get())
|
||||
(dataX, dataY) = dl
|
||||
self.assertEqual(dataX, b"data2")
|
||||
self.assertEqual(dataY, b"data1")
|
||||
|
@ -75,17 +75,13 @@ class Basic(ServerBase, unittest.TestCase):
|
|||
w2 = Wormhole(APPID, self.relayurl)
|
||||
w1.set_code(u"123-purple-elephant")
|
||||
w2.set_code(u"123-purple-elephant")
|
||||
yield self.doBoth(w1.send_data(b"data1", u"p1"),
|
||||
w2.send_data(b"data2", u"p1"))
|
||||
yield self.doBoth(w1.send_data(b"data3", u"p2"),
|
||||
w2.send_data(b"data4", u"p2"))
|
||||
dl = yield self.doBoth(w1.get_data(u"p2"),
|
||||
w2.get_data(u"p1"))
|
||||
yield self.doBoth(w1.send(b"data1", u"p1"), w2.send(b"data2", u"p1"))
|
||||
yield self.doBoth(w1.send(b"data3", u"p2"), w2.send(b"data4", u"p2"))
|
||||
dl = yield self.doBoth(w1.get(u"p2"), w2.get(u"p1"))
|
||||
(dataX, dataY) = dl
|
||||
self.assertEqual(dataX, b"data4")
|
||||
self.assertEqual(dataY, b"data1")
|
||||
dl = yield self.doBoth(w1.get_data(u"p1"),
|
||||
w2.get_data(u"p2"))
|
||||
dl = yield self.doBoth(w1.get(u"p1"), w2.get(u"p2"))
|
||||
(dataX, dataY) = dl
|
||||
self.assertEqual(dataX, b"data2")
|
||||
self.assertEqual(dataY, b"data3")
|
||||
|
@ -100,21 +96,21 @@ class Basic(ServerBase, unittest.TestCase):
|
|||
|
||||
# w2 can't throw WrongPasswordError until it sees a CONFIRM message,
|
||||
# and w1 won't send CONFIRM until it sees a PAKE message, which w2
|
||||
# won't send until we call get_data. So we need both sides to be
|
||||
# won't send until we call get. So we need both sides to be
|
||||
# running at the same time for this test.
|
||||
d1 = w1.send_data(b"data1")
|
||||
d1 = w1.send(b"data1")
|
||||
# at this point, w1 should be waiting for w2.PAKE
|
||||
|
||||
yield self.assertFailure(w2.get_data(), WrongPasswordError)
|
||||
yield self.assertFailure(w2.get(), WrongPasswordError)
|
||||
# * w2 will send w2.PAKE, wait for (and get) w1.PAKE, compute a key,
|
||||
# send w2.CONFIRM, then wait for w1.DATA.
|
||||
# * w1 will get w2.PAKE, compute a key, send w1.CONFIRM.
|
||||
# * w1 might also get w2.CONFIRM, and may notice the error before it
|
||||
# sends w1.CONFIRM, in which case the wait=True will signal an
|
||||
# error inside _get_master_key() (inside send_data), and d1 will
|
||||
# error inside _get_master_key() (inside send), and d1 will
|
||||
# errback.
|
||||
# * but w1 might not see w2.CONFIRM yet, in which case it won't
|
||||
# errback until we do w1.get_data()
|
||||
# errback until we do w1.get()
|
||||
# * w2 gets w1.CONFIRM, notices the error, records it.
|
||||
# * w2 (waiting for w1.DATA) wakes up, sees the error, throws
|
||||
# * meanwhile w1 finishes sending its data. w2.CONFIRM may or may not
|
||||
|
@ -124,20 +120,20 @@ class Basic(ServerBase, unittest.TestCase):
|
|||
except WrongPasswordError:
|
||||
pass
|
||||
|
||||
# When we ask w1 to get_data(), one of two things might happen:
|
||||
# When we ask w1 to get(), one of two things might happen:
|
||||
# * if w2.CONFIRM arrived already, it will have recorded the error.
|
||||
# When w1.get_data() sleeps (waiting for w2.DATA), we'll notice the
|
||||
# When w1.get() sleeps (waiting for w2.DATA), we'll notice the
|
||||
# error before sleeping, and throw WrongPasswordError
|
||||
# * if w2.CONFIRM hasn't arrived yet, we'll sleep. When w2.CONFIRM
|
||||
# arrives, we notice and record the error, and wake up, and throw
|
||||
|
||||
# Note that we didn't do w2.send_data(), so we're hoping that w1 will
|
||||
# Note that we didn't do w2.send(), so we're hoping that w1 will
|
||||
# have enough information to detect the error before it sleeps
|
||||
# (waiting for w2.DATA). Checking for the error both before sleeping
|
||||
# and after waking up makes this happen.
|
||||
|
||||
# so now w1 should have enough information to throw too
|
||||
yield self.assertFailure(w1.get_data(), WrongPasswordError)
|
||||
yield self.assertFailure(w1.get(), WrongPasswordError)
|
||||
|
||||
# both sides are closed automatically upon error, but it's still
|
||||
# legal to call .close(), and should be idempotent
|
||||
|
@ -153,9 +149,9 @@ class Basic(ServerBase, unittest.TestCase):
|
|||
|
||||
code = yield w1.get_code()
|
||||
w2.set_code(code)
|
||||
dl = yield self.doBoth(w1.send_data(b"data1"), w2.get_data())
|
||||
dl = yield self.doBoth(w1.send(b"data1"), w2.get())
|
||||
self.assertEqual(dl[1], b"data1")
|
||||
dl = yield self.doBoth(w1.get_data(), w2.send_data(b"data2"))
|
||||
dl = yield self.doBoth(w1.get(), w2.send(b"data2"))
|
||||
self.assertEqual(dl[0], b"data2")
|
||||
yield self.doBoth(w1.close(), w2.close())
|
||||
|
||||
|
@ -169,8 +165,8 @@ class Basic(ServerBase, unittest.TestCase):
|
|||
v1, v2 = res
|
||||
self.failUnlessEqual(type(v1), type(b""))
|
||||
self.failUnlessEqual(v1, v2)
|
||||
yield self.doBoth(w1.send_data(b"data1"), w2.send_data(b"data2"))
|
||||
dl = yield self.doBoth(w1.get_data(), w2.get_data())
|
||||
yield self.doBoth(w1.send(b"data1"), w2.send(b"data2"))
|
||||
dl = yield self.doBoth(w1.get(), w2.get())
|
||||
(dataX, dataY) = dl
|
||||
self.assertEqual(dataX, b"data2")
|
||||
self.assertEqual(dataY, b"data1")
|
||||
|
@ -180,8 +176,8 @@ class Basic(ServerBase, unittest.TestCase):
|
|||
def test_errors(self):
|
||||
w1 = Wormhole(APPID, self.relayurl)
|
||||
yield self.assertFailure(w1.get_verifier(), UsageError)
|
||||
yield self.assertFailure(w1.send_data(b"data"), UsageError)
|
||||
yield self.assertFailure(w1.get_data(), UsageError)
|
||||
yield self.assertFailure(w1.send(b"data"), UsageError)
|
||||
yield self.assertFailure(w1.get(), UsageError)
|
||||
w1.set_code(u"123-purple-elephant")
|
||||
yield self.assertRaises(UsageError, w1.set_code, u"123-nope")
|
||||
yield self.assertFailure(w1.get_code(), UsageError)
|
||||
|
@ -198,22 +194,20 @@ class Basic(ServerBase, unittest.TestCase):
|
|||
w2.set_code(u"123-purple-elephant")
|
||||
# we must let them establish a key before we can send data
|
||||
yield self.doBoth(w1.get_verifier(), w2.get_verifier())
|
||||
yield w1.send_data(b"data1", phase=u"1")
|
||||
yield w1.send(b"data1", phase=u"1")
|
||||
# underscore-prefixed phases are reserved
|
||||
yield self.assertFailure(w1.send_data(b"data1", phase=u"_1"),
|
||||
UsageError)
|
||||
yield self.assertFailure(w1.get_data(phase=u"_1"), UsageError)
|
||||
yield self.assertFailure(w1.send(b"data1", phase=u"_1"), UsageError)
|
||||
yield self.assertFailure(w1.get(phase=u"_1"), UsageError)
|
||||
# you can't send twice to the same phase
|
||||
yield self.assertFailure(w1.send_data(b"data1", phase=u"1"),
|
||||
UsageError)
|
||||
yield self.assertFailure(w1.send(b"data1", phase=u"1"), UsageError)
|
||||
# but you can send to a different one
|
||||
yield w1.send_data(b"data2", phase=u"2")
|
||||
res = yield w2.get_data(phase=u"1")
|
||||
yield w1.send(b"data2", phase=u"2")
|
||||
res = yield w2.get(phase=u"1")
|
||||
self.failUnlessEqual(res, b"data1")
|
||||
# and you can't read twice from the same phase
|
||||
yield self.assertFailure(w2.get_data(phase=u"1"), UsageError)
|
||||
yield self.assertFailure(w2.get(phase=u"1"), UsageError)
|
||||
# but you can read from a different one
|
||||
res = yield w2.get_data(phase=u"2")
|
||||
res = yield w2.get(phase=u"2")
|
||||
self.failUnlessEqual(res, b"data2")
|
||||
yield self.doBoth(w1.close(), w2.close())
|
||||
|
||||
|
@ -232,12 +226,10 @@ class Basic(ServerBase, unittest.TestCase):
|
|||
self.assertEqual(type(unpacked), dict)
|
||||
|
||||
self.new_w1 = Wormhole.from_serialized(s)
|
||||
yield self.doBoth(self.new_w1.send_data(b"data1"),
|
||||
w2.send_data(b"data2"))
|
||||
dl = yield self.doBoth(self.new_w1.get_data(), w2.get_data())
|
||||
yield self.doBoth(self.new_w1.send(b"data1"), w2.send(b"data2"))
|
||||
dl = yield self.doBoth(self.new_w1.get(), w2.get())
|
||||
(dataX, dataY) = dl
|
||||
self.assertEqual((dataX, dataY), (b"data2", b"data1"))
|
||||
self.assertRaises(UsageError, w2.serialize) # too late
|
||||
yield gatherResults([w1.close(), w2.close(), self.new_w1.close()],
|
||||
True)
|
||||
yield gatherResults([w1.close(), w2.close(), self.new_w1.close()], True)
|
||||
|
||||
|
|
|
@ -332,7 +332,7 @@ class Wormhole:
|
|||
|
||||
def serialize(self):
|
||||
# I can only be serialized after get_code/set_code and before
|
||||
# get_verifier/get_data
|
||||
# get_verifier/get
|
||||
if self._code is None: raise UsageError
|
||||
if self._key is not None: raise UsageError
|
||||
if self._sent_phases: raise UsageError
|
||||
|
@ -448,7 +448,7 @@ class Wormhole:
|
|||
def derive_key(self, purpose, length=SecretBox.KEY_SIZE):
|
||||
if not isinstance(purpose, type(u"")): raise TypeError(type(purpose))
|
||||
if self._key is None:
|
||||
# call after get_verifier() or get_data()
|
||||
# call after get_verifier() or get()
|
||||
raise UsageError
|
||||
return HKDF(self._key, length, CTXinfo=to_bytes(purpose))
|
||||
|
||||
|
@ -469,17 +469,17 @@ class Wormhole:
|
|||
return data
|
||||
|
||||
@inlineCallbacks
|
||||
def send_data(self, outbound_data, phase=u"data", wait=False):
|
||||
def send(self, outbound_data, phase=u"data", wait=False):
|
||||
if not isinstance(outbound_data, type(b"")):
|
||||
raise TypeError(type(outbound_data))
|
||||
if not isinstance(phase, type(u"")): raise TypeError(type(phase))
|
||||
if self._closed: raise UsageError
|
||||
if self._code is None:
|
||||
raise UsageError("You must set_code() before send_data()")
|
||||
raise UsageError("You must set_code() before send()")
|
||||
if phase.startswith(u"_"): raise UsageError # reserved for internals
|
||||
if phase in self._sent_phases: raise UsageError # only call this once
|
||||
self._sent_phases.add(phase)
|
||||
with self._timing.add("API send_data", phase=phase, wait=wait):
|
||||
with self._timing.add("API send", phase=phase, wait=wait):
|
||||
# Without predefined roles, we can't derive predictably unique
|
||||
# keys for each side, so we use the same key for both. We use
|
||||
# random nonces to keep the messages distinct, and we
|
||||
|
@ -490,14 +490,14 @@ class Wormhole:
|
|||
yield self._msg_send(phase, outbound_encrypted, wait)
|
||||
|
||||
@inlineCallbacks
|
||||
def get_data(self, phase=u"data"):
|
||||
def get(self, phase=u"data"):
|
||||
if not isinstance(phase, type(u"")): raise TypeError(type(phase))
|
||||
if self._closed: raise UsageError
|
||||
if self._code is None: raise UsageError
|
||||
if phase.startswith(u"_"): raise UsageError # reserved for internals
|
||||
if phase in self._got_phases: raise UsageError # only call this once
|
||||
self._got_phases.add(phase)
|
||||
with self._timing.add("API get_data", phase=phase):
|
||||
with self._timing.add("API get", phase=phase):
|
||||
yield self._get_master_key()
|
||||
body = yield self._msg_get(phase) # we can wait a long time here
|
||||
try:
|
||||
|
|
Loading…
Reference in New Issue
Block a user