OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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/chromeos/policy/affiliated_cloud_policy_invalidator.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/run_loop.h" |
| 10 #include "base/thread_task_runner_handle.h" |
| 11 #include "chrome/browser/chromeos/policy/device_policy_builder.h" |
| 12 #include "chrome/browser/chromeos/policy/fake_affiliated_invalidation_service_pr
ovider.h" |
| 13 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h" |
| 14 #include "chrome/browser/invalidation/fake_invalidation_service.h" |
| 15 #include "chrome/browser/policy/cloud/cloud_policy_invalidator.h" |
| 16 #include "components/invalidation/invalidation.h" |
| 17 #include "components/invalidation/object_id_invalidation_map.h" |
| 18 #include "components/policy/core/common/cloud/cloud_policy_constants.h" |
| 19 #include "components/policy/core/common/cloud/cloud_policy_core.h" |
| 20 #include "components/policy/core/common/cloud/cloud_policy_store.h" |
| 21 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h" |
| 22 #include "content/public/test/test_browser_thread_bundle.h" |
| 23 #include "google/cacheinvalidation/include/types.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 |
| 27 namespace em = enterprise_management; |
| 28 |
| 29 using testing::Invoke; |
| 30 using testing::Mock; |
| 31 using testing::WithArgs; |
| 32 |
| 33 namespace policy { |
| 34 |
| 35 namespace { |
| 36 |
| 37 const int kInvalidationSource = 123; |
| 38 const char kInvalidationName[] = "invalidation"; |
| 39 |
| 40 class FakeCloudPolicyStore : public CloudPolicyStore { |
| 41 public: |
| 42 FakeCloudPolicyStore(); |
| 43 |
| 44 // CloudPolicyStore: |
| 45 void Store(const em::PolicyFetchResponse& policy) override; |
| 46 void Load() override; |
| 47 |
| 48 private: |
| 49 DISALLOW_COPY_AND_ASSIGN(FakeCloudPolicyStore); |
| 50 }; |
| 51 |
| 52 FakeCloudPolicyStore::FakeCloudPolicyStore() { |
| 53 } |
| 54 |
| 55 void FakeCloudPolicyStore::Store(const em::PolicyFetchResponse& policy) { |
| 56 policy_.reset(new em::PolicyData); |
| 57 policy_->ParseFromString(policy.policy_data()); |
| 58 Load(); |
| 59 } |
| 60 |
| 61 void FakeCloudPolicyStore::Load() { |
| 62 NotifyStoreLoaded(); |
| 63 } |
| 64 |
| 65 } // namespace |
| 66 |
| 67 // Verifies that an invalidator is created/destroyed as an invalidation service |
| 68 // becomes available/unavailable. Also verifies that invalidations are handled |
| 69 // correctly and the highest handled invalidation version is preserved when |
| 70 // switching invalidation services. |
| 71 TEST(AffiliatedCloudPolicyInvalidatorTest, CreateUseDestroy) { |
| 72 content::TestBrowserThreadBundle thread_bundle; |
| 73 |
| 74 // Set up a CloudPolicyCore backed by a simple CloudPolicyStore that does no |
| 75 // signature verification and stores policy in memory. |
| 76 FakeCloudPolicyStore store; |
| 77 CloudPolicyCore core(dm_protocol::kChromeDevicePolicyType, |
| 78 std::string(), |
| 79 &store, |
| 80 base::ThreadTaskRunnerHandle::Get()); |
| 81 |
| 82 // Connect |core|. Expect it to send a registration request. Let the |
| 83 // registration succeed. |
| 84 scoped_ptr<MockCloudPolicyClient> policy_client_owner( |
| 85 new MockCloudPolicyClient); |
| 86 MockCloudPolicyClient* policy_client = policy_client_owner.get(); |
| 87 EXPECT_CALL(*policy_client, SetupRegistration("token", "device-id")) |
| 88 .WillOnce(WithArgs<1>(Invoke(policy_client, |
| 89 &MockCloudPolicyClient::SetDMToken))); |
| 90 core.Connect(policy_client_owner.Pass()); |
| 91 Mock::VerifyAndClearExpectations(&policy_client); |
| 92 core.StartRefreshScheduler(); |
| 93 |
| 94 DevicePolicyBuilder policy; |
| 95 policy.policy_data().set_invalidation_source(kInvalidationSource); |
| 96 policy.policy_data().set_invalidation_name(kInvalidationName); |
| 97 policy.Build(); |
| 98 store.Store(policy.policy()); |
| 99 |
| 100 FakeAffiliatedInvalidationServiceProvider provider; |
| 101 AffiliatedCloudPolicyInvalidator affiliated_invalidator( |
| 102 em::DeviceRegisterRequest::DEVICE, |
| 103 &core, |
| 104 &provider); |
| 105 |
| 106 // Verify that no invalidator exists initially. |
| 107 EXPECT_FALSE(affiliated_invalidator.GetInvalidatorForTest()); |
| 108 |
| 109 // Make a first invalidation service available. |
| 110 invalidation::FakeInvalidationService invalidation_service_1; |
| 111 affiliated_invalidator.OnInvalidationServiceSet(&invalidation_service_1); |
| 112 |
| 113 // Verify that an invalidator backed by the first invalidation service has |
| 114 // been created and its highest handled invalidation version starts out as |
| 115 // zero. |
| 116 CloudPolicyInvalidator* invalidator = |
| 117 affiliated_invalidator.GetInvalidatorForTest(); |
| 118 ASSERT_TRUE(invalidator); |
| 119 EXPECT_EQ(0, invalidator->highest_handled_invalidation_version()); |
| 120 EXPECT_EQ(&invalidation_service_1, |
| 121 invalidator->invalidation_service_for_test()); |
| 122 |
| 123 // Trigger an invalidation. The invalidation version is interpreted as a |
| 124 // timestamp in microseconds. The policy blob contains a timestamp in |
| 125 // milliseconds. Convert from one to the other by multiplying by 1000. |
| 126 const int64 invalidation_version = policy.policy_data().timestamp() * 1000; |
| 127 syncer::Invalidation invalidation = syncer::Invalidation::Init( |
| 128 invalidation::ObjectId(kInvalidationSource, kInvalidationName), |
| 129 invalidation_version, |
| 130 "dummy payload"); |
| 131 syncer::ObjectIdInvalidationMap invalidation_map; |
| 132 invalidation_map.Insert(invalidation); |
| 133 invalidator->OnIncomingInvalidation(invalidation_map); |
| 134 |
| 135 // Allow the invalidation to be handled. |
| 136 policy_client->SetFetchedInvalidationVersion(invalidation_version); |
| 137 policy.payload().mutable_reboot_on_shutdown()->set_reboot_on_shutdown(true); |
| 138 policy.Build(); |
| 139 policy_client->SetPolicy(dm_protocol::kChromeDevicePolicyType, |
| 140 std::string(), |
| 141 policy.policy()); |
| 142 EXPECT_CALL(*policy_client, FetchPolicy()) |
| 143 .WillOnce(Invoke(policy_client, |
| 144 &MockCloudPolicyClient::NotifyPolicyFetched)); |
| 145 base::RunLoop().RunUntilIdle(); |
| 146 |
| 147 // Verify that the invalidator's highest handled invalidation version was |
| 148 // updated and the new policy was stored. |
| 149 EXPECT_EQ(invalidation_version, |
| 150 invalidator->highest_handled_invalidation_version()); |
| 151 ASSERT_TRUE(store.policy()); |
| 152 em::ChromeDeviceSettingsProto device_policy; |
| 153 device_policy.ParseFromString(store.policy()->policy_value()); |
| 154 EXPECT_EQ(true, device_policy.reboot_on_shutdown().reboot_on_shutdown()); |
| 155 |
| 156 // Make the first invalidation service unavailable. Verify that the |
| 157 // invalidator is destroyed. |
| 158 affiliated_invalidator.OnInvalidationServiceSet(nullptr); |
| 159 EXPECT_FALSE(affiliated_invalidator.GetInvalidatorForTest()); |
| 160 |
| 161 // Make a second invalidation service available. |
| 162 invalidation::FakeInvalidationService invalidation_service_2; |
| 163 affiliated_invalidator.OnInvalidationServiceSet(&invalidation_service_2); |
| 164 |
| 165 // Verify that an invalidator backed by the second invalidation service has |
| 166 // been created and its highest handled invalidation version does not start |
| 167 // out as zero. |
| 168 invalidator = affiliated_invalidator.GetInvalidatorForTest(); |
| 169 ASSERT_TRUE(invalidator); |
| 170 EXPECT_EQ(invalidation_version, |
| 171 invalidator->highest_handled_invalidation_version()); |
| 172 EXPECT_EQ(&invalidation_service_2, |
| 173 invalidator->invalidation_service_for_test()); |
| 174 |
| 175 // Make the first invalidation service available again. This implies that the |
| 176 // second invalidation service is no longer available. |
| 177 affiliated_invalidator.OnInvalidationServiceSet(&invalidation_service_1); |
| 178 |
| 179 // Verify that the invalidator backed by the second invalidation service was |
| 180 // destroyed and an invalidation backed by the first invalidation service has |
| 181 // been created instead. Also verify that its highest handled invalidation |
| 182 // version does not start out as zero. |
| 183 invalidator = affiliated_invalidator.GetInvalidatorForTest(); |
| 184 ASSERT_TRUE(invalidator); |
| 185 EXPECT_EQ(invalidation_version, |
| 186 invalidator->highest_handled_invalidation_version()); |
| 187 EXPECT_EQ(&invalidation_service_1, |
| 188 invalidator->invalidation_service_for_test()); |
| 189 |
| 190 provider.Shutdown(); |
| 191 affiliated_invalidator.OnInvalidationServiceSet(nullptr); |
| 192 } |
| 193 |
| 194 } // namespace policy |
OLD | NEW |