From 4127daa65ed8962900b386e138577a83bbc6baa5 Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Tue, 21 Sep 2021 21:37:49 +0200 Subject: test: database --- src/database.c | 3 +++ tests/database.c | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/database.c b/src/database.c index 21174f7..491fae1 100644 --- a/src/database.c +++ b/src/database.c @@ -10,6 +10,9 @@ int database_init ( database_t* _database ) { unsigned int i = 0; size_t rr_size; + if ( !_database ) + return 1; + /* Initialize 2D array of tree_node pointers, paranoia style */ if ( !( _database->zone = malloc( sizeof( tree_node_t** ) * DB_CLASS_LEN ) ) ) return 1; diff --git a/tests/database.c b/tests/database.c index 18c7ec4..c1bd9ed 100644 --- a/tests/database.c +++ b/tests/database.c @@ -8,6 +8,7 @@ #include "../src/database.h" #include "../src/zonefile.h" +#include START_TEST ( test_database_parse_zonefile ) { int ret; @@ -32,11 +33,31 @@ START_TEST ( test_database_parse_zonefile ) { ck_assert_ptr_null( db.zone ); } END_TEST +START_TEST (test_database_error_handling) { + database_t db; + database_rdata_t rdata; + database_init(&db); + + ck_assert_int_ne(database_init(NULL), 0); + ck_assert_int_ne(database_destroy(NULL), 0); + ck_assert_int_ne(database_query(NULL, NULL, NULL, 0, 0, 0), 0); + + /* Record not found */ + ck_assert_int_ne(database_query(&rdata, &db, "aa", 3, 1, 1), 0); + + /* Invalid record */ + ck_assert_int_ne(database_query(&rdata, &db, "aa", 3, 1, 777), 0); + ck_assert_int_ne(database_query(&rdata, &db, "aa", 3, 777, 1), 0); + + database_destroy(&db); +} END_TEST + TCase* test_database(void) { TCase *tc = tcase_create("Database"); tcase_add_test(tc, test_database_parse_zonefile); + tcase_add_test(tc, test_database_error_handling); return tc; } -- cgit v1.2.3