Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(118)

Unified Diff: chrome/browser/policy/cloud_policy_provider_unittest.cc

Issue 7147015: Move user cloud policy to BrowserProcess (was 6979011) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/policy/cloud_policy_provider_unittest.cc
diff --git a/chrome/browser/policy/cloud_policy_provider_unittest.cc b/chrome/browser/policy/cloud_policy_provider_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..4497dad7fc827578c2730823a5fbc7e51f48637e
--- /dev/null
+++ b/chrome/browser/policy/cloud_policy_provider_unittest.cc
@@ -0,0 +1,276 @@
+// 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 "chrome/browser/policy/cloud_policy_provider.h"
+
+#include "base/basictypes.h"
+#include "base/values.h"
+#include "chrome/browser/policy/cloud_policy_cache_base.h"
+#include "chrome/browser/policy/cloud_policy_provider_impl.h"
+#include "chrome/browser/policy/configuration_policy_pref_store.h"
+#include "chrome/browser/policy/mock_configuration_policy_store.h"
+#include "testing/gmock/include/gmock/gmock.h"
+
+using testing::AnyNumber;
+using testing::_;
+
+namespace policy {
+
+class MockCloudPolicyCache : public CloudPolicyCacheBase {
+ public:
+ MockCloudPolicyCache() {}
+ virtual ~MockCloudPolicyCache() {}
+
+ // CloudPolicyCacheBase implementation.
+ void Load() {}
+ void SetPolicy(const em::PolicyFetchResponse& policy) {}
+ bool DecodePolicyData(const em::PolicyData& policy_data,
+ PolicyMap* mandatory,
+ PolicyMap* recommended) {
+ return true;
+ }
+
+ // Non-const accessors for underlying PolicyMaps.
+ PolicyMap* raw_mandatory_policy() {
+ return &mandatory_policy_;
+ }
+
+ PolicyMap* raw_recommended_policy() {
+ return &recommended_policy_;
+ }
+
+ void SetUnmanaged() {
+ is_unmanaged_ = true;
+ }
+
+ void set_initialized(bool initialized) {
+ initialization_complete_ = initialized;
+ }
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MockCloudPolicyCache);
+};
+
+class CloudPolicyProviderTest : public testing::Test {
+ protected:
+ void CreateCloudPolicyProvider(CloudPolicyCacheBase::PolicyLevel level) {
+ cloud_policy_provider_.reset(new CloudPolicyProviderImpl(
+ ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), level));
+ }
+
+ // Appends the caches to a provider and then provides the policies to
+ // |store_|.
+ void RunCachesThroughProvider(MockCloudPolicyCache caches[], int n,
+ CloudPolicyCacheBase::PolicyLevel level) {
+ store_.reset(new MockConfigurationPolicyStore);
+ CloudPolicyProviderImpl provider(
+ policy::ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(),
+ level);
+ for (int i = 0; i < n; i++) {
+ provider.AppendCache(&caches[i]);
+ }
+ EXPECT_CALL(*store_.get(), Apply(_, _)).Times(AnyNumber());
+ provider.Provide(store_.get());
+ }
+
+ // Checks a string policy in |store_|.
+ void ExpectStringPolicy(const std::string& expected,
Mattias Nissler (ping if slow) 2011/06/24 09:16:46 Again, can't we just check the policy map instead
gfeher 2011/06/24 15:32:44 We can, then I would have to extract the private m
Mattias Nissler (ping if slow) 2011/06/24 17:27:17 Actually you are right in not doing it, I had mixe
+ ConfigurationPolicyType type) {
+ const Value* value = store_->Get(type);
+ std::string string_value;
+ EXPECT_TRUE(value != NULL);
+ EXPECT_TRUE(value->GetAsString(&string_value));
+ EXPECT_EQ(expected, string_value);
+ }
+
+ // Checks a boolean policy in |store_|.
+ void ExpectBoolPolicy(bool expected, ConfigurationPolicyType type) {
+ const Value* value = store_->Get(type);
+ bool bool_value;
+ EXPECT_TRUE(value != NULL);
+ EXPECT_TRUE(value->GetAsBoolean(&bool_value));
+ EXPECT_EQ(expected, bool_value);
+ }
+
+ void ExpectNoPolicy(ConfigurationPolicyType type) {
+ EXPECT_TRUE(NULL == store_->Get(type));
+ }
+
+ void CombineTwoPolicyMaps(const PolicyMap& base,
+ const PolicyMap& overlay,
+ PolicyMap* out_map) {
+ DCHECK(cloud_policy_provider_.get());
+ cloud_policy_provider_->CombineTwoPolicyMaps(base, overlay, out_map);
+ }
+
+ static int simple_list_length();
+
+ ConfigurationPolicyType simple_list_policy_type(int id) {
+ DCHECK(id < simple_list_length());
+ DCHECK(!is_proxy_policy(simple_policies[id]));
+ return simple_policies[id];
+ }
+
+ int proxy_policy_count() {
+ return CloudPolicyProviderImpl::proxy_policy_count();
+ }
+
+ bool is_proxy_policy(ConfigurationPolicyType policy) {
+ return CloudPolicyProviderImpl::is_proxy_policy(policy);
+ }
+
+ ConfigurationPolicyType get_proxy_policy(int i) {
+ DCHECK(i < proxy_policy_count());
+ return CloudPolicyProviderImpl::proxy_policies[i];
+ }
+
+ private:
+ // Some tests need a list of policies that doesn't contain any proxy
+ // policies. Note: these policies will be handled as if they had the
+ // type of Value::TYPE_INTEGER.
+ static const ConfigurationPolicyType simple_policies[];
+
+ scoped_ptr<CloudPolicyProviderImpl> cloud_policy_provider_;
+ scoped_ptr<MockConfigurationPolicyStore> store_;
+};
+
+// static
+const ConfigurationPolicyType
+ CloudPolicyProviderTest::simple_policies[] = {
+ kPolicyHomepageLocation,
+ kPolicyHomepageIsNewTabPage,
+ kPolicyRestoreOnStartup,
+ kPolicyRestoreOnStartupURLs,
+ kPolicyDefaultSearchProviderEnabled,
+ kPolicyDefaultSearchProviderName,
+ kPolicyDefaultSearchProviderKeyword,
+ kPolicyDefaultSearchProviderSearchURL,
+ kPolicyDefaultSearchProviderSuggestURL,
+ kPolicyDefaultSearchProviderInstantURL,
+ kPolicyDefaultSearchProviderIconURL,
+ kPolicyDefaultSearchProviderEncodings,
+};
+
+// static
+int CloudPolicyProviderTest::simple_list_length() {
+ return arraysize(simple_policies);
+}
+
+TEST_F(CloudPolicyProviderTest, ProxyPolicies) {
+ EXPECT_EQ(5, proxy_policy_count()) <<
+ "Please update CloudPolicyProviderTest with the new proxy policies.";
Mattias Nissler (ping if slow) 2011/06/24 09:16:46 Meh! What is this test good for? Just remove.
gfeher 2011/06/24 15:32:44 Done.
+}
+
+// Proxy setting distributed over multiple caches.
+TEST_F(CloudPolicyProviderTest,
+ ProxySettingDistributedOverMultipleCaches) {
+ // There are proxy_policy_count()+1 = 6 caches and they are mixed together by
+ // one instance of CloudPolicyProvider. The first cache has some policies but
+ // no proxy-related ones. The following caches have each one proxy-policy set.
+ const int n = 6;
+ MockCloudPolicyCache caches[n];
+
+ // Prepare |cache[0]| to serve some non-proxy policies.
+ caches[0].raw_mandatory_policy()->Set(kPolicyShowHomeButton,
+ Value::CreateBooleanValue(true));
+ caches[0].raw_mandatory_policy()->Set(kPolicyIncognitoEnabled,
+ Value::CreateBooleanValue(true));
+ caches[0].raw_mandatory_policy()->Set(kPolicyTranslateEnabled,
+ Value::CreateBooleanValue(true));
+ caches[0].set_initialized(true);
+
+ // Prepare the other caches to serve one proxy-policy each.
+ caches[1].raw_mandatory_policy()->Set(kPolicyProxyMode,
+ Value::CreateStringValue("cache 1"));
+ caches[1].set_initialized(true);
+ caches[2].raw_mandatory_policy()->Set(kPolicyProxyServerMode,
+ Value::CreateIntegerValue(2));
+ caches[2].set_initialized(true);
+ caches[3].raw_mandatory_policy()->Set(kPolicyProxyServer,
+ Value::CreateStringValue("cache 3"));
+ caches[3].set_initialized(true);
+ caches[4].raw_mandatory_policy()->Set(kPolicyProxyPacUrl,
+ Value::CreateStringValue("cache 4"));
+ caches[4].set_initialized(true);
+ caches[5].raw_mandatory_policy()->Set(kPolicyProxyMode,
+ Value::CreateStringValue("cache 5"));
+ caches[5].set_initialized(true);
+
+ RunCachesThroughProvider(
+ caches, n, CloudPolicyCacheBase::POLICY_LEVEL_MANDATORY);
+
+ // Verify expectations.
+ ExpectStringPolicy("cache 1", kPolicyProxyMode);
+ ExpectNoPolicy(kPolicyProxyServerMode);
+ ExpectNoPolicy(kPolicyProxyServer);
+ ExpectNoPolicy(kPolicyProxyPacUrl);
+ ExpectBoolPolicy(true, kPolicyShowHomeButton);
+ ExpectBoolPolicy(true, kPolicyIncognitoEnabled);
+ ExpectBoolPolicy(true, kPolicyTranslateEnabled);
+}
+
+// Combining two PolicyMaps.
+TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsSame) {
+ PolicyMap A, B, C;
+ CreateCloudPolicyProvider(CloudPolicyCacheBase::POLICY_LEVEL_RECOMMENDED);
+ for (int i = 0; i < simple_list_length(); ++i) {
Mattias Nissler (ping if slow) 2011/06/24 09:16:46 Still using loops? Can't we just get rid of the lo
gfeher 2011/06/24 15:32:44 Done.
+ A.Set(simple_list_policy_type(i), Value::CreateIntegerValue(i));
+ B.Set(simple_list_policy_type(i), Value::CreateIntegerValue(-1 * i));
+ }
+ CombineTwoPolicyMaps(A, B, &C);
+ EXPECT_TRUE(A.Equals(C));
+}
+
+TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsEmpty) {
+ PolicyMap A, B, C;
+ CreateCloudPolicyProvider(CloudPolicyCacheBase::POLICY_LEVEL_RECOMMENDED);
+ CombineTwoPolicyMaps(A, B, &C);
+ EXPECT_TRUE(C.empty());
+}
+
+TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsPartial) {
+ PolicyMap A, B, C;
+ CreateCloudPolicyProvider(CloudPolicyCacheBase::POLICY_LEVEL_RECOMMENDED);
+
+ for (int i = 0; i < simple_list_length(); ++i) {
Mattias Nissler (ping if slow) 2011/06/24 09:16:46 Still using loops?
gfeher 2011/06/24 15:32:44 Done.
+ B.Set(simple_list_policy_type(i), Value::CreateIntegerValue(-i));
+ if (i % 2 == 0)
+ A.Set(simple_list_policy_type(i), Value::CreateIntegerValue(i));
+ }
+
+ CombineTwoPolicyMaps(A, B, &C);
+
+ for (int i = 0; i < simple_list_length(); ++i) {
+ if (const Value* value = C.Get(simple_list_policy_type(i))) {
+ int int_value;
+ EXPECT_TRUE(value->GetAsInteger(&int_value));
+ if (i % 2 == 1) {
+ EXPECT_EQ(int_value, -1 * i);
+ } else {
+ EXPECT_EQ(int_value, i);
+ }
+ }
+ }
+}
+
+TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsProxies) {
+ const int a_value = 1;
+ const int b_value = -1;
+ PolicyMap A, B, C;
+ CreateCloudPolicyProvider(CloudPolicyCacheBase::POLICY_LEVEL_RECOMMENDED);
+
+ A.Set(policy::kPolicyProxyMode, Value::CreateIntegerValue(a_value));
+
+ B.Set(policy::kPolicyProxyServerMode, Value::CreateIntegerValue(b_value));
+ B.Set(policy::kPolicyProxyServer, Value::CreateIntegerValue(b_value));
+ B.Set(policy::kPolicyProxyPacUrl, Value::CreateIntegerValue(b_value));
+ B.Set(policy::kPolicyProxyBypassList, Value::CreateIntegerValue(b_value));
+
+ CombineTwoPolicyMaps(A, B, &C);
+
+ EXPECT_TRUE(A.Equals(C));
+ EXPECT_FALSE(B.Equals(C));
+}
+
+} // namespace policy

Powered by Google App Engine
This is Rietveld 408576698