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