Truly independent web browser
/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "HeadlessTest.h"
#include <AK/Array.h>
#include <AK/Atomic.h>
#include <AK/LexicalPath.h>
#include <AK/StringBuilder.h>
#include <LibCore/EventLoop.h>
#include <LibCore/File.h>
#include <LibCore/System.h>
#include <LibCore/Timer.h>
#include <LibGfx/SystemTheme.h>
#include <LibTest/TestCase.h>
#include <LibThreading/Thread.h>
#include <LibURL/Parser.h>
#include <LibWebView/FileDownloader.h>
#include <LibWebView/HeadlessWebView.h>
#include <LibWebView/Utilities.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/socket.h>
namespace {
// Answers a request for its page with a small document, and every other request with the same response, pausing
// after the first pause_after bytes of its body when given.
class TestHttpServer {
public:
TestHttpServer(ByteString headers, ByteBuffer body, Optional<size_t> pause_after)
: m_headers(move(headers))
, m_body(move(body))
, m_pause_after(pause_after)
{
m_socket = MUST(Core::System::socket(AF_INET, SOCK_STREAM, 0));
int reuse_address = 1;
MUST(Core::System::setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, &reuse_address, sizeof(reuse_address)));
sockaddr_in address {};
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
MUST(Core::System::bind(m_socket, reinterpret_cast<sockaddr*>(&address), sizeof(address)));
MUST(Core::System::listen(m_socket, 16));
// accept() must not block when a connection that poll() reported is gone by then.
MUST(Core::System::fcntl(m_socket, F_SETFL, O_NONBLOCK));
socklen_t address_size = sizeof(address);
VERIFY(::getsockname(m_socket, reinterpret_cast<sockaddr*>(&address), &address_size) == 0);
m_port = ntohs(address.sin_port);
m_thread = Threading::Thread::construct("TestHttpServer"sv, [this] -> intptr_t {
run();
return 0;
});
m_thread->start();
}
~TestHttpServer()
{
m_shutting_down.store(true);
(void)m_thread->join();
(void)Core::System::close(m_socket);
}
URL::URL page_url() const
{
return URL::Parser::basic_parse(ByteString::formatted("http://127.0.0.1:{}/page", m_port)).release_value();
}
URL::URL url(bool cross_site = false) const
{
return URL::Parser::basic_parse(ByteString::formatted("http://{}:{}/file", cross_site ? "localhost" : "127.0.0.1", m_port)).release_value();
}
URL::URL redirect_url() const
{
return URL::Parser::basic_parse(ByteString::formatted("http://127.0.0.1:{}/redirect", m_port)).release_value();
}
// The number of requests for url(), not counting the page.
size_t request_count() const { return m_request_count.load(); }
private:
// A connection made to wake accept() can be lost to a signal, so poll and recheck for shutdown instead.
void run()
{
while (!m_shutting_down.load()) {
pollfd poll_fd { .fd = m_socket, .events = POLLIN, .revents = 0 };
auto ready = Core::System::poll({ &poll_fd, 1 }, 100);
if (ready.is_error() && ready.error().code() != EINTR)
return;
if (ready.is_error() || ready.value() == 0)
continue;
auto client = ::accept(m_socket, nullptr, nullptr);
if (client < 0) {
if (errno == EAGAIN || errno == ECONNABORTED)
continue;
return;
}
// Accepted sockets inherit O_NONBLOCK on some platforms.
MUST(Core::System::fcntl(client, F_SETFL, 0));
respond(client);
(void)Core::System::close(client);
}
}
void respond(int client)
{
StringBuilder request;
Array<u8, 4096> buffer;
while (!request.string_view().contains("\r\n\r\n"sv)) {
auto read = Core::System::read(client, buffer);
if (read.is_error() || read.value() == 0)
return;
request.append(StringView { buffer.span().trim(read.value()) });
}
if (request.string_view().starts_with("GET /page "sv)) {
auto page = "<!DOCTYPE html><title>Downloads</title>"sv;
auto response = ByteString::formatted("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}", page.length(), page);
(void)write_all(client, response.bytes());
return;
}
if (request.string_view().starts_with("GET /redirect "sv)) {
auto response = ByteString::formatted("HTTP/1.1 302 Found\r\nLocation: {}\r\nContent-Length: 0\r\nConnection: close\r\n\r\n", url(true));
(void)write_all(client, response.bytes());
return;
}
m_request_count.fetch_add(1);
auto head = ByteString::formatted("HTTP/1.1 200 OK\r\n{}Content-Length: {}\r\nConnection: close\r\n\r\n", m_headers, m_body.size());
if (!write_all(client, head.bytes()))
return;
auto body = m_body.bytes();
if (m_pause_after.has_value()) {
if (!write_all(client, body.trim(*m_pause_after)))
return;
MUST(Core::System::sleep_ms(1000));
body = body.slice(*m_pause_after);
}
(void)write_all(client, body);
}
static bool write_all(int fd, ReadonlyBytes bytes)
{
while (!bytes.is_empty()) {
auto written = Core::System::write(fd, bytes);
if (written.is_error() || written.value() == 0)
return false;
bytes = bytes.slice(written.value());
}
return true;
}
ByteString m_headers;
ByteBuffer m_body;
Optional<size_t> m_pause_after;
int m_socket { -1 };
u16 m_port { 0 };
RefPtr<Threading::Thread> m_thread;
Atomic<bool> m_shutting_down { false };
Atomic<size_t> m_request_count { 0 };
};
class FinishedDownloadRecorder final : public WebView::FileDownloaderObserver {
public:
virtual void download_updated(WebView::FileDownloader::Download const& download) override
{
if (!WebView::FileDownloader::status_is_active(download.status))
m_finished = download;
}
Optional<WebView::FileDownloader::Download> const& finished() const { return m_finished; }
private:
Optional<WebView::FileDownloader::Download> m_finished;
};
ByteBuffer make_body(size_t size)
{
auto body = MUST(ByteBuffer::create_uninitialized(size));
for (size_t i = 0; i < size; ++i)
body[i] = static_cast<u8>((i * 31 + (i >> 8) * 17) & 0xff);
return body;
}
void load_page(WebView::HeadlessWebView& view, URL::URL const& url)
{
IGNORE_USE_IN_ESCAPING_LAMBDA bool loaded = false;
view.on_load_finish = [&](URL::URL const&) { loaded = true; };
view.load_from_browser_ui(url);
auto deadline = Core::Timer::create_single_shot(AK::Duration::from_seconds(30), [] { });
deadline->start();
Core::EventLoop::current().spin_until([&] { return loaded || !deadline->is_active(); });
view.on_load_finish = nullptr;
if (!loaded) {
warnln("FAIL: the page did not load within 30 seconds");
VERIFY_NOT_REACHED();
}
}
WebView::FileDownloader::Download download_navigation_response(WebView::HeadlessWebView& view, URL::URL const& url)
{
FinishedDownloadRecorder recorder;
view.load_from_browser_ui(url);
// A download that never finishes fails here, after 30 seconds.
auto deadline = Core::Timer::create_single_shot(AK::Duration::from_seconds(30), [] { });
deadline->start();
Core::EventLoop::current().spin_until([&] { return recorder.finished().has_value() || !deadline->is_active(); });
if (!recorder.finished().has_value()) {
warnln("FAIL: no download finished within 30 seconds");
VERIFY_NOT_REACHED();
}
return *recorder.finished();
}
void expect_download_holds_body(StringView what, WebView::FileDownloader::Download const& download, ByteBuffer const& body)
{
if (download.status != WebView::FileDownloader::DownloadStatus::Completed) {
warnln("FAIL: {}: the download ended with status {}", what, to_underlying(download.status));
VERIFY_NOT_REACHED();
}
auto file = MUST(Core::File::open(download.destination.string(), Core::File::OpenMode::Read));
auto contents = MUST(file->read_until_eof());
if (contents.bytes() != body.bytes()) {
warnln("FAIL: {}: the file holds {} bytes, not the {} the server sent", what, contents.size(), body.size());
VERIFY_NOT_REACHED();
}
}
void expect_navigation_downloads_body(Core::AnonymousBuffer const& theme, StringView what, ByteString headers, ByteBuffer const& body, Optional<size_t> pause_after = {})
{
TestHttpServer server { move(headers), body, pause_after };
auto view = WebView::HeadlessWebView::create(theme, { 800, 600 });
expect_download_holds_body(what, download_navigation_response(*view, server.url()), body);
}
void expect_navigation_downloads_cached_body(Core::AnonymousBuffer const& theme, StringView what, ByteString headers, ByteBuffer const& body)
{
TestHttpServer server { move(headers), body, {} };
// Both downloads start from a document of the server's site, whose process reads and writes that site's cache.
auto view = WebView::HeadlessWebView::create(theme, { 800, 600 });
load_page(*view, server.page_url());
expect_download_holds_body(what, download_navigation_response(*view, server.url()), body);
// The first download completes only after RequestServer has written its cache entry.
auto request_count = server.request_count();
expect_download_holds_body(what, download_navigation_response(*view, server.url()), body);
if (server.request_count() != request_count) {
warnln("FAIL: {}: the second download did not come from the disk cache", what);
VERIFY_NOT_REACHED();
}
}
void expect_cross_site_navigation_downloads_body(Core::AnonymousBuffer const& theme, StringView what, ByteString headers, ByteBuffer const& body, bool redirect, Optional<size_t> pause_after = {})
{
TestHttpServer server { move(headers), body, pause_after };
auto view = WebView::HeadlessWebView::create(theme, { 800, 600 });
load_page(*view, server.page_url());
expect_download_holds_body(what, download_navigation_response(*view, redirect ? server.redirect_url() : server.url(true)), body);
EXPECT(!view->is_loading());
EXPECT_EQ(view->url(), server.page_url());
}
}
TEST_CASE(navigation_downloads)
{
Test::HeadlessTestEnvironment environment { "TestNavigationDownloads"sv, 120 };
auto const& test_directory = environment.directory();
VERIFY(setenv("XDG_CACHE_HOME", test_directory.characters(), 1) == 0);
VERIFY(setenv("XDG_DOWNLOAD_DIR", test_directory.characters(), 1) == 0);
signal(SIGPIPE, SIG_IGN);
auto app = TRY_OR_FAIL(environment.create_application());
auto theme_path = LexicalPath::join(WebView::s_ladybird_resource_root, "themes"sv, "Default.ini"sv);
auto theme = TRY_OR_FAIL(Gfx::load_system_theme(theme_path.string()));
auto body = make_body(1 * MiB);
expect_navigation_downloads_body(theme, "an attachment"sv, "Content-Disposition: attachment; filename=\"attachment.bin\"\r\n"sv, body);
expect_navigation_downloads_body(theme, "a response sniffed as unrenderable"sv, "Content-Type: application/octet-stream\r\n"sv, body);
expect_navigation_downloads_body(theme, "a response sniffed from bytes that arrive in parts"sv, "Content-Type: application/octet-stream\r\n"sv, body, 500);
expect_navigation_downloads_cached_body(theme, "a response sniffed from the disk cache"sv, "Cache-Control: max-age=3600\r\nContent-Type: application/octet-stream\r\n"sv, body);
expect_navigation_downloads_cached_body(theme, "an attachment from the disk cache"sv, "Cache-Control: max-age=3600\r\nContent-Disposition: attachment; filename=\"attachment.bin\"\r\n"sv, body);
// The host of a response with an opaque origin adopts it through its client without a site.
expect_cross_site_navigation_downloads_body(theme, "a cross-site sandboxed response"sv, "Content-Security-Policy: sandbox allow-downloads\r\nContent-Type: application/octet-stream\r\n"sv, body, false);
for (bool redirect : { false, true }) {
expect_cross_site_navigation_downloads_body(theme, "a cross-site attachment"sv, "Content-Disposition: attachment; filename=\"cross-site.bin\"\r\n"sv, body, redirect);
expect_cross_site_navigation_downloads_body(theme, "a cross-site response sniffed as unrenderable"sv, "Content-Type: application/octet-stream\r\n"sv, body, redirect);
}
expect_cross_site_navigation_downloads_body(theme, "a cross-site response whose bytes arrive in parts"sv, "Content-Type: application/octet-stream\r\n"sv, body, false, 500);
outln("PASS: navigation responses download byte for byte");
}