| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/base64.h" |
| 7 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/file_util.h" |
| 8 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 9 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 10 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 11 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/strings/string_util.h" |
| 12 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/extensions/extension_browsertest.h" | 16 #include "chrome/browser/extensions/extension_browsertest.h" |
| 14 #include "chrome/browser/extensions/extension_test_message_listener.h" | 17 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 15 #include "chrome/browser/policy/browser_policy_connector.h" | 18 #include "chrome/browser/policy/browser_policy_connector.h" |
| 16 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" | 19 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" |
| 17 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" | 20 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" |
| 18 #include "chrome/browser/policy/policy_service.h" | 21 #include "chrome/browser/policy/policy_service.h" |
| 19 #include "chrome/browser/policy/profile_policy_connector.h" | 22 #include "chrome/browser/policy/profile_policy_connector.h" |
| 20 #include "chrome/browser/policy/profile_policy_connector_factory.h" | 23 #include "chrome/browser/policy/profile_policy_connector_factory.h" |
| 21 #include "chrome/browser/policy/proto/cloud/chrome_extension_policy.pb.h" | 24 #include "chrome/browser/policy/proto/cloud/chrome_extension_policy.pb.h" |
| 22 #include "chrome/browser/policy/test/local_policy_test_server.h" | 25 #include "chrome/browser/policy/test/local_policy_test_server.h" |
| 23 #include "chrome/browser/policy/test/policy_test_utils.h" | 26 #include "chrome/browser/policy/test/policy_test_utils.h" |
| 24 #include "chrome/browser/profiles/profile.h" | 27 #include "chrome/browser/profiles/profile.h" |
| 28 #include "chrome/browser/ui/browser.h" |
| 25 #include "chrome/common/chrome_paths.h" | 29 #include "chrome/common/chrome_paths.h" |
| 26 #include "chrome/common/chrome_switches.h" | 30 #include "chrome/common/chrome_switches.h" |
| 27 #include "extensions/common/extension.h" | 31 #include "extensions/common/extension.h" |
| 28 #include "net/url_request/url_request_context_getter.h" | 32 #include "net/url_request/url_request_context_getter.h" |
| 29 #include "policy/proto/cloud_policy.pb.h" | 33 #include "policy/proto/cloud_policy.pb.h" |
| 30 #include "testing/gmock/include/gmock/gmock.h" | 34 #include "testing/gmock/include/gmock/gmock.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
| 32 | 36 |
| 33 #if defined(OS_CHROMEOS) | 37 #if defined(OS_CHROMEOS) |
| 34 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h" | 38 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 | 77 |
| 74 const char kTestPolicy2[] = | 78 const char kTestPolicy2[] = |
| 75 "{" | 79 "{" |
| 76 " \"Another\": {" | 80 " \"Another\": {" |
| 77 " \"Value\": \"turn_it_off\"" | 81 " \"Value\": \"turn_it_off\"" |
| 78 " }" | 82 " }" |
| 79 "}"; | 83 "}"; |
| 80 | 84 |
| 81 const char kTestPolicy2JSON[] = "{\"Another\":\"turn_it_off\"}"; | 85 const char kTestPolicy2JSON[] = "{\"Another\":\"turn_it_off\"}"; |
| 82 | 86 |
| 87 // Same encoding as ResourceCache does for its keys. |
| 88 bool Base64Encode(const std::string& value, std::string* encoded) { |
| 89 if (value.empty() || !base::Base64Encode(value, encoded)) |
| 90 return false; |
| 91 ReplaceChars(*encoded, "+", "-", encoded); |
| 92 ReplaceChars(*encoded, "/", "_", encoded); |
| 93 return true; |
| 94 } |
| 95 |
| 83 class ComponentCloudPolicyTest : public ExtensionBrowserTest { | 96 class ComponentCloudPolicyTest : public ExtensionBrowserTest { |
| 84 protected: | 97 protected: |
| 85 ComponentCloudPolicyTest() {} | 98 ComponentCloudPolicyTest() {} |
| 86 virtual ~ComponentCloudPolicyTest() {} | 99 virtual ~ComponentCloudPolicyTest() {} |
| 87 | 100 |
| 88 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 101 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 89 ExtensionBrowserTest::SetUpCommandLine(command_line); | 102 ExtensionBrowserTest::SetUpCommandLine(command_line); |
| 90 #if defined(OS_CHROMEOS) | 103 #if defined(OS_CHROMEOS) |
| 91 // ExtensionBrowserTest sets the login users to a non-managed value; | 104 // ExtensionBrowserTest sets the login users to a non-managed value; |
| 92 // replace it. This is the default username sent in policy blobs from the | 105 // replace it. This is the default username sent in policy blobs from the |
| (...skipping 21 matching lines...) Expand all Loading... |
| 114 << "Pre-existing policies in this machine will make this test fail."; | 127 << "Pre-existing policies in this machine will make this test fail."; |
| 115 | 128 |
| 116 // Install the initial extension. | 129 // Install the initial extension. |
| 117 ExtensionTestMessageListener ready_listener("ready", true); | 130 ExtensionTestMessageListener ready_listener("ready", true); |
| 118 event_listener_.reset(new ExtensionTestMessageListener("event", true)); | 131 event_listener_.reset(new ExtensionTestMessageListener("event", true)); |
| 119 extension_ = LoadExtension(kTestExtensionPath); | 132 extension_ = LoadExtension(kTestExtensionPath); |
| 120 ASSERT_TRUE(extension_.get()); | 133 ASSERT_TRUE(extension_.get()); |
| 121 ASSERT_EQ(kTestExtension, extension_->id()); | 134 ASSERT_EQ(kTestExtension, extension_->id()); |
| 122 EXPECT_TRUE(ready_listener.WaitUntilSatisfied()); | 135 EXPECT_TRUE(ready_listener.WaitUntilSatisfied()); |
| 123 | 136 |
| 137 // And start with a signed-in user. |
| 138 SignInAndRegister(); |
| 139 |
| 140 // The extension will receive an update event. |
| 141 EXPECT_TRUE(event_listener_->WaitUntilSatisfied()); |
| 142 |
| 143 ExtensionBrowserTest::SetUpOnMainThread(); |
| 144 } |
| 145 |
| 146 scoped_refptr<const extensions::Extension> LoadExtension( |
| 147 const base::FilePath::CharType* path) { |
| 148 base::FilePath full_path; |
| 149 if (!PathService::Get(chrome::DIR_TEST_DATA, &full_path)) { |
| 150 ADD_FAILURE(); |
| 151 return NULL; |
| 152 } |
| 153 scoped_refptr<const extensions::Extension> extension( |
| 154 ExtensionBrowserTest::LoadExtension(full_path.Append(path))); |
| 155 if (!extension.get()) { |
| 156 ADD_FAILURE(); |
| 157 return NULL; |
| 158 } |
| 159 return extension; |
| 160 } |
| 161 |
| 162 void SignInAndRegister() { |
| 124 BrowserPolicyConnector* connector = | 163 BrowserPolicyConnector* connector = |
| 125 g_browser_process->browser_policy_connector(); | 164 g_browser_process->browser_policy_connector(); |
| 126 connector->ScheduleServiceInitialization(0); | 165 connector->ScheduleServiceInitialization(0); |
| 127 | 166 |
| 128 #if defined(OS_CHROMEOS) | 167 #if defined(OS_CHROMEOS) |
| 129 UserCloudPolicyManagerChromeOS* policy_manager = | 168 UserCloudPolicyManagerChromeOS* policy_manager = |
| 130 UserCloudPolicyManagerFactoryChromeOS::GetForProfile( | 169 UserCloudPolicyManagerFactoryChromeOS::GetForProfile( |
| 131 browser()->profile()); | 170 browser()->profile()); |
| 132 ASSERT_TRUE(policy_manager); | 171 ASSERT_TRUE(policy_manager); |
| 133 #else | 172 #else |
| (...skipping 20 matching lines...) Expand all Loading... |
| 154 ASSERT_TRUE(policy_manager->core()->client()); | 193 ASSERT_TRUE(policy_manager->core()->client()); |
| 155 base::RunLoop run_loop; | 194 base::RunLoop run_loop; |
| 156 MockCloudPolicyClientObserver observer; | 195 MockCloudPolicyClientObserver observer; |
| 157 EXPECT_CALL(observer, OnRegistrationStateChanged(_)) | 196 EXPECT_CALL(observer, OnRegistrationStateChanged(_)) |
| 158 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); | 197 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); |
| 159 policy_manager->core()->client()->AddObserver(&observer); | 198 policy_manager->core()->client()->AddObserver(&observer); |
| 160 policy_manager->core()->client()->SetupRegistration(kDMToken, kDeviceID); | 199 policy_manager->core()->client()->SetupRegistration(kDMToken, kDeviceID); |
| 161 run_loop.Run(); | 200 run_loop.Run(); |
| 162 Mock::VerifyAndClearExpectations(&observer); | 201 Mock::VerifyAndClearExpectations(&observer); |
| 163 policy_manager->core()->client()->RemoveObserver(&observer); | 202 policy_manager->core()->client()->RemoveObserver(&observer); |
| 164 | |
| 165 // The extension will receive an update event. | |
| 166 EXPECT_TRUE(event_listener_->WaitUntilSatisfied()); | |
| 167 | |
| 168 ExtensionBrowserTest::SetUpOnMainThread(); | |
| 169 } | 203 } |
| 170 | 204 |
| 171 scoped_refptr<const extensions::Extension> LoadExtension( | 205 #if !defined(OS_CHROMEOS) |
| 172 const base::FilePath::CharType* path) { | 206 void SignOut() { |
| 173 base::FilePath full_path; | 207 SigninManager* signin_manager = |
| 174 if (!PathService::Get(chrome::DIR_TEST_DATA, &full_path)) { | 208 SigninManagerFactory::GetForProfile(browser()->profile()); |
| 175 ADD_FAILURE(); | 209 ASSERT_TRUE(signin_manager); |
| 176 return NULL; | 210 signin_manager->SignOut(); |
| 177 } | |
| 178 scoped_refptr<const extensions::Extension> extension( | |
| 179 ExtensionBrowserTest::LoadExtension(full_path.Append(path))); | |
| 180 if (!extension.get()) { | |
| 181 ADD_FAILURE(); | |
| 182 return NULL; | |
| 183 } | |
| 184 return extension; | |
| 185 } | 211 } |
| 212 #endif |
| 186 | 213 |
| 187 void RefreshPolicies() { | 214 void RefreshPolicies() { |
| 188 ProfilePolicyConnector* profile_connector = | 215 ProfilePolicyConnector* profile_connector = |
| 189 ProfilePolicyConnectorFactory::GetForProfile(browser()->profile()); | 216 ProfilePolicyConnectorFactory::GetForProfile(browser()->profile()); |
| 190 PolicyService* policy_service = profile_connector->policy_service(); | 217 PolicyService* policy_service = profile_connector->policy_service(); |
| 191 base::RunLoop run_loop; | 218 base::RunLoop run_loop; |
| 192 policy_service->RefreshPolicies(run_loop.QuitClosure()); | 219 policy_service->RefreshPolicies(run_loop.QuitClosure()); |
| 193 run_loop.Run(); | 220 run_loop.Run(); |
| 194 } | 221 } |
| 195 | 222 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 scoped_refptr<const extensions::Extension> extension2 = | 268 scoped_refptr<const extensions::Extension> extension2 = |
| 242 LoadExtension(kTestExtension2Path); | 269 LoadExtension(kTestExtension2Path); |
| 243 ASSERT_TRUE(extension2.get()); | 270 ASSERT_TRUE(extension2.get()); |
| 244 ASSERT_EQ(kTestExtension2, extension2->id()); | 271 ASSERT_EQ(kTestExtension2, extension2->id()); |
| 245 | 272 |
| 246 // This extension only sends the 'policy' signal once it receives the policy, | 273 // This extension only sends the 'policy' signal once it receives the policy, |
| 247 // and after verifying it has the expected value. Otherwise it sends 'fail'. | 274 // and after verifying it has the expected value. Otherwise it sends 'fail'. |
| 248 EXPECT_TRUE(result_listener.WaitUntilSatisfied()); | 275 EXPECT_TRUE(result_listener.WaitUntilSatisfied()); |
| 249 } | 276 } |
| 250 | 277 |
| 278 // Signing out on Chrome OS is a different process from signing out on the |
| 279 // Desktop platforms. On Chrome OS the session is ended, and the user goes back |
| 280 // to the sign-in screen; the Profile data is not affected. On the Desktop the |
| 281 // session goes on though, and all the signed-in services are disconnected; |
| 282 // in particular, the policy caches are dropped if the user signs out. |
| 283 // This test verifies that when the user signs out then any existing component |
| 284 // policy caches are dropped, and that it's still possible to sign back in and |
| 285 // get policy for components working again. |
| 286 #if !defined(OS_CHROMEOS) |
| 287 IN_PROC_BROWSER_TEST_F(ComponentCloudPolicyTest, SignOutAndBackIn) { |
| 288 // Read the initial policy. |
| 289 ExtensionTestMessageListener initial_policy_listener(kTestPolicyJSON, true); |
| 290 event_listener_->Reply("get-policy-Name"); |
| 291 EXPECT_TRUE(initial_policy_listener.WaitUntilSatisfied()); |
| 292 |
| 293 // Verify that the policy cache exists. |
| 294 std::string cache_key; |
| 295 ASSERT_TRUE(Base64Encode("extension-policy", &cache_key)); |
| 296 std::string cache_subkey; |
| 297 ASSERT_TRUE(Base64Encode(kTestExtension, &cache_subkey)); |
| 298 base::FilePath cache_path = browser()->profile()->GetPath() |
| 299 .Append(FILE_PATH_LITERAL("Policy")) |
| 300 .Append(FILE_PATH_LITERAL("Components")) |
| 301 .AppendASCII(cache_key) |
| 302 .AppendASCII(cache_subkey); |
| 303 EXPECT_TRUE(base::PathExists(cache_path)); |
| 304 |
| 305 // Now sign-out. The policy cache should be removed, and the extension should |
| 306 // get an empty policy update. |
| 307 ExtensionTestMessageListener event_listener("event", true); |
| 308 initial_policy_listener.Reply("idle"); |
| 309 SignOut(); |
| 310 EXPECT_TRUE(event_listener.WaitUntilSatisfied()); |
| 311 |
| 312 // The extension got an update event; verify that the policy was empty. |
| 313 ExtensionTestMessageListener signout_policy_listener("{}", true); |
| 314 event_listener.Reply("get-policy-Name"); |
| 315 EXPECT_TRUE(signout_policy_listener.WaitUntilSatisfied()); |
| 316 |
| 317 // Verify that the cache is gone. |
| 318 EXPECT_FALSE(base::PathExists(cache_path)); |
| 319 |
| 320 // Verify that the policy is fetched again if the user signs back in. |
| 321 ExtensionTestMessageListener event_listener2("event", true); |
| 322 SignInAndRegister(); |
| 323 EXPECT_TRUE(event_listener2.WaitUntilSatisfied()); |
| 324 |
| 325 // The extension got updated policy; verify it. |
| 326 ExtensionTestMessageListener signin_policy_listener(kTestPolicyJSON, true); |
| 327 event_listener2.Reply("get-policy-Name"); |
| 328 EXPECT_TRUE(signin_policy_listener.WaitUntilSatisfied()); |
| 329 |
| 330 // And the cache is back. |
| 331 EXPECT_TRUE(base::PathExists(cache_path)); |
| 332 } |
| 333 #endif |
| 334 |
| 251 } // namespace policy | 335 } // namespace policy |
| OLD | NEW |