"Fossies" - the Fresh Open Source Software Archive

Member "knot-2.8.3/tests/libknot/test_rrset.c" (16 Jul 2019, 3921 Bytes) of package /linux/misc/dns/knot-2.8.3.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 last Fossies "Diffs" side-by-side code changes report for "test_rrset.c": 2.7.6_vs_2.8.0.

    1 /*  Copyright (C) 2018 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 <assert.h>
   18 #include <inttypes.h>
   19 #include <tap/basic.h>
   20 
   21 #include "libknot/rrset.h"
   22 #include "libknot/descriptor.h"
   23 
   24 static bool check_rrset(const knot_rrset_t *rrset, const knot_dname_t *owner,
   25                         uint16_t type, uint16_t rclass, uint32_t ttl)
   26 {
   27     if (!rrset) {
   28         return false;
   29     }
   30 
   31     const bool dname_cmp = owner == NULL ? rrset->owner == NULL :
   32                                            knot_dname_is_equal(rrset->owner, owner);
   33     return rrset->type == type && rrset->rclass == rclass && dname_cmp &&
   34            rrset->ttl == ttl && rrset->rrs.count == 0; // We do not test rdataset here
   35 }
   36 
   37 int main(int argc, char *argv[])
   38 {
   39     plan_lazy();
   40 
   41     // Test new
   42     knot_dname_t *dummy_owner = knot_dname_from_str_alloc("test.");
   43     assert(dummy_owner);
   44 
   45     knot_rrset_t *rrset = knot_rrset_new(dummy_owner, KNOT_RRTYPE_TXT,
   46                                          KNOT_CLASS_IN, 3600, NULL);
   47     ok(rrset != NULL, "rrset: create.");
   48     assert(rrset);
   49 
   50     ok(check_rrset(rrset, dummy_owner, KNOT_RRTYPE_TXT, KNOT_CLASS_IN, 3600),
   51        "rrset: set fields during create.");
   52 
   53     // Test init
   54     knot_dname_free(dummy_owner, NULL);
   55     dummy_owner = knot_dname_from_str_alloc("test2.");
   56     assert(dummy_owner);
   57 
   58     knot_dname_free(rrset->owner, NULL);
   59     knot_rrset_init(rrset, dummy_owner, KNOT_RRTYPE_A, KNOT_CLASS_CH, 7200);
   60     ok(check_rrset(rrset, dummy_owner, KNOT_RRTYPE_A, KNOT_CLASS_CH, 7200),
   61        "rrset: init.");
   62 
   63     // Test copy
   64     knot_rrset_t *copy = knot_rrset_copy(rrset, NULL);
   65     ok(copy != NULL, "rrset: copy.");
   66     ok(check_rrset(copy, rrset->owner, rrset->type, rrset->rclass, 7200),
   67        "rrset: set fields during copy.");
   68     ok(knot_rrset_copy(NULL, NULL) == NULL, "rrset: copy NULL.");
   69     assert(copy);
   70 
   71     // Test equal - pointers
   72     ok(knot_rrset_equal((knot_rrset_t *)0xdeadbeef, (knot_rrset_t *)0xdeadbeef,
   73                         KNOT_RRSET_COMPARE_PTR), "rrset: cmp equal pointers");
   74     ok(!knot_rrset_equal((knot_rrset_t *)0xcafebabe, (knot_rrset_t *)0xdeadbeef,
   75                         KNOT_RRSET_COMPARE_PTR), "rrset: cmp different pointers");
   76 
   77     // Test equal - header
   78     ok(knot_rrset_equal(rrset, copy, KNOT_RRSET_COMPARE_HEADER),
   79        "rrset: cmp equal headers");
   80 
   81     copy->type = KNOT_RRTYPE_AAAA;
   82     ok(!knot_rrset_equal(rrset, copy, KNOT_RRSET_COMPARE_HEADER),
   83        "rrset: cmp headers - different type");
   84 
   85     // Test equal - full, rdata empty
   86     copy->type = rrset->type;
   87     ok(knot_rrset_equal(rrset, copy, KNOT_RRSET_COMPARE_WHOLE),
   88        "rrset: cmp headers - rdata");
   89 
   90     knot_dname_free(rrset->owner, NULL);
   91     rrset->owner = NULL;
   92     ok(!knot_rrset_equal(rrset, copy, KNOT_RRSET_COMPARE_HEADER),
   93        "rrset: cmp NULL owner");
   94 
   95     ok(knot_rrset_equal(rrset, rrset, KNOT_RRSET_COMPARE_HEADER),
   96        "rrset: cmp NULL owners");
   97 
   98     // Test clear
   99     knot_rrset_clear(rrset, NULL);
  100     ok(rrset->owner == NULL, "rrset: clear.");
  101 
  102     // Test empty
  103     ok(knot_rrset_empty(rrset), "rrset: empty.");
  104     ok(knot_rrset_empty(NULL), "rrset: empty NULL.");
  105     copy->rrs.count = 1;
  106     ok(!knot_rrset_empty(copy), "rrset: not empty.");
  107 
  108     // Test init empty
  109     knot_rrset_init_empty(rrset);
  110     ok(check_rrset(rrset, NULL, 0, KNOT_CLASS_IN, 0), "rrset: init empty.");
  111 
  112     // "Test" freeing
  113     knot_rrset_free(rrset, NULL);
  114     knot_rrset_free(copy, NULL);
  115 
  116     return 0;
  117 }