chromium/chrome/browser/extensions/api/management/management_browsertest.cc

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stddef.h>

#include "base/containers/contains.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind.h"
#include "build/build_config.h"
#include "chrome/browser/enterprise/browser_management/management_service_factory.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/extensions/extension_management.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/install_verifier.h"
#include "chrome/browser/extensions/updater/extension_updater.h"
#include "chrome/browser/policy/policy_test_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/url_constants.h"
#include "components/policy/core/browser/browser_policy_connector.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_types.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "content/public/test/url_loader_interceptor.h"
#include "extensions/browser/extension_host.h"
#include "extensions/browser/extension_host_test_helper.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/test_extension_registry_observer.h"
#include "extensions/browser/updater/extension_downloader.h"
#include "extensions/common/mojom/view_type.mojom.h"
#include "extensions/test/extension_test_message_listener.h"
#include "testing/gmock/include/gmock/gmock.h"

BrowserThread;
Extension;
ExtensionRegistry;
ExtensionService;
Manifest;
ManifestLocation;
PolicyMap;
_;
Return;

namespace {

std::string BuildForceInstallPolicyValue(const char* extension_id,
                                         const char* update_url) {}

}  // namespace

class ExtensionManagementTest : public extensions::ExtensionBrowserTest {};

// Tests that installing the same version overwrites.
IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, InstallSameVersion) {}

IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, InstallOlderVersion) {}

IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, InstallThenCancel) {}

IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, InstallRequiresConfirm) {}

// Tests that disabling and re-enabling an extension works.
IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, DisableEnable) {}

#if BUILDFLAG(IS_WIN)
// Fails consistently on Windows XP, see: http://crbug.com/120640.
#define MAYBE_AutoUpdate
#else
// See http://crbug.com/103371 and http://crbug.com/120640.
#if defined(ADDRESS_SANITIZER)
#define MAYBE_AutoUpdate
#else
#define MAYBE_AutoUpdate
#endif
#endif

// Tests extension autoupdate.
IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, MAYBE_AutoUpdate) {}

#if BUILDFLAG(IS_WIN)
// Fails consistently on Windows XP, see: http://crbug.com/120640.
#define MAYBE_AutoUpdateDisabledExtensions
#else
#if defined(ADDRESS_SANITIZER)
#define MAYBE_AutoUpdateDisabledExtensions
#else
#define MAYBE_AutoUpdateDisabledExtensions
#endif
#endif

// Tests extension autoupdate.
IN_PROC_BROWSER_TEST_F(ExtensionManagementTest,
                       MAYBE_AutoUpdateDisabledExtensions) {}

IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, ExternalUrlUpdate) {}

namespace {

const char kForceInstallNotEmptyHelp[] =;

}

// See http://crbug.com/57378 for flakiness details.
IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, ExternalPolicyRefresh) {}

// Tests that non-CWS extensions are disabled when force-installed in a low
// trust environment. See https://b/283274398.
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
IN_PROC_BROWSER_TEST_F(ExtensionManagementTest,
                       NonCWSForceInstalledDisabledInLowTrustEnvironment) {
  // Mark enterprise management authority for platform as COMPUTER_LOCAL, and
  // for profile as NONE.
  policy::ScopedManagementServiceOverrideForTesting platform_management(
      policy::ManagementServiceFactory::GetForPlatform(),
      policy::EnterpriseManagementAuthority::COMPUTER_LOCAL);
  policy::ScopedManagementServiceOverrideForTesting browser_management(
      policy::ManagementServiceFactory::GetForProfile(profile()),
      policy::EnterpriseManagementAuthority::NONE);
  static constexpr char kExtensionId[] = "ogjcoiohnmldgjemafoockdghcjciccf";

  base::ScopedAllowBlockingForTesting allow_blocking;
  base::ScopedTempDir temp_dir;
  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());

  embedded_test_server()->ServeFilesFromDirectory(temp_dir.GetPath());
  ASSERT_TRUE(embedded_test_server()->Start());

  base::FilePath crx_path;
  ASSERT_NO_FATAL_FAILURE(SetUpExtensionUpdatePackage(temp_dir.GetPath(), "v2",
                                                      "v2.crx", &crx_path));
  ASSERT_NO_FATAL_FAILURE(SetUpExtensionUpdateResponse(
      temp_dir.GetPath(), "v2.crx", "manifest_v2.xml.template"));

  ExtensionRegistry* registry = extension_registry();

  base::Value::List forcelist;
  forcelist.Append(BuildForceInstallPolicyValue(kExtensionId,
                                                GetUpdateUrl().spec().c_str()));
  PolicyMap policies;
  policies.Set(policy::key::kExtensionInstallForcelist,
               policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
               policy::POLICY_SOURCE_CLOUD, base::Value(std::move(forcelist)),
               nullptr);
  extensions::TestExtensionRegistryObserver install_observer(registry);
  UpdateProviderPolicy(policies);
  install_observer.WaitForExtensionWillBeInstalled();

  // Extension should be disabled.
  EXPECT_EQ(1u, registry->disabled_extensions().size());
  EXPECT_TRUE(registry->disabled_extensions().GetByID(kExtensionId));
}

IN_PROC_BROWSER_TEST_F(ExtensionManagementTest,
                       NonCWSForceInstalledEnabledOnManagedPlatform) {
  // Mark enterprise management authority for platform as CLOUD, and for profile
  // as NONE.
  policy::ScopedManagementServiceOverrideForTesting platform_management(
      policy::ManagementServiceFactory::GetForPlatform(),
      policy::EnterpriseManagementAuthority::CLOUD);
  policy::ScopedManagementServiceOverrideForTesting browser_management(
      policy::ManagementServiceFactory::GetForProfile(profile()),
      policy::EnterpriseManagementAuthority::NONE);
  static constexpr char kExtensionId[] = "ogjcoiohnmldgjemafoockdghcjciccf";

  base::ScopedAllowBlockingForTesting allow_blocking;
  base::ScopedTempDir temp_dir;
  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());

  embedded_test_server()->ServeFilesFromDirectory(temp_dir.GetPath());
  ASSERT_TRUE(embedded_test_server()->Start());

  base::FilePath crx_path;
  ASSERT_NO_FATAL_FAILURE(SetUpExtensionUpdatePackage(temp_dir.GetPath(), "v2",
                                                      "v2.crx", &crx_path));
  ASSERT_NO_FATAL_FAILURE(SetUpExtensionUpdateResponse(
      temp_dir.GetPath(), "v2.crx", "manifest_v2.xml.template"));

  base::Value::List forcelist;
  forcelist.Append(BuildForceInstallPolicyValue(kExtensionId,
                                                GetUpdateUrl().spec().c_str()));
  PolicyMap policies;
  policies.Set(policy::key::kExtensionInstallForcelist,
               policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
               policy::POLICY_SOURCE_CLOUD, base::Value(std::move(forcelist)),
               nullptr);

  ExtensionRegistry* registry = extension_registry();
  const size_t size_before = registry->enabled_extensions().size();
  extensions::TestExtensionRegistryObserver install_observer(registry);
  UpdateProviderPolicy(policies);
  install_observer.WaitForExtensionWillBeInstalled();

  // Extension is enabled.
  EXPECT_EQ(size_before + 1, registry->enabled_extensions().size());
  EXPECT_TRUE(registry->enabled_extensions().GetByID(kExtensionId));
}

IN_PROC_BROWSER_TEST_F(ExtensionManagementTest,
                       NonCWSForceInstalledEnabledOnManagedProfile) {
  // Mark enterprise management authority for platform as NONE, and for profile
  // as CLOUD.
  policy::ScopedManagementServiceOverrideForTesting platform_management(
      policy::ManagementServiceFactory::GetForPlatform(),
      policy::EnterpriseManagementAuthority::NONE);
  policy::ScopedManagementServiceOverrideForTesting browser_management(
      policy::ManagementServiceFactory::GetForProfile(profile()),
      policy::EnterpriseManagementAuthority::CLOUD);
  static constexpr char kExtensionId[] = "ogjcoiohnmldgjemafoockdghcjciccf";

  base::ScopedAllowBlockingForTesting allow_blocking;
  base::ScopedTempDir temp_dir;
  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());

  embedded_test_server()->ServeFilesFromDirectory(temp_dir.GetPath());
  ASSERT_TRUE(embedded_test_server()->Start());

  base::FilePath crx_path;
  ASSERT_NO_FATAL_FAILURE(SetUpExtensionUpdatePackage(temp_dir.GetPath(), "v2",
                                                      "v2.crx", &crx_path));
  ASSERT_NO_FATAL_FAILURE(SetUpExtensionUpdateResponse(
      temp_dir.GetPath(), "v2.crx", "manifest_v2.xml.template"));

  base::Value::List forcelist;
  forcelist.Append(BuildForceInstallPolicyValue(kExtensionId,
                                                GetUpdateUrl().spec().c_str()));
  PolicyMap policies;
  policies.Set(policy::key::kExtensionInstallForcelist,
               policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
               policy::POLICY_SOURCE_CLOUD, base::Value(std::move(forcelist)),
               nullptr);

  ExtensionRegistry* registry = extension_registry();
  const size_t size_before = registry->enabled_extensions().size();
  extensions::TestExtensionRegistryObserver install_observer(registry);
  UpdateProviderPolicy(policies);
  install_observer.WaitForExtensionWillBeInstalled();

  // Extension is enabled.
  EXPECT_EQ(size_before + 1, registry->enabled_extensions().size());
  EXPECT_TRUE(registry->enabled_extensions().GetByID(kExtensionId));
}
#endif

// See http://crbug.com/103371 and http://crbug.com/120640.
#if defined(ADDRESS_SANITIZER) || BUILDFLAG(IS_WIN)
#define MAYBE_PolicyOverridesUserInstall
#else
#define MAYBE_PolicyOverridesUserInstall
#endif

// Tests the behavior of force-installing extensions that the user has already
// installed.
IN_PROC_BROWSER_TEST_F(ExtensionManagementTest,
                       MAYBE_PolicyOverridesUserInstall) {}