chiark / gitweb /
catalog: determine language from the filename
[elogind.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 <stdlib.h>
25 #include <unistd.h>
26 #include <errno.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 = mkstemp(name);
49         assert(fd >= 0);
50         assert_se(write(fd, contents, size) == size);
51
52         r = catalog_import_file(h, sb, name);
53         assert(r == code);
54
55         unlink(name);
56 }
57
58 static void test_catalog_importing(void) {
59         Hashmap *h;
60         struct strbuf *sb;
61
62         assert_se(h = hashmap_new(catalog_hash_func, catalog_compare_func));
63         assert_se(sb = strbuf_new());
64
65 #define BUF "xxx"
66         test_import(h, sb, BUF, sizeof(BUF), -EINVAL);
67 #undef BUF
68         assert(hashmap_isempty(h));
69         log_debug("----------------------------------------");
70
71 #define BUF \
72 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededede\n" \
73 "Subject: message\n" \
74 "\n" \
75 "payload\n"
76         test_import(h, sb, BUF, sizeof(BUF), -EINVAL);
77 #undef BUF
78
79         log_debug("----------------------------------------");
80
81 #define BUF \
82 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
83 "Subject: message\n" \
84 "\n" \
85 "payload\n"
86         test_import(h, sb, BUF, sizeof(BUF), 0);
87 #undef BUF
88
89         assert(hashmap_size(h) == 1);
90
91         log_debug("----------------------------------------");
92
93         hashmap_free_free(h);
94         strbuf_cleanup(sb);
95 }
96
97
98 static const char* database = NULL;
99
100 static void test_catalog_update(void) {
101         int r;
102
103         static char name[] = "/tmp/test-catalog.XXXXXX";
104         r = mkstemp(name);
105         assert(r >= 0);
106
107         database = name;
108
109         /* Test what happens if there are no files. */
110         r = catalog_update(database, NULL, NULL);
111         assert(r >= 0);
112
113         /* Test what happens if there are no files in the directory. */
114         r = catalog_update(database, NULL, no_catalog_dirs);
115         assert(r >= 0);
116
117         /* Make sure that we at least have some files loaded or the
118            catalog_list below will fail. */
119         r = catalog_update(database, NULL, catalog_dirs);
120         assert(r >= 0);
121 }
122
123 static void test_catalog_file_lang(void) {
124         _cleanup_free_ char *lang = NULL, *lang2 = NULL, *lang3 = NULL;
125
126         assert_se(catalog_file_lang("systemd.de_DE.catalog", &lang) == 1);
127         assert_se(streq(lang, "de_DE"));
128
129         assert_se(catalog_file_lang("systemd..catalog", &lang2) == 0);
130         assert_se(lang2 == NULL);
131
132         assert_se(catalog_file_lang("systemd.fr.catalog", &lang2) == 1);
133         assert_se(streq(lang2, "fr"));
134
135         assert_se(catalog_file_lang("systemd.fr.catalog.gz", &lang3) == 0);
136         assert_se(lang3 == NULL);
137
138         assert_se(catalog_file_lang("systemd.01234567890123456789012345678901.catalog", &lang3) == 0);
139         assert_se(lang3 == NULL);
140
141         assert_se(catalog_file_lang("systemd.0123456789012345678901234567890.catalog", &lang3) == 1);
142         assert_se(streq(lang3, "0123456789012345678901234567890"));
143 }
144
145 int main(int argc, char *argv[]) {
146         _cleanup_free_ char *text = NULL;
147         int r;
148
149         setlocale(LC_ALL, "de_DE.UTF-8");
150
151         log_set_max_level(LOG_DEBUG);
152
153         test_catalog_importing();
154
155         test_catalog_update();
156
157         r = catalog_list(stdout, database, true);
158         assert_se(r >= 0);
159
160         r = catalog_list(stdout, database, false);
161         assert_se(r >= 0);
162
163         assert_se(catalog_get(database, SD_MESSAGE_COREDUMP, &text) >= 0);
164         printf(">>>%s<<<\n", text);
165
166         if (database)
167                 unlink(database);
168
169         test_catalog_file_lang();
170
171         return 0;
172 }