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 <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
(...skipping 16 matching lines...) Expand all Loading... | |
27 #include "components/policy/core/common/cloud/policy_builder.h" | 27 #include "components/policy/core/common/cloud/policy_builder.h" |
28 #include "components/policy/core/common/policy_types.h" | 28 #include "components/policy/core/common/policy_types.h" |
29 #include "components/policy/policy_constants.h" | 29 #include "components/policy/policy_constants.h" |
30 #include "components/policy/proto/cloud_policy.pb.h" | 30 #include "components/policy/proto/cloud_policy.pb.h" |
31 #include "crypto/rsa_private_key.h" | 31 #include "crypto/rsa_private_key.h" |
32 #include "testing/gmock/include/gmock/gmock.h" | 32 #include "testing/gmock/include/gmock/gmock.h" |
33 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
34 | 34 |
35 namespace em = enterprise_management; | 35 namespace em = enterprise_management; |
36 | 36 |
37 using RetrievePolicyResponseType = | |
38 chromeos::SessionManagerClient::RetrievePolicyResponseType; | |
39 | |
37 using testing::AllOf; | 40 using testing::AllOf; |
38 using testing::AnyNumber; | 41 using testing::AnyNumber; |
39 using testing::Eq; | 42 using testing::Eq; |
40 using testing::Mock; | 43 using testing::Mock; |
41 using testing::Property; | 44 using testing::Property; |
42 using testing::Return; | 45 using testing::Return; |
43 using testing::SaveArg; | 46 using testing::SaveArg; |
47 using testing::SetArgumentPointee; | |
44 using testing::_; | 48 using testing::_; |
45 | 49 |
46 namespace policy { | 50 namespace policy { |
47 | 51 |
48 namespace { | 52 namespace { |
49 | 53 |
50 const char kSanitizedUsername[] = | 54 const char kSanitizedUsername[] = |
51 "0123456789ABCDEF0123456789ABCDEF012345678@example.com"; | 55 "0123456789ABCDEF0123456789ABCDEF012345678@example.com"; |
52 const char kDefaultHomepage[] = "http://chromium.org"; | 56 const char kDefaultHomepage[] = "http://chromium.org"; |
53 | 57 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
103 chromeos::SessionManagerClient::RetrievePolicyCallback retrieve_callback; | 107 chromeos::SessionManagerClient::RetrievePolicyCallback retrieve_callback; |
104 EXPECT_CALL(session_manager_client_, | 108 EXPECT_CALL(session_manager_client_, |
105 RetrievePolicyForUser(cryptohome_id_, _)) | 109 RetrievePolicyForUser(cryptohome_id_, _)) |
106 .WillOnce(SaveArg<1>(&retrieve_callback)); | 110 .WillOnce(SaveArg<1>(&retrieve_callback)); |
107 store_->Load(); | 111 store_->Load(); |
108 base::RunLoop().RunUntilIdle(); | 112 base::RunLoop().RunUntilIdle(); |
109 Mock::VerifyAndClearExpectations(&session_manager_client_); | 113 Mock::VerifyAndClearExpectations(&session_manager_client_); |
110 ASSERT_FALSE(retrieve_callback.is_null()); | 114 ASSERT_FALSE(retrieve_callback.is_null()); |
111 | 115 |
112 // Run the callback. | 116 // Run the callback. |
113 retrieve_callback.Run(response); | 117 retrieve_callback.Run(response, RetrievePolicyResponseType::SUCCESS); |
114 base::RunLoop().RunUntilIdle(); | 118 base::RunLoop().RunUntilIdle(); |
115 } | 119 } |
116 | 120 |
117 // Verifies that store_->policy_map() has the HomepageLocation entry with | 121 // Verifies that store_->policy_map() has the HomepageLocation entry with |
118 // the |expected_value|. | 122 // the |expected_value|. |
119 void VerifyPolicyMap(const char* expected_value) { | 123 void VerifyPolicyMap(const char* expected_value) { |
120 EXPECT_EQ(1U, store_->policy_map().size()); | 124 EXPECT_EQ(1U, store_->policy_map().size()); |
121 const PolicyMap::Entry* entry = | 125 const PolicyMap::Entry* entry = |
122 store_->policy_map().Get(key::kHomepageLocation); | 126 store_->policy_map().Get(key::kHomepageLocation); |
123 ASSERT_TRUE(entry); | 127 ASSERT_TRUE(entry); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
176 .WillOnce(SaveArg<1>(&retrieve_callback)); | 180 .WillOnce(SaveArg<1>(&retrieve_callback)); |
177 store_callback.Run(true); | 181 store_callback.Run(true); |
178 base::RunLoop().RunUntilIdle(); | 182 base::RunLoop().RunUntilIdle(); |
179 EXPECT_TRUE(previous_policy.Equals(store_->policy_map())); | 183 EXPECT_TRUE(previous_policy.Equals(store_->policy_map())); |
180 EXPECT_EQ(initial_status, store_->status()); | 184 EXPECT_EQ(initial_status, store_->status()); |
181 Mock::VerifyAndClearExpectations(&session_manager_client_); | 185 Mock::VerifyAndClearExpectations(&session_manager_client_); |
182 ASSERT_FALSE(retrieve_callback.is_null()); | 186 ASSERT_FALSE(retrieve_callback.is_null()); |
183 | 187 |
184 // Finish the retrieve callback. | 188 // Finish the retrieve callback. |
185 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | 189 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); |
186 retrieve_callback.Run(policy_.GetBlob()); | 190 retrieve_callback.Run(policy_.GetBlob(), |
191 RetrievePolicyResponseType::SUCCESS); | |
187 base::RunLoop().RunUntilIdle(); | 192 base::RunLoop().RunUntilIdle(); |
188 ASSERT_TRUE(store_->policy()); | 193 ASSERT_TRUE(store_->policy()); |
189 EXPECT_EQ(policy_.policy_data().SerializeAsString(), | 194 EXPECT_EQ(policy_.policy_data().SerializeAsString(), |
190 store_->policy()->SerializeAsString()); | 195 store_->policy()->SerializeAsString()); |
191 VerifyPolicyMap(new_value); | 196 VerifyPolicyMap(new_value); |
192 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); | 197 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); |
193 } | 198 } |
194 | 199 |
195 void VerifyStoreHasValidationError() { | 200 void VerifyStoreHasValidationError() { |
196 EXPECT_FALSE(store_->policy()); | 201 EXPECT_FALSE(store_->policy()); |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
483 TEST_F(UserCloudPolicyStoreChromeOSTest, LoadInvalidSignature) { | 488 TEST_F(UserCloudPolicyStoreChromeOSTest, LoadInvalidSignature) { |
484 // Break the signature. | 489 // Break the signature. |
485 policy_.policy().mutable_policy_data_signature()->append("garbage"); | 490 policy_.policy().mutable_policy_data_signature()->append("garbage"); |
486 ExpectError(CloudPolicyStore::STATUS_VALIDATION_ERROR); | 491 ExpectError(CloudPolicyStore::STATUS_VALIDATION_ERROR); |
487 ASSERT_NO_FATAL_FAILURE(PerformPolicyLoad(policy_.GetBlob())); | 492 ASSERT_NO_FATAL_FAILURE(PerformPolicyLoad(policy_.GetBlob())); |
488 VerifyStoreHasValidationError(); | 493 VerifyStoreHasValidationError(); |
489 EXPECT_EQ(std::string(), store_->policy_signature_public_key()); | 494 EXPECT_EQ(std::string(), store_->policy_signature_public_key()); |
490 } | 495 } |
491 | 496 |
492 TEST_F(UserCloudPolicyStoreChromeOSTest, LoadImmediately) { | 497 TEST_F(UserCloudPolicyStoreChromeOSTest, LoadImmediately) { |
498 const std::string policy_data = policy_.GetBlob(); | |
493 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | 499 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); |
494 EXPECT_CALL(session_manager_client_, | 500 EXPECT_CALL(session_manager_client_, |
495 BlockingRetrievePolicyForUser(cryptohome_id_)) | 501 BlockingRetrievePolicyForUser(cryptohome_id_, _)) |
496 .WillOnce(Return(policy_.GetBlob())); | 502 .WillOnce(DoAll(SetArgumentPointee<1>(policy_data), |
emaxx
2017/04/28 12:48:22
gmock's changelog says that SetArgumentPointee is
igorcov
2017/04/28 13:49:05
Done.
| |
503 Return(RetrievePolicyResponseType::SUCCESS))); | |
497 EXPECT_CALL(cryptohome_client_, BlockingGetSanitizedUsername(cryptohome_id_)) | 504 EXPECT_CALL(cryptohome_client_, BlockingGetSanitizedUsername(cryptohome_id_)) |
498 .WillOnce(Return(kSanitizedUsername)); | 505 .WillOnce(Return(kSanitizedUsername)); |
499 | 506 |
500 EXPECT_FALSE(store_->policy()); | 507 EXPECT_FALSE(store_->policy()); |
501 store_->LoadImmediately(); | 508 store_->LoadImmediately(); |
502 // Note: verify that the |observer_| got notified synchronously, without | 509 // Note: verify that the |observer_| got notified synchronously, without |
503 // having to spin the current loop. TearDown() will flush the loop so this | 510 // having to spin the current loop. TearDown() will flush the loop so this |
504 // must be done within the test. | 511 // must be done within the test. |
505 Mock::VerifyAndClearExpectations(&observer_); | 512 Mock::VerifyAndClearExpectations(&observer_); |
506 Mock::VerifyAndClearExpectations(&session_manager_client_); | 513 Mock::VerifyAndClearExpectations(&session_manager_client_); |
507 Mock::VerifyAndClearExpectations(&cryptohome_client_); | 514 Mock::VerifyAndClearExpectations(&cryptohome_client_); |
508 | 515 |
509 // The policy should become available without having to spin any loops. | 516 // The policy should become available without having to spin any loops. |
510 ASSERT_TRUE(store_->policy()); | 517 ASSERT_TRUE(store_->policy()); |
511 EXPECT_EQ(policy_.policy_data().SerializeAsString(), | 518 EXPECT_EQ(policy_.policy_data().SerializeAsString(), |
512 store_->policy()->SerializeAsString()); | 519 store_->policy()->SerializeAsString()); |
513 VerifyPolicyMap(kDefaultHomepage); | 520 VerifyPolicyMap(kDefaultHomepage); |
514 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); | 521 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); |
515 EXPECT_EQ(policy_.GetPublicSigningKeyAsString(), | 522 EXPECT_EQ(policy_.GetPublicSigningKeyAsString(), |
516 store_->policy_signature_public_key()); | 523 store_->policy_signature_public_key()); |
517 } | 524 } |
518 | 525 |
519 TEST_F(UserCloudPolicyStoreChromeOSTest, LoadImmediatelyNoPolicy) { | 526 TEST_F(UserCloudPolicyStoreChromeOSTest, LoadImmediatelyNoPolicy) { |
527 const std::string policy_data = ""; | |
emaxx
2017/04/28 12:48:22
nit: s/ = ""//
| |
520 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | 528 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); |
521 EXPECT_CALL(session_manager_client_, | 529 EXPECT_CALL(session_manager_client_, |
522 BlockingRetrievePolicyForUser(cryptohome_id_)) | 530 BlockingRetrievePolicyForUser(cryptohome_id_, _)) |
523 .WillOnce(Return("")); | 531 .WillOnce(DoAll(SetArgumentPointee<1>(policy_data), |
532 Return(RetrievePolicyResponseType::SUCCESS))); | |
524 | 533 |
525 EXPECT_FALSE(store_->policy()); | 534 EXPECT_FALSE(store_->policy()); |
526 store_->LoadImmediately(); | 535 store_->LoadImmediately(); |
527 Mock::VerifyAndClearExpectations(&observer_); | 536 Mock::VerifyAndClearExpectations(&observer_); |
528 Mock::VerifyAndClearExpectations(&session_manager_client_); | 537 Mock::VerifyAndClearExpectations(&session_manager_client_); |
529 | 538 |
530 EXPECT_FALSE(store_->policy()); | 539 EXPECT_FALSE(store_->policy()); |
531 EXPECT_TRUE(store_->policy_map().empty()); | 540 EXPECT_TRUE(store_->policy_map().empty()); |
532 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); | 541 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); |
533 EXPECT_EQ(std::string(), store_->policy_signature_public_key()); | 542 EXPECT_EQ(std::string(), store_->policy_signature_public_key()); |
534 } | 543 } |
535 | 544 |
536 TEST_F(UserCloudPolicyStoreChromeOSTest, LoadImmediatelyInvalidBlob) { | 545 TEST_F(UserCloudPolicyStoreChromeOSTest, LoadImmediatelyInvalidBlob) { |
546 const std::string policy_data = "le blob"; | |
537 EXPECT_CALL(observer_, OnStoreError(store_.get())); | 547 EXPECT_CALL(observer_, OnStoreError(store_.get())); |
538 EXPECT_CALL(session_manager_client_, | 548 EXPECT_CALL(session_manager_client_, |
539 BlockingRetrievePolicyForUser(cryptohome_id_)) | 549 BlockingRetrievePolicyForUser(cryptohome_id_, _)) |
540 .WillOnce(Return("le blob")); | 550 .WillOnce(DoAll(SetArgumentPointee<1>(policy_data), |
551 Return(RetrievePolicyResponseType::SUCCESS))); | |
541 | 552 |
542 EXPECT_FALSE(store_->policy()); | 553 EXPECT_FALSE(store_->policy()); |
543 store_->LoadImmediately(); | 554 store_->LoadImmediately(); |
544 Mock::VerifyAndClearExpectations(&observer_); | 555 Mock::VerifyAndClearExpectations(&observer_); |
545 Mock::VerifyAndClearExpectations(&session_manager_client_); | 556 Mock::VerifyAndClearExpectations(&session_manager_client_); |
546 | 557 |
547 EXPECT_FALSE(store_->policy()); | 558 EXPECT_FALSE(store_->policy()); |
548 EXPECT_TRUE(store_->policy_map().empty()); | 559 EXPECT_TRUE(store_->policy_map().empty()); |
549 EXPECT_EQ(CloudPolicyStore::STATUS_PARSE_ERROR, store_->status()); | 560 EXPECT_EQ(CloudPolicyStore::STATUS_PARSE_ERROR, store_->status()); |
550 EXPECT_EQ(std::string(), store_->policy_signature_public_key()); | 561 EXPECT_EQ(std::string(), store_->policy_signature_public_key()); |
551 } | 562 } |
552 | 563 |
553 TEST_F(UserCloudPolicyStoreChromeOSTest, LoadImmediatelyDBusFailure) { | 564 TEST_F(UserCloudPolicyStoreChromeOSTest, LoadImmediatelyDBusFailure) { |
565 const std::string policy_data = policy_.GetBlob(); | |
554 EXPECT_CALL(observer_, OnStoreError(store_.get())); | 566 EXPECT_CALL(observer_, OnStoreError(store_.get())); |
555 EXPECT_CALL(session_manager_client_, | 567 EXPECT_CALL(session_manager_client_, |
556 BlockingRetrievePolicyForUser(cryptohome_id_)) | 568 BlockingRetrievePolicyForUser(cryptohome_id_, _)) |
557 .WillOnce(Return(policy_.GetBlob())); | 569 .WillOnce(DoAll(SetArgumentPointee<1>(policy_data), |
570 Return(RetrievePolicyResponseType::SUCCESS))); | |
558 EXPECT_CALL(cryptohome_client_, BlockingGetSanitizedUsername(cryptohome_id_)) | 571 EXPECT_CALL(cryptohome_client_, BlockingGetSanitizedUsername(cryptohome_id_)) |
559 .WillOnce(Return("")); | 572 .WillOnce(Return("")); |
560 | 573 |
561 EXPECT_FALSE(store_->policy()); | 574 EXPECT_FALSE(store_->policy()); |
562 store_->LoadImmediately(); | 575 store_->LoadImmediately(); |
563 Mock::VerifyAndClearExpectations(&observer_); | 576 Mock::VerifyAndClearExpectations(&observer_); |
564 Mock::VerifyAndClearExpectations(&session_manager_client_); | 577 Mock::VerifyAndClearExpectations(&session_manager_client_); |
565 Mock::VerifyAndClearExpectations(&cryptohome_client_); | 578 Mock::VerifyAndClearExpectations(&cryptohome_client_); |
566 | 579 |
567 EXPECT_FALSE(store_->policy()); | 580 EXPECT_FALSE(store_->policy()); |
568 EXPECT_TRUE(store_->policy_map().empty()); | 581 EXPECT_TRUE(store_->policy_map().empty()); |
569 EXPECT_EQ(CloudPolicyStore::STATUS_LOAD_ERROR, store_->status()); | 582 EXPECT_EQ(CloudPolicyStore::STATUS_LOAD_ERROR, store_->status()); |
570 EXPECT_EQ(std::string(), store_->policy_signature_public_key()); | 583 EXPECT_EQ(std::string(), store_->policy_signature_public_key()); |
571 } | 584 } |
572 | 585 |
573 TEST_F(UserCloudPolicyStoreChromeOSTest, LoadImmediatelyNoUserPolicyKey) { | 586 TEST_F(UserCloudPolicyStoreChromeOSTest, LoadImmediatelyNoUserPolicyKey) { |
587 const std::string policy_data = policy_.GetBlob(); | |
574 EXPECT_CALL(observer_, OnStoreError(store_.get())); | 588 EXPECT_CALL(observer_, OnStoreError(store_.get())); |
575 EXPECT_CALL(session_manager_client_, | 589 EXPECT_CALL(session_manager_client_, |
576 BlockingRetrievePolicyForUser(cryptohome_id_)) | 590 BlockingRetrievePolicyForUser(cryptohome_id_, _)) |
577 .WillOnce(Return(policy_.GetBlob())); | 591 .WillOnce(DoAll(SetArgumentPointee<1>(policy_data), |
592 Return(RetrievePolicyResponseType::SUCCESS))); | |
578 EXPECT_CALL(cryptohome_client_, BlockingGetSanitizedUsername(cryptohome_id_)) | 593 EXPECT_CALL(cryptohome_client_, BlockingGetSanitizedUsername(cryptohome_id_)) |
579 .WillOnce(Return("wrong@example.com")); | 594 .WillOnce(Return("wrong@example.com")); |
580 | 595 |
581 EXPECT_FALSE(store_->policy()); | 596 EXPECT_FALSE(store_->policy()); |
582 store_->LoadImmediately(); | 597 store_->LoadImmediately(); |
583 Mock::VerifyAndClearExpectations(&observer_); | 598 Mock::VerifyAndClearExpectations(&observer_); |
584 Mock::VerifyAndClearExpectations(&session_manager_client_); | 599 Mock::VerifyAndClearExpectations(&session_manager_client_); |
585 Mock::VerifyAndClearExpectations(&cryptohome_client_); | 600 Mock::VerifyAndClearExpectations(&cryptohome_client_); |
586 | 601 |
587 EXPECT_FALSE(store_->policy()); | 602 EXPECT_FALSE(store_->policy()); |
588 EXPECT_TRUE(store_->policy_map().empty()); | 603 EXPECT_TRUE(store_->policy_map().empty()); |
589 EXPECT_EQ(CloudPolicyStore::STATUS_VALIDATION_ERROR, store_->status()); | 604 EXPECT_EQ(CloudPolicyStore::STATUS_VALIDATION_ERROR, store_->status()); |
590 EXPECT_EQ(std::string(), store_->policy_signature_public_key()); | 605 EXPECT_EQ(std::string(), store_->policy_signature_public_key()); |
591 } | 606 } |
592 | 607 |
593 } // namespace | 608 } // namespace |
594 | 609 |
595 } // namespace policy | 610 } // namespace policy |
OLD | NEW |