2 This file is part of systemd.
4 Copyright 2012 Lennart Poettering
5 Copyright 2013 Zbigniew Jędrzejewski-Szmek
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.
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.
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/>.
26 #include "sd-messages.h"
28 #include "alloc-util.h"
34 #include "string-util.h"
37 static const char *catalog_dirs
[] = {
42 static const char *no_catalog_dirs
[] = {
43 "/bin/hopefully/with/no/catalog",
47 static Hashmap
* test_import(const char* contents
, ssize_t size
, int code
) {
49 char name
[] = "/tmp/test-catalog.XXXXXX";
50 _cleanup_close_
int fd
;
54 size
= strlen(contents
);
56 assert_se(h
= hashmap_new(&catalog_hash_ops
));
58 fd
= mkostemp_safe(name
);
60 assert_se(write(fd
, contents
, size
) == size
);
62 r
= catalog_import_file(h
, name
);
70 static void test_catalog_import_invalid(void) {
71 _cleanup_hashmap_free_free_free_ Hashmap
*h
= NULL
;
73 h
= test_import("xxx", -1, -EINVAL
);
74 assert_se(hashmap_isempty(h
));
77 static void test_catalog_import_badid(void) {
78 _cleanup_hashmap_free_free_free_ Hashmap
*h
= NULL
;
80 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededede\n" \
81 "Subject: message\n" \
84 h
= test_import(input
, -1, -EINVAL
);
87 static void test_catalog_import_one(void) {
88 _cleanup_hashmap_free_free_free_ Hashmap
*h
= NULL
;
93 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
94 "Subject: message\n" \
98 "Subject: message\n" \
102 h
= test_import(input
, -1, 0);
103 assert_se(hashmap_size(h
) == 1);
105 HASHMAP_FOREACH(payload
, h
, j
) {
106 printf("expect: %s\n", expect
);
107 printf("actual: %s\n", payload
);
108 assert_se(streq(expect
, payload
));
112 static void test_catalog_import_merge(void) {
113 _cleanup_hashmap_free_free_free_ Hashmap
*h
= NULL
;
118 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
119 "Subject: message\n" \
124 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
125 "Subject: override subject\n" \
126 "X-Header: hello\n" \
128 "override payload\n";
130 const char *combined
=
131 "Subject: override subject\n" \
132 "X-Header: hello\n" \
133 "Subject: message\n" \
136 "override payload\n";
138 h
= test_import(input
, -1, 0);
139 assert_se(hashmap_size(h
) == 1);
141 HASHMAP_FOREACH(payload
, h
, j
) {
142 assert_se(streq(combined
, payload
));
146 static void test_catalog_import_merge_no_body(void) {
147 _cleanup_hashmap_free_free_free_ Hashmap
*h
= NULL
;
152 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
153 "Subject: message\n" \
158 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
159 "Subject: override subject\n" \
160 "X-Header: hello\n" \
163 const char *combined
=
164 "Subject: override subject\n" \
165 "X-Header: hello\n" \
166 "Subject: message\n" \
171 h
= test_import(input
, -1, 0);
172 assert_se(hashmap_size(h
) == 1);
174 HASHMAP_FOREACH(payload
, h
, j
) {
175 assert_se(streq(combined
, payload
));
179 static const char* database
= NULL
;
181 static void test_catalog_update(void) {
182 static char name
[] = "/tmp/test-catalog.XXXXXX";
185 r
= mkostemp_safe(name
);
190 /* Test what happens if there are no files. */
191 r
= catalog_update(database
, NULL
, NULL
);
194 /* Test what happens if there are no files in the directory. */
195 r
= catalog_update(database
, NULL
, no_catalog_dirs
);
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
);
204 static void test_catalog_file_lang(void) {
205 _cleanup_free_
char *lang
= NULL
, *lang2
= NULL
, *lang3
= NULL
, *lang4
= NULL
;
207 assert_se(catalog_file_lang("systemd.de_DE.catalog", &lang
) == 1);
208 assert_se(streq(lang
, "de_DE"));
210 assert_se(catalog_file_lang("systemd..catalog", &lang2
) == 0);
211 assert_se(lang2
== NULL
);
213 assert_se(catalog_file_lang("systemd.fr.catalog", &lang2
) == 1);
214 assert_se(streq(lang2
, "fr"));
216 assert_se(catalog_file_lang("systemd.fr.catalog.gz", &lang3
) == 0);
217 assert_se(lang3
== NULL
);
219 assert_se(catalog_file_lang("systemd.01234567890123456789012345678901.catalog", &lang3
) == 0);
220 assert_se(lang3
== NULL
);
222 assert_se(catalog_file_lang("systemd.0123456789012345678901234567890.catalog", &lang3
) == 1);
223 assert_se(streq(lang3
, "0123456789012345678901234567890"));
225 assert_se(catalog_file_lang("/x/y/systemd.catalog", &lang4
) == 0);
226 assert_se(lang4
== NULL
);
228 assert_se(catalog_file_lang("/x/y/systemd.ru_RU.catalog", &lang4
) == 1);
229 assert_se(streq(lang4
, "ru_RU"));
232 int main(int argc
, char *argv
[]) {
233 _cleanup_free_
char *text
= NULL
;
236 setlocale(LC_ALL
, "de_DE.UTF-8");
238 log_parse_environment();
241 test_catalog_file_lang();
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();
249 test_catalog_update();
251 r
= catalog_list(stdout
, database
, true);
254 r
= catalog_list(stdout
, database
, false);
257 assert_se(catalog_get(database
, SD_MESSAGE_COREDUMP
, &text
) >= 0);
258 printf(">>>%s<<<\n", text
);