aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorGravatar Jonas Gunz <himself@jonasgunz.de> 2021-06-02 00:37:20 +0200
committerGravatar Jonas Gunz <himself@jonasgunz.de> 2021-06-02 00:37:20 +0200
commitae7d4f67ba782f7f53deaf0596a2b52155617a6b (patch)
tree4b9fb0a726d9a193e3a61db6e13a67676267394c /tests
parent1418d5cc62f2f888aed5c033fa3fd9f50eb67235 (diff)
downloaddns-ae7d4f67ba782f7f53deaf0596a2b52155617a6b.tar.gz
add dns tests
Diffstat (limited to 'tests')
-rw-r--r--tests/dns.c50
-rw-r--r--tests/main.c35
-rw-r--r--tests/tests.h15
3 files changed, 80 insertions, 20 deletions
diff --git a/tests/dns.c b/tests/dns.c
new file mode 100644
index 0000000..c100cd4
--- /dev/null
+++ b/tests/dns.c
@@ -0,0 +1,50 @@
+/*
+ * tests/dns.c
+ * (c) 2021 Jonas Gunz <himself@jonasgunz.de>
+ * License: MIT
+ */
+
+#include "tests.h"
+
+#include <dns.h>
+
+START_TEST (dns_qname) {
+ char in[128];
+ char out[128];
+
+ strncpy ( in, "sub.domain.example.com\0", 127);
+
+ ck_assert_int_gt( fqdn_to_qname (in,128,out,128), 0 );
+ ck_assert_int_ge( qname_check(out,128), 0 );
+ ck_assert_int_gt( qname_to_fqdn (out,128,in,128), 0);
+} END_TEST
+
+START_TEST (dns_qname_fuzz) {
+ FILE* urand = fopen ("/dev/urandom", "r");
+ char rand[128];
+ const unsigned long int limit = 1000000;
+ unsigned long int valid_cnt = 0;
+ unsigned long int i;
+
+ if ( !urand )
+ ck_abort_msg("Failed to open /dev/urandom");
+
+ for ( i = 0; i < limit; i++) {
+ if (fread (rand, 128, 1, urand) > 0) {
+ if ( qname_check(rand, 128) > 0 ) {
+ valid_cnt++;
+ }
+ }
+ }
+
+ ck_assert_float_le( (float)valid_cnt / (float)limit * 100, 10);
+}
+
+TCase* test_dns(void) {
+ TCase *tc = tcase_create("DNS");
+
+ tcase_add_test(tc, dns_qname);
+ tcase_add_test(tc, dns_qname_fuzz);
+
+ return tc;
+}
diff --git a/tests/main.c b/tests/main.c
index 65d527f..4b1cfd2 100644
--- a/tests/main.c
+++ b/tests/main.c
@@ -1,33 +1,28 @@
-#include <check.h>
-
-START_TEST (test_example) {
- ck_abort();
-} END_TEST
-
-Suite* tests(void) {
- Suite *s;
- TCase *tc_core;
-
- s = suite_create("DNS Tests");
-
- tc_core = tcase_create("Core");
+/*
+ * tests/main.c
+ * (c) 2021 Jonas Gunz <himself@jonasgunz.de>
+ * License: MIT
+ */
- tcase_add_test(tc_core, test_example);
- suite_add_tcase(s, tc_core);
+#include <check.h>
- return s;
-}
+#include "tests.h"
int main() {
Suite *s;
SRunner *sr;
+ int failed;
- s = tests();
- sr = srunner_create(s);
+ s = suite_create("All Tests");
+ suite_add_tcase(s, test_dns());
+
+ sr = srunner_create(s);
srunner_run_all(sr,CK_NORMAL);
+ failed = srunner_ntests_failed(sr);
+
srunner_free(sr);
- return srunner_ntests_failed(sr);
+ return failed;
}
diff --git a/tests/tests.h b/tests/tests.h
new file mode 100644
index 0000000..8af805f
--- /dev/null
+++ b/tests/tests.h
@@ -0,0 +1,15 @@
+/*
+ * tests/tests.h
+ * (c) 2021 Jonas Gunz <himself@jonasgunz.de>
+ * License: MIT
+ */
+
+#pragma once
+
+#include <check.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+TCase* test_dns(void);