chromium/chrome/browser/extensions/api/developer_private/developer_private_api_unittest.cc

// Copyright 2015 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/browser/extensions/api/developer_private/developer_private_api.h"

#include <memory>
#include <string_view>
#include <utility>

#include "base/base_paths.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/json/json_writer.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_refptr.h"
#include "base/path_service.h"
#include "base/scoped_observation.h"
#include "base/stl_util.h"
#include "base/strings/strcat.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind.h"
#include "base/test/gtest_util.h"
#include "base/test/values_test_util.h"
#include "base/values.h"
#include "chrome/browser/extensions/api/developer_private/extension_info_generator.h"
#include "chrome/browser/extensions/chrome_test_extension_loader.h"
#include "chrome/browser/extensions/error_console/error_console.h"
#include "chrome/browser/extensions/extension_action_test_util.h"
#include "chrome/browser/extensions/extension_install_prompt_show_params.h"
#include "chrome/browser/extensions/extension_management.h"
#include "chrome/browser/extensions/extension_management_test_util.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_service_test_with_install.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/extensions/manifest_v2_experiment_manager.h"
#include "chrome/browser/extensions/permissions/permissions_test_util.h"
#include "chrome/browser/extensions/permissions/permissions_updater.h"
#include "chrome/browser/extensions/permissions/scripting_permissions_modifier.h"
#include "chrome/browser/extensions/permissions/site_permissions_helper.h"
#include "chrome/browser/supervised_user/supervised_user_browser_utils.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/extensions/extension_install_ui.h"
#include "chrome/browser/ui/toolbar/toolbar_actions_model.h"
#include "chrome/common/extensions/api/developer_private.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/test_browser_window.h"
#include "chrome/test/base/testing_profile.h"
#include "components/crx_file/id_util.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "components/supervised_user/core/common/features.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/web_contents_tester.h"
#include "extensions/browser/api_test_utils.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/event_router_factory.h"
#include "extensions/browser/extension_dialog_auto_confirm.h"
#include "extensions/browser/extension_error_test_util.h"
#include "extensions/browser/extension_function_dispatcher.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_registry_observer.h"
#include "extensions/browser/extension_util.h"
#include "extensions/browser/mock_external_provider.h"
#include "extensions/browser/permissions_manager.h"
#include "extensions/browser/test_event_router_observer.h"
#include "extensions/browser/test_extension_registry_observer.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/extension_features.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/extension_set.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/mojom/context_type.mojom.h"
#include "extensions/common/permissions/permission_set.h"
#include "extensions/common/permissions/permissions_data.h"
#include "extensions/test/test_extension_dir.h"
#include "services/data_decoder/data_decoder_service.h"
#include "services/service_manager/public/cpp/test/test_connector_factory.h"
#include "ui/shell_dialogs/selected_file_info.h"

namespace extensions {

namespace {

const char kGoodCrx[] =;
const char kGoogleOnlyCrx[] =;
constexpr char kInvalidHost[] =;
constexpr char kInvalidHostError[] =;

std::unique_ptr<KeyedService> BuildAPI(content::BrowserContext* context) {}

std::unique_ptr<KeyedService> BuildEventRouter(
    content::BrowserContext* profile) {}

bool HasPrefsPermission(bool (*has_pref)(const ExtensionId&,
                                         content::BrowserContext*),
                        content::BrowserContext* context,
                        const ExtensionId& id) {}

bool WasItemChangedEventDispatched(
    const TestEventRouterObserver& observer,
    const ExtensionId& extension_id,
    const api::developer_private::EventType event_type) {}

bool WasUserSiteSettingsChangedEventDispatched(
    const TestEventRouterObserver& observer,
    api::developer_private::UserSiteSettings* settings) {}

void AddUserSpecifiedSites(Profile* profile,
                           const std::string& hosts,
                           bool restricted) {}

void RemoveUserSpecifiedSites(Profile* profile,
                              const std::string& hosts,
                              bool restricted) {}

void AddExtensionAndGrantPermissions(Profile* profile,
                                     ExtensionService* service,
                                     const Extension& extension) {}

void RunAddHostPermission(Profile* profile,
                          const Extension& extension,
                          std::string_view host,
                          bool should_succeed,
                          const char* expected_error) {}

void GetMatchingExtensionsForSite(
    Profile* profile,
    const std::string& site,
    std::vector<api::developer_private::MatchingExtensionInfo>* infos) {}

auto MatchMatchingExtensionInfo(
    const ExtensionId& extension_id,
    const api::developer_private::HostAccess& host_access,
    bool can_request_all_sites) {}

api::developer_private::ExtensionSiteAccessUpdate CreateSiteAccessUpdate(
    const ExtensionId& id,
    api::developer_private::HostAccess access) {}

void UpdateSiteAccess(
    Profile* profile,
    const std::string& site,
    const std::vector<api::developer_private::ExtensionSiteAccessUpdate>&
        updates) {}

}  // namespace

class DeveloperPrivateApiUnitTest : public ExtensionServiceTestWithInstall {};

bool DeveloperPrivateApiUnitTest::RunFunction(
    const scoped_refptr<ExtensionFunction>& function,
    const base::Value::List& args) {}

const Extension* DeveloperPrivateApiUnitTest::LoadUnpackedExtension() {}

const Extension* DeveloperPrivateApiUnitTest::LoadSimpleExtension() {}

void DeveloperPrivateApiUnitTest::TestExtensionPrefSetting(
    const base::RepeatingCallback<bool()>& has_pref,
    const std::string& key,
    const ExtensionId& extension_id,
    bool expected_default_value) {}

testing::AssertionResult DeveloperPrivateApiUnitTest::TestPackExtensionFunction(
    const base::Value::List& args,
    api::developer_private::PackStatus expected_status,
    int expected_flags) {}

void DeveloperPrivateApiUnitTest::UpdateProfileConfigurationDevMode(
    bool dev_mode) {}

void DeveloperPrivateApiUnitTest::GetProfileConfiguration(
    std::optional<api::developer_private::ProfileInfo>* profile_info) {}

void DeveloperPrivateApiUnitTest::RunUpdateHostAccess(
    const Extension& extension,
    std::string_view new_access) {}

void DeveloperPrivateApiUnitTest::SetUp() {}

void DeveloperPrivateApiUnitTest::TearDown() {}

// Test developerPrivate.updateExtensionConfiguration.
TEST_F(DeveloperPrivateApiUnitTest,
       DeveloperPrivateUpdateExtensionConfiguration) {}

// Test developerPrivate.reload.
TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateReload) {}

// Test developerPrivate.packDirectory.
TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivatePackFunction) {}

// Test developerPrivate.choosePath.
TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateChoosePath) {}

// Test developerPrivate.loadUnpacked.
TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateLoadUnpacked) {}

TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateLoadUnpackedLoadError) {}

// Test that the retryGuid supplied by loadUnpacked works correctly.
TEST_F(DeveloperPrivateApiUnitTest, LoadUnpackedRetryId) {}

// Tests calling "reload" on an unpacked extension with a manifest error,
// resulting in the reload failing. The reload call should then respond with
// the load error, which includes a retry GUID to be passed to loadUnpacked().
TEST_F(DeveloperPrivateApiUnitTest, ReloadBadExtensionToLoadUnpackedRetry) {}

TEST_F(DeveloperPrivateApiUnitTest,
       DeveloperPrivateNotifyDragInstallInProgress) {}

// Test developerPrivate.requestFileSource.
TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateRequestFileSource) {}

// Test developerPrivate.getExtensionsInfo.
TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateGetExtensionsInfo) {}

// Test developerPrivate.deleteExtensionErrors.
TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateDeleteExtensionErrors) {}

// Tests that developerPrivate.repair does not succeed for a non-corrupted
// extension.
TEST_F(DeveloperPrivateApiUnitTest, RepairNotBrokenExtension) {}

// Tests that developerPrivate.private cannot repair a policy-installed
// extension.
// Regression test for https://crbug.com/577959.
TEST_F(DeveloperPrivateApiUnitTest, RepairPolicyExtension) {}

// Tests that developerPrivate.repair does not succeed for an extension not from
// the Chrome Web Store.
TEST_F(DeveloperPrivateApiUnitTest, RepairNonCWSExtension) {}

// Test developerPrivate.updateProfileConfiguration: Try to turn on devMode
// when DeveloperToolsAvailability policy disallows developer tools.
TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateDevModeDisabledPolicy) {}

// Test developerPrivate.updateProfileConfiguration: Try to turn on devMode
// (without DeveloperToolsAvailability policy).
TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateDevMode) {}

TEST_F(DeveloperPrivateApiUnitTest, LoadUnpackedFailsWithoutDevMode) {}

TEST_F(DeveloperPrivateApiUnitTest, LoadUnpackedFailsWithBlocklistingPolicy) {}

TEST_F(DeveloperPrivateApiUnitTest,
       LoadUnpackedWorksWithBlocklistingPolicyAlongAllowlistingPolicy) {}

TEST_F(DeveloperPrivateApiUnitTest, InstallDroppedFileNoDraggedPath) {}

TEST_F(DeveloperPrivateApiUnitTest, InstallDroppedFileCrx) {}

TEST_F(DeveloperPrivateApiUnitTest, InstallDroppedFileUserScript) {}

TEST_F(DeveloperPrivateApiUnitTest, GrantHostPermission) {}

TEST_F(DeveloperPrivateApiUnitTest, RemoveHostPermission) {}

TEST_F(DeveloperPrivateApiUnitTest, UpdateHostAccess) {}

TEST_F(DeveloperPrivateApiUnitTest,
       UpdateHostAccess_SpecificSitesRemovedOnTransitionToOnClick) {}

TEST_F(DeveloperPrivateApiUnitTest,
       UpdateHostAccess_SpecificSitesRemovedOnTransitionToAllSites) {}

TEST_F(DeveloperPrivateApiUnitTest,
       UpdateHostAccess_BroadPermissionsRemovedOnTransitionToSpecificSites) {}

TEST_F(DeveloperPrivateApiUnitTest,
       UpdateHostAccess_GrantScopeGreaterThanRequestedScope) {}

TEST_F(DeveloperPrivateApiUnitTest,
       UpdateHostAccess_UnrequestedHostsDispatchUpdateEvents) {}

TEST_F(DeveloperPrivateApiUnitTest, ExtensionUpdatedEventOnPermissionsChange) {}

class DeveloperPrivateApiZipFileUnitTest
    : public DeveloperPrivateApiUnitTest,
      public testing::WithParamInterface<bool> {};

INSTANTIATE_TEST_SUITE_P();

TEST_P(DeveloperPrivateApiZipFileUnitTest, InstallDroppedFileZip) {}

// Test developerPrivate.getUserSiteSettings.
TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateGetUserSiteSettings) {}

// Test developerPrivate.addUserSpecifiedSite and removeUserSpecifiedSite for
// restricted sites.
TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateModifyUserSiteSettings) {}

// Test that the OnUserSiteSettingsChanged event is fired whenever the user
// defined site settings update.
TEST_F(DeveloperPrivateApiUnitTest, OnUserSiteSettingsChanged) {}

class DeveloperPrivateApiWithPermittedSitesUnitTest
    : public DeveloperPrivateApiUnitTest {};

DeveloperPrivateApiWithPermittedSitesUnitTest::
    DeveloperPrivateApiWithPermittedSitesUnitTest() {}

// Test developerPrivate.getUserSiteSettings.
TEST_F(DeveloperPrivateApiWithPermittedSitesUnitTest,
       DeveloperPrivateGetUserSiteSettings) {}

// Test developerPrivate.addUserSpecifiedSite and removeUserSpecifiedSite.
TEST_F(DeveloperPrivateApiWithPermittedSitesUnitTest,
       DeveloperPrivateModifyUserSiteSettings) {}

TEST_F(DeveloperPrivateApiWithPermittedSitesUnitTest,
       DeveloperPrivateGetUserAndExtensionSitesByEtld_UserSites) {}

TEST_F(DeveloperPrivateApiWithPermittedSitesUnitTest,
       DeveloperPrivateGetUserAndExtensionSitesByEtld_UserAndExtensionSites) {}

TEST_F(DeveloperPrivateApiWithPermittedSitesUnitTest,
       DeveloperPrivateGetUserAndExtensionSitesByEtld_EffectiveAllHosts) {}

TEST_F(DeveloperPrivateApiUnitTest,
       DeveloperPrivateGetUserAndExtensionSitesByEtld_RuntimeGrantedHosts) {}

// Test that host permissions from policy installed extensions are included in
// `getUserAndExtensionSitesByEtld` calls.
TEST_F(
    DeveloperPrivateApiUnitTest,
    DeveloperPrivateGetUserAndExtensionSitesByEtld_PolicyControlledExtensions) {}

TEST_F(DeveloperPrivateApiUnitTest,
       DeveloperPrivateGetMatchingExtensionsForSite) {}

// Test that the host access returned by GetMatchingExtensionsForSite reflects
// whether the extension has access to the queried site, or has withheld sites
// in general.
TEST_F(DeveloperPrivateApiUnitTest,
       DeveloperPrivateGetMatchingExtensionsForSite_RuntimeGrantedHostAccess) {}

// Tests the UpdateSiteAccess function when called on an extension with no
// withheld host permissions.
TEST_F(DeveloperPrivateApiUnitTest,
       DeveloperPrivateUpdateSiteAccess_NoWithheldHostPermissions) {}

// Tests the UpdateSiteAccess function when called on an extension with withheld
// host permissions. In particular, test that if the site access is set to
// ON_CLICK, all host permissions that match the specified site will be revoked.
TEST_F(DeveloperPrivateApiUnitTest,
       DeveloperPrivateUpdateSiteAccess_WitheldHostPermissions) {}

// Test that the UpdateSiteAccess function can be applied to multiple
// extensions.
TEST_F(DeveloperPrivateApiUnitTest,
       DeveloperPrivateUpdateSiteAccess_MultipleExtensions) {}

// Test uninstalling multiple extensions.
TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateRemoveMultipleExtensions) {}

// Test cancelling uninstall multiple extensions dialog.
TEST_F(DeveloperPrivateApiUnitTest,
       DeveloperPrivateCancelRemoveMultipleExtensions) {}

TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateRemoveComponentExtensions) {}

TEST_F(DeveloperPrivateApiUnitTest,
       DeveloperPrivateRemoveEnterpriseExtensions) {}

// Test that an event is dispatched when the list of pinned extension actions
// has changed.
TEST_F(DeveloperPrivateApiUnitTest,
       ExtensionUpdatedEventOnPinnedActionsChange) {}

class DeveloperPrivateApiAllowlistUnitTest
    : public DeveloperPrivateApiUnitTest {};

TEST_F(DeveloperPrivateApiAllowlistUnitTest,
       ExtensionUpdatedEventOnAllowlistWarningChange) {}

class DeveloperPrivateApiSupervisedUserUnitTest
    : public DeveloperPrivateApiUnitTest,
      public testing::WithParamInterface<bool> {};

// Tests trying to call loadUnpacked when the profile shouldn't be allowed to.
TEST_P(DeveloperPrivateApiSupervisedUserUnitTest,
       LoadUnpackedFailsForSupervisedUsers) {}

INSTANTIATE_TEST_SUITE_P();

// Test suite for cases where the user is in the  MV2 deprecation "warning"
// experiment phase.
class DeveloperPrivateApiWithMV2DeprecationWarningUnitTest
    : public DeveloperPrivateApiUnitTest {};

// Test suite for cases where the user is in the  MV2 deprecation "disabled"
// experiment phase.
class DeveloperPrivateApiWithMV2DeprecationDisabledUnitTest
    : public DeveloperPrivateApiUnitTest {};

TEST_F(DeveloperPrivateApiWithMV2DeprecationWarningUnitTest,
       TestAcknowledgingAnExtension) {}

TEST_F(DeveloperPrivateApiWithMV2DeprecationWarningUnitTest,
       TestAcknowledgingANonAffectedExtension) {}

TEST_F(DeveloperPrivateApiWithMV2DeprecationWarningUnitTest,
       TestAcknowledgingNoticeGlobally) {}

TEST_F(DeveloperPrivateApiWithMV2DeprecationDisabledUnitTest,
       TestAcknowledgingAnExtension) {}

}  // namespace extensions