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
|
// Copyright 2023 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/test/supervised_user/family_live_test.h"
#include <ostream>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/notreached.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_test_util.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/supervised_user/supervised_user_service_factory.h"
#include "chrome/browser/sync/sync_service_factory.h"
#include "chrome/browser/sync/test/integration/invalidations/invalidations_status_checker.h"
#include "chrome/browser/sync/test/integration/single_client_status_change_checker.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/supervised_user/browser_user.h"
#include "components/signin/public/base/signin_metrics.h"
#include "components/signin/public/identity_manager/test_accounts.h"
#include "components/supervised_user/core/browser/proto/kidsmanagement_messages.pb.h"
#include "components/supervised_user/test_support/account_repository.h"
#include "components/supervised_user/test_support/family_link_settings_state_management.h"
#include "content/public/browser/storage_partition.h"
#include "google_apis/gaia/gaia_urls.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "net/dns/mock_host_resolver.h"
#include "ui/base/interaction/interactive_test_internal.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "url/gurl.h"
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/signin/signin_view_controller.h"
#endif // BUILDFLAG(ENABLE_DICE_SUPPORT)
namespace supervised_user {
namespace {
// When enabled the tests explicitly wait for sync invalidation to be ready.
const char* kWaitForSyncInvalidationReadySwitch =
"supervised-tests-wait-for-sync-invalidation-ready";
// When enabled, the browser opens extra debugging tabs & the logging is more
// detailed.
const char* kDebugSwitch = "supervised-tests-debug-features";
bool IsSwitchEnabled(const char* flag) {
return base::CommandLine::ForCurrentProcess()->HasSwitch(flag);
}
Profile& CreateNewProfile() {
ProfileManager* profile_manager = g_browser_process->profile_manager();
base::FilePath profile_path =
profile_manager->GenerateNextProfileDirectoryPath();
return profiles::testing::CreateProfileSync(profile_manager, profile_path);
}
bool HasAuthError(syncer::SyncServiceImpl* service) {
return service->GetAuthError().state() ==
GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS ||
service->GetAuthError().state() ==
GoogleServiceAuthError::SERVICE_ERROR ||
service->GetAuthError().state() ==
GoogleServiceAuthError::REQUEST_CANCELED;
}
class SyncSetupChecker : public SingleClientStatusChangeChecker {
public:
explicit SyncSetupChecker(syncer::SyncServiceImpl* service)
: SingleClientStatusChangeChecker(service) {}
bool IsExitConditionSatisfied(std::ostream* os) override {
*os << "Waiting for sync setup to complete";
if (service()->GetTransportState() ==
syncer::SyncService::TransportState::ACTIVE &&
service()->IsSyncFeatureActive()) {
return true;
}
// Sync is blocked by an auth error.
if (HasAuthError(service())) {
return true;
}
// Still waiting on sync setup.
return false;
}
};
test_accounts::FamilyMember CreateTestAccountFromCredentialsSwitch(
std::string_view credentials_switch) {
std::string credentials =
base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
credentials_switch);
test_accounts::FamilyMember member;
if (credentials_switch == kHeadOfHouseholdCredentialsSwitch) {
member.role = kidsmanagement::FamilyRole::HEAD_OF_HOUSEHOLD;
} else if (credentials_switch == kChildCredentialsSwitch) {
member.role = kidsmanagement::FamilyRole::CHILD;
} else {
NOTREACHED() << "Unknown credentials switch: " << credentials_switch;
}
std::string username, password;
if (RE2::FullMatch(credentials, "(.*):(.*)", &member.username,
&member.password)) {
return member;
}
NOTREACHED() << "Expected username:password format, but got: " << credentials;
}
} // namespace
FamilyLiveTest::FamilyLiveTest(RpcMode rpc_mode) : rpc_mode_(rpc_mode) {}
FamilyLiveTest::FamilyLiveTest(
RpcMode rpc_mode,
const std::vector<std::string_view>& extra_enabled_hosts)
: extra_enabled_hosts_(extra_enabled_hosts.begin(),
extra_enabled_hosts.end()),
rpc_mode_(rpc_mode) {}
FamilyLiveTest::~FamilyLiveTest() = default;
BrowserUser& FamilyLiveTest::head_of_household() const {
CHECK(head_of_household_)
<< "No head of household found for given family or credentials";
return *head_of_household_;
}
BrowserUser& FamilyLiveTest::child() const {
CHECK(child_) << "No child found for given family or credentials";
return *child_;
}
BrowserUser& FamilyLiveTest::rpc_issuer() const {
switch (rpc_mode_) {
case RpcMode::kProd:
return head_of_household();
case RpcMode::kTestImpersonation:
return child();
}
NOTREACHED();
}
void FamilyLiveTest::TurnOnSync() {
TurnOnSyncFor(*head_of_household_);
TurnOnSyncFor(*child_);
}
void FamilyLiveTest::TurnOnSyncFor(BrowserUser& browser_user) {
browser_user.TurnOnSync();
browser_user.browser().tab_strip_model()->CloseWebContentsAt(
2, TabCloseTypes::CLOSE_CREATE_HISTORICAL_TAB);
browser_user.browser().tab_strip_model()->CloseWebContentsAt(
1, TabCloseTypes::CLOSE_CREATE_HISTORICAL_TAB);
if (IsSwitchEnabled(kDebugSwitch)) {
CHECK(AddTabAtIndexToBrowser(&browser_user.browser(), 1,
GURL("chrome://sync-internals"),
ui::PAGE_TRANSITION_AUTO_TOPLEVEL));
}
if (IsSwitchEnabled(kWaitForSyncInvalidationReadySwitch)) {
// After turning the sync on, wait until this is fully initialized.
LOG(INFO) << "Waiting for sync service to set up invalidations.";
syncer::SyncServiceImpl* service =
SyncServiceFactory::GetAsSyncServiceImplForProfileForTesting(
&browser_user.profile());
service->SetInvalidationsForSessionsEnabled(true);
CHECK(SyncSetupChecker(service).Wait()) << "SyncSetupChecker timed out.";
CHECK(InvalidationsStatusChecker(service, /*expected_status=*/true).Wait())
<< "Invalidation checker timed out.";
LOG(INFO) << "Invalidations ready.";
}
}
void FamilyLiveTest::SetUp() {
signin::test::LiveTest::SetUp();
// Always disable animation for stability.
ui::ScopedAnimationDurationScaleMode disable_animation(
ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
}
void FamilyLiveTest::SetUpOnMainThread() {
signin::test::LiveTest::SetUpOnMainThread();
if (IsSwitchEnabled(kFamilyFeatureIdentifierSwitch) &&
IsSwitchEnabled(kAccountRepositoryPath)) {
// Family from static test_accounts file mode
CHECK(!IsSwitchEnabled(kHeadOfHouseholdCredentialsSwitch))
<< "Head of household credentials are ignored if "
<< kFamilyFeatureIdentifierSwitch << " and " << kAccountRepositoryPath
<< " are set";
CHECK(!IsSwitchEnabled(kChildCredentialsSwitch))
<< "Child credentials are ignored if " << kFamilyFeatureIdentifierSwitch
<< " and " << kAccountRepositoryPath << " are set";
std::optional<test_accounts::Feature> family_feature =
test_accounts::ParseFeature(
base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
kFamilyFeatureIdentifierSwitch));
CHECK(family_feature.has_value()) << "Unrecognized family feature";
TestAccountRepository repository(
base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
kAccountRepositoryPath));
std::optional<test_accounts::Family> family =
repository.GetRandomFamilyByFeature(*family_feature);
CHECK(family_feature.has_value())
<< "Family with requested feature not available";
std::optional<test_accounts::FamilyMember> head_of_household =
GetFirstFamilyMemberByRole(
*family, kidsmanagement::FamilyRole::HEAD_OF_HOUSEHOLD);
CHECK(head_of_household.has_value()) << "Head of household not available";
SetHeadOfHousehold(*head_of_household);
std::optional<test_accounts::FamilyMember> child =
GetFirstFamilyMemberByRole(*family, kidsmanagement::FamilyRole::CHILD);
CHECK(child.has_value()) << "Child not available";
SetChild(*child);
return;
}
if (IsSwitchEnabled(kHeadOfHouseholdCredentialsSwitch) &&
IsSwitchEnabled(kChildCredentialsSwitch)) {
SetHeadOfHousehold(CreateTestAccountFromCredentialsSwitch(
kHeadOfHouseholdCredentialsSwitch));
SetChild(CreateTestAccountFromCredentialsSwitch(kChildCredentialsSwitch));
return;
}
NOTREACHED() << "Either specify " << kFamilyFeatureIdentifierSwitch << " and "
<< kAccountRepositoryPath << " or configure credentials using "
<< kHeadOfHouseholdCredentialsSwitch << " and "
<< kChildCredentialsSwitch << ".";
}
void FamilyLiveTest::TearDownOnMainThread() {
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
for (const BrowserUser* user : {child_.get(), head_of_household_.get()}) {
if (!user) {
continue;
}
// Signs out the account, so the server is notified to no longer attempt to
// notify this client. Explicit sign-out is critical, otherwise server-side
// data structures can still think that the current client should receive
// sync updates.
user->browser().GetFeatures().signin_view_controller()->ShowGaiaLogoutTab(
signin_metrics::SourceForRefreshTokenOperation::
kUserMenu_SignOutAllAccounts);
}
#endif
head_of_household_.reset();
child_.reset();
signin::test::LiveTest::TearDownOnMainThread();
}
void FamilyLiveTest::SetHeadOfHousehold(
const test_accounts::FamilyMember& credentials) {
head_of_household_ = MakeSignedInBrowser(credentials);
}
void FamilyLiveTest::SetChild(const test_accounts::FamilyMember& credentials) {
child_ = MakeSignedInBrowser(credentials);
}
void FamilyLiveTest::SetUpInProcessBrowserTestFixture() {
signin::test::LiveTest::SetUpInProcessBrowserTestFixture();
for (const auto& host : extra_enabled_hosts_) {
host_resolver()->AllowDirectLookup(host);
}
}
std::unique_ptr<BrowserUser> FamilyLiveTest::MakeSignedInBrowser(
const test_accounts::FamilyMember& credentials) {
// Managed externally to the test fixture.
Profile& profile = CreateNewProfile();
Browser* browser = CreateBrowser(&profile);
CHECK(browser) << "Expected to create a browser.";
BrowserUser::NewTabCallback new_tab_callback = base::BindLambdaForTesting(
[this, browser](int index, const GURL& url,
ui::PageTransition transition) -> bool {
return this->AddTabAtIndexToBrowser(browser, index, url, transition);
});
return std::make_unique<BrowserUser>(
credentials,
profile.GetDefaultStoragePartition()
->GetURLLoaderFactoryForBrowserProcess(),
*IdentityManagerFactory::GetForProfile(&profile), *browser, profile,
new_tab_callback);
}
GURL FamilyLiveTest::GetRoutedUrl(std::string_view url_spec) const {
GURL url(url_spec);
for (std::string_view enabled_host : extra_enabled_hosts_) {
if (url.host() == enabled_host) {
return url;
}
}
NOTREACHED() << "Supplied url_spec is not routed in this test fixture.";
}
InteractiveFamilyLiveTest::InteractiveFamilyLiveTest(
FamilyLiveTest::RpcMode rpc_mode)
: InteractiveBrowserTestT<FamilyLiveTest>(rpc_mode) {}
InteractiveFamilyLiveTest::InteractiveFamilyLiveTest(
FamilyLiveTest::RpcMode rpc_mode,
const std::vector<std::string_view>& extra_enabled_hosts)
: InteractiveBrowserTestT<FamilyLiveTest>(rpc_mode, extra_enabled_hosts) {}
ui::test::internal::InteractiveTestPrivate::MultiStep
InteractiveFamilyLiveTest::WaitForStateSeeding(
ui::test::StateIdentifier<InIntendedStateObserver> id,
const BrowserUser& browser_user,
const FamilyLinkSettingsState& state) {
return Steps(
Log(base::StrCat({"WaitForState[", state.ToString(), "]: start"})),
If([&]() { return !state.Check(browser_user.GetServices()); },
Then(
Do([&]() {
state.Seed(rpc_issuer().identity_manager(),
rpc_issuer().url_loader_factory(),
browser_user.GetAccountId().ToString());
}),
PollState(
id,
[&]() {
SyncServiceFactory::GetForProfile(&browser_user.profile())
->TriggerRefresh(syncer::DataTypeSet::All());
return state.Check(browser_user.GetServices());
},
/*polling_interval=*/base::Seconds(2)),
WaitForState(id, true), StopObservingState(id)),
Else(Log(base::StrCat(
{"WaitForState[", state.ToString(), "]: seeding skipped"})))),
Log(base::StrCat({"WaitForState[", state.ToString(), "]: completed"})));
}
std::string ToString(FamilyLiveTest::RpcMode rpc_mode) {
switch (rpc_mode) {
case FamilyLiveTest::RpcMode::kProd:
return "ProdRpcMode";
case FamilyLiveTest::RpcMode::kTestImpersonation:
return "TestImpersonationRpcMode";
}
NOTREACHED();
}
} // namespace supervised_user
|