test_ngrep.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import os
  2. import time
  3. import infra.basetest
  4. class TestNgrep(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_NGREP=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. port = 12345
  20. msg = 'Hello Buildroot'
  21. # Check the program can execute.
  22. self.assertRunOk("ngrep -V")
  23. # Start a netcat server in background accepting connections
  24. cmd = f"nc -l -p {port} >/dev/null </dev/null &"
  25. self.assertRunOk(cmd)
  26. time.sleep(1)
  27. # Start a netcat client in background, sending one message
  28. # every seconds
  29. cmd = "( while true ; do "
  30. cmd += f"echo '{msg}'; "
  31. cmd += "sleep 1 ; "
  32. cmd += "done) | "
  33. cmd += f"nc 127.0.0.1 {port} &"
  34. self.assertRunOk(cmd)
  35. time.sleep(1)
  36. # Capture 3 packets with the message.
  37. cmd = f"ngrep -n 3 '{msg}'"
  38. self.assertRunOk(cmd)