Commit 98f27319 authored by Nigel Tao's avatar Nigel Tao Committed by Commit Bot

Upgrade network from deprecated base::JSONReader API

Bug: 1070409
Change-Id: I711695c3e8c21c86f798b24885fd07ee123463f9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2212011Reviewed-by: default avatarAzeem Arshad <azeemarshad@chromium.org>
Commit-Queue: Nigel Tao <nigeltao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#772007}
parent a5b45b4c
...@@ -223,28 +223,25 @@ class AutoConnectHandlerTest : public testing::Test { ...@@ -223,28 +223,25 @@ class AutoConnectHandlerTest : public testing::Test {
void SetupPolicy(const std::string& network_configs_json, void SetupPolicy(const std::string& network_configs_json,
const base::DictionaryValue& global_config, const base::DictionaryValue& global_config,
bool user_policy) { bool user_policy) {
std::unique_ptr<base::ListValue> network_configs(new base::ListValue); base::ListValue network_configs;
if (!network_configs_json.empty()) { if (!network_configs_json.empty()) {
std::string error; base::JSONReader::ValueWithError parsed_json =
std::unique_ptr<base::Value> network_configs_value = base::JSONReader::ReadAndReturnValueWithError(
base::JSONReader::ReadAndReturnErrorDeprecated( network_configs_json, base::JSON_ALLOW_TRAILING_COMMAS);
network_configs_json, base::JSON_ALLOW_TRAILING_COMMAS, nullptr, ASSERT_TRUE(parsed_json.value) << parsed_json.error_message;
&error);
ASSERT_TRUE(network_configs_value) << error;
base::ListValue* network_configs_list = nullptr; base::ListValue* network_configs_list = nullptr;
ASSERT_TRUE(network_configs_value->GetAsList(&network_configs_list)); ASSERT_TRUE(parsed_json.value->GetAsList(&network_configs_list));
ignore_result(network_configs_value.release()); network_configs = std::move(*network_configs_list);
network_configs.reset(network_configs_list);
} }
if (user_policy) { if (user_policy) {
managed_config_handler_->SetPolicy(::onc::ONC_SOURCE_USER_POLICY, managed_config_handler_->SetPolicy(::onc::ONC_SOURCE_USER_POLICY,
helper_.UserHash(), *network_configs, helper_.UserHash(), network_configs,
global_config); global_config);
} else { } else {
managed_config_handler_->SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, managed_config_handler_->SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY,
std::string(), // no username hash std::string(), // no username hash
*network_configs, global_config); network_configs, global_config);
} }
task_environment_.RunUntilIdle(); task_environment_.RunUntilIdle();
} }
......
...@@ -322,15 +322,13 @@ class ClientCertResolverTest : public testing::Test, ...@@ -322,15 +322,13 @@ class ClientCertResolverTest : public testing::Test,
"CommonName": "B CA" "CommonName": "B CA"
} }
})"; })";
std::string error; base::JSONReader::ValueWithError parsed_json =
std::unique_ptr<base::Value> onc_pattern_value = base::JSONReader::ReadAndReturnValueWithError(
base::JSONReader::ReadAndReturnErrorDeprecated( test_onc_pattern, base::JSON_ALLOW_TRAILING_COMMAS);
test_onc_pattern, base::JSON_ALLOW_TRAILING_COMMAS, nullptr, ASSERT_TRUE(parsed_json.value) << parsed_json.error_message;
&error);
ASSERT_TRUE(onc_pattern_value) << error;
base::DictionaryValue* onc_pattern_dict; base::DictionaryValue* onc_pattern_dict;
onc_pattern_value->GetAsDictionary(&onc_pattern_dict); parsed_json.value->GetAsDictionary(&onc_pattern_dict);
client_cert_config->onc_source = onc_source; client_cert_config->onc_source = onc_source;
client_cert_config->client_cert_type = ::onc::client_cert::kPattern; client_cert_config->client_cert_type = ::onc::client_cert::kPattern;
...@@ -366,14 +364,13 @@ class ClientCertResolverTest : public testing::Test, ...@@ -366,14 +364,13 @@ class ClientCertResolverTest : public testing::Test,
void SetManagedNetworkPolicy(::onc::ONCSource onc_source, void SetManagedNetworkPolicy(::onc::ONCSource onc_source,
base::StringPiece policy_json) { base::StringPiece policy_json) {
std::string error; base::JSONReader::ValueWithError parsed_json =
std::unique_ptr<base::Value> policy_value = base::JSONReader::ReadAndReturnValueWithError(
base::JSONReader::ReadAndReturnErrorDeprecated( policy_json, base::JSON_ALLOW_TRAILING_COMMAS);
policy_json, base::JSON_ALLOW_TRAILING_COMMAS, nullptr, &error); ASSERT_TRUE(parsed_json.value) << parsed_json.error_message;
ASSERT_TRUE(policy_value) << error;
base::ListValue* policy = nullptr; base::ListValue* policy = nullptr;
ASSERT_TRUE(policy_value->GetAsList(&policy)); ASSERT_TRUE(parsed_json.value->GetAsList(&policy));
std::string user_hash = std::string user_hash =
onc_source == ::onc::ONC_SOURCE_USER_POLICY ? kUserHash : ""; onc_source == ::onc::ONC_SOURCE_USER_POLICY ? kUserHash : "";
......
...@@ -65,13 +65,13 @@ TEST_F(OncCertificatePatternTest, ParsePatternFromOnc) { ...@@ -65,13 +65,13 @@ TEST_F(OncCertificatePatternTest, ParsePatternFromOnc) {
}, },
"IssuerCAPEMs": [ "PEM1", "PEM2" ] "IssuerCAPEMs": [ "PEM1", "PEM2" ]
})"; })";
std::string error; base::JSONReader::ValueWithError parsed_json =
std::unique_ptr<base::Value> pattern_value = base::JSONReader::ReadAndReturnValueWithError(
base::JSONReader::ReadAndReturnErrorDeprecated( pattern_json, base::JSON_ALLOW_TRAILING_COMMAS);
pattern_json, base::JSON_ALLOW_TRAILING_COMMAS, nullptr, &error); ASSERT_TRUE(parsed_json.value) << parsed_json.error_message;
ASSERT_TRUE(pattern_value) << error;
auto pattern = OncCertificatePattern::ReadFromONCDictionary(*pattern_value); auto pattern =
OncCertificatePattern::ReadFromONCDictionary(*parsed_json.value);
ASSERT_TRUE(pattern); ASSERT_TRUE(pattern);
EXPECT_FALSE(pattern.value().Empty()); EXPECT_FALSE(pattern.value().Empty());
...@@ -98,14 +98,14 @@ TEST_F(OncCertificatePatternTest, PatternMatchingIssuer) { ...@@ -98,14 +98,14 @@ TEST_F(OncCertificatePatternTest, PatternMatchingIssuer) {
"CommonName": "B CA" "CommonName": "B CA"
} }
})"; })";
std::string error; base::JSONReader::ValueWithError parsed_json =
std::unique_ptr<base::Value> pattern_value = base::JSONReader::ReadAndReturnValueWithError(
base::JSONReader::ReadAndReturnErrorDeprecated( pattern_json, base::JSON_ALLOW_TRAILING_COMMAS);
pattern_json, base::JSON_ALLOW_TRAILING_COMMAS, nullptr, &error); ASSERT_TRUE(parsed_json.value) << parsed_json.error_message;
ASSERT_TRUE(pattern_value) << error;
{ {
auto pattern = OncCertificatePattern::ReadFromONCDictionary(*pattern_value); auto pattern =
OncCertificatePattern::ReadFromONCDictionary(*parsed_json.value);
ASSERT_TRUE(pattern); ASSERT_TRUE(pattern);
EXPECT_FALSE(pattern.value().Empty()); EXPECT_FALSE(pattern.value().Empty());
...@@ -113,12 +113,13 @@ TEST_F(OncCertificatePatternTest, PatternMatchingIssuer) { ...@@ -113,12 +113,13 @@ TEST_F(OncCertificatePatternTest, PatternMatchingIssuer) {
} }
{ {
base::Value* issuer = base::Value* issuer = parsed_json.value->FindKeyOfType(
pattern_value->FindKeyOfType("Issuer", base::Value::Type::DICTIONARY); "Issuer", base::Value::Type::DICTIONARY);
ASSERT_TRUE(issuer); ASSERT_TRUE(issuer);
issuer->SetKey("CommonName", base::Value("SomeOtherCA")); issuer->SetKey("CommonName", base::Value("SomeOtherCA"));
auto pattern = OncCertificatePattern::ReadFromONCDictionary(*pattern_value); auto pattern =
OncCertificatePattern::ReadFromONCDictionary(*parsed_json.value);
ASSERT_TRUE(pattern); ASSERT_TRUE(pattern);
EXPECT_FALSE(pattern.value().Matches(*cert_, kFakePemEncodedIssuer)); EXPECT_FALSE(pattern.value().Matches(*cert_, kFakePemEncodedIssuer));
} }
...@@ -134,14 +135,14 @@ TEST_F(OncCertificatePatternTest, PatternMatchingSubject) { ...@@ -134,14 +135,14 @@ TEST_F(OncCertificatePatternTest, PatternMatchingSubject) {
"CommonName": "Client Cert A" "CommonName": "Client Cert A"
} }
})"; })";
std::string error; base::JSONReader::ValueWithError parsed_json =
std::unique_ptr<base::Value> pattern_value = base::JSONReader::ReadAndReturnValueWithError(
base::JSONReader::ReadAndReturnErrorDeprecated( pattern_json, base::JSON_ALLOW_TRAILING_COMMAS);
pattern_json, base::JSON_ALLOW_TRAILING_COMMAS, nullptr, &error); ASSERT_TRUE(parsed_json.value) << parsed_json.error_message;
ASSERT_TRUE(pattern_value) << error;
{ {
auto pattern = OncCertificatePattern::ReadFromONCDictionary(*pattern_value); auto pattern =
OncCertificatePattern::ReadFromONCDictionary(*parsed_json.value);
ASSERT_TRUE(pattern); ASSERT_TRUE(pattern);
EXPECT_FALSE(pattern.value().Empty()); EXPECT_FALSE(pattern.value().Empty());
...@@ -149,12 +150,13 @@ TEST_F(OncCertificatePatternTest, PatternMatchingSubject) { ...@@ -149,12 +150,13 @@ TEST_F(OncCertificatePatternTest, PatternMatchingSubject) {
} }
{ {
base::Value* issuer = base::Value* issuer = parsed_json.value->FindKeyOfType(
pattern_value->FindKeyOfType("Subject", base::Value::Type::DICTIONARY); "Subject", base::Value::Type::DICTIONARY);
ASSERT_TRUE(issuer); ASSERT_TRUE(issuer);
issuer->SetKey("CommonName", base::Value("B CA")); issuer->SetKey("CommonName", base::Value("B CA"));
auto pattern = OncCertificatePattern::ReadFromONCDictionary(*pattern_value); auto pattern =
OncCertificatePattern::ReadFromONCDictionary(*parsed_json.value);
ASSERT_TRUE(pattern); ASSERT_TRUE(pattern);
EXPECT_FALSE(pattern.value().Matches(*cert_, kFakePemEncodedIssuer)); EXPECT_FALSE(pattern.value().Matches(*cert_, kFakePemEncodedIssuer));
} }
...@@ -165,13 +167,13 @@ TEST_F(OncCertificatePatternTest, PatternMatchingIssuerCAPEM) { ...@@ -165,13 +167,13 @@ TEST_F(OncCertificatePatternTest, PatternMatchingIssuerCAPEM) {
{ {
"IssuerCAPEMs": ["PEM-ENCODED-ISSUER"] "IssuerCAPEMs": ["PEM-ENCODED-ISSUER"]
})"; })";
std::string error; base::JSONReader::ValueWithError parsed_json =
std::unique_ptr<base::Value> pattern_value = base::JSONReader::ReadAndReturnValueWithError(
base::JSONReader::ReadAndReturnErrorDeprecated( pattern_json, base::JSON_ALLOW_TRAILING_COMMAS);
pattern_json, base::JSON_ALLOW_TRAILING_COMMAS, nullptr, &error); ASSERT_TRUE(parsed_json.value) << parsed_json.error_message;
ASSERT_TRUE(pattern_value) << error;
auto pattern = OncCertificatePattern::ReadFromONCDictionary(*pattern_value); auto pattern =
OncCertificatePattern::ReadFromONCDictionary(*parsed_json.value);
ASSERT_TRUE(pattern); ASSERT_TRUE(pattern);
EXPECT_FALSE(pattern.value().Empty()); EXPECT_FALSE(pattern.value().Empty());
......
...@@ -649,15 +649,14 @@ const char kEmptyUnencryptedConfiguration[] = ...@@ -649,15 +649,14 @@ const char kEmptyUnencryptedConfiguration[] =
"\"Certificates\":[]}"; "\"Certificates\":[]}";
std::unique_ptr<base::Value> ReadDictionaryFromJson(const std::string& json) { std::unique_ptr<base::Value> ReadDictionaryFromJson(const std::string& json) {
std::string error; base::JSONReader::ValueWithError parsed_json =
std::unique_ptr<base::Value> root = base::JSONReader::ReadAndReturnValueWithError(
base::JSONReader::ReadAndReturnErrorDeprecated( json, base::JSON_ALLOW_TRAILING_COMMAS);
json, base::JSON_ALLOW_TRAILING_COMMAS, nullptr, &error); if (!parsed_json.value || !parsed_json.value->is_dict()) {
if (!root || !root->is_dict()) { NET_LOG(ERROR) << "Invalid JSON Dictionary: " << parsed_json.error_message;
NET_LOG(ERROR) << "Invalid JSON Dictionary: " << error;
return nullptr; return nullptr;
} }
return root; return base::Value::ToUniquePtrValue(std::move(*parsed_json.value));
} }
std::unique_ptr<base::Value> Decrypt(const std::string& passphrase, std::unique_ptr<base::Value> Decrypt(const std::string& passphrase,
......
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