chromium/content/web_test/browser/web_test_browser_main_runner.cc

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

#include "content/web_test/browser/web_test_browser_main_runner.h"

#include <iostream>
#include <memory>

#include "base/check_op.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/run_loop.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "cc/base/switches.h"
#include "components/network_session_configurator/common/network_switches.h"
#include "components/viz/common/switches.h"
#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "content/public/browser/browser_main_runner.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/url_constants.h"
#include "content/shell/browser/shell.h"
#include "content/shell/common/shell_switches.h"
#include "content/test/gpu_browsertest_helpers.h"
#include "content/web_test/browser/test_info_extractor.h"
#include "content/web_test/browser/web_test_browser_main_platform_support.h"
#include "content/web_test/browser/web_test_control_host.h"
#include "content/web_test/common/web_test_switches.h"
#include "gpu/config/gpu_switches.h"
#include "gpu/ipc/client/gpu_channel_host.h"
#include "media/base/media_switches.h"
#include "net/base/filename_util.h"
#include "net/base/ip_address.h"
#include "net/base/ip_endpoint.h"
#include "net/base/url_util.h"
#include "ppapi/buildflags/buildflags.h"
#include "services/network/public/cpp/network_switches.h"
#include "ui/base/ui_base_switches.h"
#include "ui/display/display_switches.h"
#include "ui/gl/gl_implementation.h"
#include "ui/gl/gl_switches.h"

#if BUILDFLAG(ENABLE_PPAPI)
#include "content/public/test/ppapi_test_utils.h"
#endif

#if BUILDFLAG(IS_FUCHSIA) || BUILDFLAG(IS_IOS)
#include <sys/socket.h>
#include <unistd.h>
#endif

#if BUILDFLAG(IS_FUCHSIA)
#include <lib/sys/cpp/component_context.h>

#include "base/fuchsia/fuchsia_logging.h"
#include "base/fuchsia/process_context.h"
#include "ui/ozone/public/ozone_switches.h"
#endif

namespace content {

namespace {

#if BUILDFLAG(IS_FUCHSIA) || BUILDFLAG(IS_IOS)
// Fuchsia doesn't support stdin stream for packaged apps, and stdout from
// run-test-suite not only has extra emissions from the Fuchsia test
// infrastructure, it also merges stderr and stdout together. Combined, these
// mean that when running content_shell on Fuchsia it's not possible to use
// stdin to pass list of tests or to reliably use stdout to emit results. To
// workaround this issue for web tests we redirect stdin and stdout to a TCP
// socket connected to the web test runner. The runner uses --stdio-redirect to
// specify address and port for stdin and stdout redirection.
//
// iOS is in a similar situation where the simulator does not support the use of
// the stdin stream for applications. Therefore, iOS also redirects stdin and
// stdout to a TCP socket that is connected to the web test runner.
constexpr char kStdioRedirectSwitch[] = "stdio-redirect";

void ConnectStdioSocket(const std::string& host_and_port) {
  std::string host;
  int port;
  net::IPAddress address;
  if (!net::ParseHostAndPort(host_and_port, &host, &port) ||
      !address.AssignFromIPLiteral(host)) {
    LOG(FATAL) << "Invalid stdio address: " << host_and_port;
  }

  sockaddr_storage sockaddr_storage;
  sockaddr* addr = reinterpret_cast<sockaddr*>(&sockaddr_storage);
  socklen_t addr_len = sizeof(sockaddr_storage);
  net::IPEndPoint endpoint(address, port);
  bool converted = endpoint.ToSockAddr(addr, &addr_len);
  CHECK(converted);

  int fd = socket(addr->sa_family, SOCK_STREAM, 0);
  PCHECK(fd >= 0);
  int result = connect(fd, addr, addr_len);
  PCHECK(result == 0) << "Failed to connect to " << host_and_port;

  result = dup2(fd, STDIN_FILENO);
  PCHECK(result == STDIN_FILENO) << "Failed to dup socket to stdin";

  result = dup2(fd, STDOUT_FILENO);
  PCHECK(result == STDOUT_FILENO) << "Failed to dup socket to stdout";

  PCHECK(close(fd) == 0);
}

#endif  // BUILDFLAG(IS_FUCHSIA)

void RunOneTest(const content::TestInfo& test_info,
                content::WebTestControlHost* web_test_control_host,
                content::BrowserMainRunner* main_runner) {}

void RunTests(content::BrowserMainRunner* main_runner) {}

}  // namespace

void WebTestBrowserMainRunner::Initialize() {}

void WebTestBrowserMainRunner::RunBrowserMain(
    content::MainFunctionParams parameters) {}

}  // namespace content