diff options
author | eranm <eranm@chromium.org> | 2015-06-25 03:06:35 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-06-25 10:07:20 +0000 |
commit | f40734dd126153328e46ae1e7c3f934c473cb15d (patch) | |
tree | 8023ea2483a8788f51aa931517025fbb1c29d64b | |
parent | 9b81a6301b6e11aa997ac3fc5b128245f9834fe2 (diff) | |
download | chromium_src-f40734dd126153328e46ae1e7c3f934c473cb15d.zip chromium_src-f40734dd126153328e46ae1e7c3f934c473cb15d.tar.gz chromium_src-f40734dd126153328e46ae1e7c3f934c473cb15d.tar.bz2 |
Certificate Transparency: Adjust the CT LogResponseParser API.
Later on this API will be used for parsing Signed Tree Heads, but
the JSON itself will be parsed by the SafeJsonParser.
BUG=480867
Review URL: https://codereview.chromium.org/1198343005
Cr-Commit-Position: refs/heads/master@{#336112}
-rw-r--r-- | net/cert/ct_log_response_parser.cc | 12 | ||||
-rw-r--r-- | net/cert/ct_log_response_parser.h | 9 | ||||
-rw-r--r-- | net/cert/ct_log_response_parser_unittest.cc | 41 |
3 files changed, 34 insertions, 28 deletions
diff --git a/net/cert/ct_log_response_parser.cc b/net/cert/ct_log_response_parser.cc index 5a2edac..87dcd10 100644 --- a/net/cert/ct_log_response_parser.cc +++ b/net/cert/ct_log_response_parser.cc @@ -5,7 +5,6 @@ #include "net/cert/ct_log_response_parser.h" #include "base/base64.h" -#include "base/json/json_reader.h" #include "base/json/json_value_converter.h" #include "base/logging.h" #include "base/strings/string_piece.h" @@ -104,18 +103,11 @@ bool IsJsonSTHStructurallyValid(const JsonSignedTreeHead& sth) { } // namespace -bool FillSignedTreeHead(const base::StringPiece& json_signed_tree_head, +bool FillSignedTreeHead(const base::Value& json_signed_tree_head, SignedTreeHead* signed_tree_head) { - base::JSONReader json_reader; - scoped_ptr<base::Value> json(json_reader.Read(json_signed_tree_head)); - if (json.get() == NULL) { - DVLOG(1) << "Empty Signed Tree Head JSON."; - return false; - } - JsonSignedTreeHead parsed_sth; base::JSONValueConverter<JsonSignedTreeHead> converter; - if (!converter.Convert(*json.get(), &parsed_sth)) { + if (!converter.Convert(json_signed_tree_head, &parsed_sth)) { DVLOG(1) << "Invalid Signed Tree Head JSON."; return false; } diff --git a/net/cert/ct_log_response_parser.h b/net/cert/ct_log_response_parser.h index e458670..b7a012b 100644 --- a/net/cert/ct_log_response_parser.h +++ b/net/cert/ct_log_response_parser.h @@ -8,6 +8,10 @@ #include "base/strings/string_piece.h" #include "net/base/net_export.h" +namespace base { +class Value; +} // namespace base + namespace net { namespace ct { @@ -17,9 +21,8 @@ struct SignedTreeHead; // |json_signed_tree_head|. // Returns true and fills in |signed_tree_head| if all fields are present and // valid.Otherwise, returns false and does not modify |signed_tree_head|. -NET_EXPORT bool FillSignedTreeHead( - const base::StringPiece& json_signed_tree_head, - SignedTreeHead* signed_tree_head); +NET_EXPORT bool FillSignedTreeHead(const base::Value& json_signed_tree_head, + SignedTreeHead* signed_tree_head); } // namespace ct diff --git a/net/cert/ct_log_response_parser_unittest.cc b/net/cert/ct_log_response_parser_unittest.cc index 7d6140a..8841f48 100644 --- a/net/cert/ct_log_response_parser_unittest.cc +++ b/net/cert/ct_log_response_parser_unittest.cc @@ -7,8 +7,10 @@ #include <string> #include "base/base64.h" +#include "base/json/json_reader.h" #include "base/strings/stringprintf.h" #include "base/time/time.h" +#include "base/values.h" #include "net/cert/ct_serialization.h" #include "net/cert/signed_tree_head.h" #include "testing/gtest/include/gtest/gtest.h" @@ -17,6 +19,13 @@ namespace net { namespace ct { +namespace { +scoped_ptr<base::Value> ParseJson(const std::string& json) { + base::JSONReader json_reader; + return json_reader.Read(json).Pass(); +} +} + std::string CreateSignedTreeHeadJsonString(std::string sha256_root_hash, std::string tree_head_signature) { std::string sth_json = "{\"tree_size\":2903698,\"timestamp\":1395761621447"; @@ -41,10 +50,10 @@ const char kTreeHeadSignature[] = "mpinCjAIhAKDXdXMtqbvQ42r9dBIwV5RM/KpEzNQdIhXHesd9HPv3"; TEST(CTLogResponseParserTest, ParsesValidJsonSTH) { - std::string sample_sth = - CreateSignedTreeHeadJsonString(kSHA256RootHash, kTreeHeadSignature); + scoped_ptr<base::Value> sample_sth = ParseJson( + CreateSignedTreeHeadJsonString(kSHA256RootHash, kTreeHeadSignature)); SignedTreeHead tree_head; - EXPECT_TRUE(FillSignedTreeHead(sample_sth, &tree_head)); + EXPECT_TRUE(FillSignedTreeHead(*sample_sth.get(), &tree_head)); base::Time expected_timestamp = base::Time::UnixEpoch() + @@ -78,27 +87,29 @@ TEST(CTLogResponseParserTest, ParsesValidJsonSTH) { } TEST(CTLogResponseParserTest, FailsToParseMissingFields) { - std::string missing_signature_sth = - CreateSignedTreeHeadJsonString(kSHA256RootHash, ""); + scoped_ptr<base::Value> missing_signature_sth = + ParseJson(CreateSignedTreeHeadJsonString(kSHA256RootHash, "")); SignedTreeHead tree_head; - ASSERT_FALSE(FillSignedTreeHead(missing_signature_sth, &tree_head)); + ASSERT_FALSE(FillSignedTreeHead(*missing_signature_sth.get(), &tree_head)); - std::string missing_root_hash_sth = - CreateSignedTreeHeadJsonString("", kTreeHeadSignature); - ASSERT_FALSE(FillSignedTreeHead(missing_root_hash_sth, &tree_head)); + scoped_ptr<base::Value> missing_root_hash_sth = + ParseJson(CreateSignedTreeHeadJsonString("", kTreeHeadSignature)); + ASSERT_FALSE(FillSignedTreeHead(*missing_root_hash_sth.get(), &tree_head)); } TEST(CTLogResponseParserTest, FailsToParseIncorrectLengthRootHash) { SignedTreeHead tree_head; - std::string too_long_hash = CreateSignedTreeHeadJsonString( - kSHA256RootHash, "/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoArK\n"); - ASSERT_FALSE(FillSignedTreeHead(too_long_hash, &tree_head)); + scoped_ptr<base::Value> too_long_hash = + ParseJson(CreateSignedTreeHeadJsonString( + kSHA256RootHash, "/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoArK\n")); + ASSERT_FALSE(FillSignedTreeHead(*too_long_hash.get(), &tree_head)); - std::string too_short_hash = CreateSignedTreeHeadJsonString( - kSHA256RootHash, "/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoA==\n"); - ASSERT_FALSE(FillSignedTreeHead(too_short_hash, &tree_head)); + scoped_ptr<base::Value> too_short_hash = + ParseJson(CreateSignedTreeHeadJsonString( + kSHA256RootHash, "/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoA==\n")); + ASSERT_FALSE(FillSignedTreeHead(*too_short_hash.get(), &tree_head)); } } // namespace ct |