Commit 6c9c2357 authored by Marek Vavruša's avatar Marek Vavruša

tests: extended utils and modules unit tests

parent 582086d3
/* Copyright (C) 2015 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 "lib/module.h"
/*
* Mock module implementation.
*/
int mock_cmodule_init(struct kr_module *module)
{
return kr_ok();
}
int mock_cmodule_deinit(struct kr_module *module)
{
return kr_ok();
}
KR_MODULE_EXPORT(mock_cmodule);
/* Copyright (C) 2015 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 "lib/module.h"
/* Fake libgo */
void runtime_check(void) {}
void runtime_args(int argc, void *argv) {}
void runtime_osinit(void) {}
void runtime_schedinit(void) {}
void __go_init_main() {}
/*
* No module implementation.
*/
/* \note Renamed to mimick Go module. */
extern uint32_t Api(void) __asm__ ("_main.Api");
uint32_t Api(void)
{
return KR_MODULE_API - 1; /* Bad version */
}
\ No newline at end of file
/* Copyright (C) 201 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/module.h"
mm_ctx_t global_mm;
static void test_module_params(void **state)
{
struct kr_module module;
assert_int_equal(kr_module_load(NULL, NULL, NULL), kr_error(EINVAL));
assert_int_equal(kr_module_load(&module, NULL, NULL), kr_error(EINVAL));
kr_module_unload(NULL);
}
static void test_module_builtin(void **state)
{
struct kr_module module;
assert_int_equal(kr_module_load(&module, "iterate", NULL), 0);
kr_module_unload(&module);
}
static void test_module_c(void **state)
{
struct kr_module module;
assert_int_equal(kr_module_load(&module, "mock_cmodule", "tests"), 0);
kr_module_unload(&module);
}
static void test_module_go(void **state)
{
struct kr_module module;
assert_int_equal(kr_module_load(&module, "mock_gomodule", "tests"), kr_error(ENOTSUP));
kr_module_unload(&module);
}
int main(void)
{
test_mm_ctx_init(&global_mm);
const UnitTest tests[] = {
unit_test(test_module_params),
unit_test(test_module_builtin),
unit_test(test_module_c),
unit_test(test_module_go)
};
return run_tests(tests);
}
......@@ -14,6 +14,9 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/socket.h>
#include <stdio.h>
#include "tests/test.h"
#include <cmocka.h>
......@@ -32,6 +35,14 @@ static void test_strcatdup(void **state)
auto_free char *multi_res = kr_strcatdup(3, "need", "beef", "dead");
assert_string_equal(multi_res, "needbeefdead");
/* Test fails if this leaks. */
auto_fclose FILE* null_file = fopen("/dev/null", "r");
(void)(null_file);
/* Test fails if this leaks. */
auto_close int null_sock = socket(AF_INET, SOCK_DGRAM, 0);
(void)(null_sock);
}
int main(void)
......
......@@ -4,13 +4,20 @@
tests_BIN := \
test_utils \
test_module \
test_context \
test_rplan \
test_cache \
test_resolve
# Mock modules
mock_cmodule_SOURCES := tests/mock_cmodule.c
$(eval $(call make_lib,mock_cmodule,tests))
mock_gomodule_SOURCES := tests/mock_gomodule.c
$(eval $(call make_lib,mock_gomodule,tests))
# Dependencies
tests_DEPEND := libkresolve cmocka
tests_DEPEND := libkresolve cmocka mock_cmodule mock_gomodule
tests_LIBS := $(libkresolve_TARGET) $(libkresolve_LIBS) $(cmocka_LIBS)
# Make test binaries
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment