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

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

Issue 12255017: Revert 182279 due to failure in CloudPolicyTest.FetchPolicyWithRotatedKey on ChromeOS Asan bots. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 10 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
« no previous file with comments | « chrome/browser/policy/user_cloud_policy_store_chromeos.cc ('k') | chrome/common/chrome_paths.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_chromeos.h" 5 #include "chrome/browser/policy/user_cloud_policy_store_chromeos.h"
6 6
7 #include <vector>
8
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/file_util.h" 7 #include "base/file_util.h"
12 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
13 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop.h" 10 #include "base/message_loop.h"
15 #include "base/threading/sequenced_worker_pool.h"
16 #include "chrome/browser/policy/cloud_policy_constants.h" 11 #include "chrome/browser/policy/cloud_policy_constants.h"
17 #include "chrome/browser/policy/mock_cloud_policy_store.h" 12 #include "chrome/browser/policy/mock_cloud_policy_store.h"
18 #include "chrome/browser/policy/policy_builder.h" 13 #include "chrome/browser/policy/policy_builder.h"
19 #include "chrome/browser/policy/proto/cloud_policy.pb.h" 14 #include "chrome/browser/policy/proto/cloud_policy.pb.h"
20 #include "chrome/browser/policy/proto/device_management_local.pb.h" 15 #include "chrome/browser/policy/proto/device_management_local.pb.h"
21 #include "chromeos/dbus/mock_cryptohome_client.h"
22 #include "chromeos/dbus/mock_session_manager_client.h" 16 #include "chromeos/dbus/mock_session_manager_client.h"
23 #include "content/public/test/test_browser_thread.h" 17 #include "content/public/test/test_browser_thread.h"
24 #include "policy/policy_constants.h" 18 #include "policy/policy_constants.h"
25 #include "testing/gmock/include/gmock/gmock.h" 19 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
27 21
28 namespace em = enterprise_management; 22 namespace em = enterprise_management;
29 23
30 using testing::AllOf; 24 using testing::AllOf;
31 using testing::AnyNumber;
32 using testing::Eq; 25 using testing::Eq;
33 using testing::Mock; 26 using testing::Mock;
34 using testing::Property; 27 using testing::Property;
35 using testing::SaveArg; 28 using testing::SaveArg;
36 using testing::_; 29 using testing::_;
37 30
38 namespace policy { 31 namespace policy {
39 32
40 namespace { 33 namespace {
41 34
42 const char kLegacyDeviceId[] = "legacy-device-id"; 35 const char kLegacyDeviceId[] = "legacy-device-id";
43 const char kLegacyToken[] = "legacy-token"; 36 const char kLegacyToken[] = "legacy-token";
44 const char kSanitizedUsername[] = "0123456789ABCDEF0123456789ABCDEF012345678";
45 const char kDefaultHomepage[] = "http://chromium.org";
46
47 ACTION_P2(SendSanitizedUsername, call_status, sanitized_username) {
48 MessageLoop::current()->PostTask(
49 FROM_HERE, base::Bind(arg1, call_status, sanitized_username));
50 }
51 37
52 class UserCloudPolicyStoreChromeOSTest : public testing::Test { 38 class UserCloudPolicyStoreChromeOSTest : public testing::Test {
53 protected: 39 protected:
54 UserCloudPolicyStoreChromeOSTest() 40 UserCloudPolicyStoreChromeOSTest()
55 : loop_(MessageLoop::TYPE_UI), 41 : loop_(MessageLoop::TYPE_UI),
56 ui_thread_(content::BrowserThread::UI, &loop_), 42 ui_thread_(content::BrowserThread::UI, &loop_),
57 file_thread_(content::BrowserThread::FILE, &loop_) {} 43 file_thread_(content::BrowserThread::FILE, &loop_) {}
58 44
59 virtual void SetUp() OVERRIDE { 45 virtual void SetUp() OVERRIDE {
60 EXPECT_CALL(cryptohome_client_,
61 GetSanitizedUsername(PolicyBuilder::kFakeUsername, _))
62 .Times(AnyNumber())
63 .WillRepeatedly(
64 SendSanitizedUsername(chromeos::DBUS_METHOD_CALL_SUCCESS,
65 kSanitizedUsername));
66
67 ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir()); 46 ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir());
68 store_.reset(new UserCloudPolicyStoreChromeOS(&cryptohome_client_, 47 store_.reset(new UserCloudPolicyStoreChromeOS(&session_manager_client_,
69 &session_manager_client_,
70 PolicyBuilder::kFakeUsername, 48 PolicyBuilder::kFakeUsername,
71 user_policy_dir(),
72 token_file(), 49 token_file(),
73 policy_file())); 50 policy_file()));
74 store_->AddObserver(&observer_); 51 store_->AddObserver(&observer_);
75 52
76 // Install the initial public key, so that by default the validation of 53 policy_.payload().mutable_showhomebutton()->set_value(true);
77 // the stored/loaded policy blob succeeds.
78 std::vector<uint8> public_key;
79 ASSERT_TRUE(policy_.signing_key()->ExportPublicKey(&public_key));
80 StoreUserPolicyKey(public_key);
81
82 policy_.payload().mutable_homepagelocation()->set_value(kDefaultHomepage);
83 policy_.Build(); 54 policy_.Build();
84 } 55 }
85 56
86 virtual void TearDown() OVERRIDE { 57 virtual void TearDown() OVERRIDE {
87 store_->RemoveObserver(&observer_); 58 store_->RemoveObserver(&observer_);
88 store_.reset(); 59 store_.reset();
89 RunUntilIdle(); 60 loop_.RunUntilIdle();
90 } 61 }
91 62
92 // Install an expectation on |observer_| for an error code. 63 // Install an expectation on |observer_| for an error code.
93 void ExpectError(CloudPolicyStore::Status error) { 64 void ExpectError(CloudPolicyStore::Status error) {
94 EXPECT_CALL(observer_, 65 EXPECT_CALL(observer_,
95 OnStoreError(AllOf(Eq(store_.get()), 66 OnStoreError(AllOf(Eq(store_.get()),
96 Property(&CloudPolicyStore::status, 67 Property(&CloudPolicyStore::status,
97 Eq(error))))); 68 Eq(error)))));
98 } 69 }
99 70
100 // Triggers a store_->Load() operation, handles the expected call to 71 // Triggers a store_->Load() operation, handles the expected call to
101 // |session_manager_client_| and sends |response|. 72 // |session_manager_client_| and sends |response|.
102 void PerformPolicyLoad(const std::string& response) { 73 void PerformPolicyLoad(const std::string& response) {
103 // Issue a load command. 74 // Issue a load command.
104 chromeos::SessionManagerClient::RetrievePolicyCallback retrieve_callback; 75 chromeos::SessionManagerClient::RetrievePolicyCallback retrieve_callback;
105 EXPECT_CALL(session_manager_client_, RetrieveUserPolicy(_)) 76 EXPECT_CALL(session_manager_client_, RetrieveUserPolicy(_))
106 .WillOnce(SaveArg<0>(&retrieve_callback)); 77 .WillOnce(SaveArg<0>(&retrieve_callback));
107 store_->Load(); 78 store_->Load();
108 RunUntilIdle(); 79 loop_.RunUntilIdle();
109 Mock::VerifyAndClearExpectations(&session_manager_client_); 80 Mock::VerifyAndClearExpectations(&session_manager_client_);
110 ASSERT_FALSE(retrieve_callback.is_null()); 81 ASSERT_FALSE(retrieve_callback.is_null());
111 82
112 // Run the callback. 83 // Run the callback.
113 retrieve_callback.Run(response); 84 retrieve_callback.Run(response);
114 RunUntilIdle();
115 }
116
117 // Verifies that store_->policy_map() has the HomepageLocation entry with
118 // the |expected_value|.
119 void VerifyPolicyMap(const char* expected_value) {
120 EXPECT_EQ(1U, store_->policy_map().size());
121 const PolicyMap::Entry* entry =
122 store_->policy_map().Get(key::kHomepageLocation);
123 ASSERT_TRUE(entry);
124 EXPECT_TRUE(base::StringValue(expected_value).Equals(entry->value));
125 }
126
127 void StoreUserPolicyKey(const std::vector<uint8>& public_key) {
128 ASSERT_TRUE(file_util::CreateDirectory(user_policy_key_file().DirName()));
129 ASSERT_TRUE(
130 file_util::WriteFile(user_policy_key_file(),
131 reinterpret_cast<const char*>(public_key.data()),
132 public_key.size()));
133 }
134
135 // Stores the current |policy_| and verifies that it is published.
136 // If |new_public_key| is set then it will be persisted after storing but
137 // before loading the policy, so that the signature validation can succeed.
138 // If |previous_value| is set then a previously existing policy with that
139 // value will be expected; otherwise no previous policy is expected.
140 // If |new_value| is set then a new policy with that value is expected after
141 // storing the |policy_| blob.
142 void PerformStorePolicy(const std::vector<uint8>* new_public_key,
143 const char* previous_value,
144 const char* new_value) {
145 chromeos::SessionManagerClient::StorePolicyCallback store_callback;
146 EXPECT_CALL(session_manager_client_, StoreUserPolicy(policy_.GetBlob(), _))
147 .WillOnce(SaveArg<1>(&store_callback));
148 store_->Store(policy_.policy());
149 RunUntilIdle();
150 Mock::VerifyAndClearExpectations(&session_manager_client_);
151 ASSERT_FALSE(store_callback.is_null());
152
153 // The new policy shouldn't be present yet.
154 PolicyMap previous_policy;
155 EXPECT_EQ(previous_value != NULL, store_->policy() != NULL);
156 if (previous_value) {
157 previous_policy.Set(key::kHomepageLocation,
158 POLICY_LEVEL_MANDATORY,
159 POLICY_SCOPE_USER,
160 base::Value::CreateStringValue(previous_value));
161 }
162 EXPECT_TRUE(previous_policy.Equals(store_->policy_map()));
163 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status());
164
165 // Store the new public key so that the validation after the retrieve
166 // operation completes can verify the signature.
167 if (new_public_key)
168 StoreUserPolicyKey(*new_public_key);
169
170 // Let the store operation complete.
171 chromeos::SessionManagerClient::RetrievePolicyCallback retrieve_callback;
172 EXPECT_CALL(session_manager_client_, RetrieveUserPolicy(_))
173 .WillOnce(SaveArg<0>(&retrieve_callback));
174 store_callback.Run(true);
175 RunUntilIdle();
176 EXPECT_TRUE(previous_policy.Equals(store_->policy_map()));
177 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status());
178 Mock::VerifyAndClearExpectations(&session_manager_client_);
179 ASSERT_FALSE(retrieve_callback.is_null());
180
181 // Finish the retrieve callback.
182 EXPECT_CALL(observer_, OnStoreLoaded(store_.get()));
183 retrieve_callback.Run(policy_.GetBlob());
184 RunUntilIdle();
185 ASSERT_TRUE(store_->policy());
186 EXPECT_EQ(policy_.policy_data().SerializeAsString(),
187 store_->policy()->SerializeAsString());
188 VerifyPolicyMap(new_value);
189 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status());
190 }
191
192 void VerifyStoreHasValidationError() {
193 EXPECT_FALSE(store_->policy());
194 EXPECT_TRUE(store_->policy_map().empty());
195 EXPECT_EQ(CloudPolicyStore::STATUS_VALIDATION_ERROR, store_->status());
196 }
197
198 void RunUntilIdle() {
199 loop_.RunUntilIdle();
200 content::BrowserThread::GetBlockingPool()->FlushForTesting();
201 loop_.RunUntilIdle(); 85 loop_.RunUntilIdle();
202 } 86 }
203 87
204 base::FilePath user_policy_dir() { 88 // Verifies that store_->policy_map() has the ShowHomeButton entry.
205 return tmp_dir_.path().AppendASCII("var_run_user_policy"); 89 void VerifyPolicyMap() {
206 } 90 EXPECT_EQ(1U, store_->policy_map().size());
207 91 const PolicyMap::Entry* entry =
208 base::FilePath user_policy_key_file() { 92 store_->policy_map().Get(key::kShowHomeButton);
209 return user_policy_dir().AppendASCII(kSanitizedUsername) 93 ASSERT_TRUE(entry);
210 .AppendASCII("policy.pub"); 94 EXPECT_TRUE(base::FundamentalValue(true).Equals(entry->value));
211 } 95 }
212 96
213 base::FilePath token_file() { 97 base::FilePath token_file() {
214 return tmp_dir_.path().AppendASCII("token"); 98 return tmp_dir_.path().AppendASCII("token");
215 } 99 }
216 100
217 base::FilePath policy_file() { 101 base::FilePath policy_file() {
218 return tmp_dir_.path().AppendASCII("policy"); 102 return tmp_dir_.path().AppendASCII("policy");
219 } 103 }
220 104
221 MessageLoop loop_; 105 MessageLoop loop_;
222 chromeos::MockCryptohomeClient cryptohome_client_;
223 chromeos::MockSessionManagerClient session_manager_client_; 106 chromeos::MockSessionManagerClient session_manager_client_;
224 UserPolicyBuilder policy_; 107 UserPolicyBuilder policy_;
225 MockCloudPolicyStoreObserver observer_; 108 MockCloudPolicyStoreObserver observer_;
226 scoped_ptr<UserCloudPolicyStoreChromeOS> store_; 109 scoped_ptr<UserCloudPolicyStoreChromeOS> store_;
227 110
228 private: 111 private:
229 content::TestBrowserThread ui_thread_; 112 content::TestBrowserThread ui_thread_;
230 content::TestBrowserThread file_thread_; 113 content::TestBrowserThread file_thread_;
231 base::ScopedTempDir tmp_dir_; 114 base::ScopedTempDir tmp_dir_;
232 115
233 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyStoreChromeOSTest); 116 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyStoreChromeOSTest);
234 }; 117 };
235 118
236 TEST_F(UserCloudPolicyStoreChromeOSTest, InitialStore) { 119 TEST_F(UserCloudPolicyStoreChromeOSTest, Store) {
237 // Start without any public key to trigger the initial key checks. 120 // Store policy.
238 ASSERT_TRUE(file_util::Delete(user_policy_key_file(), false)); 121 chromeos::SessionManagerClient::StorePolicyCallback store_callback;
239 // Make the policy blob contain a new public key. 122 EXPECT_CALL(session_manager_client_, StoreUserPolicy(policy_.GetBlob(), _))
240 policy_.set_new_signing_key(PolicyBuilder::CreateTestNewSigningKey()); 123 .WillOnce(SaveArg<1>(&store_callback));
241 policy_.Build(); 124 store_->Store(policy_.policy());
242 std::vector<uint8> new_public_key; 125 loop_.RunUntilIdle();
243 ASSERT_TRUE(policy_.new_signing_key()->ExportPublicKey(&new_public_key)); 126 Mock::VerifyAndClearExpectations(&session_manager_client_);
244 ASSERT_NO_FATAL_FAILURE( 127 ASSERT_FALSE(store_callback.is_null());
245 PerformStorePolicy(&new_public_key, NULL, kDefaultHomepage));
246 }
247 128
248 TEST_F(UserCloudPolicyStoreChromeOSTest, StoreWithExistingKey) { 129 // No policy should be present yet.
249 ASSERT_NO_FATAL_FAILURE( 130 EXPECT_FALSE(store_->policy());
250 PerformStorePolicy(NULL, NULL, kDefaultHomepage)); 131 EXPECT_TRUE(store_->policy_map().empty());
251 } 132 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status());
252 133
253 TEST_F(UserCloudPolicyStoreChromeOSTest, StoreWithRotation) { 134 // Let the store operation complete.
254 // Make the policy blob contain a new public key. 135 chromeos::SessionManagerClient::RetrievePolicyCallback retrieve_callback;
255 policy_.set_new_signing_key(PolicyBuilder::CreateTestNewSigningKey()); 136 EXPECT_CALL(session_manager_client_, RetrieveUserPolicy(_))
256 policy_.Build(); 137 .WillOnce(SaveArg<0>(&retrieve_callback));
257 std::vector<uint8> new_public_key; 138 store_callback.Run(true);
258 ASSERT_TRUE(policy_.new_signing_key()->ExportPublicKey(&new_public_key)); 139 loop_.RunUntilIdle();
259 ASSERT_NO_FATAL_FAILURE( 140 EXPECT_TRUE(store_->policy_map().empty());
260 PerformStorePolicy(&new_public_key, NULL, kDefaultHomepage)); 141 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status());
142 Mock::VerifyAndClearExpectations(&session_manager_client_);
143 ASSERT_FALSE(retrieve_callback.is_null());
144
145 // Finish the retrieve callback.
146 EXPECT_CALL(observer_, OnStoreLoaded(store_.get()));
147 retrieve_callback.Run(policy_.GetBlob());
148 loop_.RunUntilIdle();
149 ASSERT_TRUE(store_->policy());
150 EXPECT_EQ(policy_.policy_data().SerializeAsString(),
151 store_->policy()->SerializeAsString());
152 VerifyPolicyMap();
153 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status());
261 } 154 }
262 155
263 TEST_F(UserCloudPolicyStoreChromeOSTest, StoreFail) { 156 TEST_F(UserCloudPolicyStoreChromeOSTest, StoreFail) {
264 // Store policy. 157 // Store policy.
265 chromeos::SessionManagerClient::StorePolicyCallback store_callback; 158 chromeos::SessionManagerClient::StorePolicyCallback store_callback;
266 EXPECT_CALL(session_manager_client_, StoreUserPolicy(policy_.GetBlob(), _)) 159 EXPECT_CALL(session_manager_client_, StoreUserPolicy(policy_.GetBlob(), _))
267 .WillOnce(SaveArg<1>(&store_callback)); 160 .WillOnce(SaveArg<1>(&store_callback));
268 store_->Store(policy_.policy()); 161 store_->Store(policy_.policy());
269 RunUntilIdle(); 162 loop_.RunUntilIdle();
270 Mock::VerifyAndClearExpectations(&session_manager_client_); 163 Mock::VerifyAndClearExpectations(&session_manager_client_);
271 ASSERT_FALSE(store_callback.is_null()); 164 ASSERT_FALSE(store_callback.is_null());
272 165
273 // Let the store operation complete. 166 // Let the store operation complete.
274 ExpectError(CloudPolicyStore::STATUS_STORE_ERROR); 167 ExpectError(CloudPolicyStore::STATUS_STORE_ERROR);
275 store_callback.Run(false); 168 store_callback.Run(false);
276 RunUntilIdle(); 169 loop_.RunUntilIdle();
277 EXPECT_FALSE(store_->policy()); 170 EXPECT_FALSE(store_->policy());
278 EXPECT_TRUE(store_->policy_map().empty()); 171 EXPECT_TRUE(store_->policy_map().empty());
279 EXPECT_EQ(CloudPolicyStore::STATUS_STORE_ERROR, store_->status()); 172 EXPECT_EQ(CloudPolicyStore::STATUS_STORE_ERROR, store_->status());
280 } 173 }
281 174
282 TEST_F(UserCloudPolicyStoreChromeOSTest, StoreValidationError) { 175 TEST_F(UserCloudPolicyStoreChromeOSTest, StoreValidationError) {
283 policy_.policy_data().clear_policy_type(); 176 policy_.policy_data().clear_policy_type();
284 policy_.Build(); 177 policy_.Build();
285 178
286 // Store policy. 179 // Store policy.
287 chromeos::SessionManagerClient::StorePolicyCallback store_callback; 180 chromeos::SessionManagerClient::StorePolicyCallback store_callback;
288 ExpectError(CloudPolicyStore::STATUS_VALIDATION_ERROR); 181 ExpectError(CloudPolicyStore::STATUS_VALIDATION_ERROR);
289 EXPECT_CALL(session_manager_client_, StoreUserPolicy(policy_.GetBlob(), _)) 182 EXPECT_CALL(session_manager_client_, StoreUserPolicy(policy_.GetBlob(), _))
290 .Times(0); 183 .Times(0);
291 store_->Store(policy_.policy()); 184 store_->Store(policy_.policy());
292 RunUntilIdle(); 185 loop_.RunUntilIdle();
293 Mock::VerifyAndClearExpectations(&session_manager_client_);
294 }
295
296 TEST_F(UserCloudPolicyStoreChromeOSTest, StoreWithoutPolicyKey) {
297 // Make the dbus call to cryptohome fail.
298 Mock::VerifyAndClearExpectations(&cryptohome_client_);
299 EXPECT_CALL(cryptohome_client_,
300 GetSanitizedUsername(PolicyBuilder::kFakeUsername, _))
301 .Times(AnyNumber())
302 .WillRepeatedly(SendSanitizedUsername(chromeos::DBUS_METHOD_CALL_FAILURE,
303 std::string()));
304
305 // Store policy.
306 chromeos::SessionManagerClient::StorePolicyCallback store_callback;
307 ExpectError(CloudPolicyStore::STATUS_VALIDATION_ERROR);
308 EXPECT_CALL(session_manager_client_, StoreUserPolicy(policy_.GetBlob(), _))
309 .Times(0);
310 store_->Store(policy_.policy());
311 RunUntilIdle();
312 Mock::VerifyAndClearExpectations(&session_manager_client_);
313 }
314
315 TEST_F(UserCloudPolicyStoreChromeOSTest, StoreWithInvalidSignature) {
316 // Break the signature.
317 policy_.policy().mutable_policy_data_signature()->append("garbage");
318
319 // Store policy.
320 chromeos::SessionManagerClient::StorePolicyCallback store_callback;
321 ExpectError(CloudPolicyStore::STATUS_VALIDATION_ERROR);
322 EXPECT_CALL(session_manager_client_, StoreUserPolicy(policy_.GetBlob(), _))
323 .Times(0);
324 store_->Store(policy_.policy());
325 RunUntilIdle();
326 Mock::VerifyAndClearExpectations(&session_manager_client_); 186 Mock::VerifyAndClearExpectations(&session_manager_client_);
327 } 187 }
328 188
329 TEST_F(UserCloudPolicyStoreChromeOSTest, Load) { 189 TEST_F(UserCloudPolicyStoreChromeOSTest, Load) {
330 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); 190 EXPECT_CALL(observer_, OnStoreLoaded(store_.get()));
331 ASSERT_NO_FATAL_FAILURE(PerformPolicyLoad(policy_.GetBlob())); 191 ASSERT_NO_FATAL_FAILURE(PerformPolicyLoad(policy_.GetBlob()));
332 Mock::VerifyAndClearExpectations(&observer_); 192 Mock::VerifyAndClearExpectations(&observer_);
333 193
334 // Verify that the policy has been loaded. 194 // Verify that the policy has been loaded.
335 ASSERT_TRUE(store_->policy()); 195 ASSERT_TRUE(store_->policy());
336 EXPECT_EQ(policy_.policy_data().SerializeAsString(), 196 EXPECT_EQ(policy_.policy_data().SerializeAsString(),
337 store_->policy()->SerializeAsString()); 197 store_->policy()->SerializeAsString());
338 VerifyPolicyMap(kDefaultHomepage); 198 VerifyPolicyMap();
339 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); 199 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status());
340 } 200 }
341 201
342 TEST_F(UserCloudPolicyStoreChromeOSTest, LoadNoPolicy) { 202 TEST_F(UserCloudPolicyStoreChromeOSTest, LoadNoPolicy) {
343 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); 203 EXPECT_CALL(observer_, OnStoreLoaded(store_.get()));
344 ASSERT_NO_FATAL_FAILURE(PerformPolicyLoad("")); 204 ASSERT_NO_FATAL_FAILURE(PerformPolicyLoad(""));
345 Mock::VerifyAndClearExpectations(&observer_); 205 Mock::VerifyAndClearExpectations(&observer_);
346 206
347 // Verify no policy has been installed. 207 // Verify no policy has been installed.
348 EXPECT_FALSE(store_->policy()); 208 EXPECT_FALSE(store_->policy());
(...skipping 10 matching lines...) Expand all
359 EXPECT_TRUE(store_->policy_map().empty()); 219 EXPECT_TRUE(store_->policy_map().empty());
360 EXPECT_EQ(CloudPolicyStore::STATUS_PARSE_ERROR, store_->status()); 220 EXPECT_EQ(CloudPolicyStore::STATUS_PARSE_ERROR, store_->status());
361 } 221 }
362 222
363 TEST_F(UserCloudPolicyStoreChromeOSTest, LoadValidationError) { 223 TEST_F(UserCloudPolicyStoreChromeOSTest, LoadValidationError) {
364 policy_.policy_data().clear_policy_type(); 224 policy_.policy_data().clear_policy_type();
365 policy_.Build(); 225 policy_.Build();
366 226
367 ExpectError(CloudPolicyStore::STATUS_VALIDATION_ERROR); 227 ExpectError(CloudPolicyStore::STATUS_VALIDATION_ERROR);
368 ASSERT_NO_FATAL_FAILURE(PerformPolicyLoad(policy_.GetBlob())); 228 ASSERT_NO_FATAL_FAILURE(PerformPolicyLoad(policy_.GetBlob()));
369 VerifyStoreHasValidationError();
370 }
371 229
372 TEST_F(UserCloudPolicyStoreChromeOSTest, LoadNoKey) { 230 // Verify no policy has been installed.
373 // The loaded policy can't be verified without the public key. 231 EXPECT_FALSE(store_->policy());
374 ASSERT_TRUE(file_util::Delete(user_policy_key_file(), false)); 232 EXPECT_TRUE(store_->policy_map().empty());
375 ExpectError(CloudPolicyStore::STATUS_VALIDATION_ERROR); 233 EXPECT_EQ(CloudPolicyStore::STATUS_VALIDATION_ERROR, store_->status());
376 ASSERT_NO_FATAL_FAILURE(PerformPolicyLoad(policy_.GetBlob()));
377 VerifyStoreHasValidationError();
378 }
379
380 TEST_F(UserCloudPolicyStoreChromeOSTest, LoadInvalidSignature) {
381 // Break the signature.
382 policy_.policy().mutable_policy_data_signature()->append("garbage");
383 ExpectError(CloudPolicyStore::STATUS_VALIDATION_ERROR);
384 ASSERT_NO_FATAL_FAILURE(PerformPolicyLoad(policy_.GetBlob()));
385 VerifyStoreHasValidationError();
386 } 234 }
387 235
388 TEST_F(UserCloudPolicyStoreChromeOSTest, MigrationFull) { 236 TEST_F(UserCloudPolicyStoreChromeOSTest, MigrationFull) {
389 std::string data; 237 std::string data;
390 238
391 em::DeviceCredentials credentials; 239 em::DeviceCredentials credentials;
392 credentials.set_device_token(kLegacyToken); 240 credentials.set_device_token(kLegacyToken);
393 credentials.set_device_id(kLegacyDeviceId); 241 credentials.set_device_id(kLegacyDeviceId);
394 ASSERT_TRUE(credentials.SerializeToString(&data)); 242 ASSERT_TRUE(credentials.SerializeToString(&data));
395 ASSERT_NE(-1, file_util::WriteFile(token_file(), data.c_str(), data.size())); 243 ASSERT_NE(-1, file_util::WriteFile(token_file(), data.c_str(), data.size()));
(...skipping 10 matching lines...) Expand all
406 // Verify that legacy user policy and token have been loaded. 254 // Verify that legacy user policy and token have been loaded.
407 em::PolicyData expected_policy_data; 255 em::PolicyData expected_policy_data;
408 EXPECT_TRUE(expected_policy_data.ParseFromString( 256 EXPECT_TRUE(expected_policy_data.ParseFromString(
409 cached_policy.cloud_policy().policy_data())); 257 cached_policy.cloud_policy().policy_data()));
410 expected_policy_data.clear_public_key_version(); 258 expected_policy_data.clear_public_key_version();
411 expected_policy_data.set_request_token(kLegacyToken); 259 expected_policy_data.set_request_token(kLegacyToken);
412 expected_policy_data.set_device_id(kLegacyDeviceId); 260 expected_policy_data.set_device_id(kLegacyDeviceId);
413 ASSERT_TRUE(store_->policy()); 261 ASSERT_TRUE(store_->policy());
414 EXPECT_EQ(expected_policy_data.SerializeAsString(), 262 EXPECT_EQ(expected_policy_data.SerializeAsString(),
415 store_->policy()->SerializeAsString()); 263 store_->policy()->SerializeAsString());
416 VerifyPolicyMap(kDefaultHomepage); 264 VerifyPolicyMap();
417 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); 265 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status());
418 } 266 };
419 267
420 TEST_F(UserCloudPolicyStoreChromeOSTest, MigrationNoToken) { 268 TEST_F(UserCloudPolicyStoreChromeOSTest, MigrationNoToken) {
421 std::string data; 269 std::string data;
422 testing::Sequence seq; 270 testing::Sequence seq;
423 271
424 em::CachedCloudPolicyResponse cached_policy; 272 em::CachedCloudPolicyResponse cached_policy;
425 cached_policy.mutable_cloud_policy()->CopyFrom(policy_.policy()); 273 cached_policy.mutable_cloud_policy()->CopyFrom(policy_.policy());
426 ASSERT_TRUE(cached_policy.SerializeToString(&data)); 274 ASSERT_TRUE(cached_policy.SerializeToString(&data));
427 ASSERT_NE(-1, file_util::WriteFile(policy_file(), data.c_str(), data.size())); 275 ASSERT_NE(-1, file_util::WriteFile(policy_file(), data.c_str(), data.size()));
428 276
429 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); 277 EXPECT_CALL(observer_, OnStoreLoaded(store_.get()));
430 ASSERT_NO_FATAL_FAILURE(PerformPolicyLoad("")); 278 ASSERT_NO_FATAL_FAILURE(PerformPolicyLoad(""));
431 Mock::VerifyAndClearExpectations(&observer_); 279 Mock::VerifyAndClearExpectations(&observer_);
432 280
433 // Verify the legacy cache has been loaded. 281 // Verify the legacy cache has been loaded.
434 em::PolicyData expected_policy_data; 282 em::PolicyData expected_policy_data;
435 EXPECT_TRUE(expected_policy_data.ParseFromString( 283 EXPECT_TRUE(expected_policy_data.ParseFromString(
436 cached_policy.cloud_policy().policy_data())); 284 cached_policy.cloud_policy().policy_data()));
437 expected_policy_data.clear_public_key_version(); 285 expected_policy_data.clear_public_key_version();
438 ASSERT_TRUE(store_->policy()); 286 ASSERT_TRUE(store_->policy());
439 EXPECT_EQ(expected_policy_data.SerializeAsString(), 287 EXPECT_EQ(expected_policy_data.SerializeAsString(),
440 store_->policy()->SerializeAsString()); 288 store_->policy()->SerializeAsString());
441 VerifyPolicyMap(kDefaultHomepage); 289 VerifyPolicyMap();
442 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); 290 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status());
443 } 291 };
444 292
445 TEST_F(UserCloudPolicyStoreChromeOSTest, MigrationNoPolicy) { 293 TEST_F(UserCloudPolicyStoreChromeOSTest, MigrationNoPolicy) {
446 std::string data; 294 std::string data;
447 295
448 em::DeviceCredentials credentials; 296 em::DeviceCredentials credentials;
449 credentials.set_device_token(kLegacyToken); 297 credentials.set_device_token(kLegacyToken);
450 credentials.set_device_id(kLegacyDeviceId); 298 credentials.set_device_id(kLegacyDeviceId);
451 ASSERT_TRUE(credentials.SerializeToString(&data)); 299 ASSERT_TRUE(credentials.SerializeToString(&data));
452 ASSERT_NE(-1, file_util::WriteFile(token_file(), data.c_str(), data.size())); 300 ASSERT_NE(-1, file_util::WriteFile(token_file(), data.c_str(), data.size()));
453 301
454 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); 302 EXPECT_CALL(observer_, OnStoreLoaded(store_.get()));
455 ASSERT_NO_FATAL_FAILURE(PerformPolicyLoad("")); 303 ASSERT_NO_FATAL_FAILURE(PerformPolicyLoad(""));
456 Mock::VerifyAndClearExpectations(&observer_); 304 Mock::VerifyAndClearExpectations(&observer_);
457 305
458 // Verify that legacy user policy and token have been loaded. 306 // Verify that legacy user policy and token have been loaded.
459 em::PolicyData expected_policy_data; 307 em::PolicyData expected_policy_data;
460 expected_policy_data.set_request_token(kLegacyToken); 308 expected_policy_data.set_request_token(kLegacyToken);
461 expected_policy_data.set_device_id(kLegacyDeviceId); 309 expected_policy_data.set_device_id(kLegacyDeviceId);
462 ASSERT_TRUE(store_->policy()); 310 ASSERT_TRUE(store_->policy());
463 EXPECT_EQ(expected_policy_data.SerializeAsString(), 311 EXPECT_EQ(expected_policy_data.SerializeAsString(),
464 store_->policy()->SerializeAsString()); 312 store_->policy()->SerializeAsString());
465 EXPECT_TRUE(store_->policy_map().empty()); 313 EXPECT_TRUE(store_->policy_map().empty());
466 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); 314 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status());
467 } 315 };
468
469 TEST_F(UserCloudPolicyStoreChromeOSTest, MigrationAndStoreNew) {
470 // Start without an existing public key.
471 ASSERT_TRUE(file_util::Delete(user_policy_key_file(), false));
472
473 std::string data;
474 em::CachedCloudPolicyResponse cached_policy;
475 cached_policy.mutable_cloud_policy()->CopyFrom(policy_.policy());
476 ASSERT_TRUE(cached_policy.SerializeToString(&data));
477 ASSERT_NE(-1, file_util::WriteFile(policy_file(), data.c_str(), data.size()));
478
479 EXPECT_CALL(observer_, OnStoreLoaded(store_.get()));
480 ASSERT_NO_FATAL_FAILURE(PerformPolicyLoad(""));
481 Mock::VerifyAndClearExpectations(&observer_);
482
483 // Verify the legacy cache has been loaded.
484 em::PolicyData expected_policy_data;
485 EXPECT_TRUE(expected_policy_data.ParseFromString(
486 cached_policy.cloud_policy().policy_data()));
487 expected_policy_data.clear_public_key_version();
488 ASSERT_TRUE(store_->policy());
489 EXPECT_EQ(expected_policy_data.SerializeAsString(),
490 store_->policy()->SerializeAsString());
491 VerifyPolicyMap(kDefaultHomepage);
492 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status());
493 EXPECT_TRUE(file_util::PathExists(policy_file()));
494
495 // Now store a new policy using the new homepage location.
496 const char kNewHomepage[] = "http://google.com";
497 policy_.payload().mutable_homepagelocation()->set_value(kNewHomepage);
498 policy_.set_new_signing_key(PolicyBuilder::CreateTestNewSigningKey());
499 policy_.Build();
500 std::vector<uint8> new_public_key;
501 ASSERT_TRUE(policy_.new_signing_key()->ExportPublicKey(&new_public_key));
502 ASSERT_NO_FATAL_FAILURE(
503 PerformStorePolicy(&new_public_key, kDefaultHomepage, kNewHomepage));
504 VerifyPolicyMap(kNewHomepage);
505
506 // Verify that the legacy cache has been removed.
507 EXPECT_FALSE(file_util::PathExists(policy_file()));
508 }
509 316
510 } // namespace 317 } // namespace
511 318
512 } // namespace policy 319 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/user_cloud_policy_store_chromeos.cc ('k') | chrome/common/chrome_paths.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698