 Chromium Code Reviews
 Chromium Code Reviews Issue 10441027:
  Stub binary for fuzzing DNS resolver.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src
    
  
    Issue 10441027:
  Stub binary for fuzzing DNS resolver.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src| 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 <stdio.h> | |
| 6 #include <stdlib.h> | |
| 7 #include <string.h> | |
| 8 | |
| 9 #include "base/time.h" | |
| 10 #include "net/base/address_list.h" | |
| 11 #include "net/base/dns_util.h" | |
| 12 #include "net/base/io_buffer.h" | |
| 13 #include "net/base/ip_endpoint.h" | |
| 14 #include "net/dns/dns_protocol.h" | |
| 15 #include "net/dns/dns_query.h" | |
| 16 #include "net/dns/dns_response.h" | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 int Usage(const char* program_name) { | |
| 21 fprintf(stderr, "Usage: %s test_case_filename\n", program_name); | |
| 22 return 1; | |
| 23 } | |
| 24 | |
| 25 int InvalidTestCase(const char* program_name) { | |
| 26 fprintf(stderr, "%s: Test case format invalid\n", program_name); | |
| 27 return 2; | |
| 28 } | |
| 29 | |
| 30 bool ReadTestCase(const char* filename, | |
| 
szym
2012/05/30 22:32:13
If you intend to land this CL as is, add TODO(ttut
 
Deprecated (see juliatuttle)
2012/05/31 16:30:15
Done.
 | |
| 31 uint16_t* id, std::string* qname, uint16_t* qtype, | |
| 32 size_t* resp_len, char** resp_buf) { | |
| 33 static const char resp[] = { | |
| 34 0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, | |
| 35 0x01, 0x61, 0x00, 0x00, 0x01, 0x00, 0x01, | |
| 36 0x01, 0x61, 0x00, 0x00, 0x01, 0x00, 0x01, | |
| 37 0x00, 0x00, 0x00, 0xff, 0x00, 0x04, 0x0a, 0x0a, 0x0a, 0x0a }; | |
| 
szym
2012/05/30 22:32:13
Use uint8 or unsigned char, so that 0xff and 0x81
 
Deprecated (see juliatuttle)
2012/05/31 16:30:15
Done.
 | |
| 38 | |
| 39 *id = 0x0000; | |
| 40 *qname = "a"; | |
| 41 *qtype = 0x0001; | |
| 42 | |
| 43 *resp_len = sizeof(resp); | |
| 44 *resp_buf = new char[sizeof(resp)]; | |
| 
szym
2012/05/30 22:32:13
arraysize(resp) is preferred and does not assume t
 
Deprecated (see juliatuttle)
2012/05/31 16:30:15
Done.
 | |
| 45 memcpy(*resp_buf, resp, sizeof(resp)); | |
| 46 | |
| 47 return true; | |
| 48 } | |
| 49 | |
| 50 } | |
| 51 | |
| 52 int main(int argc, char** argv) { | |
| 53 if (argc != 2) | |
| 54 return Usage(argv[0]); | |
| 55 | |
| 56 const char* filename = argv[1]; | |
| 57 | |
| 58 printf("Test case: %s\n", filename); | |
| 59 | |
| 60 uint16_t id; | |
| 61 std::string qname_dotted; | |
| 62 uint16_t qtype; | |
| 63 size_t resp_len; | |
| 64 char* resp_buf; | |
| 65 | |
| 66 if (!ReadTestCase(filename, | |
| 67 &id, &qname_dotted, &qtype, | |
| 68 &resp_len, &resp_buf)) { | |
| 69 return InvalidTestCase(argv[0]); | |
| 70 } | |
| 71 | |
| 72 printf("Query: id=%hu qname=%s qtype=%hu\n", id, qname_dotted.c_str(), qtype); | |
| 73 printf("Response: %d bytes\n", (int)resp_len); | |
| 74 | |
| 75 std::string qname; | |
| 76 if (!net::DNSDomainFromDot(qname_dotted, &qname)) { | |
| 77 printf("%s: DNSDomainFromDot(%s) failed.\n", argv[0], qname_dotted.c_str()); | |
| 78 return 3; | |
| 79 } | |
| 80 | |
| 81 net::DnsQuery query(id, qname, qtype); | |
| 82 net::DnsResponse response; | |
| 83 memcpy(response.io_buffer()->data(), resp_buf, resp_len); | |
| 84 | |
| 85 if (!response.InitParse(resp_len, query)) { | |
| 86 printf("InitParse failed.\n"); | |
| 87 return 0; | |
| 88 } | |
| 89 | |
| 90 net::AddressList address_list; | |
| 91 base::TimeDelta ttl; | |
| 92 net::DnsResponse::Result result = response.ParseToAddressList( | |
| 93 &address_list, &ttl); | |
| 94 if (result != net::DnsResponse::DNS_SUCCESS) { | |
| 95 printf("ParseToAddressList failed: %d\n", result); | |
| 96 return 0; | |
| 97 } | |
| 98 | |
| 99 printf("Address List:\n"); | |
| 100 for (unsigned int i = 0; i < address_list.size(); i++) { | |
| 101 std::string address_str = address_list[i].ToString(); | |
| 102 printf("\t%s\n", address_str.c_str()); | |
| 103 } | |
| 104 printf("TTL: %d seconds\n", static_cast<int>(ttl.InSeconds())); | |
| 105 | |
| 106 return 0; | |
| 107 } | |
| 108 | |
| OLD | NEW |