Debian Codegraph @

unittest/gunit/binlogevents/transaction_payload_codec-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 <gtest/gtest.h>
26 #include <vector>
27 #include "libbinlogevents/include/binary_log.h"
28 #include "libbinlogevents/include/codecs/binary.h"
29
30 namespace binary_log {
31 namespace codecs {
32 namespace unittests {
33
34 class TransactionPayloadCodecTest : public ::testing::Test {
35 protected:
36 std::vector<std::size_t> m_payloads;
37 TransactionPayloadCodecTest() = default;
38
39 void SetUp() override { m_payloads.push_back(128); }
40
41 void TearDown() override { m_payloads.clear(); }
42
43 void run_codec_idempotency_test(binary_log::codecs::Codec &codec) {
44 for (auto payload_size : m_payloads) {
45 unsigned char *payload = new unsigned char[payload_size];
46
47 memset(payload, 'a', payload_size);
48 auto ctype = binary_log::transaction::compression::type::ZSTD;
49 TransactionPayloadCodecTest::codec_idempotency_test(
50 codec, payload, payload_size, ctype, payload_size * 2);
51
52 delete[] payload;
53 }
54 }
55
56 static void codec_idempotency_test(
57 binary_log::codecs::Codec &codec, unsigned char *payload,
58 size_t payload_size,
59 binary_log::transaction::compression::type compression_type,
60 size_t uncompressed_size) {
61 const Format_description_event fde(BINLOG_VERSION, "8.0.17");
62
63 // encoded buffer
64 unsigned char *enc_buffer = new unsigned char[payload_size * 3];
65 memset(enc_buffer, 0, payload_size * 3);
66
67 // decoded buffer
68 Transaction_payload_event original((const char *)payload, payload_size,
69 compression_type, uncompressed_size);
70
71 // -------------------------------------
72 // encoding
73 // -------------------------------------
74 auto enc_result = codec.encode(original, enc_buffer, payload_size * 3);
75
76 // encoded internal header without errors
77 ASSERT_FALSE(enc_result.second);
78
79 // copy the payload
80 memcpy(enc_buffer + enc_result.first, payload, payload_size);
81
82 // -------------------------------------
83 // decoding by hand
84 // -------------------------------------
85
86 Transaction_payload_event decoded(
87 static_cast<const char *>(nullptr), static_cast<uint64_t>(0),
88 transaction::compression::type::NONE, payload_size);
89
90 // decode the post LOG_EVENT header
91 auto buffer = enc_buffer;
92 size_t buffer_size = payload_size * 3;
93
94 auto dec_result = codec.decode(buffer, buffer_size, decoded);
95 decoded.set_payload(reinterpret_cast<const char *>(buffer) +
96 dec_result.first);
97
98 int payload_diff = memcmp(decoded.get_payload(), original.get_payload(),
99 decoded.get_payload_size());
100
101 ASSERT_FALSE(dec_result.second);
102 ASSERT_TRUE(decoded.get_payload_size() == original.get_payload_size());
103 ASSERT_TRUE(decoded.get_compression_type() ==
104 original.get_compression_type());
105 ASSERT_TRUE(decoded.get_uncompressed_size() ==
106 original.get_uncompressed_size());
107
108 ASSERT_TRUE(payload_diff == 0);
109
110 delete[] enc_buffer;
111 }
112 };
113
114 TEST_F(TransactionPayloadCodecTest, EncodeDecodeIdempotencyBinaryTest) {
115 binary_log::codecs::binary::Transaction_payload codec;
116 TransactionPayloadCodecTest::run_codec_idempotency_test(codec);
117 }
118
119 } // namespace unittests
120 } // namespace codecs
121 } // namespace binary_log