test_socat.py 1.7 KB

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