| 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 | #include <gtest/gtest.h> |
| 25 | #include <sstream> |
| 26 | #include <string> |
| 27 | |
| 28 | #include "libchangestreams/include/mysql/cs/reader/state.h" |
| 29 | #include "libchangestreams/src/lib/mysql/cs/codec/pb/reader_state_codec_pb.h" |
| 30 | |
| 31 | namespace cs::reader::unittests { |
| 32 | |
| 33 | const std::string DEFAULT_UUID1 = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"; |
| 34 | const std::string DEFAULT_UUID2 = "aaaaaaaa-aaaa-aaaa-aaaa-bbbbbbbbbbbb"; |
| 35 | |
| 36 | class ReaderStateCodecTest : public ::testing::Test { |
| 37 | protected: |
| 38 | binary_log::gtids::Uuid valid_uuid1; |
| 39 | binary_log::gtids::Uuid valid_uuid2; |
| 40 | cs::reader::State state; |
| 41 | |
| 42 | binary_log::gtids::Gtid gtid1_1{valid_uuid1, 0}; |
| 43 | binary_log::gtids::Gtid gtid1_2{valid_uuid1, 0}; |
| 44 | binary_log::gtids::Gtid gtid2_1{valid_uuid1, 0}; |
| 45 | binary_log::gtids::Gtid gtid1_1_copy{valid_uuid1, 0}; |
| 46 | |
| 47 | ReaderStateCodecTest() = default; |
| 48 | |
| 49 | void SetUp() override { |
| 50 | ASSERT_FALSE( |
| 51 | valid_uuid1.parse(DEFAULT_UUID1.c_str(), DEFAULT_UUID1.size())); |
| 52 | ASSERT_FALSE( |
| 53 | valid_uuid2.parse(DEFAULT_UUID2.c_str(), DEFAULT_UUID2.size())); |
| 54 | |
| 55 | gtid1_1 = {valid_uuid1, 1}; |
| 56 | gtid1_2 = {valid_uuid1, 2}; |
| 57 | gtid2_1 = {valid_uuid2, 1}; |
| 58 | gtid1_1_copy = {valid_uuid1, 1}; |
| 59 | } |
| 60 | |
| 61 | void TearDown() override {} |
| 62 | }; |
| 63 | |
| 64 | TEST_F(ReaderStateCodecTest, StateBasics) { |
| 65 | state.add_gtid(gtid1_1); |
| 66 | state.add_gtid(gtid1_2); |
| 67 | state.add_gtid(gtid2_1); |
| 68 | state.add_gtid(gtid1_1_copy); |
| 69 | |
| 70 | ASSERT_TRUE(state.get_gtids().contains(gtid1_1)); |
| 71 | ASSERT_TRUE(state.get_gtids().contains(gtid1_2)); |
| 72 | ASSERT_TRUE(state.get_gtids().contains(gtid2_1)); |
| 73 | |
| 74 | // serialize to protobuf |
| 75 | cs::reader::codec::pb::example::stringstream pb_ss; |
| 76 | pb_ss << state; |
| 77 | |
| 78 | // de-serialize from protobuf |
| 79 | cs::reader::State state_copy; |
| 80 | pb_ss >> state_copy; |
| 81 | |
| 82 | ASSERT_TRUE(state_copy.get_gtids().contains(gtid1_1)); |
| 83 | ASSERT_TRUE(state_copy.get_gtids().contains(gtid1_2)); |
| 84 | ASSERT_TRUE(state_copy.get_gtids().contains(gtid2_1)); |
| 85 | |
| 86 | // compare string representation |
| 87 | std::stringstream ss_state1; |
| 88 | std::stringstream ss_state2; |
| 89 | |
| 90 | ss_state1 << state; |
| 91 | ss_state2 << state_copy; |
| 92 | |
| 93 | ASSERT_EQ(ss_state1.str(), ss_state2.str()); |
| 94 | } |
| 95 | |
| 96 | } // namespace cs::reader::unittests |