"Fossies" - the Fresh Open Source Software Archive

Member "knot-2.8.3/tests/libknot/test_rdataset.c" (16 Jul 2019, 7833 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_rdataset.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 <tap/basic.h>
   19 #include <string.h>
   20 
   21 #include "libknot/rdataset.h"
   22 #include "libknot/libknot.h"
   23 
   24 // Inits rdataset with given rdata.
   25 #define RDATASET_INIT_WITH(set, rdata) \
   26     knot_rdataset_clear(&set, NULL); \
   27     ret = knot_rdataset_add(&set, rdata, NULL); \
   28     assert(ret == KNOT_EOK);
   29 
   30 int main(int argc, char *argv[])
   31 {
   32     plan_lazy();
   33 
   34     // Test init
   35     knot_rdataset_t rdataset;
   36     knot_rdataset_init(&rdataset);
   37     ok(rdataset.rdata == NULL && rdataset.count == 0, "rdataset: init.");
   38 
   39     // Test rdata addition
   40     uint8_t buf_gt[knot_rdata_size(4)];
   41     knot_rdata_t *rdata_gt = (knot_rdata_t *)buf_gt;
   42     knot_rdata_init(rdata_gt, 4, (uint8_t *)"wxyz");
   43 
   44     int ret = knot_rdataset_add(NULL, NULL, NULL);
   45     is_int(KNOT_EINVAL, ret, "rdataset: add NULL.");
   46     ret = knot_rdataset_add(&rdataset, rdata_gt, NULL);
   47     bool add_ok = ret == KNOT_EOK && rdataset.count == 1 &&
   48                   knot_rdata_cmp(rdata_gt, rdataset.rdata) == 0;
   49     ok(add_ok, "rdataset: add.");
   50 
   51     uint8_t buf_lo[knot_rdata_size(4)];
   52     knot_rdata_t *rdata_lo = (knot_rdata_t *)buf_lo;
   53     knot_rdata_init(rdata_lo, 4, (uint8_t *)"abcd");
   54     ret = knot_rdataset_add(&rdataset, rdata_lo, NULL);
   55     add_ok = ret == KNOT_EOK && rdataset.count == 2 &&
   56              knot_rdata_cmp(rdata_lo, rdataset.rdata) == 0;
   57     ok(add_ok, "rdataset: add lower.");
   58 
   59     // Test getters
   60     ok(knot_rdata_cmp(knot_rdataset_at(&rdataset, 0), rdata_lo) == 0 &&
   61        knot_rdata_cmp(knot_rdataset_at(&rdataset, 1), rdata_gt) == 0,
   62        "rdataset: at.");
   63 
   64     ok(knot_rdataset_size(&rdataset) == knot_rdata_size(4) * 2,
   65        "rdataset: size.");
   66 
   67     // Test copy
   68     ok(knot_rdataset_copy(NULL, NULL, NULL) == KNOT_EINVAL,
   69        "rdataset: copy NULL.");
   70     knot_rdataset_t copy;
   71     ret = knot_rdataset_copy(&copy, &rdataset, NULL);
   72     const bool copy_ok = ret == KNOT_EOK && copy.count == rdataset.count &&
   73                          knot_rdataset_size(&copy) == knot_rdataset_size(&rdataset) &&
   74                          memcmp(rdataset.rdata, copy.rdata,
   75                                 knot_rdataset_size(&rdataset)) == 0;
   76     ok(copy_ok, "rdataset: copy");
   77 
   78     // Test eq
   79     ok(knot_rdataset_eq(&rdataset, &copy), "rdataset: equal");
   80 
   81     // Test clear
   82     knot_rdataset_clear(&copy, NULL);
   83     ok(copy.count == 0 && copy.rdata == NULL, "rdataset: clear.");
   84 
   85     // Test not equal (different count)
   86     ok(!knot_rdataset_eq(&rdataset, &copy), "rdataset: not equal - count");
   87 
   88     // Test member
   89     uint8_t buf_not[knot_rdata_size(1)];
   90     knot_rdata_t *not_a_member = (knot_rdata_t *)buf_not;
   91     knot_rdata_init(not_a_member, 1, (uint8_t *)"?");
   92     ok(knot_rdataset_member(&rdataset, rdata_gt), "rdataset: is member.");
   93     ok(!knot_rdataset_member(&rdataset, not_a_member), "rdataset: is not member.");
   94 
   95     // Test merge
   96     ok(knot_rdataset_merge(NULL, NULL, NULL) == KNOT_EINVAL,
   97        "rdataset: merge NULL.");
   98     knot_rdataset_t empty;
   99     knot_rdataset_init(&empty);
  100     ret = knot_rdataset_merge(&empty, &rdataset, NULL);
  101     bool merge_ok = ret == KNOT_EOK && knot_rdataset_eq(&empty, &rdataset);
  102     ok(merge_ok, "rdataset: merge empty.");
  103     knot_rdata_t *data_before = rdataset.rdata;
  104     ret = knot_rdataset_merge(&rdataset, &rdataset, NULL);
  105     merge_ok = ret == KNOT_EOK && rdataset.count == 2 &&
  106                data_before == rdataset.rdata;
  107     ok(merge_ok, "rdataset: merge self.");
  108 
  109     knot_rdataset_clear(&empty, NULL);
  110 
  111     // Init structs for merge sort testing
  112     knot_rdataset_t rdataset_lo; // "Lower" rdataset
  113     knot_rdataset_init(&rdataset_lo);
  114     RDATASET_INIT_WITH(rdataset_lo, rdata_lo);
  115     knot_rdataset_t rdataset_gt; // "Greater" rdataset
  116     knot_rdataset_init(&rdataset_gt);
  117     RDATASET_INIT_WITH(rdataset_gt, rdata_gt);
  118 
  119     // Test not equal - different data
  120     ok(!knot_rdataset_eq(&rdataset_gt, &rdataset_lo), "rdataset: data not equal.");
  121 
  122     // Test that merge keeps the sorted order
  123     ret = knot_rdataset_merge(&rdataset_lo, &rdataset_gt, NULL);
  124     merge_ok = ret == KNOT_EOK && knot_rdataset_eq(&rdataset_lo, &rdataset);
  125     ok(merge_ok, "rdataset: merge into lower.");
  126 
  127     RDATASET_INIT_WITH(rdataset_lo, rdata_lo);
  128     RDATASET_INIT_WITH(rdataset_gt, rdata_gt);
  129     ret = knot_rdataset_merge(&rdataset_gt, &rdataset_lo, NULL);
  130     merge_ok = ret == KNOT_EOK && knot_rdataset_eq(&rdataset_gt, &rdataset);
  131     ok(merge_ok, "rdataset: merge into greater.");
  132 
  133     // Test intersect
  134     ok(knot_rdataset_intersect(NULL, NULL, NULL, NULL) == KNOT_EINVAL,
  135        "rdataset: intersect NULL.");
  136 
  137     knot_rdataset_t intersection;
  138     ret = knot_rdataset_intersect(&rdataset, &rdataset, &intersection, NULL);
  139     bool intersect_ok = ret == KNOT_EOK && knot_rdataset_eq(&rdataset, &intersection);
  140     ok(intersect_ok, "rdataset: intersect self.");
  141     knot_rdataset_clear(&intersection, NULL);
  142 
  143     RDATASET_INIT_WITH(rdataset_lo, rdata_lo);
  144     RDATASET_INIT_WITH(rdataset_gt, rdata_gt);
  145     ret = knot_rdataset_intersect(&rdataset_lo, &rdataset_gt, &intersection, NULL);
  146     intersect_ok = ret == KNOT_EOK && intersection.count == 0;
  147     ok(intersect_ok, "rdataset: intersect no common.");
  148 
  149     ret = knot_rdataset_intersect(&rdataset, &rdataset_lo, &intersection, NULL);
  150     intersect_ok = ret == KNOT_EOK && knot_rdataset_eq(&intersection, &rdataset_lo);
  151     ok(intersect_ok, "rdataset: intersect normal.");
  152     knot_rdataset_clear(&intersection, NULL);
  153 
  154     // Test subtract
  155     ok(knot_rdataset_subtract(NULL, NULL, NULL) == KNOT_EINVAL,
  156        "rdataset: subtract NULL.");
  157     ret = knot_rdataset_copy(&copy, &rdataset, NULL);
  158     assert(ret == KNOT_EOK);
  159     ok(knot_rdataset_subtract(&copy, &copy, NULL) == KNOT_EOK &&
  160        copy.count == 0, "rdataset: subtract self.");
  161 
  162     ret = knot_rdataset_copy(&copy, &rdataset, NULL);
  163     assert(ret == KNOT_EOK);
  164     ret = knot_rdataset_subtract(&copy, &rdataset, NULL);
  165     bool subtract_ok = ret == KNOT_EOK && copy.count == 0;
  166     ok(subtract_ok, "rdataset: subtract identical.");
  167 
  168     RDATASET_INIT_WITH(rdataset_lo, rdata_lo);
  169     RDATASET_INIT_WITH(rdataset_gt, rdata_gt);
  170     data_before = rdataset_lo.rdata;
  171     ret = knot_rdataset_subtract(&rdataset_lo, &rdataset_gt, NULL);
  172     subtract_ok = ret == KNOT_EOK && rdataset_lo.count == 1 &&
  173                   rdataset_lo.rdata == data_before;
  174     ok(subtract_ok, "rdataset: subtract no common.");
  175 
  176     ret = knot_rdataset_subtract(&rdataset, &rdataset_gt, NULL);
  177     subtract_ok = ret == KNOT_EOK && rdataset.count == 1;
  178     ok(subtract_ok, "rdataset: subtract the second.");
  179 
  180     ret = knot_rdataset_subtract(&rdataset, &rdataset_lo, NULL);
  181     subtract_ok = ret == KNOT_EOK && rdataset.count == 0 &&
  182                   rdataset.rdata == NULL;
  183     ok(subtract_ok, "rdataset: subtract last.");
  184 
  185     RDATASET_INIT_WITH(rdataset, rdata_gt);
  186 
  187     size_t old_rrs_size = knot_rdataset_size(&rdataset);
  188     size_t rr_size = rdata_lo->len;
  189     ret = knot_rdataset_reserve(&rdataset, rr_size, NULL);
  190     size_t new_rrs_size = knot_rdataset_size(&rdataset);
  191     bool reserve_ok = ret == KNOT_EOK && new_rrs_size == (old_rrs_size + knot_rdata_size(rr_size));
  192     ok(reserve_ok, "rdataset: reserve normal");
  193 
  194     RDATASET_INIT_WITH(copy, rdata_lo);
  195     knot_rdataset_add(&copy, rdata_gt, NULL);
  196 
  197     knot_rdata_init(knot_rdataset_at(&rdataset, 1), 4, (uint8_t *)"abcd");
  198 
  199     knot_rdataset_clear(&copy, NULL);
  200     knot_rdataset_clear(&rdataset, NULL);
  201     knot_rdataset_clear(&rdataset_lo, NULL);
  202     knot_rdataset_clear(&rdataset_gt, NULL);
  203 
  204     return EXIT_SUCCESS;
  205 }