1"""Test firewalld for both systemd and sysvinit."""
2import os
3import time
4import infra.basetest
5
6
7class TestFirewalldSystemd(infra.basetest.BRTest):
8    """Build the kernel as firewalld requires several the nftable options."""
9
10    config = """
11        BR2_arm=y
12        BR2_cortex_a9=y
13        BR2_ARM_ENABLE_VFP=y
14        BR2_TOOLCHAIN_EXTERNAL=y
15        BR2_TOOLCHAIN_EXTERNAL_BOOTLIN=y
16        BR2_INIT_SYSTEMD=y
17        BR2_LINUX_KERNEL=y
18        BR2_LINUX_KERNEL_CUSTOM_VERSION=y
19        BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="6.1.61"
20        BR2_LINUX_KERNEL_DEFCONFIG="vexpress"
21        BR2_LINUX_KERNEL_DTS_SUPPORT=y
22        BR2_LINUX_KERNEL_INTREE_DTS_NAME="vexpress-v2p-ca9"
23        BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
24        BR2_PACKAGE_PYTHON3=y
25        BR2_PACKAGE_FIREWALLD=y
26        BR2_TARGET_ROOTFS_CPIO=y
27        # BR2_TARGET_ROOTFS_TAR is not set
28        """
29
30    def test_run(self):
31        cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
32        kernel_file = os.path.join(self.builddir, "images", "zImage")
33        dtb_file = os.path.join(self.builddir, "images", "vexpress-v2p-ca9.dtb")
34        self.emulator.boot(arch="armv7",
35                           kernel=kernel_file,
36                           kernel_cmdline=["console=ttyAMA0,115200"],
37                           options=[
38                               "-initrd", cpio_file,
39                               "-dtb", dtb_file,
40                               "-M", "vexpress-a9"
41                           ])
42        # It takes quite some time for the system to boot with firewalld,
43        self.emulator.login(timeout=120)
44
45        # It may take some time for firewalld to finish startup.
46        # Give it at least 15 seconds.
47        is_active = False
48        for i in range(15):
49            output, _ = self.emulator.run("systemctl is-active firewalld")
50            if output[0] == "active":
51                is_active = True
52                break
53            time.sleep(1)
54        if not is_active:
55            self.fail("firewalld failed to activate!")
56
57        cmd = "firewall-cmd --state"
58        output, exit_code = self.emulator.run(cmd, timeout=10)
59        self.assertIn("running", output[0])
60        self.assertEqual(exit_code, 0)
61
62
63class TestFirewalldSysVInit(infra.basetest.BRTest):
64    """Build the kernel as firewalld requires several nftable options."""
65
66    config = """
67        BR2_arm=y
68        BR2_cortex_a9=y
69        BR2_ARM_ENABLE_VFP=y
70        BR2_TOOLCHAIN_EXTERNAL=y
71        BR2_TOOLCHAIN_EXTERNAL_BOOTLIN=y
72        BR2_LINUX_KERNEL=y
73        BR2_LINUX_KERNEL_CUSTOM_VERSION=y
74        BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="6.1.61"
75        BR2_LINUX_KERNEL_DEFCONFIG="vexpress"
76        BR2_LINUX_KERNEL_DTS_SUPPORT=y
77        BR2_LINUX_KERNEL_INTREE_DTS_NAME="vexpress-v2p-ca9"
78        BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
79        BR2_PACKAGE_PYTHON3=y
80        BR2_PACKAGE_FIREWALLD=y
81        BR2_TARGET_ROOTFS_CPIO=y
82        # BR2_TARGET_ROOTFS_TAR is not set
83        """
84
85    def test_run(self):
86        cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
87        kernel_file = os.path.join(self.builddir, "images", "zImage")
88        dtb_file = os.path.join(self.builddir, "images", "vexpress-v2p-ca9.dtb")
89        self.emulator.boot(arch="armv7",
90                           kernel=kernel_file,
91                           kernel_cmdline=["console=ttyAMA0,115200"],
92                           options=[
93                               "-initrd", cpio_file,
94                               "-dtb", dtb_file,
95                               "-M", "vexpress-a9"
96                           ])
97        # It takes quite some time for the system to boot with firewalld.
98        self.emulator.login(timeout=120)
99        cmd = "firewall-cmd --state"
100        output, exit_code = self.emulator.run(cmd, timeout=10)
101        self.assertIn("running", output[0])
102        self.assertEqual(exit_code, 0)
103