test_iptables.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import os
  2. import infra.basetest
  3. class TestIptables(infra.basetest.BRTest):
  4. # The iptables package has _LINUX_CONFIG_FIXUPS, so we cannot use
  5. # the runtime test pre-built Kernel. We need to compile a Kernel
  6. # to make sure it will include the required configuration.
  7. config = \
  8. """
  9. BR2_aarch64=y
  10. BR2_TOOLCHAIN_EXTERNAL=y
  11. BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
  12. BR2_LINUX_KERNEL=y
  13. BR2_LINUX_KERNEL_CUSTOM_VERSION=y
  14. BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="6.1.82"
  15. BR2_LINUX_KERNEL_USE_CUSTOM_CONFIG=y
  16. BR2_LINUX_KERNEL_CUSTOM_CONFIG_FILE="board/qemu/aarch64-virt/linux.config"
  17. BR2_LINUX_KERNEL_NEEDS_HOST_OPENSSL=y
  18. BR2_PACKAGE_IPTABLES=y
  19. BR2_TARGET_ROOTFS_CPIO=y
  20. BR2_TARGET_ROOTFS_CPIO_GZIP=y
  21. # BR2_TARGET_ROOTFS_TAR is not set
  22. """
  23. def test_run(self):
  24. img = os.path.join(self.builddir, "images", "rootfs.cpio.gz")
  25. kern = os.path.join(self.builddir, "images", "Image")
  26. self.emulator.boot(arch="aarch64",
  27. kernel=kern,
  28. kernel_cmdline=["console=ttyAMA0"],
  29. options=["-M", "virt",
  30. "-cpu", "cortex-a57",
  31. "-m", "256M",
  32. "-initrd", img])
  33. self.emulator.login()
  34. # We check the program can execute.
  35. self.assertRunOk("iptables --version")
  36. # We delete all rules in all chains. We also set default
  37. # policies to ACCEPT for INPUT and OUTPUT chains. This should
  38. # already be the case (default Kernel config). This makes sure
  39. # this test starts from a known state and also those common
  40. # command invocations works.
  41. self.assertRunOk("iptables --flush")
  42. self.assertRunOk("iptables --policy INPUT ACCEPT")
  43. self.assertRunOk("iptables --policy OUTPUT ACCEPT")
  44. # We add a filter rule to drop all the ICMP protocol to the
  45. # IPv4 destination 127.0.0.2, in the INPUT chain. This should
  46. # block all pings (icmp echo-requests).
  47. cmd = "iptables --append INPUT"
  48. cmd += " --protocol icmp --destination 127.0.0.2 --jump DROP"
  49. self.assertRunOk(cmd)
  50. # We check we can list rules.
  51. self.assertRunOk("iptables --list")
  52. # A ping to 127.0.0.1 is expected to work, because it's not
  53. # matching our rule. We expect 3 replies (-c), with 0.5s
  54. # internal (-i), and set a maximum timeout of 2s.
  55. ping_cmd_prefix = "ping -c 3 -i 0.5 -W 2 "
  56. self.assertRunOk(ping_cmd_prefix + "127.0.0.1")
  57. # A ping to 127.0.0.2 is expected to fail, because our rule is
  58. # supposed to drop it.
  59. ping_test_cmd = ping_cmd_prefix + "127.0.0.2"
  60. _, exit_code = self.emulator.run(ping_test_cmd)
  61. self.assertNotEqual(exit_code, 0)
  62. # We delete our only rule #1 in the INPUT chain.
  63. self.assertRunOk("iptables --delete INPUT 1")
  64. # Since we deleted the rule, the ping test command which was
  65. # supposed to fail earlier is now supposed to succeed.
  66. self.assertRunOk(ping_test_cmd)