dbiterator_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 #include <stdlib.h>
00027 
00028 #include <dns/db.h>
00029 #include <dns/dbiterator.h>
00030 #include <dns/name.h>
00031 
00032 #include "dnstest.h"
00033 
00034 /*
00035  * Helper functions
00036  */
00037 
00038 #define BUFLEN          255
00039 #define BIGBUFLEN       (64 * 1024)
00040 #define TEST_ORIGIN     "test"
00041 
00042 static isc_result_t
00043 make_name(const char *src, dns_name_t *name) {
00044         isc_buffer_t b;
00045         isc_buffer_constinit(&b, src, strlen(src));
00046         isc_buffer_add(&b, strlen(src));
00047         return (dns_name_fromtext(name, &b, dns_rootname, 0, NULL));
00048 }
00049 
00050 /*
00051  * Individual unit tests
00052  */
00053 
00054 /* create: make sure we can create a dbiterator */
00055 static void
00056 test_create(const atf_tc_t *tc) {
00057         isc_result_t result;
00058         dns_db_t *db = NULL;
00059         dns_dbiterator_t *iter = NULL;
00060 
00061         result = dns_test_begin(NULL, ISC_FALSE);
00062         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00063 
00064         result = dns_test_loaddb(&db, dns_dbtype_cache, TEST_ORIGIN,
00065                                  atf_tc_get_md_var(tc, "X-filename"));
00066         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00067 
00068         result = dns_db_createiterator(db, 0, &iter);
00069         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00070 
00071         dns_dbiterator_destroy(&iter);
00072         dns_db_detach(&db);
00073         dns_test_end();
00074 }
00075 
00076 ATF_TC(create);
00077 ATF_TC_HEAD(create, tc) {
00078         atf_tc_set_md_var(tc, "descr", "create a database iterator");
00079         atf_tc_set_md_var(tc, "X-filename", "testdata/dbiterator/zone1.data");
00080 }
00081 ATF_TC_BODY(create, tc) {
00082         test_create(tc);
00083 }
00084 
00085 ATF_TC(create_nsec3);
00086 ATF_TC_HEAD(create_nsec3, tc) {
00087         atf_tc_set_md_var(tc, "descr", "create a database iterator (NSEC3)");
00088         atf_tc_set_md_var(tc, "X-filename", "testdata/dbiterator/zone2.data");
00089 }
00090 ATF_TC_BODY(create_nsec3, tc) {
00091         test_create(tc);
00092 }
00093 
00094 /* walk: walk a database */
00095 static void
00096 test_walk(const atf_tc_t *tc) {
00097         isc_result_t result;
00098         dns_db_t *db = NULL;
00099         dns_dbiterator_t *iter = NULL;
00100         dns_dbnode_t *node = NULL;
00101         dns_name_t *name;
00102         dns_fixedname_t f;
00103         int i = 0;
00104 
00105         UNUSED(tc);
00106 
00107         dns_fixedname_init(&f);
00108         name = dns_fixedname_name(&f);
00109 
00110         result = dns_test_begin(NULL, ISC_FALSE);
00111         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00112 
00113         result = dns_test_loaddb(&db, dns_dbtype_cache, TEST_ORIGIN,
00114                                  atf_tc_get_md_var(tc, "X-filename"));
00115         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00116 
00117         result = dns_db_createiterator(db, 0, &iter);
00118         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00119 
00120         for (result = dns_dbiterator_first(iter);
00121              result == ISC_R_SUCCESS;
00122              result = dns_dbiterator_next(iter)) {
00123                 result = dns_dbiterator_current(iter, &node, name);
00124                 if (result == DNS_R_NEWORIGIN)
00125                         result = ISC_R_SUCCESS;
00126                 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00127                 dns_db_detachnode(db, &node);
00128                 i++;
00129         }
00130 
00131         ATF_CHECK_EQ(i, atoi(atf_tc_get_md_var(tc, "X-nodes")));
00132 
00133         dns_dbiterator_destroy(&iter);
00134         dns_db_detach(&db);
00135         dns_test_end();
00136 }
00137 
00138 ATF_TC(walk);
00139 ATF_TC_HEAD(walk, tc) {
00140         atf_tc_set_md_var(tc, "descr", "walk database");
00141         atf_tc_set_md_var(tc, "X-filename", "testdata/dbiterator/zone1.data");
00142         atf_tc_set_md_var(tc, "X-nodes", "12");
00143 }
00144 ATF_TC_BODY(walk, tc) {
00145         test_walk(tc);
00146 }
00147 
00148 ATF_TC(walk_nsec3);
00149 ATF_TC_HEAD(walk_nsec3, tc) {
00150         atf_tc_set_md_var(tc, "descr", "walk database");
00151         atf_tc_set_md_var(tc, "X-filename", "testdata/dbiterator/zone2.data");
00152         atf_tc_set_md_var(tc, "X-nodes", "33");
00153 }
00154 ATF_TC_BODY(walk_nsec3, tc) {
00155         test_walk(tc);
00156 }
00157 
00158 /* reverse: walk database backwards */
00159 static void test_reverse(const atf_tc_t *tc) {
00160         isc_result_t result;
00161         dns_db_t *db = NULL;
00162         dns_dbiterator_t *iter = NULL;
00163         dns_dbnode_t *node = NULL;
00164         dns_name_t *name;
00165         dns_fixedname_t f;
00166         int i = 0;
00167 
00168         UNUSED(tc);
00169 
00170         dns_fixedname_init(&f);
00171         name = dns_fixedname_name(&f);
00172 
00173         result = dns_test_begin(NULL, ISC_FALSE);
00174         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00175 
00176         result = dns_test_loaddb(&db, dns_dbtype_cache, TEST_ORIGIN,
00177                                  atf_tc_get_md_var(tc, "X-filename"));
00178         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00179 
00180         result = dns_db_createiterator(db, 0, &iter);
00181         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00182 
00183         for (result = dns_dbiterator_last(iter);
00184              result == ISC_R_SUCCESS;
00185              result = dns_dbiterator_prev(iter)) {
00186                 result = dns_dbiterator_current(iter, &node, name);
00187                 if (result == DNS_R_NEWORIGIN)
00188                         result = ISC_R_SUCCESS;
00189                 ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00190                 dns_db_detachnode(db, &node);
00191                 i++;
00192         }
00193 
00194         ATF_CHECK_EQ(i, 12);
00195 
00196         dns_dbiterator_destroy(&iter);
00197         dns_db_detach(&db);
00198         dns_test_end();
00199 }
00200 
00201 ATF_TC(reverse);
00202 ATF_TC_HEAD(reverse, tc) {
00203         atf_tc_set_md_var(tc, "descr", "walk database backwards");
00204         atf_tc_set_md_var(tc, "X-filename", "testdata/dbiterator/zone1.data");
00205 }
00206 ATF_TC_BODY(reverse, tc) {
00207         test_reverse(tc);
00208 }
00209 
00210 ATF_TC(reverse_nsec3);
00211 ATF_TC_HEAD(reverse_nsec3, tc) {
00212         atf_tc_set_md_var(tc, "descr", "walk database backwards");
00213         atf_tc_set_md_var(tc, "X-filename", "testdata/dbiterator/zone2.data");
00214 }
00215 ATF_TC_BODY(reverse_nsec3, tc) {
00216         test_reverse(tc);
00217 }
00218 
00219 /* seek: walk database starting at a particular node */
00220 static void test_seek(const atf_tc_t *tc) {
00221         isc_result_t result;
00222         dns_db_t *db = NULL;
00223         dns_dbiterator_t *iter = NULL;
00224         dns_dbnode_t *node = NULL;
00225         dns_name_t *name, *seekname;
00226         dns_fixedname_t f1, f2;
00227         int i = 0;
00228 
00229         UNUSED(tc);
00230 
00231         dns_fixedname_init(&f1);
00232         name = dns_fixedname_name(&f1);
00233         dns_fixedname_init(&f2);
00234         seekname = dns_fixedname_name(&f2);
00235 
00236         result = dns_test_begin(NULL, ISC_FALSE);
00237         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00238 
00239         result = dns_test_loaddb(&db, dns_dbtype_cache, TEST_ORIGIN,
00240                                  atf_tc_get_md_var(tc, "X-filename"));
00241         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00242 
00243         result = dns_db_createiterator(db, 0, &iter);
00244         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00245 
00246         result = make_name("c." TEST_ORIGIN, seekname);
00247         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00248 
00249         result = dns_dbiterator_seek(iter, seekname);
00250         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00251 
00252         while (result == ISC_R_SUCCESS) {
00253                 result = dns_dbiterator_current(iter, &node, name);
00254                 if (result == DNS_R_NEWORIGIN)
00255                         result = ISC_R_SUCCESS;
00256                 ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00257                 dns_db_detachnode(db, &node);
00258                 result = dns_dbiterator_next(iter);
00259                 i++;
00260         }
00261 
00262         ATF_CHECK_EQ(i, atoi(atf_tc_get_md_var(tc, "X-nodes")));
00263 
00264         dns_dbiterator_destroy(&iter);
00265         dns_db_detach(&db);
00266         dns_test_end();
00267 }
00268 
00269 ATF_TC(seek);
00270 ATF_TC_HEAD(seek, tc) {
00271         atf_tc_set_md_var(tc, "descr", "walk database starting at "
00272                                        "a particular node");
00273         atf_tc_set_md_var(tc, "X-filename", "testdata/dbiterator/zone1.data");
00274         atf_tc_set_md_var(tc, "X-nodes", "9");
00275 }
00276 ATF_TC_BODY(seek, tc) {
00277         test_seek(tc);
00278 }
00279 
00280 ATF_TC(seek_nsec3);
00281 ATF_TC_HEAD(seek_nsec3, tc) {
00282         atf_tc_set_md_var(tc, "descr", "walk database starting at "
00283                                        "a particular node");
00284         atf_tc_set_md_var(tc, "X-filename", "testdata/dbiterator/zone2.data");
00285         atf_tc_set_md_var(tc, "X-nodes", "30");
00286 }
00287 ATF_TC_BODY(seek_nsec3, tc) {
00288         test_seek(tc);
00289 }
00290 
00291 /*
00292  * seek_emty: walk database starting at an empty nonterminal node
00293  * (should fail)
00294  */
00295 static void test_seek_empty(const atf_tc_t *tc) {
00296         isc_result_t result;
00297         dns_db_t *db = NULL;
00298         dns_dbiterator_t *iter = NULL;
00299         dns_name_t *seekname;
00300         dns_fixedname_t f1;
00301 
00302         UNUSED(tc);
00303 
00304         dns_fixedname_init(&f1);
00305         seekname = dns_fixedname_name(&f1);
00306 
00307         result = dns_test_begin(NULL, ISC_FALSE);
00308         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00309 
00310         result = dns_test_loaddb(&db, dns_dbtype_cache, TEST_ORIGIN,
00311                                  atf_tc_get_md_var(tc, "X-filename"));
00312         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00313 
00314         result = dns_db_createiterator(db, 0, &iter);
00315         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00316 
00317         result = make_name("d." TEST_ORIGIN, seekname);
00318         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00319 
00320         result = dns_dbiterator_seek(iter, seekname);
00321         ATF_CHECK_EQ(result, ISC_R_NOTFOUND);
00322 
00323         dns_dbiterator_destroy(&iter);
00324         dns_db_detach(&db);
00325         dns_test_end();
00326 }
00327 
00328 ATF_TC(seek_empty);
00329 ATF_TC_HEAD(seek_empty, tc) {
00330         atf_tc_set_md_var(tc, "descr", "walk database starting at an "
00331                                        "empty nonterminal node");
00332         atf_tc_set_md_var(tc, "X-filename", "testdata/dbiterator/zone1.data");
00333 }
00334 ATF_TC_BODY(seek_empty, tc) {
00335         test_seek_empty(tc);
00336 }
00337 
00338 ATF_TC(seek_empty_nsec3);
00339 ATF_TC_HEAD(seek_empty_nsec3, tc) {
00340         atf_tc_set_md_var(tc, "descr", "walk database starting at an "
00341                                        "empty nonterminal node");
00342         atf_tc_set_md_var(tc, "X-filename", "testdata/dbiterator/zone2.data");
00343 }
00344 ATF_TC_BODY(seek_empty_nsec3, tc) {
00345         test_seek_empty(tc);
00346 }
00347 
00348 /*
00349  * seek_emty: walk database starting at an empty nonterminal node
00350  * (should fail)
00351  */
00352 static void test_seek_nx(const atf_tc_t *tc) {
00353         isc_result_t result;
00354         dns_db_t *db = NULL;
00355         dns_dbiterator_t *iter = NULL;
00356         dns_name_t *seekname;
00357         dns_fixedname_t f1;
00358 
00359         UNUSED(tc);
00360 
00361         dns_fixedname_init(&f1);
00362         seekname = dns_fixedname_name(&f1);
00363 
00364         result = dns_test_begin(NULL, ISC_FALSE);
00365         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00366 
00367         result = dns_test_loaddb(&db, dns_dbtype_cache, TEST_ORIGIN,
00368                                  atf_tc_get_md_var(tc, "X-filename"));
00369         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00370 
00371         result = dns_db_createiterator(db, 0, &iter);
00372         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00373 
00374         result = make_name("nonexistent." TEST_ORIGIN, seekname);
00375         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00376 
00377         result = dns_dbiterator_seek(iter, seekname);
00378         ATF_CHECK_EQ(result, ISC_R_NOTFOUND);
00379 
00380         dns_dbiterator_destroy(&iter);
00381         dns_db_detach(&db);
00382         dns_test_end();
00383 }
00384 
00385 ATF_TC(seek_nx);
00386 ATF_TC_HEAD(seek_nx, tc) {
00387         atf_tc_set_md_var(tc, "descr", "attempt to walk database starting "
00388                                        "at a nonexistent node");
00389         atf_tc_set_md_var(tc, "X-filename", "testdata/dbiterator/zone1.data");
00390 }
00391 ATF_TC_BODY(seek_nx, tc) {
00392         test_seek_nx(tc);
00393 }
00394 
00395 ATF_TC(seek_nx_nsec3);
00396 ATF_TC_HEAD(seek_nx_nsec3, tc) {
00397         atf_tc_set_md_var(tc, "descr", "attempt to walk database starting "
00398                                        "at a nonexistent node");
00399         atf_tc_set_md_var(tc, "X-filename", "testdata/dbiterator/zone2.data");
00400 }
00401 ATF_TC_BODY(seek_nx_nsec3, tc) {
00402         test_seek_nx(tc);
00403 }
00404 
00405 /*
00406  * Main
00407  */
00408 ATF_TP_ADD_TCS(tp) {
00409         ATF_TP_ADD_TC(tp, create);
00410         ATF_TP_ADD_TC(tp, create_nsec3);
00411         ATF_TP_ADD_TC(tp, walk);
00412         ATF_TP_ADD_TC(tp, walk_nsec3);
00413         ATF_TP_ADD_TC(tp, reverse);
00414         ATF_TP_ADD_TC(tp, reverse_nsec3);
00415         ATF_TP_ADD_TC(tp, seek);
00416         ATF_TP_ADD_TC(tp, seek_nsec3);
00417         ATF_TP_ADD_TC(tp, seek_empty);
00418         ATF_TP_ADD_TC(tp, seek_empty_nsec3);
00419         ATF_TP_ADD_TC(tp, seek_nx);
00420         ATF_TP_ADD_TC(tp, seek_nx_nsec3);
00421         return (atf_no_error());
00422 }
00423 
00424 /*
00425  * XXX:
00426  * dns_dbiterator API calls that are not yet part of this unit test:
00427  *
00428  * dns_dbiterator_pause
00429  * dns_dbiterator_origin
00430  * dns_dbiterator_setcleanmode
00431  */

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