]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/test-catalog.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 Lennart Poettering
7 Copyright 2013 Zbigniew Jędrzejewski-Szmek
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "sd-messages.h"
30 #include "alloc-util.h"
36 #include "string-util.h"
39 static const char *catalog_dirs
[] = {
44 static const char *no_catalog_dirs
[] = {
45 "/bin/hopefully/with/no/catalog",
49 static void test_import(Hashmap
*h
, struct strbuf
*sb
,
50 const char* contents
, ssize_t size
, int code
) {
52 char name
[] = "/tmp/test-catalog.XXXXXX";
53 _cleanup_close_
int fd
;
55 fd
= mkostemp_safe(name
, O_RDWR
|O_CLOEXEC
);
57 assert_se(write(fd
, contents
, size
) == size
);
59 r
= catalog_import_file(h
, sb
, name
);
65 static void test_catalog_importing(void) {
69 assert_se(h
= hashmap_new(&catalog_hash_ops
));
70 assert_se(sb
= strbuf_new());
73 test_import(h
, sb
, BUF
, sizeof(BUF
), -EINVAL
);
75 assert_se(hashmap_isempty(h
));
76 log_debug("----------------------------------------");
79 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededede\n" \
80 "Subject: message\n" \
83 test_import(h
, sb
, BUF
, sizeof(BUF
), -EINVAL
);
86 log_debug("----------------------------------------");
89 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
90 "Subject: message\n" \
93 test_import(h
, sb
, BUF
, sizeof(BUF
), 0);
96 assert_se(hashmap_size(h
) == 1);
98 log_debug("----------------------------------------");
100 hashmap_free_free(h
);
105 static const char* database
= NULL
;
107 static void test_catalog_update(void) {
108 static char name
[] = "/tmp/test-catalog.XXXXXX";
111 r
= mkostemp_safe(name
, O_RDWR
|O_CLOEXEC
);
116 /* Test what happens if there are no files. */
117 r
= catalog_update(database
, NULL
, NULL
);
120 /* Test what happens if there are no files in the directory. */
121 r
= catalog_update(database
, NULL
, no_catalog_dirs
);
124 /* Make sure that we at least have some files loaded or the
125 catalog_list below will fail. */
126 r
= catalog_update(database
, NULL
, catalog_dirs
);
130 static void test_catalog_file_lang(void) {
131 _cleanup_free_
char *lang
= NULL
, *lang2
= NULL
, *lang3
= NULL
, *lang4
= NULL
;
133 assert_se(catalog_file_lang("systemd.de_DE.catalog", &lang
) == 1);
134 assert_se(streq(lang
, "de_DE"));
136 assert_se(catalog_file_lang("systemd..catalog", &lang2
) == 0);
137 assert_se(lang2
== NULL
);
139 assert_se(catalog_file_lang("systemd.fr.catalog", &lang2
) == 1);
140 assert_se(streq(lang2
, "fr"));
142 assert_se(catalog_file_lang("systemd.fr.catalog.gz", &lang3
) == 0);
143 assert_se(lang3
== NULL
);
145 assert_se(catalog_file_lang("systemd.01234567890123456789012345678901.catalog", &lang3
) == 0);
146 assert_se(lang3
== NULL
);
148 assert_se(catalog_file_lang("systemd.0123456789012345678901234567890.catalog", &lang3
) == 1);
149 assert_se(streq(lang3
, "0123456789012345678901234567890"));
151 assert_se(catalog_file_lang("/x/y/systemd.catalog", &lang4
) == 0);
152 assert_se(lang4
== NULL
);
154 assert_se(catalog_file_lang("/x/y/systemd.ru_RU.catalog", &lang4
) == 1);
155 assert_se(streq(lang4
, "ru_RU"));
158 int main(int argc
, char *argv
[]) {
159 _cleanup_free_
char *text
= NULL
;
162 setlocale(LC_ALL
, "de_DE.UTF-8");
164 log_parse_environment();
167 test_catalog_file_lang();
169 test_catalog_importing();
171 test_catalog_update();
173 r
= catalog_list(stdout
, database
, true);
176 r
= catalog_list(stdout
, database
, false);
179 assert_se(catalog_get(database
, SD_MESSAGE_COREDUMP
, &text
) >= 0);
180 printf(">>>%s<<<\n", text
);