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
, O_RDWR
|O_CLOEXEC
);
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 assert_se(streq(expect
, payload
));
110 static void test_catalog_import_merge(void) {
111 _cleanup_hashmap_free_free_free_ Hashmap
*h
= NULL
;
116 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
117 "Subject: message\n" \
122 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
123 "Subject: override subject\n" \
124 "X-Header: hello\n" \
126 "override payload\n";
128 const char *combined
=
129 "Subject: override subject\n" \
130 "X-Header: hello\n" \
131 "Subject: message\n" \
134 "override payload\n";
136 h
= test_import(input
, -1, 0);
137 assert_se(hashmap_size(h
) == 1);
139 HASHMAP_FOREACH(payload
, h
, j
) {
140 assert_se(streq(combined
, payload
));
144 static void test_catalog_import_merge_no_body(void) {
145 _cleanup_hashmap_free_free_free_ Hashmap
*h
= NULL
;
150 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
151 "Subject: message\n" \
156 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
157 "Subject: override subject\n" \
158 "X-Header: hello\n" \
161 const char *combined
=
162 "Subject: override subject\n" \
163 "X-Header: hello\n" \
164 "Subject: message\n" \
169 h
= test_import(input
, -1, 0);
170 assert_se(hashmap_size(h
) == 1);
172 HASHMAP_FOREACH(payload
, h
, j
) {
173 assert_se(streq(combined
, payload
));
177 static const char* database
= NULL
;
179 static void test_catalog_update(void) {
180 static char name
[] = "/tmp/test-catalog.XXXXXX";
183 r
= mkostemp_safe(name
, O_RDWR
|O_CLOEXEC
);
188 /* Test what happens if there are no files. */
189 r
= catalog_update(database
, NULL
, NULL
);
192 /* Test what happens if there are no files in the directory. */
193 r
= catalog_update(database
, NULL
, no_catalog_dirs
);
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
);
202 static void test_catalog_file_lang(void) {
203 _cleanup_free_
char *lang
= NULL
, *lang2
= NULL
, *lang3
= NULL
, *lang4
= NULL
;
205 assert_se(catalog_file_lang("systemd.de_DE.catalog", &lang
) == 1);
206 assert_se(streq(lang
, "de_DE"));
208 assert_se(catalog_file_lang("systemd..catalog", &lang2
) == 0);
209 assert_se(lang2
== NULL
);
211 assert_se(catalog_file_lang("systemd.fr.catalog", &lang2
) == 1);
212 assert_se(streq(lang2
, "fr"));
214 assert_se(catalog_file_lang("systemd.fr.catalog.gz", &lang3
) == 0);
215 assert_se(lang3
== NULL
);
217 assert_se(catalog_file_lang("systemd.01234567890123456789012345678901.catalog", &lang3
) == 0);
218 assert_se(lang3
== NULL
);
220 assert_se(catalog_file_lang("systemd.0123456789012345678901234567890.catalog", &lang3
) == 1);
221 assert_se(streq(lang3
, "0123456789012345678901234567890"));
223 assert_se(catalog_file_lang("/x/y/systemd.catalog", &lang4
) == 0);
224 assert_se(lang4
== NULL
);
226 assert_se(catalog_file_lang("/x/y/systemd.ru_RU.catalog", &lang4
) == 1);
227 assert_se(streq(lang4
, "ru_RU"));
230 int main(int argc
, char *argv
[]) {
231 _cleanup_free_
char *text
= NULL
;
234 setlocale(LC_ALL
, "de_DE.UTF-8");
236 log_parse_environment();
239 test_catalog_file_lang();
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();
247 test_catalog_update();
249 r
= catalog_list(stdout
, database
, true);
252 r
= catalog_list(stdout
, database
, false);
255 assert_se(catalog_get(database
, SD_MESSAGE_COREDUMP
, &text
) >= 0);
256 printf(">>>%s<<<\n", text
);