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