| 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_manager_chromeos.h" | |
| 6 | |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/message_loop.h" | |
| 10 #include "chrome/browser/policy/cloud_policy_constants.h" | |
| 11 #include "chrome/browser/policy/mock_cloud_policy_store.h" | |
| 12 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | |
| 13 #include "chrome/browser/policy/mock_device_management_service.h" | |
| 14 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
| 15 #include "chrome/browser/prefs/browser_prefs.h" | |
| 16 #include "chrome/test/base/testing_pref_service.h" | |
| 17 #include "policy/policy_constants.h" | |
| 18 #include "testing/gmock/include/gmock/gmock.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 namespace em = enterprise_management; | |
| 22 | |
| 23 using testing::AnyNumber; | |
| 24 using testing::AtLeast; | |
| 25 using testing::Mock; | |
| 26 using testing::_; | |
| 27 | |
| 28 namespace policy { | |
| 29 namespace { | |
| 30 | |
| 31 class UserCloudPolicyManagerChromeOSTest : public testing::Test { | |
| 32 protected: | |
| 33 UserCloudPolicyManagerChromeOSTest() | |
| 34 : store_(NULL) {} | |
| 35 | |
| 36 virtual void SetUp() OVERRIDE { | |
| 37 chrome::RegisterLocalState(&prefs_); | |
| 38 | |
| 39 // Set up a policy map for testing. | |
| 40 policy_map_.Set("key", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
| 41 base::Value::CreateStringValue("value")); | |
| 42 expected_bundle_.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) | |
| 43 .CopyFrom(policy_map_); | |
| 44 | |
| 45 // Create a fake policy blob to deliver to the client. | |
| 46 em::PolicyData policy_data; | |
| 47 policy_data.set_policy_type(dm_protocol::kChromeUserPolicyType); | |
| 48 em::PolicyFetchResponse* policy_response = | |
| 49 policy_blob_.mutable_policy_response()->add_response(); | |
| 50 ASSERT_TRUE(policy_data.SerializeToString( | |
| 51 policy_response->mutable_policy_data())); | |
| 52 | |
| 53 EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _)) | |
| 54 .Times(AnyNumber()); | |
| 55 } | |
| 56 | |
| 57 virtual void TearDown() OVERRIDE { | |
| 58 if (manager_) { | |
| 59 manager_->RemoveObserver(&observer_); | |
| 60 manager_->Shutdown(); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 void CreateManagerWithPendingFetch() { | |
| 65 store_ = new MockCloudPolicyStore(); | |
| 66 EXPECT_CALL(*store_, Load()); | |
| 67 manager_.reset( | |
| 68 new UserCloudPolicyManagerChromeOS(scoped_ptr<CloudPolicyStore>(store_), | |
| 69 true)); | |
| 70 manager_->Init(); | |
| 71 manager_->AddObserver(&observer_); | |
| 72 manager_->Connect(&prefs_, &device_management_service_, | |
| 73 USER_AFFILIATION_NONE); | |
| 74 Mock::VerifyAndClearExpectations(store_); | |
| 75 EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | |
| 76 | |
| 77 // Finishing the Load() operation shouldn't set the initialized flag. | |
| 78 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); | |
| 79 store_->NotifyStoreLoaded(); | |
| 80 EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | |
| 81 Mock::VerifyAndClearExpectations(&observer_); | |
| 82 } | |
| 83 | |
| 84 // Required by the refresh scheduler that's created by the manager. | |
| 85 MessageLoop loop_; | |
| 86 | |
| 87 // Convenience policy objects. | |
| 88 em::DeviceManagementResponse policy_blob_; | |
| 89 PolicyMap policy_map_; | |
| 90 PolicyBundle expected_bundle_; | |
| 91 | |
| 92 // Policy infrastructure. | |
| 93 TestingPrefServiceSimple prefs_; | |
| 94 MockConfigurationPolicyObserver observer_; | |
| 95 MockDeviceManagementService device_management_service_; | |
| 96 MockCloudPolicyStore* store_; | |
| 97 scoped_ptr<UserCloudPolicyManagerChromeOS> manager_; | |
| 98 | |
| 99 private: | |
| 100 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerChromeOSTest); | |
| 101 }; | |
| 102 | |
| 103 TEST_F(UserCloudPolicyManagerChromeOSTest, WaitForPolicyFetch) { | |
| 104 CreateManagerWithPendingFetch(); | |
| 105 | |
| 106 // Setting the token should trigger the policy fetch. | |
| 107 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); | |
| 108 MockDeviceManagementJob* fetch_request = NULL; | |
| 109 EXPECT_CALL(device_management_service_, | |
| 110 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) | |
| 111 .WillOnce(device_management_service_.CreateAsyncJob(&fetch_request)); | |
| 112 manager_->core()->client()->SetupRegistration("dm_token", "client_id"); | |
| 113 ASSERT_TRUE(fetch_request); | |
| 114 EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | |
| 115 Mock::VerifyAndClearExpectations(&observer_); | |
| 116 | |
| 117 // Respond to the policy fetch, which should trigger a write to |store_|. | |
| 118 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); | |
| 119 EXPECT_CALL(*store_, Store(_)); | |
| 120 fetch_request->SendResponse(DM_STATUS_SUCCESS, policy_blob_); | |
| 121 EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | |
| 122 Mock::VerifyAndClearExpectations(&observer_); | |
| 123 | |
| 124 // The load notification from |store_| should trigger the policy update and | |
| 125 // flip the initialized bit. | |
| 126 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | |
| 127 store_->NotifyStoreLoaded(); | |
| 128 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | |
| 129 Mock::VerifyAndClearExpectations(&observer_); | |
| 130 } | |
| 131 | |
| 132 TEST_F(UserCloudPolicyManagerChromeOSTest, WaitForPolicyFetchError) { | |
| 133 CreateManagerWithPendingFetch(); | |
| 134 | |
| 135 // Setting the token should trigger the policy fetch. | |
| 136 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); | |
| 137 MockDeviceManagementJob* fetch_request = NULL; | |
| 138 EXPECT_CALL(device_management_service_, | |
| 139 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) | |
| 140 .WillOnce(device_management_service_.CreateAsyncJob(&fetch_request)); | |
| 141 manager_->core()->client()->SetupRegistration("dm_token", "client_id"); | |
| 142 ASSERT_TRUE(fetch_request); | |
| 143 EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | |
| 144 Mock::VerifyAndClearExpectations(&observer_); | |
| 145 | |
| 146 // Make the policy fetch fail, at which point the manager should bail out. | |
| 147 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())).Times(AtLeast(1)); | |
| 148 fetch_request->SendResponse(DM_STATUS_REQUEST_FAILED, policy_blob_); | |
| 149 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | |
| 150 Mock::VerifyAndClearExpectations(&observer_); | |
| 151 } | |
| 152 | |
| 153 TEST_F(UserCloudPolicyManagerChromeOSTest, WaitForPolicyFetchCancel) { | |
| 154 CreateManagerWithPendingFetch(); | |
| 155 | |
| 156 // Cancelling the initial fetch should flip the flag. | |
| 157 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | |
| 158 manager_->CancelWaitForPolicyFetch(); | |
| 159 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | |
| 160 Mock::VerifyAndClearExpectations(&observer_); | |
| 161 } | |
| 162 | |
| 163 } // namespace | |
| 164 } // namespace policy | |
| OLD | NEW |