diff --git a/Knot.files b/Knot.files
index 4614e0663b302fefd29543386282afc83e5f0b11..5c6a988b3bc83a0862f07e0291a8b9bc7d2db566 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 7ea8657957b1968ae864e7e9f2cba87394bc2c99..a5a334db85bf80651f907f83872ae143ea1a7f49 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 724a99bb31fc45ebf97e5d596365a2228339ddd1..c106f3fafcbcd9257199c8d5178f724a7398bc8a 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 0000000000000000000000000000000000000000..de6a3ecaee549dcf27f3ce9a2229d230f019aa5a
--- /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 c2f8d287d632d7611a8b785707a708c223f03743..1bae157986057aebc2919acb98237fdd41f23ded 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 0000000000000000000000000000000000000000..676cf771261b58b30ccaa87478fd1fb5ab391dcb
--- /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 77560c8cf944e4053667ee3c6eac697c513e181f..fdd6afc5b7be6a5ff03b01b92c4d20baf9fbbcea 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 0000000000000000000000000000000000000000..c6a8721d97db3d2211caa4e4dea38b880f7c5447
--- /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 3058eb58028395829cac2a12222d02f99bfd1e19..d09409106e5a4d01e507bfcc48221d2243700831 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 0000000000000000000000000000000000000000..7b1b846320b974e452f6776a009c36f6dfd70959
--- /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 7172560d7794ac46f54eb31b6e2cada4a736f69d..8f98d385c8cb81c394fc06a6038aa862f74434ad 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 0000000000000000000000000000000000000000..7fd008ff7e98d779d74d8d46efdaacc5ed234e37
--- /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 70339df369c3a60a356a1e29743d3c87f3cf90d1..0000000000000000000000000000000000000000
--- 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 7dee327e77e8bb905b01d63653ab7f30cb488468..cb8fdfa9b9b262b78cf17dd819da13ced523aca4 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 0000000000000000000000000000000000000000..c61f1f85bf379761faf8097dde5bbbf8133e474d
--- /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 f39b27a05cd4fc207efbb4921e6dee4271057c3c..fc49d9bbec1298348d19e0440aed8a2c9dfdecf0 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 0000000000000000000000000000000000000000..a4b4af01fabe00a831bb062ec2007dee2a2ea368
--- /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 66066549bfc03fa9dbf296cf8b829f32bbe858b1..a0beda9b863db66889c36bc298c05cebf69342a9 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 0000000000000000000000000000000000000000..c409b81829f93188ba38a62fe87e2a9f88a00d4f
--- /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 67a41dbb0b94411cce32ddfe0bcbe8951784826e..44a96074eb70422567e11ae8aaf172867ad60f4d 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 0000000000000000000000000000000000000000..3d11996432fb58616274e91f07d2223c8d1c8f0b
--- /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 759558b5fdc0b32f5c85196b19b001a186c110dd..f1f9a3be3a3dc12339d31cbb9e1fa383d905dfa2 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 0000000000000000000000000000000000000000..1c69c5c2b857ec431a643a28b8292d81ddf0174e
--- /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 6c6acbe432b0fee4ccd2a46e3a6b010644eb4da0..1544e1e4b41ef3cfa2b80e9c6a12e797a51161e5 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 0000000000000000000000000000000000000000..98ccd420845d68f9d226168a65e62b85f3e3100b
--- /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 74479f0c6406e426cdbe708340beba11b259fac5..18f4fe45f810bc2826ef37a153eaa0cd9e32f2e0 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 0000000000000000000000000000000000000000..16affd55f3f88eeb7ab8d1643792cb1ab3da6dc7
--- /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 2c5bed3f2959bf7b8f409dd5ca1744bbb68f111b..6f3443574e4ceb5761f7281f58640601d023acee 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 0000000000000000000000000000000000000000..7982751eae8f1a0141769a0a95f0eed866edad38
--- /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 98365af97b4641ddc3adc0b94a43f66fd0b0189d..efcc598abf835bd4b83bbf3a62f8c8ac7da9a830 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 0000000000000000000000000000000000000000..11b9db890205c0bcec32e82e566309d8cd4aee98
--- /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 db6c256638080d66766608c836ce933e69663b82..a1937e965cfa794370e9fb4632b883cc0421fc52 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 0000000000000000000000000000000000000000..13e091fbc0b23b01f756149a05cef1074485aa2b
--- /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 26b42f5718c777ccf51680d0037bd3303edb7242..0b8472f5a508fecb83dcb1c2258374220185e4ce 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 92b7082049a3d0eeffb68d7313167061265b4260..9bd38c421cb884cdf71f5d9c7a92204d50a7fe0d 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"