"Fossies" - the Fresh Open Source Software Archive

Member "knot-2.9.2/tests/knot/test_changeset.c" (12 Dec 2019, 5633 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_changeset.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 <config.h>
   18 #include <assert.h>
   19 #include <tap/basic.h>
   20 
   21 #include "libknot/errcode.h"
   22 #include "libknot/error.h"
   23 #include "knot/updates/changesets.h"
   24 
   25 int main(int argc, char *argv[])
   26 {
   27     plan_lazy();
   28 
   29     // Test with NULL changeset
   30     ok(changeset_size(NULL) == 0, "changeset: NULL size");
   31     ok(changeset_empty(NULL), "changeset: NULL empty");
   32 
   33     // Test creation.
   34     knot_dname_t *d = knot_dname_from_str_alloc("test.");
   35     assert(d);
   36     changeset_t *ch = changeset_new(d);
   37     knot_dname_free(d, NULL);
   38     ok(ch != NULL, "changeset: new");
   39     if (!ch) {
   40         return 1;
   41     }
   42     ok(changeset_empty(ch), "changeset: empty");
   43     ch->soa_to = (knot_rrset_t *)0xdeadbeef;
   44     ok(!changeset_empty(ch), "changseset: empty SOA");
   45     ch->soa_to = NULL;
   46     ok(changeset_size(ch) == 0, "changeset: empty size");
   47 
   48     // Test additions.
   49     d = knot_dname_from_str_alloc("non.terminals.test.");
   50     assert(d);
   51     knot_rrset_t *apex_txt_rr = knot_rrset_new(d, KNOT_RRTYPE_TXT, KNOT_CLASS_IN, 3600, NULL);
   52     assert(apex_txt_rr);
   53     uint8_t data[8] = "\7teststr";
   54     knot_rrset_add_rdata(apex_txt_rr, data, sizeof(data), NULL);
   55 
   56     int ret = changeset_add_addition(ch, apex_txt_rr, CHANGESET_CHECK);
   57     is_int(KNOT_EOK, ret, "changeset: add RRSet");
   58     ok(changeset_size(ch) == 1, "changeset: size add");
   59     ret = changeset_add_removal(ch, apex_txt_rr, CHANGESET_CHECK);
   60     is_int(KNOT_EOK, ret, "changeset: rem RRSet");
   61     ok(changeset_size(ch) == 0, "changeset: size remove");
   62     ok(changeset_empty(ch), "changeset: empty");
   63     changeset_add_addition(ch, apex_txt_rr, CHANGESET_CHECK);
   64 
   65     // Add another RR to node.
   66     knot_rrset_t *apex_spf_rr = knot_rrset_new(d, KNOT_RRTYPE_SPF, KNOT_CLASS_IN, 3600, NULL);
   67     assert(apex_spf_rr);
   68     knot_rrset_add_rdata(apex_spf_rr, data, sizeof(data), NULL);
   69     ret = changeset_add_addition(ch, apex_spf_rr, CHANGESET_CHECK);
   70     is_int(KNOT_EOK, ret, "changeset: add multiple");
   71 
   72     // Add another node.
   73     knot_dname_free(d, NULL);
   74     d = knot_dname_from_str_alloc("here.come.more.non.terminals.test");
   75     assert(d);
   76     knot_rrset_t *other_rr = knot_rrset_new(d, KNOT_RRTYPE_TXT, KNOT_CLASS_IN, 3600, NULL);
   77     assert(other_rr);
   78     knot_rrset_add_rdata(other_rr, data, sizeof(data), NULL);
   79     ret = changeset_add_addition(ch, other_rr, CHANGESET_CHECK);
   80     is_int(KNOT_EOK, ret, "changeset: remove multiple");
   81 
   82     // Test add traversal.
   83     changeset_iter_t it;
   84     ret = changeset_iter_add(&it, ch);
   85     is_int(KNOT_EOK, ret, "changeset: create iter add");
   86     // Order: non.terminals.test. TXT, SPF, here.come.more.non.terminals.test. TXT.
   87     knot_rrset_t iter = changeset_iter_next(&it);
   88     bool trav_ok = knot_rrset_equal(&iter, apex_txt_rr, true);
   89     iter = changeset_iter_next(&it);
   90     trav_ok = trav_ok && knot_rrset_equal(&iter, apex_spf_rr, true);
   91     iter = changeset_iter_next(&it);
   92     trav_ok = trav_ok && knot_rrset_equal(&iter, other_rr, true);
   93 
   94     ok(trav_ok, "changeset: add traversal");
   95 
   96     iter = changeset_iter_next(&it);
   97     changeset_iter_clear(&it);
   98     ok(knot_rrset_empty(&iter), "changeset: traversal: skip non-terminals");
   99 
  100     changeset_add_removal(ch, apex_txt_rr, CHANGESET_CHECK);
  101     changeset_add_removal(ch, apex_txt_rr, CHANGESET_CHECK);
  102 
  103     // Test remove traversal.
  104     ret = changeset_iter_rem(&it, ch);
  105     is_int(KNOT_EOK, ret, "changeset: create iter rem");
  106     iter = changeset_iter_next(&it);
  107     ok(knot_rrset_equal(&iter, apex_txt_rr, true),
  108        "changeset: rem traversal");
  109     changeset_iter_clear(&it);
  110 
  111     // Test all traversal - just count.
  112     ret = changeset_iter_all(&it, ch);
  113     is_int(KNOT_EOK, ret, "changest: create iter all");
  114     size_t size = 0;
  115     iter = changeset_iter_next(&it);
  116     while (!knot_rrset_empty(&iter)) {
  117         ++size;
  118         iter = changeset_iter_next(&it);
  119     }
  120     changeset_iter_clear(&it);
  121     ok(size == 3, "changeset: iter all");
  122 
  123     // Create new changeset.
  124     knot_dname_free(d, NULL);
  125     d = knot_dname_from_str_alloc("test.");
  126     assert(d);
  127     changeset_t *ch2 = changeset_new(d);
  128     knot_dname_free(d, NULL);
  129     assert(ch2);
  130     // Add something to add section.
  131     knot_dname_free(apex_txt_rr->owner, NULL);
  132     apex_txt_rr->owner = knot_dname_from_str_alloc("something.test.");
  133     assert(apex_txt_rr->owner);
  134     ret = changeset_add_addition(ch2, apex_txt_rr, CHANGESET_CHECK);
  135     assert(ret == KNOT_EOK);
  136 
  137     // Add something to remove section.
  138     knot_dname_free(apex_txt_rr->owner, NULL);
  139     apex_txt_rr->owner =
  140         knot_dname_from_str_alloc("and.now.for.something.completely.different.test.");
  141     assert(apex_txt_rr->owner);
  142     ret = changeset_add_removal(ch2, apex_txt_rr, CHANGESET_CHECK);
  143     assert(ret == KNOT_EOK);
  144 
  145     // Test merge.
  146     ret = changeset_merge(ch, ch2, 0);
  147     ok(ret == KNOT_EOK && changeset_size(ch) == 5, "changeset: merge");
  148 
  149     // Test cleanup.
  150     changeset_clear(ch);
  151     ok(changeset_empty(ch), "changeset: clear");
  152     free(ch);
  153 
  154     list_t chgs;
  155     init_list(&chgs);
  156     add_head(&chgs, &ch2->n);
  157     changesets_clear(&chgs);
  158     ok(changeset_empty(ch2), "changeset: clear list");
  159     free(ch2);
  160 
  161     knot_rrset_free(apex_txt_rr, NULL);
  162     knot_rrset_free(apex_spf_rr, NULL);
  163     knot_rrset_free(other_rr, NULL);
  164 
  165     return 0;
  166 }