1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "google_apis/common/request_sender.h"
#include <utility>
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "google_apis/common/base_requests.h"
#include "google_apis/common/dummy_auth_service.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace google_apis {
namespace {
const char kTestRefreshToken[] = "valid-refresh-token";
const char kTestAccessToken[] = "valid-access-token";
// Enum for indicating the reason why a request is finished.
enum FinishReason {
NONE,
SUCCESS,
CANCEL,
AUTH_FAILURE,
};
// AuthService for testing purpose. It accepts kTestRefreshToken and returns
// kTestAccessToken + {"1", "2", "3", ...}.
class TestAuthService : public DummyAuthService {
public:
TestAuthService() : auth_try_count_(0) {}
void StartAuthentication(AuthStatusCallback callback) override {
// RequestSender should clear the rejected access token before starting
// to request another one.
EXPECT_FALSE(HasAccessToken());
++auth_try_count_;
if (refresh_token() == kTestRefreshToken) {
const std::string token =
kTestAccessToken + base::NumberToString(auth_try_count_);
set_access_token(token);
std::move(callback).Run(HTTP_SUCCESS, token);
} else {
set_access_token("");
std::move(callback).Run(HTTP_UNAUTHORIZED, "");
}
}
private:
int auth_try_count_;
};
// The main test fixture class.
class RequestSenderTest : public testing::Test {
protected:
RequestSenderTest()
: request_sender_(std::make_unique<TestAuthService>(),
nullptr,
nullptr,
"dummy-user-agent",
TRAFFIC_ANNOTATION_FOR_TESTS),
auth_service_(
static_cast<TestAuthService*>(request_sender_.auth_service())) {
auth_service_->set_refresh_token(kTestRefreshToken);
auth_service_->set_access_token(kTestAccessToken);
}
RequestSender request_sender_;
raw_ptr<TestAuthService> auth_service_; // Owned by |request_sender_|.
};
// Minimal implementation for AuthenticatedRequestInterface that can interact
// with RequestSender correctly.
class TestRequest : public AuthenticatedRequestInterface {
public:
TestRequest(RequestSender* sender,
bool* start_called,
FinishReason* finish_reason)
: sender_(sender),
start_called_(start_called),
finish_reason_(finish_reason) {}
// Test the situation that the request has finished.
void FinishRequestWithSuccess() {
*finish_reason_ = SUCCESS;
sender_->RequestFinished(this);
}
const std::string& passed_access_token() const {
return passed_access_token_;
}
const ReAuthenticateCallback& passed_reauth_callback() const {
return passed_reauth_callback_;
}
void Start(const std::string& access_token,
const std::string& custom_user_agent,
ReAuthenticateCallback callback) override {
*start_called_ = true;
passed_access_token_ = access_token;
passed_reauth_callback_ = std::move(callback);
// This request class itself does not return any response at this point.
// Each test case should respond properly by using the above methods.
}
void Cancel() override {
EXPECT_TRUE(*start_called_);
*finish_reason_ = CANCEL;
sender_->RequestFinished(this);
}
void OnAuthFailed(ApiErrorCode code) override {
*finish_reason_ = AUTH_FAILURE;
sender_->RequestFinished(this);
}
base::WeakPtr<AuthenticatedRequestInterface> GetWeakPtr() override {
return weak_ptr_factory_.GetWeakPtr();
}
private:
raw_ptr<RequestSender> sender_;
raw_ptr<bool> start_called_;
raw_ptr<FinishReason> finish_reason_;
std::string passed_access_token_;
ReAuthenticateCallback passed_reauth_callback_;
base::WeakPtrFactory<TestRequest> weak_ptr_factory_{this};
};
} // namespace
TEST_F(RequestSenderTest, StartAndFinishRequest) {
bool start_called = false;
FinishReason finish_reason = NONE;
std::unique_ptr<TestRequest> request = std::make_unique<TestRequest>(
&request_sender_, &start_called, &finish_reason);
TestRequest* request_ptr = request.get();
base::WeakPtr<AuthenticatedRequestInterface> weak_ptr =
request_ptr->GetWeakPtr();
base::OnceClosure cancel_closure =
request_sender_.StartRequestWithAuthRetry(std::move(request));
EXPECT_TRUE(!cancel_closure.is_null());
// Start is called with the specified access token. Let it succeed.
EXPECT_TRUE(start_called);
EXPECT_EQ(kTestAccessToken, request_ptr->passed_access_token());
request_ptr->FinishRequestWithSuccess();
EXPECT_FALSE(weak_ptr); // The request object is deleted.
// It is safe to run the cancel closure even after the request is finished.
// It is just no-op. The TestRequest::Cancel method is not called.
std::move(cancel_closure).Run();
EXPECT_EQ(SUCCESS, finish_reason);
}
TEST_F(RequestSenderTest, StartAndCancelRequest) {
bool start_called = false;
FinishReason finish_reason = NONE;
std::unique_ptr<TestRequest> request = std::make_unique<TestRequest>(
&request_sender_, &start_called, &finish_reason);
base::WeakPtr<AuthenticatedRequestInterface> weak_ptr = request->GetWeakPtr();
base::OnceClosure cancel_closure =
request_sender_.StartRequestWithAuthRetry(std::move(request));
EXPECT_TRUE(!cancel_closure.is_null());
EXPECT_TRUE(start_called);
std::move(cancel_closure).Run();
EXPECT_EQ(CANCEL, finish_reason);
EXPECT_FALSE(weak_ptr); // The request object is deleted.
}
TEST_F(RequestSenderTest, NoRefreshToken) {
auth_service_->ClearRefreshToken();
auth_service_->ClearAccessToken();
bool start_called = false;
FinishReason finish_reason = NONE;
std::unique_ptr<TestRequest> request = std::make_unique<TestRequest>(
&request_sender_, &start_called, &finish_reason);
base::WeakPtr<AuthenticatedRequestInterface> weak_ptr = request->GetWeakPtr();
base::OnceClosure cancel_closure =
request_sender_.StartRequestWithAuthRetry(std::move(request));
EXPECT_TRUE(!cancel_closure.is_null());
// The request is not started at all because no access token is obtained.
EXPECT_FALSE(start_called);
EXPECT_EQ(AUTH_FAILURE, finish_reason);
EXPECT_FALSE(weak_ptr); // The request object is deleted.
}
TEST_F(RequestSenderTest, ValidRefreshTokenAndNoAccessToken) {
auth_service_->ClearAccessToken();
bool start_called = false;
FinishReason finish_reason = NONE;
std::unique_ptr<TestRequest> request = std::make_unique<TestRequest>(
&request_sender_, &start_called, &finish_reason);
TestRequest* request_ptr = request.get();
base::WeakPtr<AuthenticatedRequestInterface> weak_ptr =
request_ptr->GetWeakPtr();
base::OnceClosure cancel_closure =
request_sender_.StartRequestWithAuthRetry(std::move(request));
EXPECT_TRUE(!cancel_closure.is_null());
// Access token should indicate that this is the first retry.
EXPECT_TRUE(start_called);
EXPECT_EQ(kTestAccessToken + std::string("1"),
request_ptr->passed_access_token());
request_ptr->FinishRequestWithSuccess();
EXPECT_EQ(SUCCESS, finish_reason);
EXPECT_FALSE(weak_ptr); // The request object is deleted.
}
TEST_F(RequestSenderTest, AccessTokenRejectedSeveralTimes) {
bool start_called = false;
FinishReason finish_reason = NONE;
std::unique_ptr<TestRequest> request = std::make_unique<TestRequest>(
&request_sender_, &start_called, &finish_reason);
TestRequest* request_ptr = request.get();
base::WeakPtr<AuthenticatedRequestInterface> weak_ptr =
request_ptr->GetWeakPtr();
base::OnceClosure cancel_closure =
request_sender_.StartRequestWithAuthRetry(std::move(request));
EXPECT_TRUE(!cancel_closure.is_null());
EXPECT_TRUE(start_called);
EXPECT_EQ(kTestAccessToken, request_ptr->passed_access_token());
// Emulate the case that the access token was rejected by the remote service.
request_ptr->passed_reauth_callback().Run(request_ptr);
// New access token is fetched. Let it fail once again.
EXPECT_EQ(kTestAccessToken + std::string("1"),
request_ptr->passed_access_token());
request_ptr->passed_reauth_callback().Run(request_ptr);
// Once more.
EXPECT_EQ(kTestAccessToken + std::string("2"),
request_ptr->passed_access_token());
request_ptr->passed_reauth_callback().Run(request_ptr);
// Currently, limit for the retry is controlled in each request object, not
// by the RequestSender. So with this TestRequest, RequestSender retries
// infinitely. Let it succeed/
EXPECT_EQ(kTestAccessToken + std::string("3"),
request_ptr->passed_access_token());
request_ptr->FinishRequestWithSuccess();
EXPECT_EQ(SUCCESS, finish_reason);
EXPECT_FALSE(weak_ptr);
}
} // namespace google_apis
|