chromium/chrome/browser/direct_sockets/direct_sockets_apitest.cc

// Copyright 2022 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 <type_traits>

#include "base/check_deref.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/json/json_writer.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/web_applications/test/isolated_web_app_test_utils.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_url_info.h"
#include "chrome/browser/web_applications/isolated_web_apps/test/isolated_web_app_builder.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_base.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/browser/api/sockets_udp/test_udp_echo_server.h"
#include "extensions/browser/process_manager.h"
#include "extensions/buildflags/buildflags.h"
#include "extensions/common/manifest_constants.h"
#include "net/base/host_port_pair.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features_generated.h"

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "chrome/browser/extensions/extension_apitest.h"
#include "extensions/browser/api/sockets_udp/test_udp_echo_server.h"
#include "extensions/common/extension.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/result_catcher.h"
#include "extensions/test/test_extension_dir.h"
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

namespace {

constexpr char kHostname[] =;

constexpr std::string_view kTcpReadWriteScript =;

constexpr std::string_view kUdpConnectedReadWriteScript =;

constexpr std::string_view kUdpBoundReadWriteScript =;

static constexpr std::string_view kTcpServerExchangePacketWithTcpScript =;

#if BUILDFLAG(ENABLE_EXTENSIONS)

base::Value::Dict GenerateManifest(
    std::optional<base::Value::Dict> socket_permissions = {}

auto AccessBlocked() {}

auto NotDefined(std::string_view symbol) {}

auto ErrorIs(const auto& matcher) {
  return testing::Field(&content::EvalJsResult::error, matcher);
}

#endif

class TestServer {};

class TcpHttpTestServer : public TestServer {};

class UdpEchoTestServer : public TestServer {};

template <typename TestHarness>
  requires(std::is_base_of_v<InProcessBrowserTest, TestHarness>)
class ChromeDirectSocketsTest : public TestHarness {};

template <typename TestHarness>
class ChromeDirectSocketsTcpTest : public ChromeDirectSocketsTest<TestHarness> {};

template <typename TestHarness>
class ChromeDirectSocketsUdpTest : public ChromeDirectSocketsTest<TestHarness> {};

#if BUILDFLAG(ENABLE_EXTENSIONS)

class ChromeAppApiTest : public extensions::ExtensionApiTest {};

ChromeDirectSocketsTcpApiTest;

IN_PROC_BROWSER_TEST_F(ChromeDirectSocketsTcpApiTest, TcpReadWrite) {}

IN_PROC_BROWSER_TEST_F(ChromeDirectSocketsTcpApiTest,
                       TcpSocketUndefinedWithoutSocketsPermission) {}

IN_PROC_BROWSER_TEST_F(ChromeDirectSocketsTcpApiTest,
                       TcpFailsWithoutSocketsTcpConnectPermission) {}

ChromeDirectSocketsUdpApiTest;

IN_PROC_BROWSER_TEST_F(ChromeDirectSocketsUdpApiTest, UdpReadWrite) {}

IN_PROC_BROWSER_TEST_F(ChromeDirectSocketsUdpApiTest,
                       UdpSocketUndefinedWithoutSocketsPermission) {}

IN_PROC_BROWSER_TEST_F(ChromeDirectSocketsUdpApiTest,
                       UdpConnectedFailsWithoutSocketsUdpSendPermission) {}

IN_PROC_BROWSER_TEST_F(ChromeDirectSocketsUdpApiTest,
                       UdpBoundFailsWithoutSocketsUdpBindPermission) {}

IN_PROC_BROWSER_TEST_F(ChromeDirectSocketsUdpApiTest, UdpServerReadWrite) {}

ChromeDirectSocketsTcpServerApiTest;

IN_PROC_BROWSER_TEST_F(ChromeDirectSocketsTcpServerApiTest,
                       TcpServerSocketUndefinedWithoutSocketsPermission) {}

IN_PROC_BROWSER_TEST_F(ChromeDirectSocketsTcpServerApiTest,
                       TcpServerFailsWithoutSocketsTcpServerListenPermission) {}

IN_PROC_BROWSER_TEST_F(ChromeDirectSocketsTcpServerApiTest,
                       TcpServerExchangePacketWithTcpSocket) {}

#endif

class IsolatedWebAppApiTest : public web_app::IsolatedWebAppBrowserTestHarness {};

class IsolatedWebAppApiTestWithDirectSocketsEnabled
    : public IsolatedWebAppApiTest {};

ChromeDirectSocketsTcpIsolatedWebAppTest;

IN_PROC_BROWSER_TEST_F(ChromeDirectSocketsTcpIsolatedWebAppTest, TcpReadWrite) {}

ChromeDirectSocketsUdpIsolatedWebAppTest;

IN_PROC_BROWSER_TEST_F(ChromeDirectSocketsUdpIsolatedWebAppTest, UdpReadWrite) {}

IN_PROC_BROWSER_TEST_F(ChromeDirectSocketsUdpIsolatedWebAppTest,
                       UdpServerReadWrite) {}

ChromeDirectSocketsTcpServerIsolatedWebAppTest;

IN_PROC_BROWSER_TEST_F(ChromeDirectSocketsTcpServerIsolatedWebAppTest,
                       TcpServerExchangePacketWithTcpSocket) {}

enum class FeatureState {};

template <typename TestHarness>
class ChromeDirectSocketsApiAvailabilityTest
    : public TestHarness,
      public testing::WithParamInterface<FeatureState> {};

ChromeDirectSocketsApiAvailabilityChromeAppsTest;

IN_PROC_BROWSER_TEST_P(ChromeDirectSocketsApiAvailabilityChromeAppsTest,
                       WithFeatureState) {}

INSTANTIATE_TEST_SUITE_P();

ChromeDirectSocketsApiAvailabilityIsolatedWebAppsTest;

IN_PROC_BROWSER_TEST_P(ChromeDirectSocketsApiAvailabilityIsolatedWebAppsTest,
                       WithFeatureState) {}

INSTANTIATE_TEST_SUITE_P();

}  // namespace