]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/test-catalog.c
catalog: open up catalog internals
[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 <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 char _cleanup_free_ *path = NULL;
93
94 static char name[] = "/tmp/test-catalog.XXXXXX";
95 r = mkstemp(name);
96 assert(r >= 0);
97
98 database = name;
99
100 /* Test what happens if there are no files. */
101 r = catalog_update(database, NULL, NULL);
102 assert(r >= 0);
103
104 /* Note: this might actually not find anything, if systemd was
105 * not installed before. That should be fine too. */
106 r = catalog_update(database, NULL, catalog_file_dirs);
107 assert(r >= 0);
108 }
109
110 int main(int argc, char *argv[]) {
111 _cleanup_free_ char *text = NULL;
112 int r;
113
114 setlocale(LC_ALL, "de_DE.UTF-8");
115
116 log_set_max_level(LOG_DEBUG);
117
118 test_catalog_importing();
119
120 test_catalog_update();
121
122 r = catalog_list(stdout, database, true);
123 assert_se(r >= 0);
124
125 r = catalog_list(stdout, database, false);
126 assert_se(r >= 0);
127
128 assert_se(catalog_get(database, SD_MESSAGE_COREDUMP, &text) >= 0);
129 printf(">>>%s<<<\n", text);
130
131 if (database)
132 unlink(database);
133
134 return 0;
135 }