Skip to content

Truly independent web browser

test-web: Signal the HTTP server to exit through stdin EOF

Zaggy1024a4d5dce7a90c
diff --git a/Tests/LibWeb/Fixtures/http-test-server.py b/Tests/LibWeb/Fixtures/http-test-server.py
index 4e6e6266ccc..6d2feaa06ef 100755
--- a/Tests/LibWeb/Fixtures/http-test-server.py
+++ b/Tests/LibWeb/Fixtures/http-test-server.py
@@ -914,7 +914,7 @@ def _setup_aia_test_servers(http_port, ca_cert_output):
logging.info("AIA test servers ready: %s (root CA at %s)", config, ca_cert_output)
-def start_server(port, static_directory, ca_cert_output=None):
+def start_server(port, static_directory, ca_cert_output=None, exit_when_stdin_closes=False):
TestHTTPRequestHandler.static_directory = os.path.abspath(static_directory)
TestHTTPRequestHandler.wpt_directory = os.path.join(
TestHTTPRequestHandler.static_directory, "Text", "input", "wpt-import"
@@ -954,6 +954,14 @@ def start_server(port, static_directory, ca_cert_output=None):
print(httpd.socket.getsockname()[1])
sys.stdout.flush()
+ if exit_when_stdin_closes:
+
+ def shut_down_when_stdin_closes():
+ sys.stdin.buffer.read()
+ httpd.shutdown()
+
+ threading.Thread(target=shut_down_when_stdin_closes, daemon=True).start()
+
try:
httpd.serve_forever()
except KeyboardInterrupt:
@@ -980,6 +988,11 @@ if __name__ == "__main__":
default=0,
help="Port to run the server on",
)
+ parser.add_argument(
+ "--exit-when-stdin-closes",
+ action="store_true",
+ help="Shut down once stdin reaches end-of-file, as when the process that started the server closes it or exits",
+ )
parser.add_argument(
"--ca-cert-output",
type=str,
@@ -988,4 +1001,9 @@ if __name__ == "__main__":
)
args = parser.parse_args()
- start_server(port=args.port, static_directory=args.directory, ca_cert_output=args.ca_cert_output)
+ start_server(
+ port=args.port,
+ static_directory=args.directory,
+ ca_cert_output=args.ca_cert_output,
+ exit_when_stdin_closes=args.exit_when_stdin_closes,
+ )
diff --git a/Tests/LibWeb/test-web/Fixture.cpp b/Tests/LibWeb/test-web/Fixture.cpp
index b97f25a4b12..d4515857e91 100644
--- a/Tests/LibWeb/test-web/Fixture.cpp
+++ b/Tests/LibWeb/test-web/Fixture.cpp
@@ -9,9 +9,11 @@
#include <AK/ByteBuffer.h>
#include <AK/LexicalPath.h>
+#include <LibCore/File.h>
#include <LibCore/Process.h>
#include <LibCore/StandardPaths.h>
#include <LibCore/System.h>
+#include <LibFileSystem/FileSystem.h>
#include <fcntl.h>
namespace TestWeb {
@@ -46,46 +48,38 @@ public:
private:
ByteString m_script_path { "http-test-server.py" };
Optional<Core::Process> m_process;
+ int m_stdin_fd { -1 };
};
-#if defined(AK_OS_WINDOWS)
-
-ErrorOr<void> HttpEchoServerFixture::setup(WebView::WebContentOptions&)
-{
- VERIFY(0 && "HttpEchoServerFixture::setup is not implemented");
-}
-
-void HttpEchoServerFixture::teardown_impl()
-{
- VERIFY(0 && "HttpEchoServerFixture::teardown_impl is not implemented");
-}
-
-#else
-
ErrorOr<void> HttpEchoServerFixture::setup(WebView::WebContentOptions& web_content_options)
{
auto const script_path = LexicalPath::join(s_fixtures_path, m_script_path);
// Path where http-test-server.py writes the AIA test root CA. Must match the certificate trusted by
// TestWeb::Application::create_platform_options.
auto const ca_cert_path = LexicalPath::join(Core::StandardPaths::tempfile_directory(), ByteString::formatted("ladybird-aia-test-ca-{}.pem", Core::System::getpid())).string();
- auto const arguments = Vector { script_path.string(), "--directory", Application::the().test_root_path, "--ca-cert-output", ca_cert_path };
+ auto const arguments = Vector { script_path.string(), "--directory", Application::the().test_root_path, "--ca-cert-output", ca_cert_path, "--exit-when-stdin-closes" };
// FIXME: Pick a more reasonable log path that is more observable
auto const log_path = LexicalPath::join(Core::StandardPaths::tempfile_directory(), "http-test-server.log"sv).string();
+ auto stdin_fds = TRY(Core::System::pipe2(O_CLOEXEC));
auto stdout_fds = TRY(Core::System::pipe2(O_CLOEXEC));
+ auto stderr_file = TRY(Core::File::open(ByteString::formatted("{}.stderr", log_path), Core::File::OpenMode::Write));
auto const process_options = Core::ProcessSpawnOptions {
.executable = Application::the().python_executable_path,
.search_for_executable_in_path = true,
.arguments = arguments,
.file_actions = {
- Core::FileAction::OpenFile { ByteString::formatted("{}.stderr", log_path), Core::File::OpenMode::Write, STDERR_FILENO },
+ Core::FileAction::DupFd { stdin_fds[0], STDIN_FILENO },
+ Core::FileAction::DupFd { stderr_file->fd(), STDERR_FILENO },
Core::FileAction::DupFd { stdout_fds[1], STDOUT_FILENO } }
};
m_process = TRY(Core::Process::spawn(process_options));
+ TRY(Core::System::close(stdin_fds[0]));
+ m_stdin_fd = stdin_fds[1];
TRY(Core::System::close(stdout_fds[1]));
auto const stdout_file = MUST(Core::File::adopt_fd(stdout_fds[0], Core::File::OpenMode::Read));
@@ -107,23 +101,17 @@ void HttpEchoServerFixture::teardown_impl()
{
VERIFY(m_process.has_value());
- auto script_path = LexicalPath::join(s_fixtures_path, m_script_path);
-
- if (auto kill_or_error = Core::System::kill(m_process->pid(), SIGINT); kill_or_error.is_error()) {
- if (kill_or_error.error().code() != ESRCH) {
- warnln("Failed to kill HTTP echo server, error: {}", kill_or_error.error());
- } else if (auto termination_or_error = m_process->wait_for_termination(); termination_or_error.is_error()) {
- warnln("Failed to terminate HTTP echo server, error: {}", termination_or_error.error());
- }
- }
+ // Close the server's stdin to signal it to exit.
+ if (auto close_or_error = Core::System::close(exchange(m_stdin_fd, -1)); close_or_error.is_error())
+ warnln("Failed to close HTTP echo server's stdin, error: {}", close_or_error.error());
+ else if (auto termination_or_error = m_process->wait_for_termination(); termination_or_error.is_error())
+ warnln("Failed to wait for HTTP echo server to exit, error: {}", termination_or_error.error());
m_process = {};
auto const ca_cert_path = LexicalPath::join(Core::StandardPaths::tempfile_directory(), ByteString::formatted("ladybird-aia-test-ca-{}.pem", Core::System::getpid())).string();
- (void)Core::System::unlink(ca_cert_path);
+ (void)FileSystem::remove(ca_cert_path, FileSystem::RecursionMode::Disallowed);
}
-#endif
-
void Fixture::initialize_fixtures()
{
s_fixtures_path = LexicalPath::join(Application::the().test_root_path, "Fixtures"sv).string();