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

Side by Side Diff: chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos_unittest.cc

Issue 2504513002: Make FakeChromeUserManager subclass of ChromeUserManager (Closed)
Patch Set: Addressed comment 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h" 5 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h" 13 #include "base/run_loop.h"
14 #include "base/sequenced_task_runner.h" 14 #include "base/sequenced_task_runner.h"
15 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
17 #include "base/test/test_simple_task_runner.h" 17 #include "base/test/test_simple_task_runner.h"
18 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
18 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" 19 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
19 #include "chrome/browser/chromeos/policy/user_cloud_policy_token_forwarder.h" 20 #include "chrome/browser/chromeos/policy/user_cloud_policy_token_forwarder.h"
20 #include "chrome/browser/chromeos/profiles/profile_helper.h" 21 #include "chrome/browser/chromeos/profiles/profile_helper.h"
21 #include "chrome/browser/prefs/browser_prefs.h" 22 #include "chrome/browser/prefs/browser_prefs.h"
22 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" 23 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
23 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 24 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
24 #include "chrome/browser/signin/signin_manager_factory.h" 25 #include "chrome/browser/signin/signin_manager_factory.h"
25 #include "chrome/common/chrome_constants.h" 26 #include "chrome/common/chrome_constants.h"
26 #include "chrome/test/base/testing_browser_process.h" 27 #include "chrome/test/base/testing_browser_process.h"
27 #include "chrome/test/base/testing_profile.h" 28 #include "chrome/test/base/testing_profile.h"
28 #include "chrome/test/base/testing_profile_manager.h" 29 #include "chrome/test/base/testing_profile_manager.h"
29 #include "components/policy/core/common/cloud/cloud_external_data_manager.h" 30 #include "components/policy/core/common/cloud/cloud_external_data_manager.h"
30 #include "components/policy/core/common/cloud/mock_cloud_external_data_manager.h " 31 #include "components/policy/core/common/cloud/mock_cloud_external_data_manager.h "
31 #include "components/policy/core/common/cloud/mock_cloud_policy_store.h" 32 #include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
32 #include "components/policy/core/common/cloud/mock_device_management_service.h" 33 #include "components/policy/core/common/cloud/mock_device_management_service.h"
33 #include "components/policy/core/common/external_data_fetcher.h" 34 #include "components/policy/core/common/external_data_fetcher.h"
34 #include "components/policy/core/common/mock_configuration_policy_provider.h" 35 #include "components/policy/core/common/mock_configuration_policy_provider.h"
35 #include "components/policy/core/common/policy_types.h" 36 #include "components/policy/core/common/policy_types.h"
36 #include "components/policy/core/common/schema_registry.h" 37 #include "components/policy/core/common/schema_registry.h"
37 #include "components/policy/policy_constants.h" 38 #include "components/policy/policy_constants.h"
38 #include "components/policy/proto/device_management_backend.pb.h" 39 #include "components/policy/proto/device_management_backend.pb.h"
39 #include "components/prefs/pref_registry_simple.h" 40 #include "components/prefs/pref_registry_simple.h"
40 #include "components/prefs/testing_pref_service.h" 41 #include "components/prefs/testing_pref_service.h"
41 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" 42 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
42 #include "components/signin/core/browser/profile_oauth2_token_service.h" 43 #include "components/signin/core/browser/profile_oauth2_token_service.h"
43 #include "components/signin/core/browser/signin_manager.h" 44 #include "components/signin/core/browser/signin_manager.h"
44 #include "components/sync_preferences/pref_service_syncable.h" 45 #include "components/sync_preferences/pref_service_syncable.h"
45 #include "components/user_manager/fake_user_manager.h"
46 #include "content/public/test/test_browser_thread_bundle.h" 46 #include "content/public/test/test_browser_thread_bundle.h"
47 #include "google_apis/gaia/gaia_auth_consumer.h" 47 #include "google_apis/gaia/gaia_auth_consumer.h"
48 #include "google_apis/gaia/gaia_constants.h" 48 #include "google_apis/gaia/gaia_constants.h"
49 #include "google_apis/gaia/gaia_urls.h" 49 #include "google_apis/gaia/gaia_urls.h"
50 #include "net/http/http_response_headers.h" 50 #include "net/http/http_response_headers.h"
51 #include "net/url_request/test_url_fetcher_factory.h" 51 #include "net/url_request/test_url_fetcher_factory.h"
52 #include "net/url_request/url_fetcher_delegate.h" 52 #include "net/url_request/url_fetcher_delegate.h"
53 #include "net/url_request/url_request_context_getter.h" 53 #include "net/url_request/url_request_context_getter.h"
54 #include "net/url_request/url_request_status.h" 54 #include "net/url_request/url_request_status.h"
55 #include "testing/gmock/include/gmock/gmock.h" 55 #include "testing/gmock/include/gmock/gmock.h"
(...skipping 24 matching lines...) Expand all
80 "}"; 80 "}";
81 81
82 class UserCloudPolicyManagerChromeOSTest : public testing::Test { 82 class UserCloudPolicyManagerChromeOSTest : public testing::Test {
83 protected: 83 protected:
84 UserCloudPolicyManagerChromeOSTest() 84 UserCloudPolicyManagerChromeOSTest()
85 : store_(NULL), 85 : store_(NULL),
86 external_data_manager_(NULL), 86 external_data_manager_(NULL),
87 task_runner_(new base::TestSimpleTaskRunner()), 87 task_runner_(new base::TestSimpleTaskRunner()),
88 profile_(NULL), 88 profile_(NULL),
89 signin_profile_(NULL), 89 signin_profile_(NULL),
90 user_manager_(new user_manager::FakeUserManager()), 90 user_manager_(new chromeos::FakeChromeUserManager()),
91 user_manager_enabler_(user_manager_) {} 91 user_manager_enabler_(user_manager_) {}
92 92
93 void SetUp() override { 93 void SetUp() override {
94 // The initialization path that blocks on the initial policy fetch requires 94 // The initialization path that blocks on the initial policy fetch requires
95 // a signin Profile to use its URLRequestContext. 95 // a signin Profile to use its URLRequestContext.
96 profile_manager_.reset( 96 profile_manager_.reset(
97 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); 97 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
98 ASSERT_TRUE(profile_manager_->SetUp()); 98 ASSERT_TRUE(profile_manager_->SetUp());
99 TestingProfile::TestingFactories factories; 99 TestingProfile::TestingFactories factories;
100 factories.push_back( 100 factories.push_back(
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 338 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
339 SchemaRegistry schema_registry_; 339 SchemaRegistry schema_registry_;
340 std::unique_ptr<UserCloudPolicyManagerChromeOS> manager_; 340 std::unique_ptr<UserCloudPolicyManagerChromeOS> manager_;
341 std::unique_ptr<UserCloudPolicyTokenForwarder> token_forwarder_; 341 std::unique_ptr<UserCloudPolicyTokenForwarder> token_forwarder_;
342 342
343 // Required by ProfileHelper to get the signin Profile context. 343 // Required by ProfileHelper to get the signin Profile context.
344 std::unique_ptr<TestingProfileManager> profile_manager_; 344 std::unique_ptr<TestingProfileManager> profile_manager_;
345 TestingProfile* profile_; 345 TestingProfile* profile_;
346 TestingProfile* signin_profile_; 346 TestingProfile* signin_profile_;
347 347
348 user_manager::FakeUserManager* user_manager_; 348 chromeos::FakeChromeUserManager* user_manager_;
349 chromeos::ScopedUserManagerEnabler user_manager_enabler_; 349 chromeos::ScopedUserManagerEnabler user_manager_enabler_;
350 350
351 private: 351 private:
352 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerChromeOSTest); 352 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerChromeOSTest);
353 }; 353 };
354 354
355 // Test disabled. See crbug.com/534733. 355 TEST_F(UserCloudPolicyManagerChromeOSTest, BlockingFirstFetch) {
356 TEST_F(UserCloudPolicyManagerChromeOSTest, DISABLED_BlockingFirstFetch) {
357 // Tests the initialization of a manager whose Profile is waiting for the 356 // Tests the initialization of a manager whose Profile is waiting for the
358 // initial fetch, when the policy cache is empty. 357 // initial fetch, when the policy cache is empty.
359 ASSERT_NO_FATAL_FAILURE(CreateManager(true, 1000)); 358 ASSERT_NO_FATAL_FAILURE(CreateManager(true, 1000));
360 359
361 // Initialize the CloudPolicyService without any stored data. 360 // Initialize the CloudPolicyService without any stored data.
362 EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete()); 361 EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete());
363 store_->NotifyStoreLoaded(); 362 store_->NotifyStoreLoaded();
364 EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete()); 363 EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete());
365 EXPECT_FALSE(manager_->core()->client()->is_registered()); 364 EXPECT_FALSE(manager_->core()->client()->is_registered());
366 365
(...skipping 15 matching lines...) Expand all
382 // initial fetch, when a previously cached policy and DMToken already exist. 381 // initial fetch, when a previously cached policy and DMToken already exist.
383 ASSERT_NO_FATAL_FAILURE(CreateManager(true, 1000)); 382 ASSERT_NO_FATAL_FAILURE(CreateManager(true, 1000));
384 383
385 // Set the initially cached data and initialize the CloudPolicyService. 384 // Set the initially cached data and initialize the CloudPolicyService.
386 // The initial policy fetch is issued using the cached DMToken. 385 // The initial policy fetch is issued using the cached DMToken.
387 store_->policy_.reset(new em::PolicyData(policy_data_)); 386 store_->policy_.reset(new em::PolicyData(policy_data_));
388 FetchPolicy(base::Bind(&MockCloudPolicyStore::NotifyStoreLoaded, 387 FetchPolicy(base::Bind(&MockCloudPolicyStore::NotifyStoreLoaded,
389 base::Unretained(store_))); 388 base::Unretained(store_)));
390 } 389 }
391 390
392 // Test disabled. See crbug.com/534733. 391 TEST_F(UserCloudPolicyManagerChromeOSTest, BlockingFetchStoreError) {
393 TEST_F(UserCloudPolicyManagerChromeOSTest, DISABLED_BlockingFetchStoreError) {
394 // Tests the initialization of a manager whose Profile is waiting for the 392 // Tests the initialization of a manager whose Profile is waiting for the
395 // initial fetch, when the initial store load fails. 393 // initial fetch, when the initial store load fails.
396 ASSERT_NO_FATAL_FAILURE(CreateManager(true, 1000)); 394 ASSERT_NO_FATAL_FAILURE(CreateManager(true, 1000));
397 395
398 // Initialize the CloudPolicyService without any stored data. 396 // Initialize the CloudPolicyService without any stored data.
399 EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete()); 397 EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete());
400 store_->NotifyStoreError(); 398 store_->NotifyStoreError();
401 EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete()); 399 EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete());
402 EXPECT_FALSE(manager_->core()->client()->is_registered()); 400 EXPECT_FALSE(manager_->core()->client()->is_registered());
403 401
404 // This starts the OAuth2 policy token fetcher using the signin Profile. 402 // This starts the OAuth2 policy token fetcher using the signin Profile.
405 // The manager will then issue the registration request. 403 // The manager will then issue the registration request.
406 MockDeviceManagementJob* register_request = IssueOAuthToken(false); 404 MockDeviceManagementJob* register_request = IssueOAuthToken(false);
407 ASSERT_TRUE(register_request); 405 ASSERT_TRUE(register_request);
408 406
409 // Reply with a valid registration response. This triggers the initial policy 407 // Reply with a valid registration response. This triggers the initial policy
410 // fetch. 408 // fetch.
411 FetchPolicy(base::Bind(&MockDeviceManagementJob::SendResponse, 409 FetchPolicy(base::Bind(&MockDeviceManagementJob::SendResponse,
412 base::Unretained(register_request), 410 base::Unretained(register_request),
413 DM_STATUS_SUCCESS, register_blob_)); 411 DM_STATUS_SUCCESS, register_blob_));
414 } 412 }
415 413
416 // Test disabled. See crbug.com/534733. 414 TEST_F(UserCloudPolicyManagerChromeOSTest, BlockingFetchOAuthError) {
417 TEST_F(UserCloudPolicyManagerChromeOSTest, DISABLED_BlockingFetchOAuthError) {
418 // Tests the initialization of a manager whose Profile is waiting for the 415 // Tests the initialization of a manager whose Profile is waiting for the
419 // initial fetch, when the OAuth2 token fetch fails. 416 // initial fetch, when the OAuth2 token fetch fails.
420 ASSERT_NO_FATAL_FAILURE(CreateManager(true, 1000)); 417 ASSERT_NO_FATAL_FAILURE(CreateManager(true, 1000));
421 418
422 // Initialize the CloudPolicyService without any stored data. 419 // Initialize the CloudPolicyService without any stored data.
423 EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete()); 420 EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete());
424 store_->NotifyStoreLoaded(); 421 store_->NotifyStoreLoaded();
425 EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete()); 422 EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete());
426 EXPECT_FALSE(manager_->core()->client()->is_registered()); 423 EXPECT_FALSE(manager_->core()->client()->is_registered());
427 424
428 // This starts the OAuth2 policy token fetcher using the signin Profile. 425 // This starts the OAuth2 policy token fetcher using the signin Profile.
429 // The manager will initialize with no policy after the token fetcher fails. 426 // The manager will initialize with no policy after the token fetcher fails.
430 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); 427 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get()));
431 428
432 // The PolicyOAuth2TokenFetcher posts delayed retries on some errors. This 429 // The PolicyOAuth2TokenFetcher posts delayed retries on some errors. This
433 // data will make it fail immediately. 430 // data will make it fail immediately.
434 net::TestURLFetcher* fetcher = PrepareOAuthFetcher( 431 net::TestURLFetcher* fetcher = PrepareOAuthFetcher(
435 GaiaUrls::GetInstance()->client_login_to_oauth2_url()); 432 GaiaUrls::GetInstance()->client_login_to_oauth2_url());
436 ASSERT_TRUE(fetcher); 433 ASSERT_TRUE(fetcher);
437 fetcher->set_response_code(400); 434 fetcher->set_response_code(400);
438 fetcher->SetResponseString("Error=BadAuthentication"); 435 fetcher->SetResponseString("Error=BadAuthentication");
439 EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); 436 EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
440 fetcher->delegate()->OnURLFetchComplete(fetcher); 437 fetcher->delegate()->OnURLFetchComplete(fetcher);
441 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); 438 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
442 EXPECT_TRUE(PolicyBundle().Equals(manager_->policies())); 439 EXPECT_TRUE(PolicyBundle().Equals(manager_->policies()));
443 Mock::VerifyAndClearExpectations(&observer_); 440 Mock::VerifyAndClearExpectations(&observer_);
444 } 441 }
445 442
446 // Test disabled. See crbug.com/534733. 443 TEST_F(UserCloudPolicyManagerChromeOSTest, BlockingFetchRegisterError) {
447 TEST_F(UserCloudPolicyManagerChromeOSTest,
448 DISABLED_BlockingFetchRegisterError) {
449 // Tests the initialization of a manager whose Profile is waiting for the 444 // Tests the initialization of a manager whose Profile is waiting for the
450 // initial fetch, when the device management registration fails. 445 // initial fetch, when the device management registration fails.
451 ASSERT_NO_FATAL_FAILURE(CreateManager(true, 1000)); 446 ASSERT_NO_FATAL_FAILURE(CreateManager(true, 1000));
452 447
453 // Initialize the CloudPolicyService without any stored data. 448 // Initialize the CloudPolicyService without any stored data.
454 EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete()); 449 EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete());
455 store_->NotifyStoreError(); 450 store_->NotifyStoreError();
456 EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete()); 451 EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete());
457 EXPECT_FALSE(manager_->core()->client()->is_registered()); 452 EXPECT_FALSE(manager_->core()->client()->is_registered());
458 453
459 // This starts the OAuth2 policy token fetcher using the signin Profile. 454 // This starts the OAuth2 policy token fetcher using the signin Profile.
460 // The manager will then issue the registration request. 455 // The manager will then issue the registration request.
461 MockDeviceManagementJob* register_request = IssueOAuthToken(false); 456 MockDeviceManagementJob* register_request = IssueOAuthToken(false);
462 ASSERT_TRUE(register_request); 457 ASSERT_TRUE(register_request);
463 458
464 // Now make it fail. 459 // Now make it fail.
465 EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); 460 EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
466 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); 461 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get()));
467 register_request->SendResponse(DM_STATUS_TEMPORARY_UNAVAILABLE, 462 register_request->SendResponse(DM_STATUS_TEMPORARY_UNAVAILABLE,
468 em::DeviceManagementResponse()); 463 em::DeviceManagementResponse());
469 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); 464 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
470 EXPECT_TRUE(PolicyBundle().Equals(manager_->policies())); 465 EXPECT_TRUE(PolicyBundle().Equals(manager_->policies()));
471 Mock::VerifyAndClearExpectations(&observer_); 466 Mock::VerifyAndClearExpectations(&observer_);
472 } 467 }
473 468
474 // Test disabled. See crbug.com/534733. 469 TEST_F(UserCloudPolicyManagerChromeOSTest, BlockingFetchPolicyFetchError) {
475 TEST_F(UserCloudPolicyManagerChromeOSTest,
476 DISABLED_BlockingFetchPolicyFetchError) {
477 // Tests the initialization of a manager whose Profile is waiting for the 470 // Tests the initialization of a manager whose Profile is waiting for the
478 // initial fetch, when the policy fetch request fails. 471 // initial fetch, when the policy fetch request fails.
479 ASSERT_NO_FATAL_FAILURE(CreateManager(true, 1000)); 472 ASSERT_NO_FATAL_FAILURE(CreateManager(true, 1000));
480 473
481 // Initialize the CloudPolicyService without any stored data. 474 // Initialize the CloudPolicyService without any stored data.
482 EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete()); 475 EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete());
483 store_->NotifyStoreLoaded(); 476 store_->NotifyStoreLoaded();
484 EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete()); 477 EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete());
485 EXPECT_FALSE(manager_->core()->client()->is_registered()); 478 EXPECT_FALSE(manager_->core()->client()->is_registered());
486 479
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
598 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); 591 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
599 EXPECT_TRUE(manager_->core()->client()->is_registered()); 592 EXPECT_TRUE(manager_->core()->client()->is_registered());
600 593
601 // The refresh scheduler takes care of the initial fetch for unmanaged users. 594 // The refresh scheduler takes care of the initial fetch for unmanaged users.
602 // Running the task runner issues the initial fetch. 595 // Running the task runner issues the initial fetch.
603 FetchPolicy( 596 FetchPolicy(
604 base::Bind(&base::TestSimpleTaskRunner::RunUntilIdle, task_runner_)); 597 base::Bind(&base::TestSimpleTaskRunner::RunUntilIdle, task_runner_));
605 } 598 }
606 599
607 } // namespace policy 600 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698