• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2018 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "p2p/base/mdns_message.h"
12 
13 #include <map>
14 #include <set>
15 #include <string>
16 
17 #include "rtc_base/byte_buffer.h"
18 #include "rtc_base/gunit.h"
19 #include "rtc_base/ip_address.h"
20 #include "rtc_base/socket_address.h"
21 #include "test/gmock.h"
22 
23 #define ReadMdnsMessage(X, Y) ReadMdnsMessageTestCase(X, Y, sizeof(Y))
24 #define WriteMdnsMessageAndCompare(X, Y) \
25   WriteMdnsMessageAndCompareWithTestCast(X, Y, sizeof(Y))
26 
27 using ::testing::ElementsAre;
28 using ::testing::Pair;
29 using ::testing::UnorderedElementsAre;
30 
31 namespace webrtc {
32 
33 namespace {
34 
35 const uint8_t kSingleQuestionForIPv4AddrWithUnicastResponse[] = {
36     0x12, 0x34,                                // ID
37     0x00, 0x00,                                // flags
38     0x00, 0x01,                                // number of questions
39     0x00, 0x00,                                // number of answer rr
40     0x00, 0x00,                                // number of name server rr
41     0x00, 0x00,                                // number of additional rr
42     0x06, 0x77, 0x65, 0x62, 0x72, 0x74, 0x63,  // webrtc
43     0x03, 0x6f, 0x72, 0x67,                    // org
44     0x00,                                      // null label
45     0x00, 0x01,                                // type A Record
46     0x80, 0x01,                                // class IN, unicast response
47 };
48 
49 const uint8_t kTwoQuestionsForIPv4AndIPv6AddrWithMulticastResponse[] = {
50     0x12, 0x34,                                      // ID
51     0x00, 0x00,                                      // flags
52     0x00, 0x02,                                      // number of questions
53     0x00, 0x00,                                      // number of answer rr
54     0x00, 0x00,                                      // number of name server rr
55     0x00, 0x00,                                      // number of additional rr
56     0x07, 0x77, 0x65, 0x62, 0x72, 0x74, 0x63, 0x34,  // webrtc4
57     0x03, 0x6f, 0x72, 0x67,                          // org
58     0x00,                                            // null label
59     0x00, 0x01,                                      // type A Record
60     0x00, 0x01,  // class IN, multicast response
61     0x07, 0x77, 0x65, 0x62, 0x72, 0x74, 0x63, 0x36,  // webrtc6
62     0x03, 0x6f, 0x72, 0x67,                          // org
63     0x00,                                            // null label
64     0x00, 0x1C,                                      // type AAAA Record
65     0x00, 0x01,  // class IN, multicast response
66 };
67 
68 const uint8_t
69     kTwoQuestionsForIPv4AndIPv6AddrWithMulticastResponseAndNameCompression[] = {
70         0x12, 0x34,                                // ID
71         0x00, 0x00,                                // flags
72         0x00, 0x02,                                // number of questions
73         0x00, 0x00,                                // number of answer rr
74         0x00, 0x00,                                // number of name server rr
75         0x00, 0x00,                                // number of additional rr
76         0x03, 0x77, 0x77, 0x77,                    // www
77         0x06, 0x77, 0x65, 0x62, 0x72, 0x74, 0x63,  // webrtc
78         0x03, 0x6f, 0x72, 0x67,                    // org
79         0x00,                                      // null label
80         0x00, 0x01,                                // type A Record
81         0x00, 0x01,                    // class IN, multicast response
82         0x04, 0x6d, 0x64, 0x6e, 0x73,  // mdns
83         0xc0, 0x10,                    // offset 16, webrtc.org.
84         0x00, 0x1C,                    // type AAAA Record
85         0x00, 0x01,                    // class IN, multicast response
86 };
87 
88 const uint8_t kThreeQuestionsWithTwoPointersToTheSameNameSuffix[] = {
89     0x12, 0x34,                                // ID
90     0x00, 0x00,                                // flags
91     0x00, 0x03,                                // number of questions
92     0x00, 0x00,                                // number of answer rr
93     0x00, 0x00,                                // number of name server rr
94     0x00, 0x00,                                // number of additional rr
95     0x03, 0x77, 0x77, 0x77,                    // www
96     0x06, 0x77, 0x65, 0x62, 0x72, 0x74, 0x63,  // webrtc
97     0x03, 0x6f, 0x72, 0x67,                    // org
98     0x00,                                      // null label
99     0x00, 0x01,                                // type A Record
100     0x00, 0x01,                                // class IN, multicast response
101     0x04, 0x6d, 0x64, 0x6e, 0x73,              // mdns
102     0xc0, 0x10,                                // offset 16, webrtc.org.
103     0x00, 0x1C,                                // type AAAA Record
104     0x00, 0x01,                                // class IN, multicast response
105     0xc0, 0x10,                                // offset 16, webrtc.org.
106     0x00, 0x01,                                // type A Record
107     0x00, 0x01,                                // class IN, multicast response
108 };
109 
110 const uint8_t kThreeQuestionsWithPointerToNameSuffixContainingAnotherPointer[] =
111     {
112         0x12, 0x34,                                // ID
113         0x00, 0x00,                                // flags
114         0x00, 0x03,                                // number of questions
115         0x00, 0x00,                                // number of answer rr
116         0x00, 0x00,                                // number of name server rr
117         0x00, 0x00,                                // number of additional rr
118         0x03, 0x77, 0x77, 0x77,                    // www
119         0x06, 0x77, 0x65, 0x62, 0x72, 0x74, 0x63,  // webrtc
120         0x03, 0x6f, 0x72, 0x67,                    // org
121         0x00,                                      // null label
122         0x00, 0x01,                                // type A Record
123         0x00, 0x01,                    // class IN, multicast response
124         0x04, 0x6d, 0x64, 0x6e, 0x73,  // mdns
125         0xc0, 0x10,                    // offset 16, webrtc.org.
126         0x00, 0x1C,                    // type AAAA Record
127         0x00, 0x01,                    // class IN, multicast response
128         0x03, 0x77, 0x77, 0x77,        // www
129         0xc0, 0x20,                    // offset 32, mdns.webrtc.org.
130         0x00, 0x01,                    // type A Record
131         0x00, 0x01,                    // class IN, multicast response
132 };
133 
134 const uint8_t kCorruptedQuestionWithNameCompression1[] = {
135     0x12, 0x34,  // ID
136     0x84, 0x00,  // flags
137     0x00, 0x01,  // number of questions
138     0x00, 0x00,  // number of answer rr
139     0x00, 0x00,  // number of name server rr
140     0x00, 0x00,  // number of additional rr
141     0xc0, 0x0c,  // offset 12,
142     0x00, 0x01,  // type A Record
143     0x00, 0x01,  // class IN
144 };
145 
146 const uint8_t kCorruptedQuestionWithNameCompression2[] = {
147     0x12, 0x34,  // ID
148     0x84, 0x00,  // flags
149     0x00, 0x01,  // number of questions
150     0x00, 0x00,  // number of answer rr
151     0x00, 0x00,  // number of name server rr
152     0x00, 0x00,  // number of additional rr
153     0x01, 0x77,  // w
154     0xc0, 0x0c,  // offset 12,
155     0x00, 0x01,  // type A Record
156     0x00, 0x01,  // class IN
157 };
158 
159 const uint8_t kSingleAuthoritativeAnswerWithIPv4Addr[] = {
160     0x12, 0x34,                                // ID
161     0x84, 0x00,                                // flags
162     0x00, 0x00,                                // number of questions
163     0x00, 0x01,                                // number of answer rr
164     0x00, 0x00,                                // number of name server rr
165     0x00, 0x00,                                // number of additional rr
166     0x06, 0x77, 0x65, 0x62, 0x72, 0x74, 0x63,  // webrtc
167     0x03, 0x6f, 0x72, 0x67,                    // org
168     0x00,                                      // null label
169     0x00, 0x01,                                // type A Record
170     0x00, 0x01,                                // class IN
171     0x00, 0x00, 0x00, 0x78,                    // TTL, 120 seconds
172     0x00, 0x04,                                // rdlength, 32 bits
173     0xC0, 0xA8, 0x00, 0x01,                    // 192.168.0.1
174 };
175 
176 const uint8_t kTwoAuthoritativeAnswersWithIPv4AndIPv6Addr[] = {
177     0x12, 0x34,                                      // ID
178     0x84, 0x00,                                      // flags
179     0x00, 0x00,                                      // number of questions
180     0x00, 0x02,                                      // number of answer rr
181     0x00, 0x00,                                      // number of name server rr
182     0x00, 0x00,                                      // number of additional rr
183     0x07, 0x77, 0x65, 0x62, 0x72, 0x74, 0x63, 0x34,  // webrtc4
184     0x03, 0x6f, 0x72, 0x67,                          // org
185     0x00,                                            // null label
186     0x00, 0x01,                                      // type A Record
187     0x00, 0x01,                                      // class IN
188     0x00, 0x00, 0x00, 0x3c,                          // TTL, 60 seconds
189     0x00, 0x04,                                      // rdlength, 32 bits
190     0xC0, 0xA8, 0x00, 0x01,                          // 192.168.0.1
191     0x07, 0x77, 0x65, 0x62, 0x72, 0x74, 0x63, 0x36,  // webrtc6
192     0x03, 0x6f, 0x72, 0x67,                          // org
193     0x00,                                            // null label
194     0x00, 0x1C,                                      // type AAAA Record
195     0x00, 0x01,                                      // class IN
196     0x00, 0x00, 0x00, 0x78,                          // TTL, 120 seconds
197     0x00, 0x10,                                      // rdlength, 128 bits
198     0xfd, 0x12, 0x34, 0x56, 0x78, 0x9a, 0x00, 0x01,  // fd12:3456:789a:1::1
199     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
200 };
201 
202 const uint8_t kTwoAuthoritativeAnswersWithIPv4AndIPv6AddrWithNameCompression[] =
203     {
204         0x12, 0x34,                                // ID
205         0x84, 0x00,                                // flags
206         0x00, 0x00,                                // number of questions
207         0x00, 0x02,                                // number of answer rr
208         0x00, 0x00,                                // number of name server rr
209         0x00, 0x00,                                // number of additional rr
210         0x03, 0x77, 0x77, 0x77,                    // www
211         0x06, 0x77, 0x65, 0x62, 0x72, 0x74, 0x63,  // webrtc
212         0x03, 0x6f, 0x72, 0x67,                    // org
213         0x00,                                      // null label
214         0x00, 0x01,                                // type A Record
215         0x00, 0x01,                                // class IN
216         0x00, 0x00, 0x00, 0x3c,                    // TTL, 60 seconds
217         0x00, 0x04,                                // rdlength, 32 bits
218         0xc0, 0xA8, 0x00, 0x01,                    // 192.168.0.1
219         0xc0, 0x10,                                // offset 16, webrtc.org.
220         0x00, 0x1C,                                // type AAAA Record
221         0x00, 0x01,                                // class IN
222         0x00, 0x00, 0x00, 0x78,                    // TTL, 120 seconds
223         0x00, 0x10,                                // rdlength, 128 bits
224         0xfd, 0x12, 0x34, 0x56, 0x78, 0x9a, 0x00, 0x01,  // fd12:3456:789a:1::1
225         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
226 };
227 
228 const uint8_t kCorruptedAnswerWithNameCompression1[] = {
229     0x12, 0x34,              // ID
230     0x84, 0x00,              // flags
231     0x00, 0x00,              // number of questions
232     0x00, 0x01,              // number of answer rr
233     0x00, 0x00,              // number of name server rr
234     0x00, 0x00,              // number of additional rr
235     0xc0, 0x0c,              // offset 12,
236     0x00, 0x01,              // type A Record
237     0x00, 0x01,              // class IN
238     0x00, 0x00, 0x00, 0x3c,  // TTL, 60 seconds
239     0x00, 0x04,              // rdlength, 32 bits
240     0xc0, 0xA8, 0x00, 0x01,  // 192.168.0.1
241 };
242 
243 const uint8_t kCorruptedAnswerWithNameCompression2[] = {
244     0x12, 0x34,              // ID
245     0x84, 0x00,              // flags
246     0x00, 0x00,              // number of questions
247     0x00, 0x01,              // number of answer rr
248     0x00, 0x00,              // number of name server rr
249     0x00, 0x00,              // number of additional rr
250     0x01, 0x77,              // w
251     0xc0, 0x0c,              // offset 12,
252     0x00, 0x01,              // type A Record
253     0x00, 0x01,              // class IN
254     0x00, 0x00, 0x00, 0x3c,  // TTL, 60 seconds
255     0x00, 0x04,              // rdlength, 32 bits
256     0xc0, 0xA8, 0x00, 0x01,  // 192.168.0.1
257 };
258 
ReadMdnsMessageTestCase(MdnsMessage * msg,const uint8_t * testcase,size_t size)259 bool ReadMdnsMessageTestCase(MdnsMessage* msg,
260                              const uint8_t* testcase,
261                              size_t size) {
262   MessageBufferReader buf(reinterpret_cast<const char*>(testcase), size);
263   return msg->Read(&buf);
264 }
265 
WriteMdnsMessageAndCompareWithTestCast(MdnsMessage * msg,const uint8_t * testcase,size_t size)266 void WriteMdnsMessageAndCompareWithTestCast(MdnsMessage* msg,
267                                             const uint8_t* testcase,
268                                             size_t size) {
269   rtc::ByteBufferWriter out;
270   EXPECT_TRUE(msg->Write(&out));
271   EXPECT_EQ(size, out.Length());
272   int len = static_cast<int>(out.Length());
273   rtc::ByteBufferReader read_buf(out);
274   std::string bytes;
275   read_buf.ReadString(&bytes, len);
276   std::string testcase_bytes(reinterpret_cast<const char*>(testcase), size);
277   EXPECT_EQ(testcase_bytes, bytes);
278 }
279 
GetQueriedNames(MdnsMessage * msg,std::set<std::string> * names)280 bool GetQueriedNames(MdnsMessage* msg, std::set<std::string>* names) {
281   if (!msg->IsQuery() || msg->question_section().empty()) {
282     return false;
283   }
284   for (const auto& question : msg->question_section()) {
285     names->insert(question.GetName());
286   }
287   return true;
288 }
289 
GetResolution(MdnsMessage * msg,std::map<std::string,rtc::IPAddress> * names)290 bool GetResolution(MdnsMessage* msg,
291                    std::map<std::string, rtc::IPAddress>* names) {
292   if (msg->IsQuery() || msg->answer_section().empty()) {
293     return false;
294   }
295   for (const auto& answer : msg->answer_section()) {
296     rtc::IPAddress resolved_addr;
297     if (!answer.GetIPAddressFromRecordData(&resolved_addr)) {
298       return false;
299     }
300     (*names)[answer.GetName()] = resolved_addr;
301   }
302   return true;
303 }
304 
305 }  // namespace
306 
TEST(MdnsMessageTest,ReadSingleQuestionForIPv4Address)307 TEST(MdnsMessageTest, ReadSingleQuestionForIPv4Address) {
308   MdnsMessage msg;
309   ASSERT_TRUE(
310       ReadMdnsMessage(&msg, kSingleQuestionForIPv4AddrWithUnicastResponse));
311   EXPECT_TRUE(msg.IsQuery());
312   EXPECT_EQ(0x1234, msg.GetId());
313   ASSERT_EQ(1u, msg.question_section().size());
314   EXPECT_EQ(0u, msg.answer_section().size());
315   EXPECT_EQ(0u, msg.authority_section().size());
316   EXPECT_EQ(0u, msg.additional_section().size());
317   EXPECT_TRUE(msg.ShouldUnicastResponse());
318 
319   const auto& question = msg.question_section()[0];
320   EXPECT_EQ(SectionEntryType::kA, question.GetType());
321 
322   std::set<std::string> queried_names;
323   EXPECT_TRUE(GetQueriedNames(&msg, &queried_names));
324   EXPECT_THAT(queried_names, ElementsAre("webrtc.org."));
325 }
326 
TEST(MdnsMessageTest,ReadTwoQuestionsForIPv4AndIPv6Addr)327 TEST(MdnsMessageTest, ReadTwoQuestionsForIPv4AndIPv6Addr) {
328   MdnsMessage msg;
329   ASSERT_TRUE(ReadMdnsMessage(
330       &msg, kTwoQuestionsForIPv4AndIPv6AddrWithMulticastResponse));
331   EXPECT_TRUE(msg.IsQuery());
332   EXPECT_EQ(0x1234, msg.GetId());
333   ASSERT_EQ(2u, msg.question_section().size());
334   EXPECT_EQ(0u, msg.answer_section().size());
335   EXPECT_EQ(0u, msg.authority_section().size());
336   EXPECT_EQ(0u, msg.additional_section().size());
337 
338   const auto& question1 = msg.question_section()[0];
339   const auto& question2 = msg.question_section()[1];
340   EXPECT_EQ(SectionEntryType::kA, question1.GetType());
341   EXPECT_EQ(SectionEntryType::kAAAA, question2.GetType());
342 
343   std::set<std::string> queried_names;
344   EXPECT_TRUE(GetQueriedNames(&msg, &queried_names));
345   EXPECT_THAT(queried_names,
346               UnorderedElementsAre("webrtc4.org.", "webrtc6.org."));
347 }
348 
TEST(MdnsMessageTest,ReadTwoQuestionsForIPv4AndIPv6AddrWithNameCompression)349 TEST(MdnsMessageTest, ReadTwoQuestionsForIPv4AndIPv6AddrWithNameCompression) {
350   MdnsMessage msg;
351   ASSERT_TRUE(ReadMdnsMessage(
352       &msg,
353       kTwoQuestionsForIPv4AndIPv6AddrWithMulticastResponseAndNameCompression));
354 
355   ASSERT_EQ(2u, msg.question_section().size());
356   const auto& question1 = msg.question_section()[0];
357   const auto& question2 = msg.question_section()[1];
358   EXPECT_EQ(SectionEntryType::kA, question1.GetType());
359   EXPECT_EQ(SectionEntryType::kAAAA, question2.GetType());
360 
361   std::set<std::string> queried_names;
362   EXPECT_TRUE(GetQueriedNames(&msg, &queried_names));
363   EXPECT_THAT(queried_names,
364               UnorderedElementsAre("www.webrtc.org.", "mdns.webrtc.org."));
365 }
366 
TEST(MdnsMessageTest,ReadThreeQuestionsWithTwoPointersToTheSameNameSuffix)367 TEST(MdnsMessageTest, ReadThreeQuestionsWithTwoPointersToTheSameNameSuffix) {
368   MdnsMessage msg;
369   ASSERT_TRUE(
370       ReadMdnsMessage(&msg, kThreeQuestionsWithTwoPointersToTheSameNameSuffix));
371 
372   ASSERT_EQ(3u, msg.question_section().size());
373   const auto& question1 = msg.question_section()[0];
374   const auto& question2 = msg.question_section()[1];
375   const auto& question3 = msg.question_section()[2];
376   EXPECT_EQ(SectionEntryType::kA, question1.GetType());
377   EXPECT_EQ(SectionEntryType::kAAAA, question2.GetType());
378   EXPECT_EQ(SectionEntryType::kA, question3.GetType());
379 
380   std::set<std::string> queried_names;
381   EXPECT_TRUE(GetQueriedNames(&msg, &queried_names));
382   EXPECT_THAT(queried_names,
383               UnorderedElementsAre("www.webrtc.org.", "mdns.webrtc.org.",
384                                    "webrtc.org."));
385 }
386 
TEST(MdnsMessageTest,ReadThreeQuestionsWithPointerToNameSuffixContainingAnotherPointer)387 TEST(MdnsMessageTest,
388      ReadThreeQuestionsWithPointerToNameSuffixContainingAnotherPointer) {
389   MdnsMessage msg;
390   ASSERT_TRUE(ReadMdnsMessage(
391       &msg, kThreeQuestionsWithPointerToNameSuffixContainingAnotherPointer));
392 
393   ASSERT_EQ(3u, msg.question_section().size());
394   const auto& question1 = msg.question_section()[0];
395   const auto& question2 = msg.question_section()[1];
396   const auto& question3 = msg.question_section()[2];
397   EXPECT_EQ(SectionEntryType::kA, question1.GetType());
398   EXPECT_EQ(SectionEntryType::kAAAA, question2.GetType());
399   EXPECT_EQ(SectionEntryType::kA, question3.GetType());
400 
401   std::set<std::string> queried_names;
402   EXPECT_TRUE(GetQueriedNames(&msg, &queried_names));
403   EXPECT_THAT(queried_names,
404               UnorderedElementsAre("www.webrtc.org.", "mdns.webrtc.org.",
405                                    "www.mdns.webrtc.org."));
406 }
407 
TEST(MdnsMessageTest,ReadQuestionWithCorruptedPointerInNameCompressionShouldFail)408 TEST(MdnsMessageTest,
409      ReadQuestionWithCorruptedPointerInNameCompressionShouldFail) {
410   MdnsMessage msg;
411   EXPECT_FALSE(ReadMdnsMessage(&msg, kCorruptedQuestionWithNameCompression1));
412   EXPECT_FALSE(ReadMdnsMessage(&msg, kCorruptedQuestionWithNameCompression2));
413 }
414 
TEST(MdnsMessageTest,ReadSingleAnswerForIPv4Addr)415 TEST(MdnsMessageTest, ReadSingleAnswerForIPv4Addr) {
416   MdnsMessage msg;
417   ASSERT_TRUE(ReadMdnsMessage(&msg, kSingleAuthoritativeAnswerWithIPv4Addr));
418   EXPECT_FALSE(msg.IsQuery());
419   EXPECT_TRUE(msg.IsAuthoritative());
420   EXPECT_EQ(0x1234, msg.GetId());
421   EXPECT_EQ(0u, msg.question_section().size());
422   ASSERT_EQ(1u, msg.answer_section().size());
423   EXPECT_EQ(0u, msg.authority_section().size());
424   EXPECT_EQ(0u, msg.additional_section().size());
425 
426   const auto& answer = msg.answer_section()[0];
427   EXPECT_EQ(SectionEntryType::kA, answer.GetType());
428   EXPECT_EQ(120u, answer.GetTtlSeconds());
429 
430   std::map<std::string, rtc::IPAddress> resolution;
431   EXPECT_TRUE(GetResolution(&msg, &resolution));
432   rtc::IPAddress expected_addr(rtc::SocketAddress("192.168.0.1", 0).ipaddr());
433   EXPECT_THAT(resolution, ElementsAre(Pair("webrtc.org.", expected_addr)));
434 }
435 
TEST(MdnsMessageTest,ReadTwoAnswersForIPv4AndIPv6Addr)436 TEST(MdnsMessageTest, ReadTwoAnswersForIPv4AndIPv6Addr) {
437   MdnsMessage msg;
438   ASSERT_TRUE(
439       ReadMdnsMessage(&msg, kTwoAuthoritativeAnswersWithIPv4AndIPv6Addr));
440   EXPECT_FALSE(msg.IsQuery());
441   EXPECT_TRUE(msg.IsAuthoritative());
442   EXPECT_EQ(0x1234, msg.GetId());
443   EXPECT_EQ(0u, msg.question_section().size());
444   ASSERT_EQ(2u, msg.answer_section().size());
445   EXPECT_EQ(0u, msg.authority_section().size());
446   EXPECT_EQ(0u, msg.additional_section().size());
447 
448   const auto& answer1 = msg.answer_section()[0];
449   const auto& answer2 = msg.answer_section()[1];
450   EXPECT_EQ(SectionEntryType::kA, answer1.GetType());
451   EXPECT_EQ(SectionEntryType::kAAAA, answer2.GetType());
452   EXPECT_EQ(60u, answer1.GetTtlSeconds());
453   EXPECT_EQ(120u, answer2.GetTtlSeconds());
454 
455   std::map<std::string, rtc::IPAddress> resolution;
456   EXPECT_TRUE(GetResolution(&msg, &resolution));
457   rtc::IPAddress expected_addr_ipv4(
458       rtc::SocketAddress("192.168.0.1", 0).ipaddr());
459   rtc::IPAddress expected_addr_ipv6(
460       rtc::SocketAddress("fd12:3456:789a:1::1", 0).ipaddr());
461   EXPECT_THAT(resolution,
462               UnorderedElementsAre(Pair("webrtc4.org.", expected_addr_ipv4),
463                                    Pair("webrtc6.org.", expected_addr_ipv6)));
464 }
465 
TEST(MdnsMessageTest,ReadTwoAnswersForIPv4AndIPv6AddrWithNameCompression)466 TEST(MdnsMessageTest, ReadTwoAnswersForIPv4AndIPv6AddrWithNameCompression) {
467   MdnsMessage msg;
468   ASSERT_TRUE(ReadMdnsMessage(
469       &msg, kTwoAuthoritativeAnswersWithIPv4AndIPv6AddrWithNameCompression));
470 
471   std::map<std::string, rtc::IPAddress> resolution;
472   EXPECT_TRUE(GetResolution(&msg, &resolution));
473   rtc::IPAddress expected_addr_ipv4(
474       rtc::SocketAddress("192.168.0.1", 0).ipaddr());
475   rtc::IPAddress expected_addr_ipv6(
476       rtc::SocketAddress("fd12:3456:789a:1::1", 0).ipaddr());
477   EXPECT_THAT(resolution,
478               UnorderedElementsAre(Pair("www.webrtc.org.", expected_addr_ipv4),
479                                    Pair("webrtc.org.", expected_addr_ipv6)));
480 }
481 
TEST(MdnsMessageTest,ReadAnswerWithCorruptedPointerInNameCompressionShouldFail)482 TEST(MdnsMessageTest,
483      ReadAnswerWithCorruptedPointerInNameCompressionShouldFail) {
484   MdnsMessage msg;
485   EXPECT_FALSE(ReadMdnsMessage(&msg, kCorruptedAnswerWithNameCompression1));
486   EXPECT_FALSE(ReadMdnsMessage(&msg, kCorruptedAnswerWithNameCompression2));
487 }
488 
TEST(MdnsMessageTest,WriteSingleQuestionForIPv4Addr)489 TEST(MdnsMessageTest, WriteSingleQuestionForIPv4Addr) {
490   MdnsMessage msg;
491   msg.SetId(0x1234);
492   msg.SetQueryOrResponse(true);
493 
494   MdnsQuestion question;
495   question.SetName("webrtc.org.");
496   question.SetType(SectionEntryType::kA);
497   question.SetClass(SectionEntryClass::kIN);
498   question.SetUnicastResponse(true);
499   msg.AddQuestion(question);
500 
501   WriteMdnsMessageAndCompare(&msg,
502                              kSingleQuestionForIPv4AddrWithUnicastResponse);
503 }
504 
TEST(MdnsMessageTest,WriteTwoQuestionsForIPv4AndIPv6Addr)505 TEST(MdnsMessageTest, WriteTwoQuestionsForIPv4AndIPv6Addr) {
506   MdnsMessage msg;
507   msg.SetId(0x1234);
508   msg.SetQueryOrResponse(true);
509 
510   MdnsQuestion question1;
511   question1.SetName("webrtc4.org.");
512   question1.SetType(SectionEntryType::kA);
513   question1.SetClass(SectionEntryClass::kIN);
514   msg.AddQuestion(question1);
515 
516   MdnsQuestion question2;
517   question2.SetName("webrtc6.org.");
518   question2.SetType(SectionEntryType::kAAAA);
519   question2.SetClass(SectionEntryClass::kIN);
520   msg.AddQuestion(question2);
521 
522   WriteMdnsMessageAndCompare(
523       &msg, kTwoQuestionsForIPv4AndIPv6AddrWithMulticastResponse);
524 }
525 
TEST(MdnsMessageTest,WriteSingleAnswerToIPv4Addr)526 TEST(MdnsMessageTest, WriteSingleAnswerToIPv4Addr) {
527   MdnsMessage msg;
528   msg.SetId(0x1234);
529   msg.SetQueryOrResponse(false);
530   msg.SetAuthoritative(true);
531 
532   MdnsResourceRecord answer;
533   answer.SetName("webrtc.org.");
534   answer.SetType(SectionEntryType::kA);
535   answer.SetClass(SectionEntryClass::kIN);
536   EXPECT_TRUE(answer.SetIPAddressInRecordData(
537       rtc::SocketAddress("192.168.0.1", 0).ipaddr()));
538   answer.SetTtlSeconds(120);
539   msg.AddAnswerRecord(answer);
540 
541   WriteMdnsMessageAndCompare(&msg, kSingleAuthoritativeAnswerWithIPv4Addr);
542 }
543 
TEST(MdnsMessageTest,WriteTwoAnswersToIPv4AndIPv6Addr)544 TEST(MdnsMessageTest, WriteTwoAnswersToIPv4AndIPv6Addr) {
545   MdnsMessage msg;
546   msg.SetId(0x1234);
547   msg.SetQueryOrResponse(false);
548   msg.SetAuthoritative(true);
549 
550   MdnsResourceRecord answer1;
551   answer1.SetName("webrtc4.org.");
552   answer1.SetType(SectionEntryType::kA);
553   answer1.SetClass(SectionEntryClass::kIN);
554   answer1.SetIPAddressInRecordData(
555       rtc::SocketAddress("192.168.0.1", 0).ipaddr());
556   answer1.SetTtlSeconds(60);
557   msg.AddAnswerRecord(answer1);
558 
559   MdnsResourceRecord answer2;
560   answer2.SetName("webrtc6.org.");
561   answer2.SetType(SectionEntryType::kAAAA);
562   answer2.SetClass(SectionEntryClass::kIN);
563   answer2.SetIPAddressInRecordData(
564       rtc::SocketAddress("fd12:3456:789a:1::1", 0).ipaddr());
565   answer2.SetTtlSeconds(120);
566   msg.AddAnswerRecord(answer2);
567 
568   WriteMdnsMessageAndCompare(&msg, kTwoAuthoritativeAnswersWithIPv4AndIPv6Addr);
569 }
570 
571 }  // namespace webrtc
572