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 |
index b409fdc9a0bbd958c526551f45448c0409573009..be3bbb94a5cd7be315f200fa932248fd9b1525c8 100644 |
--- a/net/tools/dns_fuzz_stub/dns_fuzz_stub.cc |
+++ b/net/tools/dns_fuzz_stub/dns_fuzz_stub.cc |
@@ -7,7 +7,11 @@ |
#include <vector> |
#include "base/basictypes.h" |
+#include "base/file_path.h" |
+#include "base/file_util.h" |
#include "base/time.h" |
+#include "base/values.h" |
+#include "base/json/json_reader.h" |
#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.
|
#include "net/base/address_list.h" |
#include "net/base/dns_util.h" |
@@ -19,20 +23,72 @@ |
namespace { |
-// TODO(ttuttle): This should read from the file, probably in JSON. |
+bool FitsUint8(int num) { |
+ return (num >= 0) && (num <= kuint8max); |
+} |
+ |
+bool FitsUint16(int num) { |
+ return (num >= 0) && (num <= kuint16max); |
+} |
+ |
bool ReadTestCase(const char* filename, |
uint16* id, std::string* qname, uint16* qtype, |
std::vector<char>* resp_buf) { |
- static const unsigned char resp_bytes[] = { |
- 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_buf->assign(resp_bytes, resp_bytes + arraysize(resp_bytes)); |
+ FilePath filepath = FilePath::FromUTF8Unsafe(filename); |
+ |
+ std::string json; |
+ if (!file_util::ReadFileToString(filepath, &json)) { |
+ LOG(ERROR) << "Couldn't read file " << filename << "."; |
+ return false; |
+ } |
+ |
+ 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.
|
+ if (!value) { |
+ LOG(ERROR) << "Couldn't parse JSON in " << filename << "."; |
+ return false; |
+ } |
+ |
+ // root of JSON should always be a dictionary |
+ DictionaryValue* dict; |
+ 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
|
+ |
+ int id_int; |
+ 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.
|
+ LOG(ERROR) << "No id, not an integer, or not in range."; |
+ return false; |
+ } |
+ *id = static_cast<uint16>(id_int); |
+ |
+ if (!dict->GetStringASCII("qname", qname)) { |
+ LOG(ERROR) << "No qname or not a string."; |
+ return false; |
+ } |
+ |
+ int qtype_int; |
+ if (!dict->GetInteger("qtype", &qtype_int) || !FitsUint16(qtype_int)) { |
+ LOG(ERROR) << "No qtype, not an integer, or not in range."; |
+ return false; |
+ } |
+ *qtype = static_cast<uint16>(qtype_int); |
+ |
+ ListValue* resp_list; |
+ if (!dict->GetList("response", &resp_list)) { |
+ LOG(ERROR) << "No response, or not a list."; |
+ return false; |
+ } |
+ |
+ size_t resp_size = resp_list->GetSize(); |
+ resp_buf->clear(); |
+ resp_buf->reserve(resp_size); |
+ for (size_t i = 0; i < resp_size; i++) { |
+ int byte_int; |
+ if ((!resp_list->GetInteger(i, &byte_int)) || !FitsUint8(byte_int)) { |
+ LOG(ERROR) << "response[" << i << "] not an integer or not in range."; |
+ return false; |
+ } |
+ 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
|
+ } |
+ DCHECK(resp_buf->size() == resp_size); |
return true; |
} |