]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/test-catalog.c
journal: introduce JournalStorage and JournalStorageSpace structures
[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);
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 printf("expect: %s\n", expect);
107 printf("actual: %s\n", payload);
108 assert_se(streq(expect, payload));
109 }
110 }
111
112 static void test_catalog_import_merge(void) {
113 _cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
114 char *payload;
115 Iterator j;
116
117 const char *input =
118 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
119 "Subject: message\n" \
120 "Defined-By: me\n" \
121 "\n" \
122 "payload\n" \
123 "\n" \
124 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
125 "Subject: override subject\n" \
126 "X-Header: hello\n" \
127 "\n" \
128 "override payload\n";
129
130 const char *combined =
131 "Subject: override subject\n" \
132 "X-Header: hello\n" \
133 "Subject: message\n" \
134 "Defined-By: me\n" \
135 "\n" \
136 "override payload\n";
137
138 h = test_import(input, -1, 0);
139 assert_se(hashmap_size(h) == 1);
140
141 HASHMAP_FOREACH(payload, h, j) {
142 assert_se(streq(combined, payload));
143 }
144 }
145
146 static void test_catalog_import_merge_no_body(void) {
147 _cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
148 char *payload;
149 Iterator j;
150
151 const char *input =
152 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
153 "Subject: message\n" \
154 "Defined-By: me\n" \
155 "\n" \
156 "payload\n" \
157 "\n" \
158 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
159 "Subject: override subject\n" \
160 "X-Header: hello\n" \
161 "\n";
162
163 const char *combined =
164 "Subject: override subject\n" \
165 "X-Header: hello\n" \
166 "Subject: message\n" \
167 "Defined-By: me\n" \
168 "\n" \
169 "payload\n";
170
171 h = test_import(input, -1, 0);
172 assert_se(hashmap_size(h) == 1);
173
174 HASHMAP_FOREACH(payload, h, j) {
175 assert_se(streq(combined, payload));
176 }
177 }
178
179 static const char* database = NULL;
180
181 static void test_catalog_update(void) {
182 static char name[] = "/tmp/test-catalog.XXXXXX";
183 int r;
184
185 r = mkostemp_safe(name);
186 assert_se(r >= 0);
187
188 database = name;
189
190 /* Test what happens if there are no files. */
191 r = catalog_update(database, NULL, NULL);
192 assert_se(r >= 0);
193
194 /* Test what happens if there are no files in the directory. */
195 r = catalog_update(database, NULL, no_catalog_dirs);
196 assert_se(r >= 0);
197
198 /* Make sure that we at least have some files loaded or the
199 catalog_list below will fail. */
200 r = catalog_update(database, NULL, catalog_dirs);
201 assert_se(r >= 0);
202 }
203
204 static void test_catalog_file_lang(void) {
205 _cleanup_free_ char *lang = NULL, *lang2 = NULL, *lang3 = NULL, *lang4 = NULL;
206
207 assert_se(catalog_file_lang("systemd.de_DE.catalog", &lang) == 1);
208 assert_se(streq(lang, "de_DE"));
209
210 assert_se(catalog_file_lang("systemd..catalog", &lang2) == 0);
211 assert_se(lang2 == NULL);
212
213 assert_se(catalog_file_lang("systemd.fr.catalog", &lang2) == 1);
214 assert_se(streq(lang2, "fr"));
215
216 assert_se(catalog_file_lang("systemd.fr.catalog.gz", &lang3) == 0);
217 assert_se(lang3 == NULL);
218
219 assert_se(catalog_file_lang("systemd.01234567890123456789012345678901.catalog", &lang3) == 0);
220 assert_se(lang3 == NULL);
221
222 assert_se(catalog_file_lang("systemd.0123456789012345678901234567890.catalog", &lang3) == 1);
223 assert_se(streq(lang3, "0123456789012345678901234567890"));
224
225 assert_se(catalog_file_lang("/x/y/systemd.catalog", &lang4) == 0);
226 assert_se(lang4 == NULL);
227
228 assert_se(catalog_file_lang("/x/y/systemd.ru_RU.catalog", &lang4) == 1);
229 assert_se(streq(lang4, "ru_RU"));
230 }
231
232 int main(int argc, char *argv[]) {
233 _cleanup_free_ char *text = NULL;
234 int r;
235
236 setlocale(LC_ALL, "de_DE.UTF-8");
237
238 log_parse_environment();
239 log_open();
240
241 test_catalog_file_lang();
242
243 test_catalog_import_invalid();
244 test_catalog_import_badid();
245 test_catalog_import_one();
246 test_catalog_import_merge();
247 test_catalog_import_merge_no_body();
248
249 test_catalog_update();
250
251 r = catalog_list(stdout, database, true);
252 assert_se(r >= 0);
253
254 r = catalog_list(stdout, database, false);
255 assert_se(r >= 0);
256
257 assert_se(catalog_get(database, SD_MESSAGE_COREDUMP, &text) >= 0);
258 printf(">>>%s<<<\n", text);
259
260 if (database)
261 unlink(database);
262
263 return 0;
264 }