OLD | NEW |
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/chromeos/policy/user_cloud_policy_store_chromeos.h" | 5 #include "chrome/browser/chromeos/policy/user_cloud_policy_store_chromeos.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 EXPECT_EQ(1U, store_->policy_map().size()); | 120 EXPECT_EQ(1U, store_->policy_map().size()); |
121 const PolicyMap::Entry* entry = | 121 const PolicyMap::Entry* entry = |
122 store_->policy_map().Get(key::kHomepageLocation); | 122 store_->policy_map().Get(key::kHomepageLocation); |
123 ASSERT_TRUE(entry); | 123 ASSERT_TRUE(entry); |
124 EXPECT_TRUE(base::StringValue(expected_value).Equals(entry->value)); | 124 EXPECT_TRUE(base::StringValue(expected_value).Equals(entry->value)); |
125 } | 125 } |
126 | 126 |
127 void StoreUserPolicyKey(const std::vector<uint8>& public_key) { | 127 void StoreUserPolicyKey(const std::vector<uint8>& public_key) { |
128 ASSERT_TRUE(base::CreateDirectory(user_policy_key_file().DirName())); | 128 ASSERT_TRUE(base::CreateDirectory(user_policy_key_file().DirName())); |
129 ASSERT_TRUE( | 129 ASSERT_TRUE( |
130 file_util::WriteFile(user_policy_key_file(), | 130 base::WriteFile(user_policy_key_file(), |
131 reinterpret_cast<const char*>(public_key.data()), | 131 reinterpret_cast<const char*>(public_key.data()), |
132 public_key.size())); | 132 public_key.size())); |
133 } | 133 } |
134 | 134 |
135 // Stores the current |policy_| and verifies that it is published. | 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 | 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. | 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 | 138 // If |previous_value| is set then a previously existing policy with that |
139 // value will be expected; otherwise no previous policy is expected. | 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 | 140 // If |new_value| is set then a new policy with that value is expected after |
141 // storing the |policy_| blob. | 141 // storing the |policy_| blob. |
142 void PerformStorePolicy(const std::vector<uint8>* new_public_key, | 142 void PerformStorePolicy(const std::vector<uint8>* new_public_key, |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 VerifyStoreHasValidationError(); | 393 VerifyStoreHasValidationError(); |
394 } | 394 } |
395 | 395 |
396 TEST_F(UserCloudPolicyStoreChromeOSTest, MigrationFull) { | 396 TEST_F(UserCloudPolicyStoreChromeOSTest, MigrationFull) { |
397 std::string data; | 397 std::string data; |
398 | 398 |
399 em::DeviceCredentials credentials; | 399 em::DeviceCredentials credentials; |
400 credentials.set_device_token(kLegacyToken); | 400 credentials.set_device_token(kLegacyToken); |
401 credentials.set_device_id(kLegacyDeviceId); | 401 credentials.set_device_id(kLegacyDeviceId); |
402 ASSERT_TRUE(credentials.SerializeToString(&data)); | 402 ASSERT_TRUE(credentials.SerializeToString(&data)); |
403 ASSERT_NE(-1, file_util::WriteFile(token_file(), data.c_str(), data.size())); | 403 ASSERT_NE(-1, base::WriteFile(token_file(), data.c_str(), data.size())); |
404 | 404 |
405 em::CachedCloudPolicyResponse cached_policy; | 405 em::CachedCloudPolicyResponse cached_policy; |
406 cached_policy.mutable_cloud_policy()->CopyFrom(policy_.policy()); | 406 cached_policy.mutable_cloud_policy()->CopyFrom(policy_.policy()); |
407 ASSERT_TRUE(cached_policy.SerializeToString(&data)); | 407 ASSERT_TRUE(cached_policy.SerializeToString(&data)); |
408 ASSERT_NE(-1, file_util::WriteFile(policy_file(), data.c_str(), data.size())); | 408 ASSERT_NE(-1, base::WriteFile(policy_file(), data.c_str(), data.size())); |
409 | 409 |
410 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | 410 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); |
411 ASSERT_NO_FATAL_FAILURE(PerformPolicyLoad("")); | 411 ASSERT_NO_FATAL_FAILURE(PerformPolicyLoad("")); |
412 Mock::VerifyAndClearExpectations(&observer_); | 412 Mock::VerifyAndClearExpectations(&observer_); |
413 | 413 |
414 // Verify that legacy user policy and token have been loaded. | 414 // Verify that legacy user policy and token have been loaded. |
415 em::PolicyData expected_policy_data; | 415 em::PolicyData expected_policy_data; |
416 EXPECT_TRUE(expected_policy_data.ParseFromString( | 416 EXPECT_TRUE(expected_policy_data.ParseFromString( |
417 cached_policy.cloud_policy().policy_data())); | 417 cached_policy.cloud_policy().policy_data())); |
418 expected_policy_data.clear_public_key_version(); | 418 expected_policy_data.clear_public_key_version(); |
419 expected_policy_data.set_request_token(kLegacyToken); | 419 expected_policy_data.set_request_token(kLegacyToken); |
420 expected_policy_data.set_device_id(kLegacyDeviceId); | 420 expected_policy_data.set_device_id(kLegacyDeviceId); |
421 ASSERT_TRUE(store_->policy()); | 421 ASSERT_TRUE(store_->policy()); |
422 EXPECT_EQ(expected_policy_data.SerializeAsString(), | 422 EXPECT_EQ(expected_policy_data.SerializeAsString(), |
423 store_->policy()->SerializeAsString()); | 423 store_->policy()->SerializeAsString()); |
424 VerifyPolicyMap(kDefaultHomepage); | 424 VerifyPolicyMap(kDefaultHomepage); |
425 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); | 425 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); |
426 } | 426 } |
427 | 427 |
428 TEST_F(UserCloudPolicyStoreChromeOSTest, MigrationNoToken) { | 428 TEST_F(UserCloudPolicyStoreChromeOSTest, MigrationNoToken) { |
429 std::string data; | 429 std::string data; |
430 testing::Sequence seq; | 430 testing::Sequence seq; |
431 | 431 |
432 em::CachedCloudPolicyResponse cached_policy; | 432 em::CachedCloudPolicyResponse cached_policy; |
433 cached_policy.mutable_cloud_policy()->CopyFrom(policy_.policy()); | 433 cached_policy.mutable_cloud_policy()->CopyFrom(policy_.policy()); |
434 ASSERT_TRUE(cached_policy.SerializeToString(&data)); | 434 ASSERT_TRUE(cached_policy.SerializeToString(&data)); |
435 ASSERT_NE(-1, file_util::WriteFile(policy_file(), data.c_str(), data.size())); | 435 ASSERT_NE(-1, base::WriteFile(policy_file(), data.c_str(), data.size())); |
436 | 436 |
437 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | 437 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); |
438 ASSERT_NO_FATAL_FAILURE(PerformPolicyLoad("")); | 438 ASSERT_NO_FATAL_FAILURE(PerformPolicyLoad("")); |
439 Mock::VerifyAndClearExpectations(&observer_); | 439 Mock::VerifyAndClearExpectations(&observer_); |
440 | 440 |
441 // Verify the legacy cache has been loaded. | 441 // Verify the legacy cache has been loaded. |
442 em::PolicyData expected_policy_data; | 442 em::PolicyData expected_policy_data; |
443 EXPECT_TRUE(expected_policy_data.ParseFromString( | 443 EXPECT_TRUE(expected_policy_data.ParseFromString( |
444 cached_policy.cloud_policy().policy_data())); | 444 cached_policy.cloud_policy().policy_data())); |
445 expected_policy_data.clear_public_key_version(); | 445 expected_policy_data.clear_public_key_version(); |
446 ASSERT_TRUE(store_->policy()); | 446 ASSERT_TRUE(store_->policy()); |
447 EXPECT_EQ(expected_policy_data.SerializeAsString(), | 447 EXPECT_EQ(expected_policy_data.SerializeAsString(), |
448 store_->policy()->SerializeAsString()); | 448 store_->policy()->SerializeAsString()); |
449 VerifyPolicyMap(kDefaultHomepage); | 449 VerifyPolicyMap(kDefaultHomepage); |
450 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); | 450 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); |
451 } | 451 } |
452 | 452 |
453 TEST_F(UserCloudPolicyStoreChromeOSTest, MigrationNoPolicy) { | 453 TEST_F(UserCloudPolicyStoreChromeOSTest, MigrationNoPolicy) { |
454 std::string data; | 454 std::string data; |
455 | 455 |
456 em::DeviceCredentials credentials; | 456 em::DeviceCredentials credentials; |
457 credentials.set_device_token(kLegacyToken); | 457 credentials.set_device_token(kLegacyToken); |
458 credentials.set_device_id(kLegacyDeviceId); | 458 credentials.set_device_id(kLegacyDeviceId); |
459 ASSERT_TRUE(credentials.SerializeToString(&data)); | 459 ASSERT_TRUE(credentials.SerializeToString(&data)); |
460 ASSERT_NE(-1, file_util::WriteFile(token_file(), data.c_str(), data.size())); | 460 ASSERT_NE(-1, base::WriteFile(token_file(), data.c_str(), data.size())); |
461 | 461 |
462 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | 462 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); |
463 ASSERT_NO_FATAL_FAILURE(PerformPolicyLoad("")); | 463 ASSERT_NO_FATAL_FAILURE(PerformPolicyLoad("")); |
464 Mock::VerifyAndClearExpectations(&observer_); | 464 Mock::VerifyAndClearExpectations(&observer_); |
465 | 465 |
466 // Verify that legacy user policy and token have been loaded. | 466 // Verify that legacy user policy and token have been loaded. |
467 em::PolicyData expected_policy_data; | 467 em::PolicyData expected_policy_data; |
468 expected_policy_data.set_request_token(kLegacyToken); | 468 expected_policy_data.set_request_token(kLegacyToken); |
469 expected_policy_data.set_device_id(kLegacyDeviceId); | 469 expected_policy_data.set_device_id(kLegacyDeviceId); |
470 ASSERT_TRUE(store_->policy()); | 470 ASSERT_TRUE(store_->policy()); |
471 EXPECT_EQ(expected_policy_data.SerializeAsString(), | 471 EXPECT_EQ(expected_policy_data.SerializeAsString(), |
472 store_->policy()->SerializeAsString()); | 472 store_->policy()->SerializeAsString()); |
473 EXPECT_TRUE(store_->policy_map().empty()); | 473 EXPECT_TRUE(store_->policy_map().empty()); |
474 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); | 474 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); |
475 } | 475 } |
476 | 476 |
477 TEST_F(UserCloudPolicyStoreChromeOSTest, MigrationAndStoreNew) { | 477 TEST_F(UserCloudPolicyStoreChromeOSTest, MigrationAndStoreNew) { |
478 // Start without an existing public key. | 478 // Start without an existing public key. |
479 ASSERT_TRUE(base::DeleteFile(user_policy_key_file(), false)); | 479 ASSERT_TRUE(base::DeleteFile(user_policy_key_file(), false)); |
480 | 480 |
481 std::string data; | 481 std::string data; |
482 em::CachedCloudPolicyResponse cached_policy; | 482 em::CachedCloudPolicyResponse cached_policy; |
483 cached_policy.mutable_cloud_policy()->CopyFrom(policy_.policy()); | 483 cached_policy.mutable_cloud_policy()->CopyFrom(policy_.policy()); |
484 ASSERT_TRUE(cached_policy.SerializeToString(&data)); | 484 ASSERT_TRUE(cached_policy.SerializeToString(&data)); |
485 ASSERT_NE(-1, file_util::WriteFile(policy_file(), data.c_str(), data.size())); | 485 ASSERT_NE(-1, base::WriteFile(policy_file(), data.c_str(), data.size())); |
486 | 486 |
487 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | 487 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); |
488 ASSERT_NO_FATAL_FAILURE(PerformPolicyLoad("")); | 488 ASSERT_NO_FATAL_FAILURE(PerformPolicyLoad("")); |
489 Mock::VerifyAndClearExpectations(&observer_); | 489 Mock::VerifyAndClearExpectations(&observer_); |
490 | 490 |
491 // Verify the legacy cache has been loaded. | 491 // Verify the legacy cache has been loaded. |
492 em::PolicyData expected_policy_data; | 492 em::PolicyData expected_policy_data; |
493 EXPECT_TRUE(expected_policy_data.ParseFromString( | 493 EXPECT_TRUE(expected_policy_data.ParseFromString( |
494 cached_policy.cloud_policy().policy_data())); | 494 cached_policy.cloud_policy().policy_data())); |
495 expected_policy_data.clear_public_key_version(); | 495 expected_policy_data.clear_public_key_version(); |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
609 Mock::VerifyAndClearExpectations(&cryptohome_client_); | 609 Mock::VerifyAndClearExpectations(&cryptohome_client_); |
610 | 610 |
611 EXPECT_FALSE(store_->policy()); | 611 EXPECT_FALSE(store_->policy()); |
612 EXPECT_TRUE(store_->policy_map().empty()); | 612 EXPECT_TRUE(store_->policy_map().empty()); |
613 EXPECT_EQ(CloudPolicyStore::STATUS_VALIDATION_ERROR, store_->status()); | 613 EXPECT_EQ(CloudPolicyStore::STATUS_VALIDATION_ERROR, store_->status()); |
614 } | 614 } |
615 | 615 |
616 } // namespace | 616 } // namespace |
617 | 617 |
618 } // namespace policy | 618 } // namespace policy |
OLD | NEW |