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