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

Unified Diff: net/tools/dns_fuzz_stub/dns_fuzz_stub.cc

Issue 10441027: Stub binary for fuzzing DNS resolver. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/net.gyp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..6e2eb3c874ebe2963fc017e2f0ce3a42a4ecfe79
--- /dev/null
+++ b/net/tools/dns_fuzz_stub/dns_fuzz_stub.cc
@@ -0,0 +1,109 @@
+// 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;
+}
+
+// TODO(ttuttle): This should read from the file, probably in JSON.
+bool ReadTestCase(const char* filename,
+ uint16_t* id, std::string* qname, uint16_t* qtype,
+ size_t* resp_len, char** resp_buf) {
+ static const unsigned 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 };
+
+ *id = 0x0000;
+ *qname = "a";
+ *qtype = 0x0001;
+
+ *resp_len = arraysize(resp);
szym 2012/05/31 16:46:11 This should actually be sizeof :) memcpy expects t
Deprecated (see juliatuttle) 2012/05/31 16:49:43 Gah, okay, got it.
+ *resp_buf = new char[arraysize(resp)];
+ 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;
+}
+
« no previous file with comments | « net/net.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698