]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-coredump-util.c
test: Remove unnecessary test prefix
[thirdparty/systemd.git] / src / test / test-coredump-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
b34612bd 2
2f809d29
ZJS
3#include <elf.h>
4
b34612bd
ZJS
5#include "alloc-util.h"
6#include "coredump-util.h"
2f809d29
ZJS
7#include "fileio.h"
8#include "fd-util.h"
9#include "format-util.h"
b34612bd
ZJS
10#include "macro.h"
11#include "tests.h"
12
4f7452a8 13TEST(coredump_filter_to_from_string) {
b34612bd
ZJS
14 for (CoredumpFilter i = 0; i < _COREDUMP_FILTER_MAX; i++) {
15 const char *n;
16
17 assert_se(n = coredump_filter_to_string(i));
5570a097 18 log_info("0x%x\t%s", 1u << i, n);
b34612bd
ZJS
19 assert_se(coredump_filter_from_string(n) == i);
20
21 uint64_t f;
22 assert_se(coredump_filter_mask_from_string(n, &f) == 0);
23 assert_se(f == 1u << i);
24 }
25}
26
4f7452a8 27TEST(coredump_filter_mask_from_string) {
b34612bd
ZJS
28 uint64_t f;
29 assert_se(coredump_filter_mask_from_string("default", &f) == 0);
30 assert_se(f == COREDUMP_FILTER_MASK_DEFAULT);
7f3bb8f2
LB
31 assert_se(coredump_filter_mask_from_string("all", &f) == 0);
32 assert_se(f == COREDUMP_FILTER_MASK_ALL);
b34612bd
ZJS
33
34 assert_se(coredump_filter_mask_from_string(" default\tdefault\tdefault ", &f) == 0);
35 assert_se(f == COREDUMP_FILTER_MASK_DEFAULT);
36
37 assert_se(coredump_filter_mask_from_string("defaulta", &f) < 0);
38 assert_se(coredump_filter_mask_from_string("default defaulta default", &f) < 0);
39 assert_se(coredump_filter_mask_from_string("default default defaulta", &f) < 0);
40
41 assert_se(coredump_filter_mask_from_string("private-anonymous default", &f) == 0);
42 assert_se(f == COREDUMP_FILTER_MASK_DEFAULT);
43
44 assert_se(coredump_filter_mask_from_string("shared-file-backed shared-dax", &f) == 0);
45 assert_se(f == (1 << COREDUMP_FILTER_SHARED_FILE_BACKED |
46 1 << COREDUMP_FILTER_SHARED_DAX));
47
48 assert_se(coredump_filter_mask_from_string("private-file-backed private-dax 0xF", &f) == 0);
49 assert_se(f == (1 << COREDUMP_FILTER_PRIVATE_FILE_BACKED |
50 1 << COREDUMP_FILTER_PRIVATE_DAX |
51 0xF));
52
53 assert_se(coredump_filter_mask_from_string("11", &f) == 0);
54 assert_se(f == 0x11);
55
56 assert_se(coredump_filter_mask_from_string("0x1101", &f) == 0);
57 assert_se(f == 0x1101);
58
59 assert_se(coredump_filter_mask_from_string("0", &f) == 0);
60 assert_se(f == 0);
61
62 assert_se(coredump_filter_mask_from_string("all", &f) == 0);
63 assert_se(FLAGS_SET(f, (1 << COREDUMP_FILTER_PRIVATE_ANONYMOUS |
64 1 << COREDUMP_FILTER_SHARED_ANONYMOUS |
65 1 << COREDUMP_FILTER_PRIVATE_FILE_BACKED |
66 1 << COREDUMP_FILTER_SHARED_FILE_BACKED |
67 1 << COREDUMP_FILTER_ELF_HEADERS |
68 1 << COREDUMP_FILTER_PRIVATE_HUGE |
69 1 << COREDUMP_FILTER_SHARED_HUGE |
70 1 << COREDUMP_FILTER_PRIVATE_DAX |
71 1 << COREDUMP_FILTER_SHARED_DAX)));
72}
73
f39546a8 74static void test_parse_auxv_two(
2f809d29 75 uint8_t elf_class,
f39546a8
ZJS
76 size_t offset,
77 const char *data,
78 size_t data_size,
2f809d29
ZJS
79 int expect_at_secure,
80 uid_t expect_uid,
81 uid_t expect_euid,
82 gid_t expect_gid,
83 gid_t expect_egid) {
84
2f809d29
ZJS
85 int at_secure;
86 uid_t uid, euid;
87 gid_t gid, egid;
88 assert_se(parse_auxv(LOG_ERR, elf_class, data, data_size,
89 &at_secure, &uid, &euid, &gid, &egid) == 0);
90
f39546a8
ZJS
91 log_debug("[offset=%zu] at_secure=%d, uid="UID_FMT", euid="UID_FMT", gid="GID_FMT", egid="GID_FMT,
92 offset,
93 at_secure, uid, euid, gid, egid);
2f809d29
ZJS
94
95 assert_se(uid == expect_uid);
96 assert_se(euid == expect_euid);
97 assert_se(gid == expect_gid);
98 assert_se(egid == expect_egid);
99}
100
f39546a8
ZJS
101static void test_parse_auxv_one(
102 uint8_t elf_class,
103 int dir_fd,
104 const char *filename,
105 int expect_at_secure,
106 uid_t expect_uid,
107 uid_t expect_euid,
108 gid_t expect_gid,
109 gid_t expect_egid) {
110
111 _cleanup_free_ char *buf;
112 const char *data;
113 size_t data_size;
114 log_info("Parsing %s…", filename);
115 assert_se(read_full_file_at(dir_fd, filename, &buf, &data_size) >= 0);
116
117 for (size_t offset = 0; offset < 8; offset++) {
118 _cleanup_free_ char *buf2 = NULL;
119
120 if (offset == 0)
121 data = buf;
122 else {
123 assert_se(buf2 = malloc(offset + data_size));
124 memcpy(buf2 + offset, buf, data_size);
125 data = buf2 + offset;
126 }
127
128 test_parse_auxv_two(elf_class, offset, data, data_size,
129 expect_at_secure, expect_uid, expect_euid, expect_gid, expect_egid);
130 }
131}
132
ec9d3fc5 133TEST(parse_auxv) {
2f809d29
ZJS
134 _cleanup_free_ char *dir = NULL;
135 _cleanup_close_ int dir_fd = -EBADF;
136
137 assert_se(get_testdata_dir("auxv", &dir) >= 0);
138 dir_fd = open(dir, O_RDONLY | O_CLOEXEC | O_DIRECTORY | O_PATH);
139 assert_se(dir_fd >= 0);
140
141 if (__BYTE_ORDER == __LITTLE_ENDIAN) {
142 test_parse_auxv_one(ELFCLASS32, dir_fd, "resolved.arm32", 0, 193, 193, 193, 193);
143 test_parse_auxv_one(ELFCLASS64, dir_fd, "bash.riscv64", 0, 1001, 1001, 1001, 1001);
144 test_parse_auxv_one(ELFCLASS32, dir_fd, "sleep.i686", 0, 1000, 1000, 1000, 1000);
145 /* after chgrp and chmod g+s */
146 test_parse_auxv_one(ELFCLASS32, dir_fd, "sleep32.i686", 1, 1000, 1000, 1000, 10);
147 test_parse_auxv_one(ELFCLASS64, dir_fd, "sleep64.amd64", 1, 1000, 1000, 1000, 10);
148
149 test_parse_auxv_one(ELFCLASS64, dir_fd, "sudo.aarch64", 1, 1494200408, 0, 1494200408, 1494200408);
150 test_parse_auxv_one(ELFCLASS64, dir_fd, "sudo.amd64", 1, 1000, 0, 1000, 1000);
151
152 /* Those run unprivileged, but start as root. */
153 test_parse_auxv_one(ELFCLASS64, dir_fd, "dbus-broker-launch.amd64", 0, 0, 0, 0, 0);
154 test_parse_auxv_one(ELFCLASS64, dir_fd, "dbus-broker-launch.aarch64", 0, 0, 0, 0, 0);
155 test_parse_auxv_one(ELFCLASS64, dir_fd, "polkitd.aarch64", 0, 0, 0, 0, 0);
156 } else {
157 test_parse_auxv_one(ELFCLASS64, dir_fd, "cat.s390x", 0, 3481, 3481, 3481, 3481);
158 }
159}
160
4f7452a8 161DEFINE_TEST_MAIN(LOG_INFO);