Commit d309b1dc authored by ttuttle@chromium.org's avatar ttuttle@chromium.org

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
parent 8d88b6d2
...@@ -7,8 +7,12 @@ ...@@ -7,8 +7,12 @@
#include <vector> #include <vector>
#include "base/basictypes.h" #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/memory/scoped_ptr.h"
#include "base/time.h"
#include "base/values.h"
#include "net/base/address_list.h" #include "net/base/address_list.h"
#include "net/base/dns_util.h" #include "net/base/dns_util.h"
#include "net/base/io_buffer.h" #include "net/base/io_buffer.h"
...@@ -19,20 +23,86 @@ ...@@ -19,20 +23,86 @@
namespace { 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, bool ReadTestCase(const char* filename,
uint16* id, std::string* qname, uint16* qtype, uint16* id, std::string* qname, uint16* qtype,
std::vector<char>* resp_buf) { std::vector<char>* resp_buf) {
static const unsigned char resp_bytes[] = { FilePath filepath = FilePath::FromUTF8Unsafe(filename);
0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x01, 0x61, 0x00, 0x00, 0x01, 0x00, 0x01, std::string json;
0x01, 0x61, 0x00, 0x00, 0x01, 0x00, 0x01, if (!file_util::ReadFileToString(filepath, &json)) {
0x00, 0x00, 0x00, 0xff, 0x00, 0x04, 0x0a, 0x0a, 0x0a, 0x0a }; LOG(ERROR) << "Couldn't read file " << filename << ".";
return false;
*id = 0x0000; }
*qname = "a";
*qtype = 0x0001; scoped_ptr<Value> value(base::JSONReader::Read(json));
resp_buf->assign(resp_bytes, resp_bytes + arraysize(resp_bytes)); 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; return true;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment