Skip to content

Commit 4ca36b1

Browse files
committed
cacheDB URLs: Do not mandate the "/" URL portion
Whether the "/" is given or not, semantically, the conclusion is the same: "the name of the database is NULL". Reported by Chris Zhang (@ChrisZhangJin) Fixes OpenSIPS#2416 (cherry picked from commit c6baca4) (cherry picked from commit 50b2dfe)
1 parent 5572fbe commit 4ca36b1

File tree

4 files changed

+120
-46
lines changed

4 files changed

+120
-46
lines changed

cachedb/cachedb_id.c

+11-1
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,14 @@ static int parse_cachedb_url(struct cachedb_id* id, const str* url)
286286
}
287287
}
288288

289+
if (st == ST_PORT) {
290+
if (url->s + i - begin == 0)
291+
goto err;
292+
293+
id->port = str2s(begin, url->s + i - begin, 0);
294+
return 0;
295+
}
296+
289297
if (st == ST_DB) {
290298
if (begin < url->s + len &&
291299
dupl_string(&id->database, begin, url->s + len) < 0) goto err;
@@ -311,7 +319,9 @@ static int parse_cachedb_url(struct cachedb_id* id, const str* url)
311319
if (id && id->host) pkg_free(id->host);
312320
if (id && id->database) pkg_free(id->database);
313321
if (id && id->extra_options) pkg_free(id->extra_options);
314-
if (prev_token) pkg_free(prev_token);
322+
if (prev_token && prev_token != id->host && prev_token != id->username)
323+
pkg_free(prev_token);
324+
315325
return -1;
316326
}
317327

cachedb/test/test_backends.c renamed to cachedb/test/test_cachedb.c

+100-34
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2018 OpenSIPS Solutions
2+
* Copyright (C) 2018-2021 OpenSIPS Solutions
33
*
44
* This file is part of opensips, a free SIP server.
55
*
@@ -27,8 +27,64 @@
2727
#include "../../sr_module.h"
2828
#include "../../modparam.h"
2929

30+
#define CACHEDB_SKIP_BACKEND_TESTS
31+
3032
extern cachedb_engine* lookup_cachedb(str *name);
3133
extern cachedb_con *cachedb_get_connection(cachedb_engine *cde,str *group_name);
34+
static void test_cachedb_backends(void);
35+
static void load_cachedb_modules(void);
36+
static void test_cachedb_url(void);
37+
38+
39+
void init_cachedb_tests(void)
40+
{
41+
load_cachedb_modules();
42+
}
43+
44+
45+
void test_cachedb(void)
46+
{
47+
test_cachedb_url();
48+
test_cachedb_backends();
49+
}
50+
51+
52+
static void load_cachedb_modules(void)
53+
{
54+
#ifdef CACHEDB_SKIP_BACKEND_TESTS
55+
return;
56+
#endif
57+
58+
if (load_module("cachedb_mongodb.so") != 0) {
59+
printf("failed to load mongo\n");
60+
exit(-1);
61+
}
62+
63+
if (load_module("cachedb_cassandra.so") != 0) {
64+
printf("failed to load cassandra\n");
65+
exit(-1);
66+
}
67+
68+
if (set_mod_param_regex("cachedb_mongodb", "cachedb_url", STR_PARAM,
69+
"mongodb://10.0.0.177:27017/OpensipsTests.OpensipsTests") != 0) {
70+
printf("failed to set mongo url\n");
71+
exit(-1);
72+
}
73+
74+
if (set_mod_param_regex("cachedb_cassandra", "cachedb_url", STR_PARAM,
75+
"cassandra:test1://10.0.0.178/testcass1.osstest1.osscnttest1") != 0) {
76+
printf("failed to set cassandra url\n");
77+
exit(-1);
78+
}
79+
80+
/* for Cassandra we need a different table schema for the col-oriented ops tests */
81+
if (set_mod_param_regex("cachedb_cassandra", "cachedb_url", STR_PARAM,
82+
"cassandra:test2://10.0.0.178/testcass1.osstest2.osscnttest1") != 0) {
83+
printf("failed to set cassandra url\n");
84+
exit(-1);
85+
}
86+
}
87+
3288

3389
int res_has_kv(const cdb_res_t *res, const cdb_pair_t *pair)
3490
{
@@ -433,38 +489,6 @@ static void test_cachedb_api(const char *cachedb_name, const char *group1,
433489
"column-oriented tests");
434490
}
435491

436-
void init_cachedb_tests(void)
437-
{
438-
if (load_module("cachedb_mongodb.so") != 0) {
439-
printf("failed to load mongo\n");
440-
exit(-1);
441-
}
442-
443-
if (load_module("cachedb_cassandra.so") != 0) {
444-
printf("failed to load cassandra\n");
445-
exit(-1);
446-
}
447-
448-
if (set_mod_param_regex("cachedb_mongodb", "cachedb_url", STR_PARAM,
449-
"mongodb://10.0.0.177:27017/OpensipsTests.OpensipsTests") != 0) {
450-
printf("failed to set mongo url\n");
451-
exit(-1);
452-
}
453-
454-
if (set_mod_param_regex("cachedb_cassandra", "cachedb_url", STR_PARAM,
455-
"cassandra:test1://10.0.0.178/testcass1.osstest1.osscnttest1") != 0) {
456-
printf("failed to set cassandra url\n");
457-
exit(-1);
458-
}
459-
460-
/* for Cassandra we need a different table schema for the col-oriented ops tests */
461-
if (set_mod_param_regex("cachedb_cassandra", "cachedb_url", STR_PARAM,
462-
"cassandra:test2://10.0.0.178/testcass1.osstest2.osscnttest1") != 0) {
463-
printf("failed to set cassandra url\n");
464-
exit(-1);
465-
}
466-
}
467-
468492
/*
469493
* For Cassandra make sure to create the following tables:
470494
* CREATE TABLE osstest1 (opensipskey text PRIMARY KEY, opensipsval text);
@@ -478,12 +502,54 @@ void init_cachedb_tests(void)
478502
* );
479503
*/
480504

481-
void test_cachedb_backends(void)
505+
static void test_cachedb_backends(void)
482506
{
507+
#ifdef CACHEDB_SKIP_BACKEND_TESTS
508+
return;
509+
#endif
510+
483511
test_cachedb_api("mongodb", NULL, NULL);
484512
test_cachedb_api("cassandra", "test1", "test2");
485513

486514
// todo();
487515
// skip tests here
488516
// end_todo;
489517
}
518+
519+
520+
static void test_cachedb_url(void)
521+
{
522+
struct cachedb_id *db;
523+
524+
/* invalid URLs */
525+
ok(!new_cachedb_id(_str("d:g://@")));
526+
ok(!new_cachedb_id(_str("d:g://u:@")));
527+
ok(!new_cachedb_id(_str("d:g://u:p@")));
528+
ok(!new_cachedb_id(_str("d:g://u:p@h")));
529+
ok(!new_cachedb_id(_str("d:g://u:p@h:")));
530+
531+
db = new_cachedb_id(_str("redis:group1://:[email protected]:6379"));
532+
if (!ok(db != NULL))
533+
return;
534+
ok(str_match(_str(db->scheme), &str_init("redis")));
535+
ok(str_match(_str(db->group_name), &str_init("group1")));
536+
ok(str_match(_str(db->username), &str_init("")));
537+
ok(str_match(_str(db->password), &str_init("devxxxxxx")));
538+
ok(str_match(_str(db->host), &str_init("172.31.180.127")));
539+
ok(db->port == 6379);
540+
ok(!db->database);
541+
ok(!db->extra_options);
542+
543+
db = new_cachedb_id(_str("redis:group1://:[email protected]:6379/"));
544+
if (!ok(db != NULL))
545+
return;
546+
ok(db->port == 6379);
547+
ok(!db->database);
548+
ok(!db->extra_options);
549+
550+
db = new_cachedb_id(_str("redis:group1://:[email protected]:6379/d?x=1&q=2"));
551+
if (!ok(db != NULL))
552+
return;
553+
ok(str_match(_str(db->database), &str_init("d")));
554+
ok(str_match(_str(db->extra_options), &str_init("x=1&q=2")));
555+
}

cachedb/test/test_backends.h renamed to cachedb/test/test_cachedb.h

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2018 OpenSIPS Solutions
2+
* Copyright (C) 2018-2021 OpenSIPS Solutions
33
*
44
* This file is part of opensips, a free SIP server.
55
*
@@ -18,13 +18,11 @@
1818
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,USA
1919
*/
2020

21-
#ifndef __CACHEDB_TEST_BACKENDS_H__
22-
#define __CACHEDB_TEST_BACKENDS_H__
21+
#ifndef __TEST_CACHEDB_H__
22+
#define __TEST_CACHEDB_H__
2323

24-
/* module requirements */
24+
/* test initialization & execution */
2525
void init_cachedb_tests(void);
26+
void test_cachedb(void);
2627

27-
/* test suites */
28-
void test_cachedb_backends(void);
29-
30-
#endif /* __CACHEDB_TEST_BACKENDS_H__ */
28+
#endif /* __TEST_CACHEDB_H__ */

test/unit_tests.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
#include <tap.h>
2424

25-
#include "../cachedb/test/test_backends.h"
25+
#include "../cachedb/test/test_cachedb.h"
2626
#include "../lib/test/test_csv.h"
2727
#include "../parser/test/test_parser.h"
2828
#include "../mem/test/test_malloc.h"
@@ -42,7 +42,7 @@ void init_unit_tests(void)
4242
if (!strcmp(testing_module, "core")) {
4343
set_mpath("modules/");
4444
solve_module_dependencies(modules);
45-
//init_cachedb_tests();
45+
init_cachedb_tests();
4646
//init_malloc_tests();
4747
}
4848

@@ -57,8 +57,8 @@ int run_unit_tests(void)
5757

5858
/* core tests */
5959
if (!strcmp(testing_module, "core")) {
60-
//test_cachedb_backends();
6160
//test_malloc();
61+
test_cachedb();
6262
test_lib_csv();
6363
test_parser();
6464

0 commit comments

Comments
 (0)