| 1 | /* Copyright (c) 2016, 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 <ctime> |
| 25 | |
| 26 | #include "gcs_base_test.h" |
| 27 | |
| 28 | #include "gcs_internal_message.h" |
| 29 | #include "gcs_message_stage_lz4.h" |
| 30 | #include "gcs_message_stages.h" |
| 31 | #include "gcs_xcom_statistics_interface.h" |
| 32 | #include "mysql/gcs/gcs_message.h" |
| 33 | |
| 34 | namespace gcs_xcom_packet_unittest { |
| 35 | |
| 36 | class GcsPacketTest : public GcsBaseTest { |
| 37 | protected: |
| 38 | GcsPacketTest() = default; |
| 39 | |
| 40 | void SetUp() override { lz4_stage = new Gcs_message_stage_lz4(true, 1024); } |
| 41 | |
| 42 | void TearDown() override { delete lz4_stage; } |
| 43 | |
| 44 | Gcs_message_stage_lz4 *lz4_stage; |
| 45 | |
| 46 | public: |
| 47 | static const unsigned long long LARGE_PAYLOAD_LEN; |
| 48 | static const unsigned long long SMALL_PAYLOAD_LEN; |
| 49 | }; |
| 50 | |
| 51 | const unsigned long long GcsPacketTest::LARGE_PAYLOAD_LEN = |
| 52 | 1024 + Gcs_internal_message_header::WIRE_TOTAL_FIXED_HEADER_SIZE; |
| 53 | const unsigned long long GcsPacketTest::SMALL_PAYLOAD_LEN = |
| 54 | 1024 - Gcs_internal_message_header::WIRE_TOTAL_FIXED_HEADER_SIZE; |
| 55 | |
| 56 | TEST_F(GcsPacketTest, PacketInit) { |
| 57 | const char content[] = "OLA123"; |
| 58 | unsigned int content_len = sizeof(content); |
| 59 | |
| 60 | /* |
| 61 | Simulate a message that was prepared by an upper layer such as group |
| 62 | replication. |
| 63 | */ |
| 64 | Gcs_member_identifier origin(std::string("luis")); |
| 65 | Gcs_message msg(origin, new Gcs_message_data(0, content_len)); |
| 66 | Cargo_type cargo = Cargo_type::CT_INTERNAL_STATE_EXCHANGE; |
| 67 | msg.get_message_data().append_to_payload((const unsigned char *)content, |
| 68 | content_len); |
| 69 | |
| 70 | /* |
| 71 | Create an internal gcs message that will be eventually delivered to |
| 72 | the group communication layer. |
| 73 | */ |
| 74 | Gcs_message_data &msg_data = msg.get_message_data(); |
| 75 | unsigned long long payload_length = msg_data.get_encode_size(); |
| 76 | |
| 77 | bool packet_ok; |
| 78 | Gcs_packet p; |
| 79 | std::tie(packet_ok, p) = Gcs_packet::make_outgoing_packet( |
| 80 | cargo, Gcs_protocol_version::V1, {}, {}, payload_length); |
| 81 | ASSERT_TRUE(packet_ok); |
| 82 | |
| 83 | /* |
| 84 | Encode the payload encapsulated in the group replication message into |
| 85 | the gcs message. |
| 86 | */ |
| 87 | uint64_t buffer_size = p.get_payload_length(); |
| 88 | ASSERT_FALSE(msg_data.encode(p.get_payload_pointer(), &buffer_size)); |
| 89 | |
| 90 | ASSERT_EQ(p.get_payload_length(), payload_length); |
| 91 | ASSERT_EQ(p.get_total_length(), |
| 92 | payload_length + |
| 93 | Gcs_internal_message_header::WIRE_TOTAL_FIXED_HEADER_SIZE); |
| 94 | |
| 95 | /* |
| 96 | Decode the payload from the gcs message into the group replication |
| 97 | message. |
| 98 | */ |
| 99 | Gcs_message_data msg_decoded(p.get_payload_length()); |
| 100 | msg_decoded.decode(p.get_payload_pointer(), p.get_payload_length()); |
| 101 | |
| 102 | ASSERT_TRUE(strncmp((const char *)msg_decoded.get_payload(), |
| 103 | (const char *)content, content_len) == 0); |
| 104 | } |
| 105 | |
| 106 | } // namespace gcs_xcom_packet_unittest |