"Fossies" - the Fresh Open Source Software Archive

Member "knot-2.9.2/tests/knot/test_zonedb.c" (12 Dec 2019, 3186 Bytes) of package /linux/misc/dns/knot-2.9.2.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_zonedb.c": 2.9.1_vs_2.9.2.

    1 /*  Copyright (C) 2019 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
    2 
    3     This program is free software: you can redistribute it and/or modify
    4     it under the terms of the GNU General Public License as published by
    5     the Free Software Foundation, either version 3 of the License, or
    6     (at your option) any later version.
    7 
    8     This program is distributed in the hope that it will be useful,
    9     but WITHOUT ANY WARRANTY; without even the implied warranty of
   10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   11     GNU General Public License for more details.
   12 
   13     You should have received a copy of the GNU General Public License
   14     along with this program.  If not, see <https://www.gnu.org/licenses/>.
   15  */
   16 
   17 #include <tap/basic.h>
   18 
   19 #include "knot/zone/zone.h"
   20 #include "knot/zone/zonedb.h"
   21 #include "contrib/openbsd/strlcat.h"
   22 #include "contrib/openbsd/strlcpy.h"
   23 
   24 #define ZONE_COUNT 10
   25 static const char *zone_list[ZONE_COUNT] = {
   26         ".",
   27         "com",
   28         "net",
   29         "c.com",
   30         "a.com",
   31         "a.net",
   32         "b.net",
   33         "c.a.com",
   34         "b.b.b.com",
   35         "b.b.b.b.net",
   36 };
   37 
   38 int main(int argc, char *argv[])
   39 {
   40     plan_lazy();
   41 
   42     /* Create database. */
   43     knot_dname_txt_storage_t buf;
   44     const char *prefix = "zzz.";
   45     size_t nr_passed = 0;
   46     knot_dname_t *dname = NULL;
   47     zone_t *zones[ZONE_COUNT] = {0};
   48     knot_zonedb_t *db = knot_zonedb_new();
   49     ok(db != NULL, "zonedb: new");
   50 
   51     /* Populate. */
   52     for (unsigned i = 0; i < ZONE_COUNT; ++i) {
   53         knot_dname_t *zone_name = knot_dname_from_str_alloc(zone_list[i]);
   54         zones[i] = zone_new(zone_name);
   55         knot_dname_free(zone_name, NULL);
   56 
   57         if (zones[i] == NULL) {
   58             goto cleanup;
   59         }
   60         if (knot_zonedb_insert(db, zones[i]) == KNOT_EOK) {
   61             ++nr_passed;
   62         } else {
   63             diag("knot_zonedb_add_zone(%s) failed", zone_list[i]);
   64         }
   65     }
   66     ok(nr_passed == ZONE_COUNT, "zonedb: add zones");
   67 
   68     /* Lookup of exact names. */
   69     nr_passed = 0;
   70     for (unsigned i = 0; i < ZONE_COUNT; ++i) {
   71         dname = knot_dname_from_str_alloc(zone_list[i]);
   72         if (knot_zonedb_find(db, dname) == zones[i]) {
   73             ++nr_passed;
   74         } else {
   75             diag("knot_zonedb_find(%s) failed", zone_list[i]);
   76         }
   77         knot_dname_free(dname, NULL);
   78     }
   79     ok(nr_passed == ZONE_COUNT, "zonedb: find exact zones");
   80 
   81     /* Lookup of sub-names. */
   82     nr_passed = 0;
   83     for (unsigned i = 0; i < ZONE_COUNT; ++i) {
   84         strlcpy(buf, prefix, sizeof(buf));
   85         if (strcmp(zone_list[i], ".") != 0) {
   86             strlcat(buf, zone_list[i], sizeof(buf));
   87         }
   88         dname = knot_dname_from_str_alloc(buf);
   89         if (knot_zonedb_find_suffix(db, dname) == zones[i]) {
   90             ++nr_passed;
   91         } else {
   92             diag("knot_zonedb_find_suffix(%s) failed", buf);
   93         }
   94         knot_dname_free(dname, NULL);
   95     }
   96     ok(nr_passed == ZONE_COUNT, "zonedb: find zones for subnames");
   97 
   98     /* Remove all zones. */
   99     nr_passed = 0;
  100     for (unsigned i = 0; i < ZONE_COUNT; ++i) {
  101         dname = knot_dname_from_str_alloc(zone_list[i]);
  102         if (knot_zonedb_del(db, dname) == KNOT_EOK) {
  103             zone_free(&zones[i]);
  104             ++nr_passed;
  105         } else {
  106             diag("knot_zonedb_remove_zone(%s) failed", zone_list[i]);
  107         }
  108         knot_dname_free(dname, NULL);
  109     }
  110     ok(nr_passed == ZONE_COUNT, "zonedb: removed all zones");
  111 
  112 cleanup:
  113     knot_zonedb_deep_free(&db, false);
  114     return 0;
  115 }