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 using ::testing::Return; |
| 16 using ::testing::ReturnNull; |
| 17 using ::testing::_; |
| 18 |
| 19 namespace policy { |
| 20 |
| 21 class ConfigurationPolicyReaderTest : public testing::Test { |
| 22 protected: |
| 23 ConfigurationPolicyReaderTest() : provider_() { |
| 24 managed_reader_.reset( |
| 25 new ConfigurationPolicyReader(&provider_, PolicyStatusInfo::MANDATORY)); |
| 26 recommended_reader_.reset(new ConfigurationPolicyReader(&provider_, |
| 27 PolicyStatusInfo::RECOMMENDED)); |
| 28 status_ok_ = ASCIIToUTF16("ok"); |
| 29 } |
| 30 |
| 31 DictionaryValue* CreateDictionary(const char* policy_name, |
| 32 Value* policy_value) { |
| 33 DictionaryValue* dict = new DictionaryValue(); |
| 34 dict->SetString( |
| 35 PolicyStatusInfo::kNameDictPath, ASCIIToUTF16(policy_name)); |
| 36 dict->SetString(PolicyStatusInfo::kLevelDictPath, |
| 37 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::MANDATORY)); |
| 38 dict->SetString(PolicyStatusInfo::kSourceTypeDictPath, |
| 39 PolicyStatusInfo::GetSourceTypeString(PolicyStatusInfo::USER)); |
| 40 dict->Set(PolicyStatusInfo::kValueDictPath, policy_value); |
| 41 dict->SetBoolean(PolicyStatusInfo::kSetDictPath, true); |
| 42 dict->SetString(PolicyStatusInfo::kStatusDictPath, status_ok_); |
| 43 |
| 44 return dict; |
| 45 } |
| 46 |
| 47 MockConfigurationPolicyProvider provider_; |
| 48 scoped_ptr<ConfigurationPolicyReader> managed_reader_; |
| 49 scoped_ptr<ConfigurationPolicyReader> recommended_reader_; |
| 50 string16 status_ok_; |
| 51 }; |
| 52 |
| 53 TEST_F(ConfigurationPolicyReaderTest, GetDefault) { |
| 54 EXPECT_EQ(NULL, managed_reader_->GetPolicyStatus(kPolicyHomepageLocation)); |
| 55 } |
| 56 |
| 57 // Test for list-valued policy settings. |
| 58 TEST_F(ConfigurationPolicyReaderTest, SetListValue) { |
| 59 ListValue* in_value = new ListValue(); |
| 60 in_value->Append(Value::CreateStringValue("test1")); |
| 61 in_value->Append(Value::CreateStringValue("test2")); |
| 62 provider_.AddPolicy(kPolicyRestoreOnStartupURLs, in_value); |
| 63 managed_reader_->OnUpdatePolicy(); |
| 64 |
| 65 scoped_ptr<DictionaryValue> |
| 66 dict(CreateDictionary(key::kRestoreOnStartupURLs, in_value->DeepCopy())); |
| 67 scoped_ptr<DictionaryValue> result( |
| 68 managed_reader_->GetPolicyStatus(kPolicyRestoreOnStartupURLs)); |
| 69 EXPECT_TRUE(dict->Equals(result.get())); |
| 70 |
| 71 recommended_reader_->OnUpdatePolicy(); |
| 72 dict->SetString("level", |
| 73 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); |
| 74 result.reset( |
| 75 recommended_reader_->GetPolicyStatus(kPolicyRestoreOnStartupURLs)); |
| 76 EXPECT_TRUE(dict->Equals(result.get())); |
| 77 } |
| 78 |
| 79 // Test for string-valued policy settings. |
| 80 TEST_F(ConfigurationPolicyReaderTest, SetStringValue) { |
| 81 provider_.AddPolicy(kPolicyHomepageLocation, |
| 82 Value::CreateStringValue("http://chromium.org")); |
| 83 managed_reader_->OnUpdatePolicy(); |
| 84 scoped_ptr<DictionaryValue> dict(CreateDictionary(key::kHomepageLocation, |
| 85 Value::CreateStringValue("http://chromium.org"))); |
| 86 scoped_ptr<DictionaryValue> result( |
| 87 managed_reader_->GetPolicyStatus(kPolicyHomepageLocation)); |
| 88 EXPECT_TRUE(dict->Equals(result.get())); |
| 89 |
| 90 recommended_reader_->OnUpdatePolicy(); |
| 91 dict->SetString("level", |
| 92 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); |
| 93 result.reset( |
| 94 recommended_reader_->GetPolicyStatus(kPolicyHomepageLocation)); |
| 95 EXPECT_TRUE(dict->Equals(result.get())); |
| 96 } |
| 97 |
| 98 // Test for boolean-valued policy settings. |
| 99 TEST_F(ConfigurationPolicyReaderTest, SetBooleanValue) { |
| 100 provider_.AddPolicy(kPolicyShowHomeButton, Value::CreateBooleanValue(true)); |
| 101 managed_reader_->OnUpdatePolicy(); |
| 102 scoped_ptr<DictionaryValue> dict(CreateDictionary(key::kShowHomeButton, |
| 103 Value::CreateBooleanValue(true))); |
| 104 scoped_ptr<DictionaryValue> result( |
| 105 managed_reader_->GetPolicyStatus(kPolicyShowHomeButton)); |
| 106 EXPECT_TRUE(dict->Equals(result.get())); |
| 107 |
| 108 recommended_reader_->OnUpdatePolicy(); |
| 109 dict->SetString("level", |
| 110 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); |
| 111 result.reset(recommended_reader_->GetPolicyStatus(kPolicyShowHomeButton)); |
| 112 EXPECT_TRUE(dict->Equals(result.get())); |
| 113 |
| 114 provider_.AddPolicy(kPolicyShowHomeButton, Value::CreateBooleanValue(false)); |
| 115 managed_reader_->OnUpdatePolicy(); |
| 116 dict->Set( |
| 117 PolicyStatusInfo::kValueDictPath, Value::CreateBooleanValue(false)); |
| 118 dict->SetString("level", |
| 119 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::MANDATORY)); |
| 120 result.reset(managed_reader_->GetPolicyStatus(kPolicyShowHomeButton)); |
| 121 EXPECT_TRUE(dict->Equals(result.get())); |
| 122 |
| 123 recommended_reader_->OnUpdatePolicy(); |
| 124 dict->SetString("level", |
| 125 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); |
| 126 result.reset(recommended_reader_->GetPolicyStatus(kPolicyShowHomeButton)); |
| 127 EXPECT_TRUE(dict->Equals(result.get())); |
| 128 } |
| 129 |
| 130 // Test for integer-valued policy settings. |
| 131 TEST_F(ConfigurationPolicyReaderTest, SetIntegerValue) { |
| 132 provider_.AddPolicy(kPolicyRestoreOnStartup, Value::CreateIntegerValue(3)); |
| 133 managed_reader_->OnUpdatePolicy(); |
| 134 scoped_ptr<DictionaryValue> dict(CreateDictionary(key::kRestoreOnStartup, |
| 135 Value::CreateIntegerValue(3))); |
| 136 scoped_ptr<DictionaryValue> result( |
| 137 managed_reader_->GetPolicyStatus(kPolicyRestoreOnStartup)); |
| 138 EXPECT_TRUE(dict->Equals(result.get())); |
| 139 |
| 140 recommended_reader_->OnUpdatePolicy(); |
| 141 dict->SetString("level", |
| 142 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); |
| 143 result.reset(recommended_reader_->GetPolicyStatus(kPolicyRestoreOnStartup)); |
| 144 EXPECT_TRUE(dict->Equals(result.get())); |
| 145 } |
| 146 |
| 147 class PolicyStatusTest : public testing::Test { |
| 148 protected: |
| 149 typedef ConfigurationPolicyProvider::PolicyDefinitionList |
| 150 PolicyDefinitionList; |
| 151 |
| 152 PolicyStatusTest() { |
| 153 managed_platform_ = new MockConfigurationPolicyReader(); |
| 154 managed_cloud_ = new MockConfigurationPolicyReader(); |
| 155 recommended_platform_ = new MockConfigurationPolicyReader(); |
| 156 recommended_cloud_ = new MockConfigurationPolicyReader(); |
| 157 |
| 158 policy_status_.reset(new PolicyStatus(managed_platform_, |
| 159 managed_cloud_, |
| 160 recommended_platform_, |
| 161 recommended_cloud_)); |
| 162 status_ok_ = ASCIIToUTF16("ok"); |
| 163 |
| 164 policy_list_ = |
| 165 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(); |
| 166 policy_list_size_ = |
| 167 static_cast<size_t>(policy_list_->end - policy_list_->begin); |
| 168 } |
| 169 |
| 170 void DontSendPolicies() { |
| 171 EXPECT_CALL(*managed_platform_, GetPolicyStatus(_)) |
| 172 .WillRepeatedly(ReturnNull()); |
| 173 EXPECT_CALL(*managed_cloud_, GetPolicyStatus(_)) |
| 174 .WillRepeatedly(ReturnNull()); |
| 175 EXPECT_CALL(*recommended_platform_, GetPolicyStatus(_)) |
| 176 .WillRepeatedly(ReturnNull()); |
| 177 EXPECT_CALL(*recommended_cloud_, GetPolicyStatus(_)) |
| 178 .WillRepeatedly(ReturnNull()); |
| 179 } |
| 180 |
| 181 void SendPolicies() { |
| 182 EXPECT_CALL(*managed_platform_, GetPolicyStatus(_)) |
| 183 .WillRepeatedly(ReturnNull()); |
| 184 EXPECT_CALL(*managed_cloud_, GetPolicyStatus(_)) |
| 185 .WillRepeatedly(ReturnNull()); |
| 186 EXPECT_CALL(*recommended_platform_, GetPolicyStatus(_)) |
| 187 .WillRepeatedly(ReturnNull()); |
| 188 EXPECT_CALL(*recommended_cloud_, GetPolicyStatus(_)) |
| 189 .WillRepeatedly(ReturnNull()); |
| 190 |
| 191 EXPECT_CALL(*managed_platform_, GetPolicyStatus(kPolicyInstantEnabled)) |
| 192 .WillRepeatedly(Return(CreateDictionary(key::kInstantEnabled, |
| 193 PolicyStatusInfo::MANDATORY))); |
| 194 EXPECT_CALL(*managed_cloud_, GetPolicyStatus(kPolicyDisablePluginFinder)) |
| 195 .WillRepeatedly(Return(CreateDictionary(key::kDisablePluginFinder, |
| 196 PolicyStatusInfo::MANDATORY))); |
| 197 EXPECT_CALL(*recommended_platform_, GetPolicyStatus(kPolicySyncDisabled)) |
| 198 .WillRepeatedly( |
| 199 Return(CreateDictionary(key::kSyncDisabled, |
| 200 PolicyStatusInfo::RECOMMENDED))); |
| 201 EXPECT_CALL(*recommended_cloud_, GetPolicyStatus(kPolicyTranslateEnabled)) |
| 202 .WillRepeatedly( |
| 203 Return(CreateDictionary(key::kTranslateEnabled, |
| 204 PolicyStatusInfo::RECOMMENDED))); |
| 205 } |
| 206 |
| 207 DictionaryValue* CreateDictionary(const char* name, |
| 208 PolicyStatusInfo::PolicyLevel level) { |
| 209 DictionaryValue* value = new DictionaryValue(); |
| 210 value->SetString(PolicyStatusInfo::kNameDictPath, ASCIIToUTF16(name)); |
| 211 value->SetString(PolicyStatusInfo::kLevelDictPath, |
| 212 PolicyStatusInfo::GetPolicyLevelString(level)); |
| 213 value->SetString(PolicyStatusInfo::kSourceTypeDictPath, |
| 214 PolicyStatusInfo::GetSourceTypeString( |
| 215 PolicyStatusInfo::USER)); |
| 216 value->SetBoolean(PolicyStatusInfo::kValueDictPath, true); |
| 217 value->SetBoolean(PolicyStatusInfo::kSetDictPath, true); |
| 218 value->SetString(PolicyStatusInfo::kStatusDictPath, status_ok_); |
| 219 |
| 220 return value; |
| 221 } |
| 222 |
| 223 void SetDictionaryPaths(DictionaryValue* dict, |
| 224 const char* policy_name, |
| 225 bool defined, |
| 226 PolicyStatusInfo::PolicyLevel level) { |
| 227 dict->SetString(PolicyStatusInfo::kNameDictPath, |
| 228 ASCIIToUTF16(policy_name)); |
| 229 if (defined) { |
| 230 dict->SetString(PolicyStatusInfo::kLevelDictPath, |
| 231 PolicyStatusInfo::GetPolicyLevelString(level)); |
| 232 } |
| 233 } |
| 234 |
| 235 MockConfigurationPolicyReader* managed_platform_; |
| 236 MockConfigurationPolicyReader* managed_cloud_; |
| 237 MockConfigurationPolicyReader* recommended_platform_; |
| 238 MockConfigurationPolicyReader* recommended_cloud_; |
| 239 scoped_ptr<PolicyStatus> policy_status_; |
| 240 const PolicyDefinitionList* policy_list_; |
| 241 size_t policy_list_size_; |
| 242 string16 status_ok_; |
| 243 }; |
| 244 |
| 245 TEST_F(PolicyStatusTest, GetPolicyStatusListNoSetPolicies) { |
| 246 DontSendPolicies(); |
| 247 bool any_policies_sent; |
| 248 scoped_ptr<ListValue> status_list( |
| 249 policy_status_->GetPolicyStatusList(&any_policies_sent)); |
| 250 EXPECT_FALSE(any_policies_sent); |
| 251 EXPECT_EQ(policy_list_size_, status_list->GetSize()); |
| 252 } |
| 253 |
| 254 TEST_F(PolicyStatusTest, GetPolicyStatusListSetPolicies) { |
| 255 SendPolicies(); |
| 256 bool any_policies_sent; |
| 257 scoped_ptr<ListValue> status_list( |
| 258 policy_status_->GetPolicyStatusList(&any_policies_sent)); |
| 259 EXPECT_TRUE(any_policies_sent); |
| 260 EXPECT_EQ(policy_list_size_, status_list->GetSize()); |
| 261 |
| 262 scoped_ptr<DictionaryValue> undefined_dict(new DictionaryValue()); |
| 263 undefined_dict->SetString(PolicyStatusInfo::kLevelDictPath, |
| 264 PolicyStatusInfo::GetPolicyLevelString( |
| 265 PolicyStatusInfo::LEVEL_UNDEFINED)); |
| 266 undefined_dict->SetString(PolicyStatusInfo::kSourceTypeDictPath, |
| 267 PolicyStatusInfo::GetSourceTypeString( |
| 268 PolicyStatusInfo::SOURCE_TYPE_UNDEFINED)); |
| 269 undefined_dict->Set(PolicyStatusInfo::kValueDictPath, |
| 270 Value::CreateNullValue()); |
| 271 undefined_dict->SetBoolean(PolicyStatusInfo::kSetDictPath, false); |
| 272 undefined_dict->SetString(PolicyStatusInfo::kStatusDictPath, string16()); |
| 273 |
| 274 scoped_ptr<DictionaryValue> defined_dict(new DictionaryValue()); |
| 275 defined_dict->SetString(PolicyStatusInfo::kSourceTypeDictPath, |
| 276 PolicyStatusInfo::GetSourceTypeString( |
| 277 PolicyStatusInfo::USER)); |
| 278 defined_dict->Set(PolicyStatusInfo::kValueDictPath, |
| 279 Value::CreateBooleanValue(true)); |
| 280 defined_dict->SetBoolean(PolicyStatusInfo::kSetDictPath, true); |
| 281 defined_dict->SetString(PolicyStatusInfo::kStatusDictPath, status_ok_); |
| 282 |
| 283 for (const PolicyDefinitionList::Entry* entry = policy_list_->begin; |
| 284 entry != policy_list_->end; ++entry) { |
| 285 Value* status_dict = NULL; |
| 286 |
| 287 // Every policy in |policy_list_| has to appear in the returned ListValue. |
| 288 string16 name = ASCIIToUTF16(entry->name); |
| 289 for (ListValue::const_iterator status_entry = status_list->begin(); |
| 290 status_entry != status_list->end(); |
| 291 ++status_entry) { |
| 292 string16 value; |
| 293 ASSERT_TRUE((*status_entry)->IsType(Value::TYPE_DICTIONARY)); |
| 294 DictionaryValue* dict = static_cast<DictionaryValue*>(*status_entry); |
| 295 ASSERT_TRUE(dict->GetString(PolicyStatusInfo::kNameDictPath, &value)); |
| 296 |
| 297 if (value == name) { |
| 298 status_dict = *status_entry; |
| 299 } |
| 300 } |
| 301 |
| 302 ASSERT_FALSE(status_dict == NULL); |
| 303 |
| 304 switch (entry->policy_type) { |
| 305 case kPolicyInstantEnabled: |
| 306 SetDictionaryPaths(defined_dict.get(), |
| 307 entry->name, |
| 308 true, |
| 309 PolicyStatusInfo::MANDATORY); |
| 310 EXPECT_TRUE(defined_dict->Equals(status_dict)); |
| 311 break; |
| 312 case kPolicyDisablePluginFinder: |
| 313 SetDictionaryPaths(defined_dict.get(), |
| 314 entry->name, |
| 315 true, |
| 316 PolicyStatusInfo::MANDATORY); |
| 317 EXPECT_TRUE(defined_dict->Equals(status_dict)); |
| 318 break; |
| 319 case kPolicySyncDisabled: |
| 320 SetDictionaryPaths(defined_dict.get(), |
| 321 entry->name, |
| 322 true, |
| 323 PolicyStatusInfo::RECOMMENDED); |
| 324 EXPECT_TRUE(defined_dict->Equals(status_dict)); |
| 325 break; |
| 326 case kPolicyTranslateEnabled: |
| 327 SetDictionaryPaths(defined_dict.get(), |
| 328 entry->name, |
| 329 true, |
| 330 PolicyStatusInfo::RECOMMENDED); |
| 331 EXPECT_TRUE(defined_dict->Equals(status_dict)); |
| 332 break; |
| 333 default: |
| 334 SetDictionaryPaths(undefined_dict.get(), |
| 335 entry->name, |
| 336 false, |
| 337 PolicyStatusInfo::LEVEL_UNDEFINED); |
| 338 EXPECT_TRUE(undefined_dict->Equals(status_dict)); |
| 339 break; |
| 340 } |
| 341 } |
| 342 } |
| 343 |
| 344 TEST_F(PolicyStatusTest, GetPolicyName) { |
| 345 for (const PolicyDefinitionList::Entry* entry = policy_list_->begin; |
| 346 entry != policy_list_->end; ++entry) { |
| 347 EXPECT_EQ(ASCIIToUTF16(entry->name), |
| 348 PolicyStatus::GetPolicyName(entry->policy_type)); |
| 349 } |
| 350 } |
| 351 |
| 352 } // namespace policy |
OLD | NEW |