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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <fcntl.h>
#include <memory>
#include <string>
#include <tuple>
#include <vector>
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/posix/eintr_wrapper.h"
#include "base/strings/pattern.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/test/test_timeouts.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "components/headless/policy/headless_mode_policy.h"
#include "components/headless/test/capture_std_stream.h"
#include "components/policy/core/browser/browser_policy_connector_base.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/policy_map.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "headless/lib/browser/headless_browser_impl.h"
#include "headless/public/headless_browser.h"
#include "headless/public/switches.h"
#include "headless/test/headless_browser_test.h"
#include "headless/test/headless_browser_test_utils.h"
#include "net/base/host_port_pair.h"
#include "net/base/net_errors.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#if !BUILDFLAG(IS_WIN)
#include <unistd.h>
#endif
namespace headless {
// The following enum values must match HeadlessMode policy template in
// components/policy/resources/templates/policy_definitions/Miscellaneous/HeadlessMode.yaml
enum {
kHeadlessModePolicyEnabled = 1,
kHeadlessModePolicyDisabled = 2,
kHeadlessModePolicyUnset = -1, // not in the template
};
class HeadlessBrowserTestWithPolicy : public HeadlessBrowserTest {
protected:
// Implement to set policies before headless browser is instantiated.
virtual void SetPolicy() {}
void SetUp() override {
mock_provider_ = std::make_unique<
testing::NiceMock<policy::MockConfigurationPolicyProvider>>();
mock_provider_->SetDefaultReturns(
/*is_initialization_complete_return=*/false,
/*is_first_policy_load_complete_return=*/false);
policy::BrowserPolicyConnectorBase::SetPolicyProviderForTesting(
mock_provider_.get());
SetPolicy();
HeadlessBrowserTest::SetUp();
}
void SetUpCommandLine(base::CommandLine* command_line) override {
ASSERT_TRUE(user_data_dir_.CreateUniqueTempDir());
command_line->AppendSwitchPath(switches::kUserDataDir,
user_data_dir_.GetPath());
}
void TearDown() override {
HeadlessBrowserTest::TearDown();
mock_provider_->Shutdown();
policy::BrowserPolicyConnectorBase::SetPolicyProviderForTesting(nullptr);
}
PrefService* GetPrefs() {
return static_cast<HeadlessBrowserImpl*>(browser())->GetPrefs();
}
base::ScopedTempDir user_data_dir_;
std::unique_ptr<policy::MockConfigurationPolicyProvider> mock_provider_;
};
class HeadlessBrowserTestWithHeadlessModePolicy
: public HeadlessBrowserTestWithPolicy,
public testing::WithParamInterface<std::tuple<int, bool>> {
protected:
void SetPolicy() override {
int headless_mode_policy = std::get<0>(GetParam());
if (headless_mode_policy != kHeadlessModePolicyUnset) {
SetHeadlessModePolicy(
static_cast<HeadlessModePolicy::HeadlessMode>(headless_mode_policy));
}
}
void SetHeadlessModePolicy(HeadlessModePolicy::HeadlessMode headless_mode) {
policy::PolicyMap policy;
policy.Set("HeadlessMode", policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
base::Value(static_cast<int>(headless_mode)),
/*external_data_fetcher=*/nullptr);
mock_provider_->UpdateChromePolicy(policy);
}
bool expected_enabled() { return std::get<1>(GetParam()); }
bool actual_enabled() {
return !HeadlessModePolicy::IsHeadlessModeDisabled(GetPrefs());
}
};
INSTANTIATE_TEST_SUITE_P(
/* no prefix */,
HeadlessBrowserTestWithHeadlessModePolicy,
testing::Values(std::make_tuple(kHeadlessModePolicyEnabled, true),
std::make_tuple(kHeadlessModePolicyDisabled, false),
std::make_tuple(kHeadlessModePolicyUnset, true)));
IN_PROC_BROWSER_TEST_P(HeadlessBrowserTestWithHeadlessModePolicy,
HeadlessModePolicySettings) {
EXPECT_EQ(actual_enabled(), expected_enabled());
}
class HeadlessBrowserTestWithUrlBlockPolicy
: public HeadlessBrowserTestWithPolicy {
protected:
void SetPolicy() override {
base::Value::List value;
value.Append("*/blocked.html");
policy::PolicyMap policy;
policy.Set("URLBlocklist", policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
base::Value(std::move(value)),
/*external_data_fetcher=*/nullptr);
mock_provider_->UpdateChromePolicy(policy);
}
};
IN_PROC_BROWSER_TEST_F(HeadlessBrowserTestWithUrlBlockPolicy, BlockUrl) {
EXPECT_TRUE(embedded_test_server()->Start());
HeadlessBrowserContext* browser_context =
browser()->CreateBrowserContextBuilder().Build();
GURL url = embedded_test_server()->GetURL("/blocked.html");
HeadlessWebContents* web_contents =
browser_context->CreateWebContentsBuilder().SetInitialURL(url).Build();
net::Error error = net::OK;
EXPECT_FALSE(WaitForLoad(web_contents, &error));
EXPECT_EQ(error, net::ERR_BLOCKED_BY_ADMINISTRATOR);
}
class HeadlessBrowserTestWithRemoteDebuggingAllowedPolicy
: public HeadlessBrowserTestWithPolicy,
public testing::WithParamInterface<bool> {
protected:
void SetPolicy() override {
policy::PolicyMap policy;
policy.Set("RemoteDebuggingAllowed", policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
base::Value(expect_remote_debugging_available()),
/*external_data_fetcher=*/nullptr);
mock_provider_->UpdateChromePolicy(policy);
}
void SetUpCommandLine(base::CommandLine* command_line) override {
HeadlessBrowserTestWithPolicy::SetUpCommandLine(command_line);
command_line->AppendSwitchASCII(::switches::kRemoteDebuggingPort, "0");
}
void SetUpInProcessBrowserTestFixture() override {
HeadlessBrowserTestWithPolicy::SetUpInProcessBrowserTestFixture();
capture_stderr_.StartCapture();
}
bool expect_remote_debugging_available() { return GetParam(); }
CaptureStdErr capture_stderr_;
};
INSTANTIATE_TEST_SUITE_P(/* no prefix */,
HeadlessBrowserTestWithRemoteDebuggingAllowedPolicy,
testing::Values(true, false));
// Remote debugging with ephemeral port is not working on Fuchsia, see
// crbug.com/1209251.
#if BUILDFLAG(IS_FUCHSIA)
#define MAYBE_RemoteDebuggingDisallowed DISABLED_RemoteDebuggingDisallowed
#else
#define MAYBE_RemoteDebuggingDisallowed RemoteDebuggingDisallowed
#endif
IN_PROC_BROWSER_TEST_P(HeadlessBrowserTestWithRemoteDebuggingAllowedPolicy,
MAYBE_RemoteDebuggingDisallowed) {
// DevTools starts its remote debugging port listener asynchronously and
// there is no reliable way to know when it is started, so resort to an
// ugly wait then check captured stderr.
base::PlatformThread::Sleep(TestTimeouts::action_timeout());
capture_stderr_.StopCapture();
std::vector<std::string> captured_lines =
base::SplitString(capture_stderr_.TakeCapturedData(), "\n",
base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
enum { kUnknown, kDisallowed, kListening } remote_debugging_state = kUnknown;
for (const std::string& line : captured_lines) {
LOG(INFO) << "stderr: " << line;
if (base::MatchPattern(line, "DevTools remote debugging is disallowed *")) {
EXPECT_EQ(remote_debugging_state, kUnknown);
remote_debugging_state = kDisallowed;
} else if (base::MatchPattern(line, "DevTools listening on *")) {
EXPECT_EQ(remote_debugging_state, kUnknown);
remote_debugging_state = kListening;
}
}
EXPECT_NE(remote_debugging_state, kUnknown);
if (expect_remote_debugging_available())
EXPECT_EQ(remote_debugging_state, kListening);
else
EXPECT_EQ(remote_debugging_state, kDisallowed);
}
} // namespace headless
|