summaryrefslogtreecommitdiffstats
path: root/src/lib-index/test-mail-cache-fields.c
blob: 18b6a339b2e9306d7b764d188a294454de472881 (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
/* Copyright (c) 2021 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "test-common.h"
#include "test-mail-cache.h"

static void test_mail_cache_fields_read_write(void)
{
	struct mail_cache_field cache_field = {
		.name = "testfield",
		.type = MAIL_CACHE_FIELD_STRING,
		.decision = MAIL_CACHE_DECISION_NO,
		.last_used = 0x12345678,
	};
	struct mail_cache_field cache_field2 = {
		.name = "testfield2",
		.type = MAIL_CACHE_FIELD_STRING,
		.decision = MAIL_CACHE_DECISION_NO,
		.last_used = 0xaabbccdd,
	};
	struct test_mail_cache_ctx ctx;

	test_begin("mail cache fields read-write");

	test_mail_cache_init(test_mail_index_init(), &ctx);
	mail_cache_register_fields(ctx.cache, &cache_field, 1);
	test_assert(mail_cache_purge(ctx.cache, (uint32_t)-1, "test") == 0);
	/* after writing the initial cache file, register another cache field
	   that doesn't exist in it. */
	mail_cache_register_fields(ctx.cache, &cache_field2, 1);

	struct mail_cache_field_private *priv =
		&ctx.cache->fields[cache_field.idx];
	struct mail_cache_field_private *priv2 =
		&ctx.cache->fields[cache_field2.idx];

	/* No changes */
	test_assert(mail_cache_header_fields_update(ctx.cache) == 0);
	test_assert(mail_cache_header_fields_read(ctx.cache) == 0);
	test_assert(cache_field.last_used == priv->field.last_used &&
		    cache_field.decision == priv->field.decision);
	test_assert(cache_field2.last_used == priv2->field.last_used &&
		    cache_field2.decision == priv2->field.decision);

	/* Replace decision without marking it dirty. Make sure reading
	   overwrites it. Also make sure an old last_used is overwritten. */
	priv->field.decision = MAIL_CACHE_DECISION_YES;
	priv->field.last_used = cache_field.last_used - 1;
	test_assert(mail_cache_header_fields_read(ctx.cache) == 0);
	test_assert(cache_field.last_used == priv->field.last_used &&
		    cache_field.decision == priv->field.decision);
	test_assert(cache_field2.last_used == priv2->field.last_used &&
		    cache_field2.decision == priv2->field.decision);

	/* Replace decision and set it dirty. Make sure reading doesn't
	   overwrite it. Also make sure an old last_used is overwritten. */
	priv->decision_dirty = TRUE;
	priv2->decision_dirty = TRUE;
	priv->field.last_used = cache_field.last_used - 1;
	priv->field.decision = MAIL_CACHE_DECISION_YES;
	cache_field.decision = MAIL_CACHE_DECISION_YES;
	priv2->field.decision = MAIL_CACHE_DECISION_YES;
	cache_field2.decision = MAIL_CACHE_DECISION_YES;
	test_assert(mail_cache_header_fields_read(ctx.cache) == 0);
	test_assert(cache_field.last_used == priv->field.last_used &&
		    cache_field.decision == priv->field.decision);
	test_assert(cache_field2.last_used == priv2->field.last_used &&
		    cache_field2.decision == priv2->field.decision);
	test_assert(priv->decision_dirty);
	test_assert(priv2->decision_dirty);

	/* Make sure a new last_used won't get overwritten by read. */
	priv->field.last_used = ++cache_field.last_used;
	priv2->field.last_used = ++cache_field2.last_used;
	test_assert(mail_cache_header_fields_read(ctx.cache) == 0);
	test_assert(cache_field.last_used == priv->field.last_used &&
		    cache_field.decision == priv->field.decision);
	test_assert(cache_field2.last_used == priv2->field.last_used &&
		    cache_field2.decision == priv2->field.decision);

	/* Write the new decision and last_used. Note that cache_field2
	   isn't written, because it doesn't exist in the cache file. */
	test_assert(mail_cache_header_fields_update(ctx.cache) == 0);
	test_assert(!priv->decision_dirty);
	test_assert(priv2->decision_dirty);
	/* make sure reading reads them back, even if they're changed */
	priv->field.decision = MAIL_CACHE_DECISION_NO;
	priv->field.last_used = 1;
	priv2->field.decision = MAIL_CACHE_DECISION_TEMP;
	priv2->field.last_used = 2;
	cache_field2.decision = MAIL_CACHE_DECISION_TEMP;
	cache_field2.last_used = 2;
	test_assert(mail_cache_header_fields_read(ctx.cache) == 0);
	test_assert(cache_field.last_used == priv->field.last_used &&
		    cache_field.decision == priv->field.decision);
	test_assert(cache_field2.last_used == priv2->field.last_used &&
		    cache_field2.decision == priv2->field.decision);

	test_mail_cache_deinit(&ctx);
	test_mail_index_delete();

	test_end();
}

int main(void)
{
	static void (*const test_functions[])(void) = {
		test_mail_cache_fields_read_write,
		NULL
	};
	return test_run(test_functions);
}