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

Side by Side Diff: net/tools/dns_fuzz_stub/dns_fuzz_stub.cc

Issue 10527004: Make dns_fuzz_stub read and parse JSON test case (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 6 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <algorithm> 5 #include <algorithm>
6 #include <string> 6 #include <string>
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/file_path.h"
11 #include "base/file_util.h"
10 #include "base/time.h" 12 #include "base/time.h"
13 #include "base/values.h"
14 #include "base/json/json_reader.h"
11 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
szym 2012/06/04 20:26:24 fix ordering
Deprecated (see juliatuttle) 2012/06/05 20:18:10 Done.
12 #include "net/base/address_list.h" 16 #include "net/base/address_list.h"
13 #include "net/base/dns_util.h" 17 #include "net/base/dns_util.h"
14 #include "net/base/io_buffer.h" 18 #include "net/base/io_buffer.h"
15 #include "net/base/ip_endpoint.h" 19 #include "net/base/ip_endpoint.h"
16 #include "net/dns/dns_protocol.h" 20 #include "net/dns/dns_protocol.h"
17 #include "net/dns/dns_query.h" 21 #include "net/dns/dns_query.h"
18 #include "net/dns/dns_response.h" 22 #include "net/dns/dns_response.h"
19 23
20 namespace { 24 namespace {
21 25
22 // TODO(ttuttle): This should read from the file, probably in JSON. 26 bool FitsUint8(int num) {
27 return (num >= 0) && (num <= kuint8max);
28 }
29
30 bool FitsUint16(int num) {
31 return (num >= 0) && (num <= kuint16max);
32 }
33
23 bool ReadTestCase(const char* filename, 34 bool ReadTestCase(const char* filename,
24 uint16* id, std::string* qname, uint16* qtype, 35 uint16* id, std::string* qname, uint16* qtype,
25 std::vector<char>* resp_buf) { 36 std::vector<char>* resp_buf) {
26 static const unsigned char resp_bytes[] = { 37 FilePath filepath = FilePath::FromUTF8Unsafe(filename);
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 38
32 *id = 0x0000; 39 std::string json;
33 *qname = "a"; 40 if (!file_util::ReadFileToString(filepath, &json)) {
34 *qtype = 0x0001; 41 LOG(ERROR) << "Couldn't read file " << filename << ".";
35 resp_buf->assign(resp_bytes, resp_bytes + arraysize(resp_bytes)); 42 return false;
43 }
44
45 Value* value = base::JSONReader::Read(json);
cbentzel 2012/06/04 23:49:23 Use a scoped_ptr here for the returned value. Opt
Deprecated (see juliatuttle) 2012/06/05 20:18:10 Done.
46 if (!value) {
47 LOG(ERROR) << "Couldn't parse JSON in " << filename << ".";
48 return false;
49 }
50
51 // root of JSON should always be a dictionary
52 DictionaryValue* dict;
53 CHECK(value->GetAsDictionary(&dict));
cbentzel 2012/06/04 23:49:23 Is this true? What if the file just consisted of a
Deprecated (see juliatuttle) 2012/06/05 20:18:10 It's actually not; the spec requires that it be a
54
55 int id_int;
56 if (!dict->GetInteger("id", &id_int) || !FitsUint16(id_int)) {
cbentzel 2012/06/04 23:49:23 Although boolean short-circuiting should make this
Deprecated (see juliatuttle) 2012/06/05 20:18:10 Done.
57 LOG(ERROR) << "No id, not an integer, or not in range.";
58 return false;
59 }
60 *id = static_cast<uint16>(id_int);
61
62 if (!dict->GetStringASCII("qname", qname)) {
63 LOG(ERROR) << "No qname or not a string.";
64 return false;
65 }
66
67 int qtype_int;
68 if (!dict->GetInteger("qtype", &qtype_int) || !FitsUint16(qtype_int)) {
69 LOG(ERROR) << "No qtype, not an integer, or not in range.";
70 return false;
71 }
72 *qtype = static_cast<uint16>(qtype_int);
73
74 ListValue* resp_list;
75 if (!dict->GetList("response", &resp_list)) {
76 LOG(ERROR) << "No response, or not a list.";
77 return false;
78 }
79
80 size_t resp_size = resp_list->GetSize();
81 resp_buf->clear();
82 resp_buf->reserve(resp_size);
83 for (size_t i = 0; i < resp_size; i++) {
84 int byte_int;
85 if ((!resp_list->GetInteger(i, &byte_int)) || !FitsUint8(byte_int)) {
86 LOG(ERROR) << "response[" << i << "] not an integer or not in range.";
87 return false;
88 }
89 resp_buf->push_back(static_cast<char>(byte_int));
szym 2012/06/04 20:26:24 The behavior of this cast is not defined by the st
szym 2012/06/04 20:32:17 Sorry, that won't work directly, but you get my po
Deprecated (see juliatuttle) 2012/06/05 20:18:10 We decided to stick with this, since it works and
90 }
91 DCHECK(resp_buf->size() == resp_size);
36 92
37 return true; 93 return true;
38 } 94 }
39 95
40 } 96 }
41 97
42 int main(int argc, char** argv) { 98 int main(int argc, char** argv) {
43 if (argc != 2) { 99 if (argc != 2) {
44 LOG(ERROR) << "Usage: " << argv[0] << " test_case_filename"; 100 LOG(ERROR) << "Usage: " << argv[0] << " test_case_filename";
45 return 1; 101 return 1;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 146
91 LOG(INFO) << "Address List:"; 147 LOG(INFO) << "Address List:";
92 for (unsigned int i = 0; i < address_list.size(); i++) { 148 for (unsigned int i = 0; i < address_list.size(); i++) {
93 LOG(INFO) << "\t" << address_list[i].ToString(); 149 LOG(INFO) << "\t" << address_list[i].ToString();
94 } 150 }
95 LOG(INFO) << "TTL: " << ttl.InSeconds() << " seconds"; 151 LOG(INFO) << "TTL: " << ttl.InSeconds() << " seconds";
96 152
97 return 0; 153 return 0;
98 } 154 }
99 155
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698