From ae938d9d52977c65413d2e35d651a50777991286 Mon Sep 17 00:00:00 2001
From: Marek Vavrusa <marek@vavrusa.com>
Date: Tue, 5 Apr 2011 16:55:30 +0200
Subject: [PATCH] Divided dnslib unit to submodules, fixed code compilation.

Added missing unit headers.

Commit refs #701.
---
 Knot.files                                    |  30 +++-
 src/Makefile.am                               |  15 ++
 src/tests/common/da_tests.c                   |   2 +-
 src/tests/common/da_tests.h                   |   9 +
 src/tests/common/skiplist_tests.c             |   3 +-
 src/tests/common/skiplist_tests.h             |   9 +
 src/tests/common/slab_tests.c                 |   5 +-
 src/tests/common/slab_tests.h                 |   9 +
 src/tests/dnslib/cuckoo_tests.c               |   7 +-
 src/tests/dnslib/cuckoo_tests.h               |   9 +
 .../{dnslib_dname_tests.c => dname_tests.c}   |   3 +-
 src/tests/dnslib/dname_tests.h                |   9 +
 src/tests/dnslib/dnslib_tests.c               |  90 ----------
 .../{dnslib_edns_tests.c => edns_tests.c}     |  15 +-
 src/tests/dnslib/edns_tests.h                 |  19 +++
 .../{dnslib_node_tests.c => node_tests.c}     |   3 +-
 src/tests/dnslib/node_tests.h                 |   9 +
 .../{dnslib_rdata_tests.c => rdata_tests.c}   |  68 ++++----
 src/tests/dnslib/rdata_tests.h                |  37 +++++
 ...slib_response_tests.c => response_tests.c} |  16 +-
 src/tests/dnslib/response_tests.h             |  21 +++
 .../{dnslib_rrset_tests.c => rrset_tests.c}   | 155 ++++++++++++++++--
 src/tests/dnslib/rrset_tests.h                |  19 +++
 .../{dnslib_zone_tests.c => zone_tests.c}     |   6 +-
 src/tests/dnslib/zone_tests.h                 |   9 +
 src/tests/dnslib/zonedb_tests.c               |   6 +-
 src/tests/dnslib/zonedb_tests.h               |   9 +
 src/tests/knot/conf_tests.c                   |   4 +-
 src/tests/knot/conf_tests.h                   |   9 +
 src/tests/knot/dthreads_tests.c               |   7 +-
 src/tests/knot/dthreads_tests.h               |   9 +
 src/tests/knot/server_tests.c                 |   2 +-
 src/tests/knot/server_tests.h                 |   9 +
 src/tests/unittests_main.c                    |  43 +++--
 src/zoneparser/tests/unittests_zp_main.c      |   3 +-
 35 files changed, 480 insertions(+), 198 deletions(-)
 create mode 100644 src/tests/common/da_tests.h
 create mode 100644 src/tests/common/skiplist_tests.h
 create mode 100644 src/tests/common/slab_tests.h
 create mode 100644 src/tests/dnslib/cuckoo_tests.h
 rename src/tests/dnslib/{dnslib_dname_tests.c => dname_tests.c} (99%)
 create mode 100644 src/tests/dnslib/dname_tests.h
 delete mode 100644 src/tests/dnslib/dnslib_tests.c
 rename src/tests/dnslib/{dnslib_edns_tests.c => edns_tests.c} (98%)
 create mode 100644 src/tests/dnslib/edns_tests.h
 rename src/tests/dnslib/{dnslib_node_tests.c => node_tests.c} (99%)
 create mode 100644 src/tests/dnslib/node_tests.h
 rename src/tests/dnslib/{dnslib_rdata_tests.c => rdata_tests.c} (94%)
 create mode 100644 src/tests/dnslib/rdata_tests.h
 rename src/tests/dnslib/{dnslib_response_tests.c => response_tests.c} (99%)
 create mode 100644 src/tests/dnslib/response_tests.h
 rename src/tests/dnslib/{dnslib_rrset_tests.c => rrset_tests.c} (82%)
 create mode 100644 src/tests/dnslib/rrset_tests.h
 rename src/tests/dnslib/{dnslib_zone_tests.c => zone_tests.c} (99%)
 create mode 100644 src/tests/dnslib/zone_tests.h
 create mode 100644 src/tests/dnslib/zonedb_tests.h
 create mode 100644 src/tests/knot/conf_tests.h
 create mode 100644 src/tests/knot/dthreads_tests.h
 create mode 100644 src/tests/knot/server_tests.h

diff --git a/Knot.files b/Knot.files
index 4614e0663b..5c6a988b3b 100644
--- a/Knot.files
+++ b/Knot.files
@@ -118,21 +118,35 @@ src/knot/conf/conf.h
 src/knot/conf/logconf.c
 src/knot/conf/logconf.h
 src/tests/common/da_tests.c
+src/tests/common/da_tests.h
 src/tests/common/skiplist_tests.c
+src/tests/common/skiplist_tests.h
 src/tests/common/slab_tests.c
-src/tests/dnslib/dnslib_dname_tests.c
-src/tests/dnslib/dnslib_edns_tests.c
-src/tests/dnslib/dnslib_node_tests.c
-src/tests/dnslib/dnslib_rdata_tests.c
-src/tests/dnslib/dnslib_response_tests.c
-src/tests/dnslib/dnslib_rrset_tests.c
-src/tests/dnslib/dnslib_zone_tests.c
-src/tests/dnslib/dnslib_tests.c
+src/tests/common/slab_tests.h
+src/tests/dnslib/dname_tests.c
+src/tests/dnslib/dname_tests.h
+src/tests/dnslib/edns_tests.c
+src/tests/dnslib/edns_tests.h
+src/tests/dnslib/node_tests.c
+src/tests/dnslib/node_tests.h
+src/tests/dnslib/rdata_tests.c
+src/tests/dnslib/rdata_tests.h
+src/tests/dnslib/response_tests.c
+src/tests/dnslib/response_tests.h
+src/tests/dnslib/rrset_tests.c
+src/tests/dnslib/rrset_tests.h
+src/tests/dnslib/zone_tests.c
+src/tests/dnslib/zone_tests.h
 src/tests/dnslib/zonedb_tests.c
+src/tests/dnslib/zonedb_tests.h
 src/tests/dnslib/cuckoo_tests.c
+src/tests/dnslib/cuckoo_tests.h
 src/tests/knot/dthreads_tests.c
+src/tests/knot/dthreads_tests.h
 src/tests/knot/conf_tests.c
+src/tests/knot/conf_tests.h
 src/tests/knot/server_tests.c
+src/tests/knot/server_tests.h
 src/tests/libtap/tap.c
 src/tests/libtap/tap.h
 src/tests/libtap/tap_unit.h
diff --git a/src/Makefile.am b/src/Makefile.am
index 7ea8657957..a5a334db85 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -47,6 +47,21 @@ knot_zcompile_SOURCES =			\
 
 unittests_SOURCES =				\
 	tests/libtap/tap.c			\
+	tests/common/da_tests.c			\
+	tests/common/skiplist_tests.c		\
+	tests/common/slab_tests.c		\
+	tests/dnslib/cuckoo_tests.c		\
+	tests/dnslib/dname_tests.c		\
+	tests/dnslib/edns_tests.c		\
+	tests/dnslib/node_tests.c		\
+	tests/dnslib/rdata_tests.c		\
+	tests/dnslib/response_tests.c		\
+	tests/dnslib/rrset_tests.c		\
+	tests/dnslib/zone_tests.c		\
+	tests/dnslib/zonedb_tests.c		\
+	tests/knot/conf_tests.c			\
+	tests/knot/dthreads_tests.c		\
+	tests/knot/server_tests.c		\
 	tests/unittests_main.c
 
 unittests_zcompile_SOURCES =		\
diff --git a/src/tests/common/da_tests.c b/src/tests/common/da_tests.c
index 724a99bb31..c106f3fafc 100644
--- a/src/tests/common/da_tests.c
+++ b/src/tests/common/da_tests.c
@@ -1,4 +1,4 @@
-#include "tests/tap_unit.h"
+#include "tests/common/da_tests.h"
 #include "common/dynamic-array.h"
 #include <unistd.h>
 #include <urcu.h>
diff --git a/src/tests/common/da_tests.h b/src/tests/common/da_tests.h
new file mode 100644
index 0000000000..de6a3ecaee
--- /dev/null
+++ b/src/tests/common/da_tests.h
@@ -0,0 +1,9 @@
+#ifndef _KNOT_DA_TESTS_H_
+#define _KNOT_DA_TESTS_H_
+
+#include "tests/libtap/tap_unit.h"
+
+/* Unit API. */
+unit_api da_tests_api;
+
+#endif /* _KNOT_DA_TESTS_H_ */
diff --git a/src/tests/common/skiplist_tests.c b/src/tests/common/skiplist_tests.c
index c2f8d287d6..1bae157986 100644
--- a/src/tests/common/skiplist_tests.c
+++ b/src/tests/common/skiplist_tests.c
@@ -1,5 +1,6 @@
 #include <time.h>
-#include "tests/tap_unit.h"
+
+#include "tests/common/skiplist_tests.h"
 #include "common/skip-list.h"
 
 static int skiplist_tests_count(int argc, char *argv[]);
diff --git a/src/tests/common/skiplist_tests.h b/src/tests/common/skiplist_tests.h
new file mode 100644
index 0000000000..676cf77126
--- /dev/null
+++ b/src/tests/common/skiplist_tests.h
@@ -0,0 +1,9 @@
+#ifndef _KNOT_SKIPLIST_TESTS_H_
+#define _KNOT_SKIPLIST_TESTS_H_
+
+#include "tests/libtap/tap_unit.h"
+
+/* Unit API. */
+unit_api skiplist_tests_api;
+
+#endif /* _KNOT_SKIPLIST_TESTS_H_ */
diff --git a/src/tests/common/slab_tests.c b/src/tests/common/slab_tests.c
index 77560c8cf9..fdd6afc5b7 100644
--- a/src/tests/common/slab_tests.c
+++ b/src/tests/common/slab_tests.c
@@ -3,8 +3,9 @@
 #include <time.h>
 #include <stdbool.h>
 
-#include "tests/tap_unit.h"
+#include "tests/common/slab_tests.h"
 #include "common/slab/slab.h"
+#include "knot/common.h"
 
 /* Explicitly ask for symbols,
  * as the constructor and desctructor
@@ -102,7 +103,7 @@ static int slab_tests_run(int argc, char *argv[])
 	for(int i = 0; i < alloc_count; ++i) {
 		double roll = rand() / (double) RAND_MAX;
 		size_t bsize = roll * 2048;
-		bsize = max(bsize, 8);
+		bsize = MAX(bsize, 8);
 		if ((ptrs_i == 0) || (roll < 0.6)) {
 			void* m = slab_alloc_alloc(&alloc, bsize);
 			if (m == 0) {
diff --git a/src/tests/common/slab_tests.h b/src/tests/common/slab_tests.h
new file mode 100644
index 0000000000..c6a8721d97
--- /dev/null
+++ b/src/tests/common/slab_tests.h
@@ -0,0 +1,9 @@
+#ifndef _KNOT_SLAB_TESTS_H_
+#define _KNOT_SLAB_TESTS_H_
+
+#include "tests/libtap/tap_unit.h"
+
+/* Unit API. */
+unit_api slab_tests_api;
+
+#endif /* _KNOT_SLAB_TESTS_H_ */
diff --git a/src/tests/dnslib/cuckoo_tests.c b/src/tests/dnslib/cuckoo_tests.c
index 3058eb5802..d09409106e 100644
--- a/src/tests/dnslib/cuckoo_tests.c
+++ b/src/tests/dnslib/cuckoo_tests.c
@@ -1,12 +1,11 @@
-#include "tests/tap_unit.h"
-
-#define CT_TEST_REHASH
-
 #include <time.h>
 #include <assert.h>
 
+#include "tests/dnslib/cuckoo_tests.h"
 #include "dnslib/hash/cuckoo-hash-table.h"
 
+#define CT_TEST_REHASH
+
 //#define CK_TEST_DEBUG
 //#define CK_TEST_LOOKUP
 //#define CK_TEST_OUTPUT
diff --git a/src/tests/dnslib/cuckoo_tests.h b/src/tests/dnslib/cuckoo_tests.h
new file mode 100644
index 0000000000..7b1b846320
--- /dev/null
+++ b/src/tests/dnslib/cuckoo_tests.h
@@ -0,0 +1,9 @@
+#ifndef _KNOT_CUCKOO_TESTS_H_
+#define _KNOT_CUCKOO_TESTS_H_
+
+#include "tests/libtap/tap_unit.h"
+
+/* Unit API. */
+unit_api cuckoo_tests_api;
+
+#endif /* _KNOT_CUCKOO_TESTS_H_ */
diff --git a/src/tests/dnslib/dnslib_dname_tests.c b/src/tests/dnslib/dname_tests.c
similarity index 99%
rename from src/tests/dnslib/dnslib_dname_tests.c
rename to src/tests/dnslib/dname_tests.c
index 7172560d77..8f98d385c8 100644
--- a/src/tests/dnslib/dnslib_dname_tests.c
+++ b/src/tests/dnslib/dname_tests.c
@@ -1,6 +1,7 @@
 #include <string.h>
 #include <assert.h>
 
+#include "tests/dnslib/dname_tests.h"
 #include "dnslib/dname.h"
 #include "dnslib/node.h"
 
@@ -9,7 +10,7 @@ static int dnslib_dname_tests_run(int argc, char *argv[]);
 
 /*! Exported unit API.
  */
-unit_api dnslib_dname_tests_api = {
+unit_api dname_tests_api = {
 	"DNS library - dname",        //! Unit name
 	&dnslib_dname_tests_count,  //! Count scheduled tests
 	&dnslib_dname_tests_run     //! Run scheduled tests
diff --git a/src/tests/dnslib/dname_tests.h b/src/tests/dnslib/dname_tests.h
new file mode 100644
index 0000000000..7fd008ff7e
--- /dev/null
+++ b/src/tests/dnslib/dname_tests.h
@@ -0,0 +1,9 @@
+#ifndef _KNOT_DNAME_TESTS_H_
+#define _KNOT_DNAME_TESTS_H_
+
+#include "tests/libtap/tap_unit.h"
+
+/* Unit API. */
+unit_api dname_tests_api;
+
+#endif /* _KNOT_DNAME_TESTS_H_ */
diff --git a/src/tests/dnslib/dnslib_tests.c b/src/tests/dnslib/dnslib_tests.c
deleted file mode 100644
index 70339df369..0000000000
--- a/src/tests/dnslib/dnslib_tests.c
+++ /dev/null
@@ -1,90 +0,0 @@
-#include "tests/tap_unit.h"
-
-#include "tests/dnslib/dnslib_dname_tests.c"
-#include "tests/dnslib/dnslib_rdata_tests.c"
-#include "tests/dnslib/dnslib_node_tests.c"
-#include "tests/dnslib/dnslib_rrset_tests.c"
-#include "tests/dnslib/dnslib_zone_tests.c"
-#include "tests/dnslib/dnslib_response_tests.c"
-#include "tests/dnslib/dnslib_edns_tests.c"
-
-static int dnslib_tests_count(int argc, char *argv[]);
-static int dnslib_tests_run(int argc, char *argv[]);
-
-/*! Exported unit API.
- */
-unit_api dnslib_tests_api = {
-	"DNS library",        //! Unit name
-	&dnslib_tests_count,  //! Count scheduled tests
-	&dnslib_tests_run     //! Run scheduled tests
-};
-
-/*! \todo Implement theese tests into API.
-  */
-
-/*! This helper routine should report number of
- *  scheduled tests for given parameters.
- */
-static int dnslib_tests_count(int argc, char *argv[])
-{
-        return dnslib_dname_tests_count(argc, argv)
-	       + dnslib_rdata_tests_count(argc, argv)
-               + dnslib_rrset_tests_count(argc, argv)
-               + dnslib_node_tests_count(argc, argv)
-               + dnslib_zone_tests_count(argc, argv)
-               + dnslib_response_tests_count(argc, argv)
-               + dnslib_edns_tests_count(argc, argv);
-}
-
-/*! Run all scheduled tests for given parameters.
- */
-static int dnslib_tests_run(int argc, char *argv[])
-{
-	int res = 0;
-
-        int rrset_tests = dnslib_rrset_tests_count(argc, argv);
-	int node_tests = dnslib_node_tests_count(argc, argv);
-	int zone_tests = dnslib_zone_tests_count(argc, argv);
-	int response_tests = dnslib_response_tests_count(argc, argv);
-	int edns_tests = dnslib_edns_tests_count(argc, argv);
-
-        note("Testing module: dname");
-	res = dnslib_dname_tests_run(argc, argv);
-
-	note("Testing module: rdata");
-	res *= dnslib_rdata_tests_run(argc, argv);
-
-	skip(!res, rrset_tests + node_tests + zone_tests + response_tests);
-
-	note("Testing module: rrset");
-	res = dnslib_rrset_tests_run(argc, argv);
-
-	skip(!res, node_tests + zone_tests + response_tests);
-
-	note("Testing module: node");
-	res = dnslib_node_tests_run(argc, argv);
-
-	skip(!res, zone_tests);
-
-	note("Testing module: zone");
-        res = dnslib_zone_tests_run(argc, argv);
-
-	note("Testing module: response");
-	res = dnslib_response_tests_run(argc, argv);
-
-	skip(!res, edns_tests);
-
-        note("Testing module: edns");
-	res = dnslib_edns_tests_run(argc, argv);
-
-
-	endskip; // skipped edns
-
-	endskip; // skipped zone
-
-	endskip; // skipped node & zone
-
-	endskip; // skipped rrset & node & zone & response & edns
-
-	return res;
-}
diff --git a/src/tests/dnslib/dnslib_edns_tests.c b/src/tests/dnslib/edns_tests.c
similarity index 98%
rename from src/tests/dnslib/dnslib_edns_tests.c
rename to src/tests/dnslib/edns_tests.c
index 7dee327e77..cb8fdfa9b9 100644
--- a/src/tests/dnslib/dnslib_edns_tests.c
+++ b/src/tests/dnslib/edns_tests.c
@@ -1,14 +1,7 @@
-/*!
- * \file dnslib_edns_tests.c
- *
- * \author Jan Kadlec <jan.kadlec@nic.cz>
- *
- * Contains unit tests for ENDS API
- *
- * Contains tests for:
- * - ENDS API
- */
+#include <assert.h>
 
+#include "tests/dnslib/edns_tests.h"
+#include "dnslib/dnslib-common.h"
 #include "dnslib/edns.h"
 
 static int dnslib_edns_tests_count(int argc, char *argv[]);
@@ -16,7 +9,7 @@ static int dnslib_edns_tests_run(int argc, char *argv[]);
 
 /*! Exported unit API.
  */
-unit_api dnslib_edns_tests_api = {
+unit_api edns_tests_api = {
 	"DNS library - EDNS",      //! Unit name
 	&dnslib_edns_tests_count,  //! Count scheduled tests
 	&dnslib_edns_tests_run     //! Run scheduled tests
diff --git a/src/tests/dnslib/edns_tests.h b/src/tests/dnslib/edns_tests.h
new file mode 100644
index 0000000000..c61f1f85bf
--- /dev/null
+++ b/src/tests/dnslib/edns_tests.h
@@ -0,0 +1,19 @@
+/*!
+ * \file dnslib_edns_tests.c
+ *
+ * \author Jan Kadlec <jan.kadlec@nic.cz>
+ *
+ * Contains unit tests for ENDS API
+ *
+ * Contains tests for:
+ * - ENDS API
+ */
+#ifndef _KNOT_EDNS_TESTS_H_
+#define _KNOT_EDNS_TESTS_H_
+
+#include "tests/libtap/tap_unit.h"
+
+/* Unit API. */
+unit_api edns_tests_api;
+
+#endif /* _KNOT_EDNS_TESTS_H_ */
diff --git a/src/tests/dnslib/dnslib_node_tests.c b/src/tests/dnslib/node_tests.c
similarity index 99%
rename from src/tests/dnslib/dnslib_node_tests.c
rename to src/tests/dnslib/node_tests.c
index f39b27a05c..fc49d9bbec 100644
--- a/src/tests/dnslib/dnslib_node_tests.c
+++ b/src/tests/dnslib/node_tests.c
@@ -1,3 +1,4 @@
+#include "tests/dnslib/node_tests.h"
 #include "dnslib/dname.h"
 #include "dnslib/node.h"
 #include "dnslib/descriptor.h"
@@ -7,7 +8,7 @@ static int dnslib_node_tests_run(int argc, char *argv[]);
 
 /*! Exported unit API.
  */
-unit_api dnslib_node_tests_api = {
+unit_api node_tests_api = {
 	"DNS library - node",       //! Unit name
 	&dnslib_node_tests_count,  //! Count scheduled tests
 	&dnslib_node_tests_run     //! Run scheduled tests
diff --git a/src/tests/dnslib/node_tests.h b/src/tests/dnslib/node_tests.h
new file mode 100644
index 0000000000..a4b4af01fa
--- /dev/null
+++ b/src/tests/dnslib/node_tests.h
@@ -0,0 +1,9 @@
+#ifndef _KNOT_NODE_TESTS_H_
+#define _KNOT_NODE_TESTS_H_
+
+#include "tests/libtap/tap_unit.h"
+
+/* Unit API. */
+unit_api node_tests_api;
+
+#endif /* _KNOT_NODE_TESTS_H_ */
diff --git a/src/tests/dnslib/dnslib_rdata_tests.c b/src/tests/dnslib/rdata_tests.c
similarity index 94%
rename from src/tests/dnslib/dnslib_rdata_tests.c
rename to src/tests/dnslib/rdata_tests.c
index 66066549bf..a0beda9b86 100644
--- a/src/tests/dnslib/dnslib_rdata_tests.c
+++ b/src/tests/dnslib/rdata_tests.c
@@ -1,47 +1,53 @@
-/*!
- * \file dnslib_rdata_tests.c
- *
- * \author Lubos Slovak <lubos.slovak@nic.cz>
- *
- * Contains unit tests for RDATA (dnslib_rdata_t) and RDATA item
- * (dnslib_rdata_item_t) structures.
- *
- * Contains tests for:
- * - creating empty RDATA structure with or without reserved space.
- * - setting RDATA items one-by-one
- * - setting RDATA items all at once
- *
- * As for now, the tests use several (TEST_RDATAS) RDATA structures, each
- * with different number of RDATA items (given by test_rdatas). These are all
- * initialized to pointers derived from RDATA_ITEM_PTR (first is RDATA_ITEM_PTR,
- * second RDATA_ITEM_PTR + 1, etc.). The functions only test if the pointer
- * is set properly.
- *
- * \todo It may be better to test also some RDATAs with predefined contents,
- *       such as some numbers, some domain name, etc. For this purpose, we'd
- *       need RDATA descriptors (telling the types of each RDATA item within an
- *       RDATA).
- *
- * \todo It will be fine to test all possible output values of all functions,
- *       e.g. test whether dnslib_rdata_get_item() returns NULL when passed an
- *       illegal position, etc.
- */
-
 #include <stdlib.h>
+#include <assert.h>
 
+#include "tests/dnslib/rdata_tests.h"
+#include "dnslib/dnslib-common.h"
 #include "dnslib/rdata.h"
 #include "dnslib/descriptor.h"
 #include "dnslib/utils.h"
 #include "dnslib/error.h"
 
-static const struct test_domain test_domains_ok[];
+enum { TEST_DOMAINS_OK = 8 };
+
+struct test_domain {
+	char *str;
+	char *wire;
+	uint size;
+	char *labels;
+	short label_count;
+};
+
+/*! \warning Do not change the order in those, if you want to test some other
+ *           feature with new dname, add it at the end of these arrays.
+ */
+static const struct test_domain
+		test_domains_ok[TEST_DOMAINS_OK] = {
+	{ "abc.test.domain.com.", "\3abc\4test\6domain\3com", 21,
+	  "\x0\x4\x9\x10", 4 },
+	{ "some.test.domain.com.", "\4some\4test\6domain\3com", 22,
+	  "\x0\x5\xA\x11", 4 },
+	{ "xyz.test.domain.com.", "\3xyz\4test\6domain\3com", 21,
+	  "\x0\x4\x9\x10", 4 },
+	{ "some.test.domain.com.", "\4some\4test\6domain\3com", 22,
+	  "\x0\x5\xA\x11", 4 },
+	{ "test.domain.com.", "\4test\6domain\3com", 17,
+	  "\x0\x5\xC", 3 },
+	{ ".", "\0", 1,
+	  "", 0 },
+	{ "foo.bar.net.", "\3foo\3bar\3net", 13,
+	  "\x0\x4\x8", 3},
+	{ "bar.net.", "\3bar\3net", 9,
+	  "\x0\x4", 2}
+};
+
 
 static int dnslib_rdata_tests_count(int argc, char *argv[]);
 static int dnslib_rdata_tests_run(int argc, char *argv[]);
 
 /*! Exported unit API.
  */
-unit_api dnslib_rdata_tests_api = {
+unit_api rdata_tests_api = {
 	"DNS library - rdata",        //! Unit name
 	&dnslib_rdata_tests_count,  //! Count scheduled tests
 	&dnslib_rdata_tests_run     //! Run scheduled tests
diff --git a/src/tests/dnslib/rdata_tests.h b/src/tests/dnslib/rdata_tests.h
new file mode 100644
index 0000000000..c409b81829
--- /dev/null
+++ b/src/tests/dnslib/rdata_tests.h
@@ -0,0 +1,37 @@
+/*!
+ * \file dnslib_rdata_tests.c
+ *
+ * \author Lubos Slovak <lubos.slovak@nic.cz>
+ *
+ * Contains unit tests for RDATA (dnslib_rdata_t) and RDATA item
+ * (dnslib_rdata_item_t) structures.
+ *
+ * Contains tests for:
+ * - creating empty RDATA structure with or without reserved space.
+ * - setting RDATA items one-by-one
+ * - setting RDATA items all at once
+ *
+ * As for now, the tests use several (TEST_RDATAS) RDATA structures, each
+ * with different number of RDATA items (given by test_rdatas). These are all
+ * initialized to pointers derived from RDATA_ITEM_PTR (first is RDATA_ITEM_PTR,
+ * second RDATA_ITEM_PTR + 1, etc.). The functions only test if the pointer
+ * is set properly.
+ *
+ * \todo It may be better to test also some RDATAs with predefined contents,
+ *       such as some numbers, some domain name, etc. For this purpose, we'd
+ *       need RDATA descriptors (telling the types of each RDATA item within an
+ *       RDATA).
+ *
+ * \todo It will be fine to test all possible output values of all functions,
+ *       e.g. test whether dnslib_rdata_get_item() returns NULL when passed an
+ *       illegal position, etc.
+ */
+#ifndef _KNOT_RDATA_TESTS_H_
+#define _KNOT_RDATA_TESTS_H_
+
+#include "tests/libtap/tap_unit.h"
+
+/* Unit API. */
+unit_api rdata_tests_api;
+
+#endif /* _KNOT_RDATA_TESTS_H_ */
diff --git a/src/tests/dnslib/dnslib_response_tests.c b/src/tests/dnslib/response_tests.c
similarity index 99%
rename from src/tests/dnslib/dnslib_response_tests.c
rename to src/tests/dnslib/response_tests.c
index 67a41dbb0b..44a96074eb 100644
--- a/src/tests/dnslib/dnslib_response_tests.c
+++ b/src/tests/dnslib/response_tests.c
@@ -1,15 +1,3 @@
-/*!
- * \file dnslib_response_tests.c
- *
- * \author Jan Kadlec <jan.kadlec@nic.cz>
- *
- * Contains unit tests for response structure (dnslib_response_t) and its API.
- *
- * Contains tests for:
- * - Response API
- * - \todo
- */
-
 #include <assert.h>
 #include <inttypes.h>
 
@@ -18,6 +6,8 @@
 #endif
 
 //#define RESP_TEST_DEBUG
+#include "tests/dnslib/response_tests.h"
+#include "dnslib/dnslib-common.h"
 #include "dnslib/response.h"
 #include "dnslib/rdata.h"
 #include "dnslib/rrset.h"
@@ -40,7 +30,7 @@ static int dnslib_response_tests_run(int argc, char *argv[]);
 
 /*! Exported unit API.
  */
-unit_api dnslib_response_tests_api = {
+unit_api response_tests_api = {
 	"DNS library - response",      //! Unit name
 	&dnslib_response_tests_count,  //! Count scheduled tests
 	&dnslib_response_tests_run     //! Run scheduled tests
diff --git a/src/tests/dnslib/response_tests.h b/src/tests/dnslib/response_tests.h
new file mode 100644
index 0000000000..3d11996432
--- /dev/null
+++ b/src/tests/dnslib/response_tests.h
@@ -0,0 +1,21 @@
+/*!
+ * \file dnslib_response_tests.c
+ *
+ * \author Jan Kadlec <jan.kadlec@nic.cz>
+ *
+ * Contains unit tests for response structure (dnslib_response_t) and its API.
+ *
+ * Contains tests for:
+ * - Response API
+ * - \todo
+ */
+#ifndef _KNOT_RESPONSE_TESTS_H_
+#define _KNOT_RESPONSE_TESTS_H_
+
+#include "tests/libtap/tap_unit.h"
+
+/* Unit API. */
+unit_api response_tests_api;
+
+#endif /* _KNOT_RESPONSE_TESTS_H_ */
+
diff --git a/src/tests/dnslib/dnslib_rrset_tests.c b/src/tests/dnslib/rrset_tests.c
similarity index 82%
rename from src/tests/dnslib/dnslib_rrset_tests.c
rename to src/tests/dnslib/rrset_tests.c
index 759558b5fd..f1f9a3be3a 100644
--- a/src/tests/dnslib/dnslib_rrset_tests.c
+++ b/src/tests/dnslib/rrset_tests.c
@@ -1,24 +1,20 @@
-/*!
- * \file dnslib_rrset_tests.c
- *
- * \author Jan Kadlec <jan.kadlec@nic.cz>
- *
- * Contains unit tests for RRSet (dnslib_rrset_t) and its API.
- *
- * Contains tests for:
- * -
- */
+#include <assert.h>
 
+#include "tests/dnslib/rrset_tests.h"
+#include "dnslib/dnslib-common.h"
+#include "dnslib/descriptor.h"
 #include "dnslib/rrset.h"
 #include "dnslib/dname.h"
 #include "dnslib/rdata.h"
+#include "dnslib/utils.h"
+#include "dnslib/node.h"
 
 static int dnslib_rrset_tests_count(int argc, char *argv[]);
 static int dnslib_rrset_tests_run(int argc, char *argv[]);
 
 /*! Exported unit API.
  */
-unit_api dnslib_rrset_tests_api = {
+unit_api rrset_tests_api = {
 	"DNS library - rrset",        //! Unit name
 	&dnslib_rrset_tests_count,  //! Count scheduled tests
 	&dnslib_rrset_tests_run     //! Run scheduled tests
@@ -29,11 +25,22 @@ unit_api dnslib_rrset_tests_api = {
  *  Unit implementation.
  */
 
+static dnslib_node_t *NODE_ADDRESS = (dnslib_node_t *)0xDEADBEEF;
+static uint16_t *RDATA_ITEM_PTR = (uint16_t *)0xDEADBEEF;
+
 enum { TEST_RRSETS = 6 , TEST_RRSIGS = 6};
 
 //void *RRSIG_ADDRESS = (void *)0xDEADBEEF;
 //void *RRSIG_FIRST = RRSIG_ADDRESS + 10;
 
+struct test_domain {
+	char *str;
+	char *wire;
+	uint size;
+	char *labels;
+	short label_count;
+};
+
 struct test_rrset {
 	char *owner;
 	uint16_t type;
@@ -54,6 +61,8 @@ enum {
 	RR_RDATA_COUNT = 5,
 };
 
+enum { TEST_DOMAINS_OK = 8 };
+
 static dnslib_dname_t RR_DNAMES[RR_DNAMES_COUNT] =
 	{ {(uint8_t *)"\7example\3com", 13, NULL}, //0's at the end are added
 	  {(uint8_t *)"\3ns1\7example\3com", 17, NULL},
@@ -74,6 +83,29 @@ static dnslib_rdata_t RR_RDATA[RR_RDATA_COUNT] =
 	  {&RR_ITEMS[1], 1, &RR_RDATA[4]}, /* both in cyclic list */
 	  {&RR_ITEMS[2], 1, &RR_RDATA[3]} };
 
+/*! \warning Do not change the order in those, if you want to test some other
+ *           feature with new dname, add it at the end of these arrays.
+ */
+static const struct test_domain
+		test_domains_ok[TEST_DOMAINS_OK] = {
+	{ "abc.test.domain.com.", "\3abc\4test\6domain\3com", 21,
+	  "\x0\x4\x9\x10", 4 },
+	{ "some.test.domain.com.", "\4some\4test\6domain\3com", 22,
+	  "\x0\x5\xA\x11", 4 },
+	{ "xyz.test.domain.com.", "\3xyz\4test\6domain\3com", 21,
+	  "\x0\x4\x9\x10", 4 },
+	{ "some.test.domain.com.", "\4some\4test\6domain\3com", 22,
+	  "\x0\x5\xA\x11", 4 },
+	{ "test.domain.com.", "\4test\6domain\3com", 17,
+	  "\x0\x5\xC", 3 },
+	{ ".", "\0", 1,
+	  "", 0 },
+	{ "foo.bar.net.", "\3foo\3bar\3net", 13,
+	  "\x0\x4\x8", 3},
+	{ "bar.net.", "\3bar\3net", 9,
+	  "\x0\x4", 2}
+};
+
 static struct test_rrset test_rrsets[TEST_RRSETS] = {
 	{ "example.com.",  DNSLIB_RRTYPE_NS, DNSLIB_CLASS_IN,
 	  3600, NULL, NULL },
@@ -98,6 +130,107 @@ static const struct test_rrset test_rrsigs[TEST_RRSIGS] = {
 	{ "example6.com.", 46, 1, 3600, NULL }
 };
 
+static void generate_rdata(uint8_t *data, int size)
+{
+	for (int i = 0; i < size; ++i) {
+		data[i] = rand() % 256;
+	}
+}
+
+static int fill_rdata(uint8_t *data, int max_size, uint16_t rrtype,
+		      dnslib_rdata_t *rdata)
+{
+	assert(rdata != NULL);
+	assert(data != NULL);
+	assert(max_size > 0);
+
+	uint8_t *pos = data;
+	int used = 0;
+	int wire_size = 0;
+
+	//note("Filling RRType %u", rrtype);
+
+	dnslib_rrtype_descriptor_t *desc =
+	dnslib_rrtype_descriptor_by_type(rrtype);
+
+	uint item_count = desc->length;
+	dnslib_rdata_item_t *items =
+	(dnslib_rdata_item_t *)malloc(item_count
+				      * sizeof(dnslib_rdata_item_t));
+
+	for (int i = 0; i < item_count; ++i) {
+		uint size = 0;
+		int domain = 0;
+		dnslib_dname_t *dname = NULL;
+		int binary = 0;
+		int stored_size = 0;
+
+		switch (desc->wireformat[i]) {
+		case DNSLIB_RDATA_WF_COMPRESSED_DNAME:
+		case DNSLIB_RDATA_WF_UNCOMPRESSED_DNAME:
+		case DNSLIB_RDATA_WF_LITERAL_DNAME:
+			dname = dnslib_dname_new_from_wire(
+					(uint8_t *)test_domains_ok[0].wire,
+					test_domains_ok[0].size, NULL);
+			assert(dname != NULL);
+			/* note("Created domain name: %s",
+				 dnslib_dname_name(dname)); */
+			//note("Domain name ptr: %p", dname);
+			domain = 1;
+			size = dnslib_dname_size(dname);
+			//note("Size of created domain name: %u", size);
+			assert(size < DNSLIB_MAX_RDATA_ITEM_SIZE);
+			// store size of the domain name
+			*(pos++) = size;
+			// copy the domain name
+			memcpy(pos, dnslib_dname_name(dname), size);
+			pos += size;
+			break;
+		default:
+			binary = 1;
+			size = rand() % 65534;
+		}
+
+		if (binary) {
+			// Rewrite the actual 2 bytes in the data array
+			// with length.
+			// (this is a bit ugly, but does the work ;-)
+			dnslib_wire_write_u16(pos, size);
+			//*pos = size;
+		}
+
+		//note("Filling %u bytes", size);
+		used += size;
+		assert(used < max_size);
+
+		if (domain) {
+			items[i].dname = dname;
+			wire_size += dnslib_dname_size(dname);
+/*			note("Saved domain name ptr on index %d: %p",
+			      i, items[i].dname); */
+		} else {
+			free(dname);
+//			note("Saved raw data ptr on index %d: %p",i, pos);
+			items[i].raw_data = (uint16_t *)pos;
+			pos += size;
+			wire_size += size;
+			if (binary && !stored_size) {
+				wire_size -= 2;
+			}
+		}
+	}
+
+	int res = dnslib_rdata_set_items(rdata, items, item_count);
+	if (res != 0) {
+		diag("dnslib_rdata_set_items() returned %d.", res);
+		free(items);
+		return -1;
+	} else {
+		free(items);
+		return wire_size;
+	}
+}
+
 /* fills test_rrsets with random rdata when empty */
 static void create_rdata()
 {
diff --git a/src/tests/dnslib/rrset_tests.h b/src/tests/dnslib/rrset_tests.h
new file mode 100644
index 0000000000..1c69c5c2b8
--- /dev/null
+++ b/src/tests/dnslib/rrset_tests.h
@@ -0,0 +1,19 @@
+/*!
+ * \file dnslib_rrset_tests.c
+ *
+ * \author Jan Kadlec <jan.kadlec@nic.cz>
+ *
+ * Contains unit tests for RRSet (dnslib_rrset_t) and its API.
+ *
+ * Contains tests for:
+ * -
+ */
+#ifndef _KNOT_RRSET_TESTS_H_
+#define _KNOT_RRSET_TESTS_H_
+
+#include "tests/libtap/tap_unit.h"
+
+/* Unit API. */
+unit_api rrset_tests_api;
+
+#endif /* _KNOT_RRSET_TESTS_H_ */
diff --git a/src/tests/dnslib/dnslib_zone_tests.c b/src/tests/dnslib/zone_tests.c
similarity index 99%
rename from src/tests/dnslib/dnslib_zone_tests.c
rename to src/tests/dnslib/zone_tests.c
index 6c6acbe432..1544e1e4b4 100644
--- a/src/tests/dnslib/dnslib_zone_tests.c
+++ b/src/tests/dnslib/zone_tests.c
@@ -1,3 +1,7 @@
+#include <assert.h>
+
+#include "tests/dnslib/zone_tests.h"
+#include "dnslib/dnslib-common.h"
 #include "dnslib/zone.h"
 #include "dnslib/node.h"
 
@@ -6,7 +10,7 @@ static int dnslib_zone_tests_run(int argc, char *argv[]);
 
 /*! Exported unit API.
  */
-unit_api dnslib_zone_tests_api = {
+unit_api zone_tests_api = {
 	"DNS library - zone",        //! Unit name
 	&dnslib_zone_tests_count,  //! Count scheduled tests
 	&dnslib_zone_tests_run     //! Run scheduled tests
diff --git a/src/tests/dnslib/zone_tests.h b/src/tests/dnslib/zone_tests.h
new file mode 100644
index 0000000000..98ccd42084
--- /dev/null
+++ b/src/tests/dnslib/zone_tests.h
@@ -0,0 +1,9 @@
+#ifndef _KNOT_ZONE_TESTS_H_
+#define _KNOT_ZONE_TESTS_H_
+
+#include "tests/libtap/tap_unit.h"
+
+/* Unit API. */
+unit_api zone_tests_api;
+
+#endif /* _KNOT_ZONE_TESTS_H_ */
diff --git a/src/tests/dnslib/zonedb_tests.c b/src/tests/dnslib/zonedb_tests.c
index 74479f0c64..18f4fe45f8 100644
--- a/src/tests/dnslib/zonedb_tests.c
+++ b/src/tests/dnslib/zonedb_tests.c
@@ -1,3 +1,6 @@
+#include "tests/dnslib/zonedb_tests.h"
+
+
 static int zonedb_tests_count(int argc, char *argv[]);
 static int zonedb_tests_run(int argc, char *argv[]);
 
@@ -9,9 +12,6 @@ unit_api zonedb_tests_api = {
 	&zonedb_tests_run     //! Run scheduled tests
 };
 
-/*! \todo Implement theese tests into API.
-  */
-
 /*! This helper routine should report number of
  *  scheduled tests for given parameters.
  */
diff --git a/src/tests/dnslib/zonedb_tests.h b/src/tests/dnslib/zonedb_tests.h
new file mode 100644
index 0000000000..16affd55f3
--- /dev/null
+++ b/src/tests/dnslib/zonedb_tests.h
@@ -0,0 +1,9 @@
+#ifndef _KNOT_ZONEDB_TESTS_H_
+#define _KNOT_ZONEDB_TESTS_H_
+
+#include "tests/libtap/tap_unit.h"
+
+/* Unit API. */
+unit_api zonedb_tests_api;
+
+#endif /* _KNOT_ZONEDB_TESTS_H_ */
diff --git a/src/tests/knot/conf_tests.c b/src/tests/knot/conf_tests.c
index 2c5bed3f29..6f3443574e 100644
--- a/src/tests/knot/conf_tests.c
+++ b/src/tests/knot/conf_tests.c
@@ -1,10 +1,10 @@
 #include <stdio.h>
 
-#include "tests/tap_unit.h"
+#include "tests/knot/conf_tests.h"
 #include "knot/conf/conf.h"
 
 /* Resources. */
-#include "sample_conf.rc"
+#include "tests/sample_conf.rc"
 
 static int conf_tests_count(int argc, char *argv[]);
 static int conf_tests_run(int argc, char *argv[]);
diff --git a/src/tests/knot/conf_tests.h b/src/tests/knot/conf_tests.h
new file mode 100644
index 0000000000..7982751eae
--- /dev/null
+++ b/src/tests/knot/conf_tests.h
@@ -0,0 +1,9 @@
+#ifndef _KNOT_CONF_TESTS_H_
+#define _KNOT_CONF_TESTS_H_
+
+#include "tests/libtap/tap_unit.h"
+
+/* Unit API. */
+unit_api conf_tests_api;
+
+#endif /* _KNOT_CONF_TESTS_H_ */
diff --git a/src/tests/knot/dthreads_tests.c b/src/tests/knot/dthreads_tests.c
index 98365af97b..efcc598abf 100644
--- a/src/tests/knot/dthreads_tests.c
+++ b/src/tests/knot/dthreads_tests.c
@@ -1,8 +1,9 @@
-#include "knot/server/dthreads.h"
-#include "tests/tap_unit.h"
-#include <sys/select.h>
 #include <pthread.h>
 #include <sched.h>
+#include <sys/select.h>
+
+#include "tests/knot/dthreads_tests.h"
+#include "knot/server/dthreads.h"
 
 static int dt_tests_count(int argc, char *argv[]);
 static int dt_tests_run(int argc, char *argv[]);
diff --git a/src/tests/knot/dthreads_tests.h b/src/tests/knot/dthreads_tests.h
new file mode 100644
index 0000000000..11b9db8902
--- /dev/null
+++ b/src/tests/knot/dthreads_tests.h
@@ -0,0 +1,9 @@
+#ifndef _KNOT_DTHREADS_TESTS_H_
+#define _KNOT_DTHREADS_TESTS_H_
+
+#include "tests/libtap/tap_unit.h"
+
+/* Unit API. */
+unit_api dthreads_tests_api;
+
+#endif /* _KNOT_DTHREADS_TESTS_H_ */
diff --git a/src/tests/knot/server_tests.c b/src/tests/knot/server_tests.c
index db6c256638..a1937e965c 100644
--- a/src/tests/knot/server_tests.c
+++ b/src/tests/knot/server_tests.c
@@ -1,5 +1,5 @@
+#include "tests/knot/server_tests.h"
 #include "knot/server/server.h"
-#include "tests/tap_unit.h"
 
 static int server_tests_count(int argc, char *argv[]);
 static int server_tests_run(int argc, char *argv[]);
diff --git a/src/tests/knot/server_tests.h b/src/tests/knot/server_tests.h
new file mode 100644
index 0000000000..13e091fbc0
--- /dev/null
+++ b/src/tests/knot/server_tests.h
@@ -0,0 +1,9 @@
+#ifndef _KNOT_SERVER_TESTS_H_
+#define _KNOT_SERVER_TESTS_H_
+
+#include "tests/libtap/tap_unit.h"
+
+/* Unit API. */
+unit_api server_tests_api;
+
+#endif /* _KNOT_SERVER_TESTS_H_ */
diff --git a/src/tests/unittests_main.c b/src/tests/unittests_main.c
index 26b42f5718..0b8472f5a5 100644
--- a/src/tests/unittests_main.c
+++ b/src/tests/unittests_main.c
@@ -1,17 +1,23 @@
 #include <config.h>
 #include "knot/common.h"
-#include "tests/tap_unit.h"
+#include "tests/libtap/tap_unit.h"
 
 // Units to test
-#include "skiplist_tests.c"
-#include "dthreads_tests.c"
-#include "da_tests.c"
-#include "cuckoo_tests.c"
-#include "zonedb_tests.c"
-#include "dnslib_tests.c"
-#include "server_tests.c"
-#include "slab_tests.c"
-#include "conf_tests.c"
+#include "tests/common/slab_tests.h"
+#include "tests/common/skiplist_tests.h"
+#include "tests/common/da_tests.h"
+#include "tests/dnslib/cuckoo_tests.h"
+#include "tests/dnslib/dname_tests.h"
+#include "tests/dnslib/edns_tests.h"
+#include "tests/dnslib/node_tests.h"
+#include "tests/dnslib/rdata_tests.h"
+#include "tests/dnslib/response_tests.h"
+#include "tests/dnslib/rrset_tests.h"
+#include "tests/dnslib/zone_tests.h"
+#include "tests/dnslib/zonedb_tests.h"
+#include "tests/knot/dthreads_tests.h"
+#include "tests/knot/server_tests.h"
+#include "tests/knot/conf_tests.h"
 
 // Run all loaded units
 int main(int argc, char *argv[])
@@ -21,15 +27,26 @@ int main(int argc, char *argv[])
 
 	// Build test set
 	unit_api *tests[] = {
+		/* Core data structures. */
+		&slab_tests_api,     //! SLAB allocator unit
 		&skiplist_tests_api, //! Skip list unit
 		&dthreads_tests_api, //! DThreads testing unit
 		&da_tests_api,       //! Dynamic array unit
+
+		/* DNS units */
 		&cuckoo_tests_api,   //! Cuckoo hashing unit
-		&zonedb_tests_api,   //! Zone database unit
+		&dname_tests_api,    //! DNS library (dname) unit
+		&edns_tests_api,     //! DNS library (EDNS0) unit
+		&node_tests_api,     //! DNS library (node) unit
+		&rdata_tests_api,    //! DNS library (rdata) unit
+		&response_tests_api, //! DNS library (response) unit
+		&rrset_tests_api,    //! DNS library (rrset) unit
+		&zone_tests_api,     //! DNS library (zone) unit
+		&zonedb_tests_api,   //! DNS library (zonedb) unit
+
+		/* Server parts. */
 		&conf_tests_api,     //! Configuration parser tests
-		&dnslib_tests_api,   //! DNS library unit
 		&server_tests_api,   //! Server unit
-		&slab_tests_api,     //! SLAB allocator unit
 		NULL
 	};
 
diff --git a/src/zoneparser/tests/unittests_zp_main.c b/src/zoneparser/tests/unittests_zp_main.c
index 92b7082049..9bd38c421c 100644
--- a/src/zoneparser/tests/unittests_zp_main.c
+++ b/src/zoneparser/tests/unittests_zp_main.c
@@ -1,6 +1,7 @@
 #include <config.h>
 #include "knot/common.h"
-#include "tests/tap_unit.h"
+#include "tests/libtap/tap_unit.h"
+
 // Units to test
 #include "zcompile_tests.c"
 
-- 
GitLab