Skip to content

Commit

Permalink
cacheDB URLs: Do not mandate the "/" URL portion
Browse files Browse the repository at this point in the history
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 #2416

(cherry picked from commit c6baca4)
(cherry picked from commit 50b2dfe)
  • Loading branch information
liviuchircu committed Feb 24, 2021
1 parent 5572fbe commit 4ca36b1
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 46 deletions.
12 changes: 11 additions & 1 deletion cachedb/cachedb_id.c
Expand Up @@ -286,6 +286,14 @@ static int parse_cachedb_url(struct cachedb_id* id, const str* url)
}
}

if (st == ST_PORT) {
if (url->s + i - begin == 0)
goto err;

id->port = str2s(begin, url->s + i - begin, 0);
return 0;
}

if (st == ST_DB) {
if (begin < url->s + len &&
dupl_string(&id->database, begin, url->s + len) < 0) goto err;
Expand All @@ -311,7 +319,9 @@ static int parse_cachedb_url(struct cachedb_id* id, const str* url)
if (id && id->host) pkg_free(id->host);
if (id && id->database) pkg_free(id->database);
if (id && id->extra_options) pkg_free(id->extra_options);
if (prev_token) pkg_free(prev_token);
if (prev_token && prev_token != id->host && prev_token != id->username)
pkg_free(prev_token);

return -1;
}

Expand Down
134 changes: 100 additions & 34 deletions cachedb/test/test_backends.c → cachedb/test/test_cachedb.c
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 OpenSIPS Solutions
* Copyright (C) 2018-2021 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
*
Expand Down Expand Up @@ -27,8 +27,64 @@
#include "../../sr_module.h"
#include "../../modparam.h"

#define CACHEDB_SKIP_BACKEND_TESTS

extern cachedb_engine* lookup_cachedb(str *name);
extern cachedb_con *cachedb_get_connection(cachedb_engine *cde,str *group_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.177: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:test1://10.0.0.178/testcass1.osstest1.osscnttest1") != 0) {
printf("failed to set cassandra url\n");
exit(-1);
}

/* for Cassandra we need a different table schema for the col-oriented ops tests */
if (set_mod_param_regex("cachedb_cassandra", "cachedb_url", STR_PARAM,
"cassandra:test2://10.0.0.178/testcass1.osstest2.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)
{
Expand Down Expand Up @@ -433,38 +489,6 @@ static void test_cachedb_api(const char *cachedb_name, const char *group1,
"column-oriented tests");
}

void init_cachedb_tests(void)
{
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.177: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:test1://10.0.0.178/testcass1.osstest1.osscnttest1") != 0) {
printf("failed to set cassandra url\n");
exit(-1);
}

/* for Cassandra we need a different table schema for the col-oriented ops tests */
if (set_mod_param_regex("cachedb_cassandra", "cachedb_url", STR_PARAM,
"cassandra:test2://10.0.0.178/testcass1.osstest2.osscnttest1") != 0) {
printf("failed to set cassandra url\n");
exit(-1);
}
}

/*
* For Cassandra make sure to create the following tables:
* CREATE TABLE osstest1 (opensipskey text PRIMARY KEY, opensipsval text);
Expand All @@ -478,12 +502,54 @@ void init_cachedb_tests(void)
* );
*/

void test_cachedb_backends(void)
static void test_cachedb_backends(void)
{
#ifdef CACHEDB_SKIP_BACKEND_TESTS
return;
#endif

test_cachedb_api("mongodb", NULL, NULL);
test_cachedb_api("cassandra", "test1", "test2");

// 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(str_match(_str(db->scheme), &str_init("redis")));
ok(str_match(_str(db->group_name), &str_init("group1")));
ok(str_match(_str(db->username), &str_init("")));
ok(str_match(_str(db->password), &str_init("devxxxxxx")));
ok(str_match(_str(db->host), &str_init("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(str_match(_str(db->database), &str_init("d")));
ok(str_match(_str(db->extra_options), &str_init("x=1&q=2")));
}
14 changes: 6 additions & 8 deletions cachedb/test/test_backends.h → cachedb/test/test_cachedb.h
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 OpenSIPS Solutions
* Copyright (C) 2018-2021 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
*
Expand All @@ -18,13 +18,11 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,USA
*/

#ifndef __CACHEDB_TEST_BACKENDS_H__
#define __CACHEDB_TEST_BACKENDS_H__
#ifndef __TEST_CACHEDB_H__
#define __TEST_CACHEDB_H__

/* module requirements */
/* test initialization & execution */
void init_cachedb_tests(void);
void test_cachedb(void);

/* test suites */
void test_cachedb_backends(void);

#endif /* __CACHEDB_TEST_BACKENDS_H__ */
#endif /* __TEST_CACHEDB_H__ */
6 changes: 3 additions & 3 deletions test/unit_tests.c
Expand Up @@ -22,7 +22,7 @@

#include <tap.h>

#include "../cachedb/test/test_backends.h"
#include "../cachedb/test/test_cachedb.h"
#include "../lib/test/test_csv.h"
#include "../parser/test/test_parser.h"
#include "../mem/test/test_malloc.h"
Expand All @@ -42,7 +42,7 @@ void init_unit_tests(void)
if (!strcmp(testing_module, "core")) {
set_mpath("modules/");
solve_module_dependencies(modules);
//init_cachedb_tests();
init_cachedb_tests();
//init_malloc_tests();
}

Expand All @@ -57,8 +57,8 @@ int run_unit_tests(void)

/* core tests */
if (!strcmp(testing_module, "core")) {
//test_cachedb_backends();
//test_malloc();
test_cachedb();
test_lib_csv();
test_parser();

Expand Down

0 comments on commit 4ca36b1

Please sign in to comment.