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 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
#pragma allow_unsafe_libc_calls
#endif
#include "chrome/browser/enterprise/connectors/connectors_service.h"
#include <memory>
#include <utility>
#include "base/json/json_reader.h"
#include "base/path_service.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/enterprise/connectors/common.h"
#include "chrome/browser/enterprise/connectors/test/deep_scanning_browsertest_base.h"
#include "chrome/browser/enterprise/connectors/test/deep_scanning_test_utils.h"
#include "chrome/browser/policy/chrome_browser_policy_connector.h"
#include "chrome/browser/policy/dm_token_utils.h"
#include "chrome/browser/profiles/reporting_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/testing_profile.h"
#include "components/enterprise/browser/controller/fake_browser_dm_token_storage.h"
#include "components/enterprise/browser/enterprise_switches.h"
#include "components/enterprise/buildflags/buildflags.h"
#include "components/enterprise/connectors/core/connectors_prefs.h"
#include "components/policy/core/common/cloud/machine_level_user_cloud_policy_manager.h"
#include "components/policy/core/common/cloud/mock_cloud_policy_client.h"
#include "components/policy/core/common/cloud/reporting_job_configuration_base.h"
#include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
#include "components/policy/core/common/features.h"
#include "components/policy/core/common/policy_switches.h"
#include "components/safe_browsing/core/common/features.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "components/version_info/version_info.h"
#include "content/public/test/browser_test.h"
#include "google_apis/gaia/gaia_id.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ash/policy/core/user_cloud_policy_manager_ash.h"
#include "components/account_id/account_id.h"
#include "components/user_manager/user_manager.h"
#endif
namespace enterprise_connectors {
namespace {
constexpr char kNormalCloudAnalysisSettingsPref[] = R"([
{
"service_provider": "google",
"enable": [
{"url_list": ["*"], "tags": ["dlp", "malware"]}
]
}
])";
constexpr char kNormalLocalAnalysisSettingsPref[] = R"([
{
"service_provider": "local_user_agent",
"enable": [
{"url_list": ["*"], "tags": ["dlp"]}
]
}
])";
constexpr char kNormalReportingSettingsPref[] = R"([
{
"service_provider": "google"
}
])";
#if !BUILDFLAG(IS_CHROMEOS)
constexpr char kAffiliationId2[] = "affiliation-id-2";
constexpr char kFakeEnrollmentToken[] = "fake-enrollment-token";
constexpr char kUsername1[] = "user@domain1.com";
constexpr char kUsername2[] = "admin@domain2.com";
constexpr char kDomain2[] = "domain2.com";
#endif
constexpr char kFakeBrowserDMToken[] = "fake-browser-dm-token";
constexpr char kFakeProfileDMToken[] = "fake-profile-dm-token";
constexpr char kFakeBrowserClientId[] = "fake-browser-client-id";
constexpr char kFakeProfileClientId[] = "fake-profile-client-id";
constexpr char kAffiliationId1[] = "affiliation-id-1";
constexpr char kDomain1[] = "domain1.com";
constexpr char kTestUrl[] = "https://foo.com";
std::string ExpectedOsPlatform() {
#if BUILDFLAG(IS_WIN)
return "Windows";
#elif BUILDFLAG(IS_MAC)
return "Mac OS X";
#elif BUILDFLAG(IS_CHROMEOS)
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
return "ChromeOS";
#else
return "ChromiumOS";
#endif
#elif BUILDFLAG(IS_LINUX)
return "Linux";
#endif
}
// We want a way to check whether or not any metadata was set, but the profile
// metadata will always contain the `is_chrome_os_managed_guest_session` field
// so we check another field (which should always be set if any actual metadata
// was provided).
bool ContainsClientId(const AnalysisSettings& settings) {
return settings.client_metadata && settings.client_metadata->has_device() &&
settings.client_metadata->device().has_client_id();
}
} // namespace
// Profile DM token tests
// These tests validate that ConnectorsService obtains the correct DM token on
// each GetAnalysisSettings/GetReportingSettings call. There are 3 mains cases
// to validate here:
//
// - Affiliated: The profile and browser are managed by the same customer. In
// this case, it is OK to get the profile DM token and apply Connector policies.
// - Unaffiliated: The profile and browser are managed by different customers.
// In this case, no profile settings should be returned.
// - Unmanaged: The profile is managed by a customer while the browser is
// unmanaged. In this case, it is OK to get the profile DM token and apply
// Connector policies.
//
// The exception to the above rules is CrOS. Even when the policies are applied
// at a user scope, only the browser DM token should be returned.
enum class ManagementStatus { AFFILIATED, UNAFFILIATED, UNMANAGED };
class ConnectorsServiceProfileBrowserTest
: public test::DeepScanningBrowserTestBase {
public:
explicit ConnectorsServiceProfileBrowserTest(
ManagementStatus management_status)
: management_status_(management_status) {
if (management_status_ != ManagementStatus::UNMANAGED) {
#if BUILDFLAG(IS_CHROMEOS)
policy::SetDMTokenForTesting(
policy::DMToken::CreateValidToken(kFakeBrowserDMToken));
#else
browser_dm_token_storage_ =
std::make_unique<policy::FakeBrowserDMTokenStorage>();
browser_dm_token_storage_->SetEnrollmentToken(kFakeEnrollmentToken);
browser_dm_token_storage_->SetClientId(kFakeBrowserClientId);
browser_dm_token_storage_->EnableStorage(true);
browser_dm_token_storage_->SetDMToken(kFakeBrowserDMToken);
policy::BrowserDMTokenStorage::SetForTesting(
browser_dm_token_storage_.get());
#endif
}
}
void SetUpOnMainThread() override {
test::DeepScanningBrowserTestBase::SetUpOnMainThread();
SetUpProfileData();
if (management_status_ != ManagementStatus::UNMANAGED) {
SetUpDeviceData();
}
}
void SetUpProfileData() {
#if !BUILDFLAG(IS_CHROMEOS)
test::SetProfileDMToken(browser()->profile(), kFakeProfileDMToken);
#endif
enterprise_management::PolicyData profile_policy_data;
profile_policy_data.add_user_affiliation_ids(kAffiliationId1);
profile_policy_data.set_managed_by(kDomain1);
profile_policy_data.set_device_id(kFakeProfileClientId);
profile_policy_data.set_request_token(kFakeProfileDMToken);
auto* profile_policy_manager =
#if BUILDFLAG(IS_CHROMEOS)
browser()->profile()->GetUserCloudPolicyManagerAsh();
#else
browser()->profile()->GetUserCloudPolicyManager();
#endif
profile_policy_manager->core()->store()->set_policy_data_for_testing(
std::make_unique<enterprise_management::PolicyData>(
std::move(profile_policy_data)));
}
void SetUpDeviceData() {
#if BUILDFLAG(IS_CHROMEOS)
auto* user_manager = user_manager::UserManager::Get();
auto* user = user_manager->GetActiveUser();
user_manager::UserManager::Get()->SetUserPolicyStatus(
user->GetAccountId(),
/*is_managed=*/management_status() == ManagementStatus::UNMANAGED,
/*is_affliated=*/management_status() == ManagementStatus::AFFILIATED);
#else
auto* browser_policy_manager =
g_browser_process->browser_policy_connector()
->machine_level_user_cloud_policy_manager();
auto browser_policy_data =
std::make_unique<enterprise_management::PolicyData>();
browser_policy_data->add_device_affiliation_ids(
management_status() == ManagementStatus::AFFILIATED ? kAffiliationId1
: kAffiliationId2);
browser_policy_data->set_username(
management_status() == ManagementStatus::AFFILIATED ? kUsername1
: kUsername2);
browser_policy_manager->core()->store()->set_policy_data_for_testing(
std::move(browser_policy_data));
#endif
}
#if !BUILDFLAG(GOOGLE_CHROME_BRANDING) && !BUILDFLAG(IS_CHROMEOS)
void SetUpDefaultCommandLine(base::CommandLine* command_line) override {
InProcessBrowserTest::SetUpDefaultCommandLine(command_line);
command_line->AppendSwitch(::switches::kEnableChromeBrowserCloudManagement);
}
#endif
void SetPrefs(const char* pref,
const char* scope_pref,
const char* pref_value,
bool profile_scope = true) {
browser()->profile()->GetPrefs()->Set(pref,
*base::JSONReader::Read(pref_value));
browser()->profile()->GetPrefs()->SetInteger(
scope_pref, profile_scope ? policy::POLICY_SCOPE_USER
: policy::POLICY_SCOPE_MACHINE);
}
void SetPrefs(const char* pref,
const char* scope_pref,
int pref_value,
bool profile_scope = true) {
browser()->profile()->GetPrefs()->SetInteger(pref, pref_value);
browser()->profile()->GetPrefs()->SetInteger(
scope_pref, profile_scope ? policy::POLICY_SCOPE_USER
: policy::POLICY_SCOPE_MACHINE);
}
ManagementStatus management_status() { return management_status_; }
protected:
std::unique_ptr<policy::FakeBrowserDMTokenStorage> browser_dm_token_storage_;
ManagementStatus management_status_;
};
class ConnectorsServiceReportingProfileBrowserTest
: public ConnectorsServiceProfileBrowserTest,
public testing::WithParamInterface<ManagementStatus> {
public:
ConnectorsServiceReportingProfileBrowserTest()
: ConnectorsServiceProfileBrowserTest(GetParam()) {}
};
INSTANTIATE_TEST_SUITE_P(,
ConnectorsServiceReportingProfileBrowserTest,
testing::Values(ManagementStatus::AFFILIATED,
ManagementStatus::UNAFFILIATED,
ManagementStatus::UNMANAGED));
IN_PROC_BROWSER_TEST_P(ConnectorsServiceReportingProfileBrowserTest, Test) {
SetPrefs(kOnSecurityEventPref, kOnSecurityEventScopePref,
kNormalReportingSettingsPref);
auto settings =
ConnectorsServiceFactory::GetForBrowserContext(browser()->profile())
->GetReportingSettings();
#if BUILDFLAG(IS_CHROMEOS)
if (management_status() == ManagementStatus::UNMANAGED) {
ASSERT_FALSE(settings.has_value());
} else {
ASSERT_TRUE(settings.has_value());
ASSERT_FALSE(settings.value().per_profile);
ASSERT_EQ(kFakeBrowserDMToken, settings.value().dm_token);
}
#else
std::string management_domain =
ConnectorsServiceFactory::GetForBrowserContext(browser()->profile())
->GetManagementDomain();
// expected state is the same regardless of management_status() value.
EXPECT_TRUE(settings.has_value());
ASSERT_EQ(kFakeProfileDMToken, settings.value().dm_token);
ASSERT_TRUE(settings.value().per_profile);
ASSERT_EQ(kDomain1, management_domain);
#endif
}
class ConnectorsServiceAnalysisProfileBrowserTest
: public ConnectorsServiceProfileBrowserTest,
public testing::WithParamInterface<
std::tuple<ManagementStatus, const char*, bool>> {
public:
ConnectorsServiceAnalysisProfileBrowserTest()
: ConnectorsServiceProfileBrowserTest(std::get<0>(GetParam())) {
if (enhanced_fields_enabled()) {
scoped_feature_list_.InitWithFeatures(
/*enabled_features=*/{safe_browsing::kEnhancedFieldsForSecOps,
policy::features::kEnhancedSecurityEventFields},
/*disabled_features=*/{});
} else {
scoped_feature_list_.InitWithFeatures(
/*enabled_features=*/{},
/*disabled_features=*/{
safe_browsing::kEnhancedFieldsForSecOps,
policy::features::kEnhancedSecurityEventFields});
}
}
const char* settings_value() { return std::get<1>(GetParam()); }
bool enhanced_fields_enabled() { return std::get<2>(GetParam()); }
bool is_cloud() {
return strcmp(settings_value(), kNormalCloudAnalysisSettingsPref) == 0;
}
// Returns the Value the "normal" reporting workflow uses to validate that it
// is in sync with the information sent through analysis-reporting.
base::Value::Dict ReportingMetadata(bool is_cloud, bool include_device_info) {
base::Value::Dict output;
output.Set("browser",
policy::ReportingJobConfigurationBase::BrowserDictionaryBuilder::
BuildBrowserDictionary(!is_cloud || include_device_info));
base::Value::Dict context = reporting::GetContext(browser()->profile());
output.Merge(std::move(context));
if (include_device_info) {
base::Value::Dict device;
device.Set("device", policy::ReportingJobConfigurationBase::
DeviceDictionaryBuilder ::BuildDeviceDictionary(
kFakeBrowserDMToken, kFakeBrowserClientId));
output.Merge(std::move(device));
}
return output;
}
void ValidateClientMetadata(const ClientMetadata& metadata,
bool is_cloud,
bool profile_reporting) {
#if BUILDFLAG(IS_CHROMEOS)
bool includes_device_info =
management_status() == ManagementStatus::AFFILIATED;
#else
bool includes_device_info =
!profile_reporting ||
(management_status() == ManagementStatus::AFFILIATED && is_cloud);
#endif
base::Value::Dict reporting_metadata =
ReportingMetadata(is_cloud, includes_device_info);
ASSERT_TRUE(metadata.has_browser());
ASSERT_TRUE(metadata.browser().has_browser_id());
ASSERT_EQ(metadata.browser().browser_id(),
*reporting_metadata.FindStringByDottedPath("browser.browserId"));
ASSERT_TRUE(metadata.browser().has_user_agent());
ASSERT_EQ(metadata.browser().user_agent(),
*reporting_metadata.FindStringByDottedPath("browser.userAgent"));
ASSERT_TRUE(metadata.browser().has_chrome_version());
ASSERT_EQ(metadata.browser().chrome_version(),
version_info::GetVersionNumber());
ASSERT_EQ(
metadata.browser().chrome_version(),
*reporting_metadata.FindStringByDottedPath("browser.chromeVersion"));
if (is_cloud) {
ASSERT_EQ(
includes_device_info,
!!reporting_metadata.FindStringByDottedPath("browser.machineUser"));
if (metadata.browser().has_machine_user()) {
ASSERT_EQ(
metadata.browser().machine_user(),
*reporting_metadata.FindStringByDottedPath("browser.machineUser"));
}
} else {
ASSERT_TRUE(metadata.browser().has_machine_user());
}
// We check a field that should always be set if any device metadata was
// provided.
ASSERT_EQ(includes_device_info,
metadata.has_device() && metadata.device().has_client_id());
if (includes_device_info) {
ASSERT_TRUE(metadata.device().has_dm_token());
ASSERT_EQ(metadata.device().dm_token(), kFakeBrowserDMToken);
ASSERT_TRUE(reporting_metadata.FindStringByDottedPath("device.dmToken"));
ASSERT_EQ(metadata.device().dm_token(),
*reporting_metadata.FindStringByDottedPath("device.dmToken"));
#if !BUILDFLAG(IS_CHROMEOS)
ASSERT_TRUE(metadata.device().has_client_id());
ASSERT_EQ(metadata.device().client_id(),
*reporting_metadata.FindStringByDottedPath("device.clientId"));
#endif
ASSERT_TRUE(metadata.device().has_os_version());
ASSERT_EQ(metadata.device().os_version(),
*reporting_metadata.FindStringByDottedPath("device.osVersion"));
ASSERT_TRUE(metadata.device().has_os_platform());
ASSERT_EQ(metadata.device().os_platform(), ExpectedOsPlatform());
ASSERT_EQ(
metadata.device().os_platform(),
*reporting_metadata.FindStringByDottedPath("device.osPlatform"));
ASSERT_TRUE(metadata.device().has_name());
ASSERT_EQ(metadata.device().name(),
*reporting_metadata.FindStringByDottedPath("device.name"));
if (enhanced_fields_enabled()) {
ASSERT_TRUE(metadata.device().has_device_fqdn());
ASSERT_EQ(
metadata.device().device_fqdn(),
*reporting_metadata.FindStringByDottedPath("device.deviceFqdn"));
ASSERT_TRUE(metadata.device().has_network_name());
ASSERT_EQ(
metadata.device().network_name(),
*reporting_metadata.FindStringByDottedPath("device.networkName"));
}
}
ASSERT_TRUE(metadata.has_profile());
ASSERT_TRUE(metadata.profile().has_dm_token());
ASSERT_EQ(metadata.profile().dm_token(), kFakeProfileDMToken);
ASSERT_EQ(metadata.profile().dm_token(),
*reporting_metadata.FindStringByDottedPath("profile.dmToken"));
ASSERT_TRUE(metadata.profile().has_gaia_email());
ASSERT_EQ(metadata.profile().gaia_email(),
*reporting_metadata.FindStringByDottedPath("profile.gaiaEmail"));
ASSERT_TRUE(metadata.profile().has_profile_path());
ASSERT_EQ(
metadata.profile().profile_path(),
*reporting_metadata.FindStringByDottedPath("profile.profilePath"));
ASSERT_TRUE(metadata.profile().has_profile_name());
ASSERT_EQ(
metadata.profile().profile_name(),
*reporting_metadata.FindStringByDottedPath("profile.profileName"));
#if !BUILDFLAG(IS_CHROMEOS)
ASSERT_TRUE(metadata.profile().has_client_id());
ASSERT_EQ(metadata.profile().client_id(), kFakeProfileClientId);
ASSERT_EQ(metadata.profile().client_id(),
*reporting_metadata.FindStringByDottedPath("profile.clientId"));
#endif
}
};
INSTANTIATE_TEST_SUITE_P(
,
ConnectorsServiceAnalysisProfileBrowserTest,
testing::Combine(testing::Values(ManagementStatus::AFFILIATED,
ManagementStatus::UNAFFILIATED,
ManagementStatus::UNMANAGED),
testing::Values(kNormalCloudAnalysisSettingsPref,
kNormalLocalAnalysisSettingsPref),
testing::Bool()));
IN_PROC_BROWSER_TEST_P(ConnectorsServiceAnalysisProfileBrowserTest,
DeviceReporting) {
SetPrefs(AnalysisConnectorPref(FILE_ATTACHED),
AnalysisConnectorScopePref(FILE_ATTACHED), settings_value(),
/*profile_scope*/ false);
SetPrefs(kOnSecurityEventPref, kOnSecurityEventScopePref, settings_value(),
/*profile_scope*/ false);
auto settings =
ConnectorsServiceFactory::GetForBrowserContext(browser()->profile())
->GetAnalysisSettings(GURL(kTestUrl), FILE_ATTACHED);
// Expect no local analysis settings on platforms where it is unsupported.
#if !BUILDFLAG(ENTERPRISE_LOCAL_CONTENT_ANALYSIS)
if (settings_value() == kNormalLocalAnalysisSettingsPref) {
ASSERT_FALSE(settings.has_value());
return;
}
#endif
if (management_status() == ManagementStatus::UNMANAGED) {
if (settings_value() == kNormalLocalAnalysisSettingsPref) {
ASSERT_TRUE(settings.has_value());
ASSERT_TRUE(settings.value().cloud_or_local_settings.is_local_analysis());
ASSERT_EQ("path_user",
settings.value().cloud_or_local_settings.local_path());
ASSERT_TRUE(settings.value().cloud_or_local_settings.user_specific());
} else {
ASSERT_FALSE(settings.has_value());
}
} else {
ASSERT_TRUE(settings.has_value());
if (settings.value().cloud_or_local_settings.is_cloud_analysis()) {
ASSERT_EQ(kFakeBrowserDMToken,
settings.value().cloud_or_local_settings.dm_token());
ValidateClientMetadata(*settings.value().client_metadata, is_cloud(),
/*profile_reporting*/ false);
}
ASSERT_FALSE(settings.value().per_profile);
}
#if !BUILDFLAG(IS_CHROMEOS)
ASSERT_EQ((management_status() == ManagementStatus::UNAFFILIATED) ? kDomain2
: kDomain1,
ConnectorsServiceFactory::GetForBrowserContext(browser()->profile())
->GetManagementDomain());
#endif
}
IN_PROC_BROWSER_TEST_P(ConnectorsServiceAnalysisProfileBrowserTest,
ProfileReporting) {
SetPrefs(AnalysisConnectorPref(FILE_DOWNLOADED),
AnalysisConnectorScopePref(FILE_DOWNLOADED), settings_value());
SetPrefs(kOnSecurityEventPref, kOnSecurityEventScopePref,
kNormalReportingSettingsPref);
auto settings =
ConnectorsServiceFactory::GetForBrowserContext(browser()->profile())
->GetAnalysisSettings(GURL(kTestUrl), FILE_DOWNLOADED);
// Expect no local analysis settings on platforms where it is unsupported.
#if !BUILDFLAG(ENTERPRISE_LOCAL_CONTENT_ANALYSIS)
if (settings_value() == kNormalLocalAnalysisSettingsPref) {
ASSERT_FALSE(settings.has_value());
return;
}
#endif
#if BUILDFLAG(IS_CHROMEOS)
if (management_status() == ManagementStatus::UNMANAGED) {
if (settings_value() == kNormalLocalAnalysisSettingsPref) {
ASSERT_TRUE(settings.has_value());
// TODO(b/238216275): Verify the metadata has the expected values.
} else {
ASSERT_FALSE(settings.has_value());
}
} else {
ASSERT_TRUE(settings.has_value());
if (settings.value().cloud_or_local_settings.is_cloud_analysis()) {
ASSERT_EQ(kFakeBrowserDMToken,
settings.value().cloud_or_local_settings.dm_token());
ValidateClientMetadata(*settings.value().client_metadata, is_cloud(),
/*profile_reporting*/ false);
}
ASSERT_FALSE(settings.value().per_profile);
}
#else
std::string management_domain =
ConnectorsServiceFactory::GetForBrowserContext(browser()->profile())
->GetManagementDomain();
switch (management_status()) {
case ManagementStatus::UNAFFILIATED:
if (settings_value() == kNormalLocalAnalysisSettingsPref) {
ASSERT_TRUE(settings.has_value());
ASSERT_TRUE(
settings.value().cloud_or_local_settings.is_local_analysis());
ASSERT_EQ("path_user",
settings.value().cloud_or_local_settings.local_path());
ASSERT_TRUE(settings.value().cloud_or_local_settings.user_specific());
ASSERT_FALSE(management_domain.empty());
} else {
EXPECT_TRUE(settings.has_value());
ASSERT_TRUE(
settings.value().cloud_or_local_settings.is_cloud_analysis());
ASSERT_EQ(kFakeProfileDMToken,
settings.value().cloud_or_local_settings.dm_token());
ValidateClientMetadata(*settings.value().client_metadata, is_cloud(),
/*profile_reporting*/ true);
ASSERT_TRUE(settings.value().per_profile);
ASSERT_EQ(kDomain1, management_domain);
}
break;
case ManagementStatus::AFFILIATED:
EXPECT_TRUE(settings.has_value());
if (settings.value().cloud_or_local_settings.is_cloud_analysis()) {
ASSERT_EQ(kFakeProfileDMToken,
settings.value().cloud_or_local_settings.dm_token());
ValidateClientMetadata(*settings.value().client_metadata, is_cloud(),
/*profile_reporting*/ true);
}
ASSERT_TRUE(settings.value().per_profile);
ASSERT_EQ(kDomain1, management_domain);
break;
case ManagementStatus::UNMANAGED:
EXPECT_TRUE(settings.has_value());
ASSERT_TRUE(settings.value().client_metadata);
if (settings.value().cloud_or_local_settings.is_cloud_analysis()) {
ASSERT_EQ(kFakeProfileDMToken,
settings.value().cloud_or_local_settings.dm_token());
ValidateClientMetadata(*settings.value().client_metadata, is_cloud(),
/*profile_reporting*/ true);
ASSERT_TRUE(settings.value().per_profile);
} else {
EXPECT_EQ("path_user",
settings.value().cloud_or_local_settings.local_path());
EXPECT_TRUE(settings.value().cloud_or_local_settings.user_specific());
ValidateClientMetadata(*settings.value().client_metadata, is_cloud(),
/*profile_reporting*/ true);
}
ASSERT_EQ(kDomain1, management_domain);
break;
}
#endif
}
IN_PROC_BROWSER_TEST_P(ConnectorsServiceAnalysisProfileBrowserTest,
NoReporting) {
SetPrefs(AnalysisConnectorPref(PRINT), AnalysisConnectorScopePref(PRINT),
settings_value());
auto settings =
ConnectorsServiceFactory::GetForBrowserContext(browser()->profile())
->GetAnalysisSettings(GURL(kTestUrl), PRINT);
// Expect no local analysis settings on platforms where it is unsupported.
#if !BUILDFLAG(ENTERPRISE_LOCAL_CONTENT_ANALYSIS)
if (settings_value() == kNormalLocalAnalysisSettingsPref) {
ASSERT_FALSE(settings.has_value());
return;
}
#endif
#if BUILDFLAG(IS_CHROMEOS)
if (management_status() == ManagementStatus::UNMANAGED) {
ASSERT_FALSE(settings.has_value());
} else {
ASSERT_TRUE(settings.has_value());
ASSERT_TRUE(settings.value().cloud_or_local_settings.is_cloud_analysis());
ASSERT_EQ(kFakeBrowserDMToken,
settings.value().cloud_or_local_settings.dm_token());
ASSERT_FALSE(ContainsClientId(settings.value()));
ASSERT_TRUE(settings.value().client_metadata);
ASSERT_FALSE(settings.value().per_profile);
}
#else
std::string management_domain =
ConnectorsServiceFactory::GetForBrowserContext(browser()->profile())
->GetManagementDomain();
switch (management_status()) {
case ManagementStatus::UNAFFILIATED:
if (settings_value() == kNormalLocalAnalysisSettingsPref) {
ASSERT_TRUE(settings.has_value());
ASSERT_TRUE(
settings.value().cloud_or_local_settings.is_local_analysis());
ASSERT_EQ("path_user",
settings.value().cloud_or_local_settings.local_path());
ASSERT_TRUE(settings.value().cloud_or_local_settings.user_specific());
ASSERT_NE(management_domain.empty(), true);
} else {
EXPECT_TRUE(settings.has_value());
ASSERT_EQ(kFakeProfileDMToken,
settings.value().cloud_or_local_settings.dm_token());
ASSERT_FALSE(ContainsClientId(settings.value()));
ASSERT_TRUE(settings.value().client_metadata);
ASSERT_EQ(management_domain, kDomain1);
}
break;
case ManagementStatus::AFFILIATED:
EXPECT_TRUE(settings.has_value());
if (settings.value().cloud_or_local_settings.is_cloud_analysis()) {
ASSERT_EQ(kFakeProfileDMToken,
settings.value().cloud_or_local_settings.dm_token());
ASSERT_FALSE(ContainsClientId(settings.value()));
ASSERT_TRUE(settings.value().client_metadata);
ASSERT_FALSE(
settings.value().client_metadata->profile().dm_token().empty());
ASSERT_FALSE(
settings.value().client_metadata->device().dm_token().empty());
} else {
ASSERT_EQ("path_user",
settings.value().cloud_or_local_settings.local_path());
ASSERT_TRUE(settings.value().cloud_or_local_settings.user_specific());
ASSERT_TRUE(settings.value().client_metadata);
}
ASSERT_TRUE(settings.value().per_profile);
ASSERT_EQ(kDomain1, management_domain);
break;
case ManagementStatus::UNMANAGED:
EXPECT_TRUE(settings.has_value());
if (settings.value().cloud_or_local_settings.is_cloud_analysis()) {
ASSERT_EQ(kFakeProfileDMToken,
settings.value().cloud_or_local_settings.dm_token());
ASSERT_FALSE(ContainsClientId(settings.value()));
ASSERT_TRUE(settings.value().client_metadata);
ASSERT_FALSE(
settings.value().client_metadata->profile().dm_token().empty());
ASSERT_TRUE(
settings.value().client_metadata->device().dm_token().empty());
} else {
ASSERT_EQ("path_user",
settings.value().cloud_or_local_settings.local_path());
ASSERT_TRUE(settings.value().cloud_or_local_settings.user_specific());
ASSERT_TRUE(settings.value().client_metadata);
}
ASSERT_TRUE(settings.value().per_profile);
ASSERT_EQ(kDomain1, management_domain);
break;
}
#endif
}
// TODO(b/302576851): Consider removing after EnableRelaxedAffiliationCheck
// is cleaned up.
IN_PROC_BROWSER_TEST_P(ConnectorsServiceAnalysisProfileBrowserTest,
Affiliation) {
SetPrefs(AnalysisConnectorPref(BULK_DATA_ENTRY),
AnalysisConnectorScopePref(BULK_DATA_ENTRY), settings_value());
auto settings =
ConnectorsServiceFactory::GetForBrowserContext(browser()->profile())
->GetAnalysisSettings(GURL(kTestUrl), BULK_DATA_ENTRY);
if (settings_value() == kNormalLocalAnalysisSettingsPref) {
// Expect no local analysis settings on platforms where it is unsupported.
#if !BUILDFLAG(ENTERPRISE_LOCAL_CONTENT_ANALYSIS)
ASSERT_FALSE(settings.has_value());
#else
// Since they don't use anything tied to DM tokens and rely on a separately
// installed agent, local analysis policies are always returned regardless
// of the management status.
ASSERT_TRUE(settings.has_value());
ASSERT_TRUE(settings.value().cloud_or_local_settings.is_local_analysis());
ASSERT_TRUE(settings.value().per_profile);
ASSERT_EQ("path_user",
settings.value().cloud_or_local_settings.local_path());
ASSERT_TRUE(settings.value().cloud_or_local_settings.user_specific());
#endif
} else {
#if BUILDFLAG(IS_CHROMEOS)
if (management_status() == ManagementStatus::UNMANAGED) {
ASSERT_FALSE(settings.has_value());
} else {
ASSERT_TRUE(settings.has_value());
ASSERT_TRUE(settings.value().cloud_or_local_settings.is_cloud_analysis());
ASSERT_EQ(kFakeBrowserDMToken,
settings.value().cloud_or_local_settings.dm_token());
ASSERT_FALSE(settings.value().per_profile);
}
#else
switch (management_status()) {
case ManagementStatus::UNAFFILIATED:
case ManagementStatus::AFFILIATED:
EXPECT_TRUE(settings.has_value());
ASSERT_TRUE(
settings.value().cloud_or_local_settings.is_cloud_analysis());
ASSERT_EQ(kFakeProfileDMToken,
settings.value().cloud_or_local_settings.dm_token());
ASSERT_FALSE(ContainsClientId(settings.value()));
ASSERT_TRUE(settings.value().client_metadata);
ASSERT_TRUE(settings.value().per_profile);
break;
case ManagementStatus::UNMANAGED:
EXPECT_TRUE(settings.has_value());
ASSERT_EQ(kFakeProfileDMToken,
settings.value().cloud_or_local_settings.dm_token());
ASSERT_FALSE(ContainsClientId(settings.value()));
ASSERT_TRUE(settings.value().client_metadata);
ASSERT_TRUE(settings.value().per_profile);
break;
}
#endif
}
}
class ConnectorsServiceRealtimeURLCheckProfileBrowserTest
: public ConnectorsServiceProfileBrowserTest,
public testing::WithParamInterface<ManagementStatus> {
public:
ConnectorsServiceRealtimeURLCheckProfileBrowserTest()
: ConnectorsServiceProfileBrowserTest(GetParam()) {}
};
INSTANTIATE_TEST_SUITE_P(,
ConnectorsServiceRealtimeURLCheckProfileBrowserTest,
testing::Values(ManagementStatus::AFFILIATED,
ManagementStatus::UNAFFILIATED,
ManagementStatus::UNMANAGED));
IN_PROC_BROWSER_TEST_P(ConnectorsServiceRealtimeURLCheckProfileBrowserTest,
Test) {
SetPrefs(kEnterpriseRealTimeUrlCheckMode, kEnterpriseRealTimeUrlCheckScope,
1);
auto maybe_dm_token =
ConnectorsServiceFactory::GetForBrowserContext(browser()->profile())
->GetDMTokenForRealTimeUrlCheck();
EnterpriseRealTimeUrlCheckMode url_check_pref =
ConnectorsServiceFactory::GetForBrowserContext(browser()->profile())
->GetAppliedRealTimeUrlCheck();
#if BUILDFLAG(IS_CHROMEOS)
if (management_status() == ManagementStatus::UNMANAGED) {
ASSERT_FALSE(maybe_dm_token.has_value());
ASSERT_EQ(
maybe_dm_token.error(),
ConnectorsServiceBase::NoDMTokenForRealTimeUrlCheckReason::kNoDmToken);
ASSERT_EQ(REAL_TIME_CHECK_DISABLED, url_check_pref);
} else {
ASSERT_TRUE(maybe_dm_token.has_value());
ASSERT_EQ(kFakeBrowserDMToken, maybe_dm_token.value());
ASSERT_EQ(REAL_TIME_CHECK_FOR_MAINFRAME_ENABLED, url_check_pref);
}
#else
std::string management_domain =
ConnectorsServiceFactory::GetForBrowserContext(browser()->profile())
->GetManagementDomain();
// expected state is the same regardless of management_status() value.
ASSERT_TRUE(maybe_dm_token.has_value());
ASSERT_EQ(kFakeProfileDMToken, maybe_dm_token.value());
ASSERT_EQ(REAL_TIME_CHECK_FOR_MAINFRAME_ENABLED, url_check_pref);
ASSERT_EQ(kDomain1, management_domain);
#endif
}
} // namespace enterprise_connectors
|