zt_test.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011, 2012  Internet Systems Consortium, Inc. ("ISC")
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00009  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00010  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00011  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00012  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00013  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00014  * PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 
00017 /* $Id$ */
00018 
00019 /*! \file */
00020 
00021 #include <config.h>
00022 
00023 #include <atf-c.h>
00024 
00025 #include <unistd.h>
00026 
00027 #include <isc/app.h>
00028 #include <isc/buffer.h>
00029 #include <isc/task.h>
00030 #include <isc/timer.h>
00031 
00032 #include <dns/db.h>
00033 #include <dns/name.h>
00034 #include <dns/view.h>
00035 #include <dns/zone.h>
00036 #include <dns/zt.h>
00037 
00038 #include "dnstest.h"
00039 
00040 struct args {
00041         void *arg1;
00042         void *arg2;
00043 };
00044 
00045 /*
00046  * Helper functions
00047  */
00048 static isc_result_t
00049 count_zone(dns_zone_t *zone, void *uap) {
00050         int *nzones = (int *)uap;
00051 
00052         UNUSED(zone);
00053 
00054         *nzones += 1;
00055         return (ISC_R_SUCCESS);
00056 }
00057 
00058 static isc_result_t
00059 load_done(dns_zt_t *zt, dns_zone_t *zone, isc_task_t *task) {
00060         /* We treat zt as a pointer to a boolean for testing purposes */
00061         isc_boolean_t *done = (isc_boolean_t *) zt;
00062 
00063         UNUSED(zone);
00064         UNUSED(task);
00065 
00066         *done = ISC_TRUE;
00067         isc_app_shutdown();
00068         return (ISC_R_SUCCESS);
00069 }
00070 
00071 static isc_result_t
00072 all_done(void *arg) {
00073         isc_boolean_t *done = (isc_boolean_t *) arg;
00074 
00075         *done = ISC_TRUE;
00076         isc_app_shutdown();
00077         return (ISC_R_SUCCESS);
00078 }
00079 
00080 static void
00081 start_zt_asyncload(isc_task_t *task, isc_event_t *event) {
00082         struct args *args = (struct args *)(event->ev_arg);
00083 
00084         UNUSED(task);
00085 
00086         dns_zt_asyncload(args->arg1, all_done, args->arg2);
00087 
00088         isc_event_free(&event);
00089 }
00090 
00091 static void
00092 start_zone_asyncload(isc_task_t *task, isc_event_t *event) {
00093         struct args *args = (struct args *)(event->ev_arg);
00094 
00095         UNUSED(task);
00096 
00097         dns_zone_asyncload(args->arg1, load_done, args->arg2);
00098         isc_event_free(&event);
00099 }
00100 
00101 /*
00102  * Individual unit tests
00103  */
00104 ATF_TC(apply);
00105 ATF_TC_HEAD(apply, tc) {
00106         atf_tc_set_md_var(tc, "descr", "apply a function to a zone table");
00107 }
00108 ATF_TC_BODY(apply, tc) {
00109         isc_result_t result;
00110         dns_zone_t *zone = NULL;
00111         dns_view_t *view = NULL;
00112         int nzones = 0;
00113 
00114         UNUSED(tc);
00115 
00116         result = dns_test_begin(NULL, ISC_TRUE);
00117         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00118 
00119         result = dns_test_makezone("foo", &zone, NULL, ISC_TRUE);
00120         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00121 
00122         view = dns_zone_getview(zone);
00123         ATF_REQUIRE(view->zonetable != NULL);
00124 
00125         ATF_CHECK_EQ(0, nzones);
00126         result = dns_zt_apply(view->zonetable, ISC_FALSE, count_zone, &nzones);
00127         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00128         ATF_CHECK_EQ(1, nzones);
00129 
00130         /* These steps are necessary so the zone can be detached properly */
00131         result = dns_test_setupzonemgr();
00132         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00133         result = dns_test_managezone(zone);
00134         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00135         dns_test_releasezone(zone);
00136         dns_test_closezonemgr();
00137 
00138         /* The view was left attached in dns_test_makezone() */
00139         dns_view_detach(&view);
00140         dns_zone_detach(&zone);
00141 
00142         dns_test_end();
00143 }
00144 
00145 ATF_TC(asyncload_zone);
00146 ATF_TC_HEAD(asyncload_zone, tc) {
00147         atf_tc_set_md_var(tc, "descr", "asynchronous zone load");
00148 }
00149 ATF_TC_BODY(asyncload_zone, tc) {
00150         isc_result_t result;
00151         dns_zone_t *zone = NULL;
00152         dns_view_t *view = NULL;
00153         dns_db_t *db = NULL;
00154         isc_boolean_t done = ISC_FALSE;
00155         int i = 0;
00156         struct args args;
00157 
00158         UNUSED(tc);
00159 
00160         result = dns_test_begin(NULL, ISC_TRUE);
00161         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00162 
00163         result = dns_test_makezone("foo", &zone, NULL, ISC_TRUE);
00164         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00165 
00166         result = dns_test_setupzonemgr();
00167         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00168         result = dns_test_managezone(zone);
00169         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00170 
00171         view = dns_zone_getview(zone);
00172         ATF_REQUIRE(view->zonetable != NULL);
00173 
00174         ATF_CHECK(!dns__zone_loadpending(zone));
00175         ATF_CHECK(!done);
00176         dns_zone_setfile(zone, "testdata/zt/zone1.db");
00177 
00178         args.arg1 = zone;
00179         args.arg2 = &done;
00180         isc_app_onrun(mctx, maintask, start_zone_asyncload, &args);
00181 
00182         isc_app_run();
00183         while (dns__zone_loadpending(zone) && i++ < 5000)
00184                 dns_test_nap(1000);
00185         ATF_CHECK(done);
00186 
00187         /* The zone should now be loaded; test it */
00188         result = dns_zone_getdb(zone, &db);
00189         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00190         ATF_CHECK(db != NULL);
00191         if (db != NULL)
00192                 dns_db_detach(&db);
00193 
00194         dns_test_releasezone(zone);
00195         dns_test_closezonemgr();
00196 
00197         dns_zone_detach(&zone);
00198         dns_view_detach(&view);
00199 
00200         dns_test_end();
00201 }
00202 
00203 ATF_TC(asyncload_zt);
00204 ATF_TC_HEAD(asyncload_zt, tc) {
00205         atf_tc_set_md_var(tc, "descr", "asynchronous zone table load");
00206 }
00207 ATF_TC_BODY(asyncload_zt, tc) {
00208         isc_result_t result;
00209         dns_zone_t *zone1 = NULL, *zone2 = NULL, *zone3 = NULL;
00210         dns_view_t *view;
00211         dns_zt_t *zt;
00212         dns_db_t *db = NULL;
00213         isc_boolean_t done = ISC_FALSE;
00214         int i = 0;
00215         struct args args;
00216 
00217         UNUSED(tc);
00218 
00219         result = dns_test_begin(NULL, ISC_TRUE);
00220         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00221 
00222         result = dns_test_makezone("foo", &zone1, NULL, ISC_TRUE);
00223         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00224         dns_zone_setfile(zone1, "testdata/zt/zone1.db");
00225         view = dns_zone_getview(zone1);
00226 
00227         result = dns_test_makezone("bar", &zone2, view, ISC_TRUE);
00228         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00229         dns_zone_setfile(zone2, "testdata/zt/zone1.db");
00230 
00231         /* This one will fail to load */
00232         result = dns_test_makezone("fake", &zone3, view, ISC_TRUE);
00233         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00234         dns_zone_setfile(zone3, "testdata/zt/nonexistent.db");
00235 
00236         zt = view->zonetable;
00237         ATF_REQUIRE(zt != NULL);
00238 
00239         result = dns_test_setupzonemgr();
00240         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00241         result = dns_test_managezone(zone1);
00242         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00243         result = dns_test_managezone(zone2);
00244         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00245         result = dns_test_managezone(zone3);
00246         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00247 
00248         ATF_CHECK(!dns__zone_loadpending(zone1));
00249         ATF_CHECK(!dns__zone_loadpending(zone2));
00250         ATF_CHECK(!done);
00251 
00252         args.arg1 = zt;
00253         args.arg2 = &done;
00254         isc_app_onrun(mctx, maintask, start_zt_asyncload, &args);
00255 
00256         isc_app_run();
00257         while (!done && i++ < 5000)
00258                 dns_test_nap(1000);
00259         ATF_CHECK(done);
00260 
00261         /* Both zones should now be loaded; test them */
00262         result = dns_zone_getdb(zone1, &db);
00263         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00264         ATF_CHECK(db != NULL);
00265         if (db != NULL)
00266                 dns_db_detach(&db);
00267 
00268         result = dns_zone_getdb(zone2, &db);
00269         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00270         ATF_CHECK(db != NULL);
00271         if (db != NULL)
00272                 dns_db_detach(&db);
00273 
00274         dns_test_releasezone(zone3);
00275         dns_test_releasezone(zone2);
00276         dns_test_releasezone(zone1);
00277         dns_test_closezonemgr();
00278 
00279         dns_zone_detach(&zone1);
00280         dns_zone_detach(&zone2);
00281         dns_zone_detach(&zone3);
00282         dns_view_detach(&view);
00283 
00284         dns_test_end();
00285 }
00286 
00287 /*
00288  * Main
00289  */
00290 ATF_TP_ADD_TCS(tp) {
00291         ATF_TP_ADD_TC(tp, apply);
00292         ATF_TP_ADD_TC(tp, asyncload_zone);
00293         ATF_TP_ADD_TC(tp, asyncload_zt);
00294         return (atf_no_error());
00295 }

Generated on Tue Apr 28 17:41:02 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha