LCOV - code coverage report
Current view: top level - resolve - test-dns-packet.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 59 61 96.7 %
Date: 2019-08-22 15:41:25 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <net/if.h>
       4             : #include <glob.h>
       5             : 
       6             : #include "sd-id128.h"
       7             : 
       8             : #include "alloc-util.h"
       9             : #include "fileio.h"
      10             : #include "glob-util.h"
      11             : #include "log.h"
      12             : #include "macro.h"
      13             : #include "resolved-dns-packet.h"
      14             : #include "resolved-dns-rr.h"
      15             : #include "path-util.h"
      16             : #include "string-util.h"
      17             : #include "strv.h"
      18             : #include "tests.h"
      19             : #include "unaligned.h"
      20             : 
      21             : #define HASH_KEY SD_ID128_MAKE(d3,1e,48,90,4b,fa,4c,fe,af,9d,d5,a1,d7,2e,8a,b1)
      22             : 
      23         476 : static void verify_rr_copy(DnsResourceRecord *rr) {
      24         476 :         _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *copy = NULL;
      25             :         const char *a, *b;
      26             : 
      27         476 :         assert_se(copy = dns_resource_record_copy(rr));
      28         476 :         assert_se(dns_resource_record_equal(copy, rr) > 0);
      29             : 
      30         476 :         assert_se(a = dns_resource_record_to_string(rr));
      31         476 :         assert_se(b = dns_resource_record_to_string(copy));
      32             : 
      33         476 :         assert_se(streq(a, b));
      34         476 : }
      35             : 
      36         476 : static uint64_t hash(DnsResourceRecord *rr) {
      37             :         struct siphash state;
      38             : 
      39         476 :         siphash24_init(&state, HASH_KEY.bytes);
      40         476 :         dns_resource_record_hash_func(rr, &state);
      41         476 :         return siphash24_finalize(&state);
      42             : }
      43             : 
      44          22 : static void test_packet_from_file(const char* filename, bool canonical) {
      45          22 :         _cleanup_free_ char *data = NULL;
      46             :         size_t data_size, packet_size, offset;
      47             : 
      48          22 :         assert_se(read_full_file(filename, &data, &data_size) >= 0);
      49          22 :         assert_se(data);
      50          22 :         assert_se(data_size > 8);
      51             : 
      52          22 :         log_info("============== %s %s==============", filename, canonical ? "canonical " : "");
      53             : 
      54         260 :         for (offset = 0; offset < data_size; offset += 8 + packet_size) {
      55         238 :                 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL, *p2 = NULL;
      56         238 :                 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL, *rr2 = NULL;
      57             :                 const char *s, *s2;
      58             :                 uint64_t hash1, hash2;
      59             : 
      60         238 :                 packet_size = unaligned_read_le64(data + offset);
      61         238 :                 assert_se(packet_size > 0);
      62         238 :                 assert_se(offset + 8 + packet_size <= data_size);
      63             : 
      64         238 :                 assert_se(dns_packet_new(&p, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX) >= 0);
      65             : 
      66         238 :                 assert_se(dns_packet_append_blob(p, data + offset + 8, packet_size, NULL) >= 0);
      67         238 :                 assert_se(dns_packet_read_rr(p, &rr, NULL, NULL) >= 0);
      68             : 
      69         238 :                 verify_rr_copy(rr);
      70             : 
      71         238 :                 s = dns_resource_record_to_string(rr);
      72         238 :                 assert_se(s);
      73         238 :                 puts(s);
      74             : 
      75         238 :                 hash1 = hash(rr);
      76             : 
      77         238 :                 assert_se(dns_resource_record_to_wire_format(rr, canonical) >= 0);
      78             : 
      79         238 :                 assert_se(dns_packet_new(&p2, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX) >= 0);
      80         238 :                 assert_se(dns_packet_append_blob(p2, rr->wire_format, rr->wire_format_size, NULL) >= 0);
      81         238 :                 assert_se(dns_packet_read_rr(p2, &rr2, NULL, NULL) >= 0);
      82             : 
      83         238 :                 verify_rr_copy(rr);
      84             : 
      85         238 :                 s2 = dns_resource_record_to_string(rr);
      86         238 :                 assert_se(s2);
      87         238 :                 assert_se(streq(s, s2));
      88             : 
      89         238 :                 hash2 = hash(rr);
      90         238 :                 assert_se(hash1 == hash2);
      91             :         }
      92          22 : }
      93             : 
      94           1 : int main(int argc, char **argv) {
      95             :         int i, N;
      96           1 :         _cleanup_free_ char *pkts_glob = NULL;
      97           2 :         _cleanup_globfree_ glob_t g = {};
      98             :         char **fnames;
      99             : 
     100           1 :         log_parse_environment();
     101             : 
     102           1 :         if (argc >= 2) {
     103           0 :                 N = argc - 1;
     104           0 :                 fnames = argv + 1;
     105             :         } else {
     106           1 :                 pkts_glob = path_join(get_testdata_dir(), "test-resolve/*.pkts");
     107           1 :                 assert_se(glob(pkts_glob, GLOB_NOSORT, NULL, &g) == 0);
     108           1 :                 N = g.gl_pathc;
     109           1 :                 fnames = g.gl_pathv;
     110             :         }
     111             : 
     112          12 :         for (i = 0; i < N; i++) {
     113          11 :                 test_packet_from_file(fnames[i], false);
     114          11 :                 puts("");
     115          11 :                 test_packet_from_file(fnames[i], true);
     116          11 :                 if (i + 1 < N)
     117          10 :                         puts("");
     118             :         }
     119             : 
     120           1 :         return EXIT_SUCCESS;
     121             : }

Generated by: LCOV version 1.14