1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2012 Lennart Poettering
6 Copyright 2013 Zbigniew Jędrzejewski-Szmek
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include "sd-messages.h"
29 #include "alloc-util.h"
35 #include "string-util.h"
38 static const char *catalog_dirs
[] = {
43 static const char *no_catalog_dirs
[] = {
44 "/bin/hopefully/with/no/catalog",
48 static Hashmap
* test_import(const char* contents
, ssize_t size
, int code
) {
50 char name
[] = "/tmp/test-catalog.XXXXXX";
51 _cleanup_close_
int fd
;
55 size
= strlen(contents
);
57 assert_se(h
= hashmap_new(&catalog_hash_ops
));
59 fd
= mkostemp_safe(name
);
61 assert_se(write(fd
, contents
, size
) == size
);
63 r
= catalog_import_file(h
, name
);
71 static void test_catalog_import_invalid(void) {
72 _cleanup_hashmap_free_free_free_ Hashmap
*h
= NULL
;
74 h
= test_import("xxx", -1, -EINVAL
);
75 assert_se(hashmap_isempty(h
));
78 static void test_catalog_import_badid(void) {
79 _cleanup_hashmap_free_free_free_ Hashmap
*h
= NULL
;
81 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededede\n" \
82 "Subject: message\n" \
85 h
= test_import(input
, -1, -EINVAL
);
88 static void test_catalog_import_one(void) {
89 _cleanup_hashmap_free_free_free_ Hashmap
*h
= NULL
;
94 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
95 "Subject: message\n" \
99 "Subject: message\n" \
103 h
= test_import(input
, -1, 0);
104 assert_se(hashmap_size(h
) == 1);
106 HASHMAP_FOREACH(payload
, h
, j
) {
107 printf("expect: %s\n", expect
);
108 printf("actual: %s\n", payload
);
109 assert_se(streq(expect
, payload
));
113 static void test_catalog_import_merge(void) {
114 _cleanup_hashmap_free_free_free_ Hashmap
*h
= NULL
;
119 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
120 "Subject: message\n" \
125 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
126 "Subject: override subject\n" \
127 "X-Header: hello\n" \
129 "override payload\n";
131 const char *combined
=
132 "Subject: override subject\n" \
133 "X-Header: hello\n" \
134 "Subject: message\n" \
137 "override payload\n";
139 h
= test_import(input
, -1, 0);
140 assert_se(hashmap_size(h
) == 1);
142 HASHMAP_FOREACH(payload
, h
, j
) {
143 assert_se(streq(combined
, payload
));
147 static void test_catalog_import_merge_no_body(void) {
148 _cleanup_hashmap_free_free_free_ Hashmap
*h
= NULL
;
153 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
154 "Subject: message\n" \
159 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
160 "Subject: override subject\n" \
161 "X-Header: hello\n" \
164 const char *combined
=
165 "Subject: override subject\n" \
166 "X-Header: hello\n" \
167 "Subject: message\n" \
172 h
= test_import(input
, -1, 0);
173 assert_se(hashmap_size(h
) == 1);
175 HASHMAP_FOREACH(payload
, h
, j
) {
176 assert_se(streq(combined
, payload
));
180 static const char* database
= NULL
;
182 static void test_catalog_update(void) {
183 static char name
[] = "/tmp/test-catalog.XXXXXX";
186 r
= mkostemp_safe(name
);
191 /* Test what happens if there are no files. */
192 r
= catalog_update(database
, NULL
, NULL
);
195 /* Test what happens if there are no files in the directory. */
196 r
= catalog_update(database
, NULL
, no_catalog_dirs
);
199 /* Make sure that we at least have some files loaded or the
200 catalog_list below will fail. */
201 r
= catalog_update(database
, NULL
, catalog_dirs
);
205 static void test_catalog_file_lang(void) {
206 _cleanup_free_
char *lang
= NULL
, *lang2
= NULL
, *lang3
= NULL
, *lang4
= NULL
;
208 assert_se(catalog_file_lang("systemd.de_DE.catalog", &lang
) == 1);
209 assert_se(streq(lang
, "de_DE"));
211 assert_se(catalog_file_lang("systemd..catalog", &lang2
) == 0);
212 assert_se(lang2
== NULL
);
214 assert_se(catalog_file_lang("systemd.fr.catalog", &lang2
) == 1);
215 assert_se(streq(lang2
, "fr"));
217 assert_se(catalog_file_lang("systemd.fr.catalog.gz", &lang3
) == 0);
218 assert_se(lang3
== NULL
);
220 assert_se(catalog_file_lang("systemd.01234567890123456789012345678901.catalog", &lang3
) == 0);
221 assert_se(lang3
== NULL
);
223 assert_se(catalog_file_lang("systemd.0123456789012345678901234567890.catalog", &lang3
) == 1);
224 assert_se(streq(lang3
, "0123456789012345678901234567890"));
226 assert_se(catalog_file_lang("/x/y/systemd.catalog", &lang4
) == 0);
227 assert_se(lang4
== NULL
);
229 assert_se(catalog_file_lang("/x/y/systemd.ru_RU.catalog", &lang4
) == 1);
230 assert_se(streq(lang4
, "ru_RU"));
233 int main(int argc
, char *argv
[]) {
234 _cleanup_free_
char *text
= NULL
;
237 setlocale(LC_ALL
, "de_DE.UTF-8");
239 log_parse_environment();
242 test_catalog_file_lang();
244 test_catalog_import_invalid();
245 test_catalog_import_badid();
246 test_catalog_import_one();
247 test_catalog_import_merge();
248 test_catalog_import_merge_no_body();
250 test_catalog_update();
252 r
= catalog_list(stdout
, database
, true);
255 r
= catalog_list(stdout
, database
, false);
258 assert_se(catalog_get(database
, SD_MESSAGE_COREDUMP
, &text
) >= 0);
259 printf(">>>%s<<<\n", text
);