zt_test.c revision 8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808c
a3128c1995310262648e575a9ff148d5741fd167Tinderbox User/*
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews *
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * Permission to use, copy, modify, and/or distribute this software for any
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * purpose with or without fee is hereby granted, provided that the above
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * copyright notice and this permission notice appear in all copies.
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews *
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * PERFORMANCE OF THIS SOFTWARE.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews */
12178c86525332bb0ab66155feb61fbf32eca6acEvan Hunt
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence/* $Id: zt_test.c,v 1.2 2011/09/02 21:15:37 each Exp $ */
fccb930434fc0c61658be2289dab263535109803William King
fccb930434fc0c61658be2289dab263535109803William King/*! \file */
fccb930434fc0c61658be2289dab263535109803William King
fccb930434fc0c61658be2289dab263535109803William King#include <config.h>
c2bc56dc65b4b103a5600565680eb5f33fa4c90bMark Andrews
51917258dbb23cfe6069ae1cf2b7fc5aefc1e0c2Bob Halley#include <atf-c.h>
da49cb62e02793a1b31ec877dcbfddc8911dbcb8David Lawrence
fccb930434fc0c61658be2289dab263535109803William King#include <unistd.h>
12178c86525332bb0ab66155feb61fbf32eca6acEvan Hunt
fccb930434fc0c61658be2289dab263535109803William King#include <isc/app.h>
fccb930434fc0c61658be2289dab263535109803William King#include <isc/buffer.h>
08a768e82ad64ede97f640c88e02984b59122753Michael Graff#include <isc/task.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <isc/timer.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ced719e92b11dc797da3a1b32f6140d81eca83faAndreas Gustafsson#include <dns/db.h>
08a768e82ad64ede97f640c88e02984b59122753Michael Graff#include <dns/name.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <dns/view.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <dns/zone.h>
08a768e82ad64ede97f640c88e02984b59122753Michael Graff#include <dns/zt.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
08a768e82ad64ede97f640c88e02984b59122753Michael Graff#include "dnstest.h"
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
fccb930434fc0c61658be2289dab263535109803William King/*
f4cbe536b11da614fe05aeaeff41e324854cda7bMark Andrews * Helper functions
fccb930434fc0c61658be2289dab263535109803William King */
8a0ba20b16177d40bd557b491f56003c7d38a4a2David Lawrencestatic isc_result_t
8a0ba20b16177d40bd557b491f56003c7d38a4a2David Lawrencecount_zone(dns_zone_t *zone, void *uap) {
fccb930434fc0c61658be2289dab263535109803William King int *nzones = (int *)uap;
fccb930434fc0c61658be2289dab263535109803William King
f4cbe536b11da614fe05aeaeff41e324854cda7bMark Andrews UNUSED(zone);
9f7d51ee3290e2a064d71016a6bd555b47134a7cMark Andrews
fccb930434fc0c61658be2289dab263535109803William King *nzones += 1;
f4cbe536b11da614fe05aeaeff41e324854cda7bMark Andrews return (ISC_R_SUCCESS);
94271b0d993bfe4d4495208288e856e048a2d158Mark Andrews}
fccb930434fc0c61658be2289dab263535109803William King
fccb930434fc0c61658be2289dab263535109803William Kingstatic isc_result_t
f4cbe536b11da614fe05aeaeff41e324854cda7bMark Andrewsload_done(dns_zt_t *zt, dns_zone_t *zone) {
fccb930434fc0c61658be2289dab263535109803William King /* We treat zt as a pointer to a boolean for testing purposes */
fccb930434fc0c61658be2289dab263535109803William King isc_boolean_t *done = (isc_boolean_t *) zt;
fccb930434fc0c61658be2289dab263535109803William King
UNUSED(zone);
*done = ISC_TRUE;
isc_app_shutdown();
return (ISC_R_SUCCESS);
}
static isc_result_t
all_done(void *arg) {
isc_boolean_t *done = (isc_boolean_t *) arg;
*done = ISC_TRUE;
isc_app_shutdown();
return (ISC_R_SUCCESS);
}
/*
* Individual unit tests
*/
ATF_TC(apply);
ATF_TC_HEAD(apply, tc) {
atf_tc_set_md_var(tc, "descr", "apply a function to a zone table");
}
ATF_TC_BODY(apply, tc) {
isc_result_t result;
dns_zone_t *zone = NULL;
dns_view_t *view = NULL;
int nzones = 0;
UNUSED(tc);
result = dns_test_begin(NULL, ISC_TRUE);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
result = dns_test_makezone("foo", &zone, NULL, ISC_TRUE);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
view = dns_zone_getview(zone);
ATF_REQUIRE(view->zonetable != NULL);
ATF_CHECK_EQ(0, nzones);
result = dns_zt_apply(view->zonetable, ISC_FALSE, count_zone, &nzones);
ATF_CHECK_EQ(1, nzones);
/* These steps are necessary so the zone can be detached properly */
result = dns_test_setupzonemgr();
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
result = dns_test_managezone(zone);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
dns_test_releasezone(zone);
dns_test_closezonemgr();
/* The view was left attached in dns_test_makezone() */
dns_view_detach(&view);
dns_zone_detach(&zone);
dns_test_end();
}
ATF_TC(asyncload_zone);
ATF_TC_HEAD(asyncload_zone, tc) {
atf_tc_set_md_var(tc, "descr", "asynchronous zone load");
}
ATF_TC_BODY(asyncload_zone, tc) {
isc_result_t result;
dns_zone_t *zone = NULL;
dns_view_t *view = NULL;
dns_db_t *db = NULL;
isc_boolean_t done = ISC_FALSE;
int i = 0;
UNUSED(tc);
result = dns_test_begin(NULL, ISC_TRUE);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
result = dns_test_makezone("foo", &zone, NULL, ISC_TRUE);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
result = dns_test_setupzonemgr();
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
result = dns_test_managezone(zone);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
view = dns_zone_getview(zone);
ATF_REQUIRE(view->zonetable != NULL);
ATF_CHECK(!dns__zone_loadpending(zone));
ATF_CHECK(!done);
dns_zone_setfile(zone, "testdata/zt/zone1.db");
dns_zone_asyncload(zone, load_done, (void *) &done);
ATF_CHECK(dns__zone_loadpending(zone));
isc_app_run();
while (dns__zone_loadpending(zone) && i++ < 5000)
dns_test_nap(1000);
ATF_CHECK(done);
/* The zone should now be loaded; test it */
result = dns_zone_getdb(zone, &db);
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
ATF_CHECK(db != NULL);
if (db != NULL)
dns_db_detach(&db);
dns_test_releasezone(zone);
dns_test_closezonemgr();
dns_zone_detach(&zone);
dns_view_detach(&view);
dns_test_end();
}
ATF_TC(asyncload_zt);
ATF_TC_HEAD(asyncload_zt, tc) {
atf_tc_set_md_var(tc, "descr", "asynchronous zone table load");
}
ATF_TC_BODY(asyncload_zt, tc) {
isc_result_t result;
dns_zone_t *zone1 = NULL, *zone2 = NULL, *zone3 = NULL;
dns_view_t *view;
dns_zt_t *zt;
dns_db_t *db = NULL;
isc_boolean_t done = ISC_FALSE;
int i = 0;
UNUSED(tc);
result = dns_test_begin(NULL, ISC_TRUE);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
result = dns_test_makezone("foo", &zone1, NULL, ISC_TRUE);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
dns_zone_setfile(zone1, "testdata/zt/zone1.db");
view = dns_zone_getview(zone1);
result = dns_test_makezone("bar", &zone2, view, ISC_TRUE);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
dns_zone_setfile(zone2, "testdata/zt/zone1.db");
/* This one will fail to load */
result = dns_test_makezone("fake", &zone3, view, ISC_TRUE);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
dns_zone_setfile(zone3, "testdata/zt/nonexistent.db");
zt = view->zonetable;
ATF_REQUIRE(zt != NULL);
result = dns_test_setupzonemgr();
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
result = dns_test_managezone(zone1);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
result = dns_test_managezone(zone2);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
result = dns_test_managezone(zone3);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
ATF_CHECK(!dns__zone_loadpending(zone1));
ATF_CHECK(!dns__zone_loadpending(zone2));
ATF_CHECK(!done);
dns_zt_asyncload(zt, all_done, (void *) &done);
isc_app_run();
while (!done && i++ < 5000)
dns_test_nap(1000);
ATF_CHECK(done);
/* Both zones should now be loaded; test them */
result = dns_zone_getdb(zone1, &db);
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
ATF_CHECK(db != NULL);
if (db != NULL)
dns_db_detach(&db);
result = dns_zone_getdb(zone2, &db);
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
ATF_CHECK(db != NULL);
if (db != NULL)
dns_db_detach(&db);
dns_test_releasezone(zone3);
dns_test_releasezone(zone2);
dns_test_releasezone(zone1);
dns_test_closezonemgr();
dns_zone_detach(&zone1);
dns_zone_detach(&zone2);
dns_zone_detach(&zone3);
dns_view_detach(&view);
dns_test_end();
}
/*
* Main
*/
ATF_TP_ADD_TCS(tp) {
ATF_TP_ADD_TC(tp, apply);
ATF_TP_ADD_TC(tp, asyncload_zone);
ATF_TP_ADD_TC(tp, asyncload_zt);
return (atf_no_error());
}