test_iptables.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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_INIT_BUSYBOX=y
  12. BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
  13. BR2_LINUX_KERNEL=y
  14. BR2_LINUX_KERNEL_CUSTOM_VERSION=y
  15. BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="6.1.82"
  16. BR2_LINUX_KERNEL_USE_CUSTOM_CONFIG=y
  17. BR2_LINUX_KERNEL_CUSTOM_CONFIG_FILE="board/qemu/aarch64-virt/linux.config"
  18. BR2_LINUX_KERNEL_NEEDS_HOST_OPENSSL=y
  19. BR2_PACKAGE_IPTABLES=y
  20. BR2_TARGET_ROOTFS_CPIO=y
  21. BR2_TARGET_ROOTFS_CPIO_GZIP=y
  22. # BR2_TARGET_ROOTFS_TAR is not set
  23. """
  24. def test_run(self):
  25. img = os.path.join(self.builddir, "images", "rootfs.cpio.gz")
  26. kern = os.path.join(self.builddir, "images", "Image")
  27. self.emulator.boot(arch="aarch64",
  28. kernel=kern,
  29. kernel_cmdline=["console=ttyAMA0"],
  30. options=["-M", "virt",
  31. "-cpu", "cortex-a57",
  32. "-m", "256M",
  33. "-initrd", img])
  34. self.emulator.login()
  35. # We check the program can execute.
  36. self.assertRunOk("iptables --version")
  37. # We delete all rules in all chains. We also set default
  38. # policies to ACCEPT for INPUT and OUTPUT chains. This should
  39. # already be the case (default Kernel config). This makes sure
  40. # this test starts from a known state and also those common
  41. # command invocations works.
  42. self.assertRunOk("iptables --flush")
  43. self.assertRunOk("iptables --policy INPUT ACCEPT")
  44. self.assertRunOk("iptables --policy OUTPUT ACCEPT")
  45. # We add a filter rule to drop all the ICMP protocol to the
  46. # IPv4 destination 127.0.0.2, in the INPUT chain. This should
  47. # block all pings (icmp echo-requests).
  48. cmd = "iptables --append INPUT"
  49. cmd += " --protocol icmp --destination 127.0.0.2 --jump DROP"
  50. self.assertRunOk(cmd)
  51. # We check we can list rules.
  52. self.assertRunOk("iptables --list")
  53. # A ping to 127.0.0.1 is expected to work, because it's not
  54. # matching our rule. We expect 3 replies (-c), with 0.5s
  55. # internal (-i), and set a maximum timeout of 2s.
  56. ping_cmd_prefix = "ping -c 3 -i 0.5 -W 2 "
  57. self.assertRunOk(ping_cmd_prefix + "127.0.0.1")
  58. # A ping to 127.0.0.2 is expected to fail, because our rule is
  59. # supposed to drop it.
  60. ping_test_cmd = ping_cmd_prefix + "127.0.0.2"
  61. _, exit_code = self.emulator.run(ping_test_cmd)
  62. self.assertNotEqual(exit_code, 0)
  63. # Save the current rules to test the init script later.
  64. self.assertRunOk("/etc/init.d/S35iptables save")
  65. # We delete our only rule #1 in the INPUT chain.
  66. self.assertRunOk("iptables --delete INPUT 1")
  67. # Since we deleted the rule, the ping test command which was
  68. # supposed to fail earlier is now supposed to succeed.
  69. self.assertRunOk(ping_test_cmd)
  70. # Load the rules as saved before.
  71. self.assertRunOk("/etc/init.d/S35iptables start")
  72. # Ping to 127.0.0.2 is expected to fail again.
  73. _, exit_code = self.emulator.run(ping_test_cmd)
  74. self.assertNotEqual(exit_code, 0)
  75. # And flush the rules again.
  76. self.assertRunOk("/etc/init.d/S35iptables stop")
  77. # Since we deleted the rule, the ping test command which was
  78. # supposed to fail earlier is now supposed to succeed.
  79. self.assertRunOk(ping_test_cmd)