Files
Language-Server/tests/ProcessTests.cpp
T
faumaray da8003f6d4 Implement the VLS roadmap on the native VBS build
Native VBS and CMake builds of the server and every registered suite:
protocol and document sync, VBS/compdb providers, stock GCC diagnostics
(including in-process default profile preparation), the stock occurrence
observer and hover/definition, the bounded Scheduler, C++ module graph,
scan and artifact cache with real CMI production, the context-aware
symbol index with schema-2 snapshot migration, navigation and guarded
rename, reflection provenance with splice-origin extraction, index-backed
completion and signature help, live GCC worker control with a registered
DAP suite, and the Neovim and VS Code clients.

VoidLibrary follows main. The private GCC 16.2.0 frontend is reproduced
from the versioned patches and verified stepping reflection/template code
with projected values 17/34 and 41/82.
2026-12-05 14:06:50 +03:00

342 lines
17 KiB
C++

#include "TestExecutable.hpp"
#include "TestSupport.hpp"
#include <Void/System/Process.hpp>
#include <algorithm>
#include <cerrno>
#include <chrono>
#include <poll.h>
#include <stdexcept>
#include <tuple>
#include <unistd.h>
namespace {
using namespace std::chrono_literals;
Void::Process::Child spawn(const std::vector<std::string> &arguments = {}) {
Void::Process::SpawnOptions options;
options.executable = test::server_executable();
options.arguments = arguments;
options.search_path = false;
options.kill_grace = 100ms;
auto child = Void::Process::spawn(options);
Void::Testing::require(child.has_value());
return std::move(*child);
}
Void::Process::CommunicateResult finish(Void::Process::Child &child, std::string_view input = {}) {
Void::Process::CaptureLimits limits;
limits.max_stdin_bytes = 65536;
limits.max_stdout_bytes = 65536;
limits.max_stderr_bytes = 16384;
auto output = Void::Process::communicate_blocking(child, input, limits, 2s);
Void::Testing::require(output.has_value());
Void::Testing::require(!output->timed_out && !output->cancelled && !output->overflowed);
Void::Testing::require(output->status.exited);
return std::move(*output);
}
Void::Process::CommunicateResult run(std::string_view input = {},
const std::vector<std::string> &arguments = {}) {
auto child = spawn(arguments);
return finish(child, input);
}
void write_fragment(Void::Process::Child &child, std::string_view fragment) {
const auto deadline = std::chrono::steady_clock::now() + 2s;
while (!fragment.empty()) {
if (std::chrono::steady_clock::now() >= deadline)
throw std::runtime_error{"child stdin deadline"};
pollfd descriptor{child.stdin_fd(), POLLOUT, 0};
const int ready = ::poll(&descriptor, 1, 20);
if (ready < 0 && errno == EINTR)
continue;
Void::Testing::require(ready >= 0);
if (ready == 0)
continue;
Void::Testing::require((descriptor.revents & POLLOUT) != 0);
// Tiny fragments fit within PIPE_BUF after writable readiness.
const auto count = ::write(child.stdin_fd(), fragment.data(), fragment.size());
if (count < 0 && errno == EINTR)
continue;
Void::Testing::require(count > 0);
fragment.remove_prefix(static_cast<std::size_t>(count));
}
}
void expect_no_early_output(Void::Process::Child &child) {
pollfd descriptor{child.stdout_fd(), POLLIN, 0};
const int ready = ::poll(&descriptor, 1, 20);
Void::Testing::require(ready >= 0);
Void::Testing::expect_eq(ready, 0);
}
std::string read_early_response(Void::Process::Child &child) {
const auto deadline = std::chrono::steady_clock::now() + 2s;
std::string bytes;
while (std::chrono::steady_clock::now() < deadline && bytes.size() < 8192) {
pollfd descriptor{child.stdout_fd(), POLLIN, 0};
const int ready = ::poll(&descriptor, 1, 20);
if (ready < 0 && errno == EINTR)
continue;
Void::Testing::require(ready >= 0);
if (ready == 0)
continue;
char byte{};
Void::Testing::require(::read(child.stdout_fd(), &byte, 1) == 1);
bytes.push_back(byte);
std::istringstream probe{bytes};
const auto complete = vls::read_frame(probe);
if (complete.has_value() && complete->has_value())
return bytes;
}
throw std::runtime_error{"child response deadline or byte limit"};
}
std::string lifecycle() {
return test::frame(test::initialize_request) + test::frame(test::initialized) +
test::frame(test::shutdown) + test::frame(test::exit);
}
constexpr std::string_view open_document =
R"({"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"untitled:sync","languageId":"cpp","version":1,"text":"a"}}})";
constexpr std::string_view close_document =
R"({"jsonrpc":"2.0","method":"textDocument/didClose","params":{"textDocument":{"uri":"untitled:sync"}}})";
std::string change_document(int version, std::string_view changes) {
return test::frame(
std::string{
R"({"jsonrpc":"2.0","method":"textDocument/didChange","params":{"textDocument":{"uri":"untitled:sync","version":)"} +
std::to_string(version) + R"(},"contentChanges":)" + std::string{changes} + "}}");
}
} // namespace
VOID_TEST(ProcessTests, cli_standalone_and_invalid_modes) {
for (const auto flag : {"--help", "-h", "--version", "-V"}) {
const auto output = run({}, {flag});
Void::Testing::expect_eq(output.status.code, 0);
Void::Testing::expect(!output.out.empty());
Void::Testing::expect(output.err.empty());
Void::Testing::expect(output.out.find("Content-Length:") == std::string::npos);
Void::Testing::expect(output.out.find("VoidLanguageServer") != std::string::npos);
}
const auto version = run({}, {"--version"});
Void::Testing::expect_eq(version.out, std::string{"VoidLanguageServer 0.1.0\n"});
for (const auto &args : std::vector<std::vector<std::string>>{{"--unknown"},
{"build"},
{"--"},
{"--stdio=true"},
{"-s"},
{"--help", "--stdio"},
{"--stdio", "--help"},
{"--version", "--stdio"},
{"--version", "--help"},
{"--stdio", "--stdio"},
{"-V", "-V"}}) {
const auto output = run({}, args);
Void::Testing::expect_eq(output.status.code, 2);
Void::Testing::expect(output.out.empty());
Void::Testing::expect(!output.err.empty());
}
}
VOID_TEST(ProcessTests, default_and_explicit_stdio_lifecycle) {
for (const auto &args : std::vector<std::vector<std::string>>{{}, {"--stdio"}}) {
const auto output = run(lifecycle(), args);
Void::Testing::expect_eq(output.status.code, 0);
Void::Testing::expect(output.err.empty());
const auto replies = test::responses(output.out);
Void::Testing::require_eq(replies.size(), std::size_t{2});
const auto &result = test::field(replies[0], "result");
test::expect_sync_capabilities(result);
Void::Testing::expect(
std::holds_alternative<std::nullptr_t>(test::field(replies[1], "result").value));
}
}
VOID_TEST(ProcessTests, negotiated_incremental_edit_observes_astral_character) {
constexpr auto replace =
R"([{"range":{"start":{"line":0,"character":0},"end":{"line":0,"character":1}},"text":"\uD83D\uDE42"}])";
constexpr auto split =
R"([{"range":{"start":{"line":0,"character":1},"end":{"line":0,"character":1}},"text":"x"}])";
for (const auto &[offered, encoding, diagnostic] :
{std::tuple{R"(["utf-32","utf-8","utf-16"])", "utf-8", "splits a Unicode scalar"},
std::tuple{R"(["utf-16","utf-8"])", "utf-16", "splits a surrogate pair"}}) {
const auto initialize = test::frame(
std::string{
R"({"jsonrpc":"2.0","id":1,"method":"initialize","params":{"capabilities":{"general":{"positionEncodings":)"} +
offered + "}}}}");
auto output = run(initialize + test::frame(test::initialized) + test::frame(open_document) +
change_document(2, replace) + change_document(3, split) +
test::frame(close_document) + test::frame(test::shutdown) +
test::frame(test::exit));
Void::Testing::expect_eq(output.status.code, 0);
auto replies = test::responses(output.out);
Void::Testing::require_eq(replies.size(), std::size_t{2});
test::expect_sync_capabilities(test::field(replies[0], "result"), encoding);
Void::Testing::expect(output.err.contains(diagnostic));
Void::Testing::expect_eq(std::ranges::count(output.err, '\n'), 1);
}
}
VOID_TEST(ProcessTests, malformed_batch_blocks_edits_until_newer_full_replacement) {
constexpr auto malformed = R"([{"text":"old"},{"text":"bad","range":null}])";
constexpr auto full = R"([{"text":"a"}])";
constexpr auto ranged =
R"([{"text":"b","range":{"start":{"line":0,"character":0},"end":{"line":0,"character":1}}}])";
constexpr auto overflow =
R"([{"text":"","range":{"start":{"line":0,"character":2147483648},"end":{"line":0,"character":2147483648}}}])";
auto output =
run(test::frame(test::initialize_request) + test::frame(test::initialized) +
test::frame(open_document) + change_document(12, malformed) +
change_document(10, full) + change_document(13, ranged) + change_document(14, full) +
change_document(15, ranged) + change_document(16, overflow) +
change_document(17, full) + test::frame(close_document) + change_document(18, ranged) +
test::frame(test::shutdown) + test::frame(open_document) + test::frame(test::exit));
Void::Testing::expect_eq(output.status.code, 0);
const auto replies = test::responses(output.out);
Void::Testing::require_eq(replies.size(), std::size_t{2});
test::expect_sync_capabilities(test::field(replies[0], "result"));
Void::Testing::expect(output.err.contains("invalid change range"));
Void::Testing::expect(output.err.contains("nonmonotonic"));
Void::Testing::expect(output.err.contains("newer full-text replacement"));
Void::Testing::expect(output.err.contains("document is not open"));
Void::Testing::expect_eq(std::ranges::count(output.err, '\n'), 5);
}
VOID_TEST(ProcessTests, malformed_change_envelope_invalidates_an_identifiable_document) {
const auto hybrid = test::frame(
R"({"jsonrpc":"2.0","method":"textDocument/didChange","result":null,"params":{"textDocument":{"uri":"untitled:sync","version":12},"contentChanges":[{"text":"bad"}]}})");
constexpr auto ranged =
R"([{"range":{"start":{"line":0,"character":0},"end":{"line":0,"character":1}},"text":"b"}])";
constexpr auto full = R"([{"text":"a"}])";
auto output =
run(test::frame(test::initialize_request) + test::frame(test::initialized) +
test::frame(open_document) + hybrid + change_document(10, full) +
change_document(13, ranged) + change_document(14, full) + change_document(15, ranged) +
test::frame(close_document) + test::frame(test::shutdown) + test::frame(test::exit));
Void::Testing::expect_eq(output.status.code, 0);
Void::Testing::require_eq(test::responses(output.out).size(), std::size_t{2});
Void::Testing::expect(output.err.contains("invalid didChange envelope"));
Void::Testing::expect(output.err.contains("nonmonotonic"));
Void::Testing::expect(output.err.contains("newer full-text replacement"));
Void::Testing::expect_eq(std::ranges::count(output.err, '\n'), 3);
}
VOID_TEST(ProcessTests, fragmented_headers_bodies_and_immediate_flush) {
auto child = spawn({"--stdio"});
const auto input = test::frame(test::initialize_request);
const auto split = input.find("\r\n\r\n") + 5;
write_fragment(child, std::string_view{input}.substr(0, 10));
expect_no_early_output(child);
write_fragment(child, std::string_view{input}.substr(10, split - 10));
expect_no_early_output(child);
for (std::size_t offset = split; offset < input.size(); ++offset)
write_fragment(child, std::string_view{input}.substr(offset, 1));
// Read initialize's response while stdin is still open: detects buffering
// bugs that a single communicate-at-EOF test would miss.
const auto early = read_early_response(child);
const auto rest =
finish(child, test::frame(test::initialized) +
test::frame(R"({"jsonrpc":"2.0","method":"unknown"})") +
test::frame(R"({"jsonrpc":"2.0","id":3,"method":"unknown"})") +
test::frame(test::shutdown) + test::frame(test::exit));
Void::Testing::expect_eq(rest.status.code, 0);
Void::Testing::expect(rest.err.empty());
const auto replies = test::responses(early + rest.out);
Void::Testing::require_eq(replies.size(), std::size_t{3});
Void::Testing::expect_eq(test::error_code(replies[1]), std::int64_t{-32601});
}
VOID_TEST(ProcessTests, unmatched_peer_responses_do_not_create_reply_loops) {
const auto responses =
test::frame(R"({"jsonrpc":"2.0","id":77,"result":{"ok":true}})") +
test::frame(R"({"jsonrpc":"2.0","id":null,"result":null})") +
test::frame(
R"({"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}})") +
test::frame(
R"({"jsonrpc":"2.0","id":"unknown","error":{"code":-32601,"message":"unknown"}})");
const auto output = run(responses + test::frame(test::initialize_request) + responses +
test::frame(test::shutdown) + responses + test::frame(test::exit));
Void::Testing::expect_eq(output.status.code, 0);
Void::Testing::expect(output.err.empty());
const auto replies = test::responses(output.out);
Void::Testing::require_eq(replies.size(), std::size_t{2});
Void::Testing::expect(
std::holds_alternative<vls::Json::Object>(test::field(replies[0], "result").value));
Void::Testing::expect(
std::holds_alternative<std::nullptr_t>(test::field(replies[1], "result").value));
}
VOID_TEST(ProcessTests, known_size_malformed_json_continues) {
const auto output = run(test::frame("{bad") + test::frame("[{}]") + lifecycle());
Void::Testing::expect_eq(output.status.code, 0);
Void::Testing::expect(output.err.empty());
const auto replies = test::responses(output.out);
Void::Testing::require_eq(replies.size(), std::size_t{4});
Void::Testing::expect_eq(test::error_code(replies[0]), std::int64_t{-32700});
Void::Testing::expect(
std::holds_alternative<std::nullptr_t>(test::field(replies[0], "id").value));
Void::Testing::expect_eq(test::error_code(replies[1]), std::int64_t{-32600});
}
VOID_TEST(ProcessTests, invalid_encoding_and_surrogates_continue) {
const auto output = run(test::frame(std::string{"\"\xED\xA0\x80\""}) +
test::frame(R"({"bad":"\uDC00"})") + lifecycle());
Void::Testing::expect_eq(output.status.code, 0);
const auto replies = test::responses(output.out);
Void::Testing::require_eq(replies.size(), std::size_t{4});
Void::Testing::expect_eq(test::error_code(replies[0]), std::int64_t{-32700});
Void::Testing::expect_eq(test::error_code(replies[1]), std::int64_t{-32700});
}
VOID_TEST(ProcessTests, framing_errors_exit_without_resynchronization) {
for (const auto prefix :
{"Content-Length: invalid\r\n\r\n", "Other: 2\r\n\r\n", "Content-Length: 16777217\r\n\r\n",
"Content-Length: 1\r\nContent-Length: 1\r\n\r\n",
"Content-Length: 1\r\nContent-Type: application/json; charset=utf-16\r\n\r\n"}) {
const auto output = run(std::string{prefix} + lifecycle());
Void::Testing::expect_eq(output.status.code, 1);
Void::Testing::expect(output.out.empty());
Void::Testing::expect(!output.err.empty());
}
for (const auto prefix : {"Content-Length: 2\r\n", "Content-Length: 2\r\n\r\n{"}) {
const auto output = run(prefix);
Void::Testing::expect_eq(output.status.code, 1);
Void::Testing::expect(output.out.empty());
Void::Testing::expect(!output.err.empty());
}
}
VOID_TEST(ProcessTests, premature_exit_and_unexpected_eof) {
for (const auto input : {std::string{}, test::frame(test::exit),
test::frame(test::initialize_request) + test::frame(test::exit),
test::frame(test::initialize_request) + test::frame(test::shutdown)}) {
const auto output = run(input);
Void::Testing::expect_eq(output.status.code, 1);
// Every byte of stdout must still be framed JSON.
static_cast<void>(test::responses(output.out));
}
}
VOID_TEST(ProcessTests, lifecycle_error_precedence_and_string_ids) {
const auto output = run(test::frame(test::shutdown) + test::frame(test::initialize_request) +
test::frame(test::initialize_request) +
test::frame(R"({"jsonrpc":"2.0","id":"\"\u0416","method":"unknown"})") +
test::frame(test::shutdown) +
test::frame(R"({"jsonrpc":"2.0","id":9,"method":"unknown"})") +
test::frame(test::shutdown) + test::frame(test::exit));
Void::Testing::expect_eq(output.status.code, 0);
const auto replies = test::responses(output.out);
Void::Testing::require_eq(replies.size(), std::size_t{7});
Void::Testing::expect_eq(test::error_code(replies[0]), std::int64_t{-32002});
Void::Testing::expect_eq(test::error_code(replies[2]), std::int64_t{-32600});
Void::Testing::expect_eq(test::error_code(replies[3]), std::int64_t{-32601});
Void::Testing::expect_eq(std::get<std::string>(test::field(replies[3], "id").value),
std::string{"\"\xD0\x96"});
Void::Testing::expect_eq(test::error_code(replies[5]), std::int64_t{-32600});
Void::Testing::expect_eq(test::error_code(replies[6]), std::int64_t{-32600});
}