| 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 chromeos::DeviceOAuth2TokenServiceFactory::Initialize(); | |
| 80 TestingBrowserProcess::GetGlobal()->SetSystemRequestContext( | |
| 81 system_request_context_.get()); | |
| 82 ASSERT_TRUE(profile_manager_.SetUp()); | |
| 83 | |
| 84 test_device_settings_service_.reset(new | |
| 85 chromeos::ScopedTestDeviceSettingsService); | |
| 86 test_cros_settings_.reset(new chromeos::ScopedTestCrosSettings); | |
| 87 scoped_refptr<ownership::MockOwnerKeyUtil> owner_key_util( | |
| 88 new ownership::MockOwnerKeyUtil); | |
| 89 owner_key_util->SetPublicKeyFromPrivateKey( | |
| 90 *device_policy_.GetSigningKey()); | |
| 91 chromeos::DeviceSettingsService::Get()->SetSessionManager( | |
| 92 &device_settings_test_helper_, | |
| 93 owner_key_util); | |
| 94 | |
| 95 device_policy_.policy_data().set_invalidation_source(123); | |
| 96 device_policy_.policy_data().set_invalidation_name("invalidation"); | |
| 97 device_policy_.Build(); | |
| 98 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob()); | |
| 99 device_settings_test_helper_.Flush(); | |
| 100 | |
| 101 scoped_ptr<MockCloudPolicyClient> policy_client(new MockCloudPolicyClient); | |
| 102 EXPECT_CALL(*policy_client, SetupRegistration("token", "device-id")); | |
| 103 CloudPolicyCore* core = TestingBrowserProcess::GetGlobal()->platform_part()-> | |
| 104 browser_policy_connector_chromeos()->GetDeviceCloudPolicyManager()-> | |
| 105 core(); | |
| 106 core->Connect(policy_client.Pass()); | |
| 107 core->StartRefreshScheduler(); | |
| 108 } | |
| 109 | |
| 110 void DeviceCloudPolicyInvalidatorTest::TearDown() { | |
| 111 chromeos::DeviceSettingsService::Get()->UnsetSessionManager(); | |
| 112 TestingBrowserProcess::GetGlobal()->SetBrowserPolicyConnector(nullptr); | |
| 113 chromeos::DeviceOAuth2TokenServiceFactory::Shutdown(); | |
| 114 chromeos::DBusThreadManager::Shutdown(); | |
| 115 chromeos::SystemSaltGetter::Shutdown(); | |
| 116 } | |
| 117 | |
| 118 // Verifies that an invalidator is created/destroyed as an invalidation service | |
| 119 // becomes available/unavailable. Also verifies that the highest handled | |
| 120 // invalidation version is preserved when switching invalidation services. | |
| 121 TEST_F(DeviceCloudPolicyInvalidatorTest, CreateUseDestroy) { | |
| 122 CloudPolicyStore* store = static_cast<CloudPolicyStore*>( | |
| 123 TestingBrowserProcess::GetGlobal()->platform_part()-> | |
| 124 browser_policy_connector_chromeos()->GetDeviceCloudPolicyManager()-> | |
| 125 device_store()); | |
| 126 ASSERT_TRUE(store); | |
| 127 | |
| 128 AffiliatedInvalidationServiceProvider provider; | |
| 129 DeviceCloudPolicyInvalidator device_policy_invalidator(&provider); | |
| 130 | |
| 131 // Verify that no invalidator exists initially. | |
| 132 EXPECT_FALSE(device_policy_invalidator.GetInvalidatorForTest()); | |
| 133 | |
| 134 // Make a first invalidation service available. | |
| 135 invalidation::FakeInvalidationService invalidation_service_1; | |
| 136 device_policy_invalidator.OnInvalidationServiceSet(&invalidation_service_1); | |
| 137 | |
| 138 // Verify that an invalidator backed by the first invalidation service has | |
| 139 // been created and its highest handled invalidation version starts out as 0. | |
| 140 CloudPolicyInvalidator* invalidator = | |
| 141 device_policy_invalidator.GetInvalidatorForTest(); | |
| 142 ASSERT_TRUE(invalidator); | |
| 143 EXPECT_EQ(0, invalidator->highest_handled_invalidation_version()); | |
| 144 EXPECT_EQ(&invalidation_service_1, | |
| 145 invalidator->invalidation_service_for_test()); | |
| 146 | |
| 147 // Handle an invalidation with version 1. Verify that the invalidator's | |
| 148 // highest handled invalidation version is updated accordingly. | |
| 149 store->Store(device_policy_.policy(), 1); | |
| 150 invalidator->OnStoreLoaded(store); | |
| 151 EXPECT_EQ(1, invalidator->highest_handled_invalidation_version()); | |
| 152 | |
| 153 // Make the first invalidation service unavailable. Verify that the | |
| 154 // invalidator is destroyed. | |
| 155 device_policy_invalidator.OnInvalidationServiceSet(nullptr); | |
| 156 EXPECT_FALSE(device_policy_invalidator.GetInvalidatorForTest()); | |
| 157 | |
| 158 // Make a second invalidation service available. | |
| 159 invalidation::FakeInvalidationService invalidation_service_2; | |
| 160 device_policy_invalidator.OnInvalidationServiceSet(&invalidation_service_2); | |
| 161 | |
| 162 // Verify that an invalidator backed by the second invalidation service has | |
| 163 // been created and its highest handled invalidation version starts out as 1. | |
| 164 invalidator = device_policy_invalidator.GetInvalidatorForTest(); | |
| 165 ASSERT_TRUE(invalidator); | |
| 166 EXPECT_EQ(1, invalidator->highest_handled_invalidation_version()); | |
| 167 EXPECT_EQ(&invalidation_service_2, | |
| 168 invalidator->invalidation_service_for_test()); | |
| 169 | |
| 170 // Make the first invalidation service available again. This implies that the | |
| 171 // second invalidation service is no longer available. | |
| 172 device_policy_invalidator.OnInvalidationServiceSet(&invalidation_service_1); | |
| 173 | |
| 174 // Verify that the invalidator backed by the second invalidation service was | |
| 175 // destroyed and an invalidation backed by the first invalidation service has | |
| 176 // been created instead. Also verify that its highest handled invalidation | |
| 177 // version starts out as 1. | |
| 178 invalidator = device_policy_invalidator.GetInvalidatorForTest(); | |
| 179 ASSERT_TRUE(invalidator); | |
| 180 EXPECT_EQ(1, invalidator->highest_handled_invalidation_version()); | |
| 181 EXPECT_EQ(&invalidation_service_1, | |
| 182 invalidator->invalidation_service_for_test()); | |
| 183 | |
| 184 provider.Shutdown(); | |
| 185 device_policy_invalidator.OnInvalidationServiceSet(nullptr); | |
| 186 } | |
| 187 | |
| 188 } // namespace policy | |
| OLD | NEW |