Commit 5fabb2c2 authored by Steven Bennetts's avatar Steven Bennetts Committed by Commit Bot

Convert onc::ReadDictionaryFromJson to use base::Value

This includes some partial conversion to base::Value in onc_utils.cc,
as part of a long term effort to convert network config and ONC code
to non deprecated base::Value patterns.

Bug: 651157
Change-Id: Ic0456f9221fedaf95a78ce670af9f4be4bc95ad1
Reviewed-on: https://chromium-review.googlesource.com/1144196
Commit-Queue: Steven Bennetts <stevenjb@chromium.org>
Reviewed-by: default avatarToni Barzic <tbarzic@chromium.org>
Reviewed-by: default avatarMaksim Ivanov <emaxx@chromium.org>
Cr-Commit-Position: refs/heads/master@{#577997}
parent 60f80879
...@@ -200,9 +200,9 @@ bool NetworkConfigurationPolicyHandler::CheckPolicySettings( ...@@ -200,9 +200,9 @@ bool NetworkConfigurationPolicyHandler::CheckPolicySettings(
if (value) { if (value) {
std::string onc_blob; std::string onc_blob;
value->GetAsString(&onc_blob); value->GetAsString(&onc_blob);
std::unique_ptr<base::DictionaryValue> root_dict = std::unique_ptr<base::Value> root_dict =
chromeos::onc::ReadDictionaryFromJson(onc_blob); chromeos::onc::ReadDictionaryFromJson(onc_blob);
if (root_dict.get() == NULL) { if (!root_dict) {
errors->AddError(policy_name(), IDS_POLICY_NETWORK_CONFIG_PARSE_FAILED); errors->AddError(policy_name(), IDS_POLICY_NETWORK_CONFIG_PARSE_FAILED);
return false; return false;
} }
...@@ -289,9 +289,10 @@ NetworkConfigurationPolicyHandler::SanitizeNetworkConfig( ...@@ -289,9 +289,10 @@ NetworkConfigurationPolicyHandler::SanitizeNetworkConfig(
return NULL; return NULL;
std::unique_ptr<base::DictionaryValue> toplevel_dict = std::unique_ptr<base::DictionaryValue> toplevel_dict =
chromeos::onc::ReadDictionaryFromJson(json_string); base::DictionaryValue::From(
chromeos::onc::ReadDictionaryFromJson(json_string));
if (!toplevel_dict) if (!toplevel_dict)
return NULL; return nullptr;
// Placeholder to insert in place of the filtered setting. // Placeholder to insert in place of the filtered setting.
const char kPlaceholder[] = "********"; const char kPlaceholder[] = "********";
......
...@@ -248,17 +248,18 @@ class NetworkConfigurationUpdaterTest : public testing::Test { ...@@ -248,17 +248,18 @@ class NetworkConfigurationUpdaterTest : public testing::Test {
providers.push_back(&provider_); providers.push_back(&provider_);
policy_service_ = std::make_unique<PolicyServiceImpl>(std::move(providers)); policy_service_ = std::make_unique<PolicyServiceImpl>(std::move(providers));
std::unique_ptr<base::DictionaryValue> fake_toplevel_onc = std::unique_ptr<base::Value> fake_toplevel_onc =
chromeos::onc::ReadDictionaryFromJson(kFakeONC); chromeos::onc::ReadDictionaryFromJson(kFakeONC);
base::DictionaryValue* global_config = NULL; base::DictionaryValue* global_config = nullptr;
fake_toplevel_onc->GetDictionaryWithoutPathExpansion( fake_toplevel_onc
onc::toplevel_config::kGlobalNetworkConfiguration, &global_config); ->FindKey(onc::toplevel_config::kGlobalNetworkConfiguration)
->GetAsDictionary(&global_config);
fake_global_network_config_.MergeDictionary(global_config); fake_global_network_config_.MergeDictionary(global_config);
base::ListValue* certs = NULL; base::ListValue* certs = nullptr;
fake_toplevel_onc->GetListWithoutPathExpansion( fake_toplevel_onc->FindKey(onc::toplevel_config::kCertificates)
onc::toplevel_config::kCertificates, &certs); ->GetAsList(&certs);
fake_certificates_ = fake_certificates_ =
std::make_unique<chromeos::onc::OncParsedCertificates>(*certs); std::make_unique<chromeos::onc::OncParsedCertificates>(*certs);
...@@ -267,7 +268,7 @@ class NetworkConfigurationUpdaterTest : public testing::Test { ...@@ -267,7 +268,7 @@ class NetworkConfigurationUpdaterTest : public testing::Test {
} }
base::Value* GetExpectedFakeNetworkConfigs(::onc::ONCSource source) { base::Value* GetExpectedFakeNetworkConfigs(::onc::ONCSource source) {
std::unique_ptr<base::DictionaryValue> fake_toplevel_onc = std::unique_ptr<base::Value> fake_toplevel_onc =
chromeos::onc::ReadDictionaryFromJson(kFakeONC); chromeos::onc::ReadDictionaryFromJson(kFakeONC);
fake_network_configs_ = fake_network_configs_ =
fake_toplevel_onc->FindKey(onc::toplevel_config::kNetworkConfigurations) fake_toplevel_onc->FindKey(onc::toplevel_config::kNetworkConfigurations)
......
...@@ -514,8 +514,8 @@ TEST_F(ProxyConfigServiceImplTest, SharedEthernetAndUserPolicy) { ...@@ -514,8 +514,8 @@ TEST_F(ProxyConfigServiceImplTest, SharedEthernetAndUserPolicy) {
SetUpSharedEthernet(); SetUpSharedEthernet();
SetUpProxyConfigService(&profile_prefs_); SetUpProxyConfigService(&profile_prefs_);
std::unique_ptr<base::DictionaryValue> ethernet_policy( std::unique_ptr<base::Value> ethernet_policy =
chromeos::onc::ReadDictionaryFromJson(kEthernetPolicy)); chromeos::onc::ReadDictionaryFromJson(kEthernetPolicy);
std::unique_ptr<base::ListValue> network_configs(new base::ListValue); std::unique_ptr<base::ListValue> network_configs(new base::ListValue);
network_configs->Append(std::move(ethernet_policy)); network_configs->Append(std::move(ethernet_policy));
......
...@@ -146,7 +146,8 @@ class ManagedNetworkConfigurationHandlerTest : public testing::Test { ...@@ -146,7 +146,8 @@ class ManagedNetworkConfigurationHandlerTest : public testing::Test {
const std::string& path_to_onc) { const std::string& path_to_onc) {
std::unique_ptr<base::DictionaryValue> policy = std::unique_ptr<base::DictionaryValue> policy =
path_to_onc.empty() path_to_onc.empty()
? onc::ReadDictionaryFromJson(onc::kEmptyUnencryptedConfiguration) ? base::DictionaryValue::From(onc::ReadDictionaryFromJson(
onc::kEmptyUnencryptedConfiguration))
: test_utils::ReadTestDictionary(path_to_onc); : test_utils::ReadTestDictionary(path_to_onc);
base::ListValue validated_network_configs; base::ListValue validated_network_configs;
......
...@@ -162,14 +162,11 @@ bool NetworkState::PropertyChanged(const std::string& key, ...@@ -162,14 +162,11 @@ bool NetworkState::PropertyChanged(const std::string& key,
if (proxy_config_str.empty()) if (proxy_config_str.empty())
return true; return true;
std::unique_ptr<base::DictionaryValue> proxy_config_dict( std::unique_ptr<base::Value> proxy_config_dict(
onc::ReadDictionaryFromJson(proxy_config_str)); onc::ReadDictionaryFromJson(proxy_config_str));
if (proxy_config_dict) { if (proxy_config_dict) {
// Warning: The DictionaryValue returned from proxy_config_.MergeDictionary(
// ReadDictionaryFromJson/JSONParser is an optimized derived class that base::DictionaryValue::From(std::move(proxy_config_dict)).get());
// doesn't allow releasing ownership of nested values. A Swap in the wrong
// order leads to memory access errors.
proxy_config_.MergeDictionary(proxy_config_dict.get());
} else { } else {
NET_LOG(ERROR) << "Failed to parse " << path() << "." << key; NET_LOG(ERROR) << "Failed to parse " << path() << "." << key;
} }
......
...@@ -62,7 +62,8 @@ std::string NetworkStateTest::ConfigureService( ...@@ -62,7 +62,8 @@ std::string NetworkStateTest::ConfigureService(
last_created_service_path_ = ""; last_created_service_path_ = "";
std::unique_ptr<base::DictionaryValue> shill_json_dict = std::unique_ptr<base::DictionaryValue> shill_json_dict =
onc::ReadDictionaryFromJson(shill_json_string); base::DictionaryValue::From(
onc::ReadDictionaryFromJson(shill_json_string));
if (!shill_json_dict) { if (!shill_json_dict) {
LOG(ERROR) << "Error parsing json: " << shill_json_string; LOG(ERROR) << "Error parsing json: " << shill_json_string;
return last_created_service_path_; return last_created_service_path_;
......
...@@ -623,10 +623,10 @@ void ShillToONCTranslator::TranslateNetworkWithState() { ...@@ -623,10 +623,10 @@ void ShillToONCTranslator::TranslateNetworkWithState() {
if (shill_dictionary_->GetStringWithoutPathExpansion( if (shill_dictionary_->GetStringWithoutPathExpansion(
shill::kProxyConfigProperty, &proxy_config_str) && shill::kProxyConfigProperty, &proxy_config_str) &&
!proxy_config_str.empty()) { !proxy_config_str.empty()) {
std::unique_ptr<base::DictionaryValue> proxy_config_value( std::unique_ptr<base::Value> proxy_config_value(
ReadDictionaryFromJson(proxy_config_str)); ReadDictionaryFromJson(proxy_config_str));
if (proxy_config_value) { if (proxy_config_value) {
std::unique_ptr<base::DictionaryValue> proxy_settings = std::unique_ptr<base::Value> proxy_settings =
ConvertProxyConfigToOncProxySettings(std::move(proxy_config_value)); ConvertProxyConfigToOncProxySettings(std::move(proxy_config_value));
if (proxy_settings) { if (proxy_settings) {
onc_object_->SetWithoutPathExpansion( onc_object_->SetWithoutPathExpansion(
......
This diff is collapsed.
...@@ -43,17 +43,17 @@ CHROMEOS_EXPORT extern const char kEmptyUnencryptedConfiguration[]; ...@@ -43,17 +43,17 @@ CHROMEOS_EXPORT extern const char kEmptyUnencryptedConfiguration[];
typedef std::map<std::string, std::string> CertPEMsByGUIDMap; typedef std::map<std::string, std::string> CertPEMsByGUIDMap;
// Parses |json| according to the JSON format. If |json| is a JSON formatted // Parses |json| according to the JSON format. If |json| is a JSON formatted
// dictionary, the function returns the dictionary as a DictionaryValue. // dictionary, the function returns the dictionary value, otherwise returns
// Otherwise returns NULL. // null.
CHROMEOS_EXPORT std::unique_ptr<base::DictionaryValue> ReadDictionaryFromJson( CHROMEOS_EXPORT std::unique_ptr<base::Value> ReadDictionaryFromJson(
const std::string& json); const std::string& json);
// Decrypts the given EncryptedConfiguration |onc| (see the ONC specification) // Decrypts the given EncryptedConfiguration |onc| (see the ONC specification)
// using |passphrase|. The resulting UnencryptedConfiguration is returned. If an // using |passphrase|. The resulting UnencryptedConfiguration is returned. If an
// error occurs, returns NULL. // error occurs, returns null.
CHROMEOS_EXPORT std::unique_ptr<base::DictionaryValue> Decrypt( CHROMEOS_EXPORT std::unique_ptr<base::Value> Decrypt(
const std::string& passphrase, const std::string& passphrase,
const base::DictionaryValue& onc); const base::Value& onc);
// For logging only: strings not user facing. // For logging only: strings not user facing.
CHROMEOS_EXPORT std::string GetSourceAsString(::onc::ONCSource source); CHROMEOS_EXPORT std::string GetSourceAsString(::onc::ONCSource source);
...@@ -77,12 +77,12 @@ CHROMEOS_EXPORT void ExpandStringsInNetworks( ...@@ -77,12 +77,12 @@ CHROMEOS_EXPORT void ExpandStringsInNetworks(
// specification. The object of |onc_object| is modified in place. // specification. The object of |onc_object| is modified in place.
CHROMEOS_EXPORT void FillInHexSSIDFieldsInOncObject( CHROMEOS_EXPORT void FillInHexSSIDFieldsInOncObject(
const OncValueSignature& signature, const OncValueSignature& signature,
base::DictionaryValue* onc_object); base::Value* onc_object);
// If the SSID field is set, but HexSSID is not, converts the contents of the // If the SSID field is set, but HexSSID is not, converts the contents of the
// SSID field to UTF-8 encoding, creates the hex representation and assigns the // SSID field to UTF-8 encoding, creates the hex representation and assigns the
// result to HexSSID. // result to HexSSID.
CHROMEOS_EXPORT void FillInHexSSIDField(base::DictionaryValue* wifi_fields); CHROMEOS_EXPORT void FillInHexSSIDField(base::Value* wifi_fields);
// Creates a copy of |onc_object| with all values of sensitive fields replaced // Creates a copy of |onc_object| with all values of sensitive fields replaced
// by |mask|. To find sensitive fields, signature and field name are checked // by |mask|. To find sensitive fields, signature and field name are checked
...@@ -146,9 +146,9 @@ ConvertOncProxySettingsToProxyConfig( ...@@ -146,9 +146,9 @@ ConvertOncProxySettingsToProxyConfig(
// Translates |proxy_config_value|, which must be a valid ProxyConfig dictionary // Translates |proxy_config_value|, which must be a valid ProxyConfig dictionary
// (see proxy_config_dictionary.h) to an ONC ProxySettings dictionary. // (see proxy_config_dictionary.h) to an ONC ProxySettings dictionary.
CHROMEOS_EXPORT std::unique_ptr<base::DictionaryValue> CHROMEOS_EXPORT std::unique_ptr<base::Value>
ConvertProxyConfigToOncProxySettings( ConvertProxyConfigToOncProxySettings(
std::unique_ptr<base::DictionaryValue> proxy_config_value); std::unique_ptr<base::Value> proxy_config_value);
// Replaces user-specific string placeholders in |network_configs|, which must // Replaces user-specific string placeholders in |network_configs|, which must
// be a list of ONC NetworkConfigurations. Currently only user name placeholders // be a list of ONC NetworkConfigurations. Currently only user name placeholders
......
...@@ -44,37 +44,37 @@ std::unique_ptr<base::Value> ReadTestJson(const std::string& filename) { ...@@ -44,37 +44,37 @@ std::unique_ptr<base::Value> ReadTestJson(const std::string& filename) {
namespace onc { namespace onc {
TEST(ONCDecrypterTest, BrokenEncryptionIterations) { TEST(ONCDecrypterTest, BrokenEncryptionIterations) {
std::unique_ptr<base::DictionaryValue> encrypted_onc = std::unique_ptr<base::Value> encrypted_onc =
test_utils::ReadTestDictionary("broken-encrypted-iterations.onc"); test_utils::ReadTestDictionary("broken-encrypted-iterations.onc");
std::unique_ptr<base::DictionaryValue> decrypted_onc = std::unique_ptr<base::Value> decrypted_onc =
Decrypt("test0000", *encrypted_onc); Decrypt("test0000", *encrypted_onc);
EXPECT_EQ(NULL, decrypted_onc.get()); EXPECT_EQ(nullptr, decrypted_onc.get());
} }
TEST(ONCDecrypterTest, BrokenEncryptionZeroIterations) { TEST(ONCDecrypterTest, BrokenEncryptionZeroIterations) {
std::unique_ptr<base::DictionaryValue> encrypted_onc = std::unique_ptr<base::Value> encrypted_onc =
test_utils::ReadTestDictionary("broken-encrypted-zero-iterations.onc"); test_utils::ReadTestDictionary("broken-encrypted-zero-iterations.onc");
std::string error; std::string error;
std::unique_ptr<base::DictionaryValue> decrypted_onc = std::unique_ptr<base::Value> decrypted_onc =
Decrypt("test0000", *encrypted_onc); Decrypt("test0000", *encrypted_onc);
EXPECT_EQ(NULL, decrypted_onc.get()); EXPECT_EQ(nullptr, decrypted_onc.get());
} }
TEST(ONCDecrypterTest, LoadEncryptedOnc) { TEST(ONCDecrypterTest, LoadEncryptedOnc) {
std::unique_ptr<base::DictionaryValue> encrypted_onc = std::unique_ptr<base::Value> encrypted_onc =
test_utils::ReadTestDictionary("encrypted.onc"); test_utils::ReadTestDictionary("encrypted.onc");
std::unique_ptr<base::DictionaryValue> expected_decrypted_onc = std::unique_ptr<base::Value> expected_decrypted_onc =
test_utils::ReadTestDictionary("decrypted.onc"); test_utils::ReadTestDictionary("decrypted.onc");
std::string error; std::string error;
std::unique_ptr<base::DictionaryValue> actual_decrypted_onc = std::unique_ptr<base::Value> actual_decrypted_onc =
Decrypt("test0000", *encrypted_onc); Decrypt("test0000", *encrypted_onc);
base::DictionaryValue emptyDict; base::Value emptyDict;
EXPECT_TRUE(test_utils::Equals(expected_decrypted_onc.get(), EXPECT_TRUE(test_utils::Equals(expected_decrypted_onc.get(),
actual_decrypted_onc.get())); actual_decrypted_onc.get()));
} }
...@@ -216,8 +216,8 @@ TEST(ONCUtils, ProxyConfigToOncProxySettings) { ...@@ -216,8 +216,8 @@ TEST(ONCUtils, ProxyConfigToOncProxySettings) {
test_case->GetDictionary("ONC_ProxySettings", &onc_proxy_settings); test_case->GetDictionary("ONC_ProxySettings", &onc_proxy_settings);
std::unique_ptr<base::DictionaryValue> actual_proxy_settings = std::unique_ptr<base::DictionaryValue> actual_proxy_settings =
ConvertProxyConfigToOncProxySettings( base::DictionaryValue::From(ConvertProxyConfigToOncProxySettings(
shill_proxy_config->CreateDeepCopy()); shill_proxy_config->CreateDeepCopy()));
EXPECT_TRUE( EXPECT_TRUE(
test_utils::Equals(onc_proxy_settings, actual_proxy_settings.get())); test_utils::Equals(onc_proxy_settings, actual_proxy_settings.get()));
} }
......
...@@ -28,7 +28,7 @@ class ONCValidatorTest : public ::testing::Test { ...@@ -28,7 +28,7 @@ class ONCValidatorTest : public ::testing::Test {
// validation is stored, so that expectations can be checked afterwards using // validation is stored, so that expectations can be checked afterwards using
// one of the Expect* functions below. // one of the Expect* functions below.
void Validate(bool strict, void Validate(bool strict,
std::unique_ptr<base::DictionaryValue> onc_object, std::unique_ptr<base::Value> onc_object,
const OncValueSignature* signature, const OncValueSignature* signature,
bool managed_onc, bool managed_onc,
::onc::ONCSource onc_source) { ::onc::ONCSource onc_source) {
...@@ -41,7 +41,7 @@ class ONCValidatorTest : public ::testing::Test { ...@@ -41,7 +41,7 @@ class ONCValidatorTest : public ::testing::Test {
validator.reset(new Validator(false, false, false, managed_onc)); validator.reset(new Validator(false, false, false, managed_onc));
} }
validator->SetOncSource(onc_source); validator->SetOncSource(onc_source);
original_object_ = std::move(onc_object); original_object_ = base::DictionaryValue::From(std::move(onc_object));
repaired_object_ = validator->ValidateAndRepairObject(signature, repaired_object_ = validator->ValidateAndRepairObject(signature,
*original_object_, *original_object_,
&validation_result_); &validation_result_);
......
...@@ -197,7 +197,7 @@ std::unique_ptr<NetworkUIData> GetUIDataFromValue( ...@@ -197,7 +197,7 @@ std::unique_ptr<NetworkUIData> GetUIDataFromValue(
return std::unique_ptr<NetworkUIData>(); return std::unique_ptr<NetworkUIData>();
if (ui_data_str.empty()) if (ui_data_str.empty())
return std::make_unique<NetworkUIData>(); return std::make_unique<NetworkUIData>();
std::unique_ptr<base::DictionaryValue> ui_data_dict( std::unique_ptr<base::Value> ui_data_dict(
chromeos::onc::ReadDictionaryFromJson(ui_data_str)); chromeos::onc::ReadDictionaryFromJson(ui_data_str));
if (!ui_data_dict) if (!ui_data_dict)
return std::unique_ptr<NetworkUIData>(); return std::unique_ptr<NetworkUIData>();
......
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