[email protected] | d391116 | 2011-07-18 15:12:46 | [diff] [blame^] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef NET_BASE_DNS_TEST_UTIL_H_ |
| 6 | #define NET_BASE_DNS_TEST_UTIL_H_ |
| 7 | #pragma once |
| 8 | |
| 9 | #include <deque> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "base/logging.h" |
| 13 | #include "net/base/dns_transaction.h" |
| 14 | #include "net/base/dns_util.h" |
| 15 | #include "net/base/ip_endpoint.h" |
| 16 | #include "net/base/net_util.h" |
| 17 | |
| 18 | namespace net { |
| 19 | |
| 20 | // DNS related classes make use of PRNG for various tasks. This class is |
| 21 | // used as a PRNG for unit testing those tasks. It takes a deque of |
| 22 | // integers |numbers| which should be returned by calls to GetNext. |
| 23 | class TestPrng { |
| 24 | public: |
| 25 | explicit TestPrng(const std::deque<int>& numbers); |
| 26 | ~TestPrng(); |
| 27 | |
| 28 | // Pops and returns the next number from |numbers_| deque. |
| 29 | int GetNext(int min, int max); |
| 30 | |
| 31 | private: |
| 32 | std::deque<int> numbers_; |
| 33 | |
| 34 | DISALLOW_COPY_AND_ASSIGN(TestPrng); |
| 35 | }; |
| 36 | |
| 37 | // A utility function for tests that given an array of IP literals, |
| 38 | // converts it to an IPAddressList. |
| 39 | bool ConvertStringsToIPAddressList( |
| 40 | const char* const ip_strings[], size_t size, IPAddressList* address_list); |
| 41 | |
| 42 | // A utility function for tests that creates an IPEndPoint whose IP is |
| 43 | // |ip_string| and whose port is |port| and stores it in |endpoint|. |
| 44 | bool CreateDnsAddress(const char* ip_string, uint16 port, IPEndPoint* endpoint); |
| 45 | |
| 46 | static const char kDnsIp[] = "192.168.1.1"; |
| 47 | static const uint16 kDnsPort = 53; |
| 48 | |
| 49 | //----------------------------------------------------------------------------- |
| 50 | // Query/response set for www.google.com, ID is fixed to 1. |
| 51 | |
| 52 | static const uint16 kT1Qtype = kDNS_A; |
| 53 | |
| 54 | static const char kT1DnsName[] = { |
| 55 | 0x03, 'w', 'w', 'w', |
| 56 | 0x06, 'g', 'o', 'o', 'g', 'l', 'e', |
| 57 | 0x03, 'c', 'o', 'm', |
| 58 | 0x00 |
| 59 | }; |
| 60 | |
| 61 | static const uint8 kT1QueryDatagram[] = { |
| 62 | // query for www.google.com, type A. |
| 63 | 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, |
| 64 | 0x00, 0x00, 0x00, 0x00, 0x03, 0x77, 0x77, 0x77, |
| 65 | 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03, |
| 66 | 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01 |
| 67 | }; |
| 68 | |
| 69 | static const uint8 kT1ResponseDatagram[] = { |
| 70 | // response contains one CNAME for www.l.google.com and the following |
| 71 | // IP addresses: 74.125.226.{179,180,176,177,178} |
| 72 | 0x00, 0x01, 0x81, 0x80, 0x00, 0x01, 0x00, 0x06, |
| 73 | 0x00, 0x00, 0x00, 0x00, 0x03, 0x77, 0x77, 0x77, |
| 74 | 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03, |
| 75 | 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, |
| 76 | 0xc0, 0x0c, 0x00, 0x05, 0x00, 0x01, 0x00, 0x01, |
| 77 | 0x4d, 0x13, 0x00, 0x08, 0x03, 0x77, 0x77, 0x77, |
| 78 | 0x01, 0x6c, 0xc0, 0x10, 0xc0, 0x2c, 0x00, 0x01, |
| 79 | 0x00, 0x01, 0x00, 0x00, 0x00, 0xe4, 0x00, 0x04, |
| 80 | 0x4a, 0x7d, 0xe2, 0xb3, 0xc0, 0x2c, 0x00, 0x01, |
| 81 | 0x00, 0x01, 0x00, 0x00, 0x00, 0xe4, 0x00, 0x04, |
| 82 | 0x4a, 0x7d, 0xe2, 0xb4, 0xc0, 0x2c, 0x00, 0x01, |
| 83 | 0x00, 0x01, 0x00, 0x00, 0x00, 0xe4, 0x00, 0x04, |
| 84 | 0x4a, 0x7d, 0xe2, 0xb0, 0xc0, 0x2c, 0x00, 0x01, |
| 85 | 0x00, 0x01, 0x00, 0x00, 0x00, 0xe4, 0x00, 0x04, |
| 86 | 0x4a, 0x7d, 0xe2, 0xb1, 0xc0, 0x2c, 0x00, 0x01, |
| 87 | 0x00, 0x01, 0x00, 0x00, 0x00, 0xe4, 0x00, 0x04, |
| 88 | 0x4a, 0x7d, 0xe2, 0xb2 |
| 89 | }; |
| 90 | |
| 91 | static const char* const kT1IpAddresses[] = { |
| 92 | "74.125.226.179", "74.125.226.180", "74.125.226.176", |
| 93 | "74.125.226.177", "74.125.226.178" |
| 94 | }; |
| 95 | |
| 96 | //----------------------------------------------------------------------------- |
| 97 | // Query/response set for codereview.chromium.org, ID is fixed to 2. |
| 98 | static const uint16 kT2Qtype = kDNS_A; |
| 99 | |
| 100 | static const char kT2DnsName[] = { |
| 101 | 0x12, 'c', 'o', 'd', 'e', 'r', 'e', 'v', 'i', 'e', 'w', |
| 102 | 0x10, 'c', 'h', 'r', 'o', 'm', 'i', 'u', 'm', |
| 103 | 0x03, 'o', 'r', 'g', |
| 104 | 0x00 |
| 105 | }; |
| 106 | |
| 107 | static const uint8 kT2QueryDatagram[] = { |
| 108 | // query for codereview.chromium.org, type A. |
| 109 | 0x00, 0x02, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, |
| 110 | 0x00, 0x00, 0x00, 0x00, 0x0a, 0x63, 0x6f, 0x64, |
| 111 | 0x65, 0x72, 0x65, 0x76, 0x69, 0x65, 0x77, 0x08, |
| 112 | 0x63, 0x68, 0x72, 0x6f, 0x6d, 0x69, 0x75, 0x6d, |
| 113 | 0x03, 0x6f, 0x72, 0x67, 0x00, 0x00, 0x01, 0x00, |
| 114 | 0x01 |
| 115 | }; |
| 116 | |
| 117 | static const uint8 kT2ResponseDatagram[] = { |
| 118 | // response contains one CNAME for ghs.l.google.com and the following |
| 119 | // IP address: 64.233.169.121 |
| 120 | 0x00, 0x02, 0x81, 0x80, 0x00, 0x01, 0x00, 0x02, |
| 121 | 0x00, 0x00, 0x00, 0x00, 0x0a, 0x63, 0x6f, 0x64, |
| 122 | 0x65, 0x72, 0x65, 0x76, 0x69, 0x65, 0x77, 0x08, |
| 123 | 0x63, 0x68, 0x72, 0x6f, 0x6d, 0x69, 0x75, 0x6d, |
| 124 | 0x03, 0x6f, 0x72, 0x67, 0x00, 0x00, 0x01, 0x00, |
| 125 | 0x01, 0xc0, 0x0c, 0x00, 0x05, 0x00, 0x01, 0x00, |
| 126 | 0x01, 0x41, 0x75, 0x00, 0x12, 0x03, 0x67, 0x68, |
| 127 | 0x73, 0x01, 0x6c, 0x06, 0x67, 0x6f, 0x6f, 0x67, |
| 128 | 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0xc0, |
| 129 | 0x35, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, |
| 130 | 0x0b, 0x00, 0x04, 0x40, 0xe9, 0xa9, 0x79 |
| 131 | }; |
| 132 | |
| 133 | //----------------------------------------------------------------------------- |
| 134 | |
| 135 | } // namespace net |
| 136 | |
| 137 | #endif // NET_BASE_DNS_TEST_UTIL_H_ |