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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
#if HAVE_CMOCKA
#include <sched.h> /* IWYU pragma: keep */
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#define UNIT_TESTING
#include <cmocka.h>
#include <isc/app.h>
#include <isc/buffer.h>
#include <isc/print.h>
#include <isc/socket.h>
#include <isc/task.h>
#include <isc/timer.h>
#include <isc/util.h>
#include <dns/dispatch.h>
#include <dns/name.h>
#include <dns/resolver.h>
#include <dns/view.h>
#include "dnstest.h"
static dns_dispatchmgr_t *dispatchmgr = NULL;
static dns_dispatch_t *dispatch = NULL;
static dns_view_t *view = NULL;
static int
_setup(void **state) {
isc_result_t result;
isc_sockaddr_t local;
UNUSED(state);
result = dns_test_begin(NULL, true);
assert_int_equal(result, ISC_R_SUCCESS);
result = dns_dispatchmgr_create(dt_mctx, &dispatchmgr);
assert_int_equal(result, ISC_R_SUCCESS);
result = dns_test_makeview("view", &view);
assert_int_equal(result, ISC_R_SUCCESS);
isc_sockaddr_any(&local);
result = dns_dispatch_getudp(dispatchmgr, socketmgr, taskmgr, &local,
4096, 100, 100, 100, 500, 0, 0, &dispatch);
assert_int_equal(result, ISC_R_SUCCESS);
return (0);
}
static int
_teardown(void **state) {
UNUSED(state);
dns_dispatch_detach(&dispatch);
dns_view_detach(&view);
dns_dispatchmgr_destroy(&dispatchmgr);
dns_test_end();
return (0);
}
static void
mkres(dns_resolver_t **resolverp) {
isc_result_t result;
result = dns_resolver_create(view, taskmgr, 1, 1, socketmgr, timermgr,
0, dispatchmgr, dispatch, NULL, resolverp);
assert_int_equal(result, ISC_R_SUCCESS);
}
static void
destroy_resolver(dns_resolver_t **resolverp) {
dns_resolver_shutdown(*resolverp);
dns_resolver_detach(resolverp);
}
/* dns_resolver_create */
static void
create_test(void **state) {
dns_resolver_t *resolver = NULL;
UNUSED(state);
mkres(&resolver);
destroy_resolver(&resolver);
}
/* dns_resolver_gettimeout */
static void
gettimeout_test(void **state) {
dns_resolver_t *resolver = NULL;
unsigned int timeout;
UNUSED(state);
mkres(&resolver);
timeout = dns_resolver_gettimeout(resolver);
assert_true(timeout > 0);
destroy_resolver(&resolver);
}
/* dns_resolver_settimeout */
static void
settimeout_test(void **state) {
dns_resolver_t *resolver = NULL;
unsigned int default_timeout, timeout;
UNUSED(state);
mkres(&resolver);
default_timeout = dns_resolver_gettimeout(resolver);
dns_resolver_settimeout(resolver, default_timeout + 1);
timeout = dns_resolver_gettimeout(resolver);
assert_true(timeout == default_timeout + 1);
destroy_resolver(&resolver);
}
/* dns_resolver_settimeout */
static void
settimeout_default_test(void **state) {
dns_resolver_t *resolver = NULL;
unsigned int default_timeout, timeout;
UNUSED(state);
mkres(&resolver);
default_timeout = dns_resolver_gettimeout(resolver);
dns_resolver_settimeout(resolver, default_timeout + 100);
timeout = dns_resolver_gettimeout(resolver);
assert_int_equal(timeout, default_timeout + 100);
dns_resolver_settimeout(resolver, 0);
timeout = dns_resolver_gettimeout(resolver);
assert_int_equal(timeout, default_timeout);
destroy_resolver(&resolver);
}
/* dns_resolver_settimeout below minimum */
static void
settimeout_belowmin_test(void **state) {
dns_resolver_t *resolver = NULL;
unsigned int default_timeout, timeout;
UNUSED(state);
mkres(&resolver);
default_timeout = dns_resolver_gettimeout(resolver);
dns_resolver_settimeout(resolver, 9000);
timeout = dns_resolver_gettimeout(resolver);
assert_int_equal(timeout, default_timeout);
destroy_resolver(&resolver);
}
/* dns_resolver_settimeout over maximum */
static void
settimeout_overmax_test(void **state) {
dns_resolver_t *resolver = NULL;
unsigned int timeout;
UNUSED(state);
mkres(&resolver);
dns_resolver_settimeout(resolver, 4000000);
timeout = dns_resolver_gettimeout(resolver);
assert_in_range(timeout, 0, 3999999);
destroy_resolver(&resolver);
}
int
main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup_teardown(create_test, _setup, _teardown),
cmocka_unit_test_setup_teardown(gettimeout_test, _setup,
_teardown),
cmocka_unit_test_setup_teardown(settimeout_test, _setup,
_teardown),
cmocka_unit_test_setup_teardown(settimeout_default_test, _setup,
_teardown),
cmocka_unit_test_setup_teardown(settimeout_belowmin_test,
_setup, _teardown),
cmocka_unit_test_setup_teardown(settimeout_overmax_test, _setup,
_teardown),
};
return (cmocka_run_group_tests(tests, NULL, NULL));
}
#else /* HAVE_CMOCKA */
#include <stdio.h>
int
main(void) {
printf("1..0 # Skipped: cmocka not available\n");
return (SKIPPED_TEST_EXIT_CODE);
}
#endif /* if HAVE_CMOCKA */
|