]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/test-catalog.c
Merge pull request #2495 from heftig/master
[thirdparty/systemd.git] / src / journal / test-catalog.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2012 Lennart Poettering
5 Copyright 2013 Zbigniew Jędrzejewski-Szmek
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <locale.h>
24 #include <unistd.h>
25
26 #include "sd-messages.h"
27
28 #include "alloc-util.h"
29 #include "catalog.h"
30 #include "fd-util.h"
31 #include "fileio.h"
32 #include "log.h"
33 #include "macro.h"
34 #include "string-util.h"
35 #include "util.h"
36
37 static const char *catalog_dirs[] = {
38 CATALOG_DIR,
39 NULL,
40 };
41
42 static const char *no_catalog_dirs[] = {
43 "/bin/hopefully/with/no/catalog",
44 NULL
45 };
46
47 static Hashmap * test_import(const char* contents, ssize_t size, int code) {
48 int r;
49 char name[] = "/tmp/test-catalog.XXXXXX";
50 _cleanup_close_ int fd;
51 Hashmap *h;
52
53 if (size < 0)
54 size = strlen(contents);
55
56 assert_se(h = hashmap_new(&catalog_hash_ops));
57
58 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
59 assert_se(fd >= 0);
60 assert_se(write(fd, contents, size) == size);
61
62 r = catalog_import_file(h, name);
63 assert_se(r == code);
64
65 unlink(name);
66
67 return h;
68 }
69
70 static void test_catalog_import_invalid(void) {
71 _cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
72
73 h = test_import("xxx", -1, -EINVAL);
74 assert_se(hashmap_isempty(h));
75 }
76
77 static void test_catalog_import_badid(void) {
78 _cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
79 const char *input =
80 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededede\n" \
81 "Subject: message\n" \
82 "\n" \
83 "payload\n";
84 h = test_import(input, -1, -EINVAL);
85 }
86
87 static void test_catalog_import_one(void) {
88 _cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
89 char *payload;
90 Iterator j;
91
92 const char *input =
93 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
94 "Subject: message\n" \
95 "\n" \
96 "payload\n";
97 const char *expect =
98 "Subject: message\n" \
99 "\n" \
100 "payload\n";
101
102 h = test_import(input, -1, 0);
103 assert_se(hashmap_size(h) == 1);
104
105 HASHMAP_FOREACH(payload, h, j) {
106 assert_se(streq(expect, payload));
107 }
108 }
109
110 static void test_catalog_import_merge(void) {
111 _cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
112 char *payload;
113 Iterator j;
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 "override payload\n";
127
128 const char *combined =
129 "Subject: override subject\n" \
130 "X-Header: hello\n" \
131 "Subject: message\n" \
132 "Defined-By: me\n" \
133 "\n" \
134 "override payload\n";
135
136 h = test_import(input, -1, 0);
137 assert_se(hashmap_size(h) == 1);
138
139 HASHMAP_FOREACH(payload, h, j) {
140 assert_se(streq(combined, payload));
141 }
142 }
143
144 static void test_catalog_import_merge_no_body(void) {
145 _cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
146 char *payload;
147 Iterator j;
148
149 const char *input =
150 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
151 "Subject: message\n" \
152 "Defined-By: me\n" \
153 "\n" \
154 "payload\n" \
155 "\n" \
156 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
157 "Subject: override subject\n" \
158 "X-Header: hello\n" \
159 "\n";
160
161 const char *combined =
162 "Subject: override subject\n" \
163 "X-Header: hello\n" \
164 "Subject: message\n" \
165 "Defined-By: me\n" \
166 "\n" \
167 "payload\n";
168
169 h = test_import(input, -1, 0);
170 assert_se(hashmap_size(h) == 1);
171
172 HASHMAP_FOREACH(payload, h, j) {
173 assert_se(streq(combined, payload));
174 }
175 }
176
177 static const char* database = NULL;
178
179 static void test_catalog_update(void) {
180 static char name[] = "/tmp/test-catalog.XXXXXX";
181 int r;
182
183 r = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
184 assert_se(r >= 0);
185
186 database = name;
187
188 /* Test what happens if there are no files. */
189 r = catalog_update(database, NULL, NULL);
190 assert_se(r >= 0);
191
192 /* Test what happens if there are no files in the directory. */
193 r = catalog_update(database, NULL, no_catalog_dirs);
194 assert_se(r >= 0);
195
196 /* Make sure that we at least have some files loaded or the
197 catalog_list below will fail. */
198 r = catalog_update(database, NULL, catalog_dirs);
199 assert_se(r >= 0);
200 }
201
202 static void test_catalog_file_lang(void) {
203 _cleanup_free_ char *lang = NULL, *lang2 = NULL, *lang3 = NULL, *lang4 = NULL;
204
205 assert_se(catalog_file_lang("systemd.de_DE.catalog", &lang) == 1);
206 assert_se(streq(lang, "de_DE"));
207
208 assert_se(catalog_file_lang("systemd..catalog", &lang2) == 0);
209 assert_se(lang2 == NULL);
210
211 assert_se(catalog_file_lang("systemd.fr.catalog", &lang2) == 1);
212 assert_se(streq(lang2, "fr"));
213
214 assert_se(catalog_file_lang("systemd.fr.catalog.gz", &lang3) == 0);
215 assert_se(lang3 == NULL);
216
217 assert_se(catalog_file_lang("systemd.01234567890123456789012345678901.catalog", &lang3) == 0);
218 assert_se(lang3 == NULL);
219
220 assert_se(catalog_file_lang("systemd.0123456789012345678901234567890.catalog", &lang3) == 1);
221 assert_se(streq(lang3, "0123456789012345678901234567890"));
222
223 assert_se(catalog_file_lang("/x/y/systemd.catalog", &lang4) == 0);
224 assert_se(lang4 == NULL);
225
226 assert_se(catalog_file_lang("/x/y/systemd.ru_RU.catalog", &lang4) == 1);
227 assert_se(streq(lang4, "ru_RU"));
228 }
229
230 int main(int argc, char *argv[]) {
231 _cleanup_free_ char *text = NULL;
232 int r;
233
234 setlocale(LC_ALL, "de_DE.UTF-8");
235
236 log_parse_environment();
237 log_open();
238
239 test_catalog_file_lang();
240
241 test_catalog_import_invalid();
242 test_catalog_import_badid();
243 test_catalog_import_one();
244 test_catalog_import_merge();
245 test_catalog_import_merge_no_body();
246
247 test_catalog_update();
248
249 r = catalog_list(stdout, database, true);
250 assert_se(r >= 0);
251
252 r = catalog_list(stdout, database, false);
253 assert_se(r >= 0);
254
255 assert_se(catalog_get(database, SD_MESSAGE_COREDUMP, &text) >= 0);
256 printf(">>>%s<<<\n", text);
257
258 if (database)
259 unlink(database);
260
261 return 0;
262 }