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