| 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 <chrono> |
| 27 | #include <vector> |
| 28 | |
| 29 | #include <gtest/gtest.h> |
| 30 | #include "libbinlogevents/include/binary_log.h" |
| 31 | #include "libbinlogevents/include/compression/iterator.h" |
| 32 | #include "libbinlogevents/include/compression/zstd_comp.h" |
| 33 | #include "my_byteorder.h" |
| 34 | |
| 35 | namespace binary_log { |
| 36 | namespace transaction { |
| 37 | namespace unittests { |
| 38 | |
| 39 | class TransactionPayloadIteratorTest : public ::testing::Test { |
| 40 | public: |
| 41 | std::vector<std::size_t> m_payloads; |
| 42 | |
| 43 | protected: |
| 44 | TransactionPayloadIteratorTest() = default; |
| 45 | |
| 46 | void SetUp() override {} |
| 47 | |
| 48 | void TearDown() override {} |
| 49 | |
| 50 | static unsigned char *mock_event(unsigned char *buffer, Log_event_type type) { |
| 51 | std::time_t timestamp = |
| 52 | std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); |
| 53 | |
| 54 | int4store(buffer, timestamp); |
| 55 | buffer[EVENT_TYPE_OFFSET] = type; |
| 56 | int4store(buffer + SERVER_ID_OFFSET, 1); |
| 57 | int4store(buffer + EVENT_LEN_OFFSET, |
| 58 | static_cast<uint32>(LOG_EVENT_HEADER_LEN)); |
| 59 | int4store(buffer + LOG_POS_OFFSET, static_cast<uint32>(0)); |
| 60 | int2store(buffer + FLAGS_OFFSET, 0); |
| 61 | |
| 62 | return buffer + LOG_EVENT_HEADER_LEN; |
| 63 | } |
| 64 | |
| 65 | static void iterator_test_compressed() { |
| 66 | size_t buffer_size{LOG_EVENT_HEADER_LEN * 5}; |
| 67 | size_t osize{0}; |
| 68 | unsigned char *ibuf = (unsigned char *)malloc(buffer_size); |
| 69 | unsigned char *obuf = (unsigned char *)malloc(buffer_size); |
| 70 | unsigned char *ptr{ibuf}; |
| 71 | |
| 72 | // the compressor used |
| 73 | binary_log::transaction::compression::Zstd_comp c; |
| 74 | |
| 75 | std::vector<Log_event_type> types = {QUERY_EVENT, ROWS_QUERY_LOG_EVENT, |
| 76 | TABLE_MAP_EVENT, WRITE_ROWS_EVENT, |
| 77 | XID_EVENT}; |
| 78 | |
| 79 | // fake the events |
| 80 | for (auto type : types) { |
| 81 | ptr = mock_event(ptr, type); |
| 82 | } |
| 83 | |
| 84 | // compress |
| 85 | c.set_buffer(obuf, buffer_size); |
| 86 | c.open(); |
| 87 | c.compress(ibuf, buffer_size); |
| 88 | c.close(); |
| 89 | |
| 90 | // get the compressed buffer |
| 91 | std::tie(obuf, osize, std::ignore) = c.get_buffer(); |
| 92 | |
| 93 | // create iterator over the compressed buffer |
| 94 | binary_log::transaction::compression::Iterable_buffer ib( |
| 95 | (const char *)obuf, osize, buffer_size, c.compression_type_code()); |
| 96 | |
| 97 | int i = 0; |
| 98 | for (auto p : ib) { |
| 99 | Log_event_type type = types[i]; |
| 100 | ASSERT_EQ(type, p[EVENT_TYPE_OFFSET]); |
| 101 | i++; |
| 102 | } |
| 103 | free(obuf); |
| 104 | free(ibuf); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | TEST_F(TransactionPayloadIteratorTest, IteratorBufferTest) { |
| 109 | TransactionPayloadIteratorTest::iterator_test_compressed(); |
| 110 | } |
| 111 | |
| 112 | } // namespace unittests |
| 113 | } // namespace transaction |
| 114 | } // namespace binary_log |