Commit 49b99c4e authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Avoid some base::JSONReader instantiations.

base::JSONReader::Read() is a static method, so there is no need to
instantiate a base::JSONReader object. While making this change, also
stop using ReadDeprecated(), and make the code lint error free.

Change-Id: I4ba79d61af6c736e762cb926a04e28ef13fb63fe
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1476413Reviewed-by: default avatarEric Seckler <eseckler@chromium.org>
Reviewed-by: default avatarDavid Benjamin <davidben@chromium.org>
Reviewed-by: default avatarMax Moroz <mmoroz@chromium.org>
Reviewed-by: default avatarIstiaque Ahmed <lazyboy@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#723439}
parent cf117092
......@@ -108,14 +108,12 @@ class NodeList : public std::vector<std::unique_ptr<Node>> {
static std::unique_ptr<NodeList> ParseJsonString(const uint8_t* data,
size_t size) {
std::unique_ptr<NodeList> nodes(new NodeList());
auto nodes = std::make_unique<NodeList>();
base::JSONReader reader;
std::unique_ptr<base::Value> value(reader.ReadDeprecated(
base::Optional<base::Value> value(base::JSONReader::Read(
std::string(reinterpret_cast<const char*>(data), size)));
if (value) {
if (value)
nodes->ParseJson(*value);
}
return nodes;
}
......
......@@ -4,6 +4,8 @@
#include "extensions/browser/value_store/leveldb_scoped_database.h"
#include <utility>
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
......
......@@ -5,7 +5,9 @@
#ifndef EXTENSIONS_BROWSER_VALUE_STORE_LEVELDB_SCOPED_DATABASE_H_
#define EXTENSIONS_BROWSER_VALUE_STORE_LEVELDB_SCOPED_DATABASE_H_
#include <memory>
#include <string>
#include <vector>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
......
......@@ -20,18 +20,11 @@ namespace net {
namespace ct {
namespace {
std::unique_ptr<base::Value> ParseJson(const std::string& json) {
base::JSONReader json_reader;
return json_reader.ReadDeprecated(json);
}
}
TEST(CTLogResponseParserTest, ParsesValidJsonSTH) {
std::unique_ptr<base::Value> sample_sth_json =
ParseJson(GetSampleSTHAsJson());
base::Optional<base::Value> sample_sth_json =
base::JSONReader::Read(GetSampleSTHAsJson());
SignedTreeHead tree_head;
EXPECT_TRUE(FillSignedTreeHead(*sample_sth_json.get(), &tree_head));
EXPECT_TRUE(FillSignedTreeHead(*sample_sth_json, &tree_head));
SignedTreeHead sample_sth;
ASSERT_TRUE(GetSampleSignedTreeHead(&sample_sth));
......@@ -59,17 +52,17 @@ TEST(CTLogResponseParserTest, ParsesValidJsonSTH) {
}
TEST(CTLogResponseParserTest, FailsToParseMissingFields) {
std::unique_ptr<base::Value> missing_signature_sth = ParseJson(
base::Optional<base::Value> missing_signature_sth = base::JSONReader::Read(
CreateSignedTreeHeadJsonString(1 /* tree_size */, 123456u /* timestamp */,
GetSampleSTHSHA256RootHash(), ""));
SignedTreeHead tree_head;
ASSERT_FALSE(FillSignedTreeHead(*missing_signature_sth.get(), &tree_head));
ASSERT_FALSE(FillSignedTreeHead(*missing_signature_sth, &tree_head));
std::unique_ptr<base::Value> missing_root_hash_sth = ParseJson(
base::Optional<base::Value> missing_root_hash_sth = base::JSONReader::Read(
CreateSignedTreeHeadJsonString(1 /* tree_size */, 123456u /* timestamp */,
"", GetSampleSTHTreeHeadSignature()));
ASSERT_FALSE(FillSignedTreeHead(*missing_root_hash_sth.get(), &tree_head));
ASSERT_FALSE(FillSignedTreeHead(*missing_root_hash_sth, &tree_head));
}
TEST(CTLogResponseParserTest, FailsToParseIncorrectLengthRootHash) {
......@@ -79,32 +72,32 @@ TEST(CTLogResponseParserTest, FailsToParseIncorrectLengthRootHash) {
base::Base64Decode(
base::StringPiece("/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoArK\n"),
&too_long_hash);
std::unique_ptr<base::Value> too_long_hash_json =
ParseJson(CreateSignedTreeHeadJsonString(
base::Optional<base::Value> too_long_hash_json =
base::JSONReader::Read(CreateSignedTreeHeadJsonString(
1 /* tree_size */, 123456u /* timestamp */,
GetSampleSTHSHA256RootHash(), too_long_hash));
ASSERT_FALSE(FillSignedTreeHead(*too_long_hash_json.get(), &tree_head));
ASSERT_FALSE(FillSignedTreeHead(*too_long_hash_json, &tree_head));
std::string too_short_hash;
base::Base64Decode(
base::StringPiece("/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoA==\n"),
&too_short_hash);
std::unique_ptr<base::Value> too_short_hash_json =
ParseJson(CreateSignedTreeHeadJsonString(
base::Optional<base::Value> too_short_hash_json =
base::JSONReader::Read(CreateSignedTreeHeadJsonString(
1 /* tree_size */, 123456u /* timestamp */,
GetSampleSTHSHA256RootHash(), too_short_hash));
ASSERT_FALSE(FillSignedTreeHead(*too_short_hash_json.get(), &tree_head));
ASSERT_FALSE(FillSignedTreeHead(*too_short_hash_json, &tree_head));
}
TEST(CTLogResponseParserTest, ParsesJsonSTHWithLargeTimestamp) {
SignedTreeHead tree_head;
std::unique_ptr<base::Value> large_timestamp_json =
ParseJson(CreateSignedTreeHeadJsonString(
base::Optional<base::Value> large_timestamp_json =
base::JSONReader::Read(CreateSignedTreeHeadJsonString(
100, INT64_C(1) << 34, GetSampleSTHSHA256RootHash(),
GetSampleSTHTreeHeadSignature()));
ASSERT_TRUE(FillSignedTreeHead(*large_timestamp_json.get(), &tree_head));
ASSERT_TRUE(FillSignedTreeHead(*large_timestamp_json, &tree_head));
base::Time expected_time =
base::Time::UnixEpoch() +
......@@ -122,12 +115,12 @@ TEST(CTLogResponseParserTest, ParsesConsistencyProofSuccessfully) {
raw_nodes.push_back(first);
raw_nodes.push_back(second);
raw_nodes.push_back(third);
std::unique_ptr<base::Value> sample_consistency_proof =
ParseJson(CreateConsistencyProofJsonString(raw_nodes));
base::Optional<base::Value> sample_consistency_proof =
base::JSONReader::Read(CreateConsistencyProofJsonString(raw_nodes));
std::vector<std::string> output;
ASSERT_TRUE(FillConsistencyProof(*sample_consistency_proof.get(), &output));
ASSERT_TRUE(FillConsistencyProof(*sample_consistency_proof, &output));
EXPECT_EQ(output[0], first);
EXPECT_EQ(output[1], second);
......@@ -137,28 +130,29 @@ TEST(CTLogResponseParserTest, ParsesConsistencyProofSuccessfully) {
TEST(CTLogResponseParserTest, FailsOnInvalidProofJson) {
std::vector<std::string> output;
std::unique_ptr<base::Value> badly_encoded =
ParseJson(std::string("{\"consistency\": [\"notbase64\"]}"));
EXPECT_FALSE(FillConsistencyProof(*badly_encoded.get(), &output));
base::Optional<base::Value> badly_encoded =
base::JSONReader::Read(std::string("{\"consistency\": [\"notbase64\"]}"));
EXPECT_FALSE(FillConsistencyProof(*badly_encoded, &output));
std::unique_ptr<base::Value> not_a_string =
ParseJson(std::string("{\"consistency\": [42, 16]}"));
EXPECT_FALSE(FillConsistencyProof(*badly_encoded.get(), &output));
base::Optional<base::Value> not_a_string =
base::JSONReader::Read(std::string("{\"consistency\": [42, 16]}"));
EXPECT_FALSE(FillConsistencyProof(*badly_encoded, &output));
std::unique_ptr<base::Value> missing_consistency =
ParseJson(std::string("{}"));
EXPECT_FALSE(FillConsistencyProof(*missing_consistency.get(), &output));
base::Optional<base::Value> missing_consistency =
base::JSONReader::Read(std::string("{}"));
EXPECT_FALSE(FillConsistencyProof(*missing_consistency, &output));
std::unique_ptr<base::Value> not_a_dict = ParseJson(std::string("[]"));
EXPECT_FALSE(FillConsistencyProof(*not_a_dict.get(), &output));
base::Optional<base::Value> not_a_dict =
base::JSONReader::Read(std::string("[]"));
EXPECT_FALSE(FillConsistencyProof(*not_a_dict, &output));
}
TEST(CTLogResponseParserTest, ParsesProofJsonWithExtraFields) {
std::vector<std::string> output;
std::unique_ptr<base::Value> badly_encoded =
ParseJson(std::string("{\"consistency\": [], \"somethingelse\": 3}"));
EXPECT_TRUE(FillConsistencyProof(*badly_encoded.get(), &output));
base::Optional<base::Value> badly_encoded = base::JSONReader::Read(
std::string("{\"consistency\": [], \"somethingelse\": 3}"));
EXPECT_TRUE(FillConsistencyProof(*badly_encoded, &output));
}
} // namespace ct
......
......@@ -5,6 +5,9 @@
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_INSTRUMENTATION_TRACING_TRACED_VALUE_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_INSTRUMENTATION_TRACING_TRACED_VALUE_H_
#include <memory>
#include <string>
#include "base/macros.h"
#include "base/trace_event/traced_value.h"
#include "third_party/blink/renderer/platform/platform_export.h"
......
......@@ -4,17 +4,17 @@
#include "third_party/blink/renderer/platform/instrumentation/tracing/traced_value.h"
#include <utility>
#include "base/json/json_reader.h"
#include "base/values.h"
#include "testing/gtest/include/gtest/gtest.h"
#include <memory>
namespace blink {
std::unique_ptr<base::Value> ParseTracedValue(
base::Optional<base::Value> ParseTracedValue(
std::unique_ptr<TracedValue> value) {
base::JSONReader reader;
return reader.ReadDeprecated(value->ToString().Utf8());
return base::JSONReader::Read(value->ToString().Utf8());
}
TEST(TracedValueTest, FlatDictionary) {
......@@ -24,7 +24,7 @@ TEST(TracedValueTest, FlatDictionary) {
value->SetBooleanWithCopiedName("bool", true);
value->SetStringWithCopiedName("string", "string");
std::unique_ptr<base::Value> parsed = ParseTracedValue(std::move(value));
base::Optional<base::Value> parsed = ParseTracedValue(std::move(value));
base::DictionaryValue* dictionary;
ASSERT_TRUE(parsed->GetAsDictionary(&dictionary));
int int_value;
......@@ -59,7 +59,7 @@ TEST(TracedValueTest, Hierarchy) {
value->EndArray();
value->SetStringWithCopiedName("s0", "foo");
std::unique_ptr<base::Value> parsed = ParseTracedValue(std::move(value));
base::Optional<base::Value> parsed = ParseTracedValue(std::move(value));
base::DictionaryValue* dictionary;
ASSERT_TRUE(parsed->GetAsDictionary(&dictionary));
int i0;
......@@ -106,7 +106,7 @@ TEST(TracedValueTest, Escape) {
value->SetStringWithCopiedName("s3\\", "value3");
value->SetStringWithCopiedName("\"s4\"", "value4");
std::unique_ptr<base::Value> parsed = ParseTracedValue(std::move(value));
base::Optional<base::Value> parsed = ParseTracedValue(std::move(value));
base::DictionaryValue* dictionary;
ASSERT_TRUE(parsed->GetAsDictionary(&dictionary));
std::string s0;
......@@ -142,7 +142,7 @@ TEST(TracedValueTest, NonCopiedNames) {
value->PushInteger(2);
value->EndArray();
std::unique_ptr<base::Value> parsed = ParseTracedValue(std::move(value));
base::Optional<base::Value> parsed = ParseTracedValue(std::move(value));
base::DictionaryValue* dictionary;
ASSERT_TRUE(parsed->GetAsDictionary(&dictionary));
int int_value;
......
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