1import os
2import time
3
4import infra.basetest
5
6
7class TestNetCat(infra.basetest.BRTest):
8    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
9        """
10        BR2_PACKAGE_BUSYBOX_SHOW_OTHERS=y
11        BR2_PACKAGE_NETCAT=y
12        BR2_TARGET_ROOTFS_CPIO=y
13        # BR2_TARGET_ROOTFS_TAR is not set
14        """
15
16    def test_run(self):
17        cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
18        self.emulator.boot(arch="armv5",
19                           kernel="builtin",
20                           options=["-initrd", cpio_file])
21        self.emulator.login()
22
23        self.assertRunOk("nc --version")
24
25        msg = "Hello Buildroot!"
26        out_file = "output.txt"
27        port = 12345
28
29        cmd = f"nc -n -l -p {port} > {out_file} 2> /dev/null &"
30        self.assertRunOk(cmd)
31
32        time.sleep(5)
33
34        cmd = f"echo '{msg}' | nc -n -c 127.0.0.1 {port}"
35        self.assertRunOk(cmd)
36
37        cmd = f"cat {out_file}"
38        out, ret = self.emulator.run(cmd)
39        self.assertEqual(ret, 0)
40        self.assertEqual(out[0], msg)
41