| OLD | NEW |
| 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 "base/command_line.h" |
| 5 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
| 6 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 7 #include "chrome/browser/policy/browser_policy_connector.h" | 8 #include "chrome/browser/policy/browser_policy_connector.h" |
| 9 #include "chrome/browser/policy/mock_device_management_service.h" |
| 8 #include "chrome/browser/policy/mock_user_cloud_policy_store.h" | 10 #include "chrome/browser/policy/mock_user_cloud_policy_store.h" |
| 9 #include "chrome/browser/policy/user_cloud_policy_manager.h" | 11 #include "chrome/browser/policy/user_cloud_policy_manager.h" |
| 10 #include "chrome/browser/policy/user_policy_signin_service.h" | 12 #include "chrome/browser/policy/user_policy_signin_service.h" |
| 11 #include "chrome/browser/policy/user_policy_signin_service_factory.h" | 13 #include "chrome/browser/policy/user_policy_signin_service_factory.h" |
| 12 #include "chrome/browser/prefs/browser_prefs.h" | 14 #include "chrome/browser/prefs/browser_prefs.h" |
| 13 #include "chrome/browser/prefs/pref_service.h" | 15 #include "chrome/browser/prefs/pref_service.h" |
| 14 #include "chrome/browser/signin/signin_manager.h" | 16 #include "chrome/browser/signin/signin_manager.h" |
| 15 #include "chrome/browser/signin/signin_manager_factory.h" | 17 #include "chrome/browser/signin/signin_manager_factory.h" |
| 16 #include "chrome/browser/signin/signin_manager_fake.h" | 18 #include "chrome/browser/signin/signin_manager_fake.h" |
| 17 #include "chrome/browser/signin/token_service.h" | 19 #include "chrome/browser/signin/token_service.h" |
| 18 #include "chrome/browser/signin/token_service_factory.h" | 20 #include "chrome/browser/signin/token_service_factory.h" |
| 19 #include "chrome/common/chrome_notification_types.h" | 21 #include "chrome/common/chrome_notification_types.h" |
| 22 #include "chrome/common/chrome_switches.h" |
| 20 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
| 21 #include "chrome/test/base/testing_browser_process.h" | 24 #include "chrome/test/base/testing_browser_process.h" |
| 22 #include "chrome/test/base/testing_pref_service.h" | 25 #include "chrome/test/base/testing_pref_service.h" |
| 23 #include "chrome/test/base/testing_profile.h" | 26 #include "chrome/test/base/testing_profile.h" |
| 24 #include "content/public/browser/notification_details.h" | 27 #include "content/public/browser/notification_details.h" |
| 25 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
| 26 #include "content/public/browser/notification_source.h" | 29 #include "content/public/browser/notification_source.h" |
| 27 #include "content/public/test/test_browser_thread.h" | 30 #include "content/public/test/test_browser_thread.h" |
| 28 #include "google_apis/gaia/gaia_constants.h" | 31 #include "google_apis/gaia/gaia_constants.h" |
| 29 #include "net/url_request/test_url_fetcher_factory.h" | 32 #include "net/url_request/test_url_fetcher_factory.h" |
| 33 #include "net/url_request/url_request_status.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 |
| 37 namespace em = enterprise_management; |
| 38 |
| 33 using testing::AnyNumber; | 39 using testing::AnyNumber; |
| 34 using testing::Mock; | 40 using testing::Mock; |
| 41 using testing::_; |
| 35 | 42 |
| 36 namespace policy { | 43 namespace policy { |
| 37 | 44 |
| 38 namespace { | 45 namespace { |
| 39 | 46 |
| 40 class UserPolicySigninServiceTest : public testing::Test { | 47 class UserPolicySigninServiceTest : public testing::Test { |
| 41 public: | 48 public: |
| 42 UserPolicySigninServiceTest() | 49 UserPolicySigninServiceTest() |
| 43 : loop_(MessageLoop::TYPE_UI), | 50 : loop_(MessageLoop::TYPE_UI), |
| 44 ui_thread_(content::BrowserThread::UI, &loop_), | 51 ui_thread_(content::BrowserThread::UI, &loop_), |
| 45 file_thread_(content::BrowserThread::FILE, &loop_), | 52 file_thread_(content::BrowserThread::FILE, &loop_), |
| 46 io_thread_(content::BrowserThread::IO, &loop_) {} | 53 io_thread_(content::BrowserThread::IO, &loop_) {} |
| 47 | 54 |
| 55 MOCK_METHOD1(OnPolicyRefresh, void(bool)); |
| 56 |
| 48 virtual void SetUp() OVERRIDE { | 57 virtual void SetUp() OVERRIDE { |
| 58 device_management_service_ = new MockDeviceManagementService(); |
| 59 g_browser_process->browser_policy_connector()-> |
| 60 SetDeviceManagementServiceForTesting( |
| 61 scoped_ptr<DeviceManagementService>(device_management_service_)); |
| 62 |
| 49 g_browser_process->browser_policy_connector()->Init(); | 63 g_browser_process->browser_policy_connector()->Init(); |
| 50 | 64 |
| 51 local_state_.reset(new TestingPrefService); | 65 local_state_.reset(new TestingPrefService); |
| 52 chrome::RegisterLocalState(local_state_.get()); | 66 chrome::RegisterLocalState(local_state_.get()); |
| 53 static_cast<TestingBrowserProcess*>(g_browser_process)->SetLocalState( | 67 static_cast<TestingBrowserProcess*>(g_browser_process)->SetLocalState( |
| 54 local_state_.get()); | 68 local_state_.get()); |
| 55 | 69 |
| 56 // Create a testing profile and bring up a UserCloudPolicyManager with a | 70 // Create a testing profile with cloud-policy-on-signin enabled, and bring |
| 57 // MockUserCloudPolicyStore. | 71 // up a UserCloudPolicyManager with a MockUserCloudPolicyStore. |
| 58 profile_.reset(new TestingProfile()); | 72 scoped_ptr<TestingPrefService> prefs(new TestingPrefService()); |
| 73 Profile::RegisterUserPrefs(prefs.get()); |
| 74 chrome::RegisterUserPrefs(prefs.get()); |
| 75 prefs->SetUserPref(prefs::kLoadCloudPolicyOnSignin, |
| 76 Value::CreateBooleanValue(true)); |
| 77 TestingProfile::Builder builder; |
| 78 builder.SetPrefService(scoped_ptr<PrefService>(prefs.Pass())); |
| 79 profile_ = builder.Build().Pass(); |
| 59 profile_->CreateRequestContext(); | 80 profile_->CreateRequestContext(); |
| 60 profile_->GetPrefs()->SetBoolean(prefs::kLoadCloudPolicyOnSignin, true); | |
| 61 | 81 |
| 62 mock_store_ = new MockUserCloudPolicyStore(); | 82 mock_store_ = new MockUserCloudPolicyStore(); |
| 63 EXPECT_CALL(*mock_store_, Load()).Times(AnyNumber()); | 83 EXPECT_CALL(*mock_store_, Load()).Times(AnyNumber()); |
| 64 manager_.reset(new UserCloudPolicyManager( | 84 manager_.reset(new UserCloudPolicyManager( |
| 65 profile_.get(), scoped_ptr<UserCloudPolicyStore>(mock_store_))); | 85 profile_.get(), scoped_ptr<UserCloudPolicyStore>(mock_store_))); |
| 66 SigninManagerFactory::GetInstance()->SetTestingFactory( | 86 SigninManagerFactory::GetInstance()->SetTestingFactory( |
| 67 profile_.get(), FakeSigninManager::Build); | 87 profile_.get(), FakeSigninManager::Build); |
| 68 | 88 |
| 69 // Make sure the UserPolicySigninService is created. | 89 // Make sure the UserPolicySigninService is created. |
| 70 UserPolicySigninServiceFactory::GetForProfile(profile_.get()); | 90 UserPolicySigninServiceFactory::GetForProfile(profile_.get()); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 95 | 115 |
| 96 // BrowserPolicyConnector and UrlFetcherFactory want to initialize and free | 116 // BrowserPolicyConnector and UrlFetcherFactory want to initialize and free |
| 97 // various components asynchronously via tasks, so create fake threads here. | 117 // various components asynchronously via tasks, so create fake threads here. |
| 98 MessageLoop loop_; | 118 MessageLoop loop_; |
| 99 content::TestBrowserThread ui_thread_; | 119 content::TestBrowserThread ui_thread_; |
| 100 content::TestBrowserThread file_thread_; | 120 content::TestBrowserThread file_thread_; |
| 101 content::TestBrowserThread io_thread_; | 121 content::TestBrowserThread io_thread_; |
| 102 | 122 |
| 103 net::TestURLFetcherFactory url_factory_; | 123 net::TestURLFetcherFactory url_factory_; |
| 104 | 124 |
| 125 // Weak ptr to the MockDeviceManagementService (object is owned by the |
| 126 // BrowserPolicyConnector). |
| 127 MockDeviceManagementService* device_management_service_; |
| 128 |
| 105 scoped_ptr<TestingPrefService> local_state_; | 129 scoped_ptr<TestingPrefService> local_state_; |
| 106 }; | 130 }; |
| 107 | 131 |
| 108 TEST_F(UserPolicySigninServiceTest, InitWhileSignedOut) { | 132 TEST_F(UserPolicySigninServiceTest, InitWhileSignedOut) { |
| 109 EXPECT_CALL(*mock_store_, Clear()); | 133 EXPECT_CALL(*mock_store_, Clear()); |
| 110 // Make sure user is not signed in. | 134 // Make sure user is not signed in. |
| 111 ASSERT_TRUE(SigninManagerFactory::GetForProfile(profile_.get())-> | 135 ASSERT_TRUE(SigninManagerFactory::GetForProfile(profile_.get())-> |
| 112 GetAuthenticatedUsername().empty()); | 136 GetAuthenticatedUsername().empty()); |
| 113 | 137 |
| 114 // Let the SigninService know that the profile has been created. | 138 // Let the SigninService know that the profile has been created. |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 // UserCloudPolicyManager should be initialized. | 293 // UserCloudPolicyManager should be initialized. |
| 270 ASSERT_TRUE(manager_->core()->service()); | 294 ASSERT_TRUE(manager_->core()->service()); |
| 271 | 295 |
| 272 // Now sign out. | 296 // Now sign out. |
| 273 SigninManagerFactory::GetForProfile(profile_.get())->SignOut(); | 297 SigninManagerFactory::GetForProfile(profile_.get())->SignOut(); |
| 274 | 298 |
| 275 // UserCloudPolicyManager should be shut down. | 299 // UserCloudPolicyManager should be shut down. |
| 276 ASSERT_FALSE(manager_->core()->service()); | 300 ASSERT_FALSE(manager_->core()->service()); |
| 277 } | 301 } |
| 278 | 302 |
| 303 TEST_F(UserPolicySigninServiceTest, FetchPolicyFailure) { |
| 304 // Set the user as signed in. |
| 305 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( |
| 306 "testuser@test.com"); |
| 307 |
| 308 UserPolicySigninService* signin_service = |
| 309 UserPolicySigninServiceFactory::GetForProfile(profile_.get()); |
| 310 EXPECT_CALL(*this, OnPolicyRefresh(_)).Times(0); |
| 311 signin_service->FetchPolicyForSignedInUser( |
| 312 "mock_token", |
| 313 base::Bind(&UserPolicySigninServiceTest::OnPolicyRefresh, |
| 314 base::Unretained(this))); |
| 315 Mock::VerifyAndClearExpectations(this); |
| 316 |
| 317 // UserCloudPolicyManager should be initialized. |
| 318 ASSERT_TRUE(manager_->core()->service()); |
| 319 ASSERT_TRUE(IsRequestActive()); |
| 320 |
| 321 // Cause the fetch to fail - callback should be invoked. |
| 322 EXPECT_CALL(*this, OnPolicyRefresh(false)).Times(1); |
| 323 net::TestURLFetcher* fetcher = url_factory_.GetFetcherByID(0); |
| 324 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, -1)); |
| 325 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 326 } |
| 327 |
| 328 TEST_F(UserPolicySigninServiceTest, FetchPolicySuccess) { |
| 329 // Set the user as signed in. |
| 330 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( |
| 331 "testuser@test.com"); |
| 332 |
| 333 UserPolicySigninService* signin_service = |
| 334 UserPolicySigninServiceFactory::GetForProfile(profile_.get()); |
| 335 EXPECT_CALL(*this, OnPolicyRefresh(true)).Times(0); |
| 336 signin_service->FetchPolicyForSignedInUser( |
| 337 "mock_token", |
| 338 base::Bind(&UserPolicySigninServiceTest::OnPolicyRefresh, |
| 339 base::Unretained(this))); |
| 340 |
| 341 // UserCloudPolicyManager should be initialized. |
| 342 ASSERT_TRUE(manager_->core()->service()); |
| 343 ASSERT_TRUE(IsRequestActive()); |
| 344 |
| 345 // Mimic successful client registration - this should kick off a policy |
| 346 // fetch. |
| 347 MockDeviceManagementJob* fetch_request = NULL; |
| 348 EXPECT_CALL(*device_management_service_, |
| 349 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) |
| 350 .WillOnce(device_management_service_->CreateAsyncJob(&fetch_request)); |
| 351 EXPECT_CALL(*device_management_service_, StartJob(_, _, _, _, _, _, _)) |
| 352 .Times(1); |
| 353 manager_->core()->client()->SetupRegistration("dm_token", "client_id"); |
| 354 ASSERT_TRUE(fetch_request); |
| 355 Mock::VerifyAndClearExpectations(this); |
| 356 |
| 357 // Make the policy fetch succeed - this should result in a write to the |
| 358 // store and ultimately result in a call to OnPolicyRefresh(). |
| 359 EXPECT_CALL(*mock_store_, Store(_)); |
| 360 EXPECT_CALL(*this, OnPolicyRefresh(true)).Times(1); |
| 361 // Create a fake policy blob to deliver to the client. |
| 362 em::DeviceManagementResponse policy_blob; |
| 363 em::PolicyData policy_data; |
| 364 em::PolicyFetchResponse* policy_response = |
| 365 policy_blob.mutable_policy_response()->add_response(); |
| 366 ASSERT_TRUE(policy_data.SerializeToString( |
| 367 policy_response->mutable_policy_data())); |
| 368 fetch_request->SendResponse(DM_STATUS_SUCCESS, policy_blob); |
| 369 |
| 370 // Complete the store which should cause the policy fetch callback to be |
| 371 // invoked. |
| 372 mock_store_->NotifyStoreLoaded(); |
| 373 } |
| 374 |
| 279 } // namespace | 375 } // namespace |
| 280 | 376 |
| 281 } // namespace policy | 377 } // namespace policy |
| OLD | NEW |