]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test-coredump-util: add tests for parse_aux()
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 22 Mar 2023 12:42:40 +0000 (13:42 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 23 Mar 2023 17:06:46 +0000 (18:06 +0100)
The test files are /proc//auxv files copies from various architecutres
signified by the file name suffix.

Those tests are fairly simple, but when we run them on n architectures, we do
~n² cross-arch tests.

14 files changed:
src/test/test-coredump-util.c
test/auxv/.gitattributes [new file with mode: 0644]
test/auxv/bash.riscv64 [new file with mode: 0644]
test/auxv/cat.s390x [new file with mode: 0644]
test/auxv/dbus-broker-launch.aarch64 [new file with mode: 0644]
test/auxv/dbus-broker-launch.amd64 [new file with mode: 0644]
test/auxv/polkitd.aarch64 [new file with mode: 0644]
test/auxv/resolved.arm32 [new file with mode: 0644]
test/auxv/sleep.i686 [new file with mode: 0644]
test/auxv/sleep32.i686 [new file with mode: 0644]
test/auxv/sleep64.amd64 [new file with mode: 0644]
test/auxv/sudo.aarch64 [new file with mode: 0644]
test/auxv/sudo.amd64 [new file with mode: 0644]
test/meson.build

index 40b68df9f4c32ec2fbc49c617265c2752a022691..755cb49dae76a223beeedba78c06e391c23cf565 100644 (file)
@@ -1,7 +1,12 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
+#include <elf.h>
+
 #include "alloc-util.h"
 #include "coredump-util.h"
+#include "fileio.h"
+#include "fd-util.h"
+#include "format-util.h"
 #include "macro.h"
 #include "tests.h"
 
@@ -64,4 +69,62 @@ TEST(coredump_filter_mask_from_string) {
                                 1 << COREDUMP_FILTER_SHARED_DAX)));
 }
 
+static void test_parse_auxv_one(
+                uint8_t elf_class,
+                int dir_fd,
+                const char *filename,
+                int expect_at_secure,
+                uid_t expect_uid,
+                uid_t expect_euid,
+                gid_t expect_gid,
+                gid_t expect_egid) {
+
+        _cleanup_free_ char *data;
+        size_t data_size;
+        log_info("Parsing %s…", filename);
+        assert_se(read_full_file_at(dir_fd, filename, &data, &data_size) >= 0);
+
+        int at_secure;
+        uid_t uid, euid;
+        gid_t gid, egid;
+        assert_se(parse_auxv(LOG_ERR, elf_class, data, data_size,
+                             &at_secure, &uid, &euid, &gid, &egid) == 0);
+
+        log_info("at_secure=%d, uid="UID_FMT", euid="UID_FMT", gid="GID_FMT", egid="GID_FMT,
+                 at_secure, uid, euid, gid, egid);
+
+        assert_se(uid == expect_uid);
+        assert_se(euid == expect_euid);
+        assert_se(gid == expect_gid);
+        assert_se(egid == expect_egid);
+}
+
+TEST(test_parse_auxv) {
+        _cleanup_free_ char *dir = NULL;
+        _cleanup_close_ int dir_fd = -EBADF;
+
+        assert_se(get_testdata_dir("auxv", &dir) >= 0);
+        dir_fd = open(dir, O_RDONLY | O_CLOEXEC | O_DIRECTORY | O_PATH);
+        assert_se(dir_fd >= 0);
+
+        if (__BYTE_ORDER == __LITTLE_ENDIAN) {
+                test_parse_auxv_one(ELFCLASS32, dir_fd, "resolved.arm32", 0, 193, 193, 193, 193);
+                test_parse_auxv_one(ELFCLASS64, dir_fd, "bash.riscv64", 0, 1001, 1001, 1001, 1001);
+                test_parse_auxv_one(ELFCLASS32, dir_fd, "sleep.i686", 0, 1000, 1000, 1000, 1000);
+                /* after chgrp and chmod g+s */
+                test_parse_auxv_one(ELFCLASS32, dir_fd, "sleep32.i686", 1, 1000, 1000, 1000, 10);
+                test_parse_auxv_one(ELFCLASS64, dir_fd, "sleep64.amd64", 1, 1000, 1000, 1000, 10);
+
+                test_parse_auxv_one(ELFCLASS64, dir_fd, "sudo.aarch64", 1, 1494200408, 0, 1494200408, 1494200408);
+                test_parse_auxv_one(ELFCLASS64, dir_fd, "sudo.amd64", 1, 1000, 0, 1000, 1000);
+
+                /* Those run unprivileged, but start as root. */
+                test_parse_auxv_one(ELFCLASS64, dir_fd, "dbus-broker-launch.amd64", 0, 0, 0, 0, 0);
+                test_parse_auxv_one(ELFCLASS64, dir_fd, "dbus-broker-launch.aarch64", 0, 0, 0, 0, 0);
+                test_parse_auxv_one(ELFCLASS64, dir_fd, "polkitd.aarch64", 0, 0, 0, 0, 0);
+        } else {
+                test_parse_auxv_one(ELFCLASS64, dir_fd, "cat.s390x", 0, 3481, 3481, 3481, 3481);
+        }
+}
+
 DEFINE_TEST_MAIN(LOG_INFO);
diff --git a/test/auxv/.gitattributes b/test/auxv/.gitattributes
new file mode 100644 (file)
index 0000000..58e3ff4
--- /dev/null
@@ -0,0 +1,3 @@
+/*.* -whitespace
+/*.* binary
+/*.* generated
diff --git a/test/auxv/bash.riscv64 b/test/auxv/bash.riscv64
new file mode 100644 (file)
index 0000000..273a468
Binary files /dev/null and b/test/auxv/bash.riscv64 differ
diff --git a/test/auxv/cat.s390x b/test/auxv/cat.s390x
new file mode 100644 (file)
index 0000000..aa76441
Binary files /dev/null and b/test/auxv/cat.s390x differ
diff --git a/test/auxv/dbus-broker-launch.aarch64 b/test/auxv/dbus-broker-launch.aarch64
new file mode 100644 (file)
index 0000000..3a05e3c
Binary files /dev/null and b/test/auxv/dbus-broker-launch.aarch64 differ
diff --git a/test/auxv/dbus-broker-launch.amd64 b/test/auxv/dbus-broker-launch.amd64
new file mode 100644 (file)
index 0000000..21965e8
Binary files /dev/null and b/test/auxv/dbus-broker-launch.amd64 differ
diff --git a/test/auxv/polkitd.aarch64 b/test/auxv/polkitd.aarch64
new file mode 100644 (file)
index 0000000..ff1ea9f
Binary files /dev/null and b/test/auxv/polkitd.aarch64 differ
diff --git a/test/auxv/resolved.arm32 b/test/auxv/resolved.arm32
new file mode 100644 (file)
index 0000000..7d05f3b
Binary files /dev/null and b/test/auxv/resolved.arm32 differ
diff --git a/test/auxv/sleep.i686 b/test/auxv/sleep.i686
new file mode 100644 (file)
index 0000000..d0b5f2c
Binary files /dev/null and b/test/auxv/sleep.i686 differ
diff --git a/test/auxv/sleep32.i686 b/test/auxv/sleep32.i686
new file mode 100644 (file)
index 0000000..f52f512
Binary files /dev/null and b/test/auxv/sleep32.i686 differ
diff --git a/test/auxv/sleep64.amd64 b/test/auxv/sleep64.amd64
new file mode 100644 (file)
index 0000000..c3c7ed4
Binary files /dev/null and b/test/auxv/sleep64.amd64 differ
diff --git a/test/auxv/sudo.aarch64 b/test/auxv/sudo.aarch64
new file mode 100644 (file)
index 0000000..e49ce6a
Binary files /dev/null and b/test/auxv/sudo.aarch64 differ
diff --git a/test/auxv/sudo.amd64 b/test/auxv/sudo.amd64
new file mode 100644 (file)
index 0000000..91e4646
Binary files /dev/null and b/test/auxv/sudo.amd64 differ
index 1135ecd920e7de199325c158c9ef72a6df86686d..1721cffcd557aaeb3ad5337b5afec377d17f1074 100644 (file)
@@ -3,63 +3,32 @@
 if install_tests
         testdata_dir = testsdir + '/testdata/'
 
-        install_subdir('journal-data',
-                       exclude_files : '.gitattributes',
-                       install_dir : testdata_dir)
-        install_subdir('units',
-                       exclude_files : '.gitattributes',
-                       install_dir : testdata_dir)
-        install_subdir('test-execute',
-                       exclude_files : '.gitattributes',
-                       install_dir : testdata_dir)
-        install_subdir('test-fstab-generator',
-                       exclude_files : '.gitattributes',
-                       install_dir : testdata_dir)
-        install_subdir('test-path',
-                       exclude_files : '.gitattributes',
-                       install_dir : testdata_dir)
-        install_subdir('test-path-util',
-                       exclude_files : '.gitattributes',
-                       install_dir : testdata_dir)
-        install_subdir('test-umount',
-                       exclude_files : '.gitattributes',
-                       install_dir : testdata_dir)
-        install_subdir('test-network-generator-conversion',
-                       exclude_files : '.gitattributes',
-                       install_dir : testdata_dir)
-        install_subdir('testsuite-03.units',
-                       exclude_files : '.gitattributes',
-                       install_dir : testdata_dir)
-        install_subdir('testsuite-04.units',
-                       exclude_files : '.gitattributes',
-                       install_dir : testdata_dir)
-        install_subdir('testsuite-06.units',
-                       exclude_files : '.gitattributes',
-                       install_dir : testdata_dir)
-        install_subdir('testsuite-10.units',
-                       exclude_files : '.gitattributes',
-                       install_dir : testdata_dir)
-        install_subdir('testsuite-11.units',
-                       exclude_files : '.gitattributes',
-                       install_dir : testdata_dir)
-        install_subdir('testsuite-16.units',
-                       exclude_files : '.gitattributes',
-                       install_dir : testdata_dir)
-        install_subdir('testsuite-28.units',
-                       exclude_files : '.gitattributes',
-                       install_dir : testdata_dir)
-        install_subdir('testsuite-30.units',
-                       exclude_files : '.gitattributes',
-                       install_dir : testdata_dir)
-        install_subdir('testsuite-52.units',
-                       exclude_files : '.gitattributes',
-                       install_dir : testdata_dir)
-        install_subdir('testsuite-63.units',
-                       exclude_files : '.gitattributes',
-                       install_dir : testdata_dir)
-        install_subdir('testsuite-80.units',
-                       exclude_files : '.gitattributes',
-                       install_dir : testdata_dir)
+        foreach subdir : [
+                'auxv',
+                'journal-data',
+                'units',
+                'test-execute',
+                'test-fstab-generator',
+                'test-path',
+                'test-path-util',
+                'test-umount',
+                'test-network-generator-conversion',
+                'testsuite-03.units',
+                'testsuite-04.units',
+                'testsuite-06.units',
+                'testsuite-10.units',
+                'testsuite-11.units',
+                'testsuite-16.units',
+                'testsuite-28.units',
+                'testsuite-30.units',
+                'testsuite-52.units',
+                'testsuite-63.units',
+                'testsuite-80.units',
+        ]
+                install_subdir(subdir,
+                               exclude_files : '.gitattributes',
+                               install_dir : testdata_dir)
+        endforeach
 
         install_data(kbd_model_map,
                      install_dir : testdata_dir + '/test-keymap-util')