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/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
6 #include "base/message_loop/message_loop_proxy.h" | 6 #include "base/message_loop/message_loop_proxy.h" |
7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
11 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
12 #include "chrome/browser/policy/browser_policy_connector.h" | 12 #include "chrome/browser/policy/browser_policy_connector.h" |
13 #include "chrome/browser/policy/cloud/cloud_external_data_manager.h" | 13 #include "chrome/browser/policy/cloud/cloud_external_data_manager.h" |
14 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" | 14 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" |
15 #include "chrome/browser/policy/cloud/mock_device_management_service.h" | 15 #include "chrome/browser/policy/cloud/mock_device_management_service.h" |
16 #include "chrome/browser/policy/cloud/mock_user_cloud_policy_store.h" | 16 #include "chrome/browser/policy/cloud/mock_user_cloud_policy_store.h" |
17 #include "chrome/browser/policy/cloud/user_cloud_policy_manager.h" | 17 #include "chrome/browser/policy/cloud/user_cloud_policy_manager.h" |
18 #include "chrome/browser/policy/cloud/user_policy_signin_service_factory.h" | 18 #include "chrome/browser/policy/cloud/user_policy_signin_service_factory.h" |
19 #include "chrome/browser/prefs/browser_prefs.h" | 19 #include "chrome/browser/prefs/browser_prefs.h" |
20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" |
21 #include "chrome/browser/signin/fake_signin_manager.h" | 22 #include "chrome/browser/signin/fake_signin_manager.h" |
22 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 23 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
23 #include "chrome/browser/signin/signin_manager.h" | 24 #include "chrome/browser/signin/signin_manager.h" |
24 #include "chrome/browser/signin/signin_manager_factory.h" | 25 #include "chrome/browser/signin/signin_manager_factory.h" |
25 #include "chrome/test/base/testing_browser_process.h" | 26 #include "chrome/test/base/testing_browser_process.h" |
26 #include "chrome/test/base/testing_pref_service_syncable.h" | 27 #include "chrome/test/base/testing_pref_service_syncable.h" |
27 #include "chrome/test/base/testing_profile.h" | 28 #include "chrome/test/base/testing_profile.h" |
28 #include "content/public/browser/browser_context.h" | 29 #include "content/public/browser/browser_context.h" |
29 #include "content/public/browser/notification_details.h" | 30 #include "content/public/browser/notification_details.h" |
30 #include "content/public/browser/notification_service.h" | 31 #include "content/public/browser/notification_service.h" |
31 #include "content/public/browser/notification_source.h" | 32 #include "content/public/browser/notification_source.h" |
32 #include "content/public/test/test_browser_thread_bundle.h" | 33 #include "content/public/test/test_browser_thread_bundle.h" |
33 #include "google_apis/gaia/gaia_constants.h" | 34 #include "google_apis/gaia/gaia_constants.h" |
34 #include "google_apis/gaia/google_service_auth_error.h" | 35 #include "google_apis/gaia/google_service_auth_error.h" |
35 #include "net/http/http_status_code.h" | 36 #include "net/http/http_status_code.h" |
36 #include "net/url_request/test_url_fetcher_factory.h" | 37 #include "net/url_request/test_url_fetcher_factory.h" |
37 #include "net/url_request/url_request_context_getter.h" | 38 #include "net/url_request/url_request_context_getter.h" |
38 #include "net/url_request/url_request_status.h" | 39 #include "net/url_request/url_request_status.h" |
39 #include "testing/gmock/include/gmock/gmock.h" | 40 #include "testing/gmock/include/gmock/gmock.h" |
40 #include "testing/gtest/include/gtest/gtest.h" | 41 #include "testing/gtest/include/gtest/gtest.h" |
41 | 42 |
42 #if defined(OS_ANDROID) | 43 #if defined(OS_ANDROID) |
43 #include "chrome/browser/policy/cloud/user_policy_signin_service_android.h" | 44 #include "chrome/browser/policy/cloud/user_policy_signin_service_android.h" |
44 #include "chrome/browser/signin/android_profile_oauth2_token_service.h" | |
45 #else | 45 #else |
46 #include "chrome/browser/policy/cloud/user_policy_signin_service.h" | 46 #include "chrome/browser/policy/cloud/user_policy_signin_service.h" |
47 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" | |
48 #endif | 47 #endif |
49 | 48 |
50 namespace em = enterprise_management; | 49 namespace em = enterprise_management; |
51 | 50 |
52 using testing::AnyNumber; | 51 using testing::AnyNumber; |
53 using testing::Mock; | 52 using testing::Mock; |
54 using testing::_; | 53 using testing::_; |
55 | 54 |
56 namespace policy { | 55 namespace policy { |
57 | 56 |
(...skipping 24 matching lines...) Expand all Loading... |
82 // Allow signing out now. | 81 // Allow signing out now. |
83 prohibit_signout_ = false; | 82 prohibit_signout_ = false; |
84 SignOut(); | 83 SignOut(); |
85 } | 84 } |
86 | 85 |
87 static BrowserContextKeyedService* Build(content::BrowserContext* profile) { | 86 static BrowserContextKeyedService* Build(content::BrowserContext* profile) { |
88 return new SigninManagerFake(static_cast<Profile*>(profile)); | 87 return new SigninManagerFake(static_cast<Profile*>(profile)); |
89 } | 88 } |
90 }; | 89 }; |
91 | 90 |
92 #if defined(OS_ANDROID) | |
93 // TODO(atwilson): Remove this when ProfileOAuth2TokenService supports | |
94 // usernames. | |
95 class FakeAndroidProfileOAuth2TokenService | |
96 : public AndroidProfileOAuth2TokenService { | |
97 public: | |
98 explicit FakeAndroidProfileOAuth2TokenService(Profile* profile) { | |
99 Initialize(profile); | |
100 } | |
101 | |
102 static BrowserContextKeyedService* Build(content::BrowserContext* profile) { | |
103 return new FakeAndroidProfileOAuth2TokenService( | |
104 static_cast<Profile*>(profile)); | |
105 } | |
106 | |
107 // AndroidProfileOAuth2TokenService overrides: | |
108 virtual void FetchOAuth2TokenWithUsername( | |
109 RequestImpl* request, | |
110 const std::string& username, | |
111 const OAuth2TokenService::ScopeSet& scope) OVERRIDE { | |
112 ASSERT_TRUE(!HasPendingRequest()); | |
113 ASSERT_EQ(kTestUser, username); | |
114 ASSERT_EQ(2U, scope.size()); | |
115 EXPECT_EQ(1U, scope.count(GaiaConstants::kDeviceManagementServiceOAuth)); | |
116 EXPECT_EQ(1U, scope.count( | |
117 "https://www.googleapis.com/auth/userinfo.email")); | |
118 pending_request_ = request->AsWeakPtr(); | |
119 } | |
120 | |
121 void IssueToken(const std::string& token) { | |
122 ASSERT_TRUE(HasPendingRequest()); | |
123 GoogleServiceAuthError error = GoogleServiceAuthError::AuthErrorNone(); | |
124 if (token.empty()) | |
125 error = GoogleServiceAuthError::FromServiceError("fail"); | |
126 if (pending_request_) { | |
127 pending_request_->InformConsumer( | |
128 error, | |
129 token, | |
130 base::Time::Now() + base::TimeDelta::FromDays(1)); | |
131 } | |
132 pending_request_.reset(); | |
133 } | |
134 | |
135 bool HasPendingRequest() const { | |
136 return pending_request_; | |
137 } | |
138 | |
139 private: | |
140 base::WeakPtr<RequestImpl> pending_request_; | |
141 }; | |
142 | |
143 #endif | |
144 | |
145 class UserPolicySigninServiceTest : public testing::Test { | 91 class UserPolicySigninServiceTest : public testing::Test { |
146 public: | 92 public: |
147 UserPolicySigninServiceTest() | 93 UserPolicySigninServiceTest() |
148 : mock_store_(NULL), | 94 : mock_store_(NULL), |
149 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 95 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
150 register_completed_(false) {} | 96 register_completed_(false) {} |
151 | 97 |
152 MOCK_METHOD1(OnPolicyRefresh, void(bool)); | 98 MOCK_METHOD1(OnPolicyRefresh, void(bool)); |
153 | 99 |
154 void OnRegisterCompleted(scoped_ptr<CloudPolicyClient> client) { | 100 void OnRegisterCompleted(scoped_ptr<CloudPolicyClient> client) { |
155 register_completed_ = true; | 101 register_completed_ = true; |
156 created_client_.swap(client); | 102 created_client_.swap(client); |
157 } | 103 } |
158 | 104 |
159 void RegisterPolicyClientWithCallback(UserPolicySigninService* service) { | 105 void RegisterPolicyClientWithCallback(UserPolicySigninService* service) { |
| 106 // Policy client registration on Android depends on Token Service having |
| 107 // a valid login token, while on other platforms, the login refresh token |
| 108 // is specified directly. |
| 109 #if defined(OS_ANDROID) |
| 110 GetTokenService()->IssueRefreshToken("oauth2_login_refresh_token"); |
| 111 #endif |
160 service->RegisterPolicyClient( | 112 service->RegisterPolicyClient( |
161 kTestUser, | 113 kTestUser, |
162 #if !defined(OS_ANDROID) | 114 #if !defined(OS_ANDROID) |
163 "mock_oauth_token", | 115 "mock_oauth_token", |
164 #endif | 116 #endif |
165 base::Bind(&UserPolicySigninServiceTest::OnRegisterCompleted, | 117 base::Bind(&UserPolicySigninServiceTest::OnRegisterCompleted, |
166 base::Unretained(this))); | 118 base::Unretained(this))); |
167 ASSERT_TRUE(IsRequestActive()); | 119 ASSERT_TRUE(IsRequestActive()); |
168 } | 120 } |
169 | 121 |
(...skipping 11 matching lines...) Expand all Loading... |
181 | 133 |
182 // Create a testing profile with cloud-policy-on-signin enabled, and bring | 134 // Create a testing profile with cloud-policy-on-signin enabled, and bring |
183 // up a UserCloudPolicyManager with a MockUserCloudPolicyStore. | 135 // up a UserCloudPolicyManager with a MockUserCloudPolicyStore. |
184 scoped_ptr<TestingPrefServiceSyncable> prefs( | 136 scoped_ptr<TestingPrefServiceSyncable> prefs( |
185 new TestingPrefServiceSyncable()); | 137 new TestingPrefServiceSyncable()); |
186 chrome::RegisterUserProfilePrefs(prefs->registry()); | 138 chrome::RegisterUserProfilePrefs(prefs->registry()); |
187 TestingProfile::Builder builder; | 139 TestingProfile::Builder builder; |
188 builder.SetPrefService(scoped_ptr<PrefServiceSyncable>(prefs.Pass())); | 140 builder.SetPrefService(scoped_ptr<PrefServiceSyncable>(prefs.Pass())); |
189 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), | 141 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), |
190 SigninManagerFake::Build); | 142 SigninManagerFake::Build); |
191 #if defined(OS_ANDROID) | |
192 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), | |
193 FakeAndroidProfileOAuth2TokenService::Build); | |
194 #else | |
195 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), | 143 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), |
196 FakeProfileOAuth2TokenService::Build); | 144 FakeProfileOAuth2TokenService::Build); |
197 #endif | |
198 | 145 |
199 profile_ = builder.Build().Pass(); | 146 profile_ = builder.Build().Pass(); |
200 signin_manager_ = static_cast<SigninManagerFake*>( | 147 signin_manager_ = static_cast<SigninManagerFake*>( |
201 SigninManagerFactory::GetForProfile(profile_.get())); | 148 SigninManagerFactory::GetForProfile(profile_.get())); |
202 | 149 |
203 mock_store_ = new MockUserCloudPolicyStore(); | 150 mock_store_ = new MockUserCloudPolicyStore(); |
204 EXPECT_CALL(*mock_store_, Load()).Times(AnyNumber()); | 151 EXPECT_CALL(*mock_store_, Load()).Times(AnyNumber()); |
205 manager_.reset(new UserCloudPolicyManager( | 152 manager_.reset(new UserCloudPolicyManager( |
206 profile_.get(), | 153 profile_.get(), |
207 scoped_ptr<UserCloudPolicyStore>(mock_store_), | 154 scoped_ptr<UserCloudPolicyStore>(mock_store_), |
(...skipping 10 matching lines...) Expand all Loading... |
218 profile_.reset(); | 165 profile_.reset(); |
219 TestingBrowserProcess* testing_browser_process = | 166 TestingBrowserProcess* testing_browser_process = |
220 TestingBrowserProcess::GetGlobal(); | 167 TestingBrowserProcess::GetGlobal(); |
221 testing_browser_process->SetLocalState(NULL); | 168 testing_browser_process->SetLocalState(NULL); |
222 local_state_.reset(); | 169 local_state_.reset(); |
223 testing_browser_process->SetBrowserPolicyConnector(NULL); | 170 testing_browser_process->SetBrowserPolicyConnector(NULL); |
224 base::RunLoop run_loop; | 171 base::RunLoop run_loop; |
225 run_loop.RunUntilIdle(); | 172 run_loop.RunUntilIdle(); |
226 } | 173 } |
227 | 174 |
228 #if defined(OS_ANDROID) | |
229 FakeAndroidProfileOAuth2TokenService* GetTokenService() { | |
230 ProfileOAuth2TokenService* service = | |
231 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()); | |
232 return static_cast<FakeAndroidProfileOAuth2TokenService*>(service); | |
233 } | |
234 #else | |
235 FakeProfileOAuth2TokenService* GetTokenService() { | 175 FakeProfileOAuth2TokenService* GetTokenService() { |
236 ProfileOAuth2TokenService* service = | 176 ProfileOAuth2TokenService* service = |
237 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()); | 177 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()); |
238 return static_cast<FakeProfileOAuth2TokenService*>(service); | 178 return static_cast<FakeProfileOAuth2TokenService*>(service); |
239 } | 179 } |
240 #endif | |
241 | 180 |
242 bool IsRequestActive() { | 181 bool IsRequestActive() { |
243 #if defined(OS_ANDROID) | |
244 if (GetTokenService()->HasPendingRequest()) | |
245 return true; | |
246 #else | |
247 if (!GetTokenService()->GetPendingRequests().empty()) | 182 if (!GetTokenService()->GetPendingRequests().empty()) |
248 return true; | 183 return true; |
249 #endif | |
250 return url_factory_.GetFetcherByID(0); | 184 return url_factory_.GetFetcherByID(0); |
251 } | 185 } |
252 | 186 |
253 void MakeOAuthTokenFetchSucceed() { | 187 void MakeOAuthTokenFetchSucceed() { |
| 188 ASSERT_TRUE(IsRequestActive()); |
254 #if defined(OS_ANDROID) | 189 #if defined(OS_ANDROID) |
255 ASSERT_TRUE(GetTokenService()->HasPendingRequest()); | 190 GetTokenService()->IssueTokenForAllPendingRequests("access_token", |
256 GetTokenService()->IssueToken("fake_token"); | 191 base::Time::Now()); |
257 #else | 192 #else |
258 ASSERT_TRUE(IsRequestActive()); | |
259 net::TestURLFetcher* fetcher = url_factory_.GetFetcherByID(0); | 193 net::TestURLFetcher* fetcher = url_factory_.GetFetcherByID(0); |
260 fetcher->set_response_code(net::HTTP_OK); | 194 fetcher->set_response_code(net::HTTP_OK); |
261 fetcher->SetResponseString(kValidTokenResponse); | 195 fetcher->SetResponseString(kValidTokenResponse); |
262 fetcher->delegate()->OnURLFetchComplete(fetcher); | 196 fetcher->delegate()->OnURLFetchComplete(fetcher); |
263 #endif | 197 #endif |
264 } | 198 } |
265 | 199 |
266 void ReportHostedDomainStatus(bool is_hosted_domain) { | 200 void ReportHostedDomainStatus(bool is_hosted_domain) { |
267 ASSERT_TRUE(IsRequestActive()); | 201 ASSERT_TRUE(IsRequestActive()); |
268 net::TestURLFetcher* fetcher = url_factory_.GetFetcherByID(0); | 202 net::TestURLFetcher* fetcher = url_factory_.GetFetcherByID(0); |
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
619 RegisterPolicyClientWithCallback(signin_service); | 553 RegisterPolicyClientWithCallback(signin_service); |
620 Mock::VerifyAndClearExpectations(this); | 554 Mock::VerifyAndClearExpectations(this); |
621 | 555 |
622 // UserCloudPolicyManager should not be initialized. | 556 // UserCloudPolicyManager should not be initialized. |
623 ASSERT_FALSE(manager_->core()->service()); | 557 ASSERT_FALSE(manager_->core()->service()); |
624 ASSERT_TRUE(IsRequestActive()); | 558 ASSERT_TRUE(IsRequestActive()); |
625 EXPECT_FALSE(register_completed_); | 559 EXPECT_FALSE(register_completed_); |
626 | 560 |
627 // Cause the access token fetch to fail - callback should be invoked. | 561 // Cause the access token fetch to fail - callback should be invoked. |
628 #if defined(OS_ANDROID) | 562 #if defined(OS_ANDROID) |
629 ASSERT_TRUE(GetTokenService()->HasPendingRequest()); | 563 ASSERT_TRUE(!GetTokenService()->GetPendingRequests().empty()); |
630 GetTokenService()->IssueToken(""); | 564 GetTokenService()->IssueErrorForAllPendingRequests( |
| 565 GoogleServiceAuthError::FromServiceError("fail")); |
631 #else | 566 #else |
632 net::TestURLFetcher* fetcher = url_factory_.GetFetcherByID(0); | 567 net::TestURLFetcher* fetcher = url_factory_.GetFetcherByID(0); |
633 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, -1)); | 568 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, -1)); |
634 fetcher->delegate()->OnURLFetchComplete(fetcher); | 569 fetcher->delegate()->OnURLFetchComplete(fetcher); |
635 #endif | 570 #endif |
636 | 571 |
637 EXPECT_TRUE(register_completed_); | 572 EXPECT_TRUE(register_completed_); |
638 EXPECT_FALSE(created_client_.get()); | 573 EXPECT_FALSE(created_client_.get()); |
639 EXPECT_FALSE(IsRequestActive()); | 574 EXPECT_FALSE(IsRequestActive()); |
640 } | 575 } |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
841 base::RunLoop().RunUntilIdle(); | 776 base::RunLoop().RunUntilIdle(); |
842 EXPECT_FALSE(manager_->IsClientRegistered()); | 777 EXPECT_FALSE(manager_->IsClientRegistered()); |
843 #if !defined(OS_ANDROID) | 778 #if !defined(OS_ANDROID) |
844 EXPECT_FALSE(signin_manager_->IsSignoutProhibited()); | 779 EXPECT_FALSE(signin_manager_->IsSignoutProhibited()); |
845 #endif | 780 #endif |
846 } | 781 } |
847 | 782 |
848 } // namespace | 783 } // namespace |
849 | 784 |
850 } // namespace policy | 785 } // namespace policy |
OLD | NEW |