]>
Commit | Line | Data |
---|---|---|
d4205751 LP |
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 | |
844ec79b | 7 | Copyright 2013 Zbigniew Jędrzejewski-Szmek |
d4205751 LP |
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> | |
844ec79b ZJS |
24 | #include <unistd.h> |
25 | #include <errno.h> | |
2d5bdf5b | 26 | #include <fcntl.h> |
d4205751 | 27 | |
07630cea LP |
28 | #include "sd-messages.h" |
29 | ||
b5efdb8a | 30 | #include "alloc-util.h" |
3ffd4af2 LP |
31 | #include "catalog.h" |
32 | #include "fd-util.h" | |
0d39fa9c | 33 | #include "fileio.h" |
d4205751 | 34 | #include "log.h" |
844ec79b | 35 | #include "macro.h" |
07630cea | 36 | #include "string-util.h" |
3ffd4af2 | 37 | #include "util.h" |
d4205751 | 38 | |
143bfdaf HHPF |
39 | static const char *catalog_dirs[] = { |
40 | CATALOG_DIR, | |
41 | NULL, | |
42 | }; | |
43 | ||
44 | static const char *no_catalog_dirs[] = { | |
45 | "/bin/hopefully/with/no/catalog", | |
46 | NULL | |
47 | }; | |
48 | ||
844ec79b ZJS |
49 | static void test_import(Hashmap *h, struct strbuf *sb, |
50 | const char* contents, ssize_t size, int code) { | |
51 | int r; | |
52 | char name[] = "/tmp/test-catalog.XXXXXX"; | |
2d5bdf5b LP |
53 | _cleanup_close_ int fd; |
54 | ||
55 | fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); | |
787784c4 | 56 | assert_se(fd >= 0); |
844ec79b ZJS |
57 | assert_se(write(fd, contents, size) == size); |
58 | ||
59 | r = catalog_import_file(h, sb, name); | |
787784c4 | 60 | assert_se(r == code); |
844ec79b ZJS |
61 | |
62 | unlink(name); | |
63 | } | |
64 | ||
65 | static void test_catalog_importing(void) { | |
66 | Hashmap *h; | |
67 | struct strbuf *sb; | |
68 | ||
d5099efc | 69 | assert_se(h = hashmap_new(&catalog_hash_ops)); |
844ec79b ZJS |
70 | assert_se(sb = strbuf_new()); |
71 | ||
72 | #define BUF "xxx" | |
73 | test_import(h, sb, BUF, sizeof(BUF), -EINVAL); | |
74 | #undef BUF | |
787784c4 | 75 | assert_se(hashmap_isempty(h)); |
844ec79b ZJS |
76 | log_debug("----------------------------------------"); |
77 | ||
78 | #define BUF \ | |
79 | "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededede\n" \ | |
80 | "Subject: message\n" \ | |
81 | "\n" \ | |
82 | "payload\n" | |
83 | test_import(h, sb, BUF, sizeof(BUF), -EINVAL); | |
84 | #undef BUF | |
85 | ||
86 | log_debug("----------------------------------------"); | |
87 | ||
88 | #define BUF \ | |
89 | "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \ | |
90 | "Subject: message\n" \ | |
91 | "\n" \ | |
92 | "payload\n" | |
93 | test_import(h, sb, BUF, sizeof(BUF), 0); | |
94 | #undef BUF | |
95 | ||
787784c4 | 96 | assert_se(hashmap_size(h) == 1); |
d4205751 | 97 | |
844ec79b ZJS |
98 | log_debug("----------------------------------------"); |
99 | ||
100 | hashmap_free_free(h); | |
101 | strbuf_cleanup(sb); | |
102 | } | |
103 | ||
104 | ||
105 | static const char* database = NULL; | |
106 | ||
107 | static void test_catalog_update(void) { | |
2d5bdf5b | 108 | static char name[] = "/tmp/test-catalog.XXXXXX"; |
844ec79b | 109 | int r; |
844ec79b | 110 | |
2d5bdf5b | 111 | r = mkostemp_safe(name, O_RDWR|O_CLOEXEC); |
787784c4 | 112 | assert_se(r >= 0); |
844ec79b ZJS |
113 | |
114 | database = name; | |
115 | ||
116 | /* Test what happens if there are no files. */ | |
117 | r = catalog_update(database, NULL, NULL); | |
787784c4 | 118 | assert_se(r >= 0); |
844ec79b | 119 | |
143bfdaf HHPF |
120 | /* Test what happens if there are no files in the directory. */ |
121 | r = catalog_update(database, NULL, no_catalog_dirs); | |
787784c4 | 122 | assert_se(r >= 0); |
143bfdaf HHPF |
123 | |
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); | |
787784c4 | 127 | assert_se(r >= 0); |
844ec79b ZJS |
128 | } |
129 | ||
c7332b08 | 130 | static void test_catalog_file_lang(void) { |
4b8268f8 | 131 | _cleanup_free_ char *lang = NULL, *lang2 = NULL, *lang3 = NULL, *lang4 = NULL; |
c7332b08 ZJS |
132 | |
133 | assert_se(catalog_file_lang("systemd.de_DE.catalog", &lang) == 1); | |
134 | assert_se(streq(lang, "de_DE")); | |
135 | ||
136 | assert_se(catalog_file_lang("systemd..catalog", &lang2) == 0); | |
137 | assert_se(lang2 == NULL); | |
138 | ||
139 | assert_se(catalog_file_lang("systemd.fr.catalog", &lang2) == 1); | |
140 | assert_se(streq(lang2, "fr")); | |
141 | ||
142 | assert_se(catalog_file_lang("systemd.fr.catalog.gz", &lang3) == 0); | |
143 | assert_se(lang3 == NULL); | |
144 | ||
145 | assert_se(catalog_file_lang("systemd.01234567890123456789012345678901.catalog", &lang3) == 0); | |
146 | assert_se(lang3 == NULL); | |
147 | ||
148 | assert_se(catalog_file_lang("systemd.0123456789012345678901234567890.catalog", &lang3) == 1); | |
149 | assert_se(streq(lang3, "0123456789012345678901234567890")); | |
4b8268f8 ZJS |
150 | |
151 | assert_se(catalog_file_lang("/x/y/systemd.catalog", &lang4) == 0); | |
152 | assert_se(lang4 == NULL); | |
153 | ||
154 | assert_se(catalog_file_lang("/x/y/systemd.ru_RU.catalog", &lang4) == 1); | |
155 | assert_se(streq(lang4, "ru_RU")); | |
c7332b08 ZJS |
156 | } |
157 | ||
844ec79b | 158 | int main(int argc, char *argv[]) { |
d4205751 | 159 | _cleanup_free_ char *text = NULL; |
844ec79b | 160 | int r; |
d4205751 LP |
161 | |
162 | setlocale(LC_ALL, "de_DE.UTF-8"); | |
163 | ||
e3b9d9c8 ZJS |
164 | log_parse_environment(); |
165 | log_open(); | |
d4205751 | 166 | |
4b8268f8 ZJS |
167 | test_catalog_file_lang(); |
168 | ||
844ec79b | 169 | test_catalog_importing(); |
d4205751 | 170 | |
844ec79b | 171 | test_catalog_update(); |
54b7254c | 172 | |
844ec79b ZJS |
173 | r = catalog_list(stdout, database, true); |
174 | assert_se(r >= 0); | |
d4205751 | 175 | |
844ec79b ZJS |
176 | r = catalog_list(stdout, database, false); |
177 | assert_se(r >= 0); | |
d4205751 | 178 | |
844ec79b | 179 | assert_se(catalog_get(database, SD_MESSAGE_COREDUMP, &text) >= 0); |
d4205751 LP |
180 | printf(">>>%s<<<\n", text); |
181 | ||
844ec79b ZJS |
182 | if (database) |
183 | unlink(database); | |
d4205751 LP |
184 | |
185 | return 0; | |
186 | } |