| 1 | /* Copyright (c) 2023, 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 "libbinlogevents/include/buffer/managed_buffer_sequence.h" |
| 26 | |
| 27 | #include <gmock/gmock.h> |
| 28 | #include <gtest/gtest.h> |
| 29 | #include <functional> // std::function |
| 30 | #include <iterator> // std::distance |
| 31 | |
| 32 | #include "libbinlogevents/include/math/math.h" |
| 33 | #include "libbinlogevents/include/string/concat.h" |
| 34 | |
| 35 | using mysqlns::string::concat; |
| 36 | |
| 37 | namespace mysqlns::buffer::managed_buffer_sequence::unittest { |
| 38 | |
| 39 | /// "Token" used for the template argument for Accessor. In case |
| 40 | /// another unittest needs an Accessor too, the use of different |
| 41 | /// tokens distinguishes them. |
| 42 | class Grow_test_access_token {}; |
| 43 | |
| 44 | template <> |
| 45 | class Accessor<mysqlns::buffer::managed_buffer_sequence::unittest:: |
| 46 | Grow_test_access_token> { |
| 47 | public: |
| 48 | /// Export Grow_buffer_sequence::get_boundaries |
| 49 | template <class MBS, class BS> |
| 50 | static std::tuple<typename MBS::Iterator_t, typename MBS::Iterator_t, |
| 51 | typename MBS::Size_t> |
| 52 | get_boundaries(BS &bs) { |
| 53 | return MBS::get_boundaries(bs); |
| 54 | } |
| 55 | |
| 56 | /// Export Grow_buffer_sequence::m_buffers |
| 57 | template <class MBS> |
| 58 | static typename MBS::Container_t &m_buffers(MBS &mbs) { |
| 59 | return mbs.m_buffers; |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | using Access = Accessor< |
| 64 | mysqlns::buffer::managed_buffer_sequence::unittest::Grow_test_access_token>; |
| 65 | |
| 66 | } // namespace mysqlns::buffer::managed_buffer_sequence::unittest |
| 67 | |
| 68 | namespace mysqlns::buffer::managed_buffer_sequence::unittest { |
| 69 | |
| 70 | using Grow_status_t = mysqlns::buffer::Grow_status; |
| 71 | using Size_t = mysqlns::buffer::Buffer_view<>::Size_t; |
| 72 | using Debug_function = const std::function<std::string(std::string)>; |
| 73 | using Difference_t = mysqlns::buffer::Rw_buffer_sequence<>::Difference_t; |
| 74 | |
| 75 | // Return the current file and line as a string delimited and ended by |
| 76 | // colons. |
| 77 | // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) |
| 78 | #define FILELINE() concat(__FILE__, ":", __LINE__, ": ") |
| 79 | |
| 80 | // Helper macros to make assertions output the debug info we need, and |
| 81 | // make the program stop with assertion. |
| 82 | [[maybe_unused]] static int n_assertions = 0; |
| 83 | static bool _shall_stop_after_assertion = false; |
| 84 | // NOLINTBEGIN(cppcoreguidelines-macro-usage) |
| 85 | #define ASSERTION_TAIL \ |
| 86 | << debug_output(fileline) << (_shall_stop_after_assertion = true, ""), \ |
| 87 | assert(!_shall_stop_after_assertion) |
| 88 | #define AEQ(v1, v2) \ |
| 89 | do { \ |
| 90 | ASSERT_EQ(v1, v2) ASSERTION_TAIL; \ |
| 91 | ++n_assertions; \ |
| 92 | } while (0) |
| 93 | #define ANE(v1, v2) \ |
| 94 | do { \ |
| 95 | ASSERT_NE(v1, v2) ASSERTION_TAIL; \ |
| 96 | ++n_assertions; \ |
| 97 | } while (0) |
| 98 | // NOLINTEND(cppcoreguidelines-macro-usage) |
| 99 | |
| 100 | // Requirements: |
| 101 | // |
| 102 | // * Resize operations shall satisfy the following requirements: |
| 103 | // |
| 104 | // R1. When the existing or requested capacity is bigger than the max |
| 105 | // capacity, an error shall be returned. Otherwise, the resize shall |
| 106 | // succeed. |
| 107 | // |
| 108 | // R2. When the resize operation is successful, the read part shall |
| 109 | // remain unchanged and the write part shall increase as needed. |
| 110 | // |
| 111 | // R3. When the resize operation is successful, and new space is |
| 112 | // needed, one new buffer shall be allocated to accomodate the |
| 113 | // remaining requested capacity. |
| 114 | // |
| 115 | // R4. The buffer sequence shall remain self-consistent after the |
| 116 | // operation. |
| 117 | // |
| 118 | // * The operation of moving the position shall satisfy the following |
| 119 | // requirement: |
| 120 | // |
| 121 | // R5. It shall be possible to move the position from A to B, for any |
| 122 | // A, B between 0 and the capacity. |
| 123 | // |
| 124 | // * The reset operation shall satisfy the following requirements: |
| 125 | // |
| 126 | // R7. The reset operation shall set the read position to 0, leave the |
| 127 | // specified amount of buffers in the write part, and leave the |
| 128 | // specified extra capacity in the vector |
| 129 | // |
| 130 | // The exact execution of the resize operation and the operation of |
| 131 | // moving the position depends on the relative order of the following |
| 132 | // quantities: |
| 133 | // |
| 134 | // - capacity |
| 135 | // - position |
| 136 | // - extra container capacity |
| 137 | // - requested capacity |
| 138 | // - max capacity |
| 139 | // - requested new position |
| 140 | // |
| 141 | // The behavior may also depend on whether some of these are 0 or not, |
| 142 | // and whether some are equal or not. |
| 143 | // |
| 144 | // We test all relative orders among these variables, including |
| 145 | // boundary cases where some of them are equal, and including cases |
| 146 | // where the smallest variable is 0 as well as nonzero. Therefore |
| 147 | // we vary all these quantities between 0 and 4, inclusive. |
| 148 | // |
| 149 | // We expect the following outcome: |
| 150 | // - if capacity > max capacity, or requested capacity > max capacity, |
| 151 | // exceeds_max_size is returned |
| 152 | // - else if requested capacity <= capacity, capacity is unchanged |
| 153 | // - else, new capacity is requested capacity |
| 154 | template <class Char_t, template <class E, class A> class Container_t> |
| 155 | class Grow_tester { |
| 156 | public: |
| 157 | using Managed_buffer_sequence_t = |
| 158 | buffer::Managed_buffer_sequence<Char_t, Container_t>; |
| 159 | using Rw_buffer_sequence_t = buffer::Rw_buffer_sequence<Char_t, Container_t>; |
| 160 | |
| 161 | // Execute "grow_test" in many configurations. |
| 162 | // |
| 163 | // The goal is to exercise as many potential corner cases as |
| 164 | // possible related to growing the buffer sequence or moving the |
| 165 | // position. |
| 166 | // |
| 167 | // In each scenario, we will setup the buffer in an "initial state", |
| 168 | // verify that it is as we expect, attempt to grow it, verify that |
| 169 | // it worked or didn't work according to expectation, attempt to |
| 170 | // move the position, verify that it worked, reset, verify that it |
| 171 | // worked. |
| 172 | // |
| 173 | // We vary the scenarios by using different initial states, by using |
| 174 | // different requestes for more capacity, by setting different |
| 175 | // maximum sizes in the Grow_calculator, and by moving the position to |
| 176 | // different places. |
| 177 | // |
| 178 | // In general, the state of a managed buffer sequence is described |
| 179 | // by the size of the read part, the total capacity of the read+write |
| 180 | // parts, and the number of extra null buffers. To test many types |
| 181 | // of initial states, we should exercise cases where the position |
| 182 | // and the capacity are 0, or on a buffer boundary, or in the middle of |
| 183 | // a buffer; when they coincide, or are both within the same buffer, |
| 184 | // or are farther apart; when there is no extra capacity in the |
| 185 | // container, when there is one extra buffer, or when there are two |
| 186 | // extra buffers. |
| 187 | // |
| 188 | // We use up to 3 buffers, and vary all the positions from 0 up to |
| 189 | // the end of the last buffer, in steps of 1/N buffer, where |
| 190 | // N=parts_per_buffer. |
| 191 | // |
| 192 | // We use a Grow_calculator that gives us exactly as many bytes as we |
| 193 | // ask for. |
| 194 | void combinatorial_grow_test() { |
| 195 | [[maybe_unused]] static int n_scenarios = 0; |
| 196 | for (Size_t parts_per_buffer : {1, 4}) { |
| 197 | for (Size_t part_size : {1, 100}) { |
| 198 | Size_t buffer_size = parts_per_buffer * part_size; |
| 199 | for (Size_t extra_container_capacity : {0, 1, 2}) { |
| 200 | for (Size_t capacity = 0; capacity <= buffer_size * 3; |
| 201 | capacity += buffer_size) { |
| 202 | std::set<Size_t> requests; |
| 203 | requests.insert(0); |
| 204 | if (capacity > part_size) requests.insert(capacity - part_size); |
| 205 | requests.insert(capacity); |
| 206 | requests.insert(capacity + part_size); |
| 207 | if (parts_per_buffer >= 2) |
| 208 | requests.insert(capacity + 2 * part_size); |
| 209 | for (Size_t position = 0; position <= capacity; |
| 210 | position += part_size) { |
| 211 | for (Size_t max_capacity : requests) { |
| 212 | for (Size_t requested_capacity : requests) { |
| 213 | auto new_size = compute_new_size(capacity, max_capacity, |
| 214 | requested_capacity); |
| 215 | for (Size_t requested_position = 0; |
| 216 | requested_position <= new_size; |
| 217 | requested_position += part_size) { |
| 218 | ++n_scenarios; |
| 219 | grow_test(part_size, parts_per_buffer, |
| 220 | extra_container_capacity, position, capacity, |
| 221 | max_capacity, requested_capacity, |
| 222 | requested_position); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | // Uncomment for debugging. |
| 232 | // std::cout << "scenarios: " << n_scenarios |
| 233 | // << " assertions: " << n_assertions |
| 234 | // << "\n"; |
| 235 | } |
| 236 | |
| 237 | Size_t compute_new_size(Size_t capacity, Size_t max_capacity, |
| 238 | Size_t requested_capacity) { |
| 239 | // In the first two cases, either the existing size or the |
| 240 | // requested size exceeds the maximum size configured in the |
| 241 | // Grow_calculator. Therefore it refuses to grow and leaves the buffer |
| 242 | // unchanged. In the third case, the request is for a smaller |
| 243 | // size than the existing size, so it succeeds and leaves the |
| 244 | // buffer sequence unchanged. |
| 245 | if (capacity > max_capacity || requested_capacity > max_capacity || |
| 246 | requested_capacity <= capacity) |
| 247 | return capacity; |
| 248 | return requested_capacity; |
| 249 | } |
| 250 | |
| 251 | void grow_test(Size_t part_size, Size_t parts_per_buffer, |
| 252 | Size_t extra_container_capacity, Size_t position, |
| 253 | Size_t capacity, Size_t max_capacity, |
| 254 | Size_t requested_capacity, Size_t requested_position) { |
| 255 | // NOLINTBEGIN(cppcoreguidelines-macro-usage) |
| 256 | #define CHECK_SIZES(POSITION, CAPACITY) \ |
| 257 | check_sizes(FILELINE(), debug_output, mbs, buffer_size, POSITION, CAPACITY) |
| 258 | // NOLINTEND(cppcoreguidelines-macro-usage) |
| 259 | |
| 260 | // This does the following: |
| 261 | // |
| 262 | // 1. Create empty buffer sequence |
| 263 | // 2. Allocate the required container capacity |
| 264 | // 3. Reset to the required used buffer count |
| 265 | // 4. Grow to the required total size |
| 266 | // 5. Set the position as required |
| 267 | // 6. Set Grow_calculator.max_size and grow to the requested size |
| 268 | // 7. Move position to the requested new position |
| 269 | |
| 270 | Size_t buffer_size = part_size * parts_per_buffer; |
| 271 | Size_t new_capacity = |
| 272 | compute_new_size(capacity, max_capacity, requested_capacity); |
| 273 | |
| 274 | // **** Empty buffer **** |
| 275 | |
| 276 | // Prepare initial buffer sequence with given default size and old size |
| 277 | Managed_buffer_sequence_t mbs; |
| 278 | |
| 279 | auto work = [&](bool use_increase_position_api) { |
| 280 | // Prepare grow Grow_calculator with a high max size |
| 281 | { |
| 282 | auto gc = mbs.get_grow_calculator(); |
| 283 | gc.set_max_size(1000000); |
| 284 | gc.set_block_size(1); |
| 285 | gc.set_grow_factor(1); |
| 286 | gc.set_grow_increment(1); |
| 287 | mbs.set_grow_calculator(gc); |
| 288 | } |
| 289 | |
| 290 | std::string fileline; |
| 291 | Debug_function debug_output = [&](std::string fileline_arg) { |
| 292 | // clang-format off |
| 293 | return concat( |
| 294 | fileline_arg, |
| 295 | "\npart_size=", part_size, |
| 296 | ",\nparts_per_buffer=", parts_per_buffer, |
| 297 | ",\nextra_container_capacity=", extra_container_capacity, |
| 298 | ",\nposition=", position, |
| 299 | ",\ncapacity=", capacity, |
| 300 | ",\nmax_capacity=", max_capacity, |
| 301 | ",\nrequested_capacity=", requested_capacity, |
| 302 | ",\nrequested_position=", requested_position, |
| 303 | ",\nnew_capacity=", new_capacity, |
| 304 | ",\nmbs=", mbs.debug_string()); |
| 305 | // clang-format on |
| 306 | }; |
| 307 | |
| 308 | CHECK_SIZES(0, 0); |
| 309 | |
| 310 | // **** allocate vector capacity **** |
| 311 | |
| 312 | // Allocate the buffers we need |
| 313 | Size_t accumulated_size = 0; |
| 314 | while (mbs.capacity() < |
| 315 | capacity + buffer_size * extra_container_capacity) { |
| 316 | // Grow |
| 317 | accumulated_size += buffer_size; |
| 318 | auto status = mbs.reserve_write_size(accumulated_size); |
| 319 | AEQ(status, Grow_status::success); |
| 320 | CHECK_SIZES(0, accumulated_size); |
| 321 | } |
| 322 | Size_t rw_count = capacity / buffer_size; |
| 323 | |
| 324 | mbs.reset(rw_count, rw_count + 1 + extra_container_capacity); |
| 325 | |
| 326 | CHECK_SIZES(0, capacity); |
| 327 | |
| 328 | // **** move position **** |
| 329 | |
| 330 | mbs.set_position(position); |
| 331 | CHECK_SIZES(position, capacity); |
| 332 | |
| 333 | // **** grow **** |
| 334 | { |
| 335 | auto gc = mbs.get_grow_calculator(); |
| 336 | gc.set_max_size(max_capacity); |
| 337 | mbs.set_grow_calculator(gc); |
| 338 | |
| 339 | auto status = mbs.reserve_total_size(requested_capacity); |
| 340 | |
| 341 | auto expected_status = Grow_status_t::success; |
| 342 | if (capacity > max_capacity || requested_capacity > max_capacity) |
| 343 | expected_status = Grow_status_t::exceeds_max_size; |
| 344 | AEQ(status, expected_status); |
| 345 | |
| 346 | CHECK_SIZES(position, new_capacity); |
| 347 | } |
| 348 | |
| 349 | // *** move position **** |
| 350 | |
| 351 | // Use different methods to move the position, just to exercise |
| 352 | // all the API entries. |
| 353 | if (requested_position >= position && use_increase_position_api) |
| 354 | mbs.increase_position(requested_position - position); |
| 355 | else |
| 356 | mbs.move_position(Difference_t(requested_position) - |
| 357 | Difference_t(position)); |
| 358 | |
| 359 | CHECK_SIZES(requested_position, new_capacity); |
| 360 | |
| 361 | // **** reset **** |
| 362 | mbs.reset(); |
| 363 | |
| 364 | Size_t first_buffer_size = std::min(buffer_size, new_capacity); |
| 365 | CHECK_SIZES(0, first_buffer_size); |
| 366 | |
| 367 | mbs.reset(0, 0); |
| 368 | CHECK_SIZES(0, 0); |
| 369 | }; |
| 370 | |
| 371 | // Execute twice, just to verify that the sequence is still |
| 372 | // functional after `reset()`, and also to exercise all API |
| 373 | // entries for moving the position. |
| 374 | work(false); |
| 375 | work(true); |
| 376 | } |
| 377 | |
| 378 | // Check that the sizes of the parts, and the number of buffers per |
| 379 | // part, is as expected. |
| 380 | void check_sizes(const std::string &fileline, Debug_function &debug_prefix, |
| 381 | Managed_buffer_sequence_t &mbs, Size_t buffer_size, |
| 382 | Size_t position, Size_t capacity) { |
| 383 | // size of each part |
| 384 | auto w_size = capacity - position; |
| 385 | |
| 386 | // offset of each part boundary from beginning of container |
| 387 | auto end_distance = [&](Size_t s) { |
| 388 | return mysqlns::math::ceil_div(s, buffer_size); |
| 389 | }; |
| 390 | auto begin_distance = [&](Size_t s) { |
| 391 | if (s == capacity) return mysqlns::math::ceil_div(s, buffer_size); |
| 392 | return s / buffer_size; |
| 393 | }; |
| 394 | auto r_end = end_distance(position); |
| 395 | auto w_begin = 1 + begin_distance(position); |
| 396 | auto w_end = 1 + end_distance(capacity); |
| 397 | |
| 398 | auto debug_output = [&](std::string fileline_arg) { |
| 399 | // clang-format off |
| 400 | return concat(debug_prefix(std::move(fileline_arg)), |
| 401 | ", position=", position, |
| 402 | ", w_size=", w_size, |
| 403 | ", r_end=", r_end, |
| 404 | ", w_begin=", w_begin, |
| 405 | ", w_end=", w_end); |
| 406 | // clang-format on |
| 407 | }; |
| 408 | check_self_consistent(fileline, debug_output, mbs); |
| 409 | |
| 410 | auto &r = mbs.read_part(); |
| 411 | auto &w = mbs.write_part(); |
| 412 | |
| 413 | AEQ(r.size(), position); |
| 414 | AEQ(w.size(), w_size); |
| 415 | AEQ(mbs.capacity(), capacity); |
| 416 | |
| 417 | AEQ(std::distance(r.begin(), r.end()), r_end); |
| 418 | AEQ(std::distance(r.begin(), w.begin()), w_begin); |
| 419 | AEQ(std::distance(r.begin(), w.end()), w_end); |
| 420 | } |
| 421 | |
| 422 | // Check that the Managed_buffer_sequence is self-consistent. |
| 423 | // |
| 424 | // Requirements: |
| 425 | // |
| 426 | // R1. The sum of buffer sizes should equal the result from size(). |
| 427 | // This should hold for the read part, for the write part, and |
| 428 | // for the managed buffer sequence as a whole. |
| 429 | // |
| 430 | // R2. No buffer in the read part or in the write part should be a |
| 431 | // null buffer. All buffers after the write part should be null |
| 432 | // buffers. Any buffer between the read and write part should |
| 433 | // be a null buffer. |
| 434 | // |
| 435 | // R3. If the position splits a buffer, the second half should begin |
| 436 | // where the first half ends. |
| 437 | // |
| 438 | // R4. If the position does not split a buffer, there should be a |
| 439 | // null buffer between the read part and the write part. |
| 440 | void check_self_consistent(const std::string &fileline, |
| 441 | Debug_function &debug_output, |
| 442 | Managed_buffer_sequence_t &mbs) { |
| 443 | using Iterator_t = typename Managed_buffer_sequence_t::Iterator_t; |
| 444 | using Size_t = typename Managed_buffer_sequence_t::Size_t; |
| 445 | |
| 446 | auto [read_begin, read_end, read_size] = |
| 447 | Access::get_boundaries<Managed_buffer_sequence_t>(mbs.read_part()); |
| 448 | auto [write_begin, write_end, write_size] = |
| 449 | Access::get_boundaries<Managed_buffer_sequence_t>(mbs.write_part()); |
| 450 | |
| 451 | // R1. reported size matching actual size |
| 452 | auto get_size = [&](Iterator_t begin, Iterator_t end) { |
| 453 | Size_t size{0}; |
| 454 | for (auto it = begin; it != end; ++it) size += it->size(); |
| 455 | return size; |
| 456 | }; |
| 457 | AEQ(get_size(read_begin, read_end), read_size); |
| 458 | AEQ(get_size(write_begin, write_end), write_size); |
| 459 | AEQ(get_size(read_begin, write_end), mbs.capacity()); |
| 460 | |
| 461 | // R2. null / not-null buffers where expected |
| 462 | auto check_no_null_buffer = [&](Iterator_t begin, Iterator_t end) { |
| 463 | for (auto it = begin; it != end; ++it) { |
| 464 | ANE(it->data(), nullptr); |
| 465 | ANE(it->size(), 0); |
| 466 | } |
| 467 | }; |
| 468 | auto check_null_buffer = [&](Iterator_t begin, Iterator_t end) { |
| 469 | for (auto it = begin; it != end; ++it) { |
| 470 | AEQ(it->data(), nullptr); |
| 471 | AEQ(it->size(), 0); |
| 472 | } |
| 473 | }; |
| 474 | check_no_null_buffer(read_begin, read_end); |
| 475 | check_null_buffer(read_end, write_begin); |
| 476 | check_no_null_buffer(write_begin, write_end); |
| 477 | check_null_buffer(write_end, Access::m_buffers(mbs).end()); |
| 478 | |
| 479 | // boundary between read and write part |
| 480 | auto before_write = std::next(write_begin, -1); |
| 481 | if (read_end == write_begin) |
| 482 | // R3. split buffer: right half should begin where left half ends |
| 483 | AEQ(before_write->end(), write_begin->begin()); |
| 484 | else |
| 485 | // R4. no split buffer: should be exactly one buffer between |
| 486 | AEQ(before_write, read_end); |
| 487 | } |
| 488 | }; |
| 489 | |
| 490 | TEST(ManagedBufferSequenceTest, CombinatorialGrowTestCharVector) { |
| 491 | Grow_tester<char, std::vector>().combinatorial_grow_test(); |
| 492 | } |
| 493 | |
| 494 | TEST(ManagedBufferSequenceTest, CombinatorialGrowTestUcharVector) { |
| 495 | Grow_tester<unsigned char, std::vector>().combinatorial_grow_test(); |
| 496 | } |
| 497 | |
| 498 | TEST(ManagedBufferSequenceTest, CombinatorialGrowTestCharList) { |
| 499 | Grow_tester<char, std::list>().combinatorial_grow_test(); |
| 500 | } |
| 501 | |
| 502 | TEST(ManagedBufferSequenceTest, CombinatorialGrowTestUcharList) { |
| 503 | Grow_tester<unsigned char, std::list>().combinatorial_grow_test(); |
| 504 | } |
| 505 | |
| 506 | } // namespace mysqlns::buffer::managed_buffer_sequence::unittest |