OLD | NEW |
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 <sstream> | 6 #include <sstream> |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "net/base/address_list.h" | 17 #include "net/base/address_list.h" |
18 #include "net/base/dns_util.h" | 18 #include "net/base/dns_util.h" |
19 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
20 #include "net/base/ip_endpoint.h" | 20 #include "net/base/ip_endpoint.h" |
21 #include "net/dns/dns_protocol.h" | 21 #include "net/dns/dns_protocol.h" |
22 #include "net/dns/dns_query.h" | 22 #include "net/dns/dns_query.h" |
23 #include "net/dns/dns_response.h" | 23 #include "net/dns/dns_response.h" |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 void CrashDoubleFree(void) { | 27 void CrashDoubleFree(void) { |
28 // Cause ASAN to detect a double-free | 28 // Cause ASAN to detect a double-free |
29 void *p = malloc(1); | 29 void* p = malloc(1); |
30 LOG(INFO) << "Allocated p=" << p << ". Double-freeing..."; | 30 LOG(INFO) << "Allocated p=" << p << ". Double-freeing..."; |
31 free(p); | 31 free(p); |
32 free(p); | 32 free(p); |
33 } | 33 } |
34 | 34 |
35 void CrashNullPointerDereference(void) { | 35 void CrashNullPointerDereference(void) { |
36 // Cause the program to segfault with a NULL pointer dereference | 36 // Cause the program to segfault with a NULL pointer dereference |
37 int *p = NULL; | 37 int* p = NULL; |
38 *p = 0; | 38 *p = 0; |
39 } | 39 } |
40 | 40 |
41 bool FitsUint8(int num) { | 41 bool FitsUint8(int num) { |
42 return (num >= 0) && (num <= kuint8max); | 42 return (num >= 0) && (num <= kuint8max); |
43 } | 43 } |
44 | 44 |
45 bool FitsUint16(int num) { | 45 bool FitsUint16(int num) { |
46 return (num >= 0) && (num <= kuint16max); | 46 return (num >= 0) && (num <= kuint16max); |
47 } | 47 } |
48 | 48 |
49 bool ReadTestCase(const char* filename, | 49 bool ReadTestCase(const char* filename, |
50 uint16* id, std::string* qname, uint16* qtype, | 50 uint16* id, |
| 51 std::string* qname, |
| 52 uint16* qtype, |
51 std::vector<char>* resp_buf, | 53 std::vector<char>* resp_buf, |
52 bool* crash_test) { | 54 bool* crash_test) { |
53 base::FilePath filepath = base::FilePath::FromUTF8Unsafe(filename); | 55 base::FilePath filepath = base::FilePath::FromUTF8Unsafe(filename); |
54 | 56 |
55 std::string json; | 57 std::string json; |
56 if (!base::ReadFileToString(filepath, &json)) { | 58 if (!base::ReadFileToString(filepath, &json)) { |
57 LOG(ERROR) << filename << ": couldn't read file."; | 59 LOG(ERROR) << filename << ": couldn't read file."; |
58 return false; | 60 return false; |
59 } | 61 } |
60 | 62 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 DCHECK(resp_buf->size() == resp_size); | 129 DCHECK(resp_buf->size() == resp_size); |
128 | 130 |
129 LOG(INFO) << "Query: id=" << id_int << ", " | 131 LOG(INFO) << "Query: id=" << id_int << ", " |
130 << "qname=" << *qname << ", " | 132 << "qname=" << *qname << ", " |
131 << "qtype=" << qtype_int << ", " | 133 << "qtype=" << qtype_int << ", " |
132 << "resp_size=" << resp_size; | 134 << "resp_size=" << resp_size; |
133 | 135 |
134 return true; | 136 return true; |
135 } | 137 } |
136 | 138 |
137 void RunTestCase(uint16 id, std::string& qname, uint16 qtype, | 139 void RunTestCase(uint16 id, |
| 140 std::string& qname, |
| 141 uint16 qtype, |
138 std::vector<char>& resp_buf) { | 142 std::vector<char>& resp_buf) { |
139 net::DnsQuery query(id, qname, qtype); | 143 net::DnsQuery query(id, qname, qtype); |
140 net::DnsResponse response; | 144 net::DnsResponse response; |
141 std::copy(resp_buf.begin(), resp_buf.end(), response.io_buffer()->data()); | 145 std::copy(resp_buf.begin(), resp_buf.end(), response.io_buffer()->data()); |
142 | 146 |
143 if (!response.InitParse(resp_buf.size(), query)) { | 147 if (!response.InitParse(resp_buf.size(), query)) { |
144 LOG(INFO) << "InitParse failed."; | 148 LOG(INFO) << "InitParse failed."; |
145 return; | 149 return; |
146 } | 150 } |
147 | 151 |
148 net::AddressList address_list; | 152 net::AddressList address_list; |
149 base::TimeDelta ttl; | 153 base::TimeDelta ttl; |
150 net::DnsResponse::Result result = response.ParseToAddressList( | 154 net::DnsResponse::Result result = |
151 &address_list, &ttl); | 155 response.ParseToAddressList(&address_list, &ttl); |
152 if (result != net::DnsResponse::DNS_PARSE_OK) { | 156 if (result != net::DnsResponse::DNS_PARSE_OK) { |
153 LOG(INFO) << "ParseToAddressList failed: " << result; | 157 LOG(INFO) << "ParseToAddressList failed: " << result; |
154 return; | 158 return; |
155 } | 159 } |
156 | 160 |
157 // Print the response in one compact line. | 161 // Print the response in one compact line. |
158 std::stringstream result_line; | 162 std::stringstream result_line; |
159 result_line << "Response: address_list={ "; | 163 result_line << "Response: address_list={ "; |
160 for (unsigned int i = 0; i < address_list.size(); i++) | 164 for (unsigned int i = 0; i < address_list.size(); i++) |
161 result_line << address_list[i].ToString() << " "; | 165 result_line << address_list[i].ToString() << " "; |
(...skipping 27 matching lines...) Expand all Loading... |
189 std::string qname_dns; | 193 std::string qname_dns; |
190 if (!net::DNSDomainFromDot(qname, &qname_dns)) { | 194 if (!net::DNSDomainFromDot(qname, &qname_dns)) { |
191 LOG(ERROR) << filename << ": DNSDomainFromDot(" << qname << ") failed."; | 195 LOG(ERROR) << filename << ": DNSDomainFromDot(" << qname << ") failed."; |
192 return false; | 196 return false; |
193 } | 197 } |
194 | 198 |
195 RunTestCase(id, qname_dns, qtype, resp_buf); | 199 RunTestCase(id, qname_dns, qtype, resp_buf); |
196 | 200 |
197 return true; | 201 return true; |
198 } | 202 } |
199 | |
200 } | 203 } |
201 | 204 |
202 int main(int argc, char** argv) { | 205 int main(int argc, char** argv) { |
203 int ret = 0; | 206 int ret = 0; |
204 | 207 |
205 for (int i = 1; i < argc; i++) | 208 for (int i = 1; i < argc; i++) |
206 if (!ReadAndRunTestCase(argv[i])) | 209 if (!ReadAndRunTestCase(argv[i])) |
207 ret = 2; | 210 ret = 2; |
208 | 211 |
209 // Cluster-Fuzz likes "#EOF" as the last line of output to help distinguish | 212 // Cluster-Fuzz likes "#EOF" as the last line of output to help distinguish |
210 // successful runs from crashes. | 213 // successful runs from crashes. |
211 printf("#EOF\n"); | 214 printf("#EOF\n"); |
212 | 215 |
213 return ret; | 216 return ret; |
214 } | 217 } |
215 | |
OLD | NEW |