Skip to content
Snippets Groups Projects
Commit 9751c32f authored by Marek Vavruša's avatar Marek Vavruša
Browse files

tests: unit tests for rplan, param checks for context

parent 0399b864
No related branches found
No related tags found
No related merge requests found
......@@ -37,9 +37,12 @@ lib/zonecut.h
tests/Makefile.am
tests/pydnstest/__init__.py
tests/pydnstest/scenario.py
tests/pydnstest/test.py
tests/pydnstest/testserver.py
tests/test.h
tests/test_cache.c
tests/test_context.c
tests/test_integration.c
tests/test_integration.py
tests/test_resolve.c
tests/test_rplan.c
......@@ -33,6 +33,10 @@
static struct kr_query *query_create(mm_ctx_t *pool, const knot_dname_t *name)
{
if (name == NULL) {
return NULL;
}
struct kr_query *qry = mm_alloc(pool, sizeof(struct kr_query));
if (qry == NULL) {
return NULL;
......@@ -54,18 +58,27 @@ static void query_free(mm_ctx_t *pool, struct kr_query *qry)
mm_free(pool, qry);
}
void kr_rplan_init(struct kr_rplan *rplan, struct kr_context *context, mm_ctx_t *pool)
int kr_rplan_init(struct kr_rplan *rplan, struct kr_context *context, mm_ctx_t *pool)
{
if (rplan == NULL) {
return KNOT_EINVAL;
}
memset(rplan, 0, sizeof(struct kr_rplan));
rplan->pool = pool;
rplan->context = context;
init_list(&rplan->pending);
init_list(&rplan->resolved);
return KNOT_EOK;
}
void kr_rplan_deinit(struct kr_rplan *rplan)
{
if (rplan == NULL) {
return;
}
struct kr_query *qry = NULL, *next = NULL;
WALK_LIST_DELSAFE(qry, next, rplan->pending) {
query_free(rplan->pool, qry);
......@@ -84,12 +97,19 @@ void kr_rplan_deinit(struct kr_rplan *rplan)
bool kr_rplan_empty(struct kr_rplan *rplan)
{
if (rplan == NULL) {
return true;
}
return EMPTY_LIST(rplan->pending);
}
struct kr_query *kr_rplan_push(struct kr_rplan *rplan, struct kr_query *parent,
const knot_dname_t *name, uint16_t cls, uint16_t type)
{
if (rplan == NULL) {
return NULL;
}
struct kr_query *qry = query_create(rplan->pool, name);
if (qry == NULL) {
return NULL;
......@@ -119,6 +139,10 @@ struct kr_query *kr_rplan_push(struct kr_rplan *rplan, struct kr_query *parent,
int kr_rplan_pop(struct kr_rplan *rplan, struct kr_query *qry)
{
if (rplan == NULL || qry == NULL) {
return KNOT_EINVAL;
}
rem_node(&qry->node);
add_tail(&rplan->resolved, &qry->node);
return KNOT_EOK;
......@@ -126,15 +150,16 @@ int kr_rplan_pop(struct kr_rplan *rplan, struct kr_query *qry)
struct kr_query *kr_rplan_current(struct kr_rplan *rplan)
{
if (EMPTY_LIST(rplan->pending)) {
if (rplan == NULL || EMPTY_LIST(rplan->pending)) {
return NULL;
}
return TAIL(rplan->pending);
}
namedb_txn_t *kr_rplan_txn_acquire(struct kr_rplan *rplan, unsigned flags)
{
if (rplan == NULL) {
if (rplan == NULL || rplan->context == NULL) {
return NULL;
}
......@@ -162,7 +187,7 @@ namedb_txn_t *kr_rplan_txn_acquire(struct kr_rplan *rplan, unsigned flags)
int kr_rplan_txn_commit(struct kr_rplan *rplan)
{
if (rplan == NULL) {
if (rplan == NULL || rplan->context == NULL) {
return KNOT_EINVAL;
}
......
......@@ -67,7 +67,7 @@ struct kr_rplan {
* \param context resolution context
* \param pool ephemeral memory pool for whole resolution
*/
void kr_rplan_init(struct kr_rplan *rplan, struct kr_context *context, mm_ctx_t *pool);
int kr_rplan_init(struct kr_rplan *rplan, struct kr_context *context, mm_ctx_t *pool);
/*!
* \brief Deinitialize resolution plan, aborting any uncommited transactions.
......
......@@ -13,8 +13,9 @@ LDADD = \
if BUILD_TESTS
check_PROGRAMS = \
test_cache \
test_context \
test_rplan \
test_cache \
test_resolve
check-compile-only: $(check_PROGRAMS)
......
......@@ -22,27 +22,32 @@
mm_ctx_t global_mm;
static struct kr_context global_context;
/* Create resolution context */
static void text_context_init(void **state)
static void test_context_init(void **state)
{
int ret = kr_context_init(&global_context, &global_mm);
assert_int_equal(ret, KNOT_EOK);
*state = &global_context;
}
/* Delete it */
static void text_context_deinit(void **state)
static void test_context_deinit(void **state)
{
int ret = kr_context_deinit(*state);
assert_int_equal(ret, KNOT_EOK);
}
static void test_context_params(void **state)
{
assert_int_equal(kr_context_init(NULL, NULL), KNOT_EINVAL);
assert_int_equal(kr_context_deinit(NULL), KNOT_EINVAL);
}
int main(void)
{
test_mm_ctx_init(&global_mm);
const UnitTest tests[] = {
unit_test_teardown(text_context_init, text_context_deinit),
unit_test(test_context_params),
unit_test_teardown(test_context_init, test_context_deinit),
};
return run_tests(tests);
......
/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program 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 3 of the License, or
(at your option) any later version.
This program 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, see <http://www.gnu.org/licenses/>.
*/
#include "tests/test.h"
#include <cmocka.h>
#include "lib/rplan.h"
static void test_rplan_params(void **state)
{
/* NULL rplan */
assert_int_equal(kr_rplan_init(NULL, NULL, NULL), KNOT_EINVAL);
assert_null(kr_rplan_push(NULL, NULL, NULL, 0, 0));
assert_int_equal(kr_rplan_pop(NULL, NULL), KNOT_EINVAL);
assert_true(kr_rplan_empty(NULL));
assert_null(kr_rplan_current(NULL));
assert_null(kr_rplan_txn_acquire(NULL, 0));
assert_int_equal(kr_rplan_txn_commit(NULL), KNOT_EINVAL);
kr_rplan_deinit(NULL);
/* NULL mandatory parameters */
struct kr_rplan rplan;
assert_int_equal(kr_rplan_init(&rplan, NULL, NULL), KNOT_EOK);
assert_null(kr_rplan_push(&rplan, NULL, NULL, 0, 0));
assert_int_equal(kr_rplan_pop(&rplan, NULL), KNOT_EINVAL);
assert_true(kr_rplan_empty(&rplan));
assert_null(kr_rplan_current(&rplan));
assert_null(kr_rplan_txn_acquire(&rplan, 0));
assert_int_equal(kr_rplan_txn_commit(&rplan), KNOT_EINVAL);
kr_rplan_deinit(&rplan);
}
int main(void)
{
const UnitTest tests[] = {
unit_test(test_rplan_params)
};
return run_tests(tests);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment