2015-11-29 07:33:15 +00:00
|
|
|
import os, sys, re
|
2015-09-27 01:00:09 +00:00
|
|
|
from twisted.trial import unittest
|
|
|
|
from twisted.python import procutils, log
|
|
|
|
from twisted.internet.utils import getProcessOutputAndValue
|
|
|
|
from .. import __version__
|
|
|
|
from .common import ServerBase
|
|
|
|
|
2015-09-27 17:43:25 +00:00
|
|
|
class ScriptsBase:
|
2015-09-27 01:00:09 +00:00
|
|
|
def find_executable(self):
|
2015-09-27 01:29:46 +00:00
|
|
|
# to make sure we're running the right executable (in a virtualenv),
|
|
|
|
# we require that our "wormhole" lives in the same directory as our
|
|
|
|
# "python"
|
2015-09-27 01:00:09 +00:00
|
|
|
locations = procutils.which("wormhole")
|
|
|
|
if not locations:
|
|
|
|
raise unittest.SkipTest("unable to find 'wormhole' in $PATH")
|
|
|
|
wormhole = locations[0]
|
2015-09-27 01:29:46 +00:00
|
|
|
if (os.path.dirname(os.path.abspath(wormhole)) !=
|
|
|
|
os.path.dirname(sys.executable)):
|
2015-09-27 01:00:09 +00:00
|
|
|
log.msg("locations: %s" % (locations,))
|
2015-09-27 01:29:46 +00:00
|
|
|
log.msg("sys.executable: %s" % (sys.executable,))
|
2015-09-27 01:17:50 +00:00
|
|
|
raise unittest.SkipTest("found the wrong 'wormhole' in $PATH: %s %s"
|
2015-09-27 01:29:46 +00:00
|
|
|
% (wormhole, sys.executable))
|
2015-09-27 01:00:09 +00:00
|
|
|
return wormhole
|
|
|
|
|
2015-09-27 17:43:25 +00:00
|
|
|
def is_runnable(self):
|
|
|
|
# One property of Versioneer is that many changes to the source tree
|
|
|
|
# (making a commit, dirtying a previously-clean tree) will change the
|
|
|
|
# version string. Entrypoint scripts frequently insist upon importing
|
|
|
|
# a library version that matches the script version (whatever was
|
|
|
|
# reported when 'pip install' was run), and throw a
|
|
|
|
# DistributionNotFound error when they don't match. This is really
|
|
|
|
# annoying in a workspace created with "pip install -e .", as you
|
|
|
|
# must re-run pip after each commit.
|
|
|
|
|
|
|
|
# So let's report just one error in this case (from test_version),
|
|
|
|
# and skip the other tests that we know will fail.
|
|
|
|
|
|
|
|
wormhole = self.find_executable()
|
|
|
|
d = getProcessOutputAndValue(wormhole, ["--version"])
|
|
|
|
def _check(res):
|
|
|
|
out, err, rc = res
|
|
|
|
if rc != 0:
|
|
|
|
raise unittest.SkipTest("wormhole is not runnable in this tree")
|
|
|
|
d.addCallback(_check)
|
|
|
|
return d
|
|
|
|
|
|
|
|
class ScriptVersion(ServerBase, ScriptsBase, unittest.TestCase):
|
|
|
|
# we need Twisted to run the server, but we run the sender and receiver
|
|
|
|
# with deferToThread()
|
|
|
|
|
2015-09-27 01:00:09 +00:00
|
|
|
def test_version(self):
|
|
|
|
# "wormhole" must be on the path, so e.g. "pip install -e ." in a
|
2015-09-28 23:31:35 +00:00
|
|
|
# virtualenv. This guards against an environment where the tests
|
|
|
|
# below might run the wrong executable.
|
2015-09-27 01:00:09 +00:00
|
|
|
wormhole = self.find_executable()
|
|
|
|
d = getProcessOutputAndValue(wormhole, ["--version"])
|
|
|
|
def _check(res):
|
|
|
|
out, err, rc = res
|
2015-09-28 23:31:35 +00:00
|
|
|
# argparse on py2 and py3.3 sends --version to stderr
|
|
|
|
# argparse on py3.4/py3.5 sends --version to stdout
|
2015-09-28 06:40:00 +00:00
|
|
|
# aargh
|
|
|
|
err = err.decode("utf-8")
|
2015-09-27 17:43:25 +00:00
|
|
|
if "DistributionNotFound" in err:
|
|
|
|
log.msg("stderr was %s" % err)
|
|
|
|
last = err.strip().split("\n")[-1]
|
|
|
|
self.fail("wormhole not runnable: %s" % last)
|
2015-09-28 23:31:35 +00:00
|
|
|
ver = out.decode("utf-8") or err
|
|
|
|
self.failUnlessEqual(ver, "magic-wormhole %s\n" % __version__)
|
2015-09-27 01:00:09 +00:00
|
|
|
self.failUnlessEqual(rc, 0)
|
|
|
|
d.addCallback(_check)
|
|
|
|
return d
|
|
|
|
|
2015-09-27 17:43:25 +00:00
|
|
|
class Scripts(ServerBase, ScriptsBase, unittest.TestCase):
|
|
|
|
# we need Twisted to run the server, but we run the sender and receiver
|
|
|
|
# with deferToThread()
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
d = self.is_runnable()
|
|
|
|
d.addCallback(lambda _: ServerBase.setUp(self))
|
|
|
|
return d
|
|
|
|
|
2015-09-27 01:00:09 +00:00
|
|
|
def test_send_text_pre_generated_code(self):
|
|
|
|
wormhole = self.find_executable()
|
|
|
|
server_args = ["--relay-url", self.relayurl]
|
2015-10-07 02:42:10 +00:00
|
|
|
code = u"1-abc"
|
2015-09-27 01:00:09 +00:00
|
|
|
message = "test message"
|
|
|
|
send_args = server_args + [
|
2015-10-04 05:45:43 +00:00
|
|
|
"send",
|
2015-09-27 01:00:09 +00:00
|
|
|
"--code", code,
|
2015-10-04 06:27:28 +00:00
|
|
|
"--text", message,
|
2015-09-27 01:00:09 +00:00
|
|
|
]
|
|
|
|
d1 = getProcessOutputAndValue(wormhole, send_args)
|
|
|
|
receive_args = server_args + [
|
2015-10-04 05:45:43 +00:00
|
|
|
"receive",
|
2015-09-27 01:00:09 +00:00
|
|
|
code,
|
|
|
|
]
|
|
|
|
d2 = getProcessOutputAndValue(wormhole, receive_args)
|
|
|
|
def _check_sender(res):
|
|
|
|
out, err, rc = res
|
2015-09-28 06:40:00 +00:00
|
|
|
out = out.decode("utf-8")
|
|
|
|
err = err.decode("utf-8")
|
2015-10-07 00:20:12 +00:00
|
|
|
self.maxDiff = None
|
|
|
|
expected = ("Sending text message (%d bytes)\n"
|
|
|
|
"On the other computer, please run: "
|
|
|
|
"wormhole receive\n"
|
|
|
|
"Wormhole code is: %s\n\n"
|
|
|
|
"text message sent\n" % (len(message), code))
|
|
|
|
self.failUnlessEqual( (expected, "", 0),
|
|
|
|
(out, err, rc) )
|
2015-09-27 01:00:09 +00:00
|
|
|
return d2
|
|
|
|
d1.addCallback(_check_sender)
|
|
|
|
def _check_receiver(res):
|
|
|
|
out, err, rc = res
|
2015-09-28 06:40:00 +00:00
|
|
|
out = out.decode("utf-8")
|
|
|
|
err = err.decode("utf-8")
|
2015-10-07 00:20:12 +00:00
|
|
|
self.failUnlessEqual( (message+"\n", "", 0),
|
|
|
|
(out, err, rc) )
|
2015-09-27 01:00:09 +00:00
|
|
|
d1.addCallback(_check_receiver)
|
|
|
|
return d1
|
|
|
|
|
|
|
|
def test_send_file_pre_generated_code(self):
|
2015-11-29 07:40:25 +00:00
|
|
|
return self._do_test_send_file_pre_generated_code(False)
|
|
|
|
def test_send_file_pre_generated_code_override(self):
|
|
|
|
return self._do_test_send_file_pre_generated_code(True)
|
|
|
|
|
|
|
|
def _do_test_send_file_pre_generated_code(self, override_filename):
|
2015-10-07 00:20:12 +00:00
|
|
|
self.maxDiff=None
|
2015-10-07 02:42:10 +00:00
|
|
|
code = u"1-abc"
|
2015-10-04 05:45:43 +00:00
|
|
|
filename = "testfile"
|
2015-09-27 01:00:09 +00:00
|
|
|
message = "test message"
|
|
|
|
|
|
|
|
send_dir = self.mktemp()
|
|
|
|
os.mkdir(send_dir)
|
2015-10-04 05:45:43 +00:00
|
|
|
with open(os.path.join(send_dir, filename), "w") as f:
|
2015-09-27 01:00:09 +00:00
|
|
|
f.write(message)
|
|
|
|
|
|
|
|
wormhole = self.find_executable()
|
2016-02-13 01:11:06 +00:00
|
|
|
server_args = ["--relay-url", self.relayurl,
|
|
|
|
"--transit-helper", self.transit]
|
2015-09-27 01:00:09 +00:00
|
|
|
send_args = server_args + [
|
2015-10-04 05:45:43 +00:00
|
|
|
"send",
|
2015-09-27 01:00:09 +00:00
|
|
|
"--code", code,
|
2015-10-04 05:45:43 +00:00
|
|
|
filename,
|
2015-09-27 01:00:09 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
receive_dir = self.mktemp()
|
|
|
|
os.mkdir(receive_dir)
|
|
|
|
receive_args = server_args + [
|
2015-10-04 05:45:43 +00:00
|
|
|
"receive", "--accept-file",
|
2015-09-27 01:00:09 +00:00
|
|
|
]
|
2015-11-29 07:40:25 +00:00
|
|
|
if override_filename:
|
|
|
|
receive_args.extend(["-o", "outfile"])
|
|
|
|
filename = "outfile"
|
|
|
|
receive_args.append(code)
|
|
|
|
|
|
|
|
d1 = getProcessOutputAndValue(wormhole, send_args, path=send_dir)
|
2015-09-27 01:00:09 +00:00
|
|
|
d2 = getProcessOutputAndValue(wormhole, receive_args, path=receive_dir)
|
|
|
|
def _check_sender(res):
|
|
|
|
out, err, rc = res
|
2015-09-28 06:40:00 +00:00
|
|
|
out = out.decode("utf-8")
|
|
|
|
err = err.decode("utf-8")
|
2015-10-07 00:20:12 +00:00
|
|
|
self.failUnlessEqual(err, "")
|
2015-11-29 07:40:25 +00:00
|
|
|
self.failUnlessIn("Sending %d byte file named 'testfile'\n" %
|
|
|
|
len(message), out)
|
2015-09-27 01:00:09 +00:00
|
|
|
self.failUnlessIn("On the other computer, please run: "
|
2015-10-04 05:45:43 +00:00
|
|
|
"wormhole receive\n"
|
|
|
|
"Wormhole code is: %s\n\n" % code,
|
2015-09-27 01:00:09 +00:00
|
|
|
out)
|
|
|
|
self.failUnlessIn("File sent.. waiting for confirmation\n"
|
|
|
|
"Confirmation received. Transfer complete.\n",
|
|
|
|
out)
|
|
|
|
self.failUnlessEqual(rc, 0)
|
|
|
|
return d2
|
|
|
|
d1.addCallback(_check_sender)
|
|
|
|
def _check_receiver(res):
|
|
|
|
out, err, rc = res
|
2015-09-28 06:40:00 +00:00
|
|
|
out = out.decode("utf-8")
|
|
|
|
err = err.decode("utf-8")
|
2015-10-04 05:45:43 +00:00
|
|
|
self.failUnlessIn("Receiving %d bytes for '%s'" %
|
|
|
|
(len(message), filename), out)
|
2015-09-27 01:00:09 +00:00
|
|
|
self.failUnlessIn("Received file written to ", out)
|
|
|
|
self.failUnlessEqual(err, "")
|
|
|
|
self.failUnlessEqual(rc, 0)
|
2015-10-04 05:45:43 +00:00
|
|
|
fn = os.path.join(receive_dir, filename)
|
2015-09-27 01:00:09 +00:00
|
|
|
self.failUnless(os.path.exists(fn))
|
|
|
|
with open(fn, "r") as f:
|
|
|
|
self.failUnlessEqual(f.read(), message)
|
|
|
|
d1.addCallback(_check_receiver)
|
|
|
|
return d1
|
2015-11-29 07:33:15 +00:00
|
|
|
|
|
|
|
def test_send_directory_pre_generated_code(self):
|
|
|
|
return self._do_test_send_directory_pre_generated_code(False)
|
|
|
|
def test_send_directory_pre_generated_code_override(self):
|
|
|
|
return self._do_test_send_directory_pre_generated_code(True)
|
|
|
|
|
|
|
|
def _do_test_send_directory_pre_generated_code(self, override_dirname):
|
|
|
|
self.maxDiff=None
|
|
|
|
code = u"1-abc"
|
|
|
|
dirname = "testdir"
|
|
|
|
def message(i):
|
|
|
|
return "test message %d\n" % i
|
|
|
|
|
|
|
|
source_parent_dir = self.mktemp()
|
|
|
|
os.mkdir(source_parent_dir)
|
|
|
|
os.mkdir(os.path.join(source_parent_dir, "middle"))
|
|
|
|
source_dir = os.path.join(source_parent_dir, "middle", dirname)
|
|
|
|
os.mkdir(source_dir)
|
|
|
|
for i in range(5):
|
|
|
|
with open(os.path.join(source_dir, str(i)), "w") as f:
|
|
|
|
f.write(message(i))
|
|
|
|
|
|
|
|
target_parent_dir = self.mktemp()
|
|
|
|
os.mkdir(target_parent_dir)
|
|
|
|
|
|
|
|
wormhole = self.find_executable()
|
2016-02-13 01:11:06 +00:00
|
|
|
server_args = ["--relay-url", self.relayurl,
|
|
|
|
"--transit-helper", self.transit]
|
2015-11-29 07:33:15 +00:00
|
|
|
send_args = server_args + [
|
|
|
|
"send",
|
|
|
|
"--code", code,
|
|
|
|
os.path.join("middle", dirname),
|
|
|
|
]
|
|
|
|
|
|
|
|
receive_args = server_args + [
|
|
|
|
"receive", "--accept-file",
|
|
|
|
]
|
|
|
|
if override_dirname:
|
|
|
|
receive_args.extend(["-o", "outdir"])
|
|
|
|
dirname = "outdir"
|
|
|
|
receive_args.append(code)
|
|
|
|
|
|
|
|
d1 = getProcessOutputAndValue(wormhole, send_args,
|
|
|
|
path=source_parent_dir)
|
|
|
|
|
|
|
|
d2 = getProcessOutputAndValue(wormhole, receive_args,
|
|
|
|
path=target_parent_dir)
|
|
|
|
def _check_sender(res):
|
|
|
|
out, err, rc = res
|
|
|
|
out = out.decode("utf-8")
|
|
|
|
err = err.decode("utf-8")
|
|
|
|
self.failUnlessEqual(err, "")
|
|
|
|
self.failUnlessIn("Sending directory", out)
|
|
|
|
self.failUnlessIn("named 'testdir'", out)
|
|
|
|
self.failUnlessIn("On the other computer, please run: "
|
|
|
|
"wormhole receive\n"
|
|
|
|
"Wormhole code is: %s\n\n" % code,
|
|
|
|
out)
|
|
|
|
self.failUnlessIn("File sent.. waiting for confirmation\n"
|
|
|
|
"Confirmation received. Transfer complete.\n",
|
|
|
|
out)
|
|
|
|
self.failUnlessEqual(rc, 0)
|
|
|
|
return d2
|
|
|
|
d1.addCallback(_check_sender)
|
|
|
|
def _check_receiver(res):
|
|
|
|
out, err, rc = res
|
|
|
|
out = out.decode("utf-8")
|
|
|
|
err = err.decode("utf-8")
|
|
|
|
self.failUnless(re.search(r"Receiving \d+ bytes for '%s'" %
|
|
|
|
dirname, out))
|
|
|
|
self.failUnlessIn("Received files written to %s" % dirname, out)
|
|
|
|
self.failUnlessEqual(err, "")
|
|
|
|
self.failUnlessEqual(rc, 0)
|
|
|
|
fn = os.path.join(target_parent_dir, dirname)
|
|
|
|
self.failUnless(os.path.exists(fn))
|
|
|
|
for i in range(5):
|
|
|
|
fn = os.path.join(target_parent_dir, dirname, str(i))
|
|
|
|
with open(fn, "r") as f:
|
|
|
|
self.failUnlessEqual(f.read(), message(i))
|
|
|
|
d1.addCallback(_check_receiver)
|
|
|
|
return d1
|