Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/scoped_ptr.h" | |
| 6 #include "base/string16.h" | |
| 7 #include "base/utf_string_conversions.h" | |
| 8 #include "chrome/browser/policy/configuration_policy_pref_store.h" | |
| 9 #include "chrome/browser/policy/configuration_policy_reader.h" | |
| 10 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | |
| 11 #include "chrome/browser/policy/mock_configuration_policy_reader.h" | |
| 12 #include "policy/policy_constants.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 namespace policy { | |
| 16 | |
| 17 class ConfigurationPolicyReaderTest : public testing::Test { | |
| 18 protected: | |
| 19 ConfigurationPolicyReaderTest() : provider_() { | |
| 20 managed_reader_.reset(new ConfigurationPolicyReader(&provider_, | |
| 21 PolicyStatusInfo::MANDATORY)); | |
|
Mattias Nissler (ping if slow)
2011/08/29 11:24:40
indentation
| |
| 22 recommended_reader_.reset(new ConfigurationPolicyReader(&provider_, | |
| 23 PolicyStatusInfo::RECOMMENDED)); | |
|
Mattias Nissler (ping if slow)
2011/08/29 11:24:40
indentation
| |
| 24 status_ok_ = ASCIIToUTF16("ok"); | |
| 25 } | |
| 26 | |
| 27 DictionaryValue* CreateDictionary(const char* policy_name, | |
| 28 Value* policy_value) { | |
| 29 DictionaryValue* dict = new DictionaryValue(); | |
| 30 dict->SetString( | |
| 31 PolicyStatusInfo::name_dict_path, ASCIIToUTF16(policy_name)); | |
| 32 dict->SetString(PolicyStatusInfo::level_dict_path, | |
| 33 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::MANDATORY)); | |
| 34 dict->SetString(PolicyStatusInfo::source_type_dict_path, | |
| 35 PolicyStatusInfo::GetSourceTypeString(PolicyStatusInfo::USER)); | |
| 36 dict->Set(PolicyStatusInfo::value_dict_path, policy_value); | |
| 37 dict->SetBoolean(PolicyStatusInfo::set_dict_path, true); | |
| 38 dict->SetString(PolicyStatusInfo::status_dict_path, status_ok_); | |
| 39 | |
| 40 return dict; | |
| 41 } | |
| 42 | |
| 43 MockConfigurationPolicyProvider provider_; | |
| 44 scoped_ptr<ConfigurationPolicyReader> managed_reader_; | |
| 45 scoped_ptr<ConfigurationPolicyReader> recommended_reader_; | |
| 46 string16 status_ok_; | |
| 47 }; | |
| 48 | |
| 49 TEST_F(ConfigurationPolicyReaderTest, GetDefault) { | |
| 50 EXPECT_EQ(NULL, managed_reader_->GetPolicyStatus(kPolicyHomepageLocation)); | |
| 51 } | |
| 52 | |
| 53 // Test for list-valued policy settings. | |
| 54 TEST_F(ConfigurationPolicyReaderTest, SetListValue) { | |
| 55 ListValue* in_value = new ListValue(); | |
| 56 in_value->Append(Value::CreateStringValue("test1")); | |
| 57 in_value->Append(Value::CreateStringValue("test2")); | |
| 58 provider_.AddPolicy(kPolicyRestoreOnStartupURLs, in_value); | |
| 59 managed_reader_->OnUpdatePolicy(); | |
| 60 | |
| 61 scoped_ptr<DictionaryValue> | |
| 62 dict(CreateDictionary(key::kRestoreOnStartupURLs, in_value->DeepCopy())); | |
| 63 EXPECT_TRUE( | |
| 64 dict->Equals(managed_reader_-> | |
| 65 GetPolicyStatus(kPolicyRestoreOnStartupURLs))); | |
|
Mattias Nissler (ping if slow)
2011/08/29 11:24:40
another 4 spaces indentation (or break after the o
| |
| 66 | |
| 67 recommended_reader_->OnUpdatePolicy(); | |
| 68 dict->SetString("level", | |
| 69 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); | |
| 70 EXPECT_TRUE(dict->Equals( | |
| 71 recommended_reader_->GetPolicyStatus(kPolicyRestoreOnStartupURLs))); | |
| 72 } | |
| 73 | |
| 74 // Test for string-valued policy settings. | |
| 75 TEST_F(ConfigurationPolicyReaderTest, SetStringValue) { | |
| 76 provider_.AddPolicy(kPolicyHomepageLocation, | |
| 77 Value::CreateStringValue("http://chromium.org")); | |
| 78 managed_reader_->OnUpdatePolicy(); | |
| 79 scoped_ptr<DictionaryValue> dict(CreateDictionary(key::kHomepageLocation, | |
| 80 Value::CreateStringValue("http://chromium.org"))); | |
| 81 EXPECT_TRUE( | |
| 82 dict->Equals(managed_reader_->GetPolicyStatus(kPolicyHomepageLocation))); | |
| 83 | |
| 84 recommended_reader_->OnUpdatePolicy(); | |
| 85 dict->SetString("level", | |
| 86 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); | |
| 87 EXPECT_TRUE(dict->Equals( | |
| 88 recommended_reader_->GetPolicyStatus(kPolicyHomepageLocation))); | |
| 89 } | |
| 90 | |
| 91 // Test for boolean-valued policy settings. | |
| 92 TEST_F(ConfigurationPolicyReaderTest, SetBooleanValue) { | |
| 93 provider_.AddPolicy(kPolicyShowHomeButton, Value::CreateBooleanValue(true)); | |
| 94 managed_reader_->OnUpdatePolicy(); | |
| 95 scoped_ptr<DictionaryValue> dict(CreateDictionary(key::kShowHomeButton, | |
| 96 Value::CreateBooleanValue(true))); | |
| 97 EXPECT_TRUE(dict->Equals( | |
| 98 managed_reader_->GetPolicyStatus(kPolicyShowHomeButton))); | |
| 99 | |
| 100 recommended_reader_->OnUpdatePolicy(); | |
| 101 dict->SetString("level", | |
| 102 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); | |
| 103 EXPECT_TRUE(dict->Equals( | |
| 104 recommended_reader_->GetPolicyStatus(kPolicyShowHomeButton))); | |
| 105 | |
| 106 provider_.AddPolicy(kPolicyShowHomeButton, Value::CreateBooleanValue(false)); | |
| 107 managed_reader_->OnUpdatePolicy(); | |
| 108 dict->Set( | |
| 109 PolicyStatusInfo::value_dict_path, Value::CreateBooleanValue(false)); | |
| 110 dict->SetString("level", | |
| 111 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::MANDATORY)); | |
| 112 EXPECT_TRUE( | |
| 113 dict->Equals(managed_reader_->GetPolicyStatus(kPolicyShowHomeButton))); | |
| 114 | |
| 115 recommended_reader_->OnUpdatePolicy(); | |
| 116 dict->SetString("level", | |
| 117 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); | |
| 118 EXPECT_TRUE(dict->Equals( | |
| 119 recommended_reader_->GetPolicyStatus(kPolicyShowHomeButton))); | |
| 120 } | |
| 121 | |
| 122 // Test for integer-valued policy settings. | |
| 123 TEST_F(ConfigurationPolicyReaderTest, SetValue) { | |
| 124 provider_.AddPolicy(kPolicyRestoreOnStartup, Value::CreateIntegerValue(3)); | |
| 125 managed_reader_->OnUpdatePolicy(); | |
| 126 scoped_ptr<DictionaryValue> dict(CreateDictionary(key::kRestoreOnStartup, | |
| 127 Value::CreateIntegerValue(3))); | |
| 128 EXPECT_TRUE(dict->Equals( | |
| 129 managed_reader_->GetPolicyStatus(kPolicyRestoreOnStartup))); | |
| 130 | |
| 131 recommended_reader_->OnUpdatePolicy(); | |
| 132 dict->SetString("level", | |
| 133 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); | |
| 134 EXPECT_TRUE(dict->Equals( | |
| 135 recommended_reader_->GetPolicyStatus(kPolicyRestoreOnStartup))); | |
| 136 } | |
| 137 | |
| 138 class PolicyStatusTest : public testing::Test { | |
| 139 protected: | |
| 140 typedef ConfigurationPolicyProvider::PolicyDefinitionList | |
| 141 PolicyDefinitionList; | |
| 142 | |
| 143 PolicyStatusTest() | |
| 144 : managed_platform_provider_(), | |
| 145 managed_cloud_provider_(), | |
| 146 recommended_platform_provider_(), | |
| 147 recommended_cloud_provider_() { | |
| 148 managed_platform_ = | |
| 149 new MockConfigurationPolicyReader(&managed_platform_provider_, | |
| 150 PolicyStatusInfo::MANDATORY); | |
| 151 managed_cloud_ = | |
| 152 new MockConfigurationPolicyReader(&managed_cloud_provider_, | |
| 153 PolicyStatusInfo::MANDATORY); | |
| 154 recommended_platform_ = | |
| 155 new MockConfigurationPolicyReader(&recommended_platform_provider_, | |
| 156 PolicyStatusInfo::RECOMMENDED); | |
| 157 recommended_cloud_ = | |
| 158 new MockConfigurationPolicyReader(&recommended_cloud_provider_, | |
| 159 PolicyStatusInfo::RECOMMENDED); | |
| 160 | |
| 161 policy_status_.reset(new PolicyStatus(managed_platform_, | |
| 162 managed_cloud_, | |
| 163 recommended_platform_, | |
| 164 recommended_cloud_)); | |
| 165 policy_list_ = | |
| 166 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(); | |
| 167 status_ok_ = ASCIIToUTF16("ok"); | |
| 168 } | |
| 169 | |
| 170 void UpdatePolicyReaders() { | |
| 171 managed_platform_->OnUpdatePolicy(); | |
| 172 managed_cloud_->OnUpdatePolicy(); | |
| 173 recommended_platform_->OnUpdatePolicy(); | |
| 174 recommended_cloud_->OnUpdatePolicy(); | |
| 175 } | |
| 176 | |
| 177 void SetDictionaryPaths(DictionaryValue* dict, | |
| 178 const char* policy_name, | |
| 179 bool defined, | |
| 180 PolicyStatusInfo::PolicyLevel level) { | |
| 181 dict->SetString(PolicyStatusInfo::name_dict_path, | |
| 182 ASCIIToUTF16(policy_name)); | |
| 183 if (defined) { | |
| 184 dict->SetString(PolicyStatusInfo::level_dict_path, | |
| 185 PolicyStatusInfo::GetPolicyLevelString(level)); | |
| 186 } | |
| 187 } | |
| 188 | |
| 189 MockConfigurationPolicyProvider managed_platform_provider_; | |
|
Mattias Nissler (ping if slow)
2011/08/29 11:24:40
Why do you still need providers? I think you can j
| |
| 190 MockConfigurationPolicyProvider managed_cloud_provider_; | |
| 191 MockConfigurationPolicyProvider recommended_platform_provider_; | |
| 192 MockConfigurationPolicyProvider recommended_cloud_provider_; | |
| 193 MockConfigurationPolicyReader* managed_platform_; | |
| 194 MockConfigurationPolicyReader* managed_cloud_; | |
| 195 MockConfigurationPolicyReader* recommended_platform_; | |
| 196 MockConfigurationPolicyReader* recommended_cloud_; | |
| 197 scoped_ptr<PolicyStatus> policy_status_; | |
| 198 const PolicyDefinitionList* policy_list_; | |
| 199 string16 status_ok_; | |
| 200 }; | |
| 201 | |
| 202 TEST_F(PolicyStatusTest, GetPolicyStatusList) { | |
| 203 bool any_policies_sent; | |
| 204 scoped_ptr<ListValue> status_list( | |
| 205 policy_status_->GetPolicyStatusList(&any_policies_sent)); | |
| 206 | |
| 207 EXPECT_FALSE(any_policies_sent); | |
| 208 | |
| 209 size_t policy_list_size = | |
| 210 static_cast<size_t>(policy_list_->end - policy_list_->begin); | |
| 211 | |
| 212 EXPECT_EQ(policy_list_size, status_list->GetSize()); | |
| 213 | |
| 214 managed_platform_provider_.AddPolicy(kPolicyInstantEnabled, | |
| 215 Value::CreateBooleanValue(true)); | |
| 216 managed_cloud_provider_.AddPolicy(kPolicyDisablePluginFinder, | |
| 217 Value::CreateBooleanValue(true)); | |
| 218 recommended_platform_provider_.AddPolicy(kPolicySyncDisabled, | |
| 219 Value::CreateBooleanValue(true)); | |
| 220 recommended_cloud_provider_.AddPolicy(kPolicyTranslateEnabled, | |
| 221 Value::CreateBooleanValue(true)); | |
| 222 UpdatePolicyReaders(); | |
| 223 | |
| 224 status_list.reset(policy_status_->GetPolicyStatusList(&any_policies_sent)); | |
| 225 EXPECT_TRUE(any_policies_sent); | |
| 226 EXPECT_EQ(policy_list_size, status_list->GetSize()); | |
| 227 | |
| 228 scoped_ptr<DictionaryValue> undefined_dict(new DictionaryValue()); | |
| 229 undefined_dict->SetString(PolicyStatusInfo::level_dict_path, | |
| 230 PolicyStatusInfo::GetPolicyLevelString( | |
| 231 PolicyStatusInfo::LEVEL_UNDEFINED)); | |
| 232 undefined_dict->SetString(PolicyStatusInfo::source_type_dict_path, | |
| 233 PolicyStatusInfo::GetSourceTypeString( | |
| 234 PolicyStatusInfo::SOURCE_TYPE_UNDEFINED)); | |
| 235 undefined_dict->Set(PolicyStatusInfo::value_dict_path, | |
| 236 Value::CreateNullValue()); | |
| 237 undefined_dict->SetBoolean(PolicyStatusInfo::set_dict_path, false); | |
| 238 undefined_dict->SetString(PolicyStatusInfo::status_dict_path, string16()); | |
| 239 | |
| 240 scoped_ptr<DictionaryValue> defined_dict(new DictionaryValue()); | |
| 241 defined_dict->SetString(PolicyStatusInfo::source_type_dict_path, | |
| 242 PolicyStatusInfo::GetSourceTypeString( | |
| 243 PolicyStatusInfo::USER)); | |
| 244 defined_dict->Set(PolicyStatusInfo::value_dict_path, | |
| 245 Value::CreateBooleanValue(true)); | |
| 246 defined_dict->SetBoolean(PolicyStatusInfo::set_dict_path, true); | |
| 247 defined_dict->SetString(PolicyStatusInfo::status_dict_path, status_ok_); | |
| 248 | |
| 249 for (const PolicyDefinitionList::Entry* entry = policy_list_->begin; | |
| 250 entry != policy_list_->end; ++entry) { | |
| 251 Value* status_dict = NULL; | |
| 252 | |
| 253 // Every policy in |policy_list_| has to appear in the returned ListValue. | |
| 254 string16 name = ASCIIToUTF16(entry->name); | |
| 255 for (ListValue::const_iterator status_entry = status_list->begin(); | |
| 256 status_entry != status_list->end(); | |
| 257 ++status_entry) { | |
| 258 string16 value; | |
| 259 ASSERT_TRUE((*status_entry)->IsType(Value::TYPE_DICTIONARY)); | |
| 260 DictionaryValue* dict = static_cast<DictionaryValue*>(*status_entry); | |
| 261 ASSERT_TRUE(dict->GetString(PolicyStatusInfo::name_dict_path, &value)); | |
| 262 | |
| 263 if (value == name) | |
| 264 status_dict = *status_entry; | |
| 265 } | |
| 266 | |
| 267 ASSERT_FALSE(status_dict == NULL); | |
| 268 | |
| 269 switch (entry->policy_type) { | |
| 270 case kPolicyInstantEnabled: | |
| 271 SetDictionaryPaths(defined_dict.get(), | |
| 272 entry->name, | |
| 273 true, | |
| 274 PolicyStatusInfo::MANDATORY); | |
| 275 EXPECT_TRUE(defined_dict->Equals(status_dict)); | |
| 276 break; | |
| 277 case kPolicyDisablePluginFinder: | |
| 278 SetDictionaryPaths(defined_dict.get(), | |
| 279 entry->name, | |
| 280 true, | |
| 281 PolicyStatusInfo::MANDATORY); | |
| 282 EXPECT_TRUE(defined_dict->Equals(status_dict)); | |
| 283 break; | |
| 284 case kPolicySyncDisabled: | |
| 285 SetDictionaryPaths(defined_dict.get(), | |
| 286 entry->name, | |
| 287 true, | |
| 288 PolicyStatusInfo::RECOMMENDED); | |
| 289 EXPECT_TRUE(defined_dict->Equals(status_dict)); | |
| 290 break; | |
| 291 case kPolicyTranslateEnabled: | |
| 292 SetDictionaryPaths(defined_dict.get(), | |
| 293 entry->name, | |
| 294 true, | |
| 295 PolicyStatusInfo::RECOMMENDED); | |
| 296 EXPECT_TRUE(defined_dict->Equals(status_dict)); | |
| 297 break; | |
| 298 default: | |
| 299 SetDictionaryPaths(undefined_dict.get(), | |
| 300 entry->name, | |
| 301 false, | |
| 302 PolicyStatusInfo::LEVEL_UNDEFINED); | |
| 303 EXPECT_TRUE(undefined_dict->Equals(status_dict)); | |
| 304 break; | |
| 305 } | |
| 306 } | |
| 307 } | |
| 308 | |
| 309 TEST_F(PolicyStatusTest, GetPolicyName) { | |
| 310 for (const PolicyDefinitionList::Entry* entry = policy_list_->begin; | |
| 311 entry != policy_list_->end; ++entry) { | |
| 312 EXPECT_EQ(ASCIIToUTF16(entry->name), | |
| 313 PolicyStatus::GetPolicyName(entry->policy_type)); | |
| 314 } | |
| 315 } | |
| 316 | |
| 317 } // namespace policy | |
| OLD | NEW |