-
Notifications
You must be signed in to change notification settings - Fork 564
/
test_cachedb.c
468 lines (367 loc) · 12.8 KB
/
test_cachedb.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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
* Copyright (C) 2018-2021 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,USA
*/
#include <tap.h>
#include "../../str.h"
#include "../../cachedb/cachedb.h"
#include "../../cachedb/cachedb_cap.h"
#include "../../lib/osips_malloc.h"
#include "../../sr_module.h"
#include "../../modparam.h"
#define CACHEDB_SKIP_BACKEND_TESTS
extern cachedb_engine* lookup_cachedb(str *name);
static void test_cachedb_backends(void);
static void load_cachedb_modules(void);
static void test_cachedb_url(void);
void init_cachedb_tests(void)
{
load_cachedb_modules();
}
void test_cachedb(void)
{
test_cachedb_url();
test_cachedb_backends();
}
static void load_cachedb_modules(void)
{
#ifdef CACHEDB_SKIP_BACKEND_TESTS
return;
#endif
if (load_module("cachedb_mongodb.so") != 0) {
printf("failed to load mongo\n");
exit(-1);
}
if (load_module("cachedb_cassandra.so") != 0) {
printf("failed to load cassandra\n");
exit(-1);
}
if (set_mod_param_regex("cachedb_mongodb", "cachedb_url", STR_PARAM,
"mongodb://10.0.0.4:27017/OpensipsTests.OpensipsTests") != 0) {
printf("failed to set mongo url\n");
exit(-1);
}
if (set_mod_param_regex("cachedb_cassandra", "cachedb_url", STR_PARAM,
"cassandra://10.0.0.178/testcass1.osstest1.osscnttest1") != 0) {
printf("failed to set cassandra url\n");
exit(-1);
}
}
int res_has_kv(const cdb_res_t *res, const cdb_pair_t *pair)
{
struct list_head *_;
cdb_row_t *row;
list_for_each (_, &res->rows) {
row = list_entry(_, cdb_row_t, list);
if (cdb_dict_has_pair(&row->dict, pair))
return 1;
}
return 0;
}
static inline cdb_dict_t *nth_dict(const cdb_res_t *res, int nth)
{
struct list_head *_;
cdb_row_t *row;
list_for_each (_, &res->rows) {
if (--nth == 0) {
row = list_entry(_, cdb_row_t, list);
return &row->dict;
}
}
return NULL;
}
static int test_query_filters(cachedb_funcs *api, cachedb_con *con)
{
cdb_key_t key;
str sa = str_init("A"), sb = str_init("B"), sc = str_init("C"),
sd = str_init("D");
int_str_t isv;
cdb_filter_t *filter;
cdb_res_t res;
cdb_pair_t pair;
memset(&pair, 0, sizeof pair);
init_str(&key.name, "tgr_1");
ok(api->set(con, &key.name, &sa, 0) == 0, "test_query: set A");
init_str(&key.name, "tgr_2");
ok(api->set(con, &key.name, &sb, 0) == 0, "test_query: set B");
init_str(&key.name, "tgr_3");
ok(api->set(con, &key.name, &sc, 0) == 0, "test_query: set C");
init_str(&key.name, "tgr_4");
ok(api->set(con, &key.name, &sd, 0) == 0, "test_query: set D");
memset(&key, 0, sizeof key);
init_str(&key.name, "opensips");
isv.is_str = 1;
isv.s = sd;
pair.val.type = CDB_STR;
/* single filter tests */
filter = cdb_append_filter(NULL, &key, CDB_OP_LTE, &isv);
ok(api->query(con, filter, &res) == 0, "test_query: get 4 items");
ok(res.count == 4, "test_query: have 4 items");
init_str(&pair.key.name, "opensips");
pair.val.val.st = sa; ok(res_has_kv(&res, &pair), "has A");
pair.val.val.st = sb; ok(res_has_kv(&res, &pair), "has B");
pair.val.val.st = sc; ok(res_has_kv(&res, &pair), "has C");
pair.val.val.st = sd; ok(res_has_kv(&res, &pair), "has D");
init_str(&pair.val.val.st, "Z");
ok(!res_has_kv(&res, &pair), "!has Z");
cdb_free_rows(&res);
cdb_free_filters(filter);
filter = cdb_append_filter(NULL, &key, CDB_OP_LT, &isv);
ok(api->query(con, filter, &res) == 0, "test_query: get 3 items");
ok(res.count == 3, "test_query: have 3 items");
pair.val.val.st = sa; ok(res_has_kv(&res, &pair), "has A");
pair.val.val.st = sb; ok(res_has_kv(&res, &pair), "has B");
pair.val.val.st = sc; ok(res_has_kv(&res, &pair), "has C");
pair.val.val.st = sd; ok(!res_has_kv(&res, &pair), "!has D");
cdb_free_rows(&res);
cdb_free_filters(filter);
init_str(&isv.s, "A");
filter = cdb_append_filter(NULL, &key, CDB_OP_LT, &isv);
ok(api->query(con, filter, &res) == 0, "test_query: get 0 items");
ok(res.count == 0, "test_query: have 0 items");
pair.val.val.st = sa; ok(!res_has_kv(&res, &pair), "!has A");
pair.val.val.st = sc; ok(!res_has_kv(&res, &pair), "!has C");
cdb_free_rows(&res);
cdb_free_filters(filter);
filter = cdb_append_filter(NULL, &key, CDB_OP_LTE, &isv);
ok(api->query(con, filter, &res) == 0, "test_query: get 1 item");
ok(res.count == 1, "test_query: have 1 item");
pair.val.val.st = sa; ok(res_has_kv(&res, &pair), "has A");
pair.val.val.st = sb; ok(!res_has_kv(&res, &pair), "!has B");
cdb_free_rows(&res);
cdb_free_filters(filter);
/* multi filter tests */
init_str(&isv.s, "D");
filter = cdb_append_filter(NULL, &key, CDB_OP_LT, &isv);
ok(api->query(con, filter, &res) == 0, "test_query: get 3 items");
ok(res.count == 3, "test_query: have 3 items");
init_str(&isv.s, "A");
filter = cdb_append_filter(filter, &key, CDB_OP_GTE, &isv);
ok(api->query(con, filter, &res) == 0, "test_query: get 3 items");
ok(res.count == 3, "test_query: have 3 items");
init_str(&isv.s, "B");
filter = cdb_append_filter(filter, &key, CDB_OP_GT, &isv);
ok(api->query(con, filter, &res) == 0, "test_query: get 1 item");
ok(res.count == 1, "test_query: have 1 item");
init_str(&isv.s, "C");
filter = cdb_append_filter(filter, &key, CDB_OP_EQ, &isv);
ok(api->query(con, filter, &res) == 0, "test_query: get 1 item");
ok(res.count == 1, "test_query: have 1 item");
cdb_free_rows(&res);
cdb_free_filters(filter);
return 1;
}
static int test_query(cachedb_funcs *api, cachedb_con *con,
const cdb_dict_t *pairs)
{
cdb_res_t res;
if (!ok(api->query(con, NULL, &res) == 0, "query: NULL filter"))
return 0;
ok(res.count == 2, "query: 2 results");
dbg_cdb_dict("pairs: ", pairs);
dbg_cdb_dict("res 1: ", nth_dict(&res, 1));
dbg_cdb_dict("res 2: ", nth_dict(&res, 2));
ok(!dict_cmp(nth_dict(&res, 1), nth_dict(&res, 2)), "identical results");
cdb_free_rows(&res);
return 1;
}
static int test_update(cachedb_funcs *api, cachedb_con *con,
cdb_dict_t *out_pairs)
{
cdb_filter_t *filter;
int_str_t isv;
cdb_key_t key;
str subkey;
cdb_pair_t *pair, *dict_pair;
cdb_pkey_init(&key, "aor");
init_str(&isv.s, "foo@opensips.org"); isv.is_str = 1;
filter = cdb_append_filter(NULL, &key, CDB_OP_EQ, &isv);
cdb_dict_init(out_pairs);
cdb_key_init(&key, "key"); init_str(&subkey, "subkey-null");
pair = cdb_mk_pair(&key, &subkey);
pair->val.type = CDB_NULL;
cdb_dict_add(pair, out_pairs);
cdb_key_init(&key, "key"); init_str(&subkey, "subkey-32bit");
pair = cdb_mk_pair(&key, &subkey);
pair->val.type = CDB_INT32;
pair->val.val.i32 = 2147483647;
cdb_dict_add(pair, out_pairs);
cdb_key_init(&key, "key"); init_str(&subkey, "subkey-64bit");
pair = cdb_mk_pair(&key, &subkey);
pair->val.type = CDB_INT64;
pair->val.val.i64 = 9223372036854775807;
cdb_dict_add(pair, out_pairs);
cdb_key_init(&key, "key"); init_str(&subkey, "subkey-str");
pair = cdb_mk_pair(&key, &subkey);
pair->val.type = CDB_STR;
init_str(&pair->val.val.st, pkg_strdup("31337"));
cdb_dict_add(pair, out_pairs);
/* set a dict subkey which contains a dict */
cdb_key_init(&key, "key"); init_str(&subkey, "subkey-dict");
dict_pair = cdb_mk_pair(&key, &subkey);
dict_pair->val.type = CDB_DICT;
cdb_dict_init(&dict_pair->val.val.dict);
cdb_key_init(&key, "foo");
pair = cdb_mk_pair(&key, NULL);
pair->val.type = CDB_INT32;
pair->val.val.i32 = 373333337;
cdb_dict_add(pair, &dict_pair->val.val.dict);
cdb_key_init(&key, "bar");
pair = cdb_mk_pair(&key, NULL);
pair->val.type = CDB_STR;
init_str(&pair->val.val.st, pkg_strdup("hello, world!"));
cdb_dict_add(pair, &dict_pair->val.val.dict);
cdb_dict_add(dict_pair, out_pairs);
ok(api->update(con, filter, out_pairs) == 0, "test_update #1");
cdb_free_filters(filter);
cdb_pkey_init(&key, "aor");
init_str(&isv.s, "bar@opensips.org");
filter = cdb_append_filter(NULL, &key, CDB_OP_EQ, &isv);
ok(api->update(con, filter, out_pairs) == 0, "test_update #2");
cdb_free_filters(filter);
return 1;
}
static int test_update_unset(cachedb_funcs *api, cachedb_con *con,
cdb_dict_t *out_pairs)
{
struct list_head *_;
cdb_pair_t *pair;
list_for_each (_, out_pairs) {
pair = list_entry(_, cdb_pair_t, list);
pair->unset = 1;
}
ok(api->update(con, NULL, out_pairs) == 0, "test_update_unset ALL");
return 1;
}
static int test_query_unset(cachedb_funcs *api, cachedb_con *con,
const cdb_dict_t *pairs)
{
cdb_res_t res;
cdb_dict_t *dict1, *dict2;
cdb_pair_t *pair;
cdb_key_t key = CDB_KEY_INITIALIZER;
if (!ok(api->query(con, NULL, &res) == 0, "query: NULL filter"))
return 0;
ok(res.count == 2, "query: 2 results");
dbg_cdb_dict("pairs: ", pairs);
dict1 = nth_dict(&res, 1);
dict2 = nth_dict(&res, 2);
dbg_cdb_dict("res 1: ", dict1);
dbg_cdb_dict("res 2: ", dict2);
init_str(&key.name, "key");
pair = cdb_dict_fetch(&key, dict1);
ok(!pair ||
(pair->val.type == CDB_DICT && cdb_dict_empty(&pair->val.val.dict)),
"subdict-1 is empty");
pair = cdb_dict_fetch(&key, dict2);
ok(!pair ||
(pair->val.type == CDB_DICT && cdb_dict_empty(&pair->val.val.dict)),
"subdict-2 is empty");
ok(nth_pair(dict1, 3) == NULL, "dict1 has 2 entries");
ok(nth_pair(dict2, 3) == NULL, "dict2 has 2 entries");
ok(!dict_cmp(dict1, dict2), "identical results");
cdb_free_rows(&res);
return 1;
}
static int test_column_ops(cachedb_funcs *api, cachedb_con *con)
{
cdb_dict_t cols;
if (CACHEDB_CAPABILITY(api, CACHEDB_CAP_TRUNCATE))
ok(api->truncate(con) == 0, "truncate");
if (!ok(test_query_filters(api, con), "test query filters"))
return 0;
if (CACHEDB_CAPABILITY(api, CACHEDB_CAP_TRUNCATE))
ok(api->truncate(con) == 0, "truncate");
if (!ok(test_update(api, con, &cols), "test update-set")
|| !ok(test_query(api, con, &cols), "test query-set")
|| !ok(test_update_unset(api, con, &cols), "test update-unset")
|| !ok(test_query_unset(api, con, &cols), "test query-unset"))
return 0;
cdb_free_entries(&cols, osips_pkg_free);
if (CACHEDB_CAPABILITY(api, CACHEDB_CAP_TRUNCATE))
ok(api->truncate(con) == 0, "truncate");
return 1;
}
static void test_cachedb_api(const char *cachedb_name)
{
str key = str_init("foo"),
val = str_init("bar"), cdb_str;
cachedb_engine *cde;
cachedb_con *con;
init_str(&cdb_str, cachedb_name);
/* high-level cachedb API (called by script, MI) */
/* TODO: write tests for signed integers (may be broken in current Mongo) */
ok(cachedb_store(&cdb_str, &key, &val, 0) == 1, "store key");
memset(&val, 0, sizeof val);
ok(cachedb_fetch(&cdb_str, &key, &val) == 1, "fetch key");
cmp_mem(val.s, "bar", 3, "check fetched value");
ok(cachedb_remove(&cdb_str, &key) == 1, "remove key");
ok(cachedb_fetch(&cdb_str, &key, &val) == -2, "fetch non-existing key");
/* low-level cachedb API (called by core and modules) */
cde = lookup_cachedb(&cdb_str);
if (!ok(cde != NULL, "have cachedb engine"))
return;
con = cde->default_connection;
if (!ok(con != NULL, "engine has con"))
return;
if (CACHEDB_CAPABILITY(&cde->cdb_func, CACHEDB_CAP_COL_ORIENTED))
ok(test_column_ops(&cde->cdb_func, con), "column-oriented tests");
}
static void test_cachedb_backends(void)
{
#ifdef CACHEDB_SKIP_BACKEND_TESTS
return;
#endif
test_cachedb_api("mongodb");
test_cachedb_api("cassandra");
// todo();
// skip tests here
// end_todo;
}
static void test_cachedb_url(void)
{
struct cachedb_id *db;
/* invalid URLs */
ok(!new_cachedb_id(_str("d:g://@")));
ok(!new_cachedb_id(_str("d:g://u:@")));
ok(!new_cachedb_id(_str("d:g://u:p@")));
ok(!new_cachedb_id(_str("d:g://u:p@h")));
ok(!new_cachedb_id(_str("d:g://u:p@h:")));
db = new_cachedb_id(_str("redis:group1://:devxxxxxx@172.31.180.127:6379"));
if (!ok(db != NULL))
return;
ok(!strcmp(db->scheme, "redis"));
ok(!strcmp(db->group_name, "group1"));
ok(!strcmp(db->username, ""));
ok(!strcmp(db->password, "devxxxxxx"));
ok(!strcmp(db->host, "172.31.180.127"));
ok(db->port == 6379);
ok(!db->database);
ok(!db->extra_options);
db = new_cachedb_id(_str("redis:group1://:devxxxxxx@172.31.180.127:6379/"));
if (!ok(db != NULL))
return;
ok(db->port == 6379);
ok(!db->database);
ok(!db->extra_options);
db = new_cachedb_id(_str("redis:group1://:devxxxxxx@172.31.180.127:6379/d?x=1&q=2"));
if (!ok(db != NULL))
return;
ok(!strcmp(db->database, "d"));
ok(!strcmp(db->extra_options, "x=1&q=2"));
}