diff options
author | Jonas Gunz <himself@jonasgunz.de> | 2021-06-14 20:59:28 +0200 |
---|---|---|
committer | Jonas Gunz <himself@jonasgunz.de> | 2021-06-14 20:59:28 +0200 |
commit | 0b6026ae2a96c4757688af51d927261536cc2873 (patch) | |
tree | 7b125403def39f9d7b87bc19cf0cc4c732e665ad /tests | |
parent | 64f446220da6f567c818d00a650f92d25391d6e5 (diff) | |
download | dns-0b6026ae2a96c4757688af51d927261536cc2873.tar.gz |
tests
Diffstat (limited to 'tests')
-rw-r--r-- | tests/dns.c | 28 | ||||
-rw-r--r-- | tests/main.c | 5 | ||||
-rw-r--r-- | tests/tests.h | 1 | ||||
-rw-r--r-- | tests/tree.c | 52 |
4 files changed, 84 insertions, 2 deletions
diff --git a/tests/dns.c b/tests/dns.c index 9b8a1e9..e75f209 100644 --- a/tests/dns.c +++ b/tests/dns.c @@ -38,13 +38,39 @@ START_TEST (dns_qname_fuzz) { } ck_assert_float_le( (float)valid_cnt / (float)limit * 100, 10); -} +} END_TEST + +START_TEST (dns_message_fuzz) { + const unsigned long int limit = 1000000; + unsigned long int valid_cnt = 0; + unsigned long int i; + + struct dns_message msg; + + FILE* urand = fopen ("/dev/urandom", "r"); + char rand[128]; + + if (!urand) + ck_abort_msg("Failed to open /dev/urandom"); + + + for (i = 0; i < limit; i++) { + if (fread (rand, 128, 1, urand) > 0) { + if ( ! dns_parse_packet(rand, 128, &msg) ) { + valid_cnt++; + } + } + } + + ck_assert_float_le( (float)valid_cnt / (float)limit * 100, 10); +} END_TEST TCase* test_dns(void) { TCase *tc = tcase_create("DNS"); tcase_add_test(tc, dns_qname); tcase_add_test(tc, dns_qname_fuzz); + tcase_add_test(tc, dns_message_fuzz); return tc; } diff --git a/tests/main.c b/tests/main.c index 4b1cfd2..3f60a3a 100644 --- a/tests/main.c +++ b/tests/main.c @@ -4,6 +4,8 @@ * License: MIT */ +/* https://libcheck.github.io/check/doc/doxygen/html/check_8h.html */ + #include <check.h> #include "tests.h" @@ -16,9 +18,10 @@ int main() { s = suite_create("All Tests"); suite_add_tcase(s, test_dns()); + suite_add_tcase(s, test_tree()); sr = srunner_create(s); - srunner_run_all(sr,CK_NORMAL); + srunner_run_all(sr,CK_VERBOSE); failed = srunner_ntests_failed(sr); diff --git a/tests/tests.h b/tests/tests.h index 8af805f..c3f576e 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -13,3 +13,4 @@ #include <string.h> TCase* test_dns(void); +TCase* test_tree(void); diff --git a/tests/tree.c b/tests/tree.c new file mode 100644 index 0000000..42bf248 --- /dev/null +++ b/tests/tree.c @@ -0,0 +1,52 @@ +/* + * tests/tree.c + * (c) 2021 Jonas Gunz <himself@jonasgunz.de> + * License: MIT + */ + +#include "tests.h" + +#include "../src/tree.h" + +#include <check.h> + +START_TEST(tree_basic) { + unsigned int i, j; + + unsigned const int len = pow ( 'z' - 'a' + 1, 2); + unsigned int len_cnt = 0; + char* keys[len]; + char* data[len]; + + struct tree_node* root = NULL; + + for ( i = 'a'; i <= 'z'; i++ ) { + for ( j = 'a'; j <= 'z'; j++ ) { + keys[len_cnt] = malloc (3); + keys[len_cnt][0] = i; + keys[len_cnt][1] = j; + keys[len_cnt][2] = 0; + + data[len_cnt] = malloc(10); + snprintf( data[len_cnt], 10, "N%i", len_cnt ); + + len_cnt ++; + } + } + + ck_assert_int_eq(tree_balanced_insert( &root, (void**)data, keys, len ),0); + + for ( i = 0; i < len; i++ ) { + ck_assert_str_eq( tree_get(&root, keys[i]), data[i] ); + } + + ck_assert_int_eq( tree_destroy (&root, _TREE_FREE_DATA | _TREE_FREE_KEY), 0 ); +} END_TEST + +TCase* test_tree(void) { + TCase *tc = tcase_create("Tree"); + + tcase_add_test(tc, tree_basic); + + return tc; +} |