summaryrefslogtreecommitdiffstats
path: root/src/lib/test-event-flatten.c
blob: 526366c4ae60380edfce80df53f7ae87234c895c (plain)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* Copyright (c) 2019 Dovecot authors, see the included COPYING file */

#include "test-lib.h"
#include "ioloop.h"
#include "time-util.h"
#include "lib-event-private.h"
#include "failures-private.h"
#include "array.h"
#include "str.h"

#define CHECK_FLATTEN_SAME(e) \
	check_event_same(event_flatten(e), (e))

#define CHECK_FLATTEN_DIFF(e, c, nc, f, nf) \
	check_event_diff(event_flatten(e), (e), \
			 (c), (nc), \
			 (f), (nf))

static struct event_category cats[] = {
	{ .name = "cat0", },
	{ .name = "cat1", },
};

static void check_event_diff_cats(struct event_category *const *got,
				  unsigned int ngot, const char **exp,
				  unsigned int nexp)
{
	unsigned int i;

	test_assert(ngot == nexp);

	for (i = 0; i < nexp; i++)
		test_assert(strcmp(got[i]->name, exp[i]) == 0);
}

static void check_event_diff_fields(const struct event_field *got, unsigned int ngot,
				    const struct event_field *exp, unsigned int nexp)
{
	unsigned int i;
	const char *got_str;

	test_assert(ngot == nexp);

	for (i = 0; i < nexp; i++) {
		if (got[i].value_type != exp[i].value_type) {
			test_assert(FALSE);
			continue;
		}

		switch (exp[i].value_type) {
		case EVENT_FIELD_VALUE_TYPE_STR:
			test_assert(strcmp(exp[i].value.str,
					   got[i].value.str) == 0);
			break;
		case EVENT_FIELD_VALUE_TYPE_INTMAX:
			test_assert(exp[i].value.intmax == got[i].value.intmax);
			break;
		case EVENT_FIELD_VALUE_TYPE_TIMEVAL:
			test_assert(timeval_cmp(&exp[i].value.timeval,
						&got[i].value.timeval) == 0);
			break;
		case EVENT_FIELD_VALUE_TYPE_STRLIST:
			got_str = t_array_const_string_join(&got[i].value.strlist, ",");
			test_assert_strcmp(exp[i].value.str, got_str);
			break;
		}
	}
}

static void check_event_diff(struct event *e, struct event *orig,
			     const char **expected_cats,
			     unsigned int num_expected_cats,
			     const struct event_field *expected_fields,
			     unsigned int num_expected_fields)
{
	struct event_category *const *cats;
	const struct event_field *fields;
	unsigned int num_cats;
	unsigned int num_fields;

	test_assert(e != orig);
	test_assert(e->parent == NULL);

	/* different pointers implies different ids */
	test_assert(e->id != orig->id); /* TODO: does this make sense? */

	test_assert(timeval_cmp(&e->tv_created_ioloop, &orig->tv_created_ioloop) == 0);
	test_assert(timeval_cmp(&e->tv_created, &orig->tv_created) == 0);
	test_assert(timeval_cmp(&e->tv_last_sent, &orig->tv_last_sent) == 0);

	test_assert(strcmp(e->source_filename, orig->source_filename) == 0);
	test_assert(e->source_linenum == orig->source_linenum);

	/* FIXME: check sending name? */

	cats = event_get_categories(e, &num_cats);
	check_event_diff_cats(cats, num_cats,
			      expected_cats, num_expected_cats);

	fields = event_get_fields(e, &num_fields);
	check_event_diff_fields(fields, num_fields,
				expected_fields, num_expected_fields);

	event_unref(&e);
}

static void check_event_same(struct event *e, struct event *orig)
{
	test_assert(e == orig);

	/* the pointers are the same; nothing can possibly differ */

	event_unref(&e);
}

static void test_event_flatten_no_parent(void)
{
	struct event *e;

	test_begin("event flatten: no parent");

	e = event_create(NULL);

	CHECK_FLATTEN_SAME(e);

	event_add_int(e, "abc", 4);
	CHECK_FLATTEN_SAME(e);

	event_add_int(e, "def", 2);
	CHECK_FLATTEN_SAME(e);

	event_add_str(e, "abc", "foo");
	CHECK_FLATTEN_SAME(e);

	event_add_category(e, &cats[0]);
	CHECK_FLATTEN_SAME(e);

	event_unref(&e);

	test_end();
}

static void test_event_flatten_one_parent(void)
{
	static const char *exp_1cat[] = {
		"cat0",
	};
	static const char *exp_2cat[] = {
		"cat1",
		"cat0",
	};
	static struct event_field exp_int = {
		.key = "abc",
		.value_type = EVENT_FIELD_VALUE_TYPE_INTMAX,
		.value = {
			.str = NULL,
			.intmax = 42,
			.timeval = {0,0},
		}
	};
	static struct event_field exp_2int[2] = {
		{
			.key = "abc",
			.value_type = EVENT_FIELD_VALUE_TYPE_INTMAX,
			.value = {
				.intmax = 42,
				.str = NULL,
				.timeval = {0,0},
			}
		},
		{
			.key = "def",
			.value_type = EVENT_FIELD_VALUE_TYPE_INTMAX,
			.value = {
				.intmax = 49,
				.str = NULL,
				.timeval = {0,0},
			}
		},
	};
	static struct event_field exp_1str1int[2] = {
		{
			.key = "abc",
			.value_type = EVENT_FIELD_VALUE_TYPE_STR,
			.value = {
				.str = "foo",
				.intmax = 0,
				.timeval = {0,0},
			}
		},
		{
			.key = "def",
			.value_type = EVENT_FIELD_VALUE_TYPE_INTMAX,
			.value = {
				.intmax = 49,
				.str = NULL,
				.timeval = {0,0},
			}
		},
	};
	static struct event_field exp_1str1int1strlist[3] = {
		{
			.key = "abc",
			.value_type = EVENT_FIELD_VALUE_TYPE_STR,
			.value = {
				.str = "foo",
				.intmax = 0,
				.timeval = {0,0},
			}
		},
		{
			.key = "def",
			.value_type = EVENT_FIELD_VALUE_TYPE_INTMAX,
			.value = {
				.intmax = 49,
				.str = NULL,
				.timeval = {0,0},
			}
		},
		{
			.key = "cba",
			.value_type = EVENT_FIELD_VALUE_TYPE_STRLIST,
			.value = {
				.str = "one,two,three",
			},
		},
	};

	struct event *parent;
	struct event *e;

	test_begin("event flatten: one parent");

	t_array_init(&exp_1str1int1strlist[0].value.strlist, 3);
	const char *str = "one";
	array_push_back(&exp_1str1int1strlist[0].value.strlist, &str);
	str = "two";
	array_push_back(&exp_1str1int1strlist[0].value.strlist, &str);
	str = "three";
	array_push_back(&exp_1str1int1strlist[0].value.strlist, &str);

	parent = event_create(NULL);

	e = event_create(parent);

	CHECK_FLATTEN_DIFF(e, NULL, 0, NULL, 0);

	event_add_int(e, "abc", 42);
	CHECK_FLATTEN_DIFF(e, NULL, 0, &exp_int, 1);

	event_add_int(e, "def", 49);
	CHECK_FLATTEN_DIFF(e, NULL, 0, exp_2int, 2);

	event_add_str(e, "abc", "foo");
	CHECK_FLATTEN_DIFF(e, NULL, 0, exp_1str1int, 2);

	event_add_category(e, &cats[0]);
	CHECK_FLATTEN_DIFF(e, exp_1cat, 1, exp_1str1int, 2);

	event_add_category(e, &cats[1]);
	CHECK_FLATTEN_DIFF(e, exp_2cat, 2, exp_1str1int, 2);

	event_strlist_append(e, "cba", "one");
	event_strlist_append(e, "cba", "two");
	event_strlist_append(e, "cba", "three");
	CHECK_FLATTEN_DIFF(e, exp_2cat, 2, exp_1str1int1strlist, 3);

	event_unref(&e);
	event_unref(&parent);

	test_end();
}

static void test_event_flatten_override_parent_field(void)
{
	static struct event_field exp_int = {
		.key = "abc",
		.value_type = EVENT_FIELD_VALUE_TYPE_INTMAX,
		.value = {
			.intmax = 42,
			.str = NULL,
			.timeval = {0,0},
		}
	};
	static struct event_field exp_str = {
		.key = "abc",
		.value_type = EVENT_FIELD_VALUE_TYPE_STR,
		.value = {
			.str = "def",
			.intmax = 0,
			.timeval = {0,0},
		}
	};
	static struct event_field exp_2str[2] = {
		{
			.key = "abc",
			.value_type = EVENT_FIELD_VALUE_TYPE_STR,
			.value = {
				.str = "def",
				.intmax = 0,
				.timeval = {0,0},
			}
		},
		{
			.key = "foo",
			.value_type = EVENT_FIELD_VALUE_TYPE_STR,
			.value = {
				.str = "bar",
				.intmax = 0,
				.timeval = {0,0},
			}
		},
	};
	struct event *parent;
	struct event *e;

	test_begin("event flatten: override parent field");

	parent = event_create(NULL);

	event_add_int(parent, "abc", 5);

	e = event_create(parent);

	event_add_int(e, "abc", 42);

	CHECK_FLATTEN_DIFF(e, NULL, 0, &exp_int, 1);

	event_add_str(e, "abc", "def");
	CHECK_FLATTEN_DIFF(e, NULL, 0, &exp_str, 1);

	event_add_str(parent, "foo", "bar");
	CHECK_FLATTEN_DIFF(e, NULL, 0, exp_2str, 2);

	event_unref(&e);
	event_unref(&parent);

	test_end();
}

static void test_event_strlist_flatten(void)
{
	test_begin("event flatten: strlist");
	struct event *l1 = event_create(NULL);
	event_strlist_append(l1, "test", "l3");
	struct event *l2 = event_create(l1);
	event_strlist_append(l2, "test", "l1");
	struct event *l3 = event_create(l2);
	unsigned int line = __LINE__ - 1;
	event_strlist_append(l3, "test", "l2");

	string_t *dest = t_str_new(32);
	struct event *event = event_flatten(l3);

	event_export(event, dest);
	/* see if it matches .. */
	const char *reference = t_strdup_printf("%"PRIdTIME_T"\t%u"
						"\ts"__FILE__
						"\t%u\tLtest\t3\tl3\tl1\tl2",
					event->tv_created.tv_sec,
					(unsigned int)event->tv_created.tv_usec,
					line);
	test_assert_strcmp(str_c(dest), reference);

	/* these should not end up duplicated */
	event_strlist_append(event, "test", "l1");
	event_strlist_append(event, "test", "l2");
	event_strlist_append(event, "test", "l3");

	/* and export should look the same */
	str_truncate(dest, 0);
	event_export(event, dest);
	test_assert_strcmp(str_c(dest), reference);

	event_unref(&event);

	/* export event */
	event_unref(&l3);
	event_unref(&l2);
	event_unref(&l1);

	test_end();
}

void test_event_flatten(void)
{
	test_event_flatten_no_parent();
	test_event_flatten_one_parent();
	test_event_flatten_override_parent_field();
	test_event_strlist_flatten();
}