Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(563)

Side by Side Diff: net/dns/dns_response_unittest.cc

Issue 14049018: Add simple non-response-based question parsing for mDNS passive listening (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« net/dns/dns_response.cc ('K') | « net/dns/dns_response.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/dns/dns_response.h" 5 #include "net/dns/dns_response.h"
6 6
7 #include "base/time.h" 7 #include "base/time.h"
8 #include "net/base/address_list.h" 8 #include "net/base/address_list.h"
9 #include "net/base/io_buffer.h" 9 #include "net/base/io_buffer.h"
10 #include "net/base/net_util.h" 10 #include "net/base/net_util.h"
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 238
239 DnsResourceRecord record; 239 DnsResourceRecord record;
240 DnsRecordParser parser = resp.Parser(); 240 DnsRecordParser parser = resp.Parser();
241 EXPECT_TRUE(parser.ReadRecord(&record)); 241 EXPECT_TRUE(parser.ReadRecord(&record));
242 EXPECT_FALSE(parser.AtEnd()); 242 EXPECT_FALSE(parser.AtEnd());
243 EXPECT_TRUE(parser.ReadRecord(&record)); 243 EXPECT_TRUE(parser.ReadRecord(&record));
244 EXPECT_TRUE(parser.AtEnd()); 244 EXPECT_TRUE(parser.AtEnd());
245 EXPECT_FALSE(parser.ReadRecord(&record)); 245 EXPECT_FALSE(parser.ReadRecord(&record));
246 } 246 }
247 247
248 // TODO(noamsml): Simplify this test
249 TEST(DnsResponseTest, GetQuestionSectionSize) {
250 // Easiest way to comput size of QNAME data
251 const char qname_data[] = "\x0A""codereview""\x08""chromium""\x03""org";
252 const base::StringPiece qname(qname_data, sizeof(qname_data));
253 const size_t qsec_length = qname.size() + 4;
254
255 const uint8 response_data[] = {
256 // Header
257 0xca, 0xfe, // ID
258 0x81, 0x80, // Standard query response, RA, no error
259 0x00, 0x01, // 1 question
260 0x00, 0x02, // 2 RRs (answers)
261 0x00, 0x00, // 0 authority RRs
262 0x00, 0x00, // 0 additional RRs
263
264 // Question
265 // This part is echoed back from the respective query.
266 0x0a, 'c', 'o', 'd', 'e', 'r', 'e', 'v', 'i', 'e', 'w',
267 0x08, 'c', 'h', 'r', 'o', 'm', 'i', 'u', 'm',
268 0x03, 'o', 'r', 'g',
269 0x00,
270 0x00, 0x01, // TYPE is A.
271 0x00, 0x01, // CLASS is IN.
272
273 // Answer 1
274 0xc0, 0x0c, // NAME is a pointer to name in Question section.
275 0x00, 0x05, // TYPE is CNAME.
276 0x00, 0x01, // CLASS is IN.
277 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds.
278 0x24, 0x74,
279 0x00, 0x12, // RDLENGTH is 18 bytes.
280 // ghs.l.google.com in DNS format.
281 0x03, 'g', 'h', 's',
282 0x01, 'l',
283 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
284 0x03, 'c', 'o', 'm',
285 0x00,
286
287 // Answer 2
288 0xc0, 0x35, // NAME is a pointer to name in Answer 1.
289 0x00, 0x01, // TYPE is A.
290 0x00, 0x01, // CLASS is IN.
291 0x00, 0x00, // TTL (4 bytes) is 53 seconds.
292 0x00, 0x35,
293 0x00, 0x04, // RDLENGTH is 4 bytes.
294 0x4a, 0x7d, // RDATA is the IP: 74.125.95.121
295 0x5f, 0x79,
296 };
297
298 DnsResponse resp;
299 memcpy(resp.io_buffer()->data(), response_data, sizeof(response_data));
300
301 EXPECT_EQ(qsec_length, resp.GetQuestionSectionSize());
302 }
303
304 // TODO(noamsml): Simplify this test
305 TEST(DnsResponseTest, GetQuestionSectionSizeNoQuestion) {
306 const uint8 response_data[] = {
307 // Header
308 0xca, 0xfe, // ID
309 0x81, 0x80, // Standard query response, RA, no error
310 0x00, 0x00, // 1 question
311 0x00, 0x02, // 2 RRs (answers)
312 0x00, 0x00, // 0 authority RRs
313 0x00, 0x00, // 0 additional RRs
314
315 // Answer 1
316 0xc0, 0x0c, // NAME is a pointer to name in Question section.
317 0x00, 0x05, // TYPE is CNAME.
318 0x00, 0x01, // CLASS is IN.
319 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds.
320 0x24, 0x74,
321 0x00, 0x12, // RDLENGTH is 18 bytes.
322 // ghs.l.google.com in DNS format.
323 0x03, 'g', 'h', 's',
324 0x01, 'l',
325 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
326 0x03, 'c', 'o', 'm',
327 0x00,
328
329 // Answer 2
330 0xc0, 0x35, // NAME is a pointer to name in Answer 1.
331 0x00, 0x01, // TYPE is A.
332 0x00, 0x01, // CLASS is IN.
333 0x00, 0x00, // TTL (4 bytes) is 53 seconds.
334 0x00, 0x35,
335 0x00, 0x04, // RDLENGTH is 4 bytes.
336 0x4a, 0x7d, // RDATA is the IP: 74.125.95.121
337 0x5f, 0x79,
338 };
339
340 DnsResponse resp;
341 memcpy(resp.io_buffer()->data(), response_data, sizeof(response_data));
342
343 EXPECT_EQ((size_t)0, resp.GetQuestionSectionSize());
344 }
345
346 // TODO(noamsml): Simplify this test
347 TEST(DnsResponseTest, GetQuestionSectionSizeTwoQuestions) {
348 // Easiest way to compute size of QNAME data
349 const char qname_data[] = "\x0A""codereview""\x08""chromium""\x03""org";
350 const base::StringPiece qname(qname_data, sizeof(qname_data));
351 const char qname2_data[] = "\x0B""codereview2""\xc0\x18";
352 const base::StringPiece qname2(qname2_data, sizeof(qname2_data));
353 const size_t qsec_length = qname.size() + qname2.size() + 8;
354
355 const uint8 response_data[] = {
356 // Header
357 0xca, 0xfe, // ID
358 0x81, 0x80, // Standard query response, RA, no error
359 0x00, 0x02, // 1 question
360 0x00, 0x02, // 2 RRs (answers)
361 0x00, 0x00, // 0 authority RRs
362 0x00, 0x00, // 0 additional RRs
363
364 // Question 1
365 0x0a, 'c', 'o', 'd', 'e', 'r', 'e', 'v', 'i', 'e', 'w',
366 0x08, 'c', 'h', 'r', 'o', 'm', 'i', 'u', 'm',
367 0x03, 'o', 'r', 'g',
368 0x00,
369 0x00, 0x01, // TYPE is A.
370 0x00, 0x01, // CLASS is IN.
371
372 // Question 2
373 0x0b, 'c', 'o', 'd', 'e', 'r', 'e', 'v', 'i', 'e', 'w', '2',
374 0xc0, 0x18, // pointer to "chromium.org"
375 0x00,
376 0x00, 0x01, // TYPE is A.
377 0x00, 0x01, // CLASS is IN.
378
379 // Answer 1
380 0xc0, 0x0c, // NAME is a pointer to name in Question section.
381 0x00, 0x05, // TYPE is CNAME.
382 0x00, 0x01, // CLASS is IN.
383 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds.
384 0x24, 0x74,
385 0x00, 0x12, // RDLENGTH is 18 bytes.
386 // ghs.l.google.com in DNS format.
387 0x03, 'g', 'h', 's',
388 0x01, 'l',
389 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
390 0x03, 'c', 'o', 'm',
391 0x00,
392
393 // Answer 2
394 0xc0, 0x35, // NAME is a pointer to name in Answer 1.
395 0x00, 0x01, // TYPE is A.
396 0x00, 0x01, // CLASS is IN.
397 0x00, 0x00, // TTL (4 bytes) is 53 seconds.
398 0x00, 0x35,
399 0x00, 0x04, // RDLENGTH is 4 bytes.
400 0x4a, 0x7d, // RDATA is the IP: 74.125.95.121
401 0x5f, 0x79,
402 };
403
404 DnsResponse resp;
405 memcpy(resp.io_buffer()->data(), response_data, sizeof(response_data));
406
407 EXPECT_EQ(qsec_length, resp.GetQuestionSectionSize());
408 }
409
410 // TODO(noamsml): simplify test
411 TEST(DnsResponseTest, InitParseNoQuery) {
412 // This includes \0 at the end.
413 const char qname_data[] = "\x0A""codereview""\x08""chromium""\x03""org";
414 const base::StringPiece qname(qname_data, sizeof(qname_data));
415 // Compilers want to copy when binding temporary to const &, so must use heap.
416 scoped_ptr<DnsQuery> query(new DnsQuery(0xcafe, qname, dns_protocol::kTypeA));
417
418 const uint8 response_data[] = {
419 // Header
420 0xca, 0xfe, // ID
421 0x81, 0x80, // Standard query response, RA, no error
422 0x00, 0x01, // 1 question
423 0x00, 0x02, // 2 RRs (answers)
424 0x00, 0x00, // 0 authority RRs
425 0x00, 0x00, // 0 additional RRs
426
427 // Question
428 // This part is echoed back from the respective query.
429 0x0a, 'c', 'o', 'd', 'e', 'r', 'e', 'v', 'i', 'e', 'w',
430 0x08, 'c', 'h', 'r', 'o', 'm', 'i', 'u', 'm',
431 0x03, 'o', 'r', 'g',
432 0x00,
433 0x00, 0x01, // TYPE is A.
434 0x00, 0x01, // CLASS is IN.
435
436 // Answer 1
437 0xc0, 0x0c, // NAME is a pointer to name in Question section.
438 0x00, 0x05, // TYPE is CNAME.
439 0x00, 0x01, // CLASS is IN.
440 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds.
441 0x24, 0x74,
442 0x00, 0x12, // RDLENGTH is 18 bytes.
443 // ghs.l.google.com in DNS format.
444 0x03, 'g', 'h', 's',
445 0x01, 'l',
446 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
447 0x03, 'c', 'o', 'm',
448 0x00,
449
450 // Answer 2
451 0xc0, 0x35, // NAME is a pointer to name in Answer 1.
452 0x00, 0x01, // TYPE is A.
453 0x00, 0x01, // CLASS is IN.
454 0x00, 0x00, // TTL (4 bytes) is 53 seconds.
455 0x00, 0x35,
456 0x00, 0x04, // RDLENGTH is 4 bytes.
457 0x4a, 0x7d, // RDATA is the IP: 74.125.95.121
458 0x5f, 0x79,
459 };
460
461 DnsResponse resp;
462 memcpy(resp.io_buffer()->data(), response_data, sizeof(response_data));
463
464 // Accept matching question.
465 EXPECT_TRUE(resp.InitParseNoQuery(sizeof(response_data)));
466 EXPECT_TRUE(resp.IsValid());
467
468 // Check header access.
469 EXPECT_EQ(0x8180, resp.flags());
470 EXPECT_EQ(0x0, resp.rcode());
471 EXPECT_EQ(2u, resp.answer_count());
472
473 // Check question access.
474 EXPECT_EQ(query->qname(), resp.qname());
475 EXPECT_EQ(query->qtype(), resp.qtype());
476 EXPECT_EQ("codereview.chromium.org", resp.GetDottedName());
477
478 DnsResourceRecord record;
479 DnsRecordParser parser = resp.Parser();
480 EXPECT_TRUE(parser.ReadRecord(&record));
481 EXPECT_FALSE(parser.AtEnd());
482 EXPECT_TRUE(parser.ReadRecord(&record));
483 EXPECT_TRUE(parser.AtEnd());
484 EXPECT_FALSE(parser.ReadRecord(&record));
485 }
486
487
248 void VerifyAddressList(const std::vector<const char*>& ip_addresses, 488 void VerifyAddressList(const std::vector<const char*>& ip_addresses,
249 const AddressList& addrlist) { 489 const AddressList& addrlist) {
250 ASSERT_EQ(ip_addresses.size(), addrlist.size()); 490 ASSERT_EQ(ip_addresses.size(), addrlist.size());
251 491
252 for (size_t i = 0; i < addrlist.size(); ++i) { 492 for (size_t i = 0; i < addrlist.size(); ++i) {
253 EXPECT_EQ(ip_addresses[i], addrlist[i].ToStringWithoutPort()); 493 EXPECT_EQ(ip_addresses[i], addrlist[i].ToStringWithoutPort());
254 } 494 }
255 } 495 }
256 496
257 TEST(DnsResponseTest, ParseToAddressList) { 497 TEST(DnsResponseTest, ParseToAddressList) {
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 AddressList addr_list; 665 AddressList addr_list;
426 base::TimeDelta ttl; 666 base::TimeDelta ttl;
427 EXPECT_EQ(t.expected_result, 667 EXPECT_EQ(t.expected_result,
428 response.ParseToAddressList(&addr_list, &ttl)); 668 response.ParseToAddressList(&addr_list, &ttl));
429 } 669 }
430 } 670 }
431 671
432 } // namespace 672 } // namespace
433 673
434 } // namespace net 674 } // namespace net
OLDNEW
« net/dns/dns_response.cc ('K') | « net/dns/dns_response.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698