diff options
author | ttuttle@chromium.org <ttuttle@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-08 18:31:19 +0000 |
---|---|---|
committer | ttuttle@chromium.org <ttuttle@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-08 18:31:19 +0000 |
commit | d309b1dc4faf84c084d508ccb1a9e8b2dbfe382c (patch) | |
tree | 6e767b0eba7b8501d39a2a5778a36dd09d7499f7 /net/tools/dns_fuzz_stub | |
parent | 8d88b6d25de93cb5c1684f63d7c43eaccd3fde62 (diff) | |
download | chromium_src-d309b1dc4faf84c084d508ccb1a9e8b2dbfe382c.zip chromium_src-d309b1dc4faf84c084d508ccb1a9e8b2dbfe382c.tar.gz chromium_src-d309b1dc4faf84c084d508ccb1a9e8b2dbfe382c.tar.bz2 |
Make dns_fuzz_stub read and parse JSON test case
BUG=130751
TEST=Adhoc; converted sample test case to JSON, and stub still works
Review URL: https://chromiumcodereview.appspot.com/10527004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@141244 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/tools/dns_fuzz_stub')
-rw-r--r-- | net/tools/dns_fuzz_stub/dns_fuzz_stub.cc | 94 |
1 files changed, 82 insertions, 12 deletions
diff --git a/net/tools/dns_fuzz_stub/dns_fuzz_stub.cc b/net/tools/dns_fuzz_stub/dns_fuzz_stub.cc index b409fdc..4b88218 100644 --- a/net/tools/dns_fuzz_stub/dns_fuzz_stub.cc +++ b/net/tools/dns_fuzz_stub/dns_fuzz_stub.cc @@ -7,8 +7,12 @@ #include <vector> #include "base/basictypes.h" -#include "base/time.h" +#include "base/file_path.h" +#include "base/file_util.h" +#include "base/json/json_reader.h" #include "base/memory/scoped_ptr.h" +#include "base/time.h" +#include "base/values.h" #include "net/base/address_list.h" #include "net/base/dns_util.h" #include "net/base/io_buffer.h" @@ -19,20 +23,86 @@ 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; + } + + scoped_ptr<Value> value(base::JSONReader::Read(json)); + if (!value.get()) { + LOG(ERROR) << "Couldn't parse JSON in " << filename << "."; + return false; + } + + DictionaryValue* dict; + if (!value->GetAsDictionary(&dict)) { + LOG(ERROR) << "Test case is not a dictionary."; + return false; + } + + int id_int; + if (!dict->GetInteger("id", &id_int)) { + LOG(ERROR) << "id is missing or not an integer."; + return false; + } + if (!FitsUint16(id_int)) { + LOG(ERROR) << "id is out of range."; + return false; + } + *id = static_cast<uint16>(id_int); + + if (!dict->GetStringASCII("qname", qname)) { + LOG(ERROR) << "qname is missing or not a string."; + return false; + } + + int qtype_int; + if (!dict->GetInteger("qtype", &qtype_int)) { + LOG(ERROR) << "qtype is missing or not an integer."; + return false; + } + if (!FitsUint16(qtype_int)) { + LOG(ERROR) << "qtype is out of range."; + return false; + } + *qtype = static_cast<uint16>(qtype_int); + + ListValue* resp_list; + if (!dict->GetList("response", &resp_list)) { + LOG(ERROR) << "response is missing 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 resp_byte_int; + if ((!resp_list->GetInteger(i, &resp_byte_int))) { + LOG(ERROR) << "response[" << i << "] is not an integer."; + return false; + } + if (!FitsUint8(resp_byte_int)) { + LOG(ERROR) << "response[" << i << "] is out of range."; + return false; + } + resp_buf->push_back(static_cast<char>(resp_byte_int)); + } + DCHECK(resp_buf->size() == resp_size); return true; } |