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/user_cloud_policy_store.h" | |
6 | |
7 #include "base/file_util.h" | |
8 #include "base/files/scoped_temp_dir.h" | |
9 #include "base/message_loop.h" | |
10 #include "base/run_loop.h" | |
11 #include "chrome/browser/policy/mock_cloud_policy_store.h" | |
12 #include "chrome/browser/policy/policy_builder.h" | |
13 #include "chrome/browser/signin/signin_manager.h" | |
14 #include "chrome/browser/signin/signin_manager_factory.h" | |
15 #include "chrome/browser/signin/signin_manager_fake.h" | |
16 #include "chrome/test/base/testing_profile.h" | |
17 #include "content/public/test/test_browser_thread.h" | |
18 #include "policy/policy_constants.h" | |
19 #include "testing/gmock/include/gmock/gmock.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 | |
22 using testing::AllOf; | |
23 using testing::Eq; | |
24 using testing::Property; | |
25 | |
26 namespace policy { | |
27 | |
28 namespace { | |
29 | |
30 void RunUntilIdle() { | |
31 base::RunLoop run_loop; | |
32 run_loop.RunUntilIdle(); | |
33 } | |
34 | |
35 class UserCloudPolicyStoreTest : public testing::Test { | |
36 public: | |
37 UserCloudPolicyStoreTest() | |
38 : loop_(MessageLoop::TYPE_UI), | |
39 ui_thread_(content::BrowserThread::UI, &loop_), | |
40 file_thread_(content::BrowserThread::FILE, &loop_), | |
41 profile_(new TestingProfile()) {} | |
42 | |
43 virtual void SetUp() OVERRIDE { | |
44 ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir()); | |
45 SigninManager* signin = static_cast<SigninManager*>( | |
46 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( | |
47 profile_.get(), FakeSigninManager::Build)); | |
48 signin->SetAuthenticatedUsername(PolicyBuilder::kFakeUsername); | |
49 store_.reset(new UserCloudPolicyStore(profile_.get(), policy_file())); | |
50 store_->AddObserver(&observer_); | |
51 | |
52 policy_.payload().mutable_showhomebutton()->set_value(true); | |
53 policy_.payload().mutable_syncdisabled()->set_value(true); | |
54 policy_.Build(); | |
55 } | |
56 | |
57 virtual void TearDown() OVERRIDE { | |
58 store_->RemoveObserver(&observer_); | |
59 store_.reset(); | |
60 RunUntilIdle(); | |
61 } | |
62 | |
63 base::FilePath policy_file() { | |
64 return tmp_dir_.path().AppendASCII("policy"); | |
65 } | |
66 | |
67 // Verifies that store_->policy_map() has the appropriate entries. | |
68 void VerifyPolicyMap(CloudPolicyStore* store) { | |
69 EXPECT_EQ(2U, store->policy_map().size()); | |
70 const PolicyMap::Entry* entry = | |
71 store->policy_map().Get(key::kShowHomeButton); | |
72 ASSERT_TRUE(entry); | |
73 EXPECT_TRUE(base::FundamentalValue(true).Equals(entry->value)); | |
74 ASSERT_TRUE(store->policy_map().Get(key::kSyncDisabled)); | |
75 } | |
76 | |
77 // Install an expectation on |observer_| for an error code. | |
78 void ExpectError(CloudPolicyStore* store, CloudPolicyStore::Status error) { | |
79 EXPECT_CALL(observer_, | |
80 OnStoreError(AllOf(Eq(store), | |
81 Property(&CloudPolicyStore::status, | |
82 Eq(error))))); | |
83 } | |
84 | |
85 UserPolicyBuilder policy_; | |
86 MockCloudPolicyStoreObserver observer_; | |
87 scoped_ptr<UserCloudPolicyStore> store_; | |
88 | |
89 // CloudPolicyValidator() requires a FILE thread so declare one here. Both | |
90 // |ui_thread_| and |file_thread_| share the same MessageLoop |loop_| so | |
91 // callers can use RunLoop to manage both virtual threads. | |
92 MessageLoop loop_; | |
93 content::TestBrowserThread ui_thread_; | |
94 content::TestBrowserThread file_thread_; | |
95 | |
96 scoped_ptr<TestingProfile> profile_; | |
97 base::ScopedTempDir tmp_dir_; | |
98 | |
99 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyStoreTest); | |
100 }; | |
101 | |
102 TEST_F(UserCloudPolicyStoreTest, LoadWithNoFile) { | |
103 EXPECT_FALSE(store_->policy()); | |
104 EXPECT_TRUE(store_->policy_map().empty()); | |
105 | |
106 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | |
107 store_->Load(); | |
108 RunUntilIdle(); | |
109 | |
110 EXPECT_FALSE(store_->policy()); | |
111 EXPECT_TRUE(store_->policy_map().empty()); | |
112 } | |
113 | |
114 TEST_F(UserCloudPolicyStoreTest, LoadWithInvalidFile) { | |
115 EXPECT_FALSE(store_->policy()); | |
116 EXPECT_TRUE(store_->policy_map().empty()); | |
117 | |
118 // Create a bogus file. | |
119 ASSERT_TRUE(file_util::CreateDirectory(policy_file().DirName())); | |
120 std::string bogus_data = "bogus_data"; | |
121 int size = bogus_data.size(); | |
122 ASSERT_EQ(size, file_util::WriteFile(policy_file(), | |
123 bogus_data.c_str(), | |
124 bogus_data.size())); | |
125 | |
126 ExpectError(store_.get(), CloudPolicyStore::STATUS_LOAD_ERROR); | |
127 store_->Load(); | |
128 RunUntilIdle(); | |
129 | |
130 EXPECT_FALSE(store_->policy()); | |
131 EXPECT_TRUE(store_->policy_map().empty()); | |
132 } | |
133 | |
134 TEST_F(UserCloudPolicyStoreTest, LoadImmediatelyWithNoFile) { | |
135 EXPECT_FALSE(store_->policy()); | |
136 EXPECT_TRUE(store_->policy_map().empty()); | |
137 | |
138 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | |
139 store_->LoadImmediately(); // Should load without running the message loop. | |
140 | |
141 EXPECT_FALSE(store_->policy()); | |
142 EXPECT_TRUE(store_->policy_map().empty()); | |
143 } | |
144 | |
145 TEST_F(UserCloudPolicyStoreTest, LoadImmediatelyWithInvalidFile) { | |
146 EXPECT_FALSE(store_->policy()); | |
147 EXPECT_TRUE(store_->policy_map().empty()); | |
148 | |
149 // Create a bogus file. | |
150 ASSERT_TRUE(file_util::CreateDirectory(policy_file().DirName())); | |
151 std::string bogus_data = "bogus_data"; | |
152 int size = bogus_data.size(); | |
153 ASSERT_EQ(size, file_util::WriteFile(policy_file(), | |
154 bogus_data.c_str(), | |
155 bogus_data.size())); | |
156 | |
157 ExpectError(store_.get(), CloudPolicyStore::STATUS_LOAD_ERROR); | |
158 store_->LoadImmediately(); // Should load without running the message loop. | |
159 | |
160 EXPECT_FALSE(store_->policy()); | |
161 EXPECT_TRUE(store_->policy_map().empty()); | |
162 } | |
163 | |
164 TEST_F(UserCloudPolicyStoreTest, Store) { | |
165 EXPECT_FALSE(store_->policy()); | |
166 EXPECT_TRUE(store_->policy_map().empty()); | |
167 | |
168 // Store a simple policy and make sure it ends up as the currently active | |
169 // policy. | |
170 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | |
171 store_->Store(policy_.policy()); | |
172 RunUntilIdle(); | |
173 | |
174 // Policy should be decoded and stored. | |
175 ASSERT_TRUE(store_->policy()); | |
176 EXPECT_EQ(policy_.policy_data().SerializeAsString(), | |
177 store_->policy()->SerializeAsString()); | |
178 VerifyPolicyMap(store_.get()); | |
179 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); | |
180 } | |
181 | |
182 TEST_F(UserCloudPolicyStoreTest, StoreThenClear) { | |
183 EXPECT_FALSE(store_->policy()); | |
184 EXPECT_TRUE(store_->policy_map().empty()); | |
185 | |
186 // Store a simple policy and make sure the file exists. | |
187 // policy. | |
188 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | |
189 store_->Store(policy_.policy()); | |
190 RunUntilIdle(); | |
191 | |
192 EXPECT_TRUE(store_->policy()); | |
193 EXPECT_FALSE(store_->policy_map().empty()); | |
194 | |
195 // Policy file should exist. | |
196 ASSERT_TRUE(file_util::PathExists(policy_file())); | |
197 | |
198 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | |
199 store_->Clear(); | |
200 RunUntilIdle(); | |
201 | |
202 // Policy file should not exist. | |
203 ASSERT_TRUE(!file_util::PathExists(policy_file())); | |
204 | |
205 // Policy should be gone. | |
206 EXPECT_FALSE(store_->policy()); | |
207 EXPECT_TRUE(store_->policy_map().empty()); | |
208 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); | |
209 } | |
210 | |
211 TEST_F(UserCloudPolicyStoreTest, StoreTwoTimes) { | |
212 EXPECT_FALSE(store_->policy()); | |
213 EXPECT_TRUE(store_->policy_map().empty()); | |
214 | |
215 // Store a simple policy then store a second policy before the first one | |
216 // finishes validating, and make sure the second policy ends up as the active | |
217 // policy. | |
218 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())).Times(2); | |
219 | |
220 UserPolicyBuilder first_policy; | |
221 first_policy.payload().mutable_showhomebutton()->set_value(false); | |
222 first_policy.Build(); | |
223 store_->Store(first_policy.policy()); | |
224 RunUntilIdle(); | |
225 | |
226 store_->Store(policy_.policy()); | |
227 RunUntilIdle(); | |
228 | |
229 // Policy should be decoded and stored. | |
230 ASSERT_TRUE(store_->policy()); | |
231 EXPECT_EQ(policy_.policy_data().SerializeAsString(), | |
232 store_->policy()->SerializeAsString()); | |
233 VerifyPolicyMap(store_.get()); | |
234 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); | |
235 } | |
236 | |
237 TEST_F(UserCloudPolicyStoreTest, StoreThenLoad) { | |
238 // Store a simple policy and make sure it can be read back in. | |
239 // policy. | |
240 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | |
241 store_->Store(policy_.policy()); | |
242 RunUntilIdle(); | |
243 | |
244 // Now, make sure the policy can be read back in from a second store. | |
245 scoped_ptr<UserCloudPolicyStore> store2( | |
246 new UserCloudPolicyStore(profile_.get(), policy_file())); | |
247 store2->AddObserver(&observer_); | |
248 EXPECT_CALL(observer_, OnStoreLoaded(store2.get())); | |
249 store2->Load(); | |
250 RunUntilIdle(); | |
251 | |
252 ASSERT_TRUE(store2->policy()); | |
253 EXPECT_EQ(policy_.policy_data().SerializeAsString(), | |
254 store2->policy()->SerializeAsString()); | |
255 VerifyPolicyMap(store2.get()); | |
256 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store2->status()); | |
257 store2->RemoveObserver(&observer_); | |
258 } | |
259 | |
260 TEST_F(UserCloudPolicyStoreTest, StoreThenLoadImmediately) { | |
261 // Store a simple policy and make sure it can be read back in. | |
262 // policy. | |
263 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | |
264 store_->Store(policy_.policy()); | |
265 RunUntilIdle(); | |
266 | |
267 // Now, make sure the policy can be read back in from a second store. | |
268 scoped_ptr<UserCloudPolicyStore> store2( | |
269 new UserCloudPolicyStore(profile_.get(), policy_file())); | |
270 store2->AddObserver(&observer_); | |
271 EXPECT_CALL(observer_, OnStoreLoaded(store2.get())); | |
272 store2->LoadImmediately(); // Should load without running the message loop. | |
273 | |
274 ASSERT_TRUE(store2->policy()); | |
275 EXPECT_EQ(policy_.policy_data().SerializeAsString(), | |
276 store2->policy()->SerializeAsString()); | |
277 VerifyPolicyMap(store2.get()); | |
278 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store2->status()); | |
279 store2->RemoveObserver(&observer_); | |
280 } | |
281 | |
282 TEST_F(UserCloudPolicyStoreTest, StoreValidationError) { | |
283 // Create an invalid policy (no policy type). | |
284 policy_.policy_data().clear_policy_type(); | |
285 policy_.Build(); | |
286 | |
287 // Store policy. | |
288 ExpectError(store_.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR); | |
289 store_->Store(policy_.policy()); | |
290 RunUntilIdle(); | |
291 ASSERT_FALSE(store_->policy()); | |
292 } | |
293 | |
294 TEST_F(UserCloudPolicyStoreTest, LoadValidationError) { | |
295 // Force a validation error by changing the username after policy is stored. | |
296 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | |
297 store_->Store(policy_.policy()); | |
298 RunUntilIdle(); | |
299 | |
300 // Sign out, and sign back in as a different user, and try to load the profile | |
301 // data (should fail due to mismatched username). | |
302 SigninManagerFactory::GetForProfile(profile_.get())->SignOut(); | |
303 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( | |
304 "foobar@foobar.com"); | |
305 | |
306 scoped_ptr<UserCloudPolicyStore> store2( | |
307 new UserCloudPolicyStore(profile_.get(), policy_file())); | |
308 store2->AddObserver(&observer_); | |
309 ExpectError(store2.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR); | |
310 store2->Load(); | |
311 RunUntilIdle(); | |
312 | |
313 ASSERT_FALSE(store2->policy()); | |
314 store2->RemoveObserver(&observer_); | |
315 } | |
316 | |
317 } // namespace | |
318 | |
319 } // namespace policy | |
OLD | NEW |