 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| Index: net/tools/dns_fuzz_stub/dns_fuzz_stub.cc | 
| diff --git a/net/tools/dns_fuzz_stub/dns_fuzz_stub.cc b/net/tools/dns_fuzz_stub/dns_fuzz_stub.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..a3de93ca906176d4163cb40827dc91527c399a5d | 
| --- /dev/null | 
| +++ b/net/tools/dns_fuzz_stub/dns_fuzz_stub.cc | 
| @@ -0,0 +1,108 @@ | 
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +#include <stdio.h> | 
| +#include <stdlib.h> | 
| +#include <string.h> | 
| + | 
| +#include "base/time.h" | 
| +#include "net/base/address_list.h" | 
| +#include "net/base/dns_util.h" | 
| +#include "net/base/io_buffer.h" | 
| +#include "net/base/ip_endpoint.h" | 
| +#include "net/dns/dns_protocol.h" | 
| +#include "net/dns/dns_query.h" | 
| +#include "net/dns/dns_response.h" | 
| + | 
| +namespace { | 
| + | 
| +int Usage(const char* program_name) { | 
| + fprintf(stderr, "Usage: %s test_case_filename\n", program_name); | 
| + return 1; | 
| +} | 
| + | 
| +int InvalidTestCase(const char* program_name) { | 
| + fprintf(stderr, "%s: Test case format invalid\n", program_name); | 
| + return 2; | 
| +} | 
| + | 
| +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.
 | 
| + uint16_t* id, std::string* qname, uint16_t* qtype, | 
| + size_t* resp_len, char** resp_buf) { | 
| + static const char resp[] = { | 
| + 0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, | 
| + 0x01, 0x61, 0x00, 0x00, 0x01, 0x00, 0x01, | 
| + 0x01, 0x61, 0x00, 0x00, 0x01, 0x00, 0x01, | 
| + 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.
 | 
| + | 
| + *id = 0x0000; | 
| + *qname = "a"; | 
| + *qtype = 0x0001; | 
| + | 
| + *resp_len = sizeof(resp); | 
| + *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.
 | 
| + memcpy(*resp_buf, resp, sizeof(resp)); | 
| + | 
| + return true; | 
| +} | 
| + | 
| +} | 
| + | 
| +int main(int argc, char** argv) { | 
| + if (argc != 2) | 
| + return Usage(argv[0]); | 
| + | 
| + const char* filename = argv[1]; | 
| + | 
| + printf("Test case: %s\n", filename); | 
| + | 
| + uint16_t id; | 
| + std::string qname_dotted; | 
| + uint16_t qtype; | 
| + size_t resp_len; | 
| + char* resp_buf; | 
| + | 
| + if (!ReadTestCase(filename, | 
| + &id, &qname_dotted, &qtype, | 
| + &resp_len, &resp_buf)) { | 
| + return InvalidTestCase(argv[0]); | 
| + } | 
| + | 
| + printf("Query: id=%hu qname=%s qtype=%hu\n", id, qname_dotted.c_str(), qtype); | 
| + printf("Response: %d bytes\n", (int)resp_len); | 
| + | 
| + std::string qname; | 
| + if (!net::DNSDomainFromDot(qname_dotted, &qname)) { | 
| + printf("%s: DNSDomainFromDot(%s) failed.\n", argv[0], qname_dotted.c_str()); | 
| + return 3; | 
| + } | 
| + | 
| + net::DnsQuery query(id, qname, qtype); | 
| + net::DnsResponse response; | 
| + memcpy(response.io_buffer()->data(), resp_buf, resp_len); | 
| + | 
| + if (!response.InitParse(resp_len, query)) { | 
| + printf("InitParse failed.\n"); | 
| + return 0; | 
| + } | 
| + | 
| + net::AddressList address_list; | 
| + base::TimeDelta ttl; | 
| + net::DnsResponse::Result result = response.ParseToAddressList( | 
| + &address_list, &ttl); | 
| + if (result != net::DnsResponse::DNS_SUCCESS) { | 
| + printf("ParseToAddressList failed: %d\n", result); | 
| + return 0; | 
| + } | 
| + | 
| + printf("Address List:\n"); | 
| + for (unsigned int i = 0; i < address_list.size(); i++) { | 
| + std::string address_str = address_list[i].ToString(); | 
| + printf("\t%s\n", address_str.c_str()); | 
| + } | 
| + printf("TTL: %d seconds\n", static_cast<int>(ttl.InSeconds())); | 
| + | 
| + return 0; | 
| +} | 
| + |