#include #include #include #include Void::Testing::Test binary_reader_checks_bounds_and_endian() { const std::array bytes{std::byte{0x12}, std::byte{0x34}, std::byte{0x56}}; Void::Binary::Reader reader{bytes}; auto value = reader.read(std::endian::big); Void::Testing::require(value.has_value()); Void::Testing::expect_eq(*value, static_cast(0x1234)); Void::Testing::expect_eq(reader.position(), static_cast(2)); auto overflow = reader.read(std::endian::big); Void::Testing::expect(!overflow.has_value()); Void::Testing::expect_eq(reader.position(), static_cast(2)); } Void::Testing::Test binary_reader_subreader_is_bounded() { const std::array bytes{std::byte{'a'}, std::byte{'b'}, std::byte{'c'}}; Void::Binary::Reader reader{bytes}; auto child = reader.subreader(2); Void::Testing::require(child.has_value()); auto value = child->readString(2); Void::Testing::require(value.has_value()); Void::Testing::expect_eq(*value, std::string{"ab"}); Void::Testing::expect(!child->readBytes(1).has_value()); Void::Testing::expect_eq(reader.remaining(), static_cast(1)); } Void::Testing::Test binary_reader_explicit_endian_bool_floats_and_transactions() { const std::array raw{ std::byte{0x12}, std::byte{0x34}, std::byte{0x56}, std::byte{0x78}, std::byte{0x78}, std::byte{0x56}, std::byte{0x34}, std::byte{0x12}, std::byte{0x01}, std::byte{0x02}, std::byte{0x3F}, std::byte{0x80}, std::byte{0x00}, std::byte{0x00}, std::byte{0x00}, std::byte{0x00}, std::byte{0x00}, std::byte{0x00}, std::byte{0x00}, std::byte{0x00}, std::byte{0x08}, std::byte{0x40}}; Void::Binary::Reader reader{raw}; const auto big32 = reader.readBig(); const auto lit32 = reader.readLittle(); Void::Testing::require(big32.has_value() && lit32.has_value()); Void::Testing::expect_eq(*big32, std::uint32_t{0x12345678u}); Void::Testing::expect_eq(*lit32, std::uint32_t{0x12345678u}); // Unsupported endian fails without moving cursor. const auto saved_pos = reader.checkpoint(); const auto invalid_endian = static_cast(99); const auto bad_end = reader.read(invalid_endian); Void::Testing::expect(!bad_end.has_value()); Void::Testing::expect(bad_end.error().code() == Void::Binary::to_error_code(Void::Binary::BinaryErrorCode::unsupported_endian)); Void::Testing::expect_eq(reader.position(), saved_pos); // Valid bool advances, invalid bool (0x02) leaves cursor untouched. const auto b_true = reader.readBool(); Void::Testing::require(b_true.has_value()); Void::Testing::expect(*b_true); const std::size_t before_bad_bool = reader.position(); const auto b_bad = reader.readBool(); Void::Testing::expect(!b_bad.has_value()); Void::Testing::expect(b_bad.error().code() == Void::Binary::to_error_code(Void::Binary::BinaryErrorCode::invalid_bool)); Void::Testing::expect_eq(reader.position(), before_bad_bool); // Skip the invalid bool byte and verify transaction rollback/commit & floats. Void::Testing::require(reader.skip(1).has_value()); { Void::Binary::Reader::Transaction tx{reader}; const auto f1 = reader.readF32(std::endian::big); Void::Testing::require(f1.has_value()); Void::Testing::expect_eq(*f1, 1.0F); } // auto-rollback Void::Testing::expect_eq(reader.position(), before_bad_bool + 1); { Void::Binary::Reader::Transaction tx{reader}; const auto f1 = reader.readF32(std::endian::big); const auto d1 = reader.readF64(std::endian::little); Void::Testing::require(f1.has_value() && d1.has_value()); Void::Testing::expect_eq(*f1, 1.0F); Void::Testing::expect_eq(*d1, 3.0); tx.commit(); } Void::Testing::expect(reader.empty()); } Void::Testing::Test binary_reader_cumulative_decode_budget_across_children() { using Void::Binary::BinaryErrorCode; const std::array bytes{ std::byte{1}, std::byte{2}, std::byte{3}, std::byte{4}, std::byte{5}, std::byte{6}, std::byte{7}, std::byte{8}}; Void::Binary::Reader root{bytes}; Void::Binary::DecodeBudget budget{ Void::Binary::DecodeLimits{.max_field_bytes = 5, .max_total_bytes = 6, .max_depth = 2}}; auto g1 = budget.enter_scope(); auto g2 = budget.enter_scope(); auto g3 = budget.enter_scope(); Void::Testing::require(g1.has_value() && g2.has_value()); Void::Testing::expect(!g3.has_value()); Void::Testing::expect(g3.error().code() == Void::Binary::to_error_code(BinaryErrorCode::depth_exceeded)); auto child = root.subreaderChecked(4, budget); Void::Testing::require(child.has_value()); Void::Testing::expect_eq(budget.consumed_bytes(), std::size_t{4}); Void::Testing::expect_eq(budget.remaining_bytes(), std::size_t{2}); // Shared cumulative limit rejects child consuming past remaining total bytes. const auto over_total = child->readBytesChecked(3, budget); Void::Testing::expect(!over_total.has_value()); Void::Testing::expect(over_total.error().code() == Void::Binary::to_error_code(BinaryErrorCode::budget_exceeded)); Void::Testing::expect_eq(budget.consumed_bytes(), std::size_t{4}); // Truncated read in child fails with truncated BEFORE charging budget. auto ok_slice = child->readBytesChecked(2, budget); Void::Testing::require(ok_slice.has_value()); Void::Testing::expect_eq(budget.consumed_bytes(), std::size_t{6}); Void::Testing::expect(!root.subreaderChecked(3, budget).has_value()); }