Debian Codegraph @

src/libsystemd/sd-journal/test-catalog.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <locale.h>
4 #include <unistd.h>
5
6 #include "sd-messages.h"
7
8 #include "alloc-util.h"
9 #include "catalog.h"
10 #include "fd-util.h"
11 #include "hashmap.h"
12 #include "log.h"
13 #include "tests.h"
14 #include "tmpfile-util.h"
15
16 static char** catalog_dirs = NULL;
17 static const char *no_catalog_dirs[] = {
18 "/bin/hopefully/with/no/catalog",
19 NULL
20 };
21
22 static OrderedHashmap* test_import(const char* contents, ssize_t size, int code) {
23 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-catalog.XXXXXX";
24 _cleanup_close_ int fd = -EBADF;
25 OrderedHashmap *h = NULL;
26
27 if (size < 0)
28 size = strlen(contents);
29
30 ASSERT_OK(fd = mkostemp_safe(name));
31 ASSERT_EQ(write(fd, contents, size), size);
32
33 ASSERT_EQ(catalog_import_file(&h, fd, name), code);
34
35 return h;
36 }
37
38 static void test_catalog_import_invalid(void) {
39 _cleanup_ordered_hashmap_free_ OrderedHashmap *h = NULL;
40
41 h = test_import("xxx", -1, -EINVAL);
42 ASSERT_TRUE(ordered_hashmap_isempty(h));
43 }
44
45 static void test_catalog_import_badid(void) {
46 _unused_ _cleanup_ordered_hashmap_free_ OrderedHashmap *h = NULL;
47 const char *input =
48 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededede\n" \
49 "Subject: message\n" \
50 "\n" \
51 "payload\n";
52 h = test_import(input, -1, -EINVAL);
53 }
54
55 static void test_catalog_import_one(void) {
56 _cleanup_ordered_hashmap_free_ OrderedHashmap *h = NULL;
57 char *payload;
58
59 const char *input =
60 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
61 "Subject: message\n" \
62 "\n" \
63 "payload\n";
64 const char *expect =
65 "Subject: message\n" \
66 "\n" \
67 "payload\n";
68
69 h = test_import(input, -1, 0);
70 ASSERT_EQ(ordered_hashmap_size(h), 1u);
71
72 ORDERED_HASHMAP_FOREACH(payload, h) {
73 printf("expect: %s\n", expect);
74 printf("actual: %s\n", payload);
75 ASSERT_STREQ(expect, payload);
76 }
77 }
78
79 static void test_catalog_import_merge(void) {
80 _cleanup_ordered_hashmap_free_ OrderedHashmap *h = NULL;
81 char *payload;
82
83 const char *input =
84 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
85 "Subject: message\n" \
86 "Defined-By: me\n" \
87 "\n" \
88 "payload\n" \
89 "\n" \
90 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
91 "Subject: override subject\n" \
92 "X-Header: hello\n" \
93 "\n" \
94 "override payload\n";
95
96 const char *combined =
97 "Subject: override subject\n" \
98 "X-Header: hello\n" \
99 "Subject: message\n" \
100 "Defined-By: me\n" \
101 "\n" \
102 "override payload\n";
103
104 h = test_import(input, -1, 0);
105 ASSERT_EQ(ordered_hashmap_size(h), 1u);
106
107 ORDERED_HASHMAP_FOREACH(payload, h)
108 ASSERT_STREQ(combined, payload);
109 }
110
111 static void test_catalog_import_merge_no_body(void) {
112 _cleanup_ordered_hashmap_free_ OrderedHashmap *h = NULL;
113 char *payload;
114
115 const char *input =
116 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
117 "Subject: message\n" \
118 "Defined-By: me\n" \
119 "\n" \
120 "payload\n" \
121 "\n" \
122 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
123 "Subject: override subject\n" \
124 "X-Header: hello\n" \
125 "\n";
126
127 const char *combined =
128 "Subject: override subject\n" \
129 "X-Header: hello\n" \
130 "Subject: message\n" \
131 "Defined-By: me\n" \
132 "\n" \
133 "payload\n";
134
135 h = test_import(input, -1, 0);
136 ASSERT_EQ(ordered_hashmap_size(h), 1u);
137
138 ORDERED_HASHMAP_FOREACH(payload, h)
139 ASSERT_STREQ(combined, payload);
140 }
141
142 static void test_catalog_update(const char *database) {
143 /* Test what happens if there are no files. */
144 ASSERT_OK_ZERO(catalog_update(database, NULL, NULL));
145
146 /* Test what happens if there are no files in the directory. */
147 ASSERT_OK_ZERO(catalog_update(database, NULL, no_catalog_dirs));
148
149 /* Make sure that we at least have some files loaded or the
150 * catalog_list below will fail. */
151 ASSERT_OK_ZERO(catalog_update(database, NULL, (const char * const *) catalog_dirs));
152 }
153
154 static void test_catalog_file_lang(void) {
155 _cleanup_free_ char *lang = NULL, *lang2 = NULL, *lang3 = NULL, *lang4 = NULL;
156
157 ASSERT_EQ(catalog_file_lang("systemd.de_DE.catalog", &lang), 1);
158 ASSERT_STREQ(lang, "de_DE");
159
160 ASSERT_OK_ZERO(catalog_file_lang("systemd..catalog", &lang2));
161 ASSERT_NULL(lang2);
162
163 ASSERT_EQ(catalog_file_lang("systemd.fr.catalog", &lang2), 1);
164 ASSERT_STREQ(lang2, "fr");
165
166 ASSERT_OK_ZERO(catalog_file_lang("systemd.fr.catalog.gz", &lang3));
167 ASSERT_NULL(lang3);
168
169 ASSERT_OK_ZERO(catalog_file_lang("systemd.01234567890123456789012345678901.catalog", &lang3));
170 ASSERT_NULL(lang3);
171
172 ASSERT_EQ(catalog_file_lang("systemd.0123456789012345678901234567890.catalog", &lang3), 1);
173 ASSERT_STREQ(lang3, "0123456789012345678901234567890");
174
175 ASSERT_OK_ZERO(catalog_file_lang("/x/y/systemd.catalog", &lang4));
176 ASSERT_NULL(lang4);
177
178 ASSERT_EQ(catalog_file_lang("/x/y/systemd.ru_RU.catalog", &lang4), 1);
179 ASSERT_STREQ(lang4, "ru_RU");
180 }
181
182 int main(int argc, char *argv[]) {
183 _cleanup_(unlink_tempfilep) char database[] = "/tmp/test-catalog.XXXXXX";
184 _cleanup_close_ int fd = -EBADF;
185 _cleanup_free_ char *text = NULL;
186
187 setlocale(LC_ALL, "de_DE.UTF-8");
188
189 test_setup_logging(LOG_DEBUG);
190
191 /* If test-catalog is located at the build directory, then use catalogs in that.
192 * If it is not, e.g. installed by systemd-tests package, then use installed catalogs. */
193 catalog_dirs = STRV_MAKE(get_catalog_dir());
194
195 ASSERT_OK_ERRNO(access(catalog_dirs[0], F_OK));
196 log_notice("Using catalog directory '%s'", catalog_dirs[0]);
197
198 test_catalog_file_lang();
199
200 test_catalog_import_invalid();
201 test_catalog_import_badid();
202 test_catalog_import_one();
203 test_catalog_import_merge();
204 test_catalog_import_merge_no_body();
205
206 ASSERT_OK(fd = mkostemp_safe(database));
207
208 test_catalog_update(database);
209
210 ASSERT_OK(catalog_list(NULL, database, true));
211
212 ASSERT_OK(catalog_list(NULL, database, false));
213
214 ASSERT_OK(catalog_get(database, SD_MESSAGE_COREDUMP, &text));
215 printf(">>>%s<<<\n", text);
216
217 return 0;
218 }