| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <memory> | 5 #include <memory> |
| 6 #include <string> | |
| 7 #include <utility> | |
| 8 | 6 |
| 9 #include "base/bind.h" | |
| 10 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 11 #include "base/files/dir_reader_posix.h" | 8 #include "base/files/dir_reader_posix.h" |
| 12 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 13 #include "base/logging.h" | |
| 14 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 15 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 16 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 17 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 18 #include "base/test/null_task_runner.h" | 14 #include "base/test/null_task_runner.h" |
| 19 #include "base/values.h" | |
| 20 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
| 21 #include "chrome/browser/browser_process_platform_part.h" | 16 #include "chrome/browser/browser_process_platform_part.h" |
| 22 #include "chrome/browser/chromeos/extensions/signin_screen_policy_provider.h" | 17 #include "chrome/browser/chromeos/extensions/signin_screen_policy_provider.h" |
| 23 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" | 18 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" |
| 24 #include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h" | 19 #include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h" |
| 25 #include "chrome/browser/chromeos/policy/device_policy_builder.h" | |
| 26 #include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h" | 20 #include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h" |
| 27 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h" | |
| 28 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 21 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 29 #include "chrome/browser/chromeos/settings/device_settings_service.h" | |
| 30 #include "chrome/browser/extensions/extension_service.h" | 22 #include "chrome/browser/extensions/extension_service.h" |
| 31 #include "chrome/browser/extensions/unpacked_installer.h" | 23 #include "chrome/browser/extensions/unpacked_installer.h" |
| 32 #include "chrome/browser/policy/test/local_policy_test_server.h" | 24 #include "chrome/browser/policy/test/local_policy_test_server.h" |
| 33 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
| 34 #include "chrome/browser/ui/browser.h" | 26 #include "chrome/browser/ui/browser.h" |
| 35 #include "chrome/common/chrome_constants.cc" | 27 #include "chrome/common/chrome_constants.cc" |
| 36 #include "chrome/common/chrome_paths.h" | 28 #include "chrome/common/chrome_paths.h" |
| 37 #include "chrome/common/chrome_switches.h" | 29 #include "chrome/common/chrome_switches.h" |
| 38 #include "chrome/test/base/in_process_browser_test.h" | 30 #include "chrome/test/base/in_process_browser_test.h" |
| 39 #include "chrome/test/base/ui_test_utils.h" | 31 #include "chrome/test/base/ui_test_utils.h" |
| 40 #include "chromeos/chromeos_paths.h" | 32 #include "chromeos/chromeos_paths.h" |
| 41 #include "chromeos/chromeos_switches.h" | 33 #include "chromeos/chromeos_switches.h" |
| 42 #include "chromeos/dbus/fake_session_manager_client.h" | 34 #include "chromeos/dbus/fake_session_manager_client.h" |
| 43 #include "components/ownership/owner_key_util.h" | |
| 44 #include "components/policy/core/browser/browser_policy_connector.h" | |
| 45 #include "components/policy/core/common/cloud/cloud_policy_client.h" | 35 #include "components/policy/core/common/cloud/cloud_policy_client.h" |
| 46 #include "components/policy/core/common/cloud/device_management_service.h" | 36 #include "components/policy/core/common/cloud/device_management_service.h" |
| 47 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h" | 37 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h" |
| 48 #include "components/policy/core/common/cloud/policy_builder.h" | 38 #include "components/policy/core/common/cloud/policy_builder.h" |
| 49 #include "components/policy/core/common/cloud/resource_cache.h" | 39 #include "components/policy/core/common/cloud/resource_cache.h" |
| 50 #include "components/policy/core/common/policy_service.h" | |
| 51 #include "components/policy/core/common/policy_switches.h" | 40 #include "components/policy/core/common/policy_switches.h" |
| 52 #include "components/policy/policy_constants.h" | |
| 53 #include "components/policy/proto/chrome_extension_policy.pb.h" | 41 #include "components/policy/proto/chrome_extension_policy.pb.h" |
| 54 #include "components/policy/proto/device_management_backend.pb.h" | 42 #include "components/policy/proto/device_management_backend.pb.h" |
| 55 #include "crypto/rsa_private_key.h" | 43 #include "crypto/rsa_private_key.h" |
| 56 #include "crypto/sha2.h" | 44 #include "crypto/sha2.h" |
| 57 #include "extensions/browser/extension_registry.h" | 45 #include "extensions/browser/extension_registry.h" |
| 58 #include "extensions/browser/extension_system.h" | 46 #include "extensions/browser/extension_system.h" |
| 59 #include "extensions/common/constants.h" | 47 #include "extensions/common/constants.h" |
| 60 #include "extensions/common/extension.h" | 48 #include "extensions/common/extension.h" |
| 61 #include "extensions/test/extension_test_notification_observer.h" | 49 #include "extensions/test/extension_test_notification_observer.h" |
| 62 #include "extensions/test/result_catcher.h" | 50 #include "extensions/test/result_catcher.h" |
| 63 #include "net/http/http_status_code.h" | 51 #include "net/http/http_status_code.h" |
| 64 #include "net/url_request/test_url_fetcher_factory.h" | 52 #include "net/url_request/test_url_fetcher_factory.h" |
| 65 #include "testing/gtest/include/gtest/gtest.h" | 53 #include "testing/gtest/include/gtest/gtest.h" |
| 66 #include "url/gurl.h" | 54 #include "url/gurl.h" |
| 67 | 55 |
| 68 namespace policy { | 56 namespace policy { |
| 69 | 57 |
| 70 class DeviceCloudPolicyBrowserTest : public InProcessBrowserTest { | 58 class DeviceCloudPolicyBrowserTest : public InProcessBrowserTest { |
| 71 protected: | 59 public: |
| 72 DeviceCloudPolicyBrowserTest() | 60 DeviceCloudPolicyBrowserTest() : mock_client_(new MockCloudPolicyClient) { |
| 73 : mock_client_(base::MakeUnique<MockCloudPolicyClient>()) {} | 61 } |
| 74 | 62 |
| 75 std::unique_ptr<MockCloudPolicyClient> mock_client_; | 63 MockCloudPolicyClient* mock_client_; |
| 76 | |
| 77 private: | |
| 78 DISALLOW_COPY_AND_ASSIGN(DeviceCloudPolicyBrowserTest); | |
| 79 }; | 64 }; |
| 80 | 65 |
| 81 IN_PROC_BROWSER_TEST_F(DeviceCloudPolicyBrowserTest, Initializer) { | 66 IN_PROC_BROWSER_TEST_F(DeviceCloudPolicyBrowserTest, Initializer) { |
| 82 BrowserPolicyConnectorChromeOS* connector = | 67 BrowserPolicyConnectorChromeOS* connector = |
| 83 g_browser_process->platform_part()->browser_policy_connector_chromeos(); | 68 g_browser_process->platform_part()->browser_policy_connector_chromeos(); |
| 84 // Initializer exists at first. | 69 // Initializer exists at first. |
| 85 EXPECT_TRUE(connector->GetDeviceCloudPolicyInitializer()); | 70 EXPECT_TRUE(connector->GetDeviceCloudPolicyInitializer()); |
| 86 | 71 |
| 87 // Initializer is deleted when the manager connects. | 72 // Initializer is deleted when the manager connects. |
| 88 connector->GetDeviceCloudPolicyManager()->StartConnection( | 73 connector->GetDeviceCloudPolicyManager()->StartConnection( |
| 89 std::move(mock_client_), connector->GetInstallAttributes()); | 74 base::WrapUnique(mock_client_), connector->GetInstallAttributes()); |
| 90 EXPECT_FALSE(connector->GetDeviceCloudPolicyInitializer()); | 75 EXPECT_FALSE(connector->GetDeviceCloudPolicyInitializer()); |
| 91 | 76 |
| 92 // Initializer is restarted when the manager disconnects. | 77 // Initializer is restarted when the manager disconnects. |
| 93 connector->GetDeviceCloudPolicyManager()->Disconnect(); | 78 connector->GetDeviceCloudPolicyManager()->Disconnect(); |
| 94 EXPECT_TRUE(connector->GetDeviceCloudPolicyInitializer()); | 79 EXPECT_TRUE(connector->GetDeviceCloudPolicyInitializer()); |
| 95 } | 80 } |
| 96 | 81 |
| 97 // Tests for the rotation of the signing keys used for the device policy. | |
| 98 // | |
| 99 // The test is performed against a test policy server, which is set up for | |
| 100 // rotating the policy key automatically with each policy fetch. | |
| 101 class KeyRotationDeviceCloudPolicyTest : public DevicePolicyCrosBrowserTest { | |
| 102 protected: | |
| 103 const int kTestPolicyValue = 123; | |
| 104 const int kTestPolicyOtherValue = 456; | |
| 105 const char* const kTestPolicyKey = key::kDevicePolicyRefreshRate; | |
| 106 | |
| 107 KeyRotationDeviceCloudPolicyTest() {} | |
| 108 | |
| 109 void SetUp() override { | |
| 110 UpdateBuiltTestPolicyValue(kTestPolicyValue); | |
| 111 StartPolicyTestServer(); | |
| 112 DevicePolicyCrosBrowserTest::SetUp(); | |
| 113 } | |
| 114 | |
| 115 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 116 DevicePolicyCrosBrowserTest::SetUpCommandLine(command_line); | |
| 117 command_line->AppendSwitchASCII(policy::switches::kDeviceManagementUrl, | |
| 118 policy_test_server_.GetServiceURL().spec()); | |
| 119 } | |
| 120 | |
| 121 void SetUpInProcessBrowserTestFixture() override { | |
| 122 DevicePolicyCrosBrowserTest::SetUpInProcessBrowserTestFixture(); | |
| 123 InstallOwnerKey(); | |
| 124 MarkAsEnterpriseOwned(); | |
| 125 SetFakeDevicePolicy(); | |
| 126 } | |
| 127 | |
| 128 void SetUpOnMainThread() override { | |
| 129 DevicePolicyCrosBrowserTest::SetUpOnMainThread(); | |
| 130 g_browser_process->platform_part() | |
| 131 ->browser_policy_connector_chromeos() | |
| 132 ->device_management_service() | |
| 133 ->ScheduleInitialization(0); | |
| 134 StartObservingTestPolicy(); | |
| 135 } | |
| 136 | |
| 137 void TearDownOnMainThread() override { | |
| 138 policy_change_registrar_.reset(); | |
| 139 DevicePolicyCrosBrowserTest::TearDownOnMainThread(); | |
| 140 } | |
| 141 | |
| 142 void UpdateBuiltTestPolicyValue(int test_policy_value) { | |
| 143 device_policy() | |
| 144 ->payload() | |
| 145 .mutable_device_policy_refresh_rate() | |
| 146 ->set_device_policy_refresh_rate(test_policy_value); | |
| 147 device_policy()->Build(); | |
| 148 } | |
| 149 | |
| 150 void UpdateServedTestPolicy() { | |
| 151 EXPECT_TRUE(policy_test_server_.UpdatePolicy( | |
| 152 dm_protocol::kChromeDevicePolicyType, std::string() /* entity_id */, | |
| 153 device_policy()->payload().SerializeAsString())); | |
| 154 } | |
| 155 | |
| 156 int GetTestPolicyValue() { | |
| 157 PolicyService* const policy_service = | |
| 158 g_browser_process->platform_part() | |
| 159 ->browser_policy_connector_chromeos() | |
| 160 ->GetPolicyService(); | |
| 161 const base::Value* policy_value = | |
| 162 policy_service | |
| 163 ->GetPolicies(PolicyNamespace(POLICY_DOMAIN_CHROME, | |
| 164 std::string() /* component_id */)) | |
| 165 .GetValue(kTestPolicyKey); | |
| 166 EXPECT_TRUE(policy_value); | |
| 167 int refresh_rate = -1; | |
| 168 EXPECT_TRUE(policy_value->GetAsInteger(&refresh_rate)); | |
| 169 return refresh_rate; | |
| 170 } | |
| 171 | |
| 172 void WaitForTestPolicyValue(int expected_policy_value) { | |
| 173 if (GetTestPolicyValue() == expected_policy_value) | |
| 174 return; | |
| 175 awaited_policy_value_ = expected_policy_value; | |
| 176 // The run loop will be terminated by TestPolicyChangedCallback() once the | |
| 177 // policy value becomes equal to the awaited value. | |
| 178 DCHECK(!policy_change_waiting_run_loop_); | |
| 179 policy_change_waiting_run_loop_ = base::MakeUnique<base::RunLoop>(); | |
| 180 policy_change_waiting_run_loop_->Run(); | |
| 181 } | |
| 182 | |
| 183 private: | |
| 184 void StartPolicyTestServer() { | |
| 185 policy_test_server_.RegisterClient(PolicyBuilder::kFakeToken, | |
| 186 PolicyBuilder::kFakeDeviceId); | |
| 187 UpdateServedTestPolicy(); | |
| 188 policy_test_server_.EnableAutomaticRotationOfSigningKeys(); | |
| 189 EXPECT_TRUE(policy_test_server_.Start()); | |
| 190 } | |
| 191 | |
| 192 void SetFakeDevicePolicy() { | |
| 193 device_policy() | |
| 194 ->payload() | |
| 195 .mutable_device_policy_refresh_rate() | |
| 196 ->set_device_policy_refresh_rate(kTestPolicyValue); | |
| 197 device_policy()->Build(); | |
| 198 session_manager_client()->set_device_policy(device_policy()->GetBlob()); | |
| 199 } | |
| 200 | |
| 201 void StartObservingTestPolicy() { | |
| 202 policy_change_registrar_ = base::MakeUnique<PolicyChangeRegistrar>( | |
| 203 g_browser_process->platform_part() | |
| 204 ->browser_policy_connector_chromeos() | |
| 205 ->GetPolicyService(), | |
| 206 PolicyNamespace(POLICY_DOMAIN_CHROME, | |
| 207 std::string() /* component_id */)); | |
| 208 policy_change_registrar_->Observe( | |
| 209 kTestPolicyKey, | |
| 210 base::BindRepeating( | |
| 211 &KeyRotationDeviceCloudPolicyTest::TestPolicyChangedCallback, | |
| 212 base::Unretained(this))); | |
| 213 } | |
| 214 | |
| 215 void TestPolicyChangedCallback(const base::Value* old_value, | |
| 216 const base::Value* new_value) { | |
| 217 if (policy_change_waiting_run_loop_ && | |
| 218 GetTestPolicyValue() == awaited_policy_value_) { | |
| 219 policy_change_waiting_run_loop_->Quit(); | |
| 220 } | |
| 221 } | |
| 222 | |
| 223 LocalPolicyTestServer policy_test_server_; | |
| 224 std::unique_ptr<PolicyChangeRegistrar> policy_change_registrar_; | |
| 225 int awaited_policy_value_ = -1; | |
| 226 std::unique_ptr<base::RunLoop> policy_change_waiting_run_loop_; | |
| 227 | |
| 228 DISALLOW_COPY_AND_ASSIGN(KeyRotationDeviceCloudPolicyTest); | |
| 229 }; | |
| 230 | |
| 231 IN_PROC_BROWSER_TEST_F(KeyRotationDeviceCloudPolicyTest, Basic) { | |
| 232 // Initially, the policy has the first value. | |
| 233 EXPECT_EQ(kTestPolicyValue, GetTestPolicyValue()); | |
| 234 | |
| 235 const std::string original_owner_public_key = | |
| 236 chromeos::DeviceSettingsService::Get()->GetPublicKey()->as_string(); | |
| 237 | |
| 238 // The server is updated to serve the new policy value, and the client fetches | |
| 239 // it. | |
| 240 UpdateBuiltTestPolicyValue(kTestPolicyOtherValue); | |
| 241 UpdateServedTestPolicy(); | |
| 242 g_browser_process->platform_part() | |
| 243 ->browser_policy_connector_chromeos() | |
| 244 ->GetDeviceCloudPolicyManager() | |
| 245 ->RefreshPolicies(); | |
| 246 WaitForTestPolicyValue(kTestPolicyOtherValue); | |
| 247 EXPECT_EQ(kTestPolicyOtherValue, GetTestPolicyValue()); | |
| 248 | |
| 249 // The owner key has changed due to the key rotation performed by the policy | |
| 250 // test server. | |
| 251 EXPECT_NE( | |
| 252 original_owner_public_key, | |
| 253 chromeos::DeviceSettingsService::Get()->GetPublicKey()->as_string()); | |
| 254 } | |
| 255 | |
| 256 // This class is the base class for the tests of the behavior regarding | 82 // This class is the base class for the tests of the behavior regarding |
| 257 // extensions installed on the signin screen (which is generally possible only | 83 // extensions installed on the signin screen (which is generally possible only |
| 258 // through an admin policy, but the tests may install the extensions directly | 84 // through an admin policy, but the tests may install the extensions directly |
| 259 // for the test purposes). | 85 // for the test purposes). |
| 260 class SigninExtensionsDeviceCloudPolicyBrowserTestBase | 86 class SigninExtensionsDeviceCloudPolicyBrowserTestBase |
| 261 : public DevicePolicyCrosBrowserTest { | 87 : public DevicePolicyCrosBrowserTest { |
| 262 protected: | 88 protected: |
| 263 static constexpr const char* kTestExtensionId = | 89 static constexpr const char* kTestExtensionId = |
| 264 "baogpbmpccplckhhehfipokjaflkmbno"; | 90 "baogpbmpccplckhhehfipokjaflkmbno"; |
| 265 static constexpr const char* kTestExtensionSourceDir = | 91 static constexpr const char* kTestExtensionSourceDir = |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 ASSERT_TRUE(extension); | 362 ASSERT_TRUE(extension); |
| 537 Browser* browser = CreateBrowser(GetSigninProfile()); | 363 Browser* browser = CreateBrowser(GetSigninProfile()); |
| 538 extensions::ResultCatcher result_catcher; | 364 extensions::ResultCatcher result_catcher; |
| 539 ui_test_utils::NavigateToURL( | 365 ui_test_utils::NavigateToURL( |
| 540 browser, extension->GetResourceURL(kTestExtensionTestPage)); | 366 browser, extension->GetResourceURL(kTestExtensionTestPage)); |
| 541 EXPECT_TRUE(result_catcher.GetNextResult()); | 367 EXPECT_TRUE(result_catcher.GetNextResult()); |
| 542 CloseBrowserSynchronously(browser); | 368 CloseBrowserSynchronously(browser); |
| 543 } | 369 } |
| 544 | 370 |
| 545 } // namespace policy | 371 } // namespace policy |
| OLD | NEW |