| 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 <iostream> |
| 26 | |
| 27 | #include <array> |
| 28 | #include <random> |
| 29 | #include <vector> |
| 30 | |
| 31 | #include <gtest/gtest.h> |
| 32 | #include "libbinlogevents/include/binary_log.h" |
| 33 | #include "libbinlogevents/include/compression/none_comp.h" |
| 34 | #include "libbinlogevents/include/compression/payload_event_buffer_istream.h" |
| 35 | #include "libbinlogevents/include/compression/zstd_comp.h" |
| 36 | #include "libbinlogevents/include/string/concat.h" |
| 37 | #include "my_byteorder.h" |
| 38 | |
| 39 | using mysqlns::string::concat; |
| 40 | |
| 41 | namespace binary_log { |
| 42 | namespace transaction { |
| 43 | namespace unittests { |
| 44 | |
| 45 | /// Wrapper around an Iterator, providing an iterator that restarts |
| 46 | /// from the beginning, whenever the underlying one reaches the end. |
| 47 | template <class IT> |
| 48 | class Cyclic_iterator { |
| 49 | public: |
| 50 | using value_type = typename IT::value_type; |
| 51 | using pointer = typename IT::pointer; |
| 52 | using reference = typename IT::reference; |
| 53 | Cyclic_iterator(const IT &begin, const IT &end, const IT &initial) |
| 54 | : m_begin(begin), m_end(end), m_it(initial) {} |
| 55 | Cyclic_iterator(const IT &begin, const IT &end) |
| 56 | : Cyclic_iterator(begin, end, begin) {} |
| 57 | value_type operator++() { // ++it |
| 58 | ++m_it; |
| 59 | if (m_it == m_end) m_it = m_begin; |
| 60 | return *m_it; |
| 61 | } |
| 62 | value_type operator++(int) { // it++ |
| 63 | auto ret = *m_it; |
| 64 | ++m_it; |
| 65 | if (m_it == m_end) m_it = m_begin; |
| 66 | return ret; |
| 67 | } |
| 68 | reference operator*() { return *m_it; } |
| 69 | pointer operator->() { return m_it; } |
| 70 | |
| 71 | private: |
| 72 | const IT m_begin; |
| 73 | const IT m_end; |
| 74 | IT m_it; |
| 75 | }; |
| 76 | |
| 77 | template <class Compressor_tp> |
| 78 | class PayloadEventBufferStreamTest { |
| 79 | public: |
| 80 | using Managed_buffer_t = mysqlns::buffer::Managed_buffer<>; |
| 81 | using Managed_Buffer_sequence_t = mysqlns::buffer::Managed_buffer_sequence<>; |
| 82 | using Compressor_t = Compressor_tp; |
| 83 | using Compress_status_t = |
| 84 | binary_log::transaction::compression::Compress_status; |
| 85 | using Decompress_status_t = |
| 86 | binary_log::transaction::compression::Decompress_status; |
| 87 | using Size_t = typename Compressor_t::Size_t; |
| 88 | using Char_t = typename Compressor_t::Char_t; |
| 89 | using String_t = std::basic_string<Char_t>; |
| 90 | using Stream_t = |
| 91 | binary_log::transaction::compression::Payload_event_buffer_istream; |
| 92 | using Buffer_ptr_t = Stream_t::Buffer_ptr_t; |
| 93 | static constexpr auto type_code = Compressor_t::type_code; |
| 94 | using Memory_resource_t = mysqlns::resource::Memory_resource; |
| 95 | |
| 96 | // Change to true to get more debug info |
| 97 | static constexpr bool m_trace = false; |
| 98 | |
| 99 | std::random_device::result_type m_seed; |
| 100 | static constexpr int m_event_timestamp = 4711; |
| 101 | std::mt19937_64 m_random_number_generator; |
| 102 | std::vector<Log_event_type> m_type_vector; |
| 103 | |
| 104 | PayloadEventBufferStreamTest() { |
| 105 | m_type_vector = {QUERY_EVENT, ROWS_QUERY_LOG_EVENT, TABLE_MAP_EVENT, |
| 106 | WRITE_ROWS_EVENT, XID_EVENT}; |
| 107 | /// Generate a random seed for the deterministic RNG. |
| 108 | m_seed = std::random_device()(); |
| 109 | m_random_number_generator.seed(m_seed); |
| 110 | } |
| 111 | |
| 112 | std::string mock_one_event(Size_t data_size, Log_event_type type) { |
| 113 | std::string ret(LOG_EVENT_HEADER_LEN + data_size, '\0'); |
| 114 | char *p = ret.data(); |
| 115 | // Store some of the necessary fields in the common-header |
| 116 | int4store(p, m_event_timestamp); |
| 117 | p[EVENT_TYPE_OFFSET] = type; |
| 118 | int4store(p + SERVER_ID_OFFSET, 1); |
| 119 | int4store(p + EVENT_LEN_OFFSET, |
| 120 | static_cast<uint32>(LOG_EVENT_HEADER_LEN + data_size)); |
| 121 | // Fill the rest with "deterministic, compressible garbage". We |
| 122 | // just repeat the type code. |
| 123 | std::memset(p + LOG_EVENT_HEADER_LEN, (int)type, data_size); |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | void check_one_event(const std::string &debug_string, |
| 128 | const std::string &buffer, Size_t expected_data_size, |
| 129 | Log_event_type expected_type) { |
| 130 | const auto *p = buffer.data(); |
| 131 | auto expected_event_size = LOG_EVENT_HEADER_LEN + expected_data_size; |
| 132 | ASSERT_EQ(buffer.size(), expected_event_size) << debug_string; |
| 133 | ASSERT_EQ(uint4korr(p), m_event_timestamp) << debug_string; |
| 134 | ASSERT_EQ(p[EVENT_TYPE_OFFSET], expected_type) << debug_string; |
| 135 | ASSERT_EQ(uint4korr(p + SERVER_ID_OFFSET), 1) << debug_string; |
| 136 | ASSERT_EQ(uint4korr(p + EVENT_LEN_OFFSET), expected_event_size) |
| 137 | << debug_string; |
| 138 | for (Size_t i = 0; i < expected_data_size; ++i) |
| 139 | ASSERT_EQ(p[LOG_EVENT_HEADER_LEN + i], (int)expected_type) |
| 140 | << debug_string; |
| 141 | } |
| 142 | |
| 143 | /// Create a given number of events, using the specified sizes and |
| 144 | /// the specified types. |
| 145 | std::string mock_multiple_events(std::vector<Size_t> size_vector, |
| 146 | std::vector<Log_event_type> type_vector) { |
| 147 | std::stringstream ss; |
| 148 | auto type_it = Cyclic_iterator(type_vector.begin(), type_vector.end()); |
| 149 | for (auto size : size_vector) { |
| 150 | ss << mock_one_event(size, *type_it); |
| 151 | ++type_it; |
| 152 | } |
| 153 | return ss.str(); |
| 154 | } |
| 155 | |
| 156 | std::string compress_event_buffer(std::string event_buffer) { |
| 157 | Compressor_t compressor; |
| 158 | compressor.feed(event_buffer.data(), event_buffer.size()); |
| 159 | Managed_Buffer_sequence_t managed_buffer_sequence; |
| 160 | EXPECT_EQ(compressor.finish(managed_buffer_sequence), |
| 161 | Compress_status_t::success); |
| 162 | return managed_buffer_sequence.read_part().str(); |
| 163 | } |
| 164 | |
| 165 | /// Construct a sequence of events, compress it, decompress it, and |
| 166 | /// verify that the result is as expected |
| 167 | void test_one_scenario(std::vector<Size_t> size_vector) { |
| 168 | std::string debug_string = std::string("seed=") + std::to_string(m_seed); |
| 169 | auto event_buffer = mock_multiple_events(size_vector, m_type_vector); |
| 170 | auto compressed_data = compress_event_buffer(event_buffer); |
| 171 | |
| 172 | Stream_t stream(compressed_data, type_code); |
| 173 | int event_count = 0; |
| 174 | Buffer_ptr_t buffer_ptr; |
| 175 | auto type_it = Cyclic_iterator(m_type_vector.begin(), m_type_vector.end()); |
| 176 | auto size_it = size_vector.begin(); |
| 177 | while (stream >> buffer_ptr) { |
| 178 | ASSERT_NE(size_it, size_vector.end()) << debug_string; |
| 179 | auto type = *type_it; |
| 180 | auto size = *size_it; |
| 181 | check_one_event( |
| 182 | concat(debug_string, " type_it=", |
| 183 | std::distance(&*m_type_vector.begin(), &*type_it), " size_it=", |
| 184 | std::to_string(std::distance(size_vector.begin(), size_it)), |
| 185 | " event_count=", event_count), |
| 186 | buffer_ptr->str(), size, type); |
| 187 | ASSERT_EQ(type, buffer_ptr->data()[EVENT_TYPE_OFFSET]) << debug_string; |
| 188 | ++event_count; |
| 189 | ++type_it; |
| 190 | ++size_it; |
| 191 | } |
| 192 | ASSERT_EQ(stream.get_status(), Decompress_status_t::end) |
| 193 | << debug_string << "Error: " << stream.get_error_str(); |
| 194 | ASSERT_EQ(stream.has_error(), false) |
| 195 | << debug_string << "Error: " << stream.get_error_str(); |
| 196 | } |
| 197 | |
| 198 | /// Generate a vector of random length, where each element is a |
| 199 | /// random integer. |
| 200 | /// |
| 201 | /// All random distributions are uniform. |
| 202 | /// |
| 203 | /// @param max_count Generate between 0 and this number of elements. |
| 204 | /// |
| 205 | /// @param max_value Generate an integer between 0 and this number |
| 206 | /// and store in each element. |
| 207 | /// |
| 208 | /// @return integer vector |
| 209 | template <class T> |
| 210 | std::vector<T> generate_random_vector(int max_count, T max_value) { |
| 211 | std::vector<T> ret; |
| 212 | std::uniform_int_distribution<int> count_distribution(1, max_count); |
| 213 | std::uniform_int_distribution<T> value_distribution(0, max_value); |
| 214 | auto count = count_distribution(m_random_number_generator); |
| 215 | std::generate_n(std::back_inserter(ret), count, [&] { |
| 216 | return value_distribution(m_random_number_generator); |
| 217 | }); |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | /// Requirement: |
| 222 | /// |
| 223 | /// - Compressing and then decompressing should give back the |
| 224 | /// original data. |
| 225 | void test_one_random_scenario(int max_event_count, Size_t max_event_size) { |
| 226 | auto size_vector = generate_random_vector(max_event_count, max_event_size); |
| 227 | test_one_scenario(size_vector); |
| 228 | } |
| 229 | |
| 230 | /// Requirement: |
| 231 | /// |
| 232 | /// - Compressing and then decompressing should give back the |
| 233 | /// original data. |
| 234 | /// |
| 235 | /// We check this by producing random data multiple times. |
| 236 | void test_multiple_random_scenarios(int scenario_count, int max_event_count, |
| 237 | Size_t max_event_size) { |
| 238 | for (int i = 0; i < scenario_count; ++i) |
| 239 | test_one_random_scenario(max_event_count, max_event_size); |
| 240 | } |
| 241 | |
| 242 | /// Requirement: |
| 243 | /// |
| 244 | /// - Payload_event_buffer_istream should fail gracefully and return |
| 245 | /// corrupted if it contains an event of type |
| 246 | /// TRANSACTION_PAYLOAD_EVENT. |
| 247 | void test_payload_in_payload() { |
| 248 | auto event_buffer = mock_multiple_events( |
| 249 | {10, 20}, {QUERY_EVENT, TRANSACTION_PAYLOAD_EVENT}); |
| 250 | auto compressed_data = compress_event_buffer(event_buffer); |
| 251 | Stream_t stream(compressed_data, type_code); |
| 252 | Buffer_ptr_t buffer_ptr; |
| 253 | stream >> buffer_ptr; |
| 254 | ASSERT_TRUE((bool)stream); |
| 255 | check_one_event("payload_in_payload", buffer_ptr->str(), 10, QUERY_EVENT); |
| 256 | stream >> buffer_ptr; |
| 257 | ASSERT_FALSE((bool)stream); |
| 258 | ASSERT_EQ(stream.get_status(), Decompress_status_t::corrupted); |
| 259 | } |
| 260 | |
| 261 | /// Requirement: |
| 262 | /// |
| 263 | /// - Payload_event_buffer_istream should fail gracefully and return |
| 264 | /// corrupted if the uncompressed data ends in the middle of an |
| 265 | /// event. |
| 266 | void test_truncated_uncompressed_data() { |
| 267 | auto event_buffer = |
| 268 | mock_multiple_events({10, 20}, {QUERY_EVENT, QUERY_EVENT}); |
| 269 | // Test all ways to truncate the second event |
| 270 | for (Size_t i = LOG_EVENT_HEADER_LEN + 10 + 1; i < event_buffer.size(); |
| 271 | ++i) { |
| 272 | std::string debug_string = |
| 273 | std::string("truncated_uncompressed i=") + std::to_string(i); |
| 274 | auto compressed_data = |
| 275 | compress_event_buffer(std::string(event_buffer.data(), i)); |
| 276 | Stream_t stream(compressed_data, type_code); |
| 277 | Buffer_ptr_t buffer_ptr; |
| 278 | |
| 279 | stream >> buffer_ptr; |
| 280 | ASSERT_TRUE((bool)stream) << debug_string; |
| 281 | ASSERT_EQ(stream.get_status(), Decompress_status_t::success) |
| 282 | << debug_string; |
| 283 | check_one_event(debug_string, buffer_ptr->str(), 10, QUERY_EVENT); |
| 284 | |
| 285 | stream >> buffer_ptr; |
| 286 | ASSERT_FALSE((bool)stream) << debug_string; |
| 287 | ASSERT_EQ(stream.get_status(), Decompress_status_t::corrupted) |
| 288 | << debug_string; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /// Requirement: |
| 293 | /// |
| 294 | /// - Payload_event_buffer_istream should fail gracefully and return |
| 295 | /// corrupted if the compressed stream is truncated. |
| 296 | void test_truncated_compressed_data() { |
| 297 | // Just one event: if we use multiple events, it's hard to predict |
| 298 | // which event will observe the error. |
| 299 | auto event_buffer = mock_one_event(100, QUERY_EVENT); |
| 300 | auto compressed_data = compress_event_buffer(event_buffer); |
| 301 | // Test all ways to truncate the event |
| 302 | for (Size_t i = 1; i < compressed_data.size(); ++i) { |
| 303 | Stream_t stream(compressed_data.data(), i, type_code); |
| 304 | Buffer_ptr_t buffer_ptr; |
| 305 | stream >> buffer_ptr; |
| 306 | EXPECT_FALSE((bool)stream) << i; |
| 307 | EXPECT_EQ(stream.get_status(), Decompress_status_t::corrupted) << i; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /// Requirement: |
| 312 | /// |
| 313 | /// - Payload_event_buffer_istream should fail gracefully and return |
| 314 | /// exceeds_max_capacity in case the output is bigger than the |
| 315 | /// configured max size. |
| 316 | void test_exceeds_max_size() { |
| 317 | auto event_buffer = mock_multiple_events({10, 100, 10}, {QUERY_EVENT}); |
| 318 | auto compressed_data = compress_event_buffer(event_buffer); |
| 319 | Stream_t stream(compressed_data, type_code); |
| 320 | { |
| 321 | auto gc = stream.get_grow_calculator(); |
| 322 | gc.set_max_size(99); |
| 323 | stream.set_grow_calculator(gc); |
| 324 | } |
| 325 | auto debug_func = [&] { |
| 326 | return std::string("max_size=") + |
| 327 | std::to_string(stream.get_grow_calculator().get_max_size()); |
| 328 | }; |
| 329 | Buffer_ptr_t buffer_ptr; |
| 330 | // First event is within max size |
| 331 | stream >> buffer_ptr; |
| 332 | ASSERT_TRUE((bool)stream) << debug_func(); |
| 333 | check_one_event(debug_func(), buffer_ptr->str(), 10, QUERY_EVENT); |
| 334 | // Second event exceeds max size |
| 335 | stream >> buffer_ptr; |
| 336 | ASSERT_FALSE((bool)stream) << debug_func(); |
| 337 | ASSERT_EQ(stream.get_status(), Decompress_status_t::exceeds_max_size) |
| 338 | << debug_func(); |
| 339 | // Once we have failed, any subsequent read from the stream will |
| 340 | // fail the same way, even if the max size has increased. |
| 341 | { |
| 342 | auto gc = stream.get_grow_calculator(); |
| 343 | gc.set_max_size(10000); |
| 344 | stream.set_grow_calculator(gc); |
| 345 | } |
| 346 | stream >> buffer_ptr; |
| 347 | ASSERT_FALSE((bool)stream) << debug_func(); |
| 348 | ASSERT_EQ(stream.get_status(), Decompress_status_t::exceeds_max_size) |
| 349 | << debug_func(); |
| 350 | } |
| 351 | |
| 352 | /// Requirement: |
| 353 | /// |
| 354 | /// - Payload_event_buffer_istream should fail gracefully and return |
| 355 | /// out_of_memory in case allocation fails. |
| 356 | void test_allocation_failure() { |
| 357 | // Allocator that fails after `success_count_before_failure` |
| 358 | // successful allocations. |
| 359 | int allocation_failure_number = 0; |
| 360 | int allocation_number = 0; |
| 361 | auto failing_allocator = [&](Size_t n) -> Memory_resource_t::Ptr_t { |
| 362 | ++allocation_number; |
| 363 | if (allocation_number == allocation_failure_number) return nullptr; |
| 364 | // "nolint": as a general rule, malloc should not be used, so |
| 365 | // clang-tidy warns about it. But this is an allocator so it is |
| 366 | // appropriate to use malloc and therefore we suppress the check. |
| 367 | // NOLINTNEXTLINE(cppcoreguidelines-no-malloc) |
| 368 | return std::malloc(n); |
| 369 | }; |
| 370 | Memory_resource_t failing_memory_resource(failing_allocator, std::free); |
| 371 | auto debug_func = [&] { |
| 372 | return std::string("allocation_failure_number=") + |
| 373 | std::to_string(allocation_failure_number) + |
| 374 | " allocation_number=" + std::to_string(allocation_number); |
| 375 | }; |
| 376 | |
| 377 | std::vector<Size_t> size_vector({10, 500, 500, 500, 100000}); |
| 378 | auto event_buffer = mock_multiple_events(size_vector, {QUERY_EVENT}); |
| 379 | auto compressed_data = compress_event_buffer(event_buffer); |
| 380 | |
| 381 | // Repeat the test incrementing `allocation_failure_number` in |
| 382 | // each iteration. This forces the allocation_failure_number'th |
| 383 | // lower-level allocation to fail. |
| 384 | while (true) { |
| 385 | Stream_t stream(compressed_data, type_code, 0, failing_memory_resource); |
| 386 | Buffer_ptr_t buffer_ptr; |
| 387 | auto size_it = size_vector.begin(); |
| 388 | int event_count = 0; |
| 389 | ++allocation_failure_number; |
| 390 | allocation_number = 0; |
| 391 | while (stream >> buffer_ptr) { |
| 392 | ASSERT_TRUE((bool)stream) << debug_func(); |
| 393 | ASSERT_EQ(stream.get_status(), Decompress_status_t::success) |
| 394 | << debug_func(); |
| 395 | check_one_event(debug_func(), buffer_ptr->str(), *size_it, QUERY_EVENT); |
| 396 | ++size_it; |
| 397 | ++event_count; |
| 398 | } |
| 399 | ASSERT_FALSE((bool)stream) << debug_func(); |
| 400 | // Eventually allocation_failure_number is so big that the |
| 401 | // lower-level functions don't observe any error. Then we are |
| 402 | // done with the test. |
| 403 | if (stream.get_status() == Decompress_status_t::end) { |
| 404 | ASSERT_EQ(size_it, size_vector.end()) << debug_func(); |
| 405 | if (m_trace) |
| 406 | std::cout << "Got all " << event_count << " events with less than " |
| 407 | << allocation_failure_number << " allocations.\n"; |
| 408 | return; |
| 409 | } |
| 410 | // If there is an error, it should be out-of-memory. |
| 411 | ASSERT_EQ(stream.get_status(), Decompress_status_t::out_of_memory) |
| 412 | << debug_func(); |
| 413 | if (m_trace) |
| 414 | std::cout << "Got " << event_count << " events before allocation " |
| 415 | << "number " << allocation_failure_number << " failed.\n"; |
| 416 | // Retrying doesn't help |
| 417 | stream >> buffer_ptr; |
| 418 | ASSERT_FALSE((bool)stream) << debug_func(); |
| 419 | ASSERT_EQ(stream.get_status(), Decompress_status_t::out_of_memory) |
| 420 | << debug_func(); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | /// Requirement: |
| 425 | /// |
| 426 | /// - Payload_event_buffer_istream should return `corrupted` if the |
| 427 | /// input is not valid ZSTD-compressor output. |
| 428 | void test_corrupted_compressed_data() { |
| 429 | // By chance, "Hello world!" is not valid zstd compressor output. |
| 430 | std::string compressed_data = "Hello world!"; |
| 431 | Stream_t stream(compressed_data, type_code); |
| 432 | Buffer_ptr_t buffer_ptr; |
| 433 | stream >> buffer_ptr; |
| 434 | EXPECT_FALSE((bool)stream); |
| 435 | EXPECT_EQ(stream.get_status(), Decompress_status_t::corrupted); |
| 436 | } |
| 437 | |
| 438 | /// Requirement: |
| 439 | /// |
| 440 | /// - Payload_event_buffer_istream should work transparently even if |
| 441 | /// there are compression frame boundaries in the middle of the |
| 442 | /// compressed data. |
| 443 | void test_frame_boundaries() { |
| 444 | std::vector<Size_t> size_vector({5, 0, 5}); |
| 445 | auto event_buffer = mock_multiple_events(size_vector, {QUERY_EVENT}); |
| 446 | for (Size_t f1 = 0; f1 <= event_buffer.size(); ++f1) { |
| 447 | auto frame_1 = |
| 448 | compress_event_buffer(std::string(event_buffer.data(), f1)); |
| 449 | for (Size_t f2 = f1; f2 <= event_buffer.size(); ++f2) { |
| 450 | auto debug_func = [&] { |
| 451 | return std::string("f1=") + std::to_string(f1) + std::string(" f2=") + |
| 452 | std::to_string(f2); |
| 453 | }; |
| 454 | auto frame_2 = compress_event_buffer( |
| 455 | std::string(event_buffer.data() + f1, f2 - f1)); |
| 456 | auto frame_3 = compress_event_buffer( |
| 457 | std::string(event_buffer.data() + f2, event_buffer.size() - f2)); |
| 458 | auto compressed_data = frame_1; |
| 459 | compressed_data += frame_2; |
| 460 | compressed_data += frame_3; |
| 461 | Stream_t stream(compressed_data, type_code); |
| 462 | Buffer_ptr_t buffer_ptr; |
| 463 | stream >> buffer_ptr; |
| 464 | EXPECT_TRUE((bool)stream) << debug_func(); |
| 465 | EXPECT_EQ(stream.get_status(), Decompress_status_t::success) |
| 466 | << debug_func(); |
| 467 | stream >> buffer_ptr; |
| 468 | EXPECT_TRUE((bool)stream) << debug_func(); |
| 469 | EXPECT_EQ(stream.get_status(), Decompress_status_t::success) |
| 470 | << debug_func(); |
| 471 | stream >> buffer_ptr; |
| 472 | EXPECT_TRUE((bool)stream) << debug_func(); |
| 473 | EXPECT_EQ(stream.get_status(), Decompress_status_t::success) |
| 474 | << debug_func(); |
| 475 | stream >> buffer_ptr; |
| 476 | EXPECT_FALSE((bool)stream) << debug_func(); |
| 477 | EXPECT_EQ(stream.get_status(), Decompress_status_t::end) |
| 478 | << debug_func(); |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | void test_api_assertions() { |
| 484 | EXPECT_DEBUG_DEATH( |
| 485 | { |
| 486 | // Stream destructor should raise assertion if stream is in |
| 487 | // an error state, but caller has not checked if the reason |
| 488 | // that the stream ended is EOF or error. |
| 489 | std::string compressed_data = "Hello world!"; |
| 490 | Stream_t stream(compressed_data, type_code); |
| 491 | Buffer_ptr_t buffer_ptr; |
| 492 | stream >> buffer_ptr; |
| 493 | EXPECT_FALSE((bool)stream); |
| 494 | EXPECT_TRUE(!stream); |
| 495 | }, |
| 496 | "!m_outstanding_error"); |
| 497 | EXPECT_DEBUG_DEATH( |
| 498 | { |
| 499 | // Stream destructor should raise assertion if stream has |
| 500 | // reached EOF, but caller has not checked if the reason |
| 501 | // that the stream ended is EOF or error. |
| 502 | auto event_buffer = mock_one_event(100, QUERY_EVENT); |
| 503 | auto compressed_data = compress_event_buffer(event_buffer); |
| 504 | Stream_t stream(compressed_data, type_code); |
| 505 | Buffer_ptr_t buffer_ptr; |
| 506 | stream >> buffer_ptr; |
| 507 | EXPECT_TRUE((bool)stream); |
| 508 | stream >> buffer_ptr; |
| 509 | EXPECT_FALSE((bool)stream); |
| 510 | EXPECT_TRUE(!stream); |
| 511 | }, |
| 512 | "!m_outstanding_error"); |
| 513 | // Non-assertion cases |
| 514 | auto event_buffer = mock_one_event(100, QUERY_EVENT); |
| 515 | auto compressed_data = compress_event_buffer(event_buffer); |
| 516 | { |
| 517 | Stream_t stream(compressed_data, type_code); |
| 518 | // No assertion in the initial state |
| 519 | } |
| 520 | { |
| 521 | Stream_t stream(compressed_data, type_code); |
| 522 | Buffer_ptr_t buffer_ptr; |
| 523 | stream >> buffer_ptr; |
| 524 | // No assertion if no error occurred |
| 525 | } |
| 526 | { |
| 527 | Stream_t stream(compressed_data, type_code); |
| 528 | Buffer_ptr_t buffer_ptr; |
| 529 | stream >> buffer_ptr; |
| 530 | stream >> buffer_ptr; |
| 531 | stream.has_error(); |
| 532 | // No assertion if error was checked |
| 533 | } |
| 534 | { |
| 535 | Stream_t stream(compressed_data, type_code); |
| 536 | Buffer_ptr_t buffer_ptr; |
| 537 | stream >> buffer_ptr; |
| 538 | stream >> buffer_ptr; |
| 539 | stream.get_error_str(); |
| 540 | // No assertion if error was checked |
| 541 | } |
| 542 | { |
| 543 | Stream_t stream(compressed_data, type_code); |
| 544 | Buffer_ptr_t buffer_ptr; |
| 545 | stream >> buffer_ptr; |
| 546 | stream >> buffer_ptr; |
| 547 | stream.get_status(); |
| 548 | // No assertion if status was checked |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | void test_error_cases() { |
| 553 | test_payload_in_payload(); |
| 554 | test_truncated_uncompressed_data(); |
| 555 | test_truncated_compressed_data(); |
| 556 | test_exceeds_max_size(); |
| 557 | test_allocation_failure(); |
| 558 | if (type_code != binary_log::transaction::compression::NONE) |
| 559 | // "NONE" can't be corrupted |
| 560 | test_corrupted_compressed_data(); |
| 561 | test_frame_boundaries(); |
| 562 | test_api_assertions(); |
| 563 | } |
| 564 | }; |
| 565 | |
| 566 | TEST(PayloadEventBufferStreamTest, NoneCompressDecompressTest) { |
| 567 | // 50 trials, each time creating up to 20 events, each of size up to 65536. |
| 568 | PayloadEventBufferStreamTest< |
| 569 | binary_log::transaction::compression::None_comp>() |
| 570 | .test_multiple_random_scenarios(50, 20, 65536); |
| 571 | } |
| 572 | |
| 573 | TEST(PayloadEventBufferStreamTest, ZstdCompressDecompressTest) { |
| 574 | // 50 trials, each time creating up to 20 events, each of size up to 65536. |
| 575 | PayloadEventBufferStreamTest< |
| 576 | binary_log::transaction::compression::Zstd_comp>() |
| 577 | .test_multiple_random_scenarios(50, 20, 65536); |
| 578 | } |
| 579 | |
| 580 | TEST(PayloadEventBufferStreamTest, NoneDecompressErrorTest) { |
| 581 | PayloadEventBufferStreamTest< |
| 582 | binary_log::transaction::compression::None_comp>() |
| 583 | .test_error_cases(); |
| 584 | } |
| 585 | |
| 586 | TEST(PayloadEventBufferStreamTest, ZstdDecompressErrorTest) { |
| 587 | PayloadEventBufferStreamTest< |
| 588 | binary_log::transaction::compression::Zstd_comp>() |
| 589 | .test_error_cases(); |
| 590 | } |
| 591 | |
| 592 | } // namespace unittests |
| 593 | } // namespace transaction |
| 594 | } // namespace binary_log |