| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <algorithm> |
| 6 #include <string> |
| 7 #include <vector> |
| 8 |
| 9 #include "base/basictypes.h" |
| 10 #include "base/time.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "net/base/address_list.h" |
| 13 #include "net/base/dns_util.h" |
| 14 #include "net/base/io_buffer.h" |
| 15 #include "net/base/ip_endpoint.h" |
| 16 #include "net/dns/dns_protocol.h" |
| 17 #include "net/dns/dns_query.h" |
| 18 #include "net/dns/dns_response.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 // TODO(ttuttle): This should read from the file, probably in JSON. |
| 23 bool ReadTestCase(const char* filename, |
| 24 uint16* id, std::string* qname, uint16* qtype, |
| 25 std::vector<char>* resp_buf) { |
| 26 static const unsigned char resp_bytes[] = { |
| 27 0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 28 0x01, 0x61, 0x00, 0x00, 0x01, 0x00, 0x01, |
| 29 0x01, 0x61, 0x00, 0x00, 0x01, 0x00, 0x01, |
| 30 0x00, 0x00, 0x00, 0xff, 0x00, 0x04, 0x0a, 0x0a, 0x0a, 0x0a }; |
| 31 |
| 32 *id = 0x0000; |
| 33 *qname = "a"; |
| 34 *qtype = 0x0001; |
| 35 resp_buf->assign(resp_bytes, resp_bytes + arraysize(resp_bytes)); |
| 36 |
| 37 return true; |
| 38 } |
| 39 |
| 40 } |
| 41 |
| 42 int main(int argc, char** argv) { |
| 43 if (argc != 2) { |
| 44 LOG(ERROR) << "Usage: " << argv[0] << " test_case_filename"; |
| 45 return 1; |
| 46 } |
| 47 |
| 48 const char* filename = argv[1]; |
| 49 |
| 50 LOG(INFO) << "Test case: " << filename; |
| 51 |
| 52 uint16 id; |
| 53 std::string qname_dotted; |
| 54 uint16 qtype; |
| 55 std::vector<char> resp_buf; |
| 56 |
| 57 if (!ReadTestCase(filename, &id, &qname_dotted, &qtype, &resp_buf)) { |
| 58 LOG(ERROR) << "Test case format invalid"; |
| 59 return 2; |
| 60 } |
| 61 |
| 62 LOG(INFO) << "Query: id=" << id |
| 63 << " qname=" << qname_dotted |
| 64 << " qtype=" << qtype; |
| 65 LOG(INFO) << "Response: " << resp_buf.size() << " bytes"; |
| 66 |
| 67 std::string qname; |
| 68 if (!net::DNSDomainFromDot(qname_dotted, &qname)) { |
| 69 LOG(ERROR) << "DNSDomainFromDot(" << qname_dotted << ") failed."; |
| 70 return 3; |
| 71 } |
| 72 |
| 73 net::DnsQuery query(id, qname, qtype); |
| 74 net::DnsResponse response; |
| 75 std::copy(resp_buf.begin(), resp_buf.end(), response.io_buffer()->data()); |
| 76 |
| 77 if (!response.InitParse(resp_buf.size(), query)) { |
| 78 LOG(INFO) << "InitParse failed."; |
| 79 return 0; |
| 80 } |
| 81 |
| 82 net::AddressList address_list; |
| 83 base::TimeDelta ttl; |
| 84 net::DnsResponse::Result result = response.ParseToAddressList( |
| 85 &address_list, &ttl); |
| 86 if (result != net::DnsResponse::DNS_SUCCESS) { |
| 87 LOG(INFO) << "ParseToAddressList failed: " << result; |
| 88 return 0; |
| 89 } |
| 90 |
| 91 LOG(INFO) << "Address List:"; |
| 92 for (unsigned int i = 0; i < address_list.size(); i++) { |
| 93 LOG(INFO) << "\t" << address_list[i].ToString(); |
| 94 } |
| 95 LOG(INFO) << "TTL: " << ttl.InSeconds() << " seconds"; |
| 96 |
| 97 return 0; |
| 98 } |
| 99 |
| OLD | NEW |