Chromium Code Reviews| 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" |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 73 | 76 |
| 74 const char kTestPolicy2[] = | 77 const char kTestPolicy2[] = |
| 75 "{" | 78 "{" |
| 76 " \"Another\": {" | 79 " \"Another\": {" |
| 77 " \"Value\": \"turn_it_off\"" | 80 " \"Value\": \"turn_it_off\"" |
| 78 " }" | 81 " }" |
| 79 "}"; | 82 "}"; |
| 80 | 83 |
| 81 const char kTestPolicy2JSON[] = "{\"Another\":\"turn_it_off\"}"; | 84 const char kTestPolicy2JSON[] = "{\"Another\":\"turn_it_off\"}"; |
| 82 | 85 |
| 86 // Same encoding as ResourceCache does for its keys. | |
| 87 bool Base64Encode(const std::string& value, std::string* encoded) { | |
| 88 DCHECK(!value.empty()); | |
|
bartfab (slow)
2013/12/02 18:08:46
Nit 1: #include "base/logging.h"
Nit 2: CHECKs are
Joao da Silva
2013/12/03 09:02:31
Right, this was just a lazy copy/paste from resour
| |
| 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 18 matching lines...) Expand all Loading... | |
| 152 ASSERT_TRUE(policy_manager->core()->client()); | 191 ASSERT_TRUE(policy_manager->core()->client()); |
| 153 base::RunLoop run_loop; | 192 base::RunLoop run_loop; |
| 154 MockCloudPolicyClientObserver observer; | 193 MockCloudPolicyClientObserver observer; |
| 155 EXPECT_CALL(observer, OnRegistrationStateChanged(_)) | 194 EXPECT_CALL(observer, OnRegistrationStateChanged(_)) |
| 156 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); | 195 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); |
| 157 policy_manager->core()->client()->AddObserver(&observer); | 196 policy_manager->core()->client()->AddObserver(&observer); |
| 158 policy_manager->core()->client()->SetupRegistration(kDMToken, kDeviceID); | 197 policy_manager->core()->client()->SetupRegistration(kDMToken, kDeviceID); |
| 159 run_loop.Run(); | 198 run_loop.Run(); |
| 160 Mock::VerifyAndClearExpectations(&observer); | 199 Mock::VerifyAndClearExpectations(&observer); |
| 161 policy_manager->core()->client()->RemoveObserver(&observer); | 200 policy_manager->core()->client()->RemoveObserver(&observer); |
| 162 | |
| 163 // The extension will receive an update event. | |
| 164 EXPECT_TRUE(event_listener_->WaitUntilSatisfied()); | |
| 165 | |
| 166 ExtensionBrowserTest::SetUpOnMainThread(); | |
| 167 } | 201 } |
| 168 | 202 |
| 169 scoped_refptr<const extensions::Extension> LoadExtension( | 203 #if !defined(OS_CHROMEOS) |
| 170 const base::FilePath::CharType* path) { | 204 void SignOut() { |
| 171 base::FilePath full_path; | 205 SigninManager* signin_manager = |
| 172 if (!PathService::Get(chrome::DIR_TEST_DATA, &full_path)) { | 206 SigninManagerFactory::GetForProfile(browser()->profile()); |
|
bartfab (slow)
2013/12/02 18:08:46
Nit: #include "chrome/browser/ui/browser.h"
Joao da Silva
2013/12/03 09:02:31
Done.
| |
| 173 ADD_FAILURE(); | 207 ASSERT_TRUE(signin_manager); |
| 174 return NULL; | 208 signin_manager->SignOut(); |
| 175 } | |
| 176 scoped_refptr<const extensions::Extension> extension( | |
| 177 ExtensionBrowserTest::LoadExtension(full_path.Append(path))); | |
| 178 if (!extension.get()) { | |
| 179 ADD_FAILURE(); | |
| 180 return NULL; | |
| 181 } | |
| 182 return extension; | |
| 183 } | 209 } |
| 210 #endif | |
| 184 | 211 |
| 185 void RefreshPolicies() { | 212 void RefreshPolicies() { |
| 186 ProfilePolicyConnector* profile_connector = | 213 ProfilePolicyConnector* profile_connector = |
| 187 ProfilePolicyConnectorFactory::GetForProfile(browser()->profile()); | 214 ProfilePolicyConnectorFactory::GetForProfile(browser()->profile()); |
| 188 PolicyService* policy_service = profile_connector->policy_service(); | 215 PolicyService* policy_service = profile_connector->policy_service(); |
| 189 base::RunLoop run_loop; | 216 base::RunLoop run_loop; |
| 190 policy_service->RefreshPolicies(run_loop.QuitClosure()); | 217 policy_service->RefreshPolicies(run_loop.QuitClosure()); |
| 191 run_loop.Run(); | 218 run_loop.Run(); |
| 192 } | 219 } |
| 193 | 220 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 239 scoped_refptr<const extensions::Extension> extension2 = | 266 scoped_refptr<const extensions::Extension> extension2 = |
| 240 LoadExtension(kTestExtension2Path); | 267 LoadExtension(kTestExtension2Path); |
| 241 ASSERT_TRUE(extension2.get()); | 268 ASSERT_TRUE(extension2.get()); |
| 242 ASSERT_EQ(kTestExtension2, extension2->id()); | 269 ASSERT_EQ(kTestExtension2, extension2->id()); |
| 243 | 270 |
| 244 // This extension only sends the 'policy' signal once it receives the policy, | 271 // This extension only sends the 'policy' signal once it receives the policy, |
| 245 // and after verifying it has the expected value. Otherwise it sends 'fail'. | 272 // and after verifying it has the expected value. Otherwise it sends 'fail'. |
| 246 EXPECT_TRUE(result_listener.WaitUntilSatisfied()); | 273 EXPECT_TRUE(result_listener.WaitUntilSatisfied()); |
| 247 } | 274 } |
| 248 | 275 |
| 276 // Signing out on Chrome OS doesn't drop the policy caches. | |
|
bartfab (slow)
2013/12/02 18:08:46
What do you mean by signing out on Chrome OS? You
Joao da Silva
2013/12/03 09:02:31
Expanded this comment.
| |
| 277 #if !defined(OS_CHROMEOS) | |
| 278 IN_PROC_BROWSER_TEST_F(ComponentCloudPolicyTest, SignOutAndBackIn) { | |
| 279 // Read the initial policy. | |
| 280 ExtensionTestMessageListener initial_policy_listener(kTestPolicyJSON, true); | |
| 281 event_listener_->Reply("get-policy-Name"); | |
| 282 EXPECT_TRUE(initial_policy_listener.WaitUntilSatisfied()); | |
| 283 | |
| 284 // Verify that the policy cache exists. | |
| 285 std::string cache_key; | |
| 286 ASSERT_TRUE(Base64Encode("extension-policy", &cache_key)); | |
| 287 std::string cache_subkey; | |
| 288 ASSERT_TRUE(Base64Encode(kTestExtension, &cache_subkey)); | |
| 289 base::FilePath cache_path = browser()->profile()->GetPath() | |
| 290 .Append(FILE_PATH_LITERAL("Policy")) | |
| 291 .Append(FILE_PATH_LITERAL("Components")) | |
| 292 .AppendASCII(cache_key) | |
| 293 .AppendASCII(cache_subkey); | |
| 294 EXPECT_TRUE(base::PathExists(cache_path)); | |
| 295 | |
| 296 // Now sign-out. The policy cache should be removed, and the extension should | |
| 297 // get an empty policy update. | |
| 298 ExtensionTestMessageListener event_listener("event", true); | |
| 299 initial_policy_listener.Reply("idle"); | |
| 300 SignOut(); | |
| 301 EXPECT_TRUE(event_listener.WaitUntilSatisfied()); | |
| 302 | |
| 303 // The extension got an update event; verify that the policy was empty. | |
| 304 ExtensionTestMessageListener signout_policy_listener("{}", true); | |
| 305 event_listener.Reply("get-policy-Name"); | |
| 306 EXPECT_TRUE(signout_policy_listener.WaitUntilSatisfied()); | |
| 307 | |
| 308 // Verify that the cache is gone. | |
| 309 EXPECT_FALSE(base::PathExists(cache_path)); | |
| 310 | |
| 311 // Verify that the policy is fetched again if the user signs back in. | |
| 312 ExtensionTestMessageListener event_listener2("event", true); | |
| 313 SignInAndRegister(); | |
| 314 EXPECT_TRUE(event_listener2.WaitUntilSatisfied()); | |
| 315 | |
| 316 // The extension got updated policy; verify it. | |
| 317 ExtensionTestMessageListener signin_policy_listener(kTestPolicyJSON, true); | |
| 318 event_listener2.Reply("get-policy-Name"); | |
| 319 EXPECT_TRUE(signin_policy_listener.WaitUntilSatisfied()); | |
| 320 | |
| 321 // And the cache is back. | |
| 322 EXPECT_TRUE(base::PathExists(cache_path)); | |
| 323 } | |
| 324 #endif | |
| 325 | |
| 249 } // namespace policy | 326 } // namespace policy |
| OLD | NEW |