summaryrefslogtreecommitdiffstats
path: root/tests/unit
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 15:26:00 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 15:26:00 +0000
commit830407e88f9d40d954356c3754f2647f91d5c06a (patch)
treed6a0ece6feea91f3c656166dbaa884ef8a29740e /tests/unit
parentInitial commit. (diff)
downloadknot-resolver-830407e88f9d40d954356c3754f2647f91d5c06a.tar.xz
knot-resolver-830407e88f9d40d954356c3754f2647f91d5c06a.zip
Adding upstream version 5.6.0.upstream/5.6.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--tests/unit/meson.build37
-rw-r--r--tests/unit/mock_cmodule.c21
-rw-r--r--tests/unit/packaging/debian/10/builddeps1
-rwxr-xr-xtests/unit/packaging/test.sh2
-rw-r--r--tests/unit/test.h110
5 files changed, 171 insertions, 0 deletions
diff --git a/tests/unit/meson.build b/tests/unit/meson.build
new file mode 100644
index 0000000..b10789c
--- /dev/null
+++ b/tests/unit/meson.build
@@ -0,0 +1,37 @@
+# tests: unit
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+# mock module for test_module
+mock_cmodule_src = files([
+ 'mock_cmodule.c',
+])
+
+mock_cmodule_mod = shared_module(
+ 'mock_cmodule',
+ mock_cmodule_src,
+ name_prefix: '',
+ dependencies: libknot,
+ include_directories: mod_inc_dir,
+)
+
+# executables with tests
+foreach unit_test : unit_tests
+ exec_test = executable(
+ unit_test[0],
+ unit_test[1],
+ dependencies: [
+ contrib_dep,
+ libkres_dep,
+ libknot,
+ cmocka,
+ lmdb,
+ ],
+ )
+ test(
+ 'unit.' + unit_test[0],
+ exec_test,
+ suite: 'unit',
+ # they take very short time
+ kwargs: meson.version().version_compare('<0.52') ? {} : { 'priority': -5 },
+ )
+endforeach
diff --git a/tests/unit/mock_cmodule.c b/tests/unit/mock_cmodule.c
new file mode 100644
index 0000000..9cc9d1a
--- /dev/null
+++ b/tests/unit/mock_cmodule.c
@@ -0,0 +1,21 @@
+/* Copyright (C) CZ.NIC, z.s.p.o. <knot-resolver@labs.nic.cz>
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#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)
diff --git a/tests/unit/packaging/debian/10/builddeps b/tests/unit/packaging/debian/10/builddeps
new file mode 100644
index 0000000..5c2068b
--- /dev/null
+++ b/tests/unit/packaging/debian/10/builddeps
@@ -0,0 +1 @@
+libcmocka-dev
diff --git a/tests/unit/packaging/test.sh b/tests/unit/packaging/test.sh
new file mode 100755
index 0000000..8212dcd
--- /dev/null
+++ b/tests/unit/packaging/test.sh
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-3.0-or-later
+test -e build_packaging/tests/unit/mock_cmodule.so
diff --git a/tests/unit/test.h b/tests/unit/test.h
new file mode 100644
index 0000000..9a7eb58
--- /dev/null
+++ b/tests/unit/test.h
@@ -0,0 +1,110 @@
+/* Copyright (C) CZ.NIC, z.s.p.o. <knot-resolver@labs.nic.cz>
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <limits.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <dirent.h>
+#include <unistd.h>
+#include <cmocka.h>
+
+/* Silence clang/GCC warnings when using cmocka 1.0 */
+#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+
+#include "lib/defines.h"
+#include "lib/utils.h"
+#include <libknot/descriptor.h>
+#include <libknot/rrset.h>
+
+/* Helpers */
+static inline void *mm_test_malloc(void *ctx, size_t n)
+{ return test_malloc(n); }
+static inline void mm_test_free(void *p)
+{ if (p) test_free(p); }
+
+/** Memory context using CMocka allocator. */
+static inline void test_mm_ctx_init(knot_mm_t *mm)
+{
+ mm->alloc = &mm_test_malloc;
+ mm->free = &mm_test_free;
+}
+
+/** Recursively delete directory. */
+static inline int test_tmpdir_remove(const char *path)
+{
+ char buf[512];
+ struct dirent *ent = NULL;
+ DIR *dir = opendir(path);
+ if (dir == NULL) {
+ return kr_error(errno);
+ }
+ while ((ent = readdir(dir)) != NULL) {
+ /* Skip special dirs (this presumes no files begin with '.') */
+ if (ent->d_name[0] == '.') {
+ continue;
+ }
+ sprintf(buf, "%s/%s", path, ent->d_name);
+ remove(buf);
+ }
+ remove(path);
+ closedir(dir);
+ return 0;
+}
+
+/** Create temporary directory. */
+static inline const char* test_tmpdir_create(void)
+{
+ static char env_path[64];
+ strcpy(env_path, "./tmpXXXXXX");
+ return mkdtemp(env_path);
+}
+
+/** Generate random string with given length. */
+static inline void test_randstr(char* dst, size_t len)
+{
+ if (len == 0) {
+ return;
+ }
+
+ for (int i = 0; i < len - 1; ++i) {
+ dst[i] = '0' + (int) (('Z'-'0') * (rand() / (RAND_MAX + 1.0)));
+ }
+ dst[len - 1] = '\0';
+ return;
+}
+
+/** Init RRSet with type TXT, random owner and random payload.
+ * @note Static memory reused, copy it if you need persistence.
+ */
+static inline void test_random_rr(knot_rrset_t *rr, uint32_t ttl)
+{
+ static uint8_t owner_buf[KNOT_DNAME_MAXLEN] = { 0 };
+ static uint8_t rdata_buf[65535];
+ knot_rdata_t *rdata = (knot_rdata_t *)rdata_buf;
+
+ uint16_t num = rand() % (sizeof(owner_buf) - 2);
+ uint8_t tmp_buf[KNOT_DNAME_MAXLEN];
+
+ /* Create random label. */
+ uint8_t label_len = num % KNOT_DNAME_MAXLABELLEN;
+ owner_buf[0] = label_len;
+ test_randstr((char *)(owner_buf + 1), label_len);
+
+ /* Create payload */
+ tmp_buf[0] = num;
+ test_randstr((char *)(tmp_buf + 1), tmp_buf[0] + 1);
+ knot_rdata_init(rdata, num + 1, tmp_buf);
+
+ /* Assign static buffers. */
+ knot_rrset_init(rr, owner_buf, KNOT_RRTYPE_TXT, KNOT_CLASS_IN, ttl);
+ rr->rrs.count = 1;
+ rr->rrs.rdata = rdata;
+}
+