Files
Language-Server/tests/FramingTests.cpp
T
faumaray ca04975d54 bootstrap
Signed-off-by: faumaray <faumaray@gmail.com>
2026-11-25 02:11:19 +03:00

93 lines
4.2 KiB
C++

#include "TestSupport.hpp"
#include <limits>
Void::Testing::Test consecutive_frames_preserve_byte_lengths() {
const std::string body{"\"\xF0\x9F\x98\x83\""};
std::istringstream input{test::frame(body) + test::frame("{}")};
auto first = vls::read_frame(input);
Void::Testing::require(first.has_value() && first->has_value());
Void::Testing::expect_eq(**first, body);
auto second = vls::read_frame(input);
Void::Testing::require(second.has_value() && second->has_value());
Void::Testing::expect_eq(**second, std::string{"{}"});
auto eof = vls::read_frame(input);
Void::Testing::expect(eof.has_value() && !eof->has_value());
}
Void::Testing::Test exact_frame_limits() {
const auto wire = test::frame("1234");
std::istringstream exact{wire};
Void::Testing::expect(vls::read_frame(exact, {wire.size() - 4, 4}).has_value());
std::istringstream header_over{wire};
Void::Testing::expect(!vls::read_frame(header_over, {wire.size() - 5, 4}).has_value());
std::istringstream body_over{wire};
Void::Testing::expect(!vls::read_frame(body_over, {wire.size() - 4, 3}).has_value());
}
Void::Testing::Test length_validation() {
for (const std::string header :
{"Other: 1", "Content-Length: ", "Content-Length: -1", "Content-Length: +1",
"Content-Length: 1x", "Content-Length: 1 0", "Content-Length: 1.0",
"Content-Length: 999999999999999999999999999999999999", "Content-Length: 16777217",
"Content-Length: 1\r\ncontent-length: 1"}) {
std::istringstream input{header + "\r\n\r\nx"};
Void::Testing::expect(!vls::read_frame(input).has_value());
}
std::istringstream zero{"Content-Length: 0\r\n\r\n"};
auto result = vls::read_frame(zero);
Void::Testing::require(result.has_value() && result->has_value());
Void::Testing::expect((**result).empty());
}
Void::Testing::Test charset_and_header_validation() {
for (const std::string type :
{"application/vscode-jsonrpc; charset=utf-8", "application/vscode-jsonrpc; charset=UTF8",
"application/vscode-jsonrpc", "application/json; charset=\"utf-8\""}) {
std::istringstream input{"content-length:\t2 \r\nContent-Type: " + type + "\r\n\r\n{}"};
Void::Testing::expect(vls::read_frame(input).has_value());
}
for (const std::string wire :
{"Content-Length: 2\n\n{}", "Content-Length: 2\rx\r\n\r\n{}", "NoColon\r\n\r\n",
"Content-Length : 2\r\n\r\n{}",
"Content-Length: 2\r\nContent-Type: application/json; charset=latin1\r\n\r\n{}",
"Content-Length: 2\r\nContent-Type: application/json; charset=utf-8; "
"charset=utf8\r\n\r\n{}",
"Content-Length: 2\r\nContent-Type: application/json\r\nContent-Type: "
"application/json\r\n\r\n{}"}) {
std::istringstream input{wire};
Void::Testing::expect(!vls::read_frame(input).has_value());
}
std::istringstream non_ascii{"Content-Length: 0\r\nOther: \xFF\r\n\r\n"};
Void::Testing::expect(!vls::read_frame(non_ascii).has_value());
}
Void::Testing::Test truncated_header_and_body_are_fatal() {
for (const std::string wire : {"C", "Content-Length: 2\r\n", "Content-Length: 2\r\n\r\n{"}) {
std::istringstream input{wire};
Void::Testing::expect(!vls::read_frame(input).has_value());
}
}
Void::Testing::Test stream_failures_propagate() {
std::istringstream broken;
broken.setstate(std::ios::badbit);
Void::Testing::expect(!vls::read_frame(broken).has_value());
std::ostringstream output;
output.setstate(std::ios::badbit);
Void::Testing::expect(!vls::write_frame(output, "{}").has_value());
std::istringstream input{test::frame(test::initialize_request)};
std::ostringstream diagnostics;
Void::Testing::expect_eq(vls::run_stdio(input, output, diagnostics), 1);
Void::Testing::expect(!diagnostics.str().empty());
}
Void::Testing::Test no_resynchronization_after_framing_error() {
std::istringstream input{"Content-Length: nope\r\n\r\n" +
test::frame(test::initialize_request)};
std::ostringstream output;
std::ostringstream diagnostics;
Void::Testing::expect_eq(vls::run_stdio(input, output, diagnostics), 1);
Void::Testing::expect(output.str().empty());
}