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