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 FakeCloudPolicyStore store; | |
pneubeck (no reviews)
2015/02/04 10:02:14
if possible to describe briefly, please do so for
bartfab (slow)
2015/02/04 12:34:51
Done.
| |
75 CloudPolicyCore core(dm_protocol::kChromeDevicePolicyType, | |
76 std::string(), | |
77 &store, | |
78 base::ThreadTaskRunnerHandle::Get()); | |
79 | |
80 scoped_ptr<MockCloudPolicyClient> policy_client_owner( | |
81 new MockCloudPolicyClient); | |
82 MockCloudPolicyClient* policy_client = policy_client_owner.get(); | |
83 EXPECT_CALL(*policy_client, SetupRegistration("token", "device-id")) | |
84 .WillOnce(WithArgs<1>(Invoke(policy_client, | |
85 &MockCloudPolicyClient::SetDMToken))); | |
86 core.Connect(policy_client_owner.Pass()); | |
87 Mock::VerifyAndClearExpectations(&policy_client); | |
88 core.StartRefreshScheduler(); | |
89 | |
90 DevicePolicyBuilder policy; | |
91 policy.policy_data().set_invalidation_source(kInvalidationSource); | |
92 policy.policy_data().set_invalidation_name(kInvalidationName); | |
93 policy.Build(); | |
94 store.Store(policy.policy()); | |
95 | |
96 FakeAffiliatedInvalidationServiceProvider provider; | |
97 AffiliatedCloudPolicyInvalidator affiliated_invalidator( | |
98 em::DeviceRegisterRequest::DEVICE, | |
99 &core, | |
100 &provider); | |
101 | |
102 // Verify that no invalidator exists initially. | |
103 EXPECT_FALSE(affiliated_invalidator.GetInvalidatorForTest()); | |
104 | |
105 // Make a first invalidation service available. | |
106 invalidation::FakeInvalidationService invalidation_service_1; | |
107 affiliated_invalidator.OnInvalidationServiceSet(&invalidation_service_1); | |
108 | |
109 // Verify that an invalidator backed by the first invalidation service has | |
110 // been created and its highest handled invalidation version starts out as | |
111 // zero. | |
112 CloudPolicyInvalidator* invalidator = | |
113 affiliated_invalidator.GetInvalidatorForTest(); | |
114 ASSERT_TRUE(invalidator); | |
115 EXPECT_EQ(0, invalidator->highest_handled_invalidation_version()); | |
116 EXPECT_EQ(&invalidation_service_1, | |
117 invalidator->invalidation_service_for_test()); | |
118 | |
119 // Trigger an invalidation. The invalidation version is interpreted as a | |
120 // timestamp in microseconds. The policy blob contains a timestamp in | |
121 // milliseconds. Convert from one to the other by multiplying by 1000. | |
122 const int64 invalidation_version = policy.policy_data().timestamp() * 1000; | |
123 syncer::Invalidation invalidation = syncer::Invalidation::Init( | |
124 invalidation::ObjectId(kInvalidationSource, kInvalidationName), | |
125 invalidation_version, | |
126 "dummy payload"); | |
127 syncer::ObjectIdInvalidationMap invalidation_map; | |
128 invalidation_map.Insert(invalidation); | |
129 invalidator->OnIncomingInvalidation(invalidation_map); | |
130 | |
131 // Allow the invalidation to be handled. | |
132 policy_client->SetFetchedInvalidationVersion(invalidation_version); | |
133 policy.payload().mutable_reboot_on_shutdown()->set_reboot_on_shutdown(true); | |
134 policy.Build(); | |
135 policy_client->SetPolicy(dm_protocol::kChromeDevicePolicyType, | |
136 std::string(), | |
137 policy.policy()); | |
138 EXPECT_CALL(*policy_client, FetchPolicy()) | |
139 .WillOnce(Invoke(policy_client, | |
140 &MockCloudPolicyClient::NotifyPolicyFetched)); | |
141 base::RunLoop().RunUntilIdle(); | |
142 | |
143 // Verify that the invalidator's highest handled invalidation version was | |
144 // updated and the new policy was stored. | |
145 EXPECT_EQ(invalidation_version, | |
146 invalidator->highest_handled_invalidation_version()); | |
147 ASSERT_TRUE(store.policy()); | |
148 em::ChromeDeviceSettingsProto device_policy; | |
149 device_policy.ParseFromString(store.policy()->policy_value()); | |
150 EXPECT_EQ(true, device_policy.reboot_on_shutdown().reboot_on_shutdown()); | |
151 | |
152 // Make the first invalidation service unavailable. Verify that the | |
153 // invalidator is destroyed. | |
154 affiliated_invalidator.OnInvalidationServiceSet(nullptr); | |
155 EXPECT_FALSE(affiliated_invalidator.GetInvalidatorForTest()); | |
156 | |
157 // Make a second invalidation service available. | |
158 invalidation::FakeInvalidationService invalidation_service_2; | |
159 affiliated_invalidator.OnInvalidationServiceSet(&invalidation_service_2); | |
160 | |
161 // Verify that an invalidator backed by the second invalidation service has | |
162 // been created and its highest handled invalidation version does not start | |
163 // out as zero. | |
164 invalidator = affiliated_invalidator.GetInvalidatorForTest(); | |
165 ASSERT_TRUE(invalidator); | |
166 EXPECT_EQ(invalidation_version, | |
167 invalidator->highest_handled_invalidation_version()); | |
168 EXPECT_EQ(&invalidation_service_2, | |
169 invalidator->invalidation_service_for_test()); | |
170 | |
171 // Make the first invalidation service available again. This implies that the | |
172 // second invalidation service is no longer available. | |
173 affiliated_invalidator.OnInvalidationServiceSet(&invalidation_service_1); | |
174 | |
175 // Verify that the invalidator backed by the second invalidation service was | |
176 // destroyed and an invalidation backed by the first invalidation service has | |
177 // been created instead. Also verify that its highest handled invalidation | |
178 // version does not start out as zero. | |
179 invalidator = affiliated_invalidator.GetInvalidatorForTest(); | |
180 ASSERT_TRUE(invalidator); | |
181 EXPECT_EQ(invalidation_version, | |
182 invalidator->highest_handled_invalidation_version()); | |
183 EXPECT_EQ(&invalidation_service_1, | |
184 invalidator->invalidation_service_for_test()); | |
185 | |
186 provider.Shutdown(); | |
187 affiliated_invalidator.OnInvalidationServiceSet(nullptr); | |
188 } | |
189 | |
190 } // namespace policy | |
OLD | NEW |