| 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 <stdint.h> | |
| 6 | |
| 7 #include <algorithm> | 5 #include <algorithm> |
| 8 #include <limits> | |
| 9 #include <sstream> | 6 #include <sstream> |
| 10 #include <string> | 7 #include <string> |
| 11 #include <vector> | 8 #include <vector> |
| 12 | 9 |
| 10 #include "base/basictypes.h" |
| 13 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 14 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 15 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" |
| 16 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 18 #include "base/values.h" | 16 #include "base/values.h" |
| 19 #include "net/base/address_list.h" | 17 #include "net/base/address_list.h" |
| 20 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
| 21 #include "net/base/ip_endpoint.h" | 19 #include "net/base/ip_endpoint.h" |
| 22 #include "net/dns/dns_protocol.h" | 20 #include "net/dns/dns_protocol.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 34 free(p); | 32 free(p); |
| 35 } | 33 } |
| 36 | 34 |
| 37 void CrashNullPointerDereference(void) { | 35 void CrashNullPointerDereference(void) { |
| 38 // Cause the program to segfault with a NULL pointer dereference | 36 // Cause the program to segfault with a NULL pointer dereference |
| 39 int *p = NULL; | 37 int *p = NULL; |
| 40 *p = 0; | 38 *p = 0; |
| 41 } | 39 } |
| 42 | 40 |
| 43 bool FitsUint8(int num) { | 41 bool FitsUint8(int num) { |
| 44 return (num >= 0) && (num <= std::numeric_limits<uint8_t>::max()); | 42 return (num >= 0) && (num <= kuint8max); |
| 45 } | 43 } |
| 46 | 44 |
| 47 bool FitsUint16(int num) { | 45 bool FitsUint16(int num) { |
| 48 return (num >= 0) && (num <= std::numeric_limits<uint16_t>::max()); | 46 return (num >= 0) && (num <= kuint16max); |
| 49 } | 47 } |
| 50 | 48 |
| 51 bool ReadTestCase(const char* filename, | 49 bool ReadTestCase(const char* filename, |
| 52 uint16_t* id, | 50 uint16* id, std::string* qname, uint16* qtype, |
| 53 std::string* qname, | |
| 54 uint16_t* qtype, | |
| 55 std::vector<char>* resp_buf, | 51 std::vector<char>* resp_buf, |
| 56 bool* crash_test) { | 52 bool* crash_test) { |
| 57 base::FilePath filepath = base::FilePath::FromUTF8Unsafe(filename); | 53 base::FilePath filepath = base::FilePath::FromUTF8Unsafe(filename); |
| 58 | 54 |
| 59 std::string json; | 55 std::string json; |
| 60 if (!base::ReadFileToString(filepath, &json)) { | 56 if (!base::ReadFileToString(filepath, &json)) { |
| 61 LOG(ERROR) << filename << ": couldn't read file."; | 57 LOG(ERROR) << filename << ": couldn't read file."; |
| 62 return false; | 58 return false; |
| 63 } | 59 } |
| 64 | 60 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 82 | 78 |
| 83 int id_int; | 79 int id_int; |
| 84 if (!dict->GetInteger("id", &id_int)) { | 80 if (!dict->GetInteger("id", &id_int)) { |
| 85 LOG(ERROR) << filename << ": id is missing or not an integer."; | 81 LOG(ERROR) << filename << ": id is missing or not an integer."; |
| 86 return false; | 82 return false; |
| 87 } | 83 } |
| 88 if (!FitsUint16(id_int)) { | 84 if (!FitsUint16(id_int)) { |
| 89 LOG(ERROR) << filename << ": id is out of range."; | 85 LOG(ERROR) << filename << ": id is out of range."; |
| 90 return false; | 86 return false; |
| 91 } | 87 } |
| 92 *id = static_cast<uint16_t>(id_int); | 88 *id = static_cast<uint16>(id_int); |
| 93 | 89 |
| 94 if (!dict->GetStringASCII("qname", qname)) { | 90 if (!dict->GetStringASCII("qname", qname)) { |
| 95 LOG(ERROR) << filename << ": qname is missing or not a string."; | 91 LOG(ERROR) << filename << ": qname is missing or not a string."; |
| 96 return false; | 92 return false; |
| 97 } | 93 } |
| 98 | 94 |
| 99 int qtype_int; | 95 int qtype_int; |
| 100 if (!dict->GetInteger("qtype", &qtype_int)) { | 96 if (!dict->GetInteger("qtype", &qtype_int)) { |
| 101 LOG(ERROR) << filename << ": qtype is missing or not an integer."; | 97 LOG(ERROR) << filename << ": qtype is missing or not an integer."; |
| 102 return false; | 98 return false; |
| 103 } | 99 } |
| 104 if (!FitsUint16(qtype_int)) { | 100 if (!FitsUint16(qtype_int)) { |
| 105 LOG(ERROR) << filename << ": qtype is out of range."; | 101 LOG(ERROR) << filename << ": qtype is out of range."; |
| 106 return false; | 102 return false; |
| 107 } | 103 } |
| 108 *qtype = static_cast<uint16_t>(qtype_int); | 104 *qtype = static_cast<uint16>(qtype_int); |
| 109 | 105 |
| 110 base::ListValue* resp_list; | 106 base::ListValue* resp_list; |
| 111 if (!dict->GetList("response", &resp_list)) { | 107 if (!dict->GetList("response", &resp_list)) { |
| 112 LOG(ERROR) << filename << ": response is missing or not a list."; | 108 LOG(ERROR) << filename << ": response is missing or not a list."; |
| 113 return false; | 109 return false; |
| 114 } | 110 } |
| 115 | 111 |
| 116 size_t resp_size = resp_list->GetSize(); | 112 size_t resp_size = resp_list->GetSize(); |
| 117 resp_buf->clear(); | 113 resp_buf->clear(); |
| 118 resp_buf->reserve(resp_size); | 114 resp_buf->reserve(resp_size); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 131 DCHECK(resp_buf->size() == resp_size); | 127 DCHECK(resp_buf->size() == resp_size); |
| 132 | 128 |
| 133 LOG(INFO) << "Query: id=" << id_int << ", " | 129 LOG(INFO) << "Query: id=" << id_int << ", " |
| 134 << "qname=" << *qname << ", " | 130 << "qname=" << *qname << ", " |
| 135 << "qtype=" << qtype_int << ", " | 131 << "qtype=" << qtype_int << ", " |
| 136 << "resp_size=" << resp_size; | 132 << "resp_size=" << resp_size; |
| 137 | 133 |
| 138 return true; | 134 return true; |
| 139 } | 135 } |
| 140 | 136 |
| 141 void RunTestCase(uint16_t id, | 137 void RunTestCase(uint16 id, std::string& qname, uint16 qtype, |
| 142 std::string& qname, | |
| 143 uint16_t qtype, | |
| 144 std::vector<char>& resp_buf) { | 138 std::vector<char>& resp_buf) { |
| 145 net::DnsQuery query(id, qname, qtype); | 139 net::DnsQuery query(id, qname, qtype); |
| 146 net::DnsResponse response; | 140 net::DnsResponse response; |
| 147 std::copy(resp_buf.begin(), resp_buf.end(), response.io_buffer()->data()); | 141 std::copy(resp_buf.begin(), resp_buf.end(), response.io_buffer()->data()); |
| 148 | 142 |
| 149 if (!response.InitParse(resp_buf.size(), query)) { | 143 if (!response.InitParse(resp_buf.size(), query)) { |
| 150 LOG(INFO) << "InitParse failed."; | 144 LOG(INFO) << "InitParse failed."; |
| 151 return; | 145 return; |
| 152 } | 146 } |
| 153 | 147 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 164 std::stringstream result_line; | 158 std::stringstream result_line; |
| 165 result_line << "Response: address_list={ "; | 159 result_line << "Response: address_list={ "; |
| 166 for (unsigned int i = 0; i < address_list.size(); i++) | 160 for (unsigned int i = 0; i < address_list.size(); i++) |
| 167 result_line << address_list[i].ToString() << " "; | 161 result_line << address_list[i].ToString() << " "; |
| 168 result_line << "}, ttl=" << ttl.InSeconds() << "s"; | 162 result_line << "}, ttl=" << ttl.InSeconds() << "s"; |
| 169 | 163 |
| 170 LOG(INFO) << result_line.str(); | 164 LOG(INFO) << result_line.str(); |
| 171 } | 165 } |
| 172 | 166 |
| 173 bool ReadAndRunTestCase(const char* filename) { | 167 bool ReadAndRunTestCase(const char* filename) { |
| 174 uint16_t id = 0; | 168 uint16 id = 0; |
| 175 std::string qname; | 169 std::string qname; |
| 176 uint16_t qtype = 0; | 170 uint16 qtype = 0; |
| 177 std::vector<char> resp_buf; | 171 std::vector<char> resp_buf; |
| 178 bool crash_test = false; | 172 bool crash_test = false; |
| 179 | 173 |
| 180 LOG(INFO) << "Test case: " << filename; | 174 LOG(INFO) << "Test case: " << filename; |
| 181 | 175 |
| 182 // ReadTestCase will print a useful error message if it fails. | 176 // ReadTestCase will print a useful error message if it fails. |
| 183 if (!ReadTestCase(filename, &id, &qname, &qtype, &resp_buf, &crash_test)) | 177 if (!ReadTestCase(filename, &id, &qname, &qtype, &resp_buf, &crash_test)) |
| 184 return false; | 178 return false; |
| 185 | 179 |
| 186 if (crash_test) { | 180 if (crash_test) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 212 if (!ReadAndRunTestCase(argv[i])) | 206 if (!ReadAndRunTestCase(argv[i])) |
| 213 ret = 2; | 207 ret = 2; |
| 214 | 208 |
| 215 // Cluster-Fuzz likes "#EOF" as the last line of output to help distinguish | 209 // Cluster-Fuzz likes "#EOF" as the last line of output to help distinguish |
| 216 // successful runs from crashes. | 210 // successful runs from crashes. |
| 217 printf("#EOF\n"); | 211 printf("#EOF\n"); |
| 218 | 212 |
| 219 return ret; | 213 return ret; |
| 220 } | 214 } |
| 221 | 215 |
| OLD | NEW |