Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(288)

Side by Side Diff: chrome/browser/policy/cloud/component_cloud_policy_browsertest.cc

Issue 2493603002: Implement component cloud policy signature validation (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/base64url.h" 7 #include "base/base64url.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/path_service.h" 12 #include "base/path_service.h"
13 #include "base/run_loop.h" 13 #include "base/run_loop.h"
14 #include "build/build_config.h" 14 #include "build/build_config.h"
15 #include "chrome/browser/browser_process.h" 15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/extensions/extension_browsertest.h" 16 #include "chrome/browser/extensions/extension_browsertest.h"
17 #include "chrome/browser/policy/profile_policy_connector.h" 17 #include "chrome/browser/policy/profile_policy_connector.h"
18 #include "chrome/browser/policy/profile_policy_connector_factory.h" 18 #include "chrome/browser/policy/profile_policy_connector_factory.h"
19 #include "chrome/browser/policy/test/local_policy_test_server.h" 19 #include "chrome/browser/policy/test/local_policy_test_server.h"
20 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/ui/browser.h" 21 #include "chrome/browser/ui/browser.h"
22 #include "chrome/common/chrome_paths.h" 22 #include "chrome/common/chrome_paths.h"
23 #include "components/policy/core/browser/browser_policy_connector.h" 23 #include "components/policy/core/browser/browser_policy_connector.h"
24 #include "components/policy/core/common/cloud/cloud_policy_client.h"
24 #include "components/policy/core/common/cloud/cloud_policy_constants.h" 25 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
25 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h" 26 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h"
26 #include "components/policy/core/common/cloud/policy_builder.h" 27 #include "components/policy/core/common/cloud/policy_builder.h"
27 #include "components/policy/core/common/policy_service.h" 28 #include "components/policy/core/common/policy_service.h"
28 #include "components/policy/core/common/policy_switches.h" 29 #include "components/policy/core/common/policy_switches.h"
29 #include "components/policy/core/common/policy_test_utils.h" 30 #include "components/policy/core/common/policy_test_utils.h"
30 #include "components/policy/proto/chrome_extension_policy.pb.h" 31 #include "components/policy/proto/chrome_extension_policy.pb.h"
31 #include "components/policy/proto/cloud_policy.pb.h" 32 #include "components/policy/proto/cloud_policy.pb.h"
33 #include "components/policy/proto/device_management_backend.pb.h"
32 #include "extensions/common/extension.h" 34 #include "extensions/common/extension.h"
33 #include "extensions/test/extension_test_message_listener.h" 35 #include "extensions/test/extension_test_message_listener.h"
34 #include "net/url_request/url_request_context_getter.h" 36 #include "net/url_request/url_request_context_getter.h"
35 #include "testing/gmock/include/gmock/gmock.h" 37 #include "testing/gmock/include/gmock/gmock.h"
36 #include "testing/gtest/include/gtest/gtest.h" 38 #include "testing/gtest/include/gtest/gtest.h"
37 39
38 #if defined(OS_CHROMEOS) 40 #if defined(OS_CHROMEOS)
39 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h" 41 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
40 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chrom eos.h" 42 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chrom eos.h"
41 #include "chromeos/chromeos_switches.h" 43 #include "chromeos/chromeos_switches.h"
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 SigninManager* signin_manager = 178 SigninManager* signin_manager =
177 SigninManagerFactory::GetForProfile(browser()->profile()); 179 SigninManagerFactory::GetForProfile(browser()->profile());
178 ASSERT_TRUE(signin_manager); 180 ASSERT_TRUE(signin_manager);
179 signin_manager->SetAuthenticatedAccountInfo("12345", 181 signin_manager->SetAuthenticatedAccountInfo("12345",
180 PolicyBuilder::kFakeUsername); 182 PolicyBuilder::kFakeUsername);
181 183
182 UserCloudPolicyManager* policy_manager = 184 UserCloudPolicyManager* policy_manager =
183 UserCloudPolicyManagerFactory::GetForBrowserContext( 185 UserCloudPolicyManagerFactory::GetForBrowserContext(
184 browser()->profile()); 186 browser()->profile());
185 ASSERT_TRUE(policy_manager); 187 ASSERT_TRUE(policy_manager);
188 policy_manager->SetSigninUsername(PolicyBuilder::kFakeUsername);
186 policy_manager->Connect(g_browser_process->local_state(), 189 policy_manager->Connect(g_browser_process->local_state(),
187 g_browser_process->system_request_context(), 190 g_browser_process->system_request_context(),
188 UserCloudPolicyManager::CreateCloudPolicyClient( 191 UserCloudPolicyManager::CreateCloudPolicyClient(
189 connector->device_management_service(), 192 connector->device_management_service(),
190 g_browser_process->system_request_context())); 193 g_browser_process->system_request_context()));
191 #endif // defined(OS_CHROMEOS) 194 #endif // defined(OS_CHROMEOS)
192 195
193 // Register the cloud policy client. 196 // Register the cloud policy client.
194 ASSERT_TRUE(policy_manager->core()->client()); 197 client_ = policy_manager->core()->client();
198 ASSERT_TRUE(client_);
195 base::RunLoop run_loop; 199 base::RunLoop run_loop;
196 MockCloudPolicyClientObserver observer; 200 MockCloudPolicyClientObserver observer;
197 EXPECT_CALL(observer, OnRegistrationStateChanged(_)) 201 EXPECT_CALL(observer, OnRegistrationStateChanged(_))
198 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); 202 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
199 policy_manager->core()->client()->AddObserver(&observer); 203 client_->AddObserver(&observer);
200 policy_manager->core()->client()->SetupRegistration(kDMToken, kDeviceID); 204 client_->SetupRegistration(kDMToken, kDeviceID);
201 run_loop.Run(); 205 run_loop.Run();
202 Mock::VerifyAndClearExpectations(&observer); 206 Mock::VerifyAndClearExpectations(&observer);
203 policy_manager->core()->client()->RemoveObserver(&observer); 207 client_->RemoveObserver(&observer);
204 } 208 }
205 209
206 #if !defined(OS_CHROMEOS) 210 #if !defined(OS_CHROMEOS)
207 void SignOut() { 211 void SignOut() {
208 SigninManager* signin_manager = 212 SigninManager* signin_manager =
209 SigninManagerFactory::GetForProfile(browser()->profile()); 213 SigninManagerFactory::GetForProfile(browser()->profile());
210 ASSERT_TRUE(signin_manager); 214 ASSERT_TRUE(signin_manager);
211 signin_manager->SignOut(signin_metrics::SIGNOUT_TEST, 215 signin_manager->SignOut(signin_metrics::SIGNOUT_TEST,
212 signin_metrics::SignoutDelete::IGNORE_METRIC); 216 signin_metrics::SignoutDelete::IGNORE_METRIC);
213 } 217 }
214 #endif 218 #endif
215 219
216 void RefreshPolicies() { 220 void RefreshPolicies() {
217 ProfilePolicyConnector* profile_connector = 221 ProfilePolicyConnector* profile_connector =
218 ProfilePolicyConnectorFactory::GetForBrowserContext( 222 ProfilePolicyConnectorFactory::GetForBrowserContext(
219 browser()->profile()); 223 browser()->profile());
220 PolicyService* policy_service = profile_connector->policy_service(); 224 PolicyService* policy_service = profile_connector->policy_service();
221 base::RunLoop run_loop; 225 base::RunLoop run_loop;
222 policy_service->RefreshPolicies(run_loop.QuitClosure()); 226 policy_service->RefreshPolicies(run_loop.QuitClosure());
223 run_loop.Run(); 227 run_loop.Run();
224 } 228 }
225 229
230 int GetFetchedPolicyPublicKeyVersion(const std::string& extension_id) {
231 const em::PolicyFetchResponse* fetched_policy = client_->GetPolicyFor(
232 dm_protocol::kChromeExtensionPolicyType, extension_id);
233 if (!fetched_policy || !fetched_policy->has_policy_data())
234 return -1;
235 em::PolicyData policy_data;
236 if (!policy_data.ParseFromString(fetched_policy->policy_data()) ||
237 !policy_data.has_public_key_version())
238 return -1;
239 return policy_data.public_key_version();
240 }
241
226 LocalPolicyTestServer test_server_; 242 LocalPolicyTestServer test_server_;
227 scoped_refptr<const extensions::Extension> extension_; 243 scoped_refptr<const extensions::Extension> extension_;
228 std::unique_ptr<ExtensionTestMessageListener> event_listener_; 244 std::unique_ptr<ExtensionTestMessageListener> event_listener_;
245
246 private:
247 CloudPolicyClient* client_ = nullptr;
229 }; 248 };
230 249
231 IN_PROC_BROWSER_TEST_F(ComponentCloudPolicyTest, FetchExtensionPolicy) { 250 IN_PROC_BROWSER_TEST_F(ComponentCloudPolicyTest, FetchExtensionPolicy) {
232 // Read the initial policy. 251 // Read the initial policy.
233 ExtensionTestMessageListener policy_listener(kTestPolicyJSON, false); 252 ExtensionTestMessageListener policy_listener(kTestPolicyJSON, false);
234 event_listener_->Reply("get-policy-Name"); 253 event_listener_->Reply("get-policy-Name");
235 EXPECT_TRUE(policy_listener.WaitUntilSatisfied()); 254 EXPECT_TRUE(policy_listener.WaitUntilSatisfied());
236 } 255 }
237 256
238 IN_PROC_BROWSER_TEST_F(ComponentCloudPolicyTest, UpdateExtensionPolicy) { 257 IN_PROC_BROWSER_TEST_F(ComponentCloudPolicyTest, UpdateExtensionPolicy) {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
279 scoped_refptr<const extensions::Extension> extension2 = 298 scoped_refptr<const extensions::Extension> extension2 =
280 LoadExtension(kTestExtension2Path); 299 LoadExtension(kTestExtension2Path);
281 ASSERT_TRUE(extension2.get()); 300 ASSERT_TRUE(extension2.get());
282 ASSERT_EQ(kTestExtension2, extension2->id()); 301 ASSERT_EQ(kTestExtension2, extension2->id());
283 302
284 // This extension only sends the 'policy' signal once it receives the policy, 303 // This extension only sends the 'policy' signal once it receives the policy,
285 // and after verifying it has the expected value. Otherwise it sends 'fail'. 304 // and after verifying it has the expected value. Otherwise it sends 'fail'.
286 EXPECT_TRUE(result_listener.WaitUntilSatisfied()); 305 EXPECT_TRUE(result_listener.WaitUntilSatisfied());
287 } 306 }
288 307
308 IN_PROC_BROWSER_TEST_F(ComponentCloudPolicyTest, KeyRotation) {
309 // Read the initial policy.
310 ExtensionTestMessageListener policy_listener(kTestPolicyJSON, true);
311 event_listener_->Reply("get-policy-Name");
312 EXPECT_TRUE(policy_listener.WaitUntilSatisfied());
313 const int public_key_version =
314 GetFetchedPolicyPublicKeyVersion(kTestExtension);
315 EXPECT_NE(-1, public_key_version);
316
317 // Update the policy at the server and reload the policy, causing also the key
318 // rotation to be performed by the policy test server.
319 event_listener_.reset(new ExtensionTestMessageListener("event", true));
320 policy_listener.Reply("idle");
321 EXPECT_TRUE(test_server_.UpdatePolicyData(
322 dm_protocol::kChromeExtensionPolicyType, kTestExtension, kTestPolicy2));
323 RefreshPolicies();
324
325 // Check that the update event was received, and verify that the policy has
326 // the new value and that the key rotation happened.
327 EXPECT_TRUE(event_listener_->WaitUntilSatisfied());
328 const int new_public_key_version =
329 GetFetchedPolicyPublicKeyVersion(kTestExtension);
330 EXPECT_LT(public_key_version, new_public_key_version);
emaxx 2016/11/09 22:16:56 This new test is failing here due to the policy te
331
332 ExtensionTestMessageListener policy_listener1("{}", true);
333 event_listener_->Reply("get-policy-Name");
334 EXPECT_TRUE(policy_listener1.WaitUntilSatisfied());
335
336 ExtensionTestMessageListener policy_listener2(kTestPolicy2JSON, false);
337 policy_listener1.Reply("get-policy-Another");
338 EXPECT_TRUE(policy_listener2.WaitUntilSatisfied());
339 }
340
289 // Signing out on Chrome OS is a different process from signing out on the 341 // Signing out on Chrome OS is a different process from signing out on the
290 // Desktop platforms. On Chrome OS the session is ended, and the user goes back 342 // Desktop platforms. On Chrome OS the session is ended, and the user goes back
291 // to the sign-in screen; the Profile data is not affected. On the Desktop the 343 // to the sign-in screen; the Profile data is not affected. On the Desktop the
292 // session goes on though, and all the signed-in services are disconnected; 344 // session goes on though, and all the signed-in services are disconnected;
293 // in particular, the policy caches are dropped if the user signs out. 345 // in particular, the policy caches are dropped if the user signs out.
294 // This test verifies that when the user signs out then any existing component 346 // This test verifies that when the user signs out then any existing component
295 // policy caches are dropped, and that it's still possible to sign back in and 347 // policy caches are dropped, and that it's still possible to sign back in and
296 // get policy for components working again. 348 // get policy for components working again.
297 #if !defined(OS_CHROMEOS) 349 #if !defined(OS_CHROMEOS)
298 IN_PROC_BROWSER_TEST_F(ComponentCloudPolicyTest, SignOutAndBackIn) { 350 IN_PROC_BROWSER_TEST_F(ComponentCloudPolicyTest, SignOutAndBackIn) {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 ExtensionTestMessageListener signin_policy_listener(kTestPolicyJSON, false); 393 ExtensionTestMessageListener signin_policy_listener(kTestPolicyJSON, false);
342 event_listener2.Reply("get-policy-Name"); 394 event_listener2.Reply("get-policy-Name");
343 EXPECT_TRUE(signin_policy_listener.WaitUntilSatisfied()); 395 EXPECT_TRUE(signin_policy_listener.WaitUntilSatisfied());
344 396
345 // And the cache is back. 397 // And the cache is back.
346 EXPECT_TRUE(base::PathExists(cache_path)); 398 EXPECT_TRUE(base::PathExists(cache_path));
347 } 399 }
348 #endif 400 #endif
349 401
350 } // namespace policy 402 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698