chromium/chrome/browser/extensions/api/enterprise_reporting_private/enterprise_reporting_private_browsertest.cc

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string_view>

#include "base/command_line.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/enterprise/connectors/test/deep_scanning_test_utils.h"
#include "chrome/browser/enterprise/util/managed_browser_utils.h"
#include "chrome/browser/extensions/api/enterprise_reporting_private/enterprise_reporting_private_api.h"
#include "chrome/browser/net/profile_network_context_service.h"
#include "chrome/browser/net/profile_network_context_service_factory.h"
#include "chrome/browser/policy/chrome_browser_policy_connector.h"
#include "chrome/browser/policy/policy_test_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/extensions/api/enterprise_reporting_private.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/enterprise/browser/controller/fake_browser_dm_token_storage.h"
#include "components/enterprise/browser/enterprise_switches.h"
#include "components/policy/core/common/cloud/machine_level_user_cloud_policy_manager.h"
#include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_service.h"
#include "components/version_info/version_info.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "extensions/browser/api_test_utils.h"
#include "net/base/features.h"
#include "net/cert/x509_certificate.h"
#include "net/cert/x509_util.h"
#include "net/ssl/client_cert_identity_test_util.h"
#include "net/ssl/client_cert_store.h"
#include "net/test/cert_test_util.h"
#include "net/test/test_data_directory.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/policy/dm_token_utils.h"
#endif

#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "chrome/browser/ash/policy/core/browser_policy_connector_ash.h"
#include "chrome/browser/ash/policy/core/user_cloud_policy_manager_ash.h"
#include "chrome/browser/browser_process_platform_part.h"
#include "chromeos/dbus/constants/dbus_switches.h"
#endif

enterprise_reporting_private;

namespace extensions {

namespace {

constexpr char kBrowserID1[] =;
constexpr char kBrowserID2[] =;
constexpr char kProfileID1[] =;
constexpr char kProfileID2[] =;

constexpr char kGoogleServiceProvider[] =;

constexpr char kOtherServiceProvider[] =;

constexpr char kAnotherServiceProvider[] =;

constexpr char kAndAnotherServiceProvider[] =;

constexpr char kRequestingUrl[] =;

class MockClientCertStore : public net::ClientCertStore {};

}  // namespace

// Base class for non-parametrized GetContextInfo test cases. This class enables
// test cases that would otherwise require a ton of setup in order to be unit
// tests.
class EnterpriseReportingPrivateGetContextInfoBaseBrowserTest
    : public InProcessBrowserTest {};

// This browser test class is used to avoid mocking too much browser/profile
// management objects in order to keep the test simple and useful. Please add
// new tests for the getContextInfo API in enterprise_reporting_private_unittest
// and only add tests in this file if they have similar constraints.
class EnterpriseReportingPrivateGetContextInfoBrowserTest
    : public EnterpriseReportingPrivateGetContextInfoBaseBrowserTest,
      public testing::WithParamInterface<testing::tuple<bool, bool>> {};

class EnterpriseReportingPrivateGetContextInfoSiteIsolationTest
    : public EnterpriseReportingPrivateGetContextInfoBaseBrowserTest,
      public testing::WithParamInterface<bool> {};

INSTANTIATE_TEST_SUITE_P();

INSTANTIATE_TEST_SUITE_P();

IN_PROC_BROWSER_TEST_P(
    EnterpriseReportingPrivateGetContextInfoSiteIsolationTest,
    Test) {}

#if BUILDFLAG(IS_CHROMEOS_ASH)
class EnterpriseReportingPrivateGetContextInfoChromeOSFirewallTest
    : public EnterpriseReportingPrivateGetContextInfoBaseBrowserTest,
      public testing::WithParamInterface<bool> {
 public:
  bool dev_mode_enabled() { return GetParam(); }

  void SetUpCommandLine(base::CommandLine* command_line) override {
    if (dev_mode_enabled()) {
      command_line->AppendSwitch(chromeos::switches::kSystemDevMode);
    } else {
      command_line->RemoveSwitch(chromeos::switches::kSystemDevMode);
    }
  }

  bool BuiltInDnsClientPlatformDefault() {
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_ANDROID) || \
    BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
    return true;
#else
    return false;
#endif
  }

  void ExpectDefaultThirdPartyBlockingEnabled(
      const enterprise_reporting_private::ContextInfo& info) {
#if BUILDFLAG(IS_WIN) && BUILDFLAG(GOOGLE_CHROME_BRANDING)
    EXPECT_TRUE(*info.third_party_blocking_enabled);
#else
    EXPECT_FALSE(info.third_party_blocking_enabled.has_value());
#endif
  }
};

IN_PROC_BROWSER_TEST_P(
    EnterpriseReportingPrivateGetContextInfoChromeOSFirewallTest,
    Test) {
  auto function =
      base::MakeRefCounted<EnterpriseReportingPrivateGetContextInfoFunction>();
  auto context_info_value = api_test_utils::RunFunctionAndReturnSingleResult(
      function.get(),
      /*args*/ "[]", browser()->profile());
  ASSERT_TRUE(context_info_value);
  ASSERT_TRUE(context_info_value->is_dict());

  auto info = enterprise_reporting_private::ContextInfo::FromValue(
      context_info_value->GetDict());
  ASSERT_TRUE(info);

  EXPECT_TRUE(info->browser_affiliation_ids.empty());
  EXPECT_TRUE(info->profile_affiliation_ids.empty());
  EXPECT_TRUE(info->on_file_attached_providers.empty());
  EXPECT_TRUE(info->on_file_downloaded_providers.empty());
  EXPECT_TRUE(info->on_bulk_data_entry_providers.empty());
  EXPECT_TRUE(info->on_print_providers.empty());
  EXPECT_EQ(enterprise_reporting_private::RealtimeUrlCheckMode::kDisabled,
            info->realtime_url_check_mode);
  EXPECT_TRUE(info->on_security_event_providers.empty());
  EXPECT_EQ(version_info::GetVersionNumber(), info->browser_version);
  EXPECT_EQ(enterprise_reporting_private::SafeBrowsingLevel::kStandard,
            info->safe_browsing_protection_level);
  EXPECT_EQ(BuiltInDnsClientPlatformDefault(),
            info->built_in_dns_client_enabled);
  EXPECT_EQ(
      enterprise_reporting_private::PasswordProtectionTrigger::kPolicyUnset,
      info->password_protection_warning_trigger);
  EXPECT_FALSE(info->chrome_remote_desktop_app_blocked);
  ExpectDefaultThirdPartyBlockingEnabled(*info);
  EXPECT_EQ(dev_mode_enabled()
                ? api::enterprise_reporting_private::SettingValue::kUnknown
                : api::enterprise_reporting_private::SettingValue::kEnabled,
            info->os_firewall);
}

INSTANTIATE_TEST_SUITE_P(
    ,
    EnterpriseReportingPrivateGetContextInfoChromeOSFirewallTest,
    testing::Bool());
#endif

// crbug.com/1230268 not working on Lacros.
#if BUILDFLAG(IS_CHROMEOS_LACROS)
#define MAYBE_AffiliationIDs
#else
#define MAYBE_AffiliationIDs
#endif
IN_PROC_BROWSER_TEST_P(EnterpriseReportingPrivateGetContextInfoBrowserTest,
                       MAYBE_AffiliationIDs) {}

IN_PROC_BROWSER_TEST_F(EnterpriseReportingPrivateGetContextInfoBaseBrowserTest,
                       TestFileAttachedProviderName) {}

IN_PROC_BROWSER_TEST_F(EnterpriseReportingPrivateGetContextInfoBaseBrowserTest,
                       TestFileDownloadedProviderName) {}

IN_PROC_BROWSER_TEST_F(EnterpriseReportingPrivateGetContextInfoBaseBrowserTest,
                       TestBulkDataEntryProviderName) {}

IN_PROC_BROWSER_TEST_F(EnterpriseReportingPrivateGetContextInfoBaseBrowserTest,
                       TestPrintProviderName) {}

IN_PROC_BROWSER_TEST_F(EnterpriseReportingPrivateGetContextInfoBaseBrowserTest,
                       TestAllProviderNamesSet) {}

IN_PROC_BROWSER_TEST_F(EnterpriseReportingPrivateGetContextInfoBaseBrowserTest,
                       TestOnSecurityEventProviderNameUnset) {}

IN_PROC_BROWSER_TEST_F(EnterpriseReportingPrivateGetContextInfoBaseBrowserTest,
                       TestOnSecurityEventProviderNameSet) {}

class EnterpriseReportingPrivateGetCertificateTest : public policy::PolicyTest {};

IN_PROC_BROWSER_TEST_F(EnterpriseReportingPrivateGetCertificateTest,
                       TestPolicyUnset) {}

IN_PROC_BROWSER_TEST_F(EnterpriseReportingPrivateGetCertificateTest,
                       TestPolicySet) {}

IN_PROC_BROWSER_TEST_F(EnterpriseReportingPrivateGetCertificateTest,
                       TestPolicySetCertsPresentButNotMatching) {}

IN_PROC_BROWSER_TEST_F(EnterpriseReportingPrivateGetCertificateTest,
                       TestPolicySetCertsPresentUrlNotMatching) {}

IN_PROC_BROWSER_TEST_F(EnterpriseReportingPrivateGetCertificateTest,
                       TestPolicySetCertsPresentAndMatching) {}

}  // namespace extensions