| 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/device_cloud_policy_invalidator.h" | |
| 6 | |
| 7 #include "base/memory/ref_counted.h" | |
| 8 #include "base/message_loop/message_loop_proxy.h" | |
| 9 #include "chrome/browser/browser_process_platform_part.h" | |
| 10 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" | |
| 11 #include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h" | |
| 12 #include "chrome/browser/chromeos/policy/device_cloud_policy_store_chromeos.h" | |
| 13 #include "chrome/browser/chromeos/policy/device_policy_builder.h" | |
| 14 #include "chrome/browser/chromeos/policy/stub_enterprise_install_attributes.h" | |
| 15 #include "chrome/browser/chromeos/settings/cros_settings.h" | |
| 16 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h
" | |
| 17 #include "chrome/browser/chromeos/settings/device_settings_service.h" | |
| 18 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h" | |
| 19 #include "chrome/browser/invalidation/fake_invalidation_service.h" | |
| 20 #include "chrome/browser/policy/cloud/cloud_policy_invalidator.h" | |
| 21 #include "chrome/test/base/testing_browser_process.h" | |
| 22 #include "chrome/test/base/testing_profile_manager.h" | |
| 23 #include "chromeos/cryptohome/system_salt_getter.h" | |
| 24 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 25 #include "components/ownership/mock_owner_key_util.h" | |
| 26 #include "components/policy/core/common/cloud/cloud_policy_constants.h" | |
| 27 #include "components/policy/core/common/cloud/cloud_policy_core.h" | |
| 28 #include "components/policy/core/common/cloud/cloud_policy_store.h" | |
| 29 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h" | |
| 30 #include "content/public/test/test_browser_thread_bundle.h" | |
| 31 #include "net/url_request/url_request_context_getter.h" | |
| 32 #include "net/url_request/url_request_test_util.h" | |
| 33 #include "policy/proto/device_management_backend.pb.h" | |
| 34 #include "testing/gmock/include/gmock/gmock.h" | |
| 35 #include "testing/gtest/include/gtest/gtest.h" | |
| 36 | |
| 37 namespace policy { | |
| 38 | |
| 39 class DeviceCloudPolicyInvalidatorTest : public testing::Test { | |
| 40 public: | |
| 41 DeviceCloudPolicyInvalidatorTest(); | |
| 42 ~DeviceCloudPolicyInvalidatorTest() override; | |
| 43 | |
| 44 // testing::Test: | |
| 45 void SetUp() override; | |
| 46 void TearDown() override; | |
| 47 | |
| 48 protected: | |
| 49 DevicePolicyBuilder device_policy_; | |
| 50 | |
| 51 private: | |
| 52 content::TestBrowserThreadBundle thread_bundle_; | |
| 53 scoped_refptr<net::URLRequestContextGetter> system_request_context_; | |
| 54 ScopedStubEnterpriseInstallAttributes install_attributes_; | |
| 55 scoped_ptr<chromeos::ScopedTestDeviceSettingsService> | |
| 56 test_device_settings_service_; | |
| 57 scoped_ptr<chromeos::ScopedTestCrosSettings> test_cros_settings_; | |
| 58 chromeos::DeviceSettingsTestHelper device_settings_test_helper_; | |
| 59 TestingProfileManager profile_manager_; | |
| 60 }; | |
| 61 | |
| 62 DeviceCloudPolicyInvalidatorTest::DeviceCloudPolicyInvalidatorTest() | |
| 63 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | |
| 64 system_request_context_(new net::TestURLRequestContextGetter( | |
| 65 base::MessageLoopProxy::current())), | |
| 66 install_attributes_("example.com", | |
| 67 "user@example.com", | |
| 68 "device_id", | |
| 69 DEVICE_MODE_ENTERPRISE), | |
| 70 profile_manager_(TestingBrowserProcess::GetGlobal()) { | |
| 71 } | |
| 72 | |
| 73 DeviceCloudPolicyInvalidatorTest::~DeviceCloudPolicyInvalidatorTest() { | |
| 74 } | |
| 75 | |
| 76 void DeviceCloudPolicyInvalidatorTest::SetUp() { | |
| 77 chromeos::SystemSaltGetter::Initialize(); | |
| 78 chromeos::DBusThreadManager::Initialize(); | |
| 79 TestingBrowserProcess::GetGlobal()->SetSystemRequestContext( | |
| 80 system_request_context_.get()); | |
| 81 ASSERT_TRUE(profile_manager_.SetUp()); | |
| 82 | |
| 83 test_device_settings_service_.reset(new | |
| 84 chromeos::ScopedTestDeviceSettingsService); | |
| 85 test_cros_settings_.reset(new chromeos::ScopedTestCrosSettings); | |
| 86 chromeos::DeviceOAuth2TokenServiceFactory::Initialize(); | |
| 87 | |
| 88 scoped_refptr<ownership::MockOwnerKeyUtil> owner_key_util( | |
| 89 new ownership::MockOwnerKeyUtil); | |
| 90 owner_key_util->SetPublicKeyFromPrivateKey( | |
| 91 *device_policy_.GetSigningKey()); | |
| 92 chromeos::DeviceSettingsService::Get()->SetSessionManager( | |
| 93 &device_settings_test_helper_, | |
| 94 owner_key_util); | |
| 95 | |
| 96 device_policy_.policy_data().set_invalidation_source(123); | |
| 97 device_policy_.policy_data().set_invalidation_name("invalidation"); | |
| 98 device_policy_.Build(); | |
| 99 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob()); | |
| 100 device_settings_test_helper_.Flush(); | |
| 101 | |
| 102 scoped_ptr<MockCloudPolicyClient> policy_client(new MockCloudPolicyClient); | |
| 103 EXPECT_CALL(*policy_client, SetupRegistration("token", "device-id")); | |
| 104 CloudPolicyCore* core = TestingBrowserProcess::GetGlobal()->platform_part()-> | |
| 105 browser_policy_connector_chromeos()->GetDeviceCloudPolicyManager()-> | |
| 106 core(); | |
| 107 core->Connect(policy_client.Pass()); | |
| 108 core->StartRefreshScheduler(); | |
| 109 } | |
| 110 | |
| 111 void DeviceCloudPolicyInvalidatorTest::TearDown() { | |
| 112 chromeos::DeviceSettingsService::Get()->UnsetSessionManager(); | |
| 113 TestingBrowserProcess::GetGlobal()->SetBrowserPolicyConnector(nullptr); | |
| 114 chromeos::DeviceOAuth2TokenServiceFactory::Shutdown(); | |
| 115 chromeos::DBusThreadManager::Shutdown(); | |
| 116 chromeos::SystemSaltGetter::Shutdown(); | |
| 117 } | |
| 118 | |
| 119 // Verifies that an invalidator is created/destroyed as an invalidation service | |
| 120 // becomes available/unavailable. Also verifies that the highest handled | |
| 121 // invalidation version is preserved when switching invalidation services. | |
| 122 TEST_F(DeviceCloudPolicyInvalidatorTest, CreateUseDestroy) { | |
| 123 CloudPolicyStore* store = static_cast<CloudPolicyStore*>( | |
| 124 TestingBrowserProcess::GetGlobal()->platform_part()-> | |
| 125 browser_policy_connector_chromeos()->GetDeviceCloudPolicyManager()-> | |
| 126 device_store()); | |
| 127 ASSERT_TRUE(store); | |
| 128 | |
| 129 AffiliatedInvalidationServiceProvider provider; | |
| 130 DeviceCloudPolicyInvalidator device_policy_invalidator(&provider); | |
| 131 | |
| 132 // Verify that no invalidator exists initially. | |
| 133 EXPECT_FALSE(device_policy_invalidator.GetInvalidatorForTest()); | |
| 134 | |
| 135 // Make a first invalidation service available. | |
| 136 invalidation::FakeInvalidationService invalidation_service_1; | |
| 137 device_policy_invalidator.OnInvalidationServiceSet(&invalidation_service_1); | |
| 138 | |
| 139 // Verify that an invalidator backed by the first invalidation service has | |
| 140 // been created and its highest handled invalidation version starts out as 0. | |
| 141 CloudPolicyInvalidator* invalidator = | |
| 142 device_policy_invalidator.GetInvalidatorForTest(); | |
| 143 ASSERT_TRUE(invalidator); | |
| 144 EXPECT_EQ(0, invalidator->highest_handled_invalidation_version()); | |
| 145 EXPECT_EQ(&invalidation_service_1, | |
| 146 invalidator->invalidation_service_for_test()); | |
| 147 | |
| 148 // Handle an invalidation with version 1. Verify that the invalidator's | |
| 149 // highest handled invalidation version is updated accordingly. | |
| 150 store->Store(device_policy_.policy(), 1); | |
| 151 invalidator->OnStoreLoaded(store); | |
| 152 EXPECT_EQ(1, invalidator->highest_handled_invalidation_version()); | |
| 153 | |
| 154 // Make the first invalidation service unavailable. Verify that the | |
| 155 // invalidator is destroyed. | |
| 156 device_policy_invalidator.OnInvalidationServiceSet(nullptr); | |
| 157 EXPECT_FALSE(device_policy_invalidator.GetInvalidatorForTest()); | |
| 158 | |
| 159 // Make a second invalidation service available. | |
| 160 invalidation::FakeInvalidationService invalidation_service_2; | |
| 161 device_policy_invalidator.OnInvalidationServiceSet(&invalidation_service_2); | |
| 162 | |
| 163 // Verify that an invalidator backed by the second invalidation service has | |
| 164 // been created and its highest handled invalidation version starts out as 1. | |
| 165 invalidator = device_policy_invalidator.GetInvalidatorForTest(); | |
| 166 ASSERT_TRUE(invalidator); | |
| 167 EXPECT_EQ(1, 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 device_policy_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 starts out as 1. | |
| 179 invalidator = device_policy_invalidator.GetInvalidatorForTest(); | |
| 180 ASSERT_TRUE(invalidator); | |
| 181 EXPECT_EQ(1, invalidator->highest_handled_invalidation_version()); | |
| 182 EXPECT_EQ(&invalidation_service_1, | |
| 183 invalidator->invalidation_service_for_test()); | |
| 184 | |
| 185 provider.Shutdown(); | |
| 186 device_policy_invalidator.OnInvalidationServiceSet(nullptr); | |
| 187 } | |
| 188 | |
| 189 } // namespace policy | |
| OLD | NEW |