chromium/chrome/browser/hid/hid_chooser_context_unittest.cc

// Copyright 2019 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/hid/hid_chooser_context.h"

#include <optional>
#include <string_view>

#include "base/barrier_closure.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/test/bind.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "base/test/values_test_util.h"
#include "base/uuid.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/hid/hid_chooser_context_factory.h"
#include "chrome/browser/hid/mock_hid_device_observer.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/permissions/test/object_permission_context_base_mock_permission_observer.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/browser_task_environment.h"
#include "extensions/common/extension_features.h"
#include "services/device/public/cpp/hid/hid_blocklist.h"
#include "services/device/public/cpp/test/fake_hid_manager.h"
#include "services/device/public/mojom/hid.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/origin.h"

#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "chromeos/ash/components/browser_context_helper/browser_context_types.h"
#include "components/account_id/account_id.h"
#include "components/user_manager/scoped_user_manager.h"
#endif

ParseJson;
RunClosure;

namespace {

// The device IDs used by the simulated HID device.
constexpr uint16_t kTestVendorId =;
constexpr uint16_t kTestProductId =;
constexpr char kTestSerialNumber[] =;
constexpr char kTestProductName[] =;
const char* const kTestPhysicalDeviceIds[] =;
constexpr char kTestUserEmail[] =;

// The HID usages assigned to the top-level collection of the simulated device.
constexpr uint16_t kTestUsagePage =;
constexpr uint16_t kTestUsage =;

constexpr char kTestExtensionId[] =;

// Main text fixture.
class HidChooserContextTestBase {};

class HidChooserContextTest : public HidChooserContextTestBase,
                              public testing::Test {};

}  // namespace

TEST_F(HidChooserContextTest, GrantAndRevokeEphemeralDevice) {}

TEST_F(HidChooserContextTest, GrantAndForgetEphemeralDevice) {}

TEST_F(HidChooserContextTest, GrantTwoEphemeralDevicesForgetOne) {}

TEST_F(HidChooserContextTest, GrantAndDisconnectEphemeralDevice) {}

TEST_F(HidChooserContextTest, GrantDisconnectRevokeUsbPersistentDevice) {}

TEST_F(HidChooserContextTest, GrantForgetUsbPersistentDevice) {}

TEST_F(HidChooserContextTest, GuardPermission) {}

TEST_F(HidChooserContextTest, ConnectionErrorWithEphemeralPermission) {}

TEST_F(HidChooserContextTest, ConnectionErrorWithPersistentPermission) {}

namespace {

device::mojom::HidDeviceInfoPtr CreateDeviceWithOneCollection(
    const std::string& guid) {}

device::mojom::HidDeviceInfoPtr CreateDeviceWithTwoCollections(
    const std::string& guid) {}

}  // namespace

TEST_F(HidChooserContextTest, AddChangeRemoveDevice) {}

namespace {

struct BlocklistTestData {} kBlocklistTestData[]{};

class HidChooserContextBlocklistTest
    : public HidChooserContextTestBase,
      public testing::TestWithParam<BlocklistTestData> {};

}  // namespace

TEST_P(HidChooserContextBlocklistTest, Blocklist) {}

INSTANTIATE_TEST_SUITE_P();

TEST_F(HidChooserContextTest, PolicyGuardPermission) {}

TEST_F(HidChooserContextTest, PolicyAskForUrls) {}

TEST_F(HidChooserContextTest, PolicyBlockedForUrls) {}

namespace {

class HidChooserContextAffiliatedTest : public HidChooserContextTestBase,
                                        public testing::TestWithParam<bool> {};

}  // namespace

TEST_P(HidChooserContextAffiliatedTest, PolicyAllowForUrls) {}

TEST_P(HidChooserContextAffiliatedTest, BlocklistOverridesPolicy) {}

TEST_F(HidChooserContextTest, FidoAllowlistOverridesBlocklistDeviceIdRule) {}

TEST_P(HidChooserContextAffiliatedTest, FidoAllowlistAndPolicy) {}

// Boolean parameter means if user is affiliated on the device. Affiliated
// users belong to the domain that owns the device and is only meaningful
// on Chrome OS.
//
// The WebHidAllowDevicesForUrls, WebHidAllowDevicesWithHidUsagesForUrls, and
// WebHidAllowAllDevicesForUrls policies only take effect for affiliated users.
INSTANTIATE_TEST_SUITE_P();

namespace {

class HidChooserContextLoginScreenTest : public HidChooserContextTestBase,
                                         public testing::Test {};

}  // namespace

TEST_F(HidChooserContextLoginScreenTest, ApplyPolicyOnLoginScreen) {}

class HidChooserContextWebViewTest : public HidChooserContextTest {};

// Verifies that a permission for WebView can be granted successfully.
// Checks that this permission is *not* accessible outside the embedding app.
TEST_F(HidChooserContextWebViewTest, GrantDevicePermissionToWebView) {}

// Tests that revoking HID device permission from an embedding application
// simultaneously revokes permissions for any associated WebViews that were
// granted access through the embedder.
TEST_F(HidChooserContextWebViewTest, RevokeDevicePermissionFromEmbedder) {}

// Ensures that the revocation of HID device permission from a WebView does not
// interfere with the permissions held by the embedding application.
TEST_F(HidChooserContextWebViewTest, RevokeDevicePermissionFromWebView) {}

// Confirms the strict isolation of HID device permissions granted directly to a
// website's origin. Verifies that these permissions are not accessible within
// an embedded WebView, even if belonging to the same website.
TEST_F(HidChooserContextWebViewTest, WebsitePermissionDoesNotLeakToWebView) {}