| 1 | /* Copyright (c) 2021, 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/codecs/binary.h" |
| 28 | #include "libbinlogevents/include/codecs/factory.h" |
| 29 | |
| 30 | namespace binary_log { |
| 31 | namespace codecs { |
| 32 | namespace unittests { |
| 33 | |
| 34 | class HeartbeatCodecTest : public ::testing::Test { |
| 35 | protected: |
| 36 | HeartbeatCodecTest() {} |
| 37 | |
| 38 | void run_codec_idempotency_test( |
| 39 | std::unique_ptr<binary_log::codecs::Codec> &codec) { |
| 40 | std::vector<uint64_t> positions{UINT8_MAX, UINT16_MAX, UINT32_MAX, |
| 41 | UINT64_MAX}; |
| 42 | for (auto pos : positions) |
| 43 | HeartbeatCodecTest::codec_idempotency_test(codec, "binlog.1000000", pos); |
| 44 | } |
| 45 | |
| 46 | static void codec_idempotency_test( |
| 47 | std::unique_ptr<binary_log::codecs::Codec> &codec, |
| 48 | const std::string logname, uint64_t pos) { |
| 49 | const Format_description_event fde(BINLOG_VERSION, "8.0.22"); |
| 50 | |
| 51 | // encoded buffer |
| 52 | unsigned char enc_buffer[1024]; |
| 53 | memset(enc_buffer, 0, sizeof(enc_buffer)); |
| 54 | |
| 55 | // decoded buffer |
| 56 | Heartbeat_event_v2 original; |
| 57 | original.set_log_filename(logname); |
| 58 | original.set_log_position(pos); |
| 59 | |
| 60 | // ------------------------------------- |
| 61 | // encoding |
| 62 | // ------------------------------------- |
| 63 | auto enc_result = codec->encode(original, enc_buffer, sizeof(enc_buffer)); |
| 64 | |
| 65 | // encoded internal header without errors |
| 66 | ASSERT_FALSE(enc_result.second); |
| 67 | |
| 68 | // ------------------------------------- |
| 69 | // decoding by hand |
| 70 | // ------------------------------------- |
| 71 | |
| 72 | Heartbeat_event_v2 decoded; |
| 73 | |
| 74 | // decode the post LOG_EVENT header |
| 75 | auto buffer = enc_buffer; |
| 76 | size_t buffer_size = sizeof(enc_buffer); |
| 77 | |
| 78 | auto dec_result = codec->decode(buffer, buffer_size, decoded); |
| 79 | |
| 80 | ASSERT_FALSE(dec_result.second); |
| 81 | ASSERT_TRUE( |
| 82 | original.get_log_filename().compare(decoded.get_log_filename()) == 0); |
| 83 | ASSERT_EQ(original.get_log_position(), decoded.get_log_position()); |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | TEST_F(HeartbeatCodecTest, EncodeDecodeIdempotencyBinaryTest) { |
| 88 | auto codec = binary_log::codecs::Factory::build_codec(HEARTBEAT_LOG_EVENT_V2); |
| 89 | HeartbeatCodecTest::run_codec_idempotency_test(codec); |
| 90 | } |
| 91 | |
| 92 | } // namespace unittests |
| 93 | } // namespace codecs |
| 94 | } // namespace binary_log |