File: fake_origin_test_server_mixin.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (122 lines) | stat: -rw-r--r-- 4,422 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2024 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/app_mode/test/fake_origin_test_server_mixin.h"

#include <cstddef>
#include <string>
#include <string_view>
#include <utility>

#include "base/base_paths.h"
#include "base/check.h"
#include "base/check_deref.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/strings/strcat.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/base/mixin_based_in_process_browser_test.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "services/network/public/cpp/network_switches.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace ash {

using net::test_server::EmbeddedTestServer;

namespace {

EmbeddedTestServer::Type SchemeTypeOf(const GURL& origin) {
  CHECK(origin.SchemeIs("http") || origin.SchemeIs("https"));
  return origin.SchemeIs("http") ? EmbeddedTestServer::TYPE_HTTP
                                 : EmbeddedTestServer::TYPE_HTTPS;
}

// Returns "MAP example.com 127.0.0.1:1234", where "example.com" is the host in
// `origin` and "127.0.0.1:1234" is the host and port of `server`.
std::string MapOriginToServer(const GURL& origin,
                              const EmbeddedTestServer& server) {
  return base::StrCat(
      {"MAP ", origin.host_piece(), " ", server.host_port_pair().ToString()});
}

// Appends the given `rule` to `kHostResolverRules`, maintaining existing rules.
void AppendHostResolverRule(base::CommandLine* command_line,
                            std::string_view rule) {
  if (!command_line->HasSwitch(network::switches::kHostResolverRules)) {
    return command_line->AppendSwitchASCII(
        network::switches::kHostResolverRules, rule);
  }

  std::string existing_rules =
      command_line->GetSwitchValueASCII(network::switches::kHostResolverRules);
  command_line->RemoveSwitch(network::switches::kHostResolverRules);
  command_line->AppendSwitchASCII(network::switches::kHostResolverRules,
                                  base::StrCat({existing_rules, ",", rule}));
}

void LogUrl(const net::test_server::HttpRequest& request) {
  LOG(INFO) << "Received request for url '" << request.GetURL() << "'";
}

}  // namespace

FakeOriginTestServerMixin::FakeOriginTestServerMixin(
    InProcessBrowserTestMixinHost* host,
    GURL origin,
    base::FilePath::StringViewType path_to_be_served)
    : InProcessBrowserTestMixin(host),
      origin_(std::move(origin)),
      path_to_be_served_(path_to_be_served),
      server_(SchemeTypeOf(origin_)) {
  CHECK(origin_.is_valid());
  CHECK(origin_.has_scheme());
  CHECK(origin_.has_host());
  CHECK(!origin_.has_path() || origin_.path_piece() == "/");
  CHECK(!origin_.has_query());
  CHECK(!origin_.has_username());
  CHECK(!origin_.has_password());
  CHECK(!path_to_be_served_.value().starts_with("/"))
      << "path_to_be_served_ must be relative to Chrome's src/";

  // Generate SSL certificates for `origin_` on HTTPS.
  if (SchemeTypeOf(origin_) == EmbeddedTestServer::TYPE_HTTPS) {
    server_.SetCertHostnames({origin_.host()});
  }
}

FakeOriginTestServerMixin::~FakeOriginTestServerMixin() = default;

void FakeOriginTestServerMixin::SetUp() {
  auto src_path = base::PathService::CheckedGet(base::DIR_SRC_TEST_DATA_ROOT);
  server_.ServeFilesFromDirectory(src_path.Append(path_to_be_served_));
  server_.RegisterRequestMonitor(base::BindRepeating(&LogUrl));
  CHECK(server_.InitializeAndListen());
}

void FakeOriginTestServerMixin::SetUpCommandLine(
    base::CommandLine* command_line) {
  // Set the `kHostResolverRules` switch so Chrome forwards request made to
  // `origin_` to the given `server_`.
  auto rule = MapOriginToServer(origin_, server_);
  AppendHostResolverRule(command_line, rule);
  LOG(INFO) << "Configured host resolver rule '" << rule << "'";
}

void FakeOriginTestServerMixin::SetUpOnMainThread() {
  server_handle_ = server_.StartAcceptingConnectionsAndReturnHandle();
}

GURL FakeOriginTestServerMixin::GetUrl(std::string_view url_suffix) const {
  CHECK(url_suffix.starts_with("/"))
      << "URL suffix must start with '/': " << url_suffix;
  return origin_.Resolve(url_suffix);
}

}  // namespace ash