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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/* test.c
* (c) Jonas Gunz, 2020
* License: MIT
* */
#include "test.h"
#ifdef _TEST
void run_test ()
{
log_init_stdout(_LOG_DEBUG);
//Space for temporary tests
//tree_balanced_insert(NULL, NULL, NULL, 15 );
//
//Normal tests
int ret = 0;
ret += test_tree();
ret += test_dns_parsing();
ret += test_dns_message_fuzz();
ret += test_dns_qname_fuzz();
ret += test_database();
exit(ret);
}
int test_tree ()
{
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;
printf("\n-> test_tree()\n======\n\n");
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 ++;
}
}
printf("len_cnt %i\n", len_cnt);
tree_balanced_insert( &root, (void**)data, keys, len );
printf("After Insert\n");
printf("%s\n", (char*)tree_get(&root, "aa"));
for ( i = 0; i < len; i++ ) {
if ( strcmp( tree_get(&root, keys[i]), data[i] ) )
LOGPRINTF(_LOG_WARNING, "Data does not match for %s", keys[i]);
}
printf("After Get\n");
tree_destroy (&root, _TREE_FREE_DATA | _TREE_FREE_KEY);
printf("After destroy\n");
return 0;
}
int test_dns_parsing ()
{
int i;
printf("\n-> test_dns_parsing()\n======\n\n");
char in[128];
char out[128];
strncpy ( in, "sub.domain.example.com\0", 127);
printf("%s\n", in);
int written = fqdn_to_qname (in,128,out,128);
if ( qname_check(out,128) < 0)
printf("Wrong\n");
else
printf("qname ok\n");
if (written < 0) {
printf("invallid fqdn\n");
return 1;
}
for(i = 0; i < written; i++)
printf(" %x ", out[i]);
written = qname_to_fqdn (out,128,in,128);
if (written < 0) {
printf("invalid qname\n");
return 1;
}
printf("%s\n", in);
printf("\n\n");
return 0;
}
int test_dns_qname_fuzz()
{
printf("\n-> test_parsing_fuzz()\n======\n\n");
FILE* urand = fopen ("/dev/urandom", "r");
char rand[128];
char out[129];
out[128] = 0;
if (!urand)
return 1;
unsigned long int limit = 10000000;
unsigned long int valid_cnt = 0;
for (unsigned long int i = 0; i < limit; i++) {
if (fread (rand, 128, 1, urand) > 0) {
if ( qname_check(rand, 128) > 0 ) {
qname_to_fqdn ( rand, 128, out, 128);
//printf("Valid %s\n", out);
valid_cnt++;
}
}
}
float valid_percent = (float)valid_cnt / (float)limit * 100;
printf("# of valid qnames in random data: %lu / %lu = %f%%\n", valid_cnt, limit, valid_percent);
return 0;
}
int test_dns_message_fuzz()
{
printf("\n-> test_dns_message_fuzz()\n======\n\n");
FILE* urand = fopen ("/dev/urandom", "r");
char rand[128];
if (!urand)
return 1;
unsigned long int limit = 10000000;
unsigned long int valid_cnt = 0;
struct dns_message msg;
for (unsigned long int i = 0; i < limit; i++) {
if (fread (rand, 128, 1, urand) > 0) {
if ( ! dns_parse_packet(rand, 128, &msg) ) {
valid_cnt++;
}
}
}
float valid_percent = (float)valid_cnt / (float)limit * 100;
printf("# of valid messages in random data: %lu / %lu = %f%%\n", valid_cnt, limit, valid_percent);
return 0;
}
int test_database(){
struct database db;
struct database_rdata rdata;
if ( database_populate( &db, "nofile" ) )
return 1;
printf("Populated\n");
char* qname = malloc(32);
int len = fqdn_to_qname( "test.example.com.", 18, qname, 32 );
int ret = database_query ( &rdata, &db, qname, len, 1, 1 );
printf("Return code %i, rdlen %i\n", ret, rdata.rdlen);
database_destroy( &db );
free(qname);
qname = NULL;
return 0;
}
#endif
|