aboutsummaryrefslogtreecommitdiff
path: root/tests/database.c
blob: c1bd9ed490307eaba9a63fb1d84205e7dacb8f3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
 * tests/database.c
 * (c) 2021 Jonas Gunz <himself@jonasgunz.de>
 * License: MIT
 */

#include "tests.h"

#include "../src/database.h"
#include "../src/zonefile.h"
#include <check.h>

START_TEST ( test_database_parse_zonefile ) {
	int ret;
	char qname[128];
	int qname_len;
	database_t db;
	database_rdata_t data;

	qname_len = fqdn_to_qname("test.example.com.", 18, qname, 128);
	ck_assert_int_gt(qname_len, 0);

	database_init( &db );
	ck_assert_ptr_nonnull( db.zone );

	ret = zonefile_to_database( &db, "tests/zone.file" );
	ck_assert_int_eq(ret,0);

	ret = database_query( &data, &db, qname, qname_len, 1, 1 );
	ck_assert_int_eq(ret, 0);

	database_destroy(&db);
	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;
}