test_nmap.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import os
  2. import time
  3. import infra.basetest
  4. class TestNmap(infra.basetest.BRTest):
  5. config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
  6. """
  7. BR2_PACKAGE_BUSYBOX_SHOW_OTHERS=y
  8. BR2_PACKAGE_NMAP=y
  9. BR2_PACKAGE_NMAP_NCAT=y
  10. BR2_PACKAGE_NMAP_NMAP=y
  11. BR2_TARGET_ROOTFS_CPIO=y
  12. # BR2_TARGET_ROOTFS_TAR is not set
  13. """
  14. def test_run(self):
  15. cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
  16. self.emulator.boot(arch="armv5",
  17. kernel="builtin",
  18. options=["-initrd", cpio_file])
  19. self.emulator.login()
  20. # Check the program can execute.
  21. self.assertRunOk("nmap --version")
  22. # We open few ports, using the nmap netcat "nc" commands.
  23. ports = [21, 23, 25, 80]
  24. for port in ports:
  25. cmd = f"nc -l -p {port} </dev/null &>/dev/null &"
  26. self.assertRunOk(cmd)
  27. time.sleep(1 * self.timeout_multiplier)
  28. # We run a local port scan. We should see in the output the
  29. # ports we previously opened.
  30. out, ret = self.emulator.run("nmap -n -sS 127.0.0.1", timeout=20)
  31. self.assertEqual(ret, 0)
  32. nmap_out = "\n".join(out)
  33. for port in ports:
  34. self.assertRegex(nmap_out, f"{port}/tcp *open")