From bd947215e7fd94f4677953befe45e677d128536f Mon Sep 17 00:00:00 2001
From: Lubos Slovak <lubos.slovak@nic.cz>
Date: Tue, 9 Nov 2010 19:44:20 +0100
Subject: [PATCH] Added tests for RDATA.

Tests implemented so far:
- create empty
- create non-empty
- set_item() for setting RDATA items one-by-one

TODO:
= set_items()

refs #88, #31
---
 CuteDNS.files                         |   1 +
 src/tests/dnslib/dnslib_rdata_tests.c | 198 ++++++++++++++++++++++++++
 src/tests/dnslib_tests.c              |  10 +-
 3 files changed, 206 insertions(+), 3 deletions(-)
 create mode 100644 src/tests/dnslib/dnslib_rdata_tests.c

diff --git a/CuteDNS.files b/CuteDNS.files
index 86fc4723c..90f97772b 100644
--- a/CuteDNS.files
+++ b/CuteDNS.files
@@ -64,3 +64,4 @@ src/dnslib/node.h
 src/dnslib/node.c
 src/dnslib/consts.h
 src/tests/dnslib/dnslib_dname_tests.c
+src/tests/dnslib/dnslib_rdata_tests.c
diff --git a/src/tests/dnslib/dnslib_rdata_tests.c b/src/tests/dnslib/dnslib_rdata_tests.c
new file mode 100644
index 000000000..e27c66511
--- /dev/null
+++ b/src/tests/dnslib/dnslib_rdata_tests.c
@@ -0,0 +1,198 @@
+#include "tap_unit.h"
+
+#include "common.h"
+#include "rdata.h"
+//#include "node.h"
+
+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 = {
+   "DNS library - rdata",        //! Unit name
+   &dnslib_rdata_tests_count,  //! Count scheduled tests
+   &dnslib_rdata_tests_run     //! Run scheduled tests
+};
+
+/*
+ *  Unit implementation.
+ */
+
+// C will not accept const int in other const definition
+enum { TEST_RDATAS = 7 };
+
+static uint8_t *RDATA_ITEM_PTR = (uint8_t *)0xDEADBEEF;
+
+struct test_rdata {
+	uint items;
+};
+
+static const struct test_rdata
+		test_rdatas[TEST_RDATAS] = {
+	{ 1 },
+	{ 2 },
+	{ 3 },
+	{ 4 },
+	{ 5 },
+	{ 10 },
+	{ 100 },
+};
+
+/*!
+ * \brief Tests dnslib_rdata_new().
+ * \retval > 0 on success.
+ * \retval 0 otherwise.
+ */
+static int test_rdata_create( uint count )
+{
+	dnslib_rdata_t *rdata = dnslib_rdata_new(count);
+	if (rdata == NULL) {
+		diag("RDATA structure not created!");
+		return 0;
+	}
+
+	for (int i = 0; i < count; ++i) {
+		const dnslib_rdata_item_t *item;
+		if ((item = dnslib_rdata_get_item(rdata, i)) == NULL) {
+			diag("Missing RDATA item on position %d", i);
+			dnslib_rdata_free(rdata);
+			return 0;
+		} else if (item->dname != NULL) {
+			diag("RDATA item on position %d not properly initialized: %p"
+				 " (should be NULL).", i, item->dname);
+			dnslib_rdata_free(rdata);
+			return 0;
+		}
+	}
+
+	dnslib_rdata_free(rdata);
+	return 1;
+}
+
+/*!
+ * \brief Tests dnslib_rdata_free().
+ * \retval > 0 on success.
+ * \retval 0 otherwise.
+ */
+static int test_rdata_delete() {
+	// how to test this??
+	return 0;
+}
+
+static int check_rdata( const dnslib_rdata_t *rdata, int i )
+{
+	assert(rdata != NULL);
+
+	int errors = 0;
+
+	for (int j = 0; j < test_rdatas[i].items; ++j) {
+		const dnslib_rdata_item_t *item = dnslib_rdata_get_item(rdata, j);
+		if (item == NULL) {
+			diag("RDATA item at position %d NULL when it should not be!", j);
+			++errors;
+		} else if (item->raw_data != RDATA_ITEM_PTR + j) {
+			diag("RDATA item at position %d should be %p, but is %p!",
+				 RDATA_ITEM_PTR + j, item->raw_data);
+			++errors;
+		}
+	}
+
+	return errors;
+}
+
+static void set_rdata( dnslib_rdata_t *rdata, int i )
+{
+	assert(rdata != NULL);
+
+	dnslib_rdata_item_t item;
+	item.raw_data = RDATA_ITEM_PTR;
+
+	for (int j = 0; j < test_rdatas[i].items; ++j) {
+		dnslib_rdata_set_item(rdata, j, item);
+		++item.raw_data;
+	}
+}
+
+/*!
+ * \brief Tests dnslib_rdata_set_item().
+ * \retval > 0 on success.
+ * \retval 0 otherwise.
+ */
+static int test_rdata_set_item()
+{
+	dnslib_rdata_t *rdata;
+
+	for (int i = 0; i < TEST_RDATAS; ++i) {
+		rdata = dnslib_rdata_new(test_rdatas[i].items);
+		set_rdata(rdata, i);
+		if (check_rdata(rdata, i) != 0) {
+			dnslib_rdata_free(rdata);
+			return 0;
+		}
+		dnslib_rdata_free(rdata);
+	}
+
+	return 1;
+}
+
+//static int test_rdata_set_items( dnslib_rdata_t *rdata, uint count )
+//{
+//	assert(rdata != NULL);
+
+//	dnslib_rdata_item_t *items = (dnslib_rdata_item_t *)malloc(
+//			count * sizeof(dnslib_rdata_item_t));
+//	if (items == NULL) {
+//		diag("Error during executing test test_rdata_set_items()");
+//		return 0;
+//	}
+
+//	for (int i = 0; i < count; ++i) {
+//		items[i].raw_data = RDATA_ITEM_PTR + i;
+//	}
+
+//	return 1;
+
+//}
+
+
+static const int DNSLIB_RDATA_TEST_COUNT = 4;
+
+/*! This helper routine should report number of
+ *  scheduled tests for given parameters.
+ */
+static int dnslib_rdata_tests_count(int argc, char *argv[])
+{
+   return DNSLIB_RDATA_TEST_COUNT;
+}
+
+/*! Run all scheduled tests for given parameters.
+ */
+static int dnslib_rdata_tests_run(int argc, char *argv[])
+{
+	int res_create = 0;
+
+	res_create = test_rdata_create(0);
+	ok(res_create, "rdata: create empty");
+
+	skip(!res_create, 3);
+
+	res_create = test_rdata_create(TEST_RDATAS);
+	ok(res_create, "rdata: create non-empty");
+
+	skip(!res_create, 2);
+
+	todo();
+
+	ok(test_rdata_delete(), "rdata: delete");
+
+	endtodo;
+
+	ok(test_rdata_set_item(), "rdata: set items one-by-one");
+
+	endskip;	/* !res_create (count > 0) */
+
+	endskip;	/* !res_create (count == 0) */
+
+	return 0;
+}
diff --git a/src/tests/dnslib_tests.c b/src/tests/dnslib_tests.c
index 47d1826c4..0fc4f78e4 100644
--- a/src/tests/dnslib_tests.c
+++ b/src/tests/dnslib_tests.c
@@ -1,6 +1,7 @@
 #include "tap_unit.h"
 
 #include "dnslib/dnslib_dname_tests.c"
+#include "dnslib/dnslib_rdata_tests.c"
 
 static int dnslib_tests_count(int argc, char *argv[]);
 static int dnslib_tests_run(int argc, char *argv[]);
@@ -21,7 +22,8 @@ unit_api dnslib_tests_api = {
  */
 static int dnslib_tests_count(int argc, char *argv[])
 {
-   return dnslib_dname_tests_count(argc, argv);
+   return dnslib_dname_tests_count(argc, argv)
+		   + dnslib_rdata_tests_count(argc, argv);
 }
 
 /*! Run all scheduled tests for given parameters.
@@ -30,8 +32,10 @@ static int dnslib_tests_run(int argc, char *argv[])
 {
 	int res = 0;
 	// dname tests
-	note("dname tests...");
-	res = dnslib_dname_tests_run(argc, argv);
+	note("Testing module: dname");
+	res = dnslib_dname_tests_run(argc, argv);	
+	note("Testing module: rdata");
+	res += dnslib_rdata_tests_run(argc, argv);
 
 	return res;
 }
-- 
GitLab