| 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 "libbinlogevents/include/gtids/gtid.h" |
| 29 | #include "libbinlogevents/include/gtids/gtidset.h" |
| 30 | |
| 31 | namespace binary_log::gtids::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 | const std::string DEFAULT_UUID3 = "cccccccc-cccc-cccc-cccc-cccccccccccc"; |
| 36 | const std::string INVALID_UUID = "-aaaa-aaaa-aaaa-bbbbbbbbbbbb"; |
| 37 | |
| 38 | class GtidsTest : public ::testing::Test { |
| 39 | protected: |
| 40 | binary_log::gtids::Uuid valid_uuid1; |
| 41 | binary_log::gtids::Uuid valid_uuid2; |
| 42 | binary_log::gtids::Uuid valid_uuid3; |
| 43 | binary_log::gtids::Uuid invalid_uuid; |
| 44 | |
| 45 | binary_log::gtids::Gtid gtid1_1{valid_uuid1, 0}; |
| 46 | binary_log::gtids::Gtid gtid1_2{valid_uuid1, 0}; |
| 47 | binary_log::gtids::Gtid gtid1_100{valid_uuid1, 0}; |
| 48 | binary_log::gtids::Gtid gtid2_1{valid_uuid2, 0}; |
| 49 | binary_log::gtids::Gtid gtid2_2{valid_uuid2, 0}; |
| 50 | binary_log::gtids::Gtid gtid2_1000{valid_uuid2, 0}; |
| 51 | binary_log::gtids::Gtid gtid3_1{valid_uuid3, 0}; |
| 52 | binary_log::gtids::Gtid gtid3_2{valid_uuid3, 0}; |
| 53 | binary_log::gtids::Gtid gtid1_1_copy{valid_uuid1, 0}; |
| 54 | binary_log::gtids::Gtid gtid__1{invalid_uuid, 0}; |
| 55 | |
| 56 | GtidsTest() = default; |
| 57 | |
| 58 | void SetUp() override { |
| 59 | ASSERT_FALSE( |
| 60 | valid_uuid1.parse(DEFAULT_UUID1.c_str(), DEFAULT_UUID1.size())); |
| 61 | ASSERT_FALSE( |
| 62 | valid_uuid2.parse(DEFAULT_UUID2.c_str(), DEFAULT_UUID2.size())); |
| 63 | ASSERT_FALSE( |
| 64 | valid_uuid3.parse(DEFAULT_UUID3.c_str(), DEFAULT_UUID3.size())); |
| 65 | ASSERT_TRUE(invalid_uuid.parse(INVALID_UUID.c_str(), INVALID_UUID.size())); |
| 66 | |
| 67 | gtid1_1 = {valid_uuid1, 1}; |
| 68 | gtid1_2 = {valid_uuid1, 2}; |
| 69 | gtid1_100 = {valid_uuid1, 100}; |
| 70 | gtid2_1 = {valid_uuid2, 1}; |
| 71 | gtid2_2 = {valid_uuid2, 2}; |
| 72 | gtid2_1000 = {valid_uuid2, 1000}; |
| 73 | gtid3_1 = {valid_uuid3, 1}; |
| 74 | gtid3_2 = {valid_uuid3, 2}; |
| 75 | gtid1_1_copy = {valid_uuid1, 1}; |
| 76 | gtid__1 = {invalid_uuid, 1}; |
| 77 | } |
| 78 | |
| 79 | void TearDown() override {} |
| 80 | }; |
| 81 | |
| 82 | TEST_F(GtidsTest, GtidCopyAssignment) { |
| 83 | binary_log::gtids::Gtid gtid2_1_assigned = gtid2_1; |
| 84 | ASSERT_EQ(gtid2_1_assigned, gtid2_1); |
| 85 | } |
| 86 | |
| 87 | TEST_F(GtidsTest, GtidComparison) { |
| 88 | ASSERT_TRUE(gtid1_1 != gtid1_2); |
| 89 | ASSERT_FALSE(gtid1_1 == gtid1_2); |
| 90 | ASSERT_FALSE(gtid1_1 == gtid2_1); |
| 91 | ASSERT_TRUE(gtid1_1 == gtid1_1_copy); |
| 92 | } |
| 93 | |
| 94 | TEST_F(GtidsTest, GtidToString) { |
| 95 | std::stringstream expected; |
| 96 | expected << DEFAULT_UUID1 << binary_log::gtids::Gtid::SEPARATOR_UUID_SEQNO |
| 97 | << gtid1_1.get_gno(); |
| 98 | |
| 99 | ASSERT_EQ(expected.str(), gtid1_1.to_string()); |
| 100 | |
| 101 | expected.str(""); |
| 102 | expected << DEFAULT_UUID2 << binary_log::gtids::Gtid::SEPARATOR_UUID_SEQNO |
| 103 | << gtid2_1.get_gno(); |
| 104 | ASSERT_EQ(expected.str(), gtid2_1.to_string()); |
| 105 | |
| 106 | expected.str(""); |
| 107 | expected << INVALID_UUID << binary_log::gtids::Gtid::SEPARATOR_UUID_SEQNO |
| 108 | << gtid__1.get_gno(); |
| 109 | ASSERT_NE(expected.str(), gtid__1.to_string()); |
| 110 | } |
| 111 | |
| 112 | TEST_F(GtidsTest, IntervalsBasics) { |
| 113 | binary_log::gtids::gno_t range_start = 1; |
| 114 | binary_log::gtids::gno_t range_end = 2; |
| 115 | binary_log::gtids::gno_t next_in_range = 3; |
| 116 | binary_log::gtids::gno_t not_in_range = 100; |
| 117 | |
| 118 | binary_log::gtids::Gno_interval interval{range_start, range_end + 1}; |
| 119 | |
| 120 | binary_log::gtids::Gtid next_gtid{valid_uuid1, next_in_range}; |
| 121 | binary_log::gtids::Gtid not_in_range_gtid1{valid_uuid1, not_in_range}; |
| 122 | binary_log::gtids::Gtid not_in_range_gtid2{valid_uuid2, next_in_range}; |
| 123 | binary_log::gtids::Gtid next_gtid_copy{valid_uuid1, next_in_range}; |
| 124 | |
| 125 | ASSERT_TRUE(interval.intersects_or_contiguous( |
| 126 | binary_log::gtids::Gno_interval{next_in_range, next_in_range + 1})); |
| 127 | ASSERT_FALSE(interval.intersects( |
| 128 | binary_log::gtids::Gno_interval{not_in_range, not_in_range + 1})); |
| 129 | ASSERT_TRUE(next_gtid == next_gtid_copy); |
| 130 | } |
| 131 | |
| 132 | TEST_F(GtidsTest, IntervalsMerge) { |
| 133 | binary_log::gtids::Gno_interval interval1{1, 2}; |
| 134 | binary_log::gtids::Gno_interval interval2{3, 4}; |
| 135 | binary_log::gtids::Gno_interval interval3{100, 101}; |
| 136 | binary_log::gtids::Gno_interval interval4{3, 90}; |
| 137 | |
| 138 | ASSERT_TRUE(interval1.intersects_or_contiguous(interval2)); |
| 139 | ASSERT_FALSE(interval1.add(interval2)); |
| 140 | ASSERT_TRUE(interval1.add(interval3)); |
| 141 | ASSERT_FALSE(interval1.add(interval4)); |
| 142 | |
| 143 | std::stringstream expected; |
| 144 | expected << interval1.get_start() |
| 145 | << binary_log::gtids::Gno_interval::SEPARATOR_GNO_START_END |
| 146 | << interval4.get_end(); |
| 147 | ASSERT_EQ(expected.str(), interval1.to_string()); |
| 148 | |
| 149 | expected.str(""); |
| 150 | expected << 100 << binary_log::gtids::Gno_interval::SEPARATOR_GNO_START_END |
| 151 | << 101; |
| 152 | ASSERT_EQ(expected.str(), interval3.to_string()); |
| 153 | } |
| 154 | |
| 155 | TEST_F(GtidsTest, GtidSetBasics) { |
| 156 | binary_log::gtids::Gtid_set set1; |
| 157 | |
| 158 | std::stringstream expected; |
| 159 | expected << binary_log::gtids::Gtid_set::EMPTY_GTID_SET; |
| 160 | ASSERT_EQ(expected.str(), set1.to_string()); |
| 161 | expected.str(""); |
| 162 | |
| 163 | set1.add(gtid1_1); |
| 164 | expected << DEFAULT_UUID1 << binary_log::gtids::Gtid::SEPARATOR_UUID_SEQNO |
| 165 | << gtid1_1.get_gno(); |
| 166 | ASSERT_EQ(set1.to_string(), expected.str()); |
| 167 | expected.str(""); |
| 168 | |
| 169 | set1.add(gtid1_2); |
| 170 | binary_log::gtids::Gno_interval i1{1, 2}; |
| 171 | expected << DEFAULT_UUID1 << binary_log::gtids::Gtid::SEPARATOR_UUID_SEQNO |
| 172 | << i1.to_string(); |
| 173 | ASSERT_EQ(set1.to_string(), expected.str()); |
| 174 | expected.str(""); |
| 175 | |
| 176 | set1.add(gtid1_100); |
| 177 | expected << DEFAULT_UUID1 << binary_log::gtids::Gtid::SEPARATOR_UUID_SEQNO |
| 178 | << i1.to_string() |
| 179 | << binary_log::gtids::Gtid_set::SEPARATOR_SEQNO_INTERVALS |
| 180 | << gtid1_100.get_gno(); |
| 181 | ASSERT_EQ(set1.to_string(), expected.str()); |
| 182 | expected.str(""); |
| 183 | |
| 184 | set1.add(gtid2_1); |
| 185 | expected << DEFAULT_UUID1 << binary_log::gtids::Gtid::SEPARATOR_UUID_SEQNO |
| 186 | << i1.to_string() |
| 187 | << binary_log::gtids::Gtid_set::SEPARATOR_SEQNO_INTERVALS |
| 188 | << gtid1_100.get_gno(); |
| 189 | expected << binary_log::gtids::Gtid_set::SEPARATOR_UUID_SETS << DEFAULT_UUID2 |
| 190 | << binary_log::gtids::Gtid::SEPARATOR_UUID_SEQNO |
| 191 | << gtid2_1.get_gno(); |
| 192 | // ASSERT_EQ(set1.to_string(), expected.str()); |
| 193 | expected.str(""); |
| 194 | |
| 195 | set1.add(gtid2_2); |
| 196 | expected << DEFAULT_UUID1 << binary_log::gtids::Gtid::SEPARATOR_UUID_SEQNO |
| 197 | << i1.to_string() |
| 198 | << binary_log::gtids::Gtid_set::SEPARATOR_SEQNO_INTERVALS |
| 199 | << gtid1_100.get_gno(); |
| 200 | expected << binary_log::gtids::Gtid_set::SEPARATOR_UUID_SETS; |
| 201 | expected << DEFAULT_UUID2 << binary_log::gtids::Gtid::SEPARATOR_UUID_SEQNO |
| 202 | << i1.to_string(); |
| 203 | // ASSERT_EQ(set1.to_string(), expected.str()); |
| 204 | expected.str(""); |
| 205 | |
| 206 | set1.add(gtid2_1000); |
| 207 | expected << DEFAULT_UUID1 << binary_log::gtids::Gtid::SEPARATOR_UUID_SEQNO |
| 208 | << i1.to_string() |
| 209 | << binary_log::gtids::Gtid_set::SEPARATOR_SEQNO_INTERVALS |
| 210 | << gtid1_100.get_gno(); |
| 211 | expected << binary_log::gtids::Gtid_set::SEPARATOR_UUID_SETS; |
| 212 | expected << DEFAULT_UUID2 << binary_log::gtids::Gtid::SEPARATOR_UUID_SEQNO |
| 213 | << i1.to_string() |
| 214 | << binary_log::gtids::Gtid_set::SEPARATOR_SEQNO_INTERVALS |
| 215 | << gtid2_1000.get_gno(); |
| 216 | ASSERT_EQ(set1.to_string(), expected.str()); |
| 217 | expected.str(""); |
| 218 | |
| 219 | binary_log::gtids::Gtid gtid2_99{valid_uuid2, 99}; |
| 220 | binary_log::gtids::Gtid gtid2_100{valid_uuid2, 100}; |
| 221 | binary_log::gtids::Gtid gtid2_101{valid_uuid2, 101}; |
| 222 | |
| 223 | binary_log::gtids::Gtid_set set2; |
| 224 | |
| 225 | set2.add(gtid2_99); |
| 226 | set2.add(gtid2_100); |
| 227 | set2.add(gtid2_101); |
| 228 | |
| 229 | binary_log::gtids::Gno_interval i2{gtid2_99.get_gno(), gtid2_101.get_gno()}; |
| 230 | expected << DEFAULT_UUID2 << binary_log::gtids::Gtid::SEPARATOR_UUID_SEQNO |
| 231 | << i2.to_string(); |
| 232 | ASSERT_EQ(set2.to_string(), expected.str()); |
| 233 | expected.str(""); |
| 234 | |
| 235 | set1.add(set2); |
| 236 | expected << DEFAULT_UUID1 << binary_log::gtids::Gtid::SEPARATOR_UUID_SEQNO |
| 237 | << i1.to_string() |
| 238 | << binary_log::gtids::Gtid_set::SEPARATOR_SEQNO_INTERVALS |
| 239 | << gtid1_100.get_gno(); |
| 240 | expected << binary_log::gtids::Gtid_set::SEPARATOR_UUID_SETS; |
| 241 | expected << DEFAULT_UUID2 << binary_log::gtids::Gtid::SEPARATOR_UUID_SEQNO |
| 242 | << i1.to_string() |
| 243 | << binary_log::gtids::Gtid_set::SEPARATOR_SEQNO_INTERVALS |
| 244 | << i2.to_string() |
| 245 | << binary_log::gtids::Gtid_set::SEPARATOR_SEQNO_INTERVALS |
| 246 | << gtid2_1000.get_gno(); |
| 247 | ASSERT_EQ(set1.to_string(), expected.str()); |
| 248 | expected.str(""); |
| 249 | } |
| 250 | |
| 251 | TEST_F(GtidsTest, GtidSetCopyAssignment) { |
| 252 | binary_log::gtids::Gtid_set set1; |
| 253 | |
| 254 | set1.add(gtid1_1); |
| 255 | set1.add(gtid1_2); |
| 256 | set1.add(gtid2_1); |
| 257 | |
| 258 | binary_log::gtids::Gtid_set set2 = set1; |
| 259 | |
| 260 | // contain the same number of gtids |
| 261 | ASSERT_EQ(set1.count(), 3); |
| 262 | ASSERT_EQ(set1.count(), set2.count()); |
| 263 | |
| 264 | // assert that the resulting set contains the gtids added to set1 |
| 265 | ASSERT_TRUE(set2.contains(gtid1_1)); |
| 266 | ASSERT_TRUE(set2.contains(gtid1_2)); |
| 267 | ASSERT_TRUE(set2.contains(gtid2_1)); |
| 268 | |
| 269 | // assert that both sets are equal |
| 270 | ASSERT_TRUE(set2 == set1); |
| 271 | } |
| 272 | |
| 273 | TEST_F(GtidsTest, GtidSetCountAndEmptyAndReset) { |
| 274 | binary_log::gtids::Gtid_set set1; |
| 275 | |
| 276 | ASSERT_EQ(set1.count(), 0); |
| 277 | ASSERT_TRUE(set1.is_empty()); |
| 278 | |
| 279 | set1.add(gtid1_1); |
| 280 | set1.add(gtid1_2); |
| 281 | set1.add(gtid2_1); |
| 282 | |
| 283 | ASSERT_EQ(set1.count(), 3); |
| 284 | ASSERT_FALSE(set1.is_empty()); |
| 285 | |
| 286 | set1.reset(); |
| 287 | |
| 288 | ASSERT_EQ(set1.count(), 0); |
| 289 | ASSERT_TRUE(set1.is_empty()); |
| 290 | } |
| 291 | |
| 292 | TEST_F(GtidsTest, GtidSetComparison) { |
| 293 | binary_log::gtids::Gtid_set set1; |
| 294 | set1.add(gtid1_1); |
| 295 | set1.add(gtid2_1); |
| 296 | |
| 297 | // empty set |
| 298 | binary_log::gtids::Gtid_set empty_set; |
| 299 | ASSERT_FALSE(set1 == empty_set); |
| 300 | |
| 301 | // same number of uuids as set1, same number of intervals |
| 302 | binary_log::gtids::Gtid_set equal_set; |
| 303 | equal_set.add(gtid1_1); |
| 304 | equal_set.add(gtid2_1); |
| 305 | ASSERT_TRUE(set1 == equal_set); |
| 306 | |
| 307 | // same number of uuids as set1, different interval in uuid2 |
| 308 | binary_log::gtids::Gtid_set set_with_same_uuids_more_intervals; |
| 309 | set_with_same_uuids_more_intervals.add(gtid1_1); |
| 310 | set_with_same_uuids_more_intervals.add(gtid2_1); |
| 311 | set_with_same_uuids_more_intervals.add(gtid2_2); |
| 312 | ASSERT_FALSE(set1 == set_with_same_uuids_more_intervals); |
| 313 | |
| 314 | // same number of uuids as set1, but one is different |
| 315 | binary_log::gtids::Gtid_set set_with_different_uuids; |
| 316 | set_with_different_uuids.add(gtid1_1); |
| 317 | set_with_different_uuids.add(gtid3_1); |
| 318 | ASSERT_FALSE(set1 == set_with_different_uuids); |
| 319 | } |
| 320 | |
| 321 | TEST_F(GtidsTest, GtidSetToString) { |
| 322 | binary_log::gtids::Gtid_set set1; |
| 323 | binary_log::gtids::Gtid_set set2; |
| 324 | |
| 325 | set1.add(gtid1_1); |
| 326 | set1.add(gtid1_2); |
| 327 | set1.add(gtid2_1); |
| 328 | |
| 329 | ASSERT_TRUE(set1.contains(gtid1_1)); |
| 330 | ASSERT_TRUE(set1.contains(gtid1_2)); |
| 331 | ASSERT_TRUE(set1.contains(gtid2_1)); |
| 332 | |
| 333 | set2.add(gtid1_1); |
| 334 | set2.add(gtid1_2); |
| 335 | set2.add(gtid2_1); |
| 336 | |
| 337 | ASSERT_TRUE(set2.contains(gtid1_1)); |
| 338 | ASSERT_TRUE(set2.contains(gtid1_2)); |
| 339 | ASSERT_TRUE(set2.contains(gtid2_1)); |
| 340 | |
| 341 | std::string set1_string = set1.to_string(); |
| 342 | std::string set2_string = set2.to_string(); |
| 343 | |
| 344 | ASSERT_EQ(set1_string, set2_string); |
| 345 | } |
| 346 | } // namespace binary_log::gtids::unittests |