#include #include #include #include #include #include #include #include #include namespace { using namespace Void::Compression; std::vector from_hex(std::string_view hex) { const auto digit = [](char c) -> int { if (c >= '0' && c <= '9') { return c - '0'; } if (c >= 'a' && c <= 'f') { return c - 'a' + 10; } if (c >= 'A' && c <= 'F') { return c - 'A' + 10; } return 0; }; std::vector out; out.reserve(hex.size() / 2); for (std::size_t i = 0; i + 1 < hex.size(); i += 2) { out.push_back(static_cast((digit(hex[i]) << 4) | digit(hex[i + 1]))); } return out; } Void::Result::Result inflate_all(bool gzip, std::span input, std::size_t chunk = 0, GzipLimits limits = {}) { auto inflater = gzip ? Inflater::create_gzip(limits) : Inflater::create_deflate(limits); if (!inflater) { return std::unexpected{inflater.error()}; } std::string output; const std::size_t step = chunk == 0 ? input.size() : chunk; for (std::size_t pos = 0; pos < input.size(); pos += step) { const std::size_t count = step == 0 ? 0 : std::min(step, input.size() - pos); if (auto fed = inflater->feed(input.subspan(pos, count), output); !fed) { return std::unexpected{fed.error()}; } } if (auto done = inflater->finish(output); !done) { return std::unexpected{done.error()}; } return output; } std::string binary_pattern() { std::string out; for (int repeat = 0; repeat < 4; ++repeat) { for (int value = 0; value < 256; ++value) { out.push_back(static_cast(value)); } } return out; } std::string text_pattern() { std::string out; for (int i = 0; i < 20; ++i) { out += "one two three four five six seven eight nine ten "; } return out; } constexpr std::string_view kHelloGz = "1f8b0800000000000003cb48cdc9c9070086a6103605000000"; constexpr std::string_view kHelloRaw = "cb48cdc9c90700"; constexpr std::string_view kEmptyGz = "1f8b080000000000000303000000000000000000"; constexpr std::string_view kEmptyRaw = "0300"; constexpr std::string_view kTextGz = "1f8b0800000000000003edcbcb0d00200804d156b6b955b94082f8295fedc203c7" "49e69912b10cd19c44b1e12832892e1b9d930a4a6d019537deb4040912fc0c0e88" "ddb3a9d4030000"; constexpr std::string_view kBinaryGz = "1f8b08000000000000036360646266616563e7e0e4e2e6e1e5e31710141216111513" "97909492969195935750545256515553d7d0d4d2d6d1d5d33730343236313533b7b0" "b4b2b6b1b5b37770747276717573f7f0f4f2f6f1f5f30f080c0a0e090d0b8f888c8a" "8e898d8b4f484c4a4e494d4bcfc8cccacec9cdcb2f282c2a2e292d2bafa8acaaaea9" "adab6f686c6a6e696d6befe8eceaeee9edeb9f3071d2e42953a74d9f3173d6ec3973" "e7cd5fb070d1e2254b972d5fb172d5ea356bd7addfb071d3e62d5bb76ddfb173d7ee" "3d7bf7ed3f70f0d0e123478f1d3f71f2d4e93367cf9dbf70f1d2e52b57af5dbf71f3" "d6ed3b77efdd7ff0f0d1e3274f9f3d7ff1f2d5eb376fdfbdfff0f1d3e72f5fbf7dff" "f1f3d7ef3f7ffffd6718f5ffa8ff47b0ff01264c0bb700040000"; } // namespace Void::Testing::Test gzip_inflates_rfc1952_vectors() { struct Vector { std::string_view gz; std::string expected; }; const Vector vectors[] = { {kHelloGz, "hello"}, {kEmptyGz, ""}, {kTextGz, text_pattern()}, {kBinaryGz, binary_pattern()}, }; for (const auto& vector : vectors) { const auto bytes = from_hex(vector.gz); auto inflated = inflate_all(true, bytes); Void::Testing::require(inflated.has_value()); Void::Testing::expect_eq(*inflated, vector.expected); } } Void::Testing::Test deflate_inflates_rfc1951_vectors() { const auto hello = from_hex(kHelloRaw); auto inflated = inflate_all(false, hello); Void::Testing::require(inflated.has_value()); Void::Testing::expect_eq(*inflated, std::string{"hello"}); const auto empty = from_hex(kEmptyRaw); auto empty_out = inflate_all(false, empty); Void::Testing::require(empty_out.has_value()); Void::Testing::expect(empty_out->empty()); } Void::Testing::Test gzip_inflates_across_chunk_boundaries() { const auto bytes = from_hex(kTextGz); for (const std::size_t chunk : {1U, 2U, 3U, 7U, 64U}) { auto inflated = inflate_all(true, bytes, chunk); Void::Testing::require(inflated.has_value()); Void::Testing::expect_eq(*inflated, text_pattern()); } } Void::Testing::Test gzip_rejects_malformed_and_truncated_input() { const auto good = from_hex(kHelloGz); // Truncated: the trailer is missing. { const std::span truncated{good.data(), good.size() - 4}; Void::Testing::expect(!inflate_all(true, truncated).has_value()); } // Corrupt CRC in the trailer. { auto corrupt = good; corrupt[corrupt.size() - 5] ^= std::byte{0xff}; Void::Testing::expect(!inflate_all(true, corrupt).has_value()); } // Bad magic. { auto bad = good; bad[0] = std::byte{0x00}; Void::Testing::expect(!inflate_all(true, bad).has_value()); } // Garbage. { const auto garbage = from_hex("deadbeefdeadbeef"); Void::Testing::expect(!inflate_all(true, garbage).has_value()); } // Trailing data after a complete member. { auto trailing = good; trailing.push_back(std::byte{0x00}); Void::Testing::expect(!inflate_all(true, trailing).has_value()); } } Void::Testing::Test gzip_encoder_rejects_reuse_after_finish() { auto deflater = Deflater::create_gzip(); Void::Testing::require(deflater.has_value()); std::string out; Void::Testing::require(deflater->finish(out).has_value()); Void::Testing::expect(!deflater->finish(out).has_value()); Void::Testing::expect(!deflater->feed({}, out).has_value()); } Void::Testing::Test gzip_stored_encoder_round_trips() { for (const std::size_t size : {0U, 1U, 1000U, 65535U, 65536U, 200000U}) { std::string data(size, '\0'); for (std::size_t i = 0; i < size; ++i) { data[i] = static_cast((i * 31 + 7) & 0xFF); } for (const bool gzip : {true, false}) { auto deflater = gzip ? Deflater::create_gzip() : Deflater::create_deflate(); Void::Testing::require(deflater.has_value()); std::string encoded; for (std::size_t pos = 0; pos < data.size(); pos += 1000) { const std::size_t count = std::min(1000, data.size() - pos); const auto chunk = std::span{ reinterpret_cast(data.data()) + pos, count}; Void::Testing::require( deflater->feed(chunk, encoded).has_value()); } Void::Testing::require(deflater->finish(encoded).has_value()); const auto bytes = std::span{ reinterpret_cast(encoded.data()), encoded.size()}; auto inflated = inflate_all(gzip, bytes, 777); Void::Testing::require(inflated.has_value()); Void::Testing::expect_eq(*inflated, data); } } } Void::Testing::Test gzip_enforces_the_output_limit() { const auto bytes = from_hex(kHelloGz); auto limited = inflate_all(true, bytes, 0, GzipLimits{.max_output_bytes = 4}); Void::Testing::expect(!limited.has_value()); }