From e03513c773a4b58b87abbe906d9ca29b0c50ae5c Mon Sep 17 00:00:00 2001
From: Julien Olivain <ju.o@free.fr>
Date: Wed, 24 Jan 2024 23:19:50 +0100
Subject: [PATCH] support/testing: add a "file" package runtime test

Signed-off-by: Julien Olivain <ju.o@free.fr>
Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
---
 DEVELOPERS                                    |   2 +
 support/testing/tests/package/test_file.py    |  48 ++++++++++++++++++
 .../test_file/rootfs-overlay/root/code.c      |   9 ++++
 .../rootfs-overlay/root/plain-text.txt        |   1 +
 .../rootfs-overlay/root/plain-text.txt.gz     | Bin 0 -> 62 bytes
 .../rootfs-overlay/root/random-data.bin       | Bin 0 -> 512 bytes
 .../test_file/rootfs-overlay/root/script.py   |   3 ++
 .../test_file/rootfs-overlay/root/script.sh   |   3 ++
 .../root/symlink-to-plain-text.txt            |   1 +
 9 files changed, 67 insertions(+)
 create mode 100644 support/testing/tests/package/test_file.py
 create mode 100644 support/testing/tests/package/test_file/rootfs-overlay/root/code.c
 create mode 100644 support/testing/tests/package/test_file/rootfs-overlay/root/plain-text.txt
 create mode 100644 support/testing/tests/package/test_file/rootfs-overlay/root/plain-text.txt.gz
 create mode 100644 support/testing/tests/package/test_file/rootfs-overlay/root/random-data.bin
 create mode 100755 support/testing/tests/package/test_file/rootfs-overlay/root/script.py
 create mode 100755 support/testing/tests/package/test_file/rootfs-overlay/root/script.sh
 create mode 120000 support/testing/tests/package/test_file/rootfs-overlay/root/symlink-to-plain-text.txt

diff --git a/DEVELOPERS b/DEVELOPERS
index f7a10c364d..5467e465d6 100644
--- a/DEVELOPERS
+++ b/DEVELOPERS
@@ -1769,6 +1769,8 @@ F:	support/testing/tests/package/test_cryptsetup/
 F:	support/testing/tests/package/test_ddrescue.py
 F:	support/testing/tests/package/test_ddrescue/
 F:	support/testing/tests/package/test_dos2unix.py
+F:	support/testing/tests/package/test_file.py
+F:	support/testing/tests/package/test_file/
 F:	support/testing/tests/package/test_fluidsynth.py
 F:	support/testing/tests/package/test_fluidsynth/
 F:	support/testing/tests/package/test_fping.py
diff --git a/support/testing/tests/package/test_file.py b/support/testing/tests/package/test_file.py
new file mode 100644
index 0000000000..84eadc5ead
--- /dev/null
+++ b/support/testing/tests/package/test_file.py
@@ -0,0 +1,48 @@
+import os
+
+import infra.basetest
+
+
+class TestFile(infra.basetest.BRTest):
+    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
+        f"""
+        BR2_PACKAGE_FILE=y
+        BR2_ROOTFS_OVERLAY="{infra.filepath("tests/package/test_file/rootfs-overlay")}"
+        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()
+
+        self.assertRunOk("file --version")
+
+        tests = [
+            ("", "plain-text.txt", "ASCII text"),
+            ("-i", "plain-text.txt", "text/plain"),
+            ("", "plain-text.txt.gz", "gzip compressed data"),
+            ("-i", "plain-text.txt.gz", "application/gzip"),
+            ("-z", "plain-text.txt.gz", "ASCII text"),
+            ("", "random-data.bin", "data"),
+            ("-i", "random-data.bin", "application/octet-stream"),
+            ("", "code.c", "C source"),
+            ("-i", "code.c", "text/x-c"),
+            ("", "script.sh", "POSIX shell script"),
+            ("-i", "script.sh", "text/x-shellscript"),
+            ("", "script.py", "Python script"),
+            ("", "/usr/share/misc/magic.mgc", "magic binary file for file"),
+            ("", "/usr/bin/file", "ELF"),
+            ("", "/dev/zero", "character special"),
+            ("", "/", "directory"),
+            ("-h", "symlink-to-plain-text.txt", "symbolic link"),
+            ("-L", "symlink-to-plain-text.txt", "ASCII text")
+        ]
+        for opt_str, path, pattern in tests:
+            cmd = f"file {opt_str} '{path}'"
+            out, ret = self.emulator.run(cmd)
+            self.assertEqual(ret, 0, f"Failed to run '{cmd}'")
+            self.assertIn(pattern, "\n".join(out))
diff --git a/support/testing/tests/package/test_file/rootfs-overlay/root/code.c b/support/testing/tests/package/test_file/rootfs-overlay/root/code.c
new file mode 100644
index 0000000000..7095eaca31
--- /dev/null
+++ b/support/testing/tests/package/test_file/rootfs-overlay/root/code.c
@@ -0,0 +1,9 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+int
+main(void)
+{
+    printf("Hello Buildroot!\n");
+    exit(EXIT_SUCCESS);
+}
diff --git a/support/testing/tests/package/test_file/rootfs-overlay/root/plain-text.txt b/support/testing/tests/package/test_file/rootfs-overlay/root/plain-text.txt
new file mode 100644
index 0000000000..1c5aa36037
--- /dev/null
+++ b/support/testing/tests/package/test_file/rootfs-overlay/root/plain-text.txt
@@ -0,0 +1 @@
+This is a plain text file...
diff --git a/support/testing/tests/package/test_file/rootfs-overlay/root/plain-text.txt.gz b/support/testing/tests/package/test_file/rootfs-overlay/root/plain-text.txt.gz
new file mode 100644
index 0000000000000000000000000000000000000000..95d398f8d17819f6f54c19556a0ede85d87b5f01
GIT binary patch
literal 62
zcmb2|=HPJnzb2J|xgaMoGf%f9wW36?q@sj@`{W6oFos1gtxa=|oUv)0bfrtEb<3GK
SA-1-*dJOq`Zii(V7#INQ6&E=G

literal 0
HcmV?d00001

diff --git a/support/testing/tests/package/test_file/rootfs-overlay/root/random-data.bin b/support/testing/tests/package/test_file/rootfs-overlay/root/random-data.bin
new file mode 100644
index 0000000000000000000000000000000000000000..eea0ae8b76732c222dabd1f41a0a3078684e36d0
GIT binary patch
literal 512
zcmV+b0{{KpR9Y!f%LlZBWnY!TWRP(B%0`7N4ZcPM4l~SX-q*er9xaj-b$K+42>O~J
z%NEm6i_0Ew>}uF2WS*4xAKu2%4)XBLR|M}bEMS<7MP>eGR^%&3@2^?lRzn`}bm9>x
z_(e`gX$&$Amx{84&euTLLPyWpOIKgSFopSYA*{q?tH+i@WrnTBuMGp4oDGzd&xO?t
zVKBxuXzI9nC|ff|lst`s;;(PFc;9#0A0`gH<k*m9Z4Ijy9@(?$=P5k34WU{LaR3kq
zGoG+n7iAQoFtA@-Z;~RL5_j`@uvlF+>5qCVbmkz4v<&z5?bXko!I`##33%!zmHZz$
z=A(e+c>Ad4Kg7T2(0auWV?t6SqMNe2JdfzWx59a*Lxh+|u>7CW4P6Uzm}!=U1AOYW
zL}{X=-Qx>X$gdtUGKtTzb3?CS7VLAH%|?9UHj7z5rMY%Bz^^nBmDw450px1I6J9E)
z%jY+!DHbvn7(NdB#-7;Z%k<&7*hIx!W-c}k3Dj`IK&z(B2yC@37KTprOVwG7sDbhv
zMegzVjj^r&OHlsGp2;ekV?C}u;KdKhVBWMZx-rUhAIesO`nXO4t3?W`yx$YhLn|RG
z##8L(0ryrm(hz5cwm#~3?Cv5c*-t&=ZT7@aHVmS{LPMK(ylJtVZ7W;M`yqg=n}4>Q
CEdYW5

literal 0
HcmV?d00001

diff --git a/support/testing/tests/package/test_file/rootfs-overlay/root/script.py b/support/testing/tests/package/test_file/rootfs-overlay/root/script.py
new file mode 100755
index 0000000000..79191b3048
--- /dev/null
+++ b/support/testing/tests/package/test_file/rootfs-overlay/root/script.py
@@ -0,0 +1,3 @@
+#! /usr/bin/env python3
+
+print("Hello Buildroot!")
diff --git a/support/testing/tests/package/test_file/rootfs-overlay/root/script.sh b/support/testing/tests/package/test_file/rootfs-overlay/root/script.sh
new file mode 100755
index 0000000000..75b2d5e017
--- /dev/null
+++ b/support/testing/tests/package/test_file/rootfs-overlay/root/script.sh
@@ -0,0 +1,3 @@
+#! /bin/sh
+
+echo "Hello Buildroot!"
diff --git a/support/testing/tests/package/test_file/rootfs-overlay/root/symlink-to-plain-text.txt b/support/testing/tests/package/test_file/rootfs-overlay/root/symlink-to-plain-text.txt
new file mode 120000
index 0000000000..a3ae3c0058
--- /dev/null
+++ b/support/testing/tests/package/test_file/rootfs-overlay/root/symlink-to-plain-text.txt
@@ -0,0 +1 @@
+plain-text.txt
\ No newline at end of file