Debian Codegraph @

unittest/gunit/binlogevents/transaction_compression-t.cc

raw symbols SCIP
1 /* Copyright (c) 2019, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #include <array>
26 #include <string>
27
28 #include <gtest/gtest.h>
29 #include "libbinlogevents/include/binary_log.h"
30 #include "libbinlogevents/include/compression/none_comp.h"
31 #include "libbinlogevents/include/compression/none_dec.h"
32 #include "libbinlogevents/include/compression/zstd_comp.h"
33 #include "libbinlogevents/include/compression/zstd_dec.h"
34 #include "libbinlogevents/include/string/concat.h"
35
36 using mysqlns::string::concat;
37
38 namespace binary_log {
39 namespace transaction {
40 namespace unittests {
41
42 static std::size_t MAX_BUFFER_SIZE = 1024 * 1024 * 512;
43
44 static std::array<std::size_t, 10> buffer_sizes{
45 0, 1, 2, 3, 4, 5, 128, 256, 512, MAX_BUFFER_SIZE / 2};
46
47 class TransactionPayloadCompressionTest : public ::testing::Test {
48 public:
49 using Compressor_t = binary_log::transaction::compression::Compressor;
50 using Managed_buffer_sequence_t = Compressor_t::Managed_buffer_sequence_t;
51 using Decompressor_t = binary_log::transaction::compression::Decompressor;
52 using Managed_buffer_t = Decompressor_t::Managed_buffer_t;
53 using Size_t = Decompressor_t::Size_t;
54 using Char_t = Decompressor_t::Char_t;
55 using Decompress_status_t =
56 binary_log::transaction::compression::Decompress_status;
57 using Compress_status_t =
58 binary_log::transaction::compression::Compress_status;
59
60 static std::string constant_data(Size_t size) {
61 return std::string(size, (Char_t)'a');
62 }
63
64 protected:
65 TransactionPayloadCompressionTest() = default;
66
67 void SetUp() override {}
68
69 void TearDown() override {}
70
71 static void compression_idempotency_test(Compressor_t &c, Decompressor_t &d,
72 const std::string &data) {
73 auto debug_string = concat(
74 binary_log::transaction::compression::type_to_string(c.get_type_code()),
75 " ", data.size());
76 Managed_buffer_sequence_t managed_buffer_sequence;
77 c.feed(data.data(), data.size());
78 ASSERT_EQ(Compress_status_t::success, c.finish(managed_buffer_sequence))
79 << debug_string;
80
81 // assert that it is smaller than the uncompressed size
82 auto compressed_size = managed_buffer_sequence.read_part().size();
83 if (c.get_type_code() != binary_log::transaction::compression::NONE) {
84 // For very small data the constant overhead results in size inflation
85 if (data.size() > 100) {
86 ASSERT_LT(compressed_size, data.size()) << debug_string;
87 }
88 } else {
89 ASSERT_EQ(compressed_size, data.size()) << debug_string;
90 }
91
92 // Get contiguous buffer.
93 const auto compressed_data = managed_buffer_sequence.read_part().str();
94
95 // Decompress
96 d.feed(compressed_data.data(), compressed_data.size());
97 Managed_buffer_t managed_buffer;
98 auto ret = d.decompress(managed_buffer, data.size());
99 auto expected_status = Decompress_status_t::success;
100 if (data.empty() &&
101 c.get_type_code() == binary_log::transaction::compression::NONE)
102 expected_status = Decompress_status_t::end;
103 ASSERT_EQ(ret, expected_status) << debug_string;
104
105 // Check decompressed data
106 ASSERT_EQ(managed_buffer.read_part().size(), data.size()) << debug_string;
107 ASSERT_EQ(data, std::string(managed_buffer.read_part().begin(),
108 managed_buffer.read_part().end()))
109 << debug_string;
110
111 // Check that we reached EOF
112 ASSERT_EQ(d.decompress(managed_buffer, 1), Decompress_status_t::end)
113 << debug_string;
114 }
115 };
116
117 TEST_F(TransactionPayloadCompressionTest, CompressDecompressZstdTest) {
118 for (auto size : buffer_sizes) {
119 binary_log::transaction::compression::Zstd_dec d;
120 binary_log::transaction::compression::Zstd_comp c;
121 std::string data{TransactionPayloadCompressionTest::constant_data(size)};
122 TransactionPayloadCompressionTest::compression_idempotency_test(c, d, data);
123 c.set_compression_level(22);
124 TransactionPayloadCompressionTest::compression_idempotency_test(c, d, data);
125 }
126 }
127
128 TEST_F(TransactionPayloadCompressionTest, CompressDecompressNoneTest) {
129 for (auto size : buffer_sizes) {
130 binary_log::transaction::compression::None_dec d;
131 binary_log::transaction::compression::None_comp c;
132 std::string data{TransactionPayloadCompressionTest::constant_data(size)};
133 TransactionPayloadCompressionTest::compression_idempotency_test(c, d, data);
134 }
135 }
136
137 } // namespace unittests
138 } // namespace transaction
139 } // namespace binary_log