summaryrefslogtreecommitdiffstats
path: root/src/lib-dict-backend/test-dict-sql.c
blob: 4de45eb5c1802b660323a0196af074f42dc2b374 (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
/* Copyright (c) 2017-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "test-lib.h"
#include "sql-api.h"
#include "dict.h"
#include "dict-private.h"
#include "dict-sql.h"
#include "dict-sql-private.h"
#include "driver-test.h"

struct dict_op_settings dict_op_settings = {
	.username = "testuser",
};

static void test_setup(struct dict **dict_r)
{
	const char *error = NULL;
	struct dict_settings set = {
		.base_dir = "."
	};
	struct dict *dict = NULL;

	if (dict_init("mysql:" DICT_SRC_DIR "/dict.conf", &set, &dict, &error) < 0)
		i_fatal("cannot initialize dict: %s", error);

	*dict_r = dict;
}

static void test_teardown(struct dict **_dict)
{
	struct dict *dict = *_dict;
	*_dict = NULL;
	if (dict != NULL) {
		dict_deinit(&dict);
	}
}

static void test_set_expected(struct dict *_dict,
			      const struct test_driver_result *result)
{
	struct sql_dict *dict =
		(struct sql_dict *)_dict;

	sql_driver_test_add_expected_result(dict->db, result);
}

static void test_lookup_one(void)
{
	const char *value = NULL, *error = NULL;
	struct test_driver_result_set rset = {
		.rows = 1,
		.cols = 1,
		.col_names = (const char *[]){"value", NULL},
		.row_data = (const char **[]){(const char*[]){"one", NULL}},
	};
	struct test_driver_result res = {
		.nqueries = 1,
		.queries = (const char *[]){"SELECT value FROM table WHERE a = 'hello' AND b = 'world'", NULL},
		.result = &rset,
	};
	const struct dict_op_settings set = {
		.username = "testuser",
	};
	struct dict *dict;
	pool_t pool = pool_datastack_create();

	test_begin("dict lookup one");
	test_setup(&dict);

	test_set_expected(dict, &res);

	test_assert(dict_lookup(dict, &set, pool, "shared/dictmap/hello/world", &value, &error) == 1);
	test_assert_strcmp(value, "one");
        if (error != NULL)
                i_error("dict_lookup failed: %s", error);
	test_teardown(&dict);
	test_end();
}

static void test_atomic_inc(void)
{
	const char *error;
	struct test_driver_result res = {
		.nqueries = 3,
		.queries = (const char *[]){
			"UPDATE counters SET value=value+128 WHERE class = 'global' AND name = 'counter'",
			"UPDATE quota SET bytes=bytes+128,count=count+1 WHERE username = 'testuser'",
			"UPDATE quota SET bytes=bytes+128,count=count+1,folders=folders+123 WHERE username = 'testuser'",
			NULL},
		.result = NULL,
	};
	struct dict_op_settings set = {
		.username = "testuser",
	};
	struct dict *dict;

	test_begin("dict atomic inc");
	test_setup(&dict);

	test_set_expected(dict, &res);

	/* 1 field */
	struct dict_transaction_context *ctx = dict_transaction_begin(dict, &set);
	dict_atomic_inc(ctx, "shared/counters/global/counter", 128);
	test_assert(dict_transaction_commit(&ctx, &error) == 0);
        if (error != NULL)
                i_error("dict_transaction_commit failed: %s", error);
	error = NULL;

	/* 2 fields */
	ctx = dict_transaction_begin(dict, &set);
	dict_atomic_inc(ctx, "priv/quota/bytes", 128);
	dict_atomic_inc(ctx, "priv/quota/count", 1);
	test_assert(dict_transaction_commit(&ctx, &error) == 0);
        if (error != NULL)
		i_error("dict_transaction_commit failed: %s", error);
	error = NULL;

	/* 3 fields */
	ctx = dict_transaction_begin(dict, &set);
	dict_atomic_inc(ctx, "priv/quota/bytes", 128);
	dict_atomic_inc(ctx, "priv/quota/count", 1);
	dict_atomic_inc(ctx, "priv/quota/folders", 123);
	test_assert(dict_transaction_commit(&ctx, &error) == 0);
        if (error != NULL)
                i_error("dict_transaction_commit failed: %s", error);
	test_teardown(&dict);
	test_end();
}

static void test_set(void)
{
	const char *error;
	struct test_driver_result res = {
		.affected_rows = 1,
		.nqueries = 3,
		.queries = (const char *[]){
			"INSERT INTO counters (value,class,name) VALUES (128,'global','counter') ON DUPLICATE KEY UPDATE value=128",
			"INSERT INTO quota (bytes,count,username) VALUES (128,1,'testuser') ON DUPLICATE KEY UPDATE bytes=128,count=1",
			"INSERT INTO quota (bytes,count,folders,username) VALUES (128,1,123,'testuser') ON DUPLICATE KEY UPDATE bytes=128,count=1,folders=123",
			NULL},
		.result = NULL,
	};
	struct dict *dict;

	test_begin("dict set");
	test_setup(&dict);

	test_set_expected(dict, &res);

	/* 1 field */
	struct dict_transaction_context *ctx = dict_transaction_begin(dict, &dict_op_settings);
	dict_set(ctx, "shared/counters/global/counter", "128");
	test_assert(dict_transaction_commit(&ctx, &error) == 1);
        if (error != NULL)
                i_error("dict_transaction_commit failed: %s", error);
	error = NULL;

	/* 2 fields */
	ctx = dict_transaction_begin(dict, &dict_op_settings);
	dict_set(ctx, "priv/quota/bytes", "128");
	dict_set(ctx, "priv/quota/count", "1");
	test_assert(dict_transaction_commit(&ctx, &error) == 1);
        if (error != NULL)
                i_error("dict_transaction_commit failed: %s", error);
	error = NULL;

	/* 3 fields */
	ctx = dict_transaction_begin(dict, &dict_op_settings);
	dict_set(ctx, "priv/quota/bytes", "128");
	dict_set(ctx, "priv/quota/count", "1");
	dict_set(ctx, "priv/quota/folders", "123");
	test_assert(dict_transaction_commit(&ctx, &error) == 1);
        if (error != NULL)
                i_error("dict_transaction_commit failed: %s", error);
	test_teardown(&dict);
	test_end();
}

static void test_unset(void)
{
	const char *error;
	struct test_driver_result res = {
		.affected_rows = 1,
		.nqueries = 3,
		.queries = (const char *[]){
			"DELETE FROM counters WHERE class = 'global' AND name = 'counter'",
			"DELETE FROM quota WHERE username = 'testuser'",
			"DELETE FROM quota WHERE username = 'testuser'",
			NULL},
		.result = NULL,
	};
	struct dict *dict;

	test_begin("dict unset");
	test_setup(&dict);

	test_set_expected(dict, &res);

	struct dict_transaction_context *ctx = dict_transaction_begin(dict, &dict_op_settings);
	dict_unset(ctx, "shared/counters/global/counter");
	test_assert(dict_transaction_commit(&ctx, &error) == 1);
	if (error != NULL)
                i_error("dict_transaction_commit failed: %s", error);
	error = NULL;
	ctx = dict_transaction_begin(dict, &dict_op_settings);
	dict_unset(ctx, "priv/quota/bytes");
	dict_unset(ctx, "priv/quota/count");
	test_assert(dict_transaction_commit(&ctx, &error) == 1);
        if (error != NULL)
                i_error("dict_transaction_commit failed: %s", error);
	test_teardown(&dict);
	test_end();
}

static void test_iterate(void)
{
	const char *key = NULL, *value = NULL, *error;
	struct test_driver_result_set rset = {
		.rows = 5,
		.cols = 2,
		.col_names = (const char *[]){"value", "name", NULL},
		.row_data = (const char **[]){
			(const char*[]){"one", "counter", NULL},
			(const char*[]){"two", "counter", NULL},
			(const char*[]){"three", "counter", NULL},
			(const char*[]){"four", "counter", NULL},
			(const char*[]){"five", "counter", NULL},
		},
	};
	struct test_driver_result res = {
		.nqueries = 1,
		.queries = (const char *[]){
			"SELECT value,name FROM counters WHERE class = 'global' AND name = 'counter'",
			NULL},
		.result = &rset,
	};
	struct dict *dict;

	test_begin("dict iterate");
	test_setup(&dict);

	test_set_expected(dict, &res);

	struct dict_iterate_context *iter =
		dict_iterate_init(dict, &dict_op_settings, "shared/counters/global/counter",
				  DICT_ITERATE_FLAG_EXACT_KEY);

	size_t idx = 0;
	while(dict_iterate(iter, &key, &value)) {
		i_assert(idx < rset.rows);
		test_assert_strcmp_idx(key, "shared/counters/global/counter", idx);
		test_assert_strcmp_idx(value, rset.row_data[idx][0], idx);
		idx++;
	}

	test_assert(idx == rset.rows);
	test_assert(dict_iterate_deinit(&iter, &error) == 0);
        if (error != NULL)
                i_error("dict_iterate_deinit failed: %s", error);
	error = NULL;

	res.queries = (const char*[]){
		"SELECT value,name FROM counters WHERE class = 'global' AND name LIKE '%' AND name NOT LIKE '%/%'",
		NULL
	};

	res.cur = 0;
	res.result->cur = 0;

	test_set_expected(dict, &res);

	iter = dict_iterate_init(dict, &dict_op_settings, "shared/counters/global/", 0);

	idx = 0;

	while(dict_iterate(iter, &key, &value)) {
		i_assert(idx < rset.rows);
		test_assert_strcmp_idx(key, "shared/counters/global/counter", idx);
		test_assert_strcmp_idx(value, rset.row_data[idx][0], idx);
		idx++;
	}

	test_assert(idx == rset.rows);
	test_assert(dict_iterate_deinit(&iter, &error) == 0);
	if (error != NULL)
		i_error("dict_iterate_deinit failed: %s", error);
	test_teardown(&dict);
	test_end();
}

int main(void) {
	sql_drivers_init();
	sql_driver_test_register();
	dict_sql_register();

	static void (*const test_functions[])(void) = {
		test_lookup_one,
		test_atomic_inc,
		test_set,
		test_unset,
		test_iterate,
		NULL
	};

	int ret = test_run(test_functions);

	dict_sql_unregister();
	sql_driver_test_unregister();
	sql_drivers_deinit();

	return ret;
}