1import os 2import time 3 4import infra.basetest 5 6 7class TestSoCat(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_PACKAGE_SOCAT=y 13 BR2_TARGET_ROOTFS_CPIO=y 14 # BR2_TARGET_ROOTFS_TAR is not set 15 """ 16 17 def test_run(self): 18 cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio") 19 self.emulator.boot(arch="armv5", 20 kernel="builtin", 21 options=["-initrd", cpio_file]) 22 self.emulator.login() 23 24 # Some values, for the test. 25 msg = "Hello Buildroot!" 26 out_file = "output.txt" 27 port1 = 11111 28 port2 = 22222 29 30 # Check the program can execute. 31 self.assertRunOk("socat -V") 32 33 # We start the receiver netcat on tcp/port2. 34 cmd = f"nc -n -l -p {port2} > {out_file} 2> /dev/null &" 35 self.assertRunOk(cmd) 36 37 time.sleep(2 * self.timeout_multiplier) 38 39 # We start socat in background to listen on tcp/port1 and 40 # forward to tcp/port2. 41 cmd = f"socat TCP4-LISTEN:{port1} TCP4:127.0.0.1:{port2} &" 42 self.assertRunOk(cmd) 43 44 time.sleep(2 * self.timeout_multiplier) 45 46 # We write a message on tcp/port1. Socat is expected to 47 # forward the message to the receiver on tcp/port2, and write 48 # our message in a file. 49 cmd = f"echo '{msg}' | nc -n -c 127.0.0.1 {port1}" 50 self.assertRunOk(cmd) 51 52 # We check the output file contains our message. 53 cmd = f"cat {out_file}" 54 out, ret = self.emulator.run(cmd) 55 self.assertEqual(ret, 0) 56 self.assertEqual(out[0], msg) 57