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(
if (value) {
std::string 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);
if (root_dict.get() == NULL) {
if (!root_dict) {
errors->AddError(policy_name(), IDS_POLICY_NETWORK_CONFIG_PARSE_FAILED);
return false;
}
......@@ -289,9 +289,10 @@ NetworkConfigurationPolicyHandler::SanitizeNetworkConfig(
return NULL;
std::unique_ptr<base::DictionaryValue> toplevel_dict =
chromeos::onc::ReadDictionaryFromJson(json_string);
base::DictionaryValue::From(
chromeos::onc::ReadDictionaryFromJson(json_string));
if (!toplevel_dict)
return NULL;
return nullptr;
// Placeholder to insert in place of the filtered setting.
const char kPlaceholder[] = "********";
......
......@@ -248,17 +248,18 @@ class NetworkConfigurationUpdaterTest : public testing::Test {
providers.push_back(&provider_);
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);
base::DictionaryValue* global_config = NULL;
fake_toplevel_onc->GetDictionaryWithoutPathExpansion(
onc::toplevel_config::kGlobalNetworkConfiguration, &global_config);
base::DictionaryValue* global_config = nullptr;
fake_toplevel_onc
->FindKey(onc::toplevel_config::kGlobalNetworkConfiguration)
->GetAsDictionary(&global_config);
fake_global_network_config_.MergeDictionary(global_config);
base::ListValue* certs = NULL;
fake_toplevel_onc->GetListWithoutPathExpansion(
onc::toplevel_config::kCertificates, &certs);
base::ListValue* certs = nullptr;
fake_toplevel_onc->FindKey(onc::toplevel_config::kCertificates)
->GetAsList(&certs);
fake_certificates_ =
std::make_unique<chromeos::onc::OncParsedCertificates>(*certs);
......@@ -267,7 +268,7 @@ class NetworkConfigurationUpdaterTest : public testing::Test {
}
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);
fake_network_configs_ =
fake_toplevel_onc->FindKey(onc::toplevel_config::kNetworkConfigurations)
......
......@@ -514,8 +514,8 @@ TEST_F(ProxyConfigServiceImplTest, SharedEthernetAndUserPolicy) {
SetUpSharedEthernet();
SetUpProxyConfigService(&profile_prefs_);
std::unique_ptr<base::DictionaryValue> ethernet_policy(
chromeos::onc::ReadDictionaryFromJson(kEthernetPolicy));
std::unique_ptr<base::Value> ethernet_policy =
chromeos::onc::ReadDictionaryFromJson(kEthernetPolicy);
std::unique_ptr<base::ListValue> network_configs(new base::ListValue);
network_configs->Append(std::move(ethernet_policy));
......
......@@ -146,7 +146,8 @@ class ManagedNetworkConfigurationHandlerTest : public testing::Test {
const std::string& path_to_onc) {
std::unique_ptr<base::DictionaryValue> policy =
path_to_onc.empty()
? onc::ReadDictionaryFromJson(onc::kEmptyUnencryptedConfiguration)
? base::DictionaryValue::From(onc::ReadDictionaryFromJson(
onc::kEmptyUnencryptedConfiguration))
: test_utils::ReadTestDictionary(path_to_onc);
base::ListValue validated_network_configs;
......
......@@ -162,14 +162,11 @@ bool NetworkState::PropertyChanged(const std::string& key,
if (proxy_config_str.empty())
return true;
std::unique_ptr<base::DictionaryValue> proxy_config_dict(
std::unique_ptr<base::Value> proxy_config_dict(
onc::ReadDictionaryFromJson(proxy_config_str));
if (proxy_config_dict) {
// Warning: The DictionaryValue returned from
// ReadDictionaryFromJson/JSONParser is an optimized derived class that
// 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());
proxy_config_.MergeDictionary(
base::DictionaryValue::From(std::move(proxy_config_dict)).get());
} else {
NET_LOG(ERROR) << "Failed to parse " << path() << "." << key;
}
......
......@@ -62,7 +62,8 @@ std::string NetworkStateTest::ConfigureService(
last_created_service_path_ = "";
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) {
LOG(ERROR) << "Error parsing json: " << shill_json_string;
return last_created_service_path_;
......
......@@ -623,10 +623,10 @@ void ShillToONCTranslator::TranslateNetworkWithState() {
if (shill_dictionary_->GetStringWithoutPathExpansion(
shill::kProxyConfigProperty, &proxy_config_str) &&
!proxy_config_str.empty()) {
std::unique_ptr<base::DictionaryValue> proxy_config_value(
std::unique_ptr<base::Value> proxy_config_value(
ReadDictionaryFromJson(proxy_config_str));
if (proxy_config_value) {
std::unique_ptr<base::DictionaryValue> proxy_settings =
std::unique_ptr<base::Value> proxy_settings =
ConvertProxyConfigToOncProxySettings(std::move(proxy_config_value));
if (proxy_settings) {
onc_object_->SetWithoutPathExpansion(
......
This diff is collapsed.
......@@ -43,17 +43,17 @@ CHROMEOS_EXPORT extern const char kEmptyUnencryptedConfiguration[];
typedef std::map<std::string, std::string> CertPEMsByGUIDMap;
// Parses |json| according to the JSON format. If |json| is a JSON formatted
// dictionary, the function returns the dictionary as a DictionaryValue.
// Otherwise returns NULL.
CHROMEOS_EXPORT std::unique_ptr<base::DictionaryValue> ReadDictionaryFromJson(
// dictionary, the function returns the dictionary value, otherwise returns
// null.
CHROMEOS_EXPORT std::unique_ptr<base::Value> ReadDictionaryFromJson(
const std::string& json);
// Decrypts the given EncryptedConfiguration |onc| (see the ONC specification)
// using |passphrase|. The resulting UnencryptedConfiguration is returned. If an
// error occurs, returns NULL.
CHROMEOS_EXPORT std::unique_ptr<base::DictionaryValue> Decrypt(
// error occurs, returns null.
CHROMEOS_EXPORT std::unique_ptr<base::Value> Decrypt(
const std::string& passphrase,
const base::DictionaryValue& onc);
const base::Value& onc);
// For logging only: strings not user facing.
CHROMEOS_EXPORT std::string GetSourceAsString(::onc::ONCSource source);
......@@ -77,12 +77,12 @@ CHROMEOS_EXPORT void ExpandStringsInNetworks(
// specification. The object of |onc_object| is modified in place.
CHROMEOS_EXPORT void FillInHexSSIDFieldsInOncObject(
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
// SSID field to UTF-8 encoding, creates the hex representation and assigns the
// 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
// by |mask|. To find sensitive fields, signature and field name are checked
......@@ -146,9 +146,9 @@ ConvertOncProxySettingsToProxyConfig(
// Translates |proxy_config_value|, which must be a valid ProxyConfig 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(
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
// 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) {
namespace onc {
TEST(ONCDecrypterTest, BrokenEncryptionIterations) {
std::unique_ptr<base::DictionaryValue> encrypted_onc =
std::unique_ptr<base::Value> encrypted_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);
EXPECT_EQ(NULL, decrypted_onc.get());
EXPECT_EQ(nullptr, decrypted_onc.get());
}
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");
std::string error;
std::unique_ptr<base::DictionaryValue> decrypted_onc =
std::unique_ptr<base::Value> decrypted_onc =
Decrypt("test0000", *encrypted_onc);
EXPECT_EQ(NULL, decrypted_onc.get());
EXPECT_EQ(nullptr, decrypted_onc.get());
}
TEST(ONCDecrypterTest, LoadEncryptedOnc) {
std::unique_ptr<base::DictionaryValue> encrypted_onc =
std::unique_ptr<base::Value> 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");
std::string error;
std::unique_ptr<base::DictionaryValue> actual_decrypted_onc =
std::unique_ptr<base::Value> actual_decrypted_onc =
Decrypt("test0000", *encrypted_onc);
base::DictionaryValue emptyDict;
base::Value emptyDict;
EXPECT_TRUE(test_utils::Equals(expected_decrypted_onc.get(),
actual_decrypted_onc.get()));
}
......@@ -216,8 +216,8 @@ TEST(ONCUtils, ProxyConfigToOncProxySettings) {
test_case->GetDictionary("ONC_ProxySettings", &onc_proxy_settings);
std::unique_ptr<base::DictionaryValue> actual_proxy_settings =
ConvertProxyConfigToOncProxySettings(
shill_proxy_config->CreateDeepCopy());
base::DictionaryValue::From(ConvertProxyConfigToOncProxySettings(
shill_proxy_config->CreateDeepCopy()));
EXPECT_TRUE(
test_utils::Equals(onc_proxy_settings, actual_proxy_settings.get()));
}
......
......@@ -28,7 +28,7 @@ class ONCValidatorTest : public ::testing::Test {
// validation is stored, so that expectations can be checked afterwards using
// one of the Expect* functions below.
void Validate(bool strict,
std::unique_ptr<base::DictionaryValue> onc_object,
std::unique_ptr<base::Value> onc_object,
const OncValueSignature* signature,
bool managed_onc,
::onc::ONCSource onc_source) {
......@@ -41,7 +41,7 @@ class ONCValidatorTest : public ::testing::Test {
validator.reset(new Validator(false, false, false, managed_onc));
}
validator->SetOncSource(onc_source);
original_object_ = std::move(onc_object);
original_object_ = base::DictionaryValue::From(std::move(onc_object));
repaired_object_ = validator->ValidateAndRepairObject(signature,
*original_object_,
&validation_result_);
......
......@@ -197,7 +197,7 @@ std::unique_ptr<NetworkUIData> GetUIDataFromValue(
return std::unique_ptr<NetworkUIData>();
if (ui_data_str.empty())
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));
if (!ui_data_dict)
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