OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "chrome/browser/chromeos/settings/device_oauth2_token_service.h" | 5 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/prefs/testing_pref_service.h" | 8 #include "base/prefs/testing_pref_service.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/threading/sequenced_worker_pool.h" | 10 #include "base/threading/sequenced_worker_pool.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "chromeos/dbus/fake_cryptohome_client.h" | 21 #include "chromeos/dbus/fake_cryptohome_client.h" |
22 #include "components/ownership/mock_owner_key_util.h" | 22 #include "components/ownership/mock_owner_key_util.h" |
23 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
24 #include "content/public/test/test_browser_thread.h" | 24 #include "content/public/test/test_browser_thread.h" |
25 #include "google_apis/gaia/gaia_oauth_client.h" | 25 #include "google_apis/gaia/gaia_oauth_client.h" |
26 #include "google_apis/gaia/oauth2_token_service_test_util.h" | 26 #include "google_apis/gaia/oauth2_token_service_test_util.h" |
27 #include "net/http/http_status_code.h" | 27 #include "net/http/http_status_code.h" |
28 #include "net/url_request/test_url_fetcher_factory.h" | 28 #include "net/url_request/test_url_fetcher_factory.h" |
29 #include "net/url_request/url_fetcher_delegate.h" | 29 #include "net/url_request/url_fetcher_delegate.h" |
30 #include "net/url_request/url_request_test_util.h" | 30 #include "net/url_request/url_request_test_util.h" |
| 31 #include "testing/gmock/include/gmock/gmock.h" |
31 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
32 | 33 |
33 namespace chromeos { | 34 namespace chromeos { |
34 | 35 |
| 36 namespace { |
| 37 |
| 38 class MockOAuth2TokenServiceObserver : public OAuth2TokenService::Observer { |
| 39 public: |
| 40 MockOAuth2TokenServiceObserver(); |
| 41 ~MockOAuth2TokenServiceObserver() override; |
| 42 |
| 43 MOCK_METHOD1(OnRefreshTokenAvailable, void(const std::string&)); |
| 44 }; |
| 45 |
| 46 MockOAuth2TokenServiceObserver::MockOAuth2TokenServiceObserver() { |
| 47 } |
| 48 |
| 49 MockOAuth2TokenServiceObserver::~MockOAuth2TokenServiceObserver() { |
| 50 } |
| 51 |
| 52 } // namespace |
| 53 |
35 static const int kOAuthTokenServiceUrlFetcherId = 0; | 54 static const int kOAuthTokenServiceUrlFetcherId = 0; |
36 static const int kValidatorUrlFetcherId = gaia::GaiaOAuthClient::kUrlFetcherId; | 55 static const int kValidatorUrlFetcherId = gaia::GaiaOAuthClient::kUrlFetcherId; |
37 | 56 |
38 class DeviceOAuth2TokenServiceTest : public testing::Test { | 57 class DeviceOAuth2TokenServiceTest : public testing::Test { |
39 public: | 58 public: |
40 DeviceOAuth2TokenServiceTest() | 59 DeviceOAuth2TokenServiceTest() |
41 : scoped_testing_local_state_(TestingBrowserProcess::GetGlobal()), | 60 : scoped_testing_local_state_(TestingBrowserProcess::GetGlobal()), |
42 request_context_getter_(new net::TestURLRequestContextGetter( | 61 request_context_getter_(new net::TestURLRequestContextGetter( |
43 message_loop_.message_loop_proxy())) {} | 62 message_loop_.message_loop_proxy())) {} |
44 ~DeviceOAuth2TokenServiceTest() override {} | 63 ~DeviceOAuth2TokenServiceTest() override {} |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 new ownership::MockOwnerKeyUtil()); | 108 new ownership::MockOwnerKeyUtil()); |
90 owner_key_util_->SetPublicKeyFromPrivateKey( | 109 owner_key_util_->SetPublicKeyFromPrivateKey( |
91 *device_policy_.GetSigningKey()); | 110 *device_policy_.GetSigningKey()); |
92 DeviceSettingsService::Get()->SetSessionManager( | 111 DeviceSettingsService::Get()->SetSessionManager( |
93 &device_settings_test_helper_, owner_key_util_); | 112 &device_settings_test_helper_, owner_key_util_); |
94 | 113 |
95 CrosSettings::Initialize(); | 114 CrosSettings::Initialize(); |
96 } | 115 } |
97 | 116 |
98 void TearDown() override { | 117 void TearDown() override { |
| 118 oauth2_service_.reset(); |
99 CrosSettings::Shutdown(); | 119 CrosSettings::Shutdown(); |
100 TestingBrowserProcess::GetGlobal()->SetBrowserPolicyConnector(NULL); | 120 TestingBrowserProcess::GetGlobal()->SetBrowserPolicyConnector(NULL); |
101 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 121 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
102 DeviceSettingsService::Get()->UnsetSessionManager(); | 122 DeviceSettingsService::Get()->UnsetSessionManager(); |
103 DeviceSettingsService::Shutdown(); | 123 DeviceSettingsService::Shutdown(); |
104 SystemSaltGetter::Shutdown(); | 124 SystemSaltGetter::Shutdown(); |
105 DBusThreadManager::Shutdown(); | 125 DBusThreadManager::Shutdown(); |
106 base::RunLoop().RunUntilIdle(); | 126 base::RunLoop().RunUntilIdle(); |
107 } | 127 } |
108 | 128 |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 net::HTTP_OK, GetValidTokenResponse("ignored", 3600)); | 443 net::HTTP_OK, GetValidTokenResponse("ignored", 3600)); |
424 | 444 |
425 AssertConsumerTokensAndErrors(0, 1); | 445 AssertConsumerTokensAndErrors(0, 1); |
426 | 446 |
427 // Retry should succeed. | 447 // Retry should succeed. |
428 request = StartTokenRequest(); | 448 request = StartTokenRequest(); |
429 PerformURLFetches(); | 449 PerformURLFetches(); |
430 AssertConsumerTokensAndErrors(1, 1); | 450 AssertConsumerTokensAndErrors(1, 1); |
431 } | 451 } |
432 | 452 |
| 453 TEST_F(DeviceOAuth2TokenServiceTest, DoNotAnnounceTokenWithoutAccountID) { |
| 454 CreateService(); |
| 455 |
| 456 testing::StrictMock<MockOAuth2TokenServiceObserver> observer; |
| 457 oauth2_service_->AddObserver(&observer); |
| 458 |
| 459 // Make a token available during enrollment. Verify that the token is not |
| 460 // announced yet. |
| 461 oauth2_service_->SetAndSaveRefreshToken( |
| 462 "test-token", DeviceOAuth2TokenService::StatusCallback()); |
| 463 testing::Mock::VerifyAndClearExpectations(&observer); |
| 464 |
| 465 // Also make the robot account ID available. Verify that the token is |
| 466 // announced now. |
| 467 EXPECT_CALL(observer, OnRefreshTokenAvailable("robot@example.com")); |
| 468 SetRobotAccountId("robot@example.com"); |
| 469 testing::Mock::VerifyAndClearExpectations(&observer); |
| 470 |
| 471 oauth2_service_->RemoveObserver(&observer); |
| 472 } |
| 473 |
433 } // namespace chromeos | 474 } // namespace chromeos |
OLD | NEW |