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

Side by Side Diff: chrome/browser/policy/user_cloud_policy_store_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698