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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/ip_protection/common/ip_protection_token_direct_fetcher.h"
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/strings/strcat.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "base/time/time.h"
#include "components/ip_protection/common/ip_protection_data_types.h"
#include "components/ip_protection/common/ip_protection_token_fetcher_helper.h"
#include "components/ip_protection/common/mock_blind_sign_auth.h"
#include "net/base/features.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace ip_protection {
constexpr char kTryGetAuthTokensResultHistogram[] =
"NetworkService.IpProtection.TryGetAuthTokensResult";
constexpr char kOAuthTokenFetchHistogram[] =
"NetworkService.IpProtection.OAuthTokenFetchTime";
constexpr char kTryGetAuthTokensErrorHistogram[] =
"NetworkService.IpProtection.TryGetAuthTokensErrors";
constexpr char kTokenBatchHistogram[] =
"NetworkService.IpProtection.TokenBatchRequestTime";
const GeoHint kMountainViewGeo = {.country_code = "US",
.iso_region = "US-CA",
.city_name = "MOUNTAIN VIEW"};
const std::string kMountainViewGeoId = GetGeoIdFromGeoHint(kMountainViewGeo);
// A mock delegate for use in testing the fetcher.
struct MockIpProtectionTokenDirectFetcherDelegate
: public IpProtectionTokenDirectFetcher::Delegate {
bool IsTokenFetchEnabled() override { return is_ip_protection_enabled; }
void RequestOAuthToken(RequestOAuthTokenCallback callback) override {
std::move(callback).Run(response_result, std::move(response_access_token));
}
bool is_ip_protection_enabled = true;
TryGetAuthTokensResult response_result = TryGetAuthTokensResult::kSuccess;
std::optional<std::string> response_access_token = "access_token";
};
class IpProtectionTokenDirectFetcherTest : public testing::Test {
protected:
IpProtectionTokenDirectFetcherTest()
: expiration_time_(base::Time::Now() + base::Hours(1)),
geo_hint_({.country_code = "US",
.iso_region = "US-AL",
.city_name = "ALABASTER"}),
token_server_get_proxy_config_url_(GURL(base::StrCat(
{net::features::kIpPrivacyTokenServer.Get(),
net::features::kIpPrivacyTokenServerGetProxyConfigPath.Get()}))),
default_transient_backoff_(
net::features::kIpPrivacyTryGetAuthTokensTransientBackoff.Get()),
default_bug_backoff_(
net::features::kIpPrivacyTryGetAuthTokensBugBackoff.Get()),
default_not_eligible_backoff_(
net::features::kIpPrivacyTryGetAuthTokensNotEligibleBackoff.Get()) {
auto bsa = std::make_unique<MockBlindSignAuth>();
bsa_ = bsa.get();
fetcher_ = std::make_unique<IpProtectionTokenDirectFetcher>(
&delegate_, test_url_loader_factory_.GetSafeWeakWrapper()->Clone(),
std::move(bsa));
}
// Call `TryGetAuthTokens()` and run until it completes.
void TryGetAuthTokens(int num_tokens, ProxyLayer proxy_layer) {
fetcher_->TryGetAuthTokens(num_tokens, proxy_layer,
tokens_future_.GetCallback());
ASSERT_TRUE(tokens_future_.Wait()) << "TryGetAuthTokens did not call back";
}
// Expect that the TryGetAuthTokens call returned the given tokens.
void ExpectTryGetAuthTokensResult(
std::vector<BlindSignedAuthToken> bsa_tokens) {
EXPECT_EQ(std::get<0>(tokens_future_.Get()), bsa_tokens);
}
// Expect that the TryGetAuthTokens call returned nullopt, with
// `try_again_after` at the given delta from the current time.
void ExpectTryGetAuthTokensResultFailed(base::TimeDelta try_again_delta) {
auto& [bsa_tokens, try_again_after] = tokens_future_.Get();
EXPECT_EQ(bsa_tokens, std::nullopt);
if (!bsa_tokens) {
EXPECT_EQ(*try_again_after, base::Time::Now() + try_again_delta);
}
// Clear future so it can be reused and accept new tokens.
tokens_future_.Clear();
}
protected:
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
base::test::TestFuture<const std::optional<std::vector<BlindSignedAuthToken>>,
std::optional<base::Time>>
tokens_future_;
network::TestURLLoaderFactory test_url_loader_factory_;
base::Time expiration_time_;
// A convenient geo hint for fake tokens.
GeoHint geo_hint_;
// URL at which getProxyConfig is invoked.
GURL token_server_get_proxy_config_url_;
// Delegate for the fetcher under test.
MockIpProtectionTokenDirectFetcherDelegate delegate_;
// Fetcher under test.
std::unique_ptr<IpProtectionTokenDirectFetcher> fetcher_;
base::HistogramTester histogram_tester_;
// quiche::BlindSignAuthInterface owned and used by the fetcher.
raw_ptr<MockBlindSignAuth> bsa_;
// Default backoff times applied for calculating `try_again_after`.
base::TimeDelta default_transient_backoff_;
base::TimeDelta default_bug_backoff_;
base::TimeDelta default_not_eligible_backoff_;
};
TEST_F(IpProtectionTokenDirectFetcherTest, Success) {
bsa_->set_tokens(
{IpProtectionTokenFetcherHelper::CreateBlindSignTokenForTesting(
"single-use-1", expiration_time_, geo_hint_),
IpProtectionTokenFetcherHelper::CreateBlindSignTokenForTesting(
"single-use-2", expiration_time_, geo_hint_)});
TryGetAuthTokens(2, ProxyLayer::kProxyB);
EXPECT_TRUE(bsa_->GetTokensCalledInDifferentThread());
EXPECT_EQ(bsa_->oauth_token(), "access_token");
EXPECT_EQ(bsa_->num_tokens(), 2);
EXPECT_EQ(bsa_->proxy_layer(), quiche::ProxyLayer::kProxyB);
std::vector<BlindSignedAuthToken> expected;
expected.push_back(
IpProtectionTokenFetcherHelper::CreateMockBlindSignedAuthTokenForTesting(
"single-use-1", expiration_time_, geo_hint_)
.value());
expected.push_back(
IpProtectionTokenFetcherHelper::CreateMockBlindSignedAuthTokenForTesting(
"single-use-2", expiration_time_, geo_hint_)
.value());
ExpectTryGetAuthTokensResult(std::move(expected));
histogram_tester_.ExpectUniqueSample(kTryGetAuthTokensResultHistogram,
TryGetAuthTokensResult::kSuccess, 1);
histogram_tester_.ExpectTotalCount(kOAuthTokenFetchHistogram, 1);
histogram_tester_.ExpectTotalCount(kTokenBatchHistogram, 1);
}
// BSA returns no tokens.
TEST_F(IpProtectionTokenDirectFetcherTest, NoTokens) {
TryGetAuthTokens(1, ProxyLayer::kProxyA);
EXPECT_TRUE(bsa_->GetTokensCalledInDifferentThread());
EXPECT_EQ(bsa_->num_tokens(), 1);
EXPECT_EQ(bsa_->proxy_layer(), quiche::ProxyLayer::kProxyA);
EXPECT_EQ(bsa_->oauth_token(), "access_token");
ExpectTryGetAuthTokensResultFailed(default_transient_backoff_);
histogram_tester_.ExpectUniqueSample(kTryGetAuthTokensResultHistogram,
TryGetAuthTokensResult::kFailedBSAOther,
1);
histogram_tester_.ExpectTotalCount(kOAuthTokenFetchHistogram, 1);
histogram_tester_.ExpectTotalCount(kTokenBatchHistogram, 0);
}
// BSA returns malformed tokens.
TEST_F(IpProtectionTokenDirectFetcherTest, MalformedTokens) {
auto geo_hint = anonymous_tokens::GeoHint{
.geo_hint = "US,US-CA,MOUNTAIN VIEW",
.country_code = "US",
.region = "US-CA",
.city = "MOUNTAIN VIEW",
};
bsa_->set_tokens(
{{"invalid-token-proto-data", absl::Now() + absl::Hours(1), geo_hint}});
TryGetAuthTokens(1, ProxyLayer::kProxyB);
EXPECT_TRUE(bsa_->GetTokensCalledInDifferentThread());
EXPECT_EQ(bsa_->num_tokens(), 1);
EXPECT_EQ(bsa_->proxy_layer(), quiche::ProxyLayer::kProxyB);
EXPECT_EQ(bsa_->oauth_token(), "access_token");
ExpectTryGetAuthTokensResultFailed(default_transient_backoff_);
histogram_tester_.ExpectUniqueSample(kTryGetAuthTokensResultHistogram,
TryGetAuthTokensResult::kFailedBSAOther,
1);
histogram_tester_.ExpectTotalCount(kOAuthTokenFetchHistogram, 1);
histogram_tester_.ExpectTotalCount(kTokenBatchHistogram, 0);
}
TEST_F(IpProtectionTokenDirectFetcherTest, TokenGeoHintContainsOnlyCountry) {
GeoHint geo_hint_country;
geo_hint_country.country_code = "US";
bsa_->set_tokens(
{IpProtectionTokenFetcherHelper::CreateBlindSignTokenForTesting(
"single-use-1", expiration_time_, geo_hint_country),
IpProtectionTokenFetcherHelper::CreateBlindSignTokenForTesting(
"single-use-2", expiration_time_, geo_hint_country)});
TryGetAuthTokens(2, ProxyLayer::kProxyB);
EXPECT_TRUE(bsa_->GetTokensCalledInDifferentThread());
EXPECT_EQ(bsa_->oauth_token(), "access_token");
EXPECT_EQ(bsa_->num_tokens(), 2);
EXPECT_EQ(bsa_->proxy_layer(), quiche::ProxyLayer::kProxyB);
std::vector<BlindSignedAuthToken> expected;
expected.push_back(
IpProtectionTokenFetcherHelper::CreateMockBlindSignedAuthTokenForTesting(
"single-use-1", expiration_time_, geo_hint_country)
.value());
expected.push_back(
IpProtectionTokenFetcherHelper::CreateMockBlindSignedAuthTokenForTesting(
"single-use-2", expiration_time_, geo_hint_country)
.value());
ExpectTryGetAuthTokensResult(std::move(expected));
histogram_tester_.ExpectUniqueSample(kTryGetAuthTokensResultHistogram,
TryGetAuthTokensResult::kSuccess, 1);
histogram_tester_.ExpectTotalCount(kOAuthTokenFetchHistogram, 1);
histogram_tester_.ExpectTotalCount(kTokenBatchHistogram, 1);
}
TEST_F(IpProtectionTokenDirectFetcherTest, TokenHasMissingGeoHint) {
GeoHint geo_hint;
bsa_->set_tokens(
{IpProtectionTokenFetcherHelper::CreateBlindSignTokenForTesting(
"single-use-1", expiration_time_, geo_hint)});
TryGetAuthTokens(1, ProxyLayer::kProxyA);
EXPECT_TRUE(bsa_->GetTokensCalledInDifferentThread());
EXPECT_EQ(bsa_->num_tokens(), 1);
EXPECT_EQ(bsa_->proxy_layer(), quiche::ProxyLayer::kProxyA);
EXPECT_EQ(bsa_->oauth_token(), "access_token");
ExpectTryGetAuthTokensResultFailed(default_transient_backoff_);
histogram_tester_.ExpectUniqueSample(kTryGetAuthTokensResultHistogram,
TryGetAuthTokensResult::kFailedBSAOther,
1);
histogram_tester_.ExpectTotalCount(kOAuthTokenFetchHistogram, 1);
histogram_tester_.ExpectTotalCount(kTokenBatchHistogram, 0);
}
// BSA returns a 400 error.
TEST_F(IpProtectionTokenDirectFetcherTest, BlindSignedTokenError400) {
bsa_->set_status(absl::InvalidArgumentError("uhoh"));
TryGetAuthTokens(1, ProxyLayer::kProxyA);
EXPECT_TRUE(bsa_->GetTokensCalledInDifferentThread());
EXPECT_EQ(bsa_->num_tokens(), 1);
EXPECT_EQ(bsa_->proxy_layer(), quiche::ProxyLayer::kProxyA);
EXPECT_EQ(bsa_->oauth_token(), "access_token");
ExpectTryGetAuthTokensResultFailed(default_bug_backoff_);
histogram_tester_.ExpectUniqueSample(kTryGetAuthTokensResultHistogram,
TryGetAuthTokensResult::kFailedBSA400,
1);
histogram_tester_.ExpectUniqueSample(
kTryGetAuthTokensErrorHistogram,
4043967578, // base::PersistentHash("INVALID_ARGUMENT: uhoh")
1);
histogram_tester_.ExpectTotalCount(kOAuthTokenFetchHistogram, 1);
histogram_tester_.ExpectTotalCount(kTokenBatchHistogram, 0);
}
// BSA returns a 401 error.
TEST_F(IpProtectionTokenDirectFetcherTest, BlindSignedTokenError401) {
bsa_->set_status(absl::UnauthenticatedError("uhoh"));
TryGetAuthTokens(1, ProxyLayer::kProxyB);
EXPECT_TRUE(bsa_->GetTokensCalledInDifferentThread());
EXPECT_EQ(bsa_->num_tokens(), 1);
EXPECT_EQ(bsa_->proxy_layer(), quiche::ProxyLayer::kProxyB);
EXPECT_EQ(bsa_->oauth_token(), "access_token");
ExpectTryGetAuthTokensResultFailed(default_bug_backoff_);
histogram_tester_.ExpectUniqueSample(kTryGetAuthTokensResultHistogram,
TryGetAuthTokensResult::kFailedBSA401,
1);
histogram_tester_.ExpectUniqueSample(
kTryGetAuthTokensErrorHistogram,
4264091263, // base::PersistentHash("UNAUTHENTICATED: uhoh")
1);
histogram_tester_.ExpectTotalCount(kOAuthTokenFetchHistogram, 1);
histogram_tester_.ExpectTotalCount(kTokenBatchHistogram, 0);
}
// BSA returns a 403 error.
TEST_F(IpProtectionTokenDirectFetcherTest, BlindSignedTokenError403) {
bsa_->set_status(absl::PermissionDeniedError("uhoh"));
TryGetAuthTokens(1, ProxyLayer::kProxyA);
EXPECT_TRUE(bsa_->GetTokensCalledInDifferentThread());
EXPECT_EQ(bsa_->num_tokens(), 1);
EXPECT_EQ(bsa_->proxy_layer(), quiche::ProxyLayer::kProxyA);
EXPECT_EQ(bsa_->oauth_token(), "access_token");
ExpectTryGetAuthTokensResultFailed(default_not_eligible_backoff_);
histogram_tester_.ExpectUniqueSample(kTryGetAuthTokensResultHistogram,
TryGetAuthTokensResult::kFailedBSA403,
1);
histogram_tester_.ExpectUniqueSample(
kTryGetAuthTokensErrorHistogram,
4104528123, // base::PersistentHash("PERMISSION_DENIED: uhoh")
1);
// Failed to parse GetInitialDataResponse
histogram_tester_.ExpectTotalCount(kOAuthTokenFetchHistogram, 1);
histogram_tester_.ExpectTotalCount(kTokenBatchHistogram, 0);
}
// BSA returns some other error.
TEST_F(IpProtectionTokenDirectFetcherTest, BlindSignedTokenErrorOther) {
bsa_->set_status(absl::UnknownError("uhoh"));
TryGetAuthTokens(1, ProxyLayer::kProxyB);
EXPECT_TRUE(bsa_->GetTokensCalledInDifferentThread());
EXPECT_EQ(bsa_->num_tokens(), 1);
EXPECT_EQ(bsa_->proxy_layer(), quiche::ProxyLayer::kProxyB);
EXPECT_EQ(bsa_->oauth_token(), "access_token");
ExpectTryGetAuthTokensResultFailed(default_transient_backoff_);
histogram_tester_.ExpectUniqueSample(kTryGetAuthTokensResultHistogram,
TryGetAuthTokensResult::kFailedBSAOther,
1);
histogram_tester_.ExpectUniqueSample(
kTryGetAuthTokensErrorHistogram,
2844845398, // base::PersistentHash("UNKNOWN: uhoh")
1);
histogram_tester_.ExpectTotalCount(kOAuthTokenFetchHistogram, 1);
histogram_tester_.ExpectTotalCount(kTokenBatchHistogram, 0);
}
// Fetching OAuth token returns a transient error.
TEST_F(IpProtectionTokenDirectFetcherTest, AuthTokenTransientError) {
delegate_.response_access_token = std::nullopt;
delegate_.response_result =
TryGetAuthTokensResult::kFailedOAuthTokenTransient;
TryGetAuthTokens(1, ProxyLayer::kProxyB);
EXPECT_FALSE(bsa_->get_tokens_called());
ExpectTryGetAuthTokensResultFailed(default_transient_backoff_);
histogram_tester_.ExpectUniqueSample(
kTryGetAuthTokensResultHistogram,
TryGetAuthTokensResult::kFailedOAuthTokenTransient, 1);
}
// Fetching OAuth token returns a persistent error.
TEST_F(IpProtectionTokenDirectFetcherTest, AuthTokenPersistentError) {
delegate_.response_access_token = std::nullopt;
delegate_.response_result =
TryGetAuthTokensResult::kFailedOAuthTokenPersistent;
TryGetAuthTokens(1, ProxyLayer::kProxyA);
EXPECT_FALSE(bsa_->get_tokens_called());
ExpectTryGetAuthTokensResultFailed(base::TimeDelta::Max());
histogram_tester_.ExpectUniqueSample(
kTryGetAuthTokensResultHistogram,
TryGetAuthTokensResult::kFailedOAuthTokenPersistent, 1);
}
// No primary account.
TEST_F(IpProtectionTokenDirectFetcherTest, NoAccount) {
delegate_.response_access_token = std::nullopt;
delegate_.response_result = TryGetAuthTokensResult::kFailedNoAccount;
TryGetAuthTokens(1, ProxyLayer::kProxyB);
EXPECT_FALSE(bsa_->get_tokens_called());
ExpectTryGetAuthTokensResultFailed(base::TimeDelta::Max());
histogram_tester_.ExpectUniqueSample(kTryGetAuthTokensResultHistogram,
TryGetAuthTokensResult::kFailedNoAccount,
1);
histogram_tester_.ExpectTotalCount(kOAuthTokenFetchHistogram, 0);
histogram_tester_.ExpectTotalCount(kTokenBatchHistogram, 0);
}
// TryGetAuthTokens() fails because IP Protection is disabled by user settings.
TEST_F(IpProtectionTokenDirectFetcherTest, IpProtectionDisabled) {
delegate_.is_ip_protection_enabled = false;
TryGetAuthTokens(1, ProxyLayer::kProxyA);
EXPECT_FALSE(bsa_->get_tokens_called());
ExpectTryGetAuthTokensResultFailed(base::TimeDelta::Max());
histogram_tester_.ExpectUniqueSample(
kTryGetAuthTokensResultHistogram,
TryGetAuthTokensResult::kFailedDisabledByUser, 1);
histogram_tester_.ExpectTotalCount(kOAuthTokenFetchHistogram, 0);
histogram_tester_.ExpectTotalCount(kTokenBatchHistogram, 0);
}
// Backoff calculations.
TEST_F(IpProtectionTokenDirectFetcherTest, CalculateBackoff) {
using enum TryGetAuthTokensResult;
auto check = [&](TryGetAuthTokensResult result,
std::optional<base::TimeDelta> backoff, bool exponential) {
SCOPED_TRACE(::testing::Message()
<< "result: " << static_cast<int>(result));
EXPECT_EQ(fetcher_->CalculateBackoff(result), backoff);
if (backoff && exponential) {
EXPECT_EQ(fetcher_->CalculateBackoff(result), (*backoff) * 2);
EXPECT_EQ(fetcher_->CalculateBackoff(result), (*backoff) * 4);
} else {
EXPECT_EQ(fetcher_->CalculateBackoff(result), backoff);
}
};
check(kSuccess, std::nullopt, false);
check(kFailedNotEligible, default_not_eligible_backoff_, false);
check(kFailedBSA400, default_bug_backoff_, true);
check(kFailedBSA401, default_bug_backoff_, true);
check(kFailedBSA403, default_not_eligible_backoff_, false);
check(kFailedBSAOther, default_transient_backoff_, true);
check(kFailedOAuthTokenTransient, default_transient_backoff_, true);
check(kFailedNoAccount, base::TimeDelta::Max(), false);
// The account-related backoffs should not be changed except by account change
// events.
check(kFailedBSA400, base::TimeDelta::Max(), false);
fetcher_->AccountStatusChanged(true);
check(kFailedBSA400, default_bug_backoff_, true);
}
} // namespace ip_protection
|