1:255.13-alt1
[systemd_ALT.git] / src / resolve / test-dns-packet.c
blobca09b0840c59b0b631953f8201b564983b0b007f
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <net/if.h>
5 #include "sd-id128.h"
7 #include "alloc-util.h"
8 #include "fileio.h"
9 #include "glob-util.h"
10 #include "log.h"
11 #include "macro.h"
12 #include "resolved-dns-packet.h"
13 #include "resolved-dns-rr.h"
14 #include "path-util.h"
15 #include "string-util.h"
16 #include "strv.h"
17 #include "tests.h"
18 #include "unaligned.h"
20 #define HASH_KEY SD_ID128_MAKE(d3,1e,48,90,4b,fa,4c,fe,af,9d,d5,a1,d7,2e,8a,b1)
22 static void verify_rr_copy(DnsResourceRecord *rr) {
23 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *copy = NULL;
24 const char *a, *b;
26 assert_se(copy = dns_resource_record_copy(rr));
27 assert_se(dns_resource_record_equal(copy, rr) > 0);
29 assert_se(a = dns_resource_record_to_string(rr));
30 assert_se(b = dns_resource_record_to_string(copy));
32 assert_se(streq(a, b));
35 static uint64_t hash(DnsResourceRecord *rr) {
36 struct siphash state;
38 siphash24_init(&state, HASH_KEY.bytes);
39 dns_resource_record_hash_func(rr, &state);
40 return siphash24_finalize(&state);
43 static void test_packet_from_file(const char* filename, bool canonical) {
44 _cleanup_free_ char *data = NULL;
45 size_t data_size, packet_size, offset;
47 assert_se(read_full_file(filename, &data, &data_size) >= 0);
48 assert_se(data);
49 assert_se(data_size > 8);
51 log_info("============== %s %s==============", filename, canonical ? "canonical " : "");
53 for (offset = 0; offset < data_size; offset += 8 + packet_size) {
54 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL, *p2 = NULL;
55 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL, *rr2 = NULL;
56 const char *s, *s2;
57 uint64_t hash1, hash2;
59 packet_size = unaligned_read_le64(data + offset);
60 assert_se(packet_size > 0);
61 assert_se(offset + 8 + packet_size <= data_size);
63 assert_se(dns_packet_new(&p, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX) >= 0);
65 assert_se(dns_packet_append_blob(p, data + offset + 8, packet_size, NULL) >= 0);
66 assert_se(dns_packet_read_rr(p, &rr, NULL, NULL) >= 0);
68 verify_rr_copy(rr);
70 s = dns_resource_record_to_string(rr);
71 assert_se(s);
72 puts(s);
74 hash1 = hash(rr);
76 assert_se(dns_resource_record_to_wire_format(rr, canonical) >= 0);
78 assert_se(dns_packet_new(&p2, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX) >= 0);
79 assert_se(dns_packet_append_blob(p2, rr->wire_format, rr->wire_format_size, NULL) >= 0);
80 assert_se(dns_packet_read_rr(p2, &rr2, NULL, NULL) >= 0);
82 verify_rr_copy(rr);
84 s2 = dns_resource_record_to_string(rr);
85 assert_se(s2);
86 assert_se(streq(s, s2));
88 hash2 = hash(rr);
89 assert_se(hash1 == hash2);
93 static void test_dns_resource_record_get_cname_target(void) {
94 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *cname = NULL, *dname = NULL;
95 _cleanup_free_ char *target = NULL;
97 assert_se(cname = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, "quux.foobar"));
98 assert_se(cname->cname.name = strdup("wuff.wuff"));
100 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_A, "waldo"), cname, &target) == -EUNATCH);
101 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_A, "foobar"), cname, &target) == -EUNATCH);
102 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_A, "quux"), cname, &target) == -EUNATCH);
103 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_A, ""), cname, &target) == -EUNATCH);
104 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_A, "."), cname, &target) == -EUNATCH);
105 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_A, "nope.quux.foobar"), cname, &target) == -EUNATCH);
106 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_A, "quux.foobar"), cname, &target) == 0);
107 assert_se(streq(target, "wuff.wuff"));
108 target = mfree(target);
110 assert_se(dname = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNAME, "quux.foobar"));
111 assert_se(dname->dname.name = strdup("wuff.wuff"));
113 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_A, "waldo"), dname, &target) == -EUNATCH);
114 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_A, "foobar"), dname, &target) == -EUNATCH);
115 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_A, "quux"), dname, &target) == -EUNATCH);
116 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_A, ""), dname, &target) == -EUNATCH);
117 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_A, "."), dname, &target) == -EUNATCH);
118 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_A, "yupp.quux.foobar"), dname, &target) == 0);
119 assert_se(streq(target, "yupp.wuff.wuff"));
120 target = mfree(target);
122 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_A, "quux.foobar"), cname, &target) == 0);
123 assert_se(streq(target, "wuff.wuff"));
126 int main(int argc, char **argv) {
127 int N;
128 _cleanup_globfree_ glob_t g = {};
129 char **fnames;
131 test_setup_logging(LOG_DEBUG);
133 if (argc >= 2) {
134 N = argc - 1;
135 fnames = argv + 1;
136 } else {
137 _cleanup_free_ char *pkts_glob = NULL;
138 assert_se(get_testdata_dir("test-resolve/*.pkts", &pkts_glob) >= 0);
139 assert_se(glob(pkts_glob, GLOB_NOSORT, NULL, &g) == 0);
140 N = g.gl_pathc;
141 fnames = g.gl_pathv;
144 for (int i = 0; i < N; i++) {
145 test_packet_from_file(fnames[i], false);
146 puts("");
147 test_packet_from_file(fnames[i], true);
148 if (i + 1 < N)
149 puts("");
152 test_dns_resource_record_get_cname_target();
154 return EXIT_SUCCESS;