/
test-event-log.c
187 lines (172 loc) · 4.44 KB
/
test-event-log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* Copyright (c) 2018 Dovecot authors, see the included COPYING file */
#include "test-lib.h"
#include "ioloop.h"
#include "str.h"
#include "failures-private.h"
enum test_log_prefix_type {
TYPE_END,
TYPE_APPEND,
TYPE_REPLACE,
TYPE_SKIP,
};
struct test_log_prefix {
enum test_log_prefix_type type;
const char *str;
};
struct test_log {
const struct test_log_prefix *prefixes;
const char *global_log_prefix;
const char *result;
};
static char *test_output;
static void ATTR_FORMAT(2, 0)
info_handler(const struct failure_context *ctx,
const char *format, va_list args)
{
size_t prefix_len;
i_assert(ctx->type == LOG_TYPE_INFO);
i_free(test_output);
T_BEGIN {
string_t *str = failure_handler.v->format(ctx, &prefix_len,
format, args);
test_output = i_strdup(str_c(str));
} T_END;
}
static void test_event_log_prefix(void)
{
struct test_log tests[] = {
{
.prefixes = (const struct test_log_prefix []) {
{ .type = TYPE_END }
},
.global_log_prefix = "global1.",
.result = "global1.Info: TEXT",
},
{
.prefixes = (const struct test_log_prefix []) {
{ TYPE_REPLACE, "replaced1," },
{ .type = TYPE_END }
},
.result = "replaced1,Info: TEXT",
},
{
.prefixes = (const struct test_log_prefix []) {
{ TYPE_REPLACE, "replaced1," },
{ TYPE_REPLACE, "replaced2." },
{ .type = TYPE_END }
},
.result = "replaced2.Info: TEXT",
},
{
.prefixes = (const struct test_log_prefix []) {
{ TYPE_REPLACE, "replaced1," },
{ TYPE_APPEND, "appended2." },
{ .type = TYPE_END }
},
.result = "replaced1,Info: appended2.TEXT",
},
{
.prefixes = (const struct test_log_prefix []) {
{ TYPE_APPEND, "appended1," },
{ TYPE_REPLACE, "replaced1," },
{ .type = TYPE_END }
},
.result = "replaced1,Info: TEXT",
},
{
.prefixes = (const struct test_log_prefix []) {
{ TYPE_APPEND, "appended1," },
{ .type = TYPE_END }
},
.global_log_prefix = "global2.",
.result = "global2.Info: appended1,TEXT",
},
{
.prefixes = (const struct test_log_prefix []) {
{ TYPE_APPEND, "appended1," },
{ TYPE_APPEND, "appended2." },
{ .type = TYPE_END }
},
.global_log_prefix = "global3.",
.result = "global3.Info: appended1,appended2.TEXT",
},
{
.prefixes = (const struct test_log_prefix []) {
{ TYPE_APPEND, "appended1," },
{ TYPE_REPLACE, "replaced2." },
{ TYPE_APPEND, "appended3#" },
{ .type = TYPE_END }
},
.result = "replaced2.Info: appended3#TEXT",
},
{
.prefixes = (const struct test_log_prefix []) {
{ TYPE_APPEND, "appended1," },
{ TYPE_REPLACE, "replaced2." },
{ TYPE_APPEND, "appended3#" },
{ TYPE_REPLACE, "replaced4;" },
{ .type = TYPE_END }
},
.result = "replaced4;Info: TEXT",
},
{
.prefixes = (const struct test_log_prefix []) {
{ TYPE_APPEND, "appended1," },
{ TYPE_REPLACE, "replaced2." },
{ TYPE_APPEND, "appended3#" },
{ TYPE_REPLACE, "replaced4;" },
{ TYPE_APPEND, "appended5-" },
{ .type = TYPE_END }
},
.result = "replaced4;Info: appended5-TEXT",
},
};
const struct event_log_params params = {
.log_type = LOG_TYPE_INFO,
};
test_begin("event log prefixes");
failure_callback_t *orig_fatal, *orig_error, *orig_info, *orig_debug;
i_get_failure_handlers(&orig_fatal, &orig_error, &orig_info, &orig_debug);
i_set_info_handler(info_handler);
for (unsigned int i = 0; i < N_ELEMENTS(tests); i++) {
const struct test_log *test = &tests[i];
if (test->global_log_prefix != NULL)
i_set_failure_prefix("%s", test->global_log_prefix);
else
i_set_failure_prefix("UNEXPECTED GLOBAL PREFIX");
struct event *event, *parent;
event = parent = event_create(NULL);
for (unsigned int j = 0; test->prefixes[j].type != TYPE_END; j++) {
if (event == NULL) {
struct event *child = event_create(parent);
event_unref(&parent);
event = parent = child;
}
switch (test->prefixes[j].type) {
case TYPE_END:
i_unreached();
case TYPE_APPEND:
event_set_append_log_prefix(event, test->prefixes[j].str);
break;
case TYPE_REPLACE:
event_replace_log_prefix(event, test->prefixes[j].str);
break;
case TYPE_SKIP:
break;
}
event = NULL;
}
event = parent;
event_log(event, ¶ms, "TEXT");
test_assert_strcmp(test->result, test_output);
event_unref(&event);
}
i_set_info_handler(orig_info);
i_unset_failure_prefix();
i_free(test_output);
test_end();
}
void test_event_log(void)
{
test_event_log_prefix();
}