summaryrefslogtreecommitdiffstats
path: root/src/resolve/test-resolved-packet.c
blob: 8a65ea0f5e78446107776ff35fb5f959a3799f6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "hexdecoct.h"
#include "log.h"
#include "resolved-dns-packet.h"
#include "tests.h"

TEST(dns_packet_new) {
        size_t i;
         _cleanup_(dns_packet_unrefp) DnsPacket *p2 = NULL;

        for (i = 0; i <= DNS_PACKET_SIZE_MAX; i++) {
                _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;

                assert_se(dns_packet_new(&p, DNS_PROTOCOL_DNS, i, DNS_PACKET_SIZE_MAX) == 0);

                log_debug("dns_packet_new: %zu → %zu", i, p->allocated);
                assert_se(p->allocated >= MIN(DNS_PACKET_SIZE_MAX, i));

                if (i > DNS_PACKET_SIZE_START + 10 && i < DNS_PACKET_SIZE_MAX - 10)
                        i = MIN(i * 2, DNS_PACKET_SIZE_MAX - 10);
        }

        assert_se(dns_packet_new(&p2, DNS_PROTOCOL_DNS, DNS_PACKET_SIZE_MAX + 1, DNS_PACKET_SIZE_MAX) == -EFBIG);
}

TEST(naptr) {
        _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;

        static const char twilio_reply[] =
                "Sq+BgAABAAkAAAABBnR3aWxpbwNjb20AACMAAcAMACMAAQAABwgAMgAUAAoBUwdTSVArRDJUAARf"
                "c2lwBF90Y3AEcHN0bgdpZTEtdG54BnR3aWxpbwNjb20AwAwAIwABAAAHCAAyAAoACgFTB1NJUCtE"
                "MlUABF9zaXAEX3VkcARwc3RuB3VzMi10bngGdHdpbGlvA2NvbQDADAAjAAEAAAcIADQAFAAKAVMI"
                "U0lQUytEMlQABV9zaXBzBF90Y3AEcHN0bgd1czEtdG54BnR3aWxpbwNjb20AwAwAIwABAAAHCAAy"
                "AAoACgFTB1NJUCtEMlUABF9zaXAEX3VkcARwc3RuB2llMS10bngGdHdpbGlvA2NvbQDADAAjAAEA"
                "AAcIADIAFAAKAVMHU0lQK0QyVAAEX3NpcARfdGNwBHBzdG4HdXMyLXRueAZ0d2lsaW8DY29tAMAM"
                "ACMAAQAABwgANAAUAAoBUwhTSVBTK0QyVAAFX3NpcHMEX3RjcARwc3RuB3VzMi10bngGdHdpbGlv"
                "A2NvbQDADAAjAAEAAAcIADQAFAAKAVMIU0lQUytEMlQABV9zaXBzBF90Y3AEcHN0bgdpZTEtdG54"
                "BnR3aWxpbwNjb20AwAwAIwABAAAHCAAyAAoACgFTB1NJUCtEMlUABF9zaXAEX3VkcARwc3RuB3Vz"
                "MS10bngGdHdpbGlvA2NvbQDADAAjAAEAAAcIADIAFAAKAVMHU0lQK0QyVAAEX3NpcARfdGNwBHBz"
                "dG4HdXMxLXRueAZ0d2lsaW8DY29tAAAAKQIAAAAAAAAA";

        static const char twilio_reply_string[] =
                "20 10 \"S\" \"SIP+D2T\" \"\" _sip._tcp.pstn.ie1-tnx.twilio.com.\n"
                "10 10 \"S\" \"SIP+D2U\" \"\" _sip._udp.pstn.us2-tnx.twilio.com.\n"
                "20 10 \"S\" \"SIPS+D2T\" \"\" _sips._tcp.pstn.us1-tnx.twilio.com.\n"
                "10 10 \"S\" \"SIP+D2U\" \"\" _sip._udp.pstn.ie1-tnx.twilio.com.\n"
                "20 10 \"S\" \"SIP+D2T\" \"\" _sip._tcp.pstn.us2-tnx.twilio.com.\n"
                "20 10 \"S\" \"SIPS+D2T\" \"\" _sips._tcp.pstn.us2-tnx.twilio.com.\n"
                "20 10 \"S\" \"SIPS+D2T\" \"\" _sips._tcp.pstn.ie1-tnx.twilio.com.\n"
                "10 10 \"S\" \"SIP+D2U\" \"\" _sip._udp.pstn.us1-tnx.twilio.com.\n"
                "20 10 \"S\" \"SIP+D2T\" \"\" _sip._tcp.pstn.us1-tnx.twilio.com.\n";

        static const char twilio_reply_json[] =
                "[\n"
                "        {\n"
                "               \"key\" : {\n"
                "                       \"class\" : 1,\n"
                "                       \"type\" : 35,\n"
                "                       \"name\" : \"twilio.com\"\n"
                "               },\n"
                "               \"order\" : 20,\n"
                "               \"preference\" : 10,\n"
                "               \"naptrFlags\" : \"S\",\n"
                "               \"services\" : \"SIP+D2T\",\n"
                "               \"regexp\" : \"\",\n"
                "               \"replacement\" : \"_sip._tcp.pstn.ie1-tnx.twilio.com\"\n"
                "       },\n"
                "       {\n"
                "               \"key\" : {\n"
                "                       \"class\" : 1,\n"
                "                       \"type\" : 35,\n"
                "                       \"name\" : \"twilio.com\"\n"
                "               },\n"
                "               \"order\" : 10,\n"
                "               \"preference\" : 10,\n"
                "               \"naptrFlags\" : \"S\",\n"
                "               \"services\" : \"SIP+D2U\",\n"
                "               \"regexp\" : \"\",\n"
                "               \"replacement\" : \"_sip._udp.pstn.us2-tnx.twilio.com\"\n"
                "       },\n"
                "       {\n"
                "               \"key\" : {\n"
                "                       \"class\" : 1,\n"
                "                       \"type\" : 35,\n"
                "                       \"name\" : \"twilio.com\"\n"
                "               },\n"
                "               \"order\" : 20,\n"
                "               \"preference\" : 10,\n"
                "               \"naptrFlags\" : \"S\",\n"
                "               \"services\" : \"SIPS+D2T\",\n"
                "               \"regexp\" : \"\",\n"
                "               \"replacement\" : \"_sips._tcp.pstn.us1-tnx.twilio.com\"\n"
                "       },\n"
                "       {\n"
                "               \"key\" : {\n"
                "                       \"class\" : 1,\n"
                "                       \"type\" : 35,\n"
                "                       \"name\" : \"twilio.com\"\n"
                "               },\n"
                "               \"order\" : 10,\n"
                "               \"preference\" : 10,\n"
                "               \"naptrFlags\" : \"S\",\n"
                "               \"services\" : \"SIP+D2U\",\n"
                "               \"regexp\" : \"\",\n"
                "               \"replacement\" : \"_sip._udp.pstn.ie1-tnx.twilio.com\"\n"
                "       },\n"
                "       {\n"
                "               \"key\" : {\n"
                "                       \"class\" : 1,\n"
                "                       \"type\" : 35,\n"
                "                       \"name\" : \"twilio.com\"\n"
                "               },\n"
                "               \"order\" : 20,\n"
                "               \"preference\" : 10,\n"
                "               \"naptrFlags\" : \"S\",\n"
                "               \"services\" : \"SIP+D2T\",\n"
                "               \"regexp\" : \"\",\n"
                "               \"replacement\" : \"_sip._tcp.pstn.us2-tnx.twilio.com\"\n"
                "       },\n"
                "       {\n"
                "               \"key\" : {\n"
                "                       \"class\" : 1,\n"
                "                       \"type\" : 35,\n"
                "                       \"name\" : \"twilio.com\"\n"
                "               },\n"
                "               \"order\" : 20,\n"
                "               \"preference\" : 10,\n"
                "               \"naptrFlags\" : \"S\",\n"
                "               \"services\" : \"SIPS+D2T\",\n"
                "               \"regexp\" : \"\",\n"
                "               \"replacement\" : \"_sips._tcp.pstn.us2-tnx.twilio.com\"\n"
                "       },\n"
                "       {\n"
                "               \"key\" : {\n"
                "                       \"class\" : 1,\n"
                "                       \"type\" : 35,\n"
                "                       \"name\" : \"twilio.com\"\n"
                "               },\n"
                "               \"order\" : 20,\n"
                "               \"preference\" : 10,\n"
                "               \"naptrFlags\" : \"S\",\n"
                "               \"services\" : \"SIPS+D2T\",\n"
                "               \"regexp\" : \"\",\n"
                "               \"replacement\" : \"_sips._tcp.pstn.ie1-tnx.twilio.com\"\n"
                "       },\n"
                "       {\n"
                "               \"key\" : {\n"
                "                       \"class\" : 1,\n"
                "                       \"type\" : 35,\n"
                "                       \"name\" : \"twilio.com\"\n"
                "               },\n"
                "               \"order\" : 10,\n"
                "               \"preference\" : 10,\n"
                "               \"naptrFlags\" : \"S\",\n"
                "               \"services\" : \"SIP+D2U\",\n"
                "               \"regexp\" : \"\",\n"
                "               \"replacement\" : \"_sip._udp.pstn.us1-tnx.twilio.com\"\n"
                "       },\n"
                "       {\n"
                "               \"key\" : {\n"
                "                       \"class\" : 1,\n"
                "                       \"type\" : 35,\n"
                "                       \"name\" : \"twilio.com\"\n"
                "               },\n"
                "               \"order\" : 20,\n"
                "               \"preference\" : 10,\n"
                "               \"naptrFlags\" : \"S\",\n"
                "               \"services\" : \"SIP+D2T\",\n"
                "               \"regexp\" : \"\",\n"
                "               \"replacement\" : \"_sip._tcp.pstn.us1-tnx.twilio.com\"\n"
                "       }\n"
                "]\n";

        _cleanup_free_ void *buf = NULL;
        size_t sz = 0;

        assert_se(unbase64mem(twilio_reply, &buf, &sz) >= 0);

        assert_se(dns_packet_new(&p, DNS_PROTOCOL_DNS, sz, DNS_PACKET_SIZE_MAX) == 0);
        assert_se(p->allocated >= sz);

        memcpy(DNS_PACKET_DATA(p), buf, sz);
        p->size = sz;

        assert_se(dns_packet_extract(p) >= 0);

        _cleanup_(json_variant_unrefp) JsonVariant *a = NULL;
        _cleanup_free_ char *joined = NULL;
        DnsResourceRecord *rr;
        DNS_ANSWER_FOREACH(rr, p->answer) {
                const char *s;

                s = ASSERT_PTR(dns_resource_record_to_string(rr));
                printf("%s\n", s);

                assert_se(strextend(&joined, s, "\n"));

                _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
                assert_se(dns_resource_record_to_json(rr, &v) >= 0);

                assert_se(json_variant_append_array(&a, v) >= 0);
        }

        assert(streq(joined, twilio_reply_string));

        _cleanup_(json_variant_unrefp) JsonVariant *parsed = NULL;
        assert_se(json_parse(twilio_reply_json, /* flags= */ 0, &parsed, /* ret_line= */ NULL, /* ret_column= */ NULL) >= 0);

        assert_se(json_variant_equal(parsed, a));
}

DEFINE_TEST_MAIN(LOG_DEBUG);