aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/c-ares/test/ares-test-parse-uri.cc
blob: 1e436970e66b96c76e648e6d2e6a4c3fd504ef26 (plain) (blame)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
 * Copyright (C) The c-ares project
 *
 * Permission to use, copy, modify, and distribute this
 * software and its documentation for any purpose and without
 * fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting
 * documentation, and that the name of M.I.T. not be used in
 * advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * SPDX-License-Identifier: MIT
 */
#include "ares-test.h"
#include "dns-proto.h"

#include <sstream>
#include <vector>

namespace ares {
namespace test {

TEST_F(LibraryTest, ParseUriReplyOK) {
  DNSPacket pkt;
  pkt.set_qid(0x1234).set_response().set_aa()
    .add_question(new DNSQuestion("example.com", T_URI))
    .add_answer(new DNSUriRR("example.com", 100, 10, 20, "uri.example.com"))
    .add_answer(new DNSUriRR("example.com", 200, 11, 21, "uri2.example.com"));
  std::vector<byte> data = pkt.data();

  struct ares_uri_reply* uri = nullptr;
  EXPECT_EQ(ARES_SUCCESS, ares_parse_uri_reply(data.data(), data.size(), &uri));
  ASSERT_NE(nullptr, uri);

  EXPECT_EQ("uri.example.com", std::string(uri->uri));
  EXPECT_EQ(10, uri->priority);
  EXPECT_EQ(20, uri->weight);
  EXPECT_EQ(100, uri->ttl);

  struct ares_uri_reply* uri2 = uri->next;
  ASSERT_NE(nullptr, uri2);
  EXPECT_EQ("uri2.example.com", std::string(uri2->uri));
  EXPECT_EQ(11, uri2->priority);
  EXPECT_EQ(21, uri2->weight);
  EXPECT_EQ(200, uri2->ttl);
  EXPECT_EQ(nullptr, uri2->next);

  ares_free_data(uri);
}

TEST_F(LibraryTest, ParseUriReplySingle) {
  DNSPacket pkt;
  pkt.set_qid(0x1234).set_response().set_aa()
    .add_question(new DNSQuestion("example.abc.def.com", T_URI))
    .add_answer(new DNSUriRR("example.abc.def.com", 180, 0, 10, "example.abc.def.com"))
    .add_auth(new DNSNsRR("abc.def.com", 44, "else1.where.com"))
    .add_auth(new DNSNsRR("abc.def.com", 44, "else2.where.com"))
    .add_auth(new DNSNsRR("abc.def.com", 44, "else3.where.com"))
    .add_auth(new DNSNsRR("abc.def.com", 44, "else4.where.com"))
    .add_auth(new DNSNsRR("abc.def.com", 44, "else5.where.com"))
    .add_additional(new DNSARR("else2.where.com", 42, {172,19,0,1}))
    .add_additional(new DNSARR("else5.where.com", 42, {172,19,0,2}));
  std::vector<byte> data = pkt.data();

  struct ares_uri_reply* uri = nullptr;
  EXPECT_EQ(ARES_SUCCESS, ares_parse_uri_reply(data.data(), data.size(), &uri));
  ASSERT_NE(nullptr, uri);

  EXPECT_EQ("example.abc.def.com", std::string(uri->uri));
  EXPECT_EQ(0, uri->priority);
  EXPECT_EQ(10, uri->weight);
  EXPECT_EQ(180, uri->ttl);
  EXPECT_EQ(nullptr, uri->next);

  ares_free_data(uri);
}

TEST_F(LibraryTest, ParseUriReplyMalformed) {
  std::vector<byte> data = {
    0x12, 0x34,  // qid
    0x84, // response + query + AA + not-TC + not-RD
    0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
    0x00, 0x01,  // num questions
    0x00, 0x01,  // num answer RRs
    0x00, 0x00,  // num authority RRs
    0x00, 0x00,  // num additional RRs
    // Question
    0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
    0x03, 'c', 'o', 'm',
    0x00,
    0x01, 0x00,  // type URI
    0x00, 0x01,  // class IN
    // Answer 1
    0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
    0x03, 'c', 'o', 'm',
    0x00,
    0x01, 0x00,  // RR type
    0x00, 0x01,  // class IN
    0x01, 0x02, 0x03, 0x04, // TTL
    0x00, 0x04,  // rdata length -- too short
    0x02, 0x03, 0x04, 0x05,
  };

  struct ares_uri_reply* uri = nullptr;
  EXPECT_EQ(ARES_EBADRESP, ares_parse_uri_reply(data.data(), data.size(), &uri));
  ASSERT_EQ(nullptr, uri);
}

TEST_F(LibraryTest, ParseUriReplyMultiple) {
  DNSPacket pkt;
  pkt.set_qid(0x1234).set_response().set_ra().set_rd()
    .add_question(new DNSQuestion("uri.example.com", T_URI))
    .add_answer(new DNSUriRR("uri.example.com", 600, 0, 5, "a1.uri.example.com"))
    .add_answer(new DNSUriRR("uri.example.com", 660, 0, 5, "a2.uri.example.com"))
    .add_answer(new DNSUriRR("uri.example.com", 720, 0, 5, "a3.uri.example.com"))
    .add_auth(new DNSNsRR("example.com", 300, "ns1.example.com"))
    .add_auth(new DNSNsRR("example.com", 300, "ns2.example.com"))
    .add_auth(new DNSNsRR("example.com", 300, "ns3.example.com"))
    .add_additional(new DNSARR("a1.uri.example.com", 300, {172,19,1,1}))
    .add_additional(new DNSARR("a2.uri.example.com", 300, {172,19,1,2}))
    .add_additional(new DNSARR("a3.uri.example.com", 300, {172,19,1,3}))
    .add_additional(new DNSARR("n1.example.com", 300, {172,19,0,1}))
    .add_additional(new DNSARR("n2.example.com", 300, {172,19,0,2}))
    .add_additional(new DNSARR("n3.example.com", 300, {172,19,0,3}));
  std::vector<byte> data = pkt.data();

  struct ares_uri_reply* uri0 = nullptr;
  EXPECT_EQ(ARES_SUCCESS, ares_parse_uri_reply(data.data(), data.size(), &uri0));
  ASSERT_NE(nullptr, uri0);
  struct ares_uri_reply* uri = uri0;

  EXPECT_EQ("a1.uri.example.com", std::string(uri->uri));
  EXPECT_EQ(0, uri->priority);
  EXPECT_EQ(5, uri->weight);
  EXPECT_EQ(600, uri->ttl);
  EXPECT_NE(nullptr, uri->next);
  uri = uri->next;

  EXPECT_EQ("a2.uri.example.com", std::string(uri->uri));
  EXPECT_EQ(0, uri->priority);
  EXPECT_EQ(5, uri->weight);
  EXPECT_EQ(660, uri->ttl);
  EXPECT_NE(nullptr, uri->next);
  uri = uri->next;

  EXPECT_EQ("a3.uri.example.com", std::string(uri->uri));
  EXPECT_EQ(0, uri->priority);
  EXPECT_EQ(5, uri->weight);
  EXPECT_EQ(720, uri->ttl);
  EXPECT_EQ(nullptr, uri->next);

  ares_free_data(uri0);
}

TEST_F(LibraryTest, ParseUriReplyCname) {
  DNSPacket pkt;
  pkt.set_qid(0x1234).set_response().set_aa()
    .add_question(new DNSQuestion("example.abc.def.com", T_URI))
    .add_answer(new DNSCnameRR("example.abc.def.com", 300, "cname.abc.def.com"))
    .add_answer(new DNSUriRR("cname.abc.def.com", 600, 0, 10, "uri.abc.def.com"))
    .add_auth(new DNSNsRR("abc.def.com", 44, "else1.where.com"))
    .add_auth(new DNSNsRR("abc.def.com", 44, "else2.where.com"))
    .add_auth(new DNSNsRR("abc.def.com", 44, "else3.where.com"))
    .add_additional(new DNSARR("example.abc.def.com", 300, {172,19,0,1}))
    .add_additional(new DNSARR("else1.where.com", 42, {172,19,0,1}))
    .add_additional(new DNSARR("else2.where.com", 42, {172,19,0,2}))
    .add_additional(new DNSARR("else3.where.com", 42, {172,19,0,3}));
  std::vector<byte> data = pkt.data();

  struct ares_uri_reply* uri = nullptr;
  EXPECT_EQ(ARES_SUCCESS, ares_parse_uri_reply(data.data(), data.size(), &uri));
  ASSERT_NE(nullptr, uri);

  EXPECT_EQ("uri.abc.def.com", std::string(uri->uri));
  EXPECT_EQ(0, uri->priority);
  EXPECT_EQ(10, uri->weight);
  EXPECT_EQ(600, uri->ttl);
  EXPECT_EQ(nullptr, uri->next);

  ares_free_data(uri);
}

TEST_F(LibraryTest, ParseUriReplyCnameMultiple) {
  DNSPacket pkt;
  pkt.set_qid(0x1234).set_response().set_ra().set_rd()
    .add_question(new DNSQuestion("query.example.com", T_URI))
    .add_answer(new DNSCnameRR("query.example.com", 300, "uri.example.com"))
    .add_answer(new DNSUriRR("uri.example.com", 600, 0, 5, "a1.uri.example.com"))
    .add_answer(new DNSUriRR("uri.example.com", 660, 0, 5, "a2.uri.example.com"))
    .add_answer(new DNSUriRR("uri.example.com", 720, 0, 5, "a3.uri.example.com"))
    .add_auth(new DNSNsRR("example.com", 300, "ns1.example.com"))
    .add_auth(new DNSNsRR("example.com", 300, "ns2.example.com"))
    .add_auth(new DNSNsRR("example.com", 300, "ns3.example.com"))
    .add_additional(new DNSARR("a1.uri.example.com", 300, {172,19,1,1}))
    .add_additional(new DNSARR("a2.uri.example.com", 300, {172,19,1,2}))
    .add_additional(new DNSARR("a3.uri.example.com", 300, {172,19,1,3}))
    .add_additional(new DNSARR("n1.example.com", 300, {172,19,0,1}))
    .add_additional(new DNSARR("n2.example.com", 300, {172,19,0,2}))
    .add_additional(new DNSARR("n3.example.com", 300, {172,19,0,3}));
  std::vector<byte> data = pkt.data();

  struct ares_uri_reply* uri0 = nullptr;
  EXPECT_EQ(ARES_SUCCESS, ares_parse_uri_reply(data.data(), data.size(), &uri0));
  ASSERT_NE(nullptr, uri0);
  struct ares_uri_reply* uri = uri0;

  EXPECT_EQ("a1.uri.example.com", std::string(uri->uri));
  EXPECT_EQ(0, uri->priority);
  EXPECT_EQ(5, uri->weight);
  EXPECT_EQ(600, uri->ttl);
  EXPECT_NE(nullptr, uri->next);
  uri = uri->next;

  EXPECT_EQ("a2.uri.example.com", std::string(uri->uri));
  EXPECT_EQ(0, uri->priority);
  EXPECT_EQ(5, uri->weight);
  EXPECT_EQ(660, uri->ttl);
  EXPECT_NE(nullptr, uri->next);
  uri = uri->next;

  EXPECT_EQ("a3.uri.example.com", std::string(uri->uri));
  EXPECT_EQ(0, uri->priority);
  EXPECT_EQ(5, uri->weight);
  EXPECT_EQ(720, uri->ttl);
  EXPECT_EQ(nullptr, uri->next);

  ares_free_data(uri0);
}

TEST_F(LibraryTest, ParseUriReplyErrors) {
  DNSPacket pkt;
  pkt.set_qid(0x1234).set_response().set_aa()
    .add_question(new DNSQuestion("example.abc.def.com", T_URI))
    .add_answer(new DNSUriRR("example.abc.def.com", 180, 0, 10, "example.abc.def.com"));
  std::vector<byte> data;
  struct ares_uri_reply* uri = nullptr;

  // No question.
  pkt.questions_.clear();
  data = pkt.data();
  EXPECT_EQ(ARES_EBADRESP, ares_parse_uri_reply(data.data(), data.size(), &uri));
  pkt.add_question(new DNSQuestion("example.abc.def.com", T_URI));

#ifdef DISABLED
  // Question != answer
  pkt.questions_.clear();
  pkt.add_question(new DNSQuestion("Axample.com", T_URI));
  data = pkt.data();
  EXPECT_EQ(ARES_ENODATA, ares_parse_uri_reply(data.data(), data.size(), &uri));
  pkt.questions_.clear();
  pkt.add_question(new DNSQuestion("example.com", T_URI));
#endif

  // Two questions.
  pkt.add_question(new DNSQuestion("example.abc.def.com", T_URI));
  data = pkt.data();
  EXPECT_EQ(ARES_EBADRESP, ares_parse_uri_reply(data.data(), data.size(), &uri));
  pkt.questions_.clear();
  pkt.add_question(new DNSQuestion("64.48.32.16.in-addr.arpa", T_PTR));

  // Wrong sort of answer.
  pkt.answers_.clear();
  pkt.add_answer(new DNSMxRR("example.com", 100, 100, "mx1.example.com"));
  data = pkt.data();
  EXPECT_EQ(ARES_SUCCESS, ares_parse_uri_reply(data.data(), data.size(), &uri));
  EXPECT_EQ(nullptr, uri);
  pkt.answers_.clear();
  pkt.add_answer(new DNSUriRR("example.abc.def.com", 180, 0, 10, "example.abc.def.com"));

  // No answer.
  pkt.answers_.clear();
  data = pkt.data();
  EXPECT_EQ(ARES_ENODATA, ares_parse_uri_reply(data.data(), data.size(), &uri));
  pkt.add_answer(new DNSUriRR("example.abc.def.com", 180, 0, 10, "example.abc.def.com"));

  // Truncated packets.
  data = pkt.data();
  for (size_t len = 1; len < data.size(); len++) {
    int rc = ares_parse_uri_reply(data.data(), len, &uri);
    EXPECT_TRUE(rc == ARES_EBADRESP || rc == ARES_EBADNAME);
  }
}

TEST_F(LibraryTest, ParseUriReplyAllocFail) {
  DNSPacket pkt;
  pkt.set_qid(0x1234).set_response().set_aa()
    .add_question(new DNSQuestion("example.abc.def.com", T_URI))
    .add_answer(new DNSCnameRR("example.com", 300, "c.example.com"))
    .add_answer(new DNSUriRR("example.abc.def.com", 180, 0, 10, "example.abc.def.com"));
  std::vector<byte> data = pkt.data();
  struct ares_uri_reply* uri = nullptr;

  for (int ii = 1; ii <= 5; ii++) {
    ClearFails();
    SetAllocFail(ii);
    EXPECT_EQ(ARES_ENOMEM, ares_parse_uri_reply(data.data(), data.size(), &uri)) << ii;
  }
}

}  // namespace test
}  // namespace ares