diff mbox series

[1/1] support/testing: add nmap runtime test

Message ID 20240721205731.266149-1-ju.o@free.fr
State Accepted
Headers show
Series [1/1] support/testing: add nmap runtime test | expand

Commit Message

Julien Olivain July 21, 2024, 8:57 p.m. UTC
Signed-off-by: Julien Olivain <ju.o@free.fr>
---
 DEVELOPERS                                 |  1 +
 support/testing/tests/package/test_nmap.py | 42 ++++++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100644 support/testing/tests/package/test_nmap.py

Comments

Arnout Vandecappelle July 22, 2024, 8:03 a.m. UTC | #1
On 21/07/2024 22:57, Julien Olivain wrote:
> Signed-off-by: Julien Olivain <ju.o@free.fr>

  Applied to master, thanks.

  Regards,
  Arnout

> ---
>   DEVELOPERS                                 |  1 +
>   support/testing/tests/package/test_nmap.py | 42 ++++++++++++++++++++++
>   2 files changed, 43 insertions(+)
>   create mode 100644 support/testing/tests/package/test_nmap.py
> 
> diff --git a/DEVELOPERS b/DEVELOPERS
> index 333e26d74e..60f6b740c7 100644
> --- a/DEVELOPERS
> +++ b/DEVELOPERS
> @@ -1918,6 +1918,7 @@ F:	support/testing/tests/package/test_netsnmp/
>   F:	support/testing/tests/package/test_nftables.py
>   F:	support/testing/tests/package/test_nftables/
>   F:	support/testing/tests/package/test_ngrep.py
> +F:	support/testing/tests/package/test_nmap.py
>   F:	support/testing/tests/package/test_ntp.py
>   F:	support/testing/tests/package/test_ntp/
>   F:	support/testing/tests/package/test_numactl.py
> diff --git a/support/testing/tests/package/test_nmap.py b/support/testing/tests/package/test_nmap.py
> new file mode 100644
> index 0000000000..92b085ece9
> --- /dev/null
> +++ b/support/testing/tests/package/test_nmap.py
> @@ -0,0 +1,42 @@
> +import os
> +import time
> +
> +import infra.basetest
> +
> +
> +class TestNmap(infra.basetest.BRTest):
> +    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
> +        """
> +        BR2_PACKAGE_BUSYBOX_SHOW_OTHERS=y
> +        BR2_PACKAGE_NMAP=y
> +        BR2_PACKAGE_NMAP_NCAT=y
> +        BR2_PACKAGE_NMAP_NMAP=y
> +        BR2_TARGET_ROOTFS_CPIO=y
> +        # BR2_TARGET_ROOTFS_TAR is not set
> +        """
> +
> +    def test_run(self):
> +        cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
> +        self.emulator.boot(arch="armv5",
> +                           kernel="builtin",
> +                           options=["-initrd", cpio_file])
> +        self.emulator.login()
> +
> +        # Check the program can execute.
> +        self.assertRunOk("nmap --version")
> +
> +        # We open few ports, using the nmap netcat "nc" commands.
> +        ports = [21, 23, 25, 80]
> +        for port in ports:
> +            cmd = f"nc -l -p {port} </dev/null &>/dev/null &"
> +            self.assertRunOk(cmd)
> +
> +        time.sleep(1 * self.timeout_multiplier)
> +
> +        # We run a local port scan. We should see in the output the
> +        # ports we previously opened.
> +        out, ret = self.emulator.run("nmap -n -sS 127.0.0.1", timeout=20)
> +        self.assertEqual(ret, 0)
> +        nmap_out = "\n".join(out)
> +        for port in ports:
> +            self.assertRegex(nmap_out, f"{port}/tcp *open")
Peter Korsgaard Aug. 30, 2024, 12:21 p.m. UTC | #2
>>>>> "Julien" == Julien Olivain <ju.o@free.fr> writes:

 > Signed-off-by: Julien Olivain <ju.o@free.fr>

Committed to 2024.02.x and 2024.05.x, thanks.
diff mbox series

Patch

diff --git a/DEVELOPERS b/DEVELOPERS
index 333e26d74e..60f6b740c7 100644
--- a/DEVELOPERS
+++ b/DEVELOPERS
@@ -1918,6 +1918,7 @@  F:	support/testing/tests/package/test_netsnmp/
 F:	support/testing/tests/package/test_nftables.py
 F:	support/testing/tests/package/test_nftables/
 F:	support/testing/tests/package/test_ngrep.py
+F:	support/testing/tests/package/test_nmap.py
 F:	support/testing/tests/package/test_ntp.py
 F:	support/testing/tests/package/test_ntp/
 F:	support/testing/tests/package/test_numactl.py
diff --git a/support/testing/tests/package/test_nmap.py b/support/testing/tests/package/test_nmap.py
new file mode 100644
index 0000000000..92b085ece9
--- /dev/null
+++ b/support/testing/tests/package/test_nmap.py
@@ -0,0 +1,42 @@ 
+import os
+import time
+
+import infra.basetest
+
+
+class TestNmap(infra.basetest.BRTest):
+    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
+        """
+        BR2_PACKAGE_BUSYBOX_SHOW_OTHERS=y
+        BR2_PACKAGE_NMAP=y
+        BR2_PACKAGE_NMAP_NCAT=y
+        BR2_PACKAGE_NMAP_NMAP=y
+        BR2_TARGET_ROOTFS_CPIO=y
+        # BR2_TARGET_ROOTFS_TAR is not set
+        """
+
+    def test_run(self):
+        cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
+        self.emulator.boot(arch="armv5",
+                           kernel="builtin",
+                           options=["-initrd", cpio_file])
+        self.emulator.login()
+
+        # Check the program can execute.
+        self.assertRunOk("nmap --version")
+
+        # We open few ports, using the nmap netcat "nc" commands.
+        ports = [21, 23, 25, 80]
+        for port in ports:
+            cmd = f"nc -l -p {port} </dev/null &>/dev/null &"
+            self.assertRunOk(cmd)
+
+        time.sleep(1 * self.timeout_multiplier)
+
+        # We run a local port scan. We should see in the output the
+        # ports we previously opened.
+        out, ret = self.emulator.run("nmap -n -sS 127.0.0.1", timeout=20)
+        self.assertEqual(ret, 0)
+        nmap_out = "\n".join(out)
+        for port in ports:
+            self.assertRegex(nmap_out, f"{port}/tcp *open")