Chromium Code Reviews| Index: chrome/browser/policy/configuration_policy_reader_unittest.cc |
| diff --git a/chrome/browser/policy/configuration_policy_reader_unittest.cc b/chrome/browser/policy/configuration_policy_reader_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9f9fe4315bcc1b53068cdc427dfdc44e7c7c0b7c |
| --- /dev/null |
| +++ b/chrome/browser/policy/configuration_policy_reader_unittest.cc |
| @@ -0,0 +1,316 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/scoped_ptr.h" |
| +#include "base/string16.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "chrome/browser/policy/configuration_policy_pref_store.h" |
| +#include "chrome/browser/policy/configuration_policy_reader.h" |
| +#include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| +#include "policy/policy_constants.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace policy { |
| + |
| +class ConfigurationPolicyReaderTest : public testing::Test { |
| + protected: |
| + ConfigurationPolicyReaderTest() : provider_() { |
| + managed_reader_.reset(new ConfigurationPolicyReader(&provider_, |
| + PolicyStatusInfo::MANDATORY)); |
| + recommended_reader_.reset(new ConfigurationPolicyReader(&provider_, |
| + PolicyStatusInfo::RECOMMENDED)); |
| + status_ok_ = ASCIIToUTF16("ok"); |
| + } |
| + |
| + DictionaryValue* CreateDictionary(const char* policy_name) { |
|
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
How about accepting an additional parameter to set
simo
2011/08/25 13:29:47
Done.
|
| + DictionaryValue* dict = new DictionaryValue(); |
| + dict->SetString( |
| + PolicyStatusInfo::name_dict_path, ASCIIToUTF16(policy_name)); |
| + dict->SetString(PolicyStatusInfo::level_dict_path, |
| + PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::MANDATORY)); |
| + dict->SetString(PolicyStatusInfo::source_type_dict_path, |
| + PolicyStatusInfo::GetSourceTypeString(PolicyStatusInfo::USER)); |
| + dict->SetBoolean(PolicyStatusInfo::set_dict_path, true); |
| + dict->SetString(PolicyStatusInfo::status_dict_path, status_ok_); |
| + |
| + return dict; |
| + } |
| + |
| + MockConfigurationPolicyProvider provider_; |
| + scoped_ptr<ConfigurationPolicyReader> managed_reader_; |
| + scoped_ptr<ConfigurationPolicyReader> recommended_reader_; |
| + string16 status_ok_; |
| +}; |
| + |
| +TEST_F(ConfigurationPolicyReaderTest, GetDefault) { |
| + EXPECT_EQ(NULL, managed_reader_->GetPolicyStatus(kPolicyHomepageLocation)); |
| +} |
| + |
| +// Test for list-valued policy settings. |
| +TEST_F(ConfigurationPolicyReaderTest, SetListValue) { |
| + ListValue* in_value = new ListValue(); |
| + in_value->Append(Value::CreateStringValue("test1")); |
| + in_value->Append(Value::CreateStringValue("test2")); |
| + provider_.AddPolicy(kPolicyRestoreOnStartupURLs, in_value); |
| + managed_reader_->OnUpdatePolicy(); |
| + |
| + scoped_ptr<DictionaryValue> |
| + dict(CreateDictionary(key::kRestoreOnStartupURLs)); |
| + dict->Set(PolicyStatusInfo::value_dict_path, in_value->DeepCopy()); |
| + EXPECT_TRUE( |
| + dict->Equals(managed_reader_-> |
| + GetPolicyStatus(kPolicyRestoreOnStartupURLs))); |
| + |
| + recommended_reader_->OnUpdatePolicy(); |
| + dict->SetString("level", |
| + PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); |
| + EXPECT_TRUE(dict->Equals( |
| + recommended_reader_->GetPolicyStatus(kPolicyRestoreOnStartupURLs))); |
| +} |
| + |
| +// Test for string-valued policy settings. |
| +TEST_F(ConfigurationPolicyReaderTest, SetStringValue) { |
| + provider_.AddPolicy(kPolicyHomepageLocation, |
| + Value::CreateStringValue("http://chromium.org")); |
| + managed_reader_->OnUpdatePolicy(); |
| + scoped_ptr<DictionaryValue> dict(CreateDictionary(key::kHomepageLocation)); |
| + dict->Set(PolicyStatusInfo::value_dict_path, |
| + Value::CreateStringValue("http://chromium.org")); |
| + EXPECT_TRUE( |
| + dict->Equals(managed_reader_->GetPolicyStatus(kPolicyHomepageLocation))); |
| + |
| + recommended_reader_->OnUpdatePolicy(); |
| + dict->SetString("level", |
| + PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); |
| + EXPECT_TRUE(dict->Equals( |
| + recommended_reader_->GetPolicyStatus(kPolicyHomepageLocation))); |
| +} |
| + |
| +// Test for boolean-valued policy settings. |
| +TEST_F(ConfigurationPolicyReaderTest, SetBooleanValue) { |
| + provider_.AddPolicy(kPolicyShowHomeButton, Value::CreateBooleanValue(true)); |
| + managed_reader_->OnUpdatePolicy(); |
| + scoped_ptr<DictionaryValue> dict(CreateDictionary(key::kShowHomeButton)); |
| + dict->Set(PolicyStatusInfo::value_dict_path, Value::CreateBooleanValue(true)); |
| + EXPECT_TRUE(dict->Equals( |
| + managed_reader_->GetPolicyStatus(kPolicyShowHomeButton))); |
| + |
| + recommended_reader_->OnUpdatePolicy(); |
| + dict->SetString("level", |
| + PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); |
| + EXPECT_TRUE(dict->Equals( |
| + recommended_reader_->GetPolicyStatus(kPolicyShowHomeButton))); |
| + |
| + provider_.AddPolicy(kPolicyShowHomeButton, Value::CreateBooleanValue(false)); |
| + managed_reader_->OnUpdatePolicy(); |
| + dict->Set( |
| + PolicyStatusInfo::value_dict_path, Value::CreateBooleanValue(false)); |
| + dict->SetString("level", |
| + PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::MANDATORY)); |
| + EXPECT_TRUE( |
| + dict->Equals(managed_reader_->GetPolicyStatus(kPolicyShowHomeButton))); |
| + |
| + recommended_reader_->OnUpdatePolicy(); |
| + dict->SetString("level", |
| + PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); |
| + EXPECT_TRUE(dict->Equals( |
| + recommended_reader_->GetPolicyStatus(kPolicyShowHomeButton))); |
| +} |
| + |
| +// Test for integer-valued policy settings. |
| +TEST_F(ConfigurationPolicyReaderTest, SetValue) { |
| + provider_.AddPolicy(kPolicyRestoreOnStartup, Value::CreateIntegerValue(3)); |
| + managed_reader_->OnUpdatePolicy(); |
| + scoped_ptr<DictionaryValue> dict(CreateDictionary(key::kRestoreOnStartup)); |
| + dict->Set(PolicyStatusInfo::value_dict_path, Value::CreateIntegerValue(3)); |
| + |
| + EXPECT_TRUE(dict->Equals( |
| + managed_reader_->GetPolicyStatus(kPolicyRestoreOnStartup))); |
| + |
| + recommended_reader_->OnUpdatePolicy(); |
| + dict->SetString("level", |
| + PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); |
| + EXPECT_TRUE(dict->Equals( |
| + recommended_reader_->GetPolicyStatus(kPolicyRestoreOnStartup))); |
| +} |
| + |
| +class PolicyStatusTest : public testing::Test { |
| + protected: |
| + typedef ConfigurationPolicyProvider::PolicyDefinitionList |
| + PolicyDefinitionList; |
| + |
| + PolicyStatusTest() |
| + : managed_platform_provider_(), |
|
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
4 spaces of indentation
simo
2011/08/25 13:29:47
Done.
|
| + managed_cloud_provider_(), |
| + recommended_platform_provider_(), |
| + recommended_cloud_provider_() { |
| + managed_platform_ = |
| + new ConfigurationPolicyReader(&managed_platform_provider_, |
|
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
Instead of actual ConfigurationPolicyReaders, can
simo
2011/08/25 13:29:47
Done.
|
| + PolicyStatusInfo::MANDATORY); |
| + managed_cloud_ = |
| + new ConfigurationPolicyReader(&managed_cloud_provider_, |
| + PolicyStatusInfo::MANDATORY); |
| + recommended_platform_ = |
| + new ConfigurationPolicyReader(&recommended_platform_provider_, |
| + PolicyStatusInfo::RECOMMENDED); |
| + recommended_cloud_ = |
| + new ConfigurationPolicyReader(&recommended_cloud_provider_, |
| + PolicyStatusInfo::RECOMMENDED); |
| + |
| + policy_status_.reset( new PolicyStatus(managed_platform_, |
|
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
no space after (
simo
2011/08/25 13:29:47
Done.
|
| + managed_cloud_, |
| + recommended_platform_, |
| + recommended_cloud_)); |
| + policy_list_ = |
| + ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(); |
| + status_ok_ = ASCIIToUTF16("ok"); |
| + } |
| + |
| + void UpdatePolicyReaders() { |
| + managed_platform_->OnUpdatePolicy(); |
| + managed_cloud_->OnUpdatePolicy(); |
| + recommended_platform_->OnUpdatePolicy(); |
| + recommended_cloud_->OnUpdatePolicy(); |
| + } |
| + |
| + void SetDictionaryPaths(DictionaryValue* dict, |
| + const char* policy_name, |
| + bool defined, |
| + PolicyStatusInfo::PolicyLevel level) { |
| + dict->SetString(PolicyStatusInfo::name_dict_path, |
| + ASCIIToUTF16(policy_name)); |
| + if (defined) |
|
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
need braces here (multi-line conditional block)
simo
2011/08/25 13:29:47
Done.
|
| + dict->SetString(PolicyStatusInfo::level_dict_path, |
| + PolicyStatusInfo::GetPolicyLevelString(level)); |
| + } |
| + |
| + MockConfigurationPolicyProvider managed_platform_provider_; |
| + MockConfigurationPolicyProvider managed_cloud_provider_; |
| + MockConfigurationPolicyProvider recommended_platform_provider_; |
| + MockConfigurationPolicyProvider recommended_cloud_provider_; |
| + ConfigurationPolicyReader* managed_platform_; |
|
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
I think you're leaking these, you might want to wr
simo
2011/08/25 13:29:47
They are passed in the the PolicyStatus object in
|
| + ConfigurationPolicyReader* managed_cloud_; |
| + ConfigurationPolicyReader* recommended_platform_; |
| + ConfigurationPolicyReader* recommended_cloud_; |
| + scoped_ptr<PolicyStatus> policy_status_; |
| + const PolicyDefinitionList* policy_list_; |
| + string16 status_ok_; |
| +}; |
| + |
| +TEST_F(PolicyStatusTest, GetPolicyStatusList) { |
| + bool any_policies_sent; |
| + scoped_ptr<ListValue> status_list( |
| + policy_status_->GetPolicyStatusList(&any_policies_sent)); |
| + |
| + EXPECT_FALSE(any_policies_sent); |
| + |
| + size_t policy_list_size = |
| + static_cast<size_t>(policy_list_->end - policy_list_->begin); |
| + |
| + EXPECT_EQ(policy_list_size, status_list->GetSize()); |
| + |
| + managed_platform_provider_.AddPolicy(kPolicyInstantEnabled, |
| + Value::CreateBooleanValue(true)); |
| + managed_cloud_provider_.AddPolicy(kPolicyDisablePluginFinder, |
| + Value::CreateBooleanValue(true)); |
| + recommended_platform_provider_.AddPolicy(kPolicySyncDisabled, |
| + Value::CreateBooleanValue(true)); |
| + recommended_cloud_provider_.AddPolicy(kPolicyTranslateEnabled, |
| + Value::CreateBooleanValue(true)); |
| + UpdatePolicyReaders(); |
| + |
| + status_list.reset(policy_status_->GetPolicyStatusList(&any_policies_sent)); |
| + EXPECT_TRUE(any_policies_sent); |
| + EXPECT_EQ(policy_list_size, status_list->GetSize()); |
| + |
| + scoped_ptr<DictionaryValue> undefined_dict(new DictionaryValue()); |
| + undefined_dict->SetString(PolicyStatusInfo::level_dict_path, |
| + PolicyStatusInfo::GetPolicyLevelString( |
| + PolicyStatusInfo::LEVEL_UNDEFINED)); |
|
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
+4 spaces identation
simo
2011/08/25 13:29:47
Done.
|
| + undefined_dict->SetString(PolicyStatusInfo::source_type_dict_path, |
| + PolicyStatusInfo::GetSourceTypeString( |
| + PolicyStatusInfo::SOURCE_TYPE_UNDEFINED)); |
|
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
same here
simo
2011/08/25 13:29:47
Done.
|
| + undefined_dict->Set(PolicyStatusInfo::value_dict_path, |
| + Value::CreateNullValue()); |
| + undefined_dict->SetBoolean(PolicyStatusInfo::set_dict_path, false); |
| + undefined_dict->SetString(PolicyStatusInfo::status_dict_path, string16()); |
| + |
| + scoped_ptr<DictionaryValue> defined_dict(new DictionaryValue()); |
| + defined_dict->SetString(PolicyStatusInfo::source_type_dict_path, |
| + PolicyStatusInfo::GetSourceTypeString( |
| + PolicyStatusInfo::USER)); |
|
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
same here
simo
2011/08/25 13:29:47
Done.
|
| + defined_dict->Set(PolicyStatusInfo::value_dict_path, |
| + Value::CreateBooleanValue(true)); |
| + defined_dict->SetBoolean(PolicyStatusInfo::set_dict_path, true); |
| + defined_dict->SetString(PolicyStatusInfo::status_dict_path, status_ok_); |
| + |
| + for (const PolicyDefinitionList::Entry* entry = policy_list_->begin; |
| + entry != policy_list_->end; ++entry) { |
| + Value* status_dict = Value::CreateNullValue(); |
|
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
Can just use NULL here.
simo
2011/08/25 13:29:47
Done.
|
| + |
| + // Every policy in policy_list_ has to appear in the returned ListValue. |
|
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
it should be |policy_list_| (just convention for v
simo
2011/08/25 13:29:47
Done.
|
| + string16 name = ASCIIToUTF16(entry->name); |
| + for (ListValue::const_iterator status_entry = status_list->begin(); |
| + status_entry != status_list->end(); |
| + ++status_entry) { |
| + string16 value; |
| + ASSERT_TRUE((*status_entry)->IsType(Value::TYPE_DICTIONARY)); |
| + DictionaryValue* dict = static_cast<DictionaryValue*>(*status_entry); |
| + ASSERT_TRUE(dict->GetString(PolicyStatusInfo::name_dict_path, &value)); |
| + |
| + if (value == name) |
| + status_dict = *status_entry; |
| + } |
| + |
| + ASSERT_FALSE(status_dict->Equals(Value::CreateNullValue())); |
|
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
You leak the created null value.
|
| + |
| + switch (entry->policy_type) { |
| + case kPolicyInstantEnabled: |
| + SetDictionaryPaths(defined_dict.get(), |
| + entry->name, |
| + true, |
| + PolicyStatusInfo::MANDATORY); |
| + EXPECT_TRUE(defined_dict->Equals(status_dict)); |
| + break; |
| + case kPolicyDisablePluginFinder: |
| + SetDictionaryPaths(defined_dict.get(), |
| + entry->name, |
| + true, |
| + PolicyStatusInfo::MANDATORY); |
| + EXPECT_TRUE(defined_dict->Equals(status_dict)); |
| + break; |
| + case kPolicySyncDisabled: |
| + SetDictionaryPaths(defined_dict.get(), |
| + entry->name, |
| + true, |
| + PolicyStatusInfo::RECOMMENDED); |
| + EXPECT_TRUE(defined_dict->Equals(status_dict)); |
| + break; |
| + case kPolicyTranslateEnabled: |
| + SetDictionaryPaths(defined_dict.get(), |
| + entry->name, |
| + true, |
| + PolicyStatusInfo::RECOMMENDED); |
| + EXPECT_TRUE(defined_dict->Equals(status_dict)); |
| + break; |
| + default: |
| + SetDictionaryPaths(undefined_dict.get(), |
| + entry->name, |
| + false, |
| + PolicyStatusInfo::LEVEL_UNDEFINED); |
| + EXPECT_TRUE(undefined_dict->Equals(status_dict)); |
| + break; |
| + } |
| + } |
| +} |
| + |
| +TEST_F(PolicyStatusTest, GetPolicyName) { |
| + for (const PolicyDefinitionList::Entry* entry = policy_list_->begin; |
| + entry != policy_list_->end; ++entry) { |
| + EXPECT_EQ(ASCIIToUTF16(entry->name), |
| + PolicyStatus::GetPolicyName(entry->policy_type)); |
| + } |
| +} |
| + |
| +} // namespace policy |