1import os
2import time
3
4from tests.package.test_python import TestPythonPackageBase
5
6
7class TestPythonPy3MagicWormhole(TestPythonPackageBase):
8    __test__ = True
9    config = TestPythonPackageBase.config + \
10        """
11        BR2_PACKAGE_PYTHON3=y
12        BR2_PACKAGE_PYTHON_MAGIC_WORMHOLE=y
13        BR2_PACKAGE_PYTHON_MAGIC_WORMHOLE_MAILBOX_SERVER=y
14        BR2_PACKAGE_PYTHON_MAGIC_WORMHOLE_TRANSIT_RELAY=y
15        BR2_TARGET_ROOTFS_CPIO=y
16        # BR2_TARGET_ROOTFS_TAR is not set
17        """
18    timeout = 60
19
20    def twistd_cmd(self, command):
21        s = "twistd"
22        s += " --pidfile=/tmp/{}.pid".format(command)
23        s += " --logfile=/tmp/{}.log".format(command)
24        s += " {}".format(command)
25
26        return s
27
28    def test_run(self):
29        code = "123-hello-buildroot"
30        text = "Hello Buildroot!"
31
32        relay_url = "ws://localhost:4000/v1"
33        transit_helper = "tcp:localhost:4001"
34
35        img = os.path.join(self.builddir, "images", "rootfs.cpio")
36        self.emulator.boot(arch="armv5", kernel="builtin",
37                           options=["-initrd", img])
38
39        self.emulator.login()
40
41        cmd = self.twistd_cmd("wormhole-mailbox")
42        self.assertRunOk(cmd, timeout=30)
43
44        cmd = self.twistd_cmd("transitrelay")
45        self.assertRunOk(cmd, timeout=30)
46
47        wormhole_cmd = "wormhole --relay-url={} --transit-helper={}".format(
48            relay_url, transit_helper)
49
50        cmd = wormhole_cmd
51        cmd += f" send --code={code} --text=\"{text}\""
52        cmd += " &> /dev/null &"
53        self.assertRunOk(cmd)
54
55        time.sleep(30 * self.timeout_multiplier)
56
57        wormhole_env = "_MAGIC_WORMHOLE_TEST_KEY_TIMER=100 "
58        wormhole_env += "_MAGIC_WORMHOLE_TEST_VERIFY_TIMER=100 "
59        cmd = wormhole_env + wormhole_cmd + " receive {}".format(code)
60        output, exit_code = self.emulator.run(cmd, timeout=35)
61        self.assertEqual(exit_code, 0)
62        self.assertEqual(output[0], text)
63