user_cloud_policy_manager_chromeos_unittest.cc revision 58537e28ecd584eab876aee8be7156509866d23a
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 3// found in the LICENSE file. 4 5#include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h" 6 7#include "base/basictypes.h" 8#include "base/bind.h" 9#include "base/bind_helpers.h" 10#include "base/callback.h" 11#include "base/memory/scoped_ptr.h" 12#include "base/message_loop/message_loop.h" 13#include "base/prefs/pref_registry_simple.h" 14#include "base/prefs/testing_pref_service.h" 15#include "base/run_loop.h" 16#include "base/strings/string_util.h" 17#include "base/strings/utf_string_conversions.h" 18#include "base/time/time.h" 19#include "chrome/browser/chromeos/policy/user_cloud_policy_token_forwarder.h" 20#include "chrome/browser/chromeos/profiles/profile_helper.h" 21#include "chrome/browser/policy/cloud/cloud_policy_constants.h" 22#include "chrome/browser/policy/cloud/cloud_policy_service.h" 23#include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" 24#include "chrome/browser/policy/cloud/mock_device_management_service.h" 25#include "chrome/browser/policy/cloud/resource_cache.h" 26#include "chrome/browser/policy/external_data_fetcher.h" 27#include "chrome/browser/policy/mock_configuration_policy_provider.h" 28#include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" 29#include "chrome/browser/prefs/browser_prefs.h" 30#include "chrome/browser/prefs/pref_service_syncable.h" 31#include "chrome/browser/signin/profile_oauth2_token_service.h" 32#include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 33#include "chrome/browser/signin/token_service.h" 34#include "chrome/browser/signin/token_service_factory.h" 35#include "chrome/common/chrome_constants.h" 36#include "chrome/test/base/testing_browser_process.h" 37#include "chrome/test/base/testing_profile.h" 38#include "chrome/test/base/testing_profile_manager.h" 39#include "content/public/test/test_browser_thread_bundle.h" 40#include "google_apis/gaia/gaia_auth_consumer.h" 41#include "google_apis/gaia/gaia_urls.h" 42#include "net/url_request/test_url_fetcher_factory.h" 43#include "net/url_request/url_fetcher_delegate.h" 44#include "net/url_request/url_request_context_getter.h" 45#include "net/url_request/url_request_status.h" 46#include "policy/policy_constants.h" 47#include "testing/gmock/include/gmock/gmock.h" 48#include "testing/gtest/include/gtest/gtest.h" 49 50namespace em = enterprise_management; 51 52using testing::AnyNumber; 53using testing::AtLeast; 54using testing::Mock; 55using testing::_; 56 57namespace policy { 58 59namespace { 60 61const char kOAuthTokenCookie[] = "oauth_token=1234"; 62 63const char kOAuth2TokenPairData[] = 64 "{" 65 " \"refresh_token\": \"1234\"," 66 " \"access_token\": \"5678\"," 67 " \"expires_in\": 3600" 68 "}"; 69 70const char kOAuth2AccessTokenData[] = 71 "{" 72 " \"access_token\": \"5678\"," 73 " \"expires_in\": 3600" 74 "}"; 75 76} // namespace 77 78class UserCloudPolicyManagerChromeOSTest : public testing::Test { 79 protected: 80 UserCloudPolicyManagerChromeOSTest() 81 : store_(NULL), 82 profile_(NULL), 83 signin_profile_(NULL) {} 84 85 virtual void SetUp() OVERRIDE { 86 // The initialization path that blocks on the initial policy fetch requires 87 // a signin Profile to use its URLRequestContext. 88 profile_manager_.reset( 89 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); 90 ASSERT_TRUE(profile_manager_->SetUp()); 91 profile_ = profile_manager_->CreateTestingProfile( 92 chrome::kInitialProfile, scoped_ptr<PrefServiceSyncable>(), 93 UTF8ToUTF16("testing_profile"), 0, std::string()); 94 signin_profile_ = profile_manager_->CreateTestingProfile(kSigninProfile); 95 signin_profile_->ForceIncognito(true); 96 // Usually the signin Profile and the main Profile are separate, but since 97 // the signin Profile is an OTR Profile then for this test it suffices to 98 // attach it to the main Profile. 99 profile_->SetOffTheRecordProfile(scoped_ptr<Profile>(signin_profile_)); 100 signin_profile_->SetOriginalProfile(profile_); 101 ASSERT_EQ(signin_profile_, chromeos::ProfileHelper::GetSigninProfile()); 102 103 chrome::RegisterLocalState(prefs_.registry()); 104 105 // Set up a policy map for testing. 106 policy_map_.Set("HomepageLocation", 107 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 108 base::Value::CreateStringValue("http://chromium.org"), 109 NULL); 110 expected_bundle_.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) 111 .CopyFrom(policy_map_); 112 113 // Create fake policy blobs to deliver to the client. 114 em::DeviceRegisterResponse* register_response = 115 register_blob_.mutable_register_response(); 116 register_response->set_device_management_token("dmtoken123"); 117 118 em::CloudPolicySettings policy_proto; 119 policy_proto.mutable_homepagelocation()->set_value("http://chromium.org"); 120 ASSERT_TRUE( 121 policy_proto.SerializeToString(policy_data_.mutable_policy_value())); 122 policy_data_.set_policy_type(dm_protocol::kChromeUserPolicyType); 123 policy_data_.set_request_token("dmtoken123"); 124 policy_data_.set_device_id("id987"); 125 em::PolicyFetchResponse* policy_response = 126 policy_blob_.mutable_policy_response()->add_response(); 127 ASSERT_TRUE(policy_data_.SerializeToString( 128 policy_response->mutable_policy_data())); 129 130 EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _)) 131 .Times(AnyNumber()); 132 } 133 134 virtual void TearDown() OVERRIDE { 135 if (token_forwarder_) 136 token_forwarder_->Shutdown(); 137 if (manager_) { 138 manager_->RemoveObserver(&observer_); 139 manager_->Shutdown(); 140 } 141 signin_profile_ = NULL; 142 profile_ = NULL; 143 profile_manager_->DeleteTestingProfile(kSigninProfile); 144 profile_manager_->DeleteTestingProfile(chrome::kInitialProfile); 145 } 146 147 void CreateManager(bool wait_for_fetch, int fetch_timeout) { 148 store_ = new MockCloudPolicyStore(); 149 EXPECT_CALL(*store_, Load()); 150 manager_.reset(new UserCloudPolicyManagerChromeOS( 151 scoped_ptr<CloudPolicyStore>(store_), 152 scoped_ptr<ResourceCache>(), 153 wait_for_fetch, 154 base::TimeDelta::FromSeconds(fetch_timeout))); 155 manager_->Init(); 156 manager_->AddObserver(&observer_); 157 manager_->Connect(&prefs_, &device_management_service_, NULL, 158 USER_AFFILIATION_NONE); 159 Mock::VerifyAndClearExpectations(store_); 160 EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); 161 EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete()); 162 163 if (!wait_for_fetch) { 164 // Create the UserCloudPolicyTokenForwarder, which fetches the access 165 // token using the OAuth2PolicyFetcher and forwards it to the 166 // UserCloudPolicyManagerChromeOS. This service is automatically created 167 // for regular Profiles but not for testing Profiles. 168 ProfileOAuth2TokenService* token_service = 169 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); 170 ASSERT_TRUE(token_service); 171 token_forwarder_.reset( 172 new UserCloudPolicyTokenForwarder(manager_.get(), token_service)); 173 } 174 } 175 176 // Expects a pending URLFetcher for the |expected_url|, and returns it with 177 // prepared to deliver a response to its delegate. 178 net::TestURLFetcher* PrepareOAuthFetcher(const std::string& expected_url) { 179 net::TestURLFetcher* fetcher = test_url_fetcher_factory_.GetFetcherByID(0); 180 EXPECT_TRUE(fetcher); 181 if (!fetcher) 182 return NULL; 183 EXPECT_TRUE(fetcher->delegate()); 184 EXPECT_TRUE(StartsWithASCII(fetcher->GetOriginalURL().spec(), 185 expected_url, 186 true)); 187 fetcher->set_url(fetcher->GetOriginalURL()); 188 fetcher->set_response_code(200); 189 fetcher->set_status(net::URLRequestStatus()); 190 return fetcher; 191 } 192 193 // Issues the OAuth2 tokens and returns the device management register job 194 // if the flow succeeded. 195 MockDeviceManagementJob* IssueOAuthToken(bool has_request_token) { 196 EXPECT_FALSE(manager_->core()->client()->is_registered()); 197 198 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); 199 net::TestURLFetcher* fetcher = NULL; 200 201 if (!has_request_token) { 202 // Issue the oauth_token cookie first. 203 fetcher = PrepareOAuthFetcher(gaia_urls->client_login_to_oauth2_url()); 204 if (!fetcher) 205 return NULL; 206 net::ResponseCookies cookies; 207 cookies.push_back(kOAuthTokenCookie); 208 fetcher->set_cookies(cookies); 209 fetcher->delegate()->OnURLFetchComplete(fetcher); 210 211 // Issue the refresh token. 212 fetcher = PrepareOAuthFetcher(gaia_urls->oauth2_token_url()); 213 if (!fetcher) 214 return NULL; 215 fetcher->SetResponseString(kOAuth2TokenPairData); 216 fetcher->delegate()->OnURLFetchComplete(fetcher); 217 } 218 219 // Issue the access token. 220 fetcher = PrepareOAuthFetcher(gaia_urls->oauth2_token_url()); 221 if (!fetcher) 222 return NULL; 223 fetcher->SetResponseString(kOAuth2AccessTokenData); 224 225 // Issuing this token triggers the callback of the OAuth2PolicyFetcher, 226 // which triggers the registration request. 227 MockDeviceManagementJob* register_request = NULL; 228 EXPECT_CALL(device_management_service_, 229 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION)) 230 .WillOnce(device_management_service_.CreateAsyncJob(®ister_request)); 231 fetcher->delegate()->OnURLFetchComplete(fetcher); 232 EXPECT_TRUE(register_request); 233 EXPECT_FALSE(manager_->core()->client()->is_registered()); 234 235 Mock::VerifyAndClearExpectations(&device_management_service_); 236 EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _)) 237 .Times(AnyNumber()); 238 239 return register_request; 240 } 241 242 // Expects a policy fetch request to be issued after invoking |trigger_fetch|. 243 // This method replies to that fetch request and verifies that the manager 244 // handled the response. 245 void FetchPolicy(const base::Closure& trigger_fetch) { 246 MockDeviceManagementJob* policy_request = NULL; 247 EXPECT_CALL(device_management_service_, 248 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) 249 .WillOnce(device_management_service_.CreateAsyncJob(&policy_request)); 250 trigger_fetch.Run(); 251 ASSERT_TRUE(policy_request); 252 EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete()); 253 EXPECT_TRUE(manager_->core()->client()->is_registered()); 254 255 Mock::VerifyAndClearExpectations(&device_management_service_); 256 EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _)) 257 .Times(AnyNumber()); 258 259 // Send the initial policy back. This completes the initialization flow. 260 EXPECT_CALL(*store_, Store(_)); 261 policy_request->SendResponse(DM_STATUS_SUCCESS, policy_blob_); 262 Mock::VerifyAndClearExpectations(store_); 263 264 // Notifying that the store is has cached the fetched policy completes the 265 // process, and initializes the manager. 266 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); 267 store_->policy_map_.CopyFrom(policy_map_); 268 store_->NotifyStoreLoaded(); 269 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); 270 Mock::VerifyAndClearExpectations(&observer_); 271 EXPECT_TRUE(manager_->policies().Equals(expected_bundle_)); 272 } 273 274 // Required by the refresh scheduler that's created by the manager and 275 // for the cleanup of URLRequestContextGetter in the |signin_profile_|. 276 content::TestBrowserThreadBundle thread_bundle_; 277 278 // Convenience policy objects. 279 em::PolicyData policy_data_; 280 em::DeviceManagementResponse register_blob_; 281 em::DeviceManagementResponse policy_blob_; 282 PolicyMap policy_map_; 283 PolicyBundle expected_bundle_; 284 285 // Policy infrastructure. 286 net::TestURLFetcherFactory test_url_fetcher_factory_; 287 TestingPrefServiceSimple prefs_; 288 MockConfigurationPolicyObserver observer_; 289 MockDeviceManagementService device_management_service_; 290 MockCloudPolicyStore* store_; 291 scoped_ptr<UserCloudPolicyManagerChromeOS> manager_; 292 scoped_ptr<UserCloudPolicyTokenForwarder> token_forwarder_; 293 294 // Required by ProfileHelper to get the signin Profile context. 295 scoped_ptr<TestingProfileManager> profile_manager_; 296 TestingProfile* profile_; 297 TestingProfile* signin_profile_; 298 299 static const char kSigninProfile[]; 300 301 private: 302 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerChromeOSTest); 303}; 304 305const char UserCloudPolicyManagerChromeOSTest::kSigninProfile[] = 306 "signin_profile"; 307 308TEST_F(UserCloudPolicyManagerChromeOSTest, BlockingFirstFetch) { 309 // Tests the initialization of a manager whose Profile is waiting for the 310 // initial fetch, when the policy cache is empty. 311 ASSERT_NO_FATAL_FAILURE(CreateManager(true, 1000)); 312 313 // Initialize the CloudPolicyService without any stored data. 314 EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete()); 315 store_->NotifyStoreLoaded(); 316 EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete()); 317 EXPECT_FALSE(manager_->core()->client()->is_registered()); 318 319 // This starts the OAuth2 policy token fetcher using the signin Profile. 320 // The manager will then issue the registration request. 321 MockDeviceManagementJob* register_request = IssueOAuthToken(false); 322 ASSERT_TRUE(register_request); 323 324 // Reply with a valid registration response. This triggers the initial policy 325 // fetch. 326 FetchPolicy(base::Bind(&MockDeviceManagementJob::SendResponse, 327 base::Unretained(register_request), 328 DM_STATUS_SUCCESS, register_blob_)); 329} 330 331TEST_F(UserCloudPolicyManagerChromeOSTest, BlockingRefreshFetch) { 332 // Tests the initialization of a manager whose Profile is waiting for the 333 // initial fetch, when a previously cached policy and DMToken already exist. 334 ASSERT_NO_FATAL_FAILURE(CreateManager(true, 1000)); 335 336 // Set the initially cached data and initialize the CloudPolicyService. 337 // The initial policy fetch is issued using the cached DMToken. 338 store_->policy_.reset(new em::PolicyData(policy_data_)); 339 FetchPolicy(base::Bind(&MockCloudPolicyStore::NotifyStoreLoaded, 340 base::Unretained(store_))); 341} 342 343TEST_F(UserCloudPolicyManagerChromeOSTest, BlockingFetchStoreError) { 344 // Tests the initialization of a manager whose Profile is waiting for the 345 // initial fetch, when the initial store load fails. 346 ASSERT_NO_FATAL_FAILURE(CreateManager(true, 1000)); 347 348 // Initialize the CloudPolicyService without any stored data. 349 EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete()); 350 store_->NotifyStoreError(); 351 EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete()); 352 EXPECT_FALSE(manager_->core()->client()->is_registered()); 353 354 // This starts the OAuth2 policy token fetcher using the signin Profile. 355 // The manager will then issue the registration request. 356 MockDeviceManagementJob* register_request = IssueOAuthToken(false); 357 ASSERT_TRUE(register_request); 358 359 // Reply with a valid registration response. This triggers the initial policy 360 // fetch. 361 FetchPolicy(base::Bind(&MockDeviceManagementJob::SendResponse, 362 base::Unretained(register_request), 363 DM_STATUS_SUCCESS, register_blob_)); 364} 365 366TEST_F(UserCloudPolicyManagerChromeOSTest, BlockingFetchOAuthError) { 367 // Tests the initialization of a manager whose Profile is waiting for the 368 // initial fetch, when the OAuth2 token fetch fails. 369 ASSERT_NO_FATAL_FAILURE(CreateManager(true, 1000)); 370 371 // Initialize the CloudPolicyService without any stored data. 372 EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete()); 373 store_->NotifyStoreLoaded(); 374 EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete()); 375 EXPECT_FALSE(manager_->core()->client()->is_registered()); 376 377 // This starts the OAuth2 policy token fetcher using the signin Profile. 378 // The manager will initialize with no policy after the token fetcher fails. 379 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); 380 381 // The PolicyOAuth2TokenFetcher posts delayed retries on some errors. This 382 // data will make it fail immediately. 383 net::TestURLFetcher* fetcher = PrepareOAuthFetcher( 384 GaiaUrls::GetInstance()->client_login_to_oauth2_url()); 385 ASSERT_TRUE(fetcher); 386 fetcher->set_response_code(400); 387 fetcher->SetResponseString("Error=BadAuthentication"); 388 EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); 389 fetcher->delegate()->OnURLFetchComplete(fetcher); 390 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); 391 EXPECT_TRUE(PolicyBundle().Equals(manager_->policies())); 392 Mock::VerifyAndClearExpectations(&observer_); 393} 394 395TEST_F(UserCloudPolicyManagerChromeOSTest, BlockingFetchRegisterError) { 396 // Tests the initialization of a manager whose Profile is waiting for the 397 // initial fetch, when the device management registration fails. 398 ASSERT_NO_FATAL_FAILURE(CreateManager(true, 1000)); 399 400 // Initialize the CloudPolicyService without any stored data. 401 EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete()); 402 store_->NotifyStoreError(); 403 EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete()); 404 EXPECT_FALSE(manager_->core()->client()->is_registered()); 405 406 // This starts the OAuth2 policy token fetcher using the signin Profile. 407 // The manager will then issue the registration request. 408 MockDeviceManagementJob* register_request = IssueOAuthToken(false); 409 ASSERT_TRUE(register_request); 410 411 // Now make it fail. 412 EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); 413 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); 414 register_request->SendResponse(DM_STATUS_TEMPORARY_UNAVAILABLE, 415 em::DeviceManagementResponse()); 416 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); 417 EXPECT_TRUE(PolicyBundle().Equals(manager_->policies())); 418 Mock::VerifyAndClearExpectations(&observer_); 419} 420 421TEST_F(UserCloudPolicyManagerChromeOSTest, BlockingFetchPolicyFetchError) { 422 // Tests the initialization of a manager whose Profile is waiting for the 423 // initial fetch, when the policy fetch request fails. 424 ASSERT_NO_FATAL_FAILURE(CreateManager(true, 1000)); 425 426 // Initialize the CloudPolicyService without any stored data. 427 EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete()); 428 store_->NotifyStoreLoaded(); 429 EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete()); 430 EXPECT_FALSE(manager_->core()->client()->is_registered()); 431 432 // This starts the OAuth2 policy token fetcher using the signin Profile. 433 // The manager will then issue the registration request. 434 MockDeviceManagementJob* register_request = IssueOAuthToken(false); 435 ASSERT_TRUE(register_request); 436 437 // Reply with a valid registration response. This triggers the initial policy 438 // fetch. 439 MockDeviceManagementJob* policy_request = NULL; 440 EXPECT_CALL(device_management_service_, 441 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) 442 .WillOnce(device_management_service_.CreateAsyncJob(&policy_request)); 443 register_request->SendResponse(DM_STATUS_SUCCESS, register_blob_); 444 Mock::VerifyAndClearExpectations(&device_management_service_); 445 ASSERT_TRUE(policy_request); 446 EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete()); 447 EXPECT_TRUE(manager_->core()->client()->is_registered()); 448 449 // Make the policy fetch fail. The observer gets 2 notifications: one from the 450 // RefreshPolicies callback, and another from the OnClientError callback. 451 // A single notification suffices for this edge case, but this behavior is 452 // also correct and makes the implementation simpler. 453 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())).Times(AtLeast(1)); 454 EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); 455 policy_request->SendResponse(DM_STATUS_TEMPORARY_UNAVAILABLE, 456 em::DeviceManagementResponse()); 457 Mock::VerifyAndClearExpectations(&observer_); 458 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); 459 EXPECT_TRUE(PolicyBundle().Equals(manager_->policies())); 460} 461 462TEST_F(UserCloudPolicyManagerChromeOSTest, BlockingFetchTimeout) { 463 // The blocking fetch should be abandoned after the timeout. 464 ASSERT_NO_FATAL_FAILURE(CreateManager(true, 0)); 465 466 // Initialize the CloudPolicyService without any stored data. 467 EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete()); 468 store_->NotifyStoreLoaded(); 469 EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete()); 470 EXPECT_FALSE(manager_->core()->client()->is_registered()); 471 472 // Running the message loop should trigger the timeout. 473 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())).Times(AtLeast(1)); 474 EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); 475 base::RunLoop().RunUntilIdle(); 476 Mock::VerifyAndClearExpectations(&observer_); 477 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); 478 EXPECT_TRUE(PolicyBundle().Equals(manager_->policies())); 479} 480 481 482TEST_F(UserCloudPolicyManagerChromeOSTest, NonBlockingFirstFetch) { 483 // Tests the first policy fetch request by a Profile that isn't managed. 484 ASSERT_NO_FATAL_FAILURE(CreateManager(false, 1000)); 485 486 // Initialize the CloudPolicyService without any stored data. Since the 487 // manager is not waiting for the initial fetch, it will become initialized 488 // once the store is ready. 489 EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete()); 490 EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); 491 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); 492 store_->NotifyStoreLoaded(); 493 Mock::VerifyAndClearExpectations(&observer_); 494 EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete()); 495 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); 496 EXPECT_FALSE(manager_->core()->client()->is_registered()); 497 498 // The manager is waiting for the refresh token, and hasn't started any 499 // fetchers. 500 EXPECT_FALSE(test_url_fetcher_factory_.GetFetcherByID(0)); 501 502 // Set a fake refresh token at the TokenService. 503 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); 504 ASSERT_TRUE(token_service); 505 GaiaAuthConsumer::ClientOAuthResult tokens("refresh", "access", 3600); 506 EXPECT_FALSE(token_service->HasOAuthLoginToken()); 507 token_service->UpdateCredentialsWithOAuth2(tokens); 508 EXPECT_TRUE(token_service->HasOAuthLoginToken()); 509 510 // That should have notified the manager, which now issues the request for the 511 // policy oauth token. 512 MockDeviceManagementJob* register_request = IssueOAuthToken(true); 513 ASSERT_TRUE(register_request); 514 register_request->SendResponse(DM_STATUS_SUCCESS, register_blob_); 515 516 // The refresh scheduler takes care of the initial fetch for unmanaged users. 517 // It posts a delayed task with 0ms delay in this case, so spinning the loop 518 // issues the initial fetch. 519 base::RunLoop loop; 520 FetchPolicy( 521 base::Bind(&base::RunLoop::RunUntilIdle, base::Unretained(&loop))); 522} 523 524TEST_F(UserCloudPolicyManagerChromeOSTest, NonBlockingRefreshFetch) { 525 // Tests a non-blocking initial policy fetch for a Profile that already has 526 // a cached DMToken. 527 ASSERT_NO_FATAL_FAILURE(CreateManager(false, 1000)); 528 529 // Set the initially cached data and initialize the CloudPolicyService. 530 // The initial policy fetch is issued using the cached DMToken. 531 EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete()); 532 EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); 533 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); 534 store_->policy_.reset(new em::PolicyData(policy_data_)); 535 store_->NotifyStoreLoaded(); 536 Mock::VerifyAndClearExpectations(&observer_); 537 EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete()); 538 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); 539 EXPECT_TRUE(manager_->core()->client()->is_registered()); 540 541 // The refresh scheduler takes care of the initial fetch for unmanaged users. 542 // It posts a delayed task with 0ms delay in this case, so spinning the loop 543 // issues the initial fetch. 544 base::RunLoop loop; 545 FetchPolicy( 546 base::Bind(&base::RunLoop::RunUntilIdle, base::Unretained(&loop))); 547} 548 549} // namespace policy 550