1/*
2 * Copyright (C) 2011, 2012  Internet Systems Consortium, Inc. ("ISC")
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
15 */
16
17/* $Id$ */
18
19/*! \file */
20
21#include <config.h>
22
23#include <atf-c.h>
24
25#include <unistd.h>
26
27#include <isc/buffer.h>
28#include <isc/task.h>
29#include <isc/timer.h>
30
31#include <dns/name.h>
32#include <dns/view.h>
33#include <dns/zone.h>
34
35#include "dnstest.h"
36
37static isc_result_t
38make_zone(const char *name, dns_zone_t **zonep) {
39	isc_result_t result;
40	dns_view_t *view = NULL;
41	dns_zone_t *zone = NULL;
42	isc_buffer_t buffer;
43	dns_fixedname_t fixorigin;
44	dns_name_t *origin;
45
46	CHECK(dns_view_create(mctx, dns_rdataclass_in, "view", &view));
47	CHECK(dns_zone_create(&zone, mctx));
48
49	isc_buffer_init(&buffer, name, strlen(name));
50	isc_buffer_add(&buffer, strlen(name));
51	dns_fixedname_init(&fixorigin);
52	origin = dns_fixedname_name(&fixorigin);
53	CHECK(dns_name_fromtext(origin, &buffer, dns_rootname, 0, NULL));
54	CHECK(dns_zone_setorigin(zone, origin));
55	dns_zone_setview(zone, view);
56	dns_zone_settype(zone, dns_zone_master);
57	dns_zone_setclass(zone, view->rdclass);
58
59	dns_view_detach(&view);
60	*zonep = zone;
61
62	return (ISC_R_SUCCESS);
63
64  cleanup:
65	if (zone != NULL)
66		dns_zone_detach(&zone);
67	if (view != NULL)
68		dns_view_detach(&view);
69	return (result);
70}
71
72/*
73 * Individual unit tests
74 */
75ATF_TC(zonemgr_create);
76ATF_TC_HEAD(zonemgr_create, tc) {
77	atf_tc_set_md_var(tc, "descr", "create zone manager");
78}
79ATF_TC_BODY(zonemgr_create, tc) {
80	dns_zonemgr_t *zonemgr = NULL;
81	isc_result_t result;
82
83	UNUSED(tc);
84
85	result = dns_test_begin(NULL, ISC_TRUE);
86	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
87
88	result = dns_zonemgr_create(mctx, taskmgr, timermgr, socketmgr,
89				    &zonemgr);
90	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
91
92	dns_zonemgr_shutdown(zonemgr);
93	dns_zonemgr_detach(&zonemgr);
94	ATF_REQUIRE_EQ(zonemgr, NULL);
95
96	dns_test_end();
97}
98
99
100ATF_TC(zonemgr_managezone);
101ATF_TC_HEAD(zonemgr_managezone, tc) {
102	atf_tc_set_md_var(tc, "descr", "manage and release a zone");
103}
104ATF_TC_BODY(zonemgr_managezone, tc) {
105	dns_zonemgr_t *zonemgr = NULL;
106	dns_zone_t *zone = NULL;
107	isc_result_t result;
108
109	UNUSED(tc);
110
111	result = dns_test_begin(NULL, ISC_TRUE);
112	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
113
114	result = dns_zonemgr_create(mctx, taskmgr, timermgr, socketmgr,
115				    &zonemgr);
116	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
117
118	result = make_zone("foo", &zone);
119	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
120
121	/* This should not succeed until the dns_zonemgr_setsize() is run */
122	result = dns_zonemgr_managezone(zonemgr, zone);
123	ATF_REQUIRE_EQ(result, ISC_R_FAILURE);
124
125	ATF_REQUIRE_EQ(dns_zonemgr_getcount(zonemgr, DNS_ZONESTATE_ANY), 0);
126
127	result = dns_zonemgr_setsize(zonemgr, 1);
128	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
129
130	/* Now it should succeed */
131	result = dns_zonemgr_managezone(zonemgr, zone);
132	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
133
134	ATF_REQUIRE_EQ(dns_zonemgr_getcount(zonemgr, DNS_ZONESTATE_ANY), 1);
135
136	dns_zonemgr_releasezone(zonemgr, zone);
137	dns_zone_detach(&zone);
138
139	ATF_REQUIRE_EQ(dns_zonemgr_getcount(zonemgr, DNS_ZONESTATE_ANY), 0);
140
141	dns_zonemgr_shutdown(zonemgr);
142	dns_zonemgr_detach(&zonemgr);
143	ATF_REQUIRE_EQ(zonemgr, NULL);
144
145	dns_test_end();
146}
147
148ATF_TC(zonemgr_unreachable);
149ATF_TC_HEAD(zonemgr_unreachable, tc) {
150	atf_tc_set_md_var(tc, "descr", "manage and release a zone");
151}
152ATF_TC_BODY(zonemgr_unreachable, tc) {
153	dns_zonemgr_t *zonemgr = NULL;
154	dns_zone_t *zone = NULL;
155	isc_sockaddr_t addr1, addr2;
156	struct in_addr in;
157	isc_result_t result;
158	isc_time_t now;
159
160	UNUSED(tc);
161
162	TIME_NOW(&now);
163
164	result = dns_test_begin(NULL, ISC_TRUE);
165
166	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
167
168	result = dns_zonemgr_create(mctx, taskmgr, timermgr, socketmgr,
169				    &zonemgr);
170	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
171
172	result = dns_test_makezone("foo", &zone, NULL, ISC_FALSE);
173	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
174
175	result = dns_zonemgr_setsize(zonemgr, 1);
176	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
177
178	result = dns_zonemgr_managezone(zonemgr, zone);
179	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
180
181	in.s_addr = inet_addr("10.53.0.1");
182	isc_sockaddr_fromin(&addr1, &in, 2112);
183	in.s_addr = inet_addr("10.53.0.2");
184	isc_sockaddr_fromin(&addr2, &in, 5150);
185	ATF_CHECK(! dns_zonemgr_unreachable(zonemgr, &addr1, &addr2, &now));
186	dns_zonemgr_unreachableadd(zonemgr, &addr1, &addr2, &now);
187	ATF_CHECK(dns_zonemgr_unreachable(zonemgr, &addr1, &addr2, &now));
188
189	in.s_addr = inet_addr("10.53.0.3");
190	isc_sockaddr_fromin(&addr2, &in, 5150);
191	ATF_CHECK(! dns_zonemgr_unreachable(zonemgr, &addr1, &addr2, &now));
192	dns_zonemgr_unreachableadd(zonemgr, &addr1, &addr2, &now);
193	ATF_CHECK(dns_zonemgr_unreachable(zonemgr, &addr1, &addr2, &now));
194
195	dns_zonemgr_unreachabledel(zonemgr, &addr1, &addr2);
196	ATF_CHECK(! dns_zonemgr_unreachable(zonemgr, &addr1, &addr2, &now));
197
198	in.s_addr = inet_addr("10.53.0.2");
199	isc_sockaddr_fromin(&addr2, &in, 5150);
200	ATF_CHECK(dns_zonemgr_unreachable(zonemgr, &addr1, &addr2, &now));
201	dns_zonemgr_unreachabledel(zonemgr, &addr1, &addr2);
202	ATF_CHECK(! dns_zonemgr_unreachable(zonemgr, &addr1, &addr2, &now));
203
204	dns_zonemgr_releasezone(zonemgr, zone);
205	dns_zone_detach(&zone);
206	dns_zonemgr_shutdown(zonemgr);
207	dns_zonemgr_detach(&zonemgr);
208	ATF_REQUIRE_EQ(zonemgr, NULL);
209
210	dns_test_end();
211}
212
213
214/*
215 * Main
216 */
217ATF_TP_ADD_TCS(tp) {
218	ATF_TP_ADD_TC(tp, zonemgr_create);
219	ATF_TP_ADD_TC(tp, zonemgr_managezone);
220	ATF_TP_ADD_TC(tp, zonemgr_unreachable);
221	return (atf_no_error());
222}
223
224/*
225 * XXX:
226 * dns_zonemgr API calls that are not yet part of this unit test:
227 *
228 * 	- dns_zonemgr_attach
229 * 	- dns_zonemgr_forcemaint
230 * 	- dns_zonemgr_resumexfrs
231 * 	- dns_zonemgr_shutdown
232 * 	- dns_zonemgr_setsize
233 * 	- dns_zonemgr_settransfersin
234 * 	- dns_zonemgr_getttransfersin
235 * 	- dns_zonemgr_settransfersperns
236 * 	- dns_zonemgr_getttransfersperns
237 * 	- dns_zonemgr_setiolimit
238 * 	- dns_zonemgr_getiolimit
239 * 	- dns_zonemgr_dbdestroyed
240 * 	- dns_zonemgr_setserialqueryrate
241 * 	- dns_zonemgr_getserialqueryrate
242 */
243