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