259 lines
12 KiB
C++
259 lines
12 KiB
C++
#include "TestSupport.hpp"
|
|
|
|
#include <Void/System/Process.hpp>
|
|
|
|
#include <cerrno>
|
|
#include <chrono>
|
|
#include <poll.h>
|
|
#include <stdexcept>
|
|
#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 = VLS_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);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
Void::Testing::Test 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::Testing::Test 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");
|
|
Void::Testing::expect(
|
|
std::get<vls::Json::Object>(test::field(result, "capabilities").value).empty());
|
|
Void::Testing::expect(
|
|
std::holds_alternative<std::nullptr_t>(test::field(replies[1], "result").value));
|
|
}
|
|
}
|
|
|
|
Void::Testing::Test 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::Testing::Test 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::Testing::Test 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::Testing::Test 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::Testing::Test 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::Testing::Test 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::Testing::Test 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});
|
|
}
|