chiark / gitweb /
c46303589154cec05ffe671e513a6535c6097f8f
[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 void test_import(Hashmap *h, struct strbuf *sb,
35                         const char* contents, ssize_t size, int code) {
36         int r;
37         char name[] = "/tmp/test-catalog.XXXXXX";
38         int _cleanup_close_ fd = mkstemp(name);
39         assert(fd >= 0);
40         assert_se(write(fd, contents, size) == size);
41
42         r = catalog_import_file(h, sb, name);
43         assert(r == code);
44
45         unlink(name);
46 }
47
48 static void test_catalog_importing(void) {
49         Hashmap *h;
50         struct strbuf *sb;
51
52         assert_se(h = hashmap_new(catalog_hash_func, catalog_compare_func));
53         assert_se(sb = strbuf_new());
54
55 #define BUF "xxx"
56         test_import(h, sb, BUF, sizeof(BUF), -EINVAL);
57 #undef BUF
58         assert(hashmap_isempty(h));
59         log_debug("----------------------------------------");
60
61 #define BUF \
62 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededede\n" \
63 "Subject: message\n" \
64 "\n" \
65 "payload\n"
66         test_import(h, sb, BUF, sizeof(BUF), -EINVAL);
67 #undef BUF
68
69         log_debug("----------------------------------------");
70
71 #define BUF \
72 "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
73 "Subject: message\n" \
74 "\n" \
75 "payload\n"
76         test_import(h, sb, BUF, sizeof(BUF), 0);
77 #undef BUF
78
79         assert(hashmap_size(h) == 1);
80
81         log_debug("----------------------------------------");
82
83         hashmap_free_free(h);
84         strbuf_cleanup(sb);
85 }
86
87
88 static const char* database = NULL;
89
90 static void test_catalog_update(void) {
91         int r;
92
93         static char name[] = "/tmp/test-catalog.XXXXXX";
94         r = mkstemp(name);
95         assert(r >= 0);
96
97         database = name;
98
99         /* Test what happens if there are no files. */
100         r = catalog_update(database, NULL, NULL);
101         assert(r >= 0);
102
103         /* Note: this might actually not find anything, if systemd was
104          * not installed before. That should be fine too. */
105         r = catalog_update(database, NULL, catalog_file_dirs);
106         assert(r >= 0);
107 }
108
109 int main(int argc, char *argv[]) {
110         _cleanup_free_ char *text = NULL;
111         int r;
112
113         setlocale(LC_ALL, "de_DE.UTF-8");
114
115         log_set_max_level(LOG_DEBUG);
116
117         test_catalog_importing();
118
119         test_catalog_update();
120
121         r = catalog_list(stdout, database, true);
122         assert_se(r >= 0);
123
124         r = catalog_list(stdout, database, false);
125         assert_se(r >= 0);
126
127         assert_se(catalog_get(database, SD_MESSAGE_COREDUMP, &text) >= 0);
128         printf(">>>%s<<<\n", text);
129
130         if (database)
131                 unlink(database);
132
133         return 0;
134 }