|
| 1 | +// Copyright 2024 Redpanda Data, Inc. |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the file licenses/BSL.md |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with |
| 7 | +// the Business Source License, use of this software will be governed |
| 8 | +// by the Apache License, Version 2.0 |
| 9 | + |
| 10 | +#include "bytes/iobuf.h" |
| 11 | +#include "bytes/iostream.h" |
| 12 | +#include "compression/internal/snappy_java_compressor.h" |
| 13 | +#include "compression/snappy_standard_compressor.h" |
| 14 | +#include "random/generators.h" |
| 15 | +#include "utils/file_io.h" |
| 16 | + |
| 17 | +#include <seastar/core/byteorder.hh> |
| 18 | +#include <seastar/core/seastar.hh> |
| 19 | +#include <seastar/core/temporary_buffer.hh> |
| 20 | +#include <seastar/util/short_streams.hh> |
| 21 | + |
| 22 | +#include <gtest/gtest.h> |
| 23 | + |
| 24 | +#include <cstdlib> |
| 25 | +#include <snappy-sinksource.h> |
| 26 | +#include <snappy.h> |
| 27 | + |
| 28 | +TEST(SnappyTest, CompressAndDecompressSnappyStandardTest) { |
| 29 | + const auto data = random_generators::gen_alphanum_string(512); |
| 30 | + |
| 31 | + iobuf buf; |
| 32 | + buf.append(data.data(), data.size()); |
| 33 | + auto compressed_buf = compression::snappy_standard_compressor::compress( |
| 34 | + buf); |
| 35 | + auto decompressed_buf = compression::snappy_standard_compressor::uncompress( |
| 36 | + compressed_buf); |
| 37 | + EXPECT_EQ(buf, decompressed_buf); |
| 38 | +} |
| 39 | + |
| 40 | +TEST(SnappyTest, CompressAndDecompressSnappyJavaTest) { |
| 41 | + const auto data = random_generators::gen_alphanum_string(512); |
| 42 | + |
| 43 | + iobuf buf; |
| 44 | + buf.append(data.data(), data.size()); |
| 45 | + auto compressed_buf |
| 46 | + = compression::internal::snappy_java_compressor::compress(buf); |
| 47 | + auto decompressed_buf |
| 48 | + = compression::internal::snappy_java_compressor::uncompress( |
| 49 | + compressed_buf); |
| 50 | + EXPECT_EQ(buf, decompressed_buf); |
| 51 | +} |
| 52 | + |
| 53 | +TEST(SnappyTest, SnappyStandardIsValidCompressedTest) { |
| 54 | + const auto data = random_generators::gen_alphanum_string(512); |
| 55 | + |
| 56 | + iobuf buf; |
| 57 | + buf.append(data.data(), data.size()); |
| 58 | + auto compressed_buf = compression::snappy_standard_compressor::compress( |
| 59 | + buf); |
| 60 | + |
| 61 | + EXPECT_EQ( |
| 62 | + snappy::IsValidCompressedBuffer( |
| 63 | + compressed_buf.begin()->get(), compressed_buf.size_bytes()), |
| 64 | + true); |
| 65 | +} |
| 66 | + |
| 67 | +TEST(SnappyTest, CompressedVersionHeadersSnappyJavaTest) { |
| 68 | + // snappy-java uses big-endian format to encode headers. See: |
| 69 | + // https://github.com/xerial/snappy-java/blob/65e1ec3de1a0d447b137c6dd6393629aa3d75b8b/src/main/java/org/xerial/snappy/SnappyOutputStream.java#L343-L349 |
| 70 | + // https://github.com/xerial/snappy-java/blob/65e1ec3de1a0d447b137c6dd6393629aa3d75b8b/src/main/java/org/xerial/snappy/SnappyCodec.java#L78-L81 |
| 71 | + const auto data = random_generators::gen_alphanum_string(512); |
| 72 | + |
| 73 | + iobuf buf; |
| 74 | + buf.append(data.data(), data.size()); |
| 75 | + auto compressed_buf |
| 76 | + = compression::internal::snappy_java_compressor::compress(buf); |
| 77 | + auto magic_buf = ss::temporary_buffer<char>( |
| 78 | + compression::internal::snappy_java_compressor::snappy_magic::java_magic |
| 79 | + .size()); |
| 80 | + auto compressed_frag = compressed_buf.begin(); |
| 81 | + |
| 82 | + // Check the magic header |
| 83 | + EXPECT_EQ( |
| 84 | + std::memcmp( |
| 85 | + compressed_frag->get(), |
| 86 | + compression::internal::snappy_java_compressor::snappy_magic::java_magic |
| 87 | + .begin(), |
| 88 | + compression::internal::snappy_java_compressor::snappy_magic::java_magic |
| 89 | + .size()), |
| 90 | + 0); |
| 91 | + compressed_frag->trim_front( |
| 92 | + compression::internal::snappy_java_compressor::snappy_magic::java_magic |
| 93 | + .size()); |
| 94 | + |
| 95 | + // Check the default version |
| 96 | + auto be_default_version = ss::cpu_to_be( |
| 97 | + compression::internal::snappy_java_compressor::snappy_magic:: |
| 98 | + default_version); |
| 99 | + EXPECT_EQ( |
| 100 | + std::memcmp( |
| 101 | + compressed_frag->get(), |
| 102 | + reinterpret_cast<const char*>(&be_default_version), |
| 103 | + sizeof(compression::internal::snappy_java_compressor::snappy_magic:: |
| 104 | + default_version)), |
| 105 | + 0); |
| 106 | + |
| 107 | + compressed_frag->trim_front( |
| 108 | + sizeof(compression::internal::snappy_java_compressor::snappy_magic:: |
| 109 | + default_version)); |
| 110 | + |
| 111 | + // Check the compat version |
| 112 | + auto be_compat_version = ss::cpu_to_be( |
| 113 | + compression::internal::snappy_java_compressor::snappy_magic:: |
| 114 | + min_compatible_version); |
| 115 | + EXPECT_EQ( |
| 116 | + std::memcmp( |
| 117 | + compressed_frag->get(), |
| 118 | + reinterpret_cast<const char*>(&be_compat_version), |
| 119 | + sizeof(compression::internal::snappy_java_compressor::snappy_magic:: |
| 120 | + min_compatible_version)), |
| 121 | + 0); |
| 122 | + |
| 123 | + compressed_frag->trim_front( |
| 124 | + sizeof(compression::internal::snappy_java_compressor::snappy_magic:: |
| 125 | + min_compatible_version)); |
| 126 | + |
| 127 | + // Check the size of the compressed payload |
| 128 | + int32_t be_compressed_size{}; |
| 129 | + std::memcpy( |
| 130 | + &be_compressed_size, compressed_frag->get(), sizeof(be_compressed_size)); |
| 131 | + int32_t compressed_size = ss::be_to_cpu(be_compressed_size); |
| 132 | + compressed_frag->trim_front(sizeof(compressed_size)); |
| 133 | + EXPECT_EQ(compressed_size, compressed_frag->size()); |
| 134 | + |
| 135 | + // Get the size of the decompressed payload |
| 136 | + snappy::ByteArraySource compressed_source( |
| 137 | + compressed_frag->get(), compressed_size); |
| 138 | + uint32_t decompressed_size; |
| 139 | + snappy::GetUncompressedLength(&compressed_source, &decompressed_size); |
| 140 | + EXPECT_EQ(decompressed_size, data.size()); |
| 141 | + compressed_frag->trim_front(sizeof(decompressed_size)); |
| 142 | +} |
| 143 | + |
| 144 | +TEST(SnappyTest, LittleEndianHeadersBackwardsCompatibilitySnappyJavaTest) { |
| 145 | + // Previously, version fields were erroneously written with |
| 146 | + // little-endian encoding. They are now corrected to be written and decoded |
| 147 | + // using big-endian, but we must retain backwards compatibility here with |
| 148 | + // the existing, improperly encoded batches (as version, min_version fields |
| 149 | + // with value 1 will decode to the value 16777216). |
| 150 | + // See: https://github.com/redpanda-data/redpanda/issues/25091 |
| 151 | + auto snappy_payload_path = std::getenv("SNAPPY_PAYLOAD_PATH"); |
| 152 | + vassert(snappy_payload_path, "expected value for payload path"); |
| 153 | + auto root = std::filesystem::path(snappy_payload_path); |
| 154 | + |
| 155 | + // The original, uncompressed data. |
| 156 | + auto expected_decompressed_file = root / "uncompressed_data"; |
| 157 | + EXPECT_TRUE(ss::file_exists(expected_decompressed_file.c_str()).get()); |
| 158 | + auto expected_decompressed_buffer |
| 159 | + = read_fully(expected_decompressed_file.native()).get(); |
| 160 | + |
| 161 | + // A payload that was previously compressed by redpanda, with version |
| 162 | + // headers in little-endian encoding. |
| 163 | + auto le_compressed_file = root / "little_endian_compressed_data.snappy"; |
| 164 | + EXPECT_TRUE(ss::file_exists(le_compressed_file.c_str()).get()); |
| 165 | + auto le_compressed_buffer = read_fully(le_compressed_file.native()).get(); |
| 166 | + |
| 167 | + auto decompressed_buffer |
| 168 | + = compression::internal::snappy_java_compressor::uncompress( |
| 169 | + le_compressed_buffer); |
| 170 | + EXPECT_EQ(decompressed_buffer, expected_decompressed_buffer); |
| 171 | +} |
0 commit comments