OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "chrome/browser/policy/cloud_policy_provider.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/values.h" | |
9 #include "chrome/browser/policy/browser_policy_connector.h" | |
10 #include "chrome/browser/policy/cloud_policy_cache_base.h" | |
11 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | |
12 #include "chrome/browser/policy/policy_bundle.h" | |
13 #include "chrome/browser/policy/policy_map.h" | |
14 #include "policy/policy_constants.h" | |
15 #include "testing/gmock/include/gmock/gmock.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 using testing::Mock; | |
19 | |
20 namespace em = enterprise_management; | |
21 | |
22 namespace policy { | |
23 | |
24 class MockCloudPolicyCache : public CloudPolicyCacheBase { | |
25 public: | |
26 MockCloudPolicyCache() {} | |
27 virtual ~MockCloudPolicyCache() {} | |
28 | |
29 virtual void Load() OVERRIDE {} | |
30 | |
31 virtual bool SetPolicy(const em::PolicyFetchResponse& policy) OVERRIDE { | |
32 return true; | |
33 } | |
34 | |
35 virtual void SetUnmanaged() OVERRIDE { | |
36 is_unmanaged_ = true; | |
37 } | |
38 | |
39 virtual bool DecodePolicyData(const em::PolicyData& policy_data, | |
40 PolicyMap* policies) OVERRIDE { | |
41 return true; | |
42 } | |
43 | |
44 PolicyMap* mutable_policy() { | |
45 return &policies_; | |
46 } | |
47 | |
48 // Make these methods public. | |
49 using CloudPolicyCacheBase::SetReady; | |
50 using CloudPolicyCacheBase::NotifyObservers; | |
51 | |
52 private: | |
53 DISALLOW_COPY_AND_ASSIGN(MockCloudPolicyCache); | |
54 }; | |
55 | |
56 class CloudPolicyProviderTest : public testing::Test { | |
57 protected: | |
58 CloudPolicyProviderTest() | |
59 : cloud_policy_provider_(&browser_policy_connector_) {} | |
60 | |
61 virtual void SetUp() OVERRIDE { | |
62 cloud_policy_provider_.Init(); | |
63 cloud_policy_provider_.AddObserver(&observer_); | |
64 } | |
65 | |
66 virtual void TearDown() OVERRIDE { | |
67 cloud_policy_provider_.RemoveObserver(&observer_); | |
68 cloud_policy_provider_.Shutdown(); | |
69 browser_policy_connector_.Shutdown(); | |
70 } | |
71 | |
72 void AddUserCache() { | |
73 EXPECT_CALL(observer_, OnUpdatePolicy(&cloud_policy_provider_)); | |
74 cloud_policy_provider_.SetUserPolicyCache(&user_policy_cache_); | |
75 Mock::VerifyAndClearExpectations(&observer_); | |
76 } | |
77 | |
78 void AddDeviceCache() { | |
79 #if defined(OS_CHROMEOS) | |
80 EXPECT_CALL(observer_, OnUpdatePolicy(&cloud_policy_provider_)); | |
81 cloud_policy_provider_.SetDevicePolicyCache(&device_policy_cache_); | |
82 Mock::VerifyAndClearExpectations(&observer_); | |
83 #endif | |
84 } | |
85 | |
86 void SetUserCacheReady() { | |
87 EXPECT_CALL(observer_, OnUpdatePolicy(&cloud_policy_provider_)); | |
88 user_policy_cache_.SetReady(); | |
89 Mock::VerifyAndClearExpectations(&observer_); | |
90 EXPECT_TRUE(user_policy_cache_.IsReady()); | |
91 } | |
92 | |
93 void SetDeviceCacheReady() { | |
94 #if defined(OS_CHROMEOS) | |
95 EXPECT_CALL(observer_, OnUpdatePolicy(&cloud_policy_provider_)); | |
96 device_policy_cache_.SetReady(); | |
97 Mock::VerifyAndClearExpectations(&observer_); | |
98 EXPECT_TRUE(device_policy_cache_.IsReady()); | |
99 #endif | |
100 } | |
101 | |
102 BrowserPolicyConnector browser_policy_connector_; | |
103 | |
104 MockCloudPolicyCache user_policy_cache_; | |
105 #if defined(OS_CHROMEOS) | |
106 MockCloudPolicyCache device_policy_cache_; | |
107 #endif | |
108 | |
109 CloudPolicyProvider cloud_policy_provider_; | |
110 MockConfigurationPolicyObserver observer_; | |
111 }; | |
112 | |
113 TEST_F(CloudPolicyProviderTest, Initialization) { | |
114 EXPECT_FALSE(cloud_policy_provider_.IsInitializationComplete()); | |
115 // The provider only becomes initialized when it has all caches, and the | |
116 // caches are ready too. | |
117 AddDeviceCache(); | |
118 EXPECT_FALSE(cloud_policy_provider_.IsInitializationComplete()); | |
119 SetDeviceCacheReady(); | |
120 EXPECT_FALSE(cloud_policy_provider_.IsInitializationComplete()); | |
121 AddUserCache(); | |
122 EXPECT_FALSE(user_policy_cache_.IsReady()); | |
123 EXPECT_FALSE(cloud_policy_provider_.IsInitializationComplete()); | |
124 PolicyBundle expected_bundle; | |
125 EXPECT_TRUE(cloud_policy_provider_.policies().Equals(expected_bundle)); | |
126 SetUserCacheReady(); | |
127 EXPECT_TRUE(cloud_policy_provider_.IsInitializationComplete()); | |
128 EXPECT_TRUE(cloud_policy_provider_.policies().Equals(expected_bundle)); | |
129 | |
130 const std::string kUrl("http://chromium.org"); | |
131 user_policy_cache_.mutable_policy()->Set(key::kHomepageLocation, | |
132 POLICY_LEVEL_MANDATORY, | |
133 POLICY_SCOPE_USER, | |
134 Value::CreateStringValue(kUrl)); | |
135 EXPECT_CALL(observer_, OnUpdatePolicy(&cloud_policy_provider_)); | |
136 user_policy_cache_.NotifyObservers(); | |
137 Mock::VerifyAndClearExpectations(&observer_); | |
138 expected_bundle.Get(POLICY_DOMAIN_CHROME, "") | |
139 .Set(key::kHomepageLocation, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
140 base::Value::CreateStringValue(kUrl)); | |
141 EXPECT_TRUE(cloud_policy_provider_.policies().Equals(expected_bundle)); | |
142 } | |
143 | |
144 TEST_F(CloudPolicyProviderTest, RefreshPolicies) { | |
145 // OnUpdatePolicy is called when the provider doesn't have any caches. | |
146 EXPECT_CALL(observer_, OnUpdatePolicy(&cloud_policy_provider_)).Times(1); | |
147 cloud_policy_provider_.RefreshPolicies(); | |
148 Mock::VerifyAndClearExpectations(&observer_); | |
149 | |
150 // OnUpdatePolicy is called when all the caches have updated. | |
151 AddUserCache(); | |
152 | |
153 EXPECT_CALL(observer_, OnUpdatePolicy(&cloud_policy_provider_)).Times(0); | |
154 cloud_policy_provider_.RefreshPolicies(); | |
155 Mock::VerifyAndClearExpectations(&observer_); | |
156 | |
157 EXPECT_CALL(observer_, OnUpdatePolicy(&cloud_policy_provider_)).Times(1); | |
158 cloud_policy_provider_.OnCacheUpdate(&user_policy_cache_); | |
159 Mock::VerifyAndClearExpectations(&observer_); | |
160 | |
161 #if defined(OS_CHROMEOS) | |
162 AddDeviceCache(); | |
163 | |
164 EXPECT_CALL(observer_, OnUpdatePolicy(&cloud_policy_provider_)).Times(0); | |
165 cloud_policy_provider_.RefreshPolicies(); | |
166 Mock::VerifyAndClearExpectations(&observer_); | |
167 | |
168 // Updating one of the caches is not enough, both must be updated. | |
169 EXPECT_CALL(observer_, OnUpdatePolicy(&cloud_policy_provider_)).Times(0); | |
170 cloud_policy_provider_.OnCacheUpdate(&user_policy_cache_); | |
171 Mock::VerifyAndClearExpectations(&observer_); | |
172 | |
173 // If a cache refreshes more than once, the provider should still wait for | |
174 // the others before firing the update. | |
175 EXPECT_CALL(observer_, OnUpdatePolicy(&cloud_policy_provider_)).Times(0); | |
176 cloud_policy_provider_.OnCacheUpdate(&user_policy_cache_); | |
177 Mock::VerifyAndClearExpectations(&observer_); | |
178 | |
179 EXPECT_CALL(observer_, OnUpdatePolicy(&cloud_policy_provider_)).Times(1); | |
180 cloud_policy_provider_.OnCacheUpdate(&device_policy_cache_); | |
181 Mock::VerifyAndClearExpectations(&observer_); | |
182 #endif | |
183 } | |
184 | |
185 #if defined(OS_CHROMEOS) | |
186 TEST_F(CloudPolicyProviderTest, MergeProxyPolicies) { | |
187 AddDeviceCache(); | |
188 AddUserCache(); | |
189 SetDeviceCacheReady(); | |
190 SetUserCacheReady(); | |
191 EXPECT_TRUE(cloud_policy_provider_.IsInitializationComplete()); | |
192 PolicyBundle expected_bundle; | |
193 EXPECT_TRUE(cloud_policy_provider_.policies().Equals(expected_bundle)); | |
194 | |
195 // User policy takes precedence over device policy. | |
196 EXPECT_CALL(observer_, OnUpdatePolicy(&cloud_policy_provider_)); | |
197 device_policy_cache_.mutable_policy()->Set( | |
198 key::kProxyMode, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_MACHINE, | |
199 Value::CreateStringValue("device mode")); | |
200 device_policy_cache_.NotifyObservers(); | |
201 Mock::VerifyAndClearExpectations(&observer_); | |
202 | |
203 EXPECT_CALL(observer_, OnUpdatePolicy(&cloud_policy_provider_)); | |
204 user_policy_cache_.mutable_policy()->Set( | |
205 key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
206 Value::CreateStringValue("user mode")); | |
207 user_policy_cache_.NotifyObservers(); | |
208 Mock::VerifyAndClearExpectations(&observer_); | |
209 | |
210 // The deprecated proxy policies are converted to the new ProxySettings. | |
211 base::DictionaryValue* proxy_settings = new base::DictionaryValue(); | |
212 proxy_settings->SetString(key::kProxyMode, "user mode"); | |
213 expected_bundle.Get(POLICY_DOMAIN_CHROME, "") | |
214 .Set(key::kProxySettings, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
215 proxy_settings); | |
216 EXPECT_TRUE(cloud_policy_provider_.policies().Equals(expected_bundle)); | |
217 } | |
218 #endif | |
219 | |
220 } // namespace policy | |
OLD | NEW |