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 442 443 444 445 446 447 448
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/safe_browsing/advanced_protection_status_manager_desktop.h"
#include <string>
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "build/build_config.h"
#include "components/prefs/pref_service.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "components/signin/public/identity_manager/accounts_mutator.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "components/signin/public/identity_manager/tribool.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace safe_browsing {
namespace {
static const char* kIdTokenAdvancedProtectionEnabled =
"dummy-header."
"eyAic2VydmljZXMiOiBbInRpYSJdIH0=" // payload: { "services": ["tia"] }
".dummy-signature";
static const char* kIdTokenAdvancedProtectionDisabled =
"dummy-header."
"eyAic2VydmljZXMiOiBbXSB9" // payload: { "services": [] }
".dummy-signature";
static const char* kAPEnabledMetric =
"SafeBrowsing.Desktop.AdvancedProtection.Enabled";
// Helper class that ensure RegisterProfilePrefs() is called on the test
// PrefService's registry before the IdentityTestEnvironment constructor
// is invoked.
class TestWithPrefService : public testing::Test {
public:
TestWithPrefService() { RegisterProfilePrefs(pref_service_.registry()); }
protected:
base::test::TaskEnvironment task_environment;
sync_preferences::TestingPrefServiceSyncable pref_service_;
};
class AdvancedProtectionStatusManagerDesktopTest : public TestWithPrefService {
public:
AdvancedProtectionStatusManagerDesktopTest()
: identity_test_env_(/*test_url_loader_factory=*/nullptr,
&pref_service_) {}
CoreAccountId SignIn(const std::string& email,
bool is_under_advanced_protection) {
AccountInfo account_info = identity_test_env_.MakeAccountAvailable(email);
account_info.is_under_advanced_protection = is_under_advanced_protection;
identity_test_env_.SetPrimaryAccount(account_info.email,
signin::ConsentLevel::kSync);
identity_test_env_.UpdateAccountInfoForAccount(account_info);
return account_info.account_id;
}
void MakeOAuthTokenFetchSucceed(const CoreAccountId& account_id,
bool is_under_advanced_protection) {
identity_test_env_.WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
account_id, "access_token", base::Time::Now() + base::Hours(1),
is_under_advanced_protection ? kIdTokenAdvancedProtectionEnabled
: kIdTokenAdvancedProtectionDisabled);
}
void MakeOAuthTokenFetchFail(const CoreAccountId& account_id,
bool is_transient_error) {
identity_test_env_.WaitForAccessTokenRequestIfNecessaryAndRespondWithError(
account_id,
GoogleServiceAuthError(
is_transient_error
? GoogleServiceAuthError::CONNECTION_FAILED
: GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS));
}
protected:
signin::IdentityTestEnvironment identity_test_env_;
};
} // namespace
TEST_F(AdvancedProtectionStatusManagerDesktopTest, NotSignedInOnStartUp) {
base::HistogramTester histograms;
ASSERT_FALSE(
pref_service_.HasPrefPath(prefs::kAdvancedProtectionLastRefreshInUs));
AdvancedProtectionStatusManagerDesktop aps_manager(
&pref_service_, identity_test_env_.identity_manager(),
base::TimeDelta() /*no min delay*/);
ASSERT_TRUE(aps_manager.GetUnconsentedPrimaryAccountId().empty());
// If user's not signed-in. No refresh is required.
EXPECT_FALSE(aps_manager.IsUnderAdvancedProtection());
EXPECT_FALSE(aps_manager.IsRefreshScheduled());
EXPECT_FALSE(
pref_service_.HasPrefPath(prefs::kAdvancedProtectionLastRefreshInUs));
aps_manager.UnsubscribeFromSigninEvents();
EXPECT_THAT(histograms.GetAllSamples(kAPEnabledMetric),
testing::ElementsAre(base::Bucket(false, 1)));
}
TEST_F(AdvancedProtectionStatusManagerDesktopTest,
SignedInLongTimeAgoRefreshFailTransientError) {
base::HistogramTester histograms;
ASSERT_FALSE(
pref_service_.HasPrefPath(prefs::kAdvancedProtectionLastRefreshInUs));
// Simulates the situation where user signed in long time ago, thus
// has no advanced protection status.
CoreAccountId account_id = SignIn("test@test.com",
/* is_under_advanced_protection = */ false);
AdvancedProtectionStatusManagerDesktop aps_manager(
&pref_service_, identity_test_env_.identity_manager(),
base::TimeDelta() /*no min delay*/);
ASSERT_FALSE(aps_manager.GetUnconsentedPrimaryAccountId().empty());
// Waits for access token request and respond with an error without advanced
// protection set.
MakeOAuthTokenFetchFail(account_id,
/* is_transient_error = */ true);
EXPECT_FALSE(aps_manager.IsUnderAdvancedProtection());
// A retry should be scheduled.
EXPECT_TRUE(aps_manager.IsRefreshScheduled());
EXPECT_FALSE(
pref_service_.HasPrefPath(prefs::kAdvancedProtectionLastRefreshInUs));
aps_manager.UnsubscribeFromSigninEvents();
EXPECT_THAT(histograms.GetAllSamples(kAPEnabledMetric),
testing::ElementsAre(base::Bucket(false, 1)));
}
TEST_F(AdvancedProtectionStatusManagerDesktopTest,
SignedInLongTimeAgoRefreshFailNonTransientError) {
base::HistogramTester histograms;
// Simulates the situation where user signed in long time ago, thus
// has no advanced protection status.
CoreAccountId account_id = SignIn("test@test.com",
/* is_under_advanced_protection = */ false);
AdvancedProtectionStatusManagerDesktop aps_manager(
&pref_service_, identity_test_env_.identity_manager(),
base::TimeDelta() /*no min delay*/);
ASSERT_FALSE(aps_manager.GetUnconsentedPrimaryAccountId().empty());
// Waits for access token request and respond with an error without advanced
// protection set.
MakeOAuthTokenFetchFail(account_id,
/* is_transient_error = */ false);
EXPECT_FALSE(aps_manager.IsUnderAdvancedProtection());
// No retry should be scheduled.
EXPECT_FALSE(aps_manager.IsRefreshScheduled());
aps_manager.UnsubscribeFromSigninEvents();
EXPECT_THAT(histograms.GetAllSamples(kAPEnabledMetric),
testing::ElementsAre(base::Bucket(false, 1)));
}
TEST_F(AdvancedProtectionStatusManagerDesktopTest,
SignedInLongTimeAgoNotUnderAP) {
ASSERT_FALSE(
pref_service_.HasPrefPath(prefs::kAdvancedProtectionLastRefreshInUs));
base::HistogramTester histograms;
// Simulates the situation where user signed in long time ago, thus
// has no advanced protection status.
CoreAccountId account_id = SignIn("test@test.com",
/* is_under_advanced_protection = */ false);
AdvancedProtectionStatusManagerDesktop aps_manager(
&pref_service_, identity_test_env_.identity_manager(),
base::TimeDelta() /*no min delay*/);
ASSERT_FALSE(aps_manager.GetUnconsentedPrimaryAccountId().empty());
// Waits for access token request and respond with a token without advanced
// protection set.
MakeOAuthTokenFetchSucceed(account_id,
/* is_under_advanced_protection = */ false);
EXPECT_FALSE(aps_manager.IsUnderAdvancedProtection());
EXPECT_FALSE(aps_manager.IsRefreshScheduled());
EXPECT_TRUE(
pref_service_.HasPrefPath(prefs::kAdvancedProtectionLastRefreshInUs));
aps_manager.UnsubscribeFromSigninEvents();
EXPECT_THAT(histograms.GetAllSamples(kAPEnabledMetric),
testing::ElementsAre(base::Bucket(false, 1)));
}
TEST_F(AdvancedProtectionStatusManagerDesktopTest, SignedInLongTimeAgoUnderAP) {
base::HistogramTester histograms;
// Simulates the situation where user signed in long time ago, thus
// has no advanced protection status yet.
CoreAccountId account_id = SignIn("test@test.com",
/* is_under_advanced_protection = */ false);
AdvancedProtectionStatusManagerDesktop aps_manager(
&pref_service_, identity_test_env_.identity_manager(),
base::TimeDelta() /*no min delay*/);
// Waits for access token request and respond with a token without advanced
// protection set.
MakeOAuthTokenFetchSucceed(account_id,
/* is_under_advanced_protection = */ true);
EXPECT_TRUE(aps_manager.IsUnderAdvancedProtection());
EXPECT_TRUE(aps_manager.IsRefreshScheduled());
EXPECT_TRUE(
pref_service_.HasPrefPath(prefs::kAdvancedProtectionLastRefreshInUs));
aps_manager.UnsubscribeFromSigninEvents();
EXPECT_THAT(histograms.GetAllSamples(kAPEnabledMetric),
testing::ElementsAre(base::Bucket(false, 1)));
}
TEST_F(AdvancedProtectionStatusManagerDesktopTest, AlreadySignedInAndUnderAP) {
base::HistogramTester histograms;
pref_service_.SetInt64(
prefs::kAdvancedProtectionLastRefreshInUs,
base::Time::Now().ToDeltaSinceWindowsEpoch().InMicroseconds());
// Simulates the situation where the user has already signed in and is
// under advanced protection.
CoreAccountId account_id = SignIn("test@test.com",
/* is_under_advanced_protection = */ true);
AdvancedProtectionStatusManagerDesktop aps_manager(
&pref_service_, identity_test_env_.identity_manager(),
base::TimeDelta() /*no min delay*/);
ASSERT_FALSE(aps_manager.GetUnconsentedPrimaryAccountId().empty());
ASSERT_TRUE(aps_manager.IsUnderAdvancedProtection());
// A refresh is scheduled in the future.
EXPECT_TRUE(aps_manager.IsRefreshScheduled());
aps_manager.UnsubscribeFromSigninEvents();
EXPECT_THAT(histograms.GetAllSamples(kAPEnabledMetric),
testing::ElementsAre(base::Bucket(true, 1)));
}
TEST_F(AdvancedProtectionStatusManagerDesktopTest,
AlreadySignedInAndNotUnderAP) {
base::HistogramTester histograms;
pref_service_.SetInt64(
prefs::kAdvancedProtectionLastRefreshInUs,
base::Time::Now().ToDeltaSinceWindowsEpoch().InMicroseconds());
// Simulates the situation where the user has already signed in and is
// NOT under advanced protection.
CoreAccountId account_id = SignIn("test@test.com",
/* is_under_advanced_protection = */ false);
AdvancedProtectionStatusManagerDesktop aps_manager(
&pref_service_, identity_test_env_.identity_manager(),
base::TimeDelta() /*no min delay*/);
// Incognito profile should share the advanced protection status with the
// original profile.
EXPECT_FALSE(aps_manager.IsUnderAdvancedProtection());
aps_manager.UnsubscribeFromSigninEvents();
EXPECT_THAT(histograms.GetAllSamples(kAPEnabledMetric),
testing::ElementsAre(base::Bucket(false, 1)));
}
TEST_F(AdvancedProtectionStatusManagerDesktopTest, StayInAdvancedProtection) {
base::HistogramTester histograms;
base::Time last_update = base::Time::Now();
pref_service_.SetInt64(
prefs::kAdvancedProtectionLastRefreshInUs,
last_update.ToDeltaSinceWindowsEpoch().InMicroseconds());
CoreAccountId account_id = SignIn("test@test.com",
/* is_under_advanced_protection = */ true);
AdvancedProtectionStatusManagerDesktop aps_manager(
&pref_service_, identity_test_env_.identity_manager(),
base::TimeDelta() /*no min delay*/);
ASSERT_FALSE(aps_manager.GetUnconsentedPrimaryAccountId().empty());
ASSERT_TRUE(aps_manager.IsUnderAdvancedProtection());
// Simulate gets refresh token.
aps_manager.OnGetIDToken(account_id, kIdTokenAdvancedProtectionEnabled);
EXPECT_GT(
base::Time::FromDeltaSinceWindowsEpoch(base::Microseconds(
pref_service_.GetInt64(prefs::kAdvancedProtectionLastRefreshInUs))),
last_update);
EXPECT_TRUE(aps_manager.IsRefreshScheduled());
aps_manager.UnsubscribeFromSigninEvents();
EXPECT_THAT(histograms.GetAllSamples(kAPEnabledMetric),
testing::ElementsAre(base::Bucket(true, 1)));
}
#if !BUILDFLAG(IS_CHROMEOS)
// Not applicable to Chrome OS.
TEST_F(AdvancedProtectionStatusManagerDesktopTest, SignInAndSignOutEvent) {
base::HistogramTester histograms;
AdvancedProtectionStatusManagerDesktop aps_manager(
&pref_service_, identity_test_env_.identity_manager(),
base::TimeDelta() /*no min delay*/);
ASSERT_FALSE(aps_manager.IsUnderAdvancedProtection());
ASSERT_TRUE(aps_manager.GetUnconsentedPrimaryAccountId().empty());
EXPECT_THAT(histograms.GetAllSamples(kAPEnabledMetric),
testing::ElementsAre(base::Bucket(false, 1)));
SignIn("test@test.com",
/* is_under_advanced_protection = */ true);
EXPECT_TRUE(aps_manager.IsUnderAdvancedProtection());
EXPECT_TRUE(aps_manager.IsRefreshScheduled());
identity_test_env_.ClearPrimaryAccount();
EXPECT_FALSE(aps_manager.IsUnderAdvancedProtection());
EXPECT_TRUE(
pref_service_.HasPrefPath(prefs::kAdvancedProtectionLastRefreshInUs));
EXPECT_FALSE(aps_manager.IsRefreshScheduled());
aps_manager.UnsubscribeFromSigninEvents();
}
#endif
TEST_F(AdvancedProtectionStatusManagerDesktopTest, AccountRemoval) {
base::HistogramTester histograms;
AdvancedProtectionStatusManagerDesktop aps_manager(
&pref_service_, identity_test_env_.identity_manager(),
base::TimeDelta() /*no min delay*/);
ASSERT_FALSE(aps_manager.IsUnderAdvancedProtection());
ASSERT_TRUE(aps_manager.GetUnconsentedPrimaryAccountId().empty());
CoreAccountId account_id = SignIn("test@test.com",
/* is_under_advanced_protection = */ false);
EXPECT_FALSE(aps_manager.IsUnderAdvancedProtection());
EXPECT_FALSE(aps_manager.IsRefreshScheduled());
EXPECT_THAT(histograms.GetAllSamples(kAPEnabledMetric),
testing::ElementsAre(base::Bucket(false, 1)));
// Simulates account update.
identity_test_env_.identity_manager()
->GetAccountsMutator()
->UpdateAccountInfo(
account_id,
/*is_child_account=*/signin::Tribool::kUnknown,
/*is_under_advanced_protection=*/signin::Tribool::kTrue);
EXPECT_TRUE(aps_manager.IsUnderAdvancedProtection());
EXPECT_TRUE(aps_manager.IsRefreshScheduled());
// This call is necessary to ensure that the account removal is fully
// processed in this testing context.
identity_test_env_.EnableRemovalOfExtendedAccountInfo();
identity_test_env_.RemoveRefreshTokenForAccount(account_id);
EXPECT_FALSE(aps_manager.IsUnderAdvancedProtection());
EXPECT_TRUE(
pref_service_.HasPrefPath(prefs::kAdvancedProtectionLastRefreshInUs));
EXPECT_FALSE(aps_manager.IsRefreshScheduled());
aps_manager.UnsubscribeFromSigninEvents();
}
TEST_F(AdvancedProtectionStatusManagerDesktopTest,
AdvancedProtectionDisabledAfterSignin) {
base::HistogramTester histograms;
AdvancedProtectionStatusManagerDesktop aps_manager(
&pref_service_, identity_test_env_.identity_manager(),
base::TimeDelta() /*no min delay*/);
// There is no account, so the timer should not run at startup.
EXPECT_FALSE(aps_manager.IsRefreshScheduled());
CoreAccountId account_id = SignIn("test@test.com",
/* is_under_advanced_protection = */ true);
// Now that we've signed into Advanced Protection, we should have a scheduled
// refresh.
EXPECT_TRUE(aps_manager.IsUnderAdvancedProtection());
EXPECT_TRUE(aps_manager.IsRefreshScheduled());
// Skip the 24 hour wait, and try to refresh the token now.
aps_manager.timer_.FireNow();
MakeOAuthTokenFetchSucceed(account_id,
/* is_under_advanced_protection = */ false);
EXPECT_FALSE(aps_manager.IsUnderAdvancedProtection());
EXPECT_FALSE(aps_manager.IsRefreshScheduled());
aps_manager.UnsubscribeFromSigninEvents();
}
TEST_F(AdvancedProtectionStatusManagerDesktopTest,
StartupAfterLongWaitRefreshesImmediately) {
base::HistogramTester histograms;
CoreAccountId account_id = SignIn("test@test.com",
/* is_under_advanced_protection = */ true);
base::Time last_refresh_time = base::Time::Now() - base::Days(1);
pref_service_.SetInt64(
prefs::kAdvancedProtectionLastRefreshInUs,
last_refresh_time.ToDeltaSinceWindowsEpoch().InMicroseconds());
AdvancedProtectionStatusManagerDesktop aps_manager(
&pref_service_, identity_test_env_.identity_manager(),
base::TimeDelta() /*no min delay*/);
ASSERT_FALSE(aps_manager.GetUnconsentedPrimaryAccountId().empty());
ASSERT_TRUE(aps_manager.IsUnderAdvancedProtection());
EXPECT_TRUE(aps_manager.IsRefreshScheduled());
MakeOAuthTokenFetchSucceed(account_id,
/* is_under_advanced_protection = */ false);
EXPECT_FALSE(aps_manager.IsUnderAdvancedProtection());
EXPECT_FALSE(aps_manager.IsRefreshScheduled());
aps_manager.UnsubscribeFromSigninEvents();
}
// On ChromeOS, there is no unconsented primary account. We can only track the
// primary account.
#if !BUILDFLAG(IS_CHROMEOS)
TEST_F(AdvancedProtectionStatusManagerDesktopTest,
TracksUnconsentedPrimaryAccount) {
base::HistogramTester histograms;
AdvancedProtectionStatusManagerDesktop aps_manager(
&pref_service_, identity_test_env_.identity_manager(),
base::TimeDelta() /*no min delay*/);
ASSERT_FALSE(aps_manager.IsUnderAdvancedProtection());
ASSERT_TRUE(aps_manager.GetUnconsentedPrimaryAccountId().empty());
// Sign in, but don't set this as the primary account.
AccountInfo account_info = identity_test_env_.MakePrimaryAccountAvailable(
"test@test.com", signin::ConsentLevel::kSignin);
account_info.is_under_advanced_protection = true;
identity_test_env_.UpdateAccountInfoForAccount(account_info);
EXPECT_TRUE(aps_manager.IsUnderAdvancedProtection());
EXPECT_TRUE(aps_manager.IsRefreshScheduled());
aps_manager.UnsubscribeFromSigninEvents();
}
#endif
} // namespace safe_browsing
|