device_cloud_policy_invalidator_unittest.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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/device_cloud_policy_invalidator.h"
6
7#include <string>
8
9#include "base/memory/ref_counted.h"
10#include "base/message_loop/message_loop_proxy.h"
11#include "base/run_loop.h"
12#include "chrome/browser/chrome_notification_types.h"
13#include "chrome/browser/chromeos/settings/cros_settings.h"
14#include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h"
15#include "chrome/browser/chromeos/settings/device_settings_service.h"
16#include "chrome/browser/invalidation/profile_invalidation_provider_factory.h"
17#include "chrome/browser/profiles/profile.h"
18#include "chrome/test/base/testing_browser_process.h"
19#include "chrome/test/base/testing_profile_manager.h"
20#include "chromeos/cryptohome/system_salt_getter.h"
21#include "chromeos/dbus/dbus_thread_manager.h"
22#include "components/invalidation/invalidator_state.h"
23#include "components/invalidation/profile_invalidation_provider.h"
24#include "components/invalidation/ticl_invalidation_service.h"
25#include "content/public/browser/notification_details.h"
26#include "content/public/browser/notification_service.h"
27#include "content/public/test/test_browser_thread_bundle.h"
28#include "net/url_request/url_request_context_getter.h"
29#include "net/url_request/url_request_test_util.h"
30#include "testing/gtest/include/gtest/gtest.h"
31
32namespace policy {
33
34class DeviceCloudPolicyInvalidatorTest : public testing::Test {
35 public:
36  DeviceCloudPolicyInvalidatorTest();
37  virtual ~DeviceCloudPolicyInvalidatorTest();
38
39  // testing::Test:
40  virtual void SetUp() OVERRIDE;
41  virtual void TearDown() OVERRIDE;
42
43  // Ownership is not passed. The Profile is owned by the global ProfileManager.
44  Profile *CreateProfile(const std::string& profile_name);
45
46  invalidation::TiclInvalidationService* GetDeviceInvalidationService();
47  bool HasDeviceInvalidationServiceObserver() const;
48
49  invalidation::TiclInvalidationService* GetProfileInvalidationService(
50      Profile* profile);
51  int GetProfileInvalidationServiceObserverCount() const;
52
53  const invalidation::TiclInvalidationService* GetInvalidationService() const;
54  bool HasCloudPolicyInvalidator() const;
55
56  void ConnectDeviceInvalidationService();
57
58 private:
59  content::TestBrowserThreadBundle thread_bundle_;
60  scoped_refptr<net::URLRequestContextGetter> system_request_context_;
61  TestingProfileManager profile_manager_;
62  scoped_ptr<chromeos::ScopedTestDeviceSettingsService>
63      test_device_settings_service_;
64  scoped_ptr<chromeos::ScopedTestCrosSettings> test_cros_settings_;
65
66  scoped_ptr<DeviceCloudPolicyInvalidator> invalidator_;
67};
68
69DeviceCloudPolicyInvalidatorTest::DeviceCloudPolicyInvalidatorTest()
70    : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
71      system_request_context_(new net::TestURLRequestContextGetter(
72          base::MessageLoopProxy::current())),
73      profile_manager_(TestingBrowserProcess::GetGlobal()) {
74}
75
76DeviceCloudPolicyInvalidatorTest::~DeviceCloudPolicyInvalidatorTest() {
77}
78
79void DeviceCloudPolicyInvalidatorTest::SetUp() {
80  chromeos::SystemSaltGetter::Initialize();
81  chromeos::DBusThreadManager::InitializeWithStub();
82  chromeos::DeviceOAuth2TokenServiceFactory::Initialize();
83  TestingBrowserProcess::GetGlobal()->SetSystemRequestContext(
84      system_request_context_.get());
85  ASSERT_TRUE(profile_manager_.SetUp());
86  test_device_settings_service_.reset(new
87      chromeos::ScopedTestDeviceSettingsService);
88  test_cros_settings_.reset(new chromeos::ScopedTestCrosSettings);
89
90  invalidator_.reset(new DeviceCloudPolicyInvalidator);
91}
92
93void DeviceCloudPolicyInvalidatorTest::TearDown() {
94  invalidator_.reset();
95  base::RunLoop().RunUntilIdle();
96
97  TestingBrowserProcess::GetGlobal()->SetBrowserPolicyConnector(NULL);
98  chromeos::DeviceOAuth2TokenServiceFactory::Shutdown();
99  chromeos::DBusThreadManager::Shutdown();
100  chromeos::SystemSaltGetter::Shutdown();
101}
102
103Profile *DeviceCloudPolicyInvalidatorTest::CreateProfile(
104    const std::string& profile_name) {
105  Profile* profile = profile_manager_.CreateTestingProfile(profile_name);
106  content::NotificationService::current()->Notify(
107      chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
108      content::NotificationService::AllSources(),
109      content::Details<Profile>(profile));
110  return profile;
111}
112
113invalidation::TiclInvalidationService*
114DeviceCloudPolicyInvalidatorTest::GetDeviceInvalidationService() {
115  return invalidator_->device_invalidation_service_.get();
116}
117
118bool DeviceCloudPolicyInvalidatorTest::HasDeviceInvalidationServiceObserver(
119    ) const {
120  return invalidator_->device_invalidation_service_observer_.get();
121}
122
123invalidation::TiclInvalidationService*
124DeviceCloudPolicyInvalidatorTest::GetProfileInvalidationService(
125    Profile* profile) {
126  invalidation::ProfileInvalidationProvider* invalidation_provider =
127      static_cast<invalidation::ProfileInvalidationProvider*>(
128          invalidation::ProfileInvalidationProviderFactory::GetInstance()->
129              GetServiceForBrowserContext(profile, false));
130  if (!invalidation_provider)
131    return NULL;
132  return static_cast<invalidation::TiclInvalidationService*>(
133      invalidation_provider->GetInvalidationService());
134}
135
136int DeviceCloudPolicyInvalidatorTest::
137        GetProfileInvalidationServiceObserverCount() const {
138  return invalidator_->profile_invalidation_service_observers_.size();
139}
140
141const invalidation::TiclInvalidationService*
142DeviceCloudPolicyInvalidatorTest::GetInvalidationService() const {
143  return static_cast<invalidation::TiclInvalidationService*>(
144      invalidator_->invalidation_service_);
145}
146
147bool DeviceCloudPolicyInvalidatorTest::HasCloudPolicyInvalidator() const {
148  return invalidator_->invalidator_.get();
149}
150
151void DeviceCloudPolicyInvalidatorTest::ConnectDeviceInvalidationService() {
152  // Verify that a device-global invalidation service has been created.
153  ASSERT_TRUE(GetDeviceInvalidationService());
154  EXPECT_TRUE(HasDeviceInvalidationServiceObserver());
155
156  // Verify that no per-profile invalidation service observers have been
157  // created.
158  EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount());
159
160  // Verify that no invalidator exists yet
161  EXPECT_FALSE(HasCloudPolicyInvalidator());
162  EXPECT_FALSE(GetInvalidationService());
163
164  // Indicate that the device-global invalidation service has connected.
165  GetDeviceInvalidationService()->OnInvalidatorStateChange(
166      syncer::INVALIDATIONS_ENABLED);
167  base::RunLoop().RunUntilIdle();
168
169  // Verify that the device-global invalidation service still exists.
170  EXPECT_TRUE(GetDeviceInvalidationService());
171  EXPECT_TRUE(HasDeviceInvalidationServiceObserver());
172
173  // Verify that an invalidator backed by the device-global invalidation service
174  // has been created.
175  EXPECT_TRUE(HasCloudPolicyInvalidator());
176  EXPECT_EQ(GetDeviceInvalidationService(), GetInvalidationService());
177}
178
179// Verifies that a DeviceCloudPolicyInvalidator backed by a device-global
180// invalidation service is created/destroyed as the service
181// connects/disconnects.
182TEST_F(DeviceCloudPolicyInvalidatorTest, UseDeviceInvalidationService) {
183  // Verify that an invalidator backed by the device-global invalidation service
184  // is created when the service connects.
185  ConnectDeviceInvalidationService();
186  ASSERT_TRUE(GetDeviceInvalidationService());
187
188  // Indicate that the device-global invalidation service has disconnected.
189  GetDeviceInvalidationService()->OnInvalidatorStateChange(
190      syncer::INVALIDATION_CREDENTIALS_REJECTED);
191  base::RunLoop().RunUntilIdle();
192
193  // Verify that the device-global invalidation service still exists.
194  EXPECT_TRUE(GetDeviceInvalidationService());
195  EXPECT_TRUE(HasDeviceInvalidationServiceObserver());
196
197  // Verify that the invalidator has been destroyed.
198  EXPECT_FALSE(HasCloudPolicyInvalidator());
199  EXPECT_FALSE(GetInvalidationService());
200}
201
202// Verifies that a DeviceCloudPolicyInvalidator backed by a per-profile
203// invalidation service is created/destroyed as the service
204// connects/disconnects.
205TEST_F(DeviceCloudPolicyInvalidatorTest, UseProfileInvalidationService) {
206  // Create a user profile.
207  Profile* profile = CreateProfile("test");
208  ASSERT_TRUE(profile);
209
210  // Verify that a device-global invalidation service has been created.
211  EXPECT_TRUE(GetDeviceInvalidationService());
212  EXPECT_TRUE(HasDeviceInvalidationServiceObserver());
213
214  // Verify that a per-profile invalidation service has been created.
215  invalidation::TiclInvalidationService* profile_invalidation_service =
216      GetProfileInvalidationService(profile);
217  ASSERT_TRUE(profile_invalidation_service);
218  EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount());
219
220  // Verify that no invalidator exists yet
221  EXPECT_FALSE(HasCloudPolicyInvalidator());
222  EXPECT_FALSE(GetInvalidationService());
223
224  // Indicate that the per-profile invalidation service has connected.
225  profile_invalidation_service->OnInvalidatorStateChange(
226      syncer::INVALIDATIONS_ENABLED);
227  base::RunLoop().RunUntilIdle();
228
229  // Verify that the device-global invalidator has been destroyed.
230  EXPECT_FALSE(GetDeviceInvalidationService());
231  EXPECT_FALSE(HasDeviceInvalidationServiceObserver());
232
233  // Verify that a per-profile invalidation service still exists.
234  profile_invalidation_service = GetProfileInvalidationService(profile);
235  ASSERT_TRUE(profile_invalidation_service);
236  EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount());
237
238  // Verify that an invalidator backed by the per-profile invalidation service
239  // has been created.
240  EXPECT_TRUE(HasCloudPolicyInvalidator());
241  EXPECT_EQ(profile_invalidation_service, GetInvalidationService());
242
243  // Indicate that the per-profile invalidation service has disconnected.
244  profile_invalidation_service->OnInvalidatorStateChange(
245      syncer::INVALIDATION_CREDENTIALS_REJECTED);
246  base::RunLoop().RunUntilIdle();
247
248  // Verify that a device-global invalidation service has been created.
249  EXPECT_TRUE(GetDeviceInvalidationService());
250  EXPECT_TRUE(HasDeviceInvalidationServiceObserver());
251
252  // Verify that a per-profile invalidation service still exists.
253  profile_invalidation_service = GetProfileInvalidationService(profile);
254  EXPECT_TRUE(profile_invalidation_service);
255  EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount());
256
257  // Verify that the invalidator has been destroyed.
258  EXPECT_FALSE(HasCloudPolicyInvalidator());
259  EXPECT_FALSE(GetInvalidationService());
260}
261
262// Verifies that a DeviceCloudPolicyInvalidator exists whenever a connected
263// invalidation service is available, automatically switching between
264// device-global and per-profile invalidation services as they
265// connect/disconnect, giving priority to per-profile invalidation services.
266TEST_F(DeviceCloudPolicyInvalidatorTest, SwitchInvalidationServices) {
267  // Verify that an invalidator backed by the device-global invalidation service
268  // is created when the service connects.
269  ConnectDeviceInvalidationService();
270  ASSERT_TRUE(GetDeviceInvalidationService());
271
272  // Create a first user profile.
273  Profile* profile_1 = CreateProfile("test_1");
274  ASSERT_TRUE(profile_1);
275
276  // Verify that the device-global invalidation service still exists.
277  EXPECT_TRUE(GetDeviceInvalidationService());
278  EXPECT_TRUE(HasDeviceInvalidationServiceObserver());
279
280  // Verify that a per-profile invalidation service has been created for the
281  // first user profile.
282  invalidation::TiclInvalidationService* profile_1_invalidation_service =
283      GetProfileInvalidationService(profile_1);
284  ASSERT_TRUE(profile_1_invalidation_service);
285  EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount());
286
287  // Verify that an invalidator backed by the device-global invalidation service
288  // still exists.
289  EXPECT_TRUE(HasCloudPolicyInvalidator());
290  EXPECT_EQ(GetDeviceInvalidationService(), GetInvalidationService());
291
292  // Indicate that the first user profile's per-profile invalidation service has
293  // connected.
294  profile_1_invalidation_service->OnInvalidatorStateChange(
295      syncer::INVALIDATIONS_ENABLED);
296  base::RunLoop().RunUntilIdle();
297
298  // Verify that the device-global invalidator has been destroyed.
299  EXPECT_FALSE(GetDeviceInvalidationService());
300  EXPECT_FALSE(HasDeviceInvalidationServiceObserver());
301
302  // Verify that a per-profile invalidation service still exists for the first
303  // user profile.
304  profile_1_invalidation_service = GetProfileInvalidationService(profile_1);
305  EXPECT_TRUE(profile_1_invalidation_service);
306  EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount());
307
308  // Verify that an invalidator backed by the per-profile invalidation service
309  // for the first user profile has been created.
310  EXPECT_TRUE(HasCloudPolicyInvalidator());
311  EXPECT_EQ(profile_1_invalidation_service, GetInvalidationService());
312
313  // Create a second user profile.
314  Profile* profile_2 = CreateProfile("test_2");
315  ASSERT_TRUE(profile_2);
316
317  // Verify that the device-global invalidator still does not exist.
318  EXPECT_FALSE(GetDeviceInvalidationService());
319  EXPECT_FALSE(HasDeviceInvalidationServiceObserver());
320
321  // Verify that a per-profile invalidation service still exists for the first
322  // user profile and one has been created for the second user profile.
323  profile_1_invalidation_service = GetProfileInvalidationService(profile_1);
324  EXPECT_TRUE(profile_1_invalidation_service);
325  invalidation::TiclInvalidationService* profile_2_invalidation_service =
326      GetProfileInvalidationService(profile_2);
327  ASSERT_TRUE(profile_2_invalidation_service);
328  EXPECT_EQ(2, GetProfileInvalidationServiceObserverCount());
329
330  // Verify that an invalidator backed by the per-profile invalidation service
331  // for the first user profile still exists.
332  EXPECT_TRUE(HasCloudPolicyInvalidator());
333  EXPECT_EQ(profile_1_invalidation_service, GetInvalidationService());
334
335  // Indicate that the second user profile's per-profile invalidation service
336  // has connected.
337  profile_2_invalidation_service->OnInvalidatorStateChange(
338      syncer::INVALIDATIONS_ENABLED);
339  base::RunLoop().RunUntilIdle();
340
341  // Verify that the device-global invalidator still does not exist.
342  EXPECT_FALSE(GetDeviceInvalidationService());
343  EXPECT_FALSE(HasDeviceInvalidationServiceObserver());
344
345  // Verify that per-profile invalidation services still exist for both user
346  // profiles.
347  profile_1_invalidation_service = GetProfileInvalidationService(profile_1);
348  ASSERT_TRUE(profile_1_invalidation_service);
349  profile_2_invalidation_service = GetProfileInvalidationService(profile_2);
350  EXPECT_TRUE(profile_2_invalidation_service);
351  EXPECT_EQ(2, GetProfileInvalidationServiceObserverCount());
352
353  // Verify that an invalidator backed by the per-profile invalidation service
354  // for the first user profile still exists.
355  EXPECT_TRUE(HasCloudPolicyInvalidator());
356  EXPECT_EQ(profile_1_invalidation_service, GetInvalidationService());
357
358  // Indicate that the per-profile invalidation service for the first user
359  // profile has disconnected.
360  profile_1_invalidation_service->OnInvalidatorStateChange(
361      syncer::INVALIDATION_CREDENTIALS_REJECTED);
362  base::RunLoop().RunUntilIdle();
363
364  // Verify that the device-global invalidator still does not exist.
365  EXPECT_FALSE(GetDeviceInvalidationService());
366  EXPECT_FALSE(HasDeviceInvalidationServiceObserver());
367
368  // Verify that per-profile invalidation services still exist for both user
369  // profiles.
370  profile_1_invalidation_service = GetProfileInvalidationService(profile_1);
371  EXPECT_TRUE(profile_1_invalidation_service);
372  profile_2_invalidation_service = GetProfileInvalidationService(profile_2);
373  ASSERT_TRUE(profile_2_invalidation_service);
374  EXPECT_EQ(2, GetProfileInvalidationServiceObserverCount());
375
376  // Verify that an invalidator backed by the per-profile invalidation service
377  // for the second user profile has been created.
378  EXPECT_TRUE(HasCloudPolicyInvalidator());
379  EXPECT_EQ(profile_2_invalidation_service, GetInvalidationService());
380
381  // Indicate that the per-profile invalidation service for the second user
382  // profile has disconnected.
383  profile_2_invalidation_service->OnInvalidatorStateChange(
384      syncer::INVALIDATION_CREDENTIALS_REJECTED);
385  base::RunLoop().RunUntilIdle();
386
387  // Verify that a device-global invalidation service has been created.
388  ASSERT_TRUE(GetDeviceInvalidationService());
389  EXPECT_TRUE(HasDeviceInvalidationServiceObserver());
390
391  // Verify that per-profile invalidation services still exist for both user
392  // profiles.
393  profile_1_invalidation_service = GetProfileInvalidationService(profile_1);
394  EXPECT_TRUE(profile_1_invalidation_service);
395  profile_2_invalidation_service = GetProfileInvalidationService(profile_2);
396  EXPECT_TRUE(profile_2_invalidation_service);
397  EXPECT_EQ(2, GetProfileInvalidationServiceObserverCount());
398
399  // Verify that the invalidator has been destroyed.
400  EXPECT_FALSE(HasCloudPolicyInvalidator());
401  EXPECT_FALSE(GetInvalidationService());
402
403  // Indicate that the device-global invalidation service has connected.
404  GetDeviceInvalidationService()->OnInvalidatorStateChange(
405      syncer::INVALIDATIONS_ENABLED);
406  base::RunLoop().RunUntilIdle();
407
408  // Verify that the device-global invalidation service still exists.
409  EXPECT_TRUE(GetDeviceInvalidationService());
410  EXPECT_TRUE(HasDeviceInvalidationServiceObserver());
411
412  // Verify that per-profile invalidation services still exist for both user
413  // profiles.
414  profile_1_invalidation_service = GetProfileInvalidationService(profile_1);
415  EXPECT_TRUE(profile_1_invalidation_service);
416  profile_2_invalidation_service = GetProfileInvalidationService(profile_2);
417  EXPECT_TRUE(profile_2_invalidation_service);
418  EXPECT_EQ(2, GetProfileInvalidationServiceObserverCount());
419
420  // Verify that an invalidator backed by the device-global invalidation service
421  // has been created.
422  EXPECT_TRUE(HasCloudPolicyInvalidator());
423  EXPECT_EQ(GetDeviceInvalidationService(), GetInvalidationService());
424}
425
426}  // namespace policy
427