]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/test-catalog.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / journal / test-catalog.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2012 Lennart Poettering
7 Copyright 2013 Zbigniew Jędrzejewski-Szmek
8
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.
13
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.
18
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/>.
21 ***/
22
23 #include <locale.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <fcntl.h>
27
28 #include "sd-messages.h"
29
30 #include "util.h"
31 #include "log.h"
32 #include "macro.h"
33 #include "catalog.h"
34 #include "string-util.h"
35
36 static const char *catalog_dirs[] = {
37 CATALOG_DIR,
38 NULL,
39 };
40
41 static const char *no_catalog_dirs[] = {
42 "/bin/hopefully/with/no/catalog",
43 NULL
44 };
45
46 static void test_import(Hashmap *h, struct strbuf *sb,
47 const char* contents, ssize_t size, int code) {
48 int r;
49 char name[] = "/tmp/test-catalog.XXXXXX";
50 _cleanup_close_ int fd;
51
52 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
53 assert_se(fd >= 0);
54 assert_se(write(fd, contents, size) == size);
55
56 r = catalog_import_file(h, sb, name);
57 assert_se(r == code);
58
59 unlink(name);
60 }
61
62 static void test_catalog_importing(void) {
63 Hashmap *h;
64 struct strbuf *sb;
65
66 assert_se(h = hashmap_new(&catalog_hash_ops));
67 assert_se(sb = strbuf_new());
68
69 #define BUF "xxx"
70 test_import(h, sb, BUF, sizeof(BUF), -EINVAL);
71 #undef BUF
72 assert_se(hashmap_isempty(h));
73 log_debug("----------------------------------------");
74
75 #define BUF \
76 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededede\n" \
77 "Subject: message\n" \
78 "\n" \
79 "payload\n"
80 test_import(h, sb, BUF, sizeof(BUF), -EINVAL);
81 #undef BUF
82
83 log_debug("----------------------------------------");
84
85 #define BUF \
86 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
87 "Subject: message\n" \
88 "\n" \
89 "payload\n"
90 test_import(h, sb, BUF, sizeof(BUF), 0);
91 #undef BUF
92
93 assert_se(hashmap_size(h) == 1);
94
95 log_debug("----------------------------------------");
96
97 hashmap_free_free(h);
98 strbuf_cleanup(sb);
99 }
100
101
102 static const char* database = NULL;
103
104 static void test_catalog_update(void) {
105 static char name[] = "/tmp/test-catalog.XXXXXX";
106 int r;
107
108 r = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
109 assert_se(r >= 0);
110
111 database = name;
112
113 /* Test what happens if there are no files. */
114 r = catalog_update(database, NULL, NULL);
115 assert_se(r >= 0);
116
117 /* Test what happens if there are no files in the directory. */
118 r = catalog_update(database, NULL, no_catalog_dirs);
119 assert_se(r >= 0);
120
121 /* Make sure that we at least have some files loaded or the
122 catalog_list below will fail. */
123 r = catalog_update(database, NULL, catalog_dirs);
124 assert_se(r >= 0);
125 }
126
127 static void test_catalog_file_lang(void) {
128 _cleanup_free_ char *lang = NULL, *lang2 = NULL, *lang3 = NULL, *lang4 = NULL;
129
130 assert_se(catalog_file_lang("systemd.de_DE.catalog", &lang) == 1);
131 assert_se(streq(lang, "de_DE"));
132
133 assert_se(catalog_file_lang("systemd..catalog", &lang2) == 0);
134 assert_se(lang2 == NULL);
135
136 assert_se(catalog_file_lang("systemd.fr.catalog", &lang2) == 1);
137 assert_se(streq(lang2, "fr"));
138
139 assert_se(catalog_file_lang("systemd.fr.catalog.gz", &lang3) == 0);
140 assert_se(lang3 == NULL);
141
142 assert_se(catalog_file_lang("systemd.01234567890123456789012345678901.catalog", &lang3) == 0);
143 assert_se(lang3 == NULL);
144
145 assert_se(catalog_file_lang("systemd.0123456789012345678901234567890.catalog", &lang3) == 1);
146 assert_se(streq(lang3, "0123456789012345678901234567890"));
147
148 assert_se(catalog_file_lang("/x/y/systemd.catalog", &lang4) == 0);
149 assert_se(lang4 == NULL);
150
151 assert_se(catalog_file_lang("/x/y/systemd.ru_RU.catalog", &lang4) == 1);
152 assert_se(streq(lang4, "ru_RU"));
153 }
154
155 int main(int argc, char *argv[]) {
156 _cleanup_free_ char *text = NULL;
157 int r;
158
159 setlocale(LC_ALL, "de_DE.UTF-8");
160
161 log_parse_environment();
162 log_open();
163
164 test_catalog_file_lang();
165
166 test_catalog_importing();
167
168 test_catalog_update();
169
170 r = catalog_list(stdout, database, true);
171 assert_se(r >= 0);
172
173 r = catalog_list(stdout, database, false);
174 assert_se(r >= 0);
175
176 assert_se(catalog_get(database, SD_MESSAGE_COREDUMP, &text) >= 0);
177 printf(">>>%s<<<\n", text);
178
179 if (database)
180 unlink(database);
181
182 return 0;
183 }