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

Side by Side Diff: google_apis/gaia/oauth2_token_service_request_unittest.cc

Issue 458753006: Fix use after free bug by calling GetTokenService in Core's ctor. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update TSP comment (CANDIDATE). Created 6 years, 4 months 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "google_apis/gaia/oauth2_token_service_request.h" 5 #include "google_apis/gaia/oauth2_token_service_request.h"
6 6
7 #include <set> 7 #include <set>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 #include "base/threading/thread.h" 10 #include "base/threading/thread.h"
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 class Provider : public OAuth2TokenServiceRequest::TokenServiceProvider { 152 class Provider : public OAuth2TokenServiceRequest::TokenServiceProvider {
153 public: 153 public:
154 Provider(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, 154 Provider(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
155 OAuth2TokenService* token_service); 155 OAuth2TokenService* token_service);
156 156
157 virtual scoped_refptr<base::SingleThreadTaskRunner> 157 virtual scoped_refptr<base::SingleThreadTaskRunner>
158 GetTokenServiceTaskRunner() OVERRIDE; 158 GetTokenServiceTaskRunner() OVERRIDE;
159 virtual OAuth2TokenService* GetTokenService() OVERRIDE; 159 virtual OAuth2TokenService* GetTokenService() OVERRIDE;
160 160
161 private: 161 private:
162 virtual ~Provider();
163
162 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 164 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
163 OAuth2TokenService* token_service_; 165 OAuth2TokenService* token_service_;
164 }; 166 };
165 167
166 base::MessageLoop ui_loop_; 168 base::MessageLoop ui_loop_;
167 OAuth2TokenService::ScopeSet scopes_; 169 OAuth2TokenService::ScopeSet scopes_;
168 scoped_ptr<MockOAuth2TokenService> oauth2_service_; 170 scoped_ptr<MockOAuth2TokenService> oauth2_service_;
169 scoped_ptr<OAuth2TokenServiceRequest::TokenServiceProvider> provider_; 171 scoped_refptr<OAuth2TokenServiceRequest::TokenServiceProvider> provider_;
170 TestingOAuth2TokenServiceConsumer consumer_; 172 TestingOAuth2TokenServiceConsumer consumer_;
171 }; 173 };
172 174
173 void OAuth2TokenServiceRequestTest::SetUp() { 175 void OAuth2TokenServiceRequestTest::SetUp() {
174 scopes_.insert(kScope); 176 scopes_.insert(kScope);
175 oauth2_service_.reset(new MockOAuth2TokenService); 177 oauth2_service_.reset(new MockOAuth2TokenService);
176 oauth2_service_->AddAccount(kAccountId); 178 oauth2_service_->AddAccount(kAccountId);
177 provider_.reset( 179 provider_ =
178 new Provider(base::MessageLoopProxy::current(), oauth2_service_.get())); 180 new Provider(base::MessageLoopProxy::current(), oauth2_service_.get());
179 } 181 }
180 182
181 void OAuth2TokenServiceRequestTest::TearDown() { 183 void OAuth2TokenServiceRequestTest::TearDown() {
182 // Run the loop to execute any pending tasks that may free resources. 184 // Run the loop to execute any pending tasks that may free resources.
183 ui_loop_.RunUntilIdle(); 185 ui_loop_.RunUntilIdle();
184 } 186 }
185 187
186 OAuth2TokenServiceRequestTest::Provider::Provider( 188 OAuth2TokenServiceRequestTest::Provider::Provider(
187 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, 189 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
188 OAuth2TokenService* token_service) 190 OAuth2TokenService* token_service)
189 : task_runner_(task_runner), token_service_(token_service) { 191 : task_runner_(task_runner), token_service_(token_service) {
190 } 192 }
191 193
192 scoped_refptr<base::SingleThreadTaskRunner> 194 scoped_refptr<base::SingleThreadTaskRunner>
193 OAuth2TokenServiceRequestTest::Provider::GetTokenServiceTaskRunner() { 195 OAuth2TokenServiceRequestTest::Provider::GetTokenServiceTaskRunner() {
194 return task_runner_; 196 return task_runner_;
195 } 197 }
196 198
197 OAuth2TokenService* OAuth2TokenServiceRequestTest::Provider::GetTokenService() { 199 OAuth2TokenService* OAuth2TokenServiceRequestTest::Provider::GetTokenService() {
198 return token_service_; 200 return token_service_;
199 } 201 }
200 202
203 OAuth2TokenServiceRequestTest::Provider::~Provider() {
204 }
205
201 TEST_F(OAuth2TokenServiceRequestTest, CreateAndStart_Failure) { 206 TEST_F(OAuth2TokenServiceRequestTest, CreateAndStart_Failure) {
202 oauth2_service_->SetResponse( 207 oauth2_service_->SetResponse(
203 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE), 208 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE),
204 std::string(), 209 std::string(),
205 base::Time()); 210 base::Time());
206 scoped_ptr<OAuth2TokenServiceRequest> request( 211 scoped_ptr<OAuth2TokenServiceRequest> request(
207 OAuth2TokenServiceRequest::CreateAndStart( 212 OAuth2TokenServiceRequest::CreateAndStart(
208 provider_.get(), kAccountId, scopes_, &consumer_)); 213 provider_.get(), kAccountId, scopes_, &consumer_));
209 ui_loop_.RunUntilIdle(); 214 ui_loop_.RunUntilIdle();
210 EXPECT_EQ(0, consumer_.num_get_token_success_); 215 EXPECT_EQ(0, consumer_.num_get_token_success_);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 OAuth2TokenServiceRequest::InvalidateToken( 259 OAuth2TokenServiceRequest::InvalidateToken(
255 provider_.get(), kAccountId, scopes_, kAccessToken); 260 provider_.get(), kAccountId, scopes_, kAccessToken);
256 ui_loop_.RunUntilIdle(); 261 ui_loop_.RunUntilIdle();
257 EXPECT_EQ(0, consumer_.num_get_token_success_); 262 EXPECT_EQ(0, consumer_.num_get_token_success_);
258 EXPECT_EQ(0, consumer_.num_get_token_failure_); 263 EXPECT_EQ(0, consumer_.num_get_token_failure_);
259 EXPECT_EQ(kAccessToken, oauth2_service_->last_token_invalidated()); 264 EXPECT_EQ(kAccessToken, oauth2_service_->last_token_invalidated());
260 EXPECT_EQ(1, oauth2_service_->num_invalidate_token()); 265 EXPECT_EQ(1, oauth2_service_->num_invalidate_token());
261 } 266 }
262 267
263 } // namespace 268 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698