#include #include #include #include #include #include #include #include #include #include #include #include #include namespace Protocol = Void::LanguageServer::Protocol; namespace Testing = Void::Testing; using Protocol::Json; namespace { void expect_encoding_failure(const Json &value, std::string_view message, std::size_t maximum_depth = Protocol::kDefaultMaxJsonDepth) { const auto encoded = Protocol::serialize_json(value, maximum_depth); Testing::require(!encoded.has_value()); Testing::expect(encoded.error().contains(message)); std::ostringstream output; const auto written = Protocol::write_json_frame(output, value, maximum_depth); Testing::require(!written.has_value()); Testing::expect_eq(written.error(), encoded.error()); Testing::expect(output.str().empty()); } Json nest_in_arrays(Json leaf, std::size_t count) { for (std::size_t i = 0; i < count; ++i) { Json::Array parent; parent.push_back(std::move(leaf)); leaf = Json{std::move(parent)}; } return leaf; } } // namespace VOID_TEST(ProtocolEncodingTests, typed_response_round_trips_through_frame) { const Protocol::RequestId id{std::string{"id\"\\\r\xF0\x9F\x98\x83"}}; const std::string key{"key\"\n\\\xD0\x96"}; std::string text{"quote\" slash\\\b\f\n\r\t"}; text.push_back('\0'); text += "\xD0\x96\xF0\x9F\x98\x83"; Json::Array values{Json{1.25}, Json{nullptr}, Json{false}, Json{Json::Object{}}}; Json::Object result{{key, Json{text}}, {"values", Json{std::move(values)}}}; const Json response{Json::Object{{"jsonrpc", Json{std::string{"2.0"}}}, {"id", Protocol::request_id_json(id)}, {"result", Json{std::move(result)}}}}; std::ostringstream output; const auto written = Protocol::write_json_frame(output, response); Testing::require(written.has_value()); const auto wire = output.str(); std::istringstream input{wire}; const auto frame = Protocol::read_frame(input); Testing::require(frame.has_value() && frame->has_value()); const auto &body = **frame; Testing::expect_eq(wire, "Content-Length: " + std::to_string(body.size()) + "\r\n\r\n" + body); const auto eof = Protocol::read_frame(input); Testing::expect(eof.has_value() && !eof->has_value()); const auto decoded = Protocol::parse_json(body); Testing::require(decoded.has_value()); const auto *decoded_response = Protocol::as_object(&*decoded); Testing::require(decoded_response != nullptr); const auto *decoded_id = Protocol::find_member(*decoded_response, "id"); Testing::require(decoded_id != nullptr); const auto checked_id = Protocol::request_id(*decoded_id); Testing::require(checked_id.has_value()); Testing::expect_eq(*checked_id, id); const auto *decoded_result = Protocol::as_object(Protocol::find_member(*decoded_response, "result")); Testing::require(decoded_result != nullptr); const auto *decoded_text = Protocol::as_string(Protocol::find_member(*decoded_result, key)); Testing::require(decoded_text != nullptr); Testing::expect_eq(*decoded_text, text); const auto *decoded_values = Protocol::as_array(Protocol::find_member(*decoded_result, "values")); Testing::require(decoded_values && decoded_values->size() == 4); const auto *decimal = std::get_if(&(*decoded_values)[0].value); Testing::require(decimal != nullptr); Testing::expect_eq(*decimal, 1.25); Testing::expect(std::holds_alternative((*decoded_values)[1].value)); Testing::expect_eq(Protocol::as_bool(&(*decoded_values)[2]), std::optional{false}); const auto *empty = Protocol::as_object(&(*decoded_values)[3]); Testing::expect(empty && empty->empty()); } VOID_TEST(ProtocolEncodingTests, invalid_utf8_in_key_or_value_writes_no_frame) { const std::string invalid{"\xED\xA0\x80"}; expect_encoding_failure(Json{Json::Array{Json{true}, Json{invalid}}}, "UTF-8"); expect_encoding_failure(Json{Json::Object{{"ok", Json{true}}, {invalid, Json{nullptr}}}}, "UTF-8"); Testing::expect(!Protocol::parse_json("{\"" + invalid + "\":null}").has_value()); Testing::expect(!Protocol::parse_json("[\"" + invalid + "\"]").has_value()); } VOID_TEST(ProtocolEncodingTests, nonfinite_numbers_write_no_frame) { for (const double number : {std::numeric_limits::quiet_NaN(), std::numeric_limits::infinity(), -std::numeric_limits::infinity()}) { expect_encoding_failure(Json{Json::Array{Json{true}, Json{number}}}, "non-finite"); } } VOID_TEST(ProtocolEncodingTests, default_depth_counts_empty_containers) { const auto encoded = Protocol::serialize_json(nest_in_arrays(Json{Json::Object{}}, 63)); Testing::require(encoded.has_value()); Testing::expect(Protocol::parse_json(*encoded).has_value()); const auto encoded_scalar = Protocol::serialize_json(nest_in_arrays(Json{nullptr}, 64)); Testing::require(encoded_scalar.has_value()); Testing::expect(Protocol::parse_json(*encoded_scalar).has_value()); expect_encoding_failure(nest_in_arrays(Json{Json::Object{}}, 64), "depth limit"); Testing::expect( !Protocol::parse_json(std::string(64, '[') + "{}" + std::string(64, ']')).has_value()); } VOID_TEST(ProtocolEncodingTests, custom_depth_counts_containers_and_accepts_root_scalars) { for (const auto text : {"null", "false", "1.25", R"("[{}]")"}) { const auto decoded = Protocol::parse_json(text, 0); Testing::require(decoded.has_value()); const auto encoded = Protocol::serialize_json(*decoded, 0); Testing::require(encoded.has_value()); Testing::expect_eq(*encoded, std::string{text}); } expect_encoding_failure(Json{Json::Array{}}, "depth limit", 0); expect_encoding_failure(Json{Json::Object{}}, "depth limit", 0); Testing::expect(!Protocol::parse_json("[]", 0).has_value()); Testing::expect(!Protocol::parse_json("{}", 0).has_value()); Testing::expect(Protocol::parse_json("[0]", 1).has_value()); Testing::expect( Protocol::serialize_json(nest_in_arrays(Json{std::int64_t{0}}, 1), 1).has_value()); expect_encoding_failure(nest_in_arrays(Json{Json::Object{}}, 1), "depth limit", 1); Testing::expect(!Protocol::parse_json("[{}]", 1).has_value()); } VOID_TEST(ProtocolEncodingTests, oversized_depth_setting_stays_bounded) { constexpr auto maximum_depth = std::numeric_limits::max(); expect_encoding_failure(nest_in_arrays(Json{nullptr}, 512), "depth limit", maximum_depth); Testing::expect( !Protocol::parse_json(std::string(512, '[') + "null" + std::string(512, ']'), maximum_depth) .has_value()); } VOID_TEST(ProtocolEncodingTests, numeric_accessors_and_typed_ids_check_ranges) { constexpr auto min_int64 = std::numeric_limits::min(); constexpr auto max_int64 = std::numeric_limits::max(); const Json lower_double{-0x1p63}; const Json upper_double{0x1p63}; const Json upper_integer{max_int64}; Testing::expect_eq(Protocol::as_int64(&lower_double), std::optional{min_int64}); Testing::expect(!Protocol::as_int64(&upper_double).has_value()); Testing::expect_eq(Protocol::as_int64(&upper_integer), std::optional{max_int64}); constexpr auto max_uint32 = std::numeric_limits::max(); const Json max_unsigned{static_cast(max_uint32)}; const Json out_of_unsigned_range{static_cast(max_uint32) + 1}; const Json negative{std::int64_t{-1}}; Testing::expect_eq(Protocol::as_uint32(&max_unsigned), std::optional{max_uint32}); Testing::expect(!Protocol::as_uint32(&out_of_unsigned_range).has_value()); Testing::expect(!Protocol::as_uint32(&negative).has_value()); for (const auto number : {std::numeric_limits::min(), std::numeric_limits::max()}) { const auto id = Protocol::request_id(Json{static_cast(number)}); Testing::require(id.has_value()); Testing::expect_eq(*id, Protocol::RequestId{number}); const auto encoded = Protocol::request_id_json(*id); Testing::expect(std::holds_alternative(encoded.value)); Testing::expect_eq(Protocol::as_int64(&encoded), std::optional{number}); } for (const Json &invalid : {Json{false}, Json{nullptr}, Json{1.25}, Json{2147483648.0}}) { Testing::expect(!Protocol::request_id(invalid).has_value()); } }