#include "chrome/browser/enterprise/connectors/connectors_service.h"
#include <tuple>
#include "base/json/json_reader.h"
#include "base/memory/raw_ptr.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/enterprise/connectors/common.h"
#include "chrome/browser/enterprise/connectors/connectors_manager.h"
#include "chrome/browser/enterprise/connectors/test/deep_scanning_test_utils.h"
#include "chrome/browser/policy/dm_token_utils.h"
#include "chrome/browser/profiles/profile_testing_helper.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/enterprise/browser/controller/fake_browser_dm_token_storage.h"
#include "components/enterprise/common/proto/connectors.pb.h"
#include "components/enterprise/connectors/core/common.h"
#include "components/enterprise/connectors/core/connectors_prefs.h"
#include "components/enterprise/connectors/core/service_provider_config.h"
#include "components/policy/core/common/policy_types.h"
#include "content/public/test/browser_task_environment.h"
#include "storage/browser/file_system/file_system_url.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chromeos/components/mgs/managed_guest_session_test_utils.h"
#endif
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "base/strings/strcat.h"
#include "extensions/common/constants.h"
#endif
namespace enterprise_connectors {
namespace {
constexpr char kEmptySettingsPref[] = …;
constexpr char kNormalReportingSettingsPref[] = …;
#if BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
constexpr char kWildcardAnalysisSettingsPref[] = …;
constexpr char kCustomMessage[] = …;
constexpr char kCustomUrl[] = …;
#endif
constexpr char kFakeDmToken[] = …;
#if !BUILDFLAG(IS_CHROMEOS)
constexpr char kFakeDeviceId[] = …;
#endif
#if BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
std::string CreateCustomUIPref(const char* custom_message,
const char* custom_url,
bool bypass_enabled) { … }
#endif
}
class ConnectorsServiceTest : public testing::Test { … };
#if BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
class ConnectorsServiceHasExtraUiTest
: public ConnectorsServiceTest,
public testing::WithParamInterface<std::tuple<std::string, bool>> { … };
TEST_P(ConnectorsServiceHasExtraUiTest, AnalysisConnectors) { … }
INSTANTIATE_TEST_SUITE_P(…);
#endif
class ConnectorsServiceReportingFeatureTest
: public ConnectorsServiceTest,
public testing::WithParamInterface<std::tuple<ReportingConnector, int>> { … };
TEST_P(ConnectorsServiceReportingFeatureTest, Test) { … }
#if BUILDFLAG(IS_CHROMEOS)
TEST_P(ConnectorsServiceReportingFeatureTest,
ChromeOsManagedGuestSessionFlagSetInMgs) {
chromeos::FakeManagedGuestSession fake_mgs;
if (policy_value() != 0) {
profile_->GetPrefs()->Set(pref(), *base::JSONReader::Read(pref_value()));
profile_->GetPrefs()->SetInteger(scope_pref(),
policy::POLICY_SCOPE_MACHINE);
}
EXPECT_TRUE(ConnectorsServiceFactory::GetForBrowserContext(profile_)
->BuildClientMetadata(true)
->is_chrome_os_managed_guest_session());
EXPECT_FALSE(ConnectorsServiceFactory::GetForBrowserContext(profile_)
->BuildClientMetadata(false)
->is_chrome_os_managed_guest_session());
}
TEST_P(ConnectorsServiceReportingFeatureTest,
ChromeOsManagedGuestSessionFlagNotSetInUserSession) {
if (policy_value() != 0) {
profile_->GetPrefs()->Set(pref(), *base::JSONReader::Read(pref_value()));
profile_->GetPrefs()->SetInteger(scope_pref(),
policy::POLICY_SCOPE_MACHINE);
}
EXPECT_FALSE(ConnectorsServiceFactory::GetForBrowserContext(profile_)
->BuildClientMetadata(true)
->is_chrome_os_managed_guest_session());
EXPECT_FALSE(ConnectorsServiceFactory::GetForBrowserContext(profile_)
->BuildClientMetadata(false)
->is_chrome_os_managed_guest_session());
}
#endif
#if BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
TEST_P(ConnectorsServiceReportingFeatureTest, CheckTelemetryPolicyObserver) { … }
#endif
INSTANTIATE_TEST_SUITE_P(…);
TEST_F(ConnectorsServiceTest, RealtimeURLCheck) { … }
#if BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
class ConnectorsServiceExemptURLsTest
: public ConnectorsServiceTest,
public testing::WithParamInterface<AnalysisConnector> { … };
TEST_P(ConnectorsServiceExemptURLsTest, WebUI) { … }
TEST_P(ConnectorsServiceExemptURLsTest, ThirdPartyExtensions) { … }
TEST_P(ConnectorsServiceExemptURLsTest, BlobAndFilesystem) { … }
#if BUILDFLAG(IS_CHROMEOS_ASH)
TEST_P(ConnectorsServiceExemptURLsTest, FirstPartyExtensions) {
auto* service = ConnectorsServiceFactory::GetForBrowserContext(profile_);
for (const std::string& suffix :
{"/", "/background", "/main.html", "/main.html?param=value"}) {
std::string url = base::StrCat(
{"chrome-extension://", extension_misc::kFilesManagerAppId, suffix});
auto settings = service->GetAnalysisSettings(GURL(url), connector());
ASSERT_FALSE(settings.has_value());
}
}
#endif
INSTANTIATE_TEST_SUITE_P(…);
#endif
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
class ConnectorsServiceProfileTypeBrowserTest : public testing::Test { … };
TEST_F(ConnectorsServiceProfileTypeBrowserTest, IsEnabled) { … }
#endif
}