| 1 | /* Copyright (c) 2018, 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 "gcs_base_test.h" |
| 25 | |
| 26 | #include <cstring> |
| 27 | #include <string> |
| 28 | #include <vector> |
| 29 | #include "app_data.h" |
| 30 | #include "get_synode_app_data.h" |
| 31 | #include "pax_msg.h" |
| 32 | #include "xcom_cache.h" |
| 33 | #include "xcom_memory.h" |
| 34 | #include "xcom_transport.h" |
| 35 | |
| 36 | namespace xcom_transport_unittest { |
| 37 | |
| 38 | class XcomTransport : public GcsBaseTest { |
| 39 | protected: |
| 40 | XcomTransport() = default; |
| 41 | ~XcomTransport() override = default; |
| 42 | }; |
| 43 | |
| 44 | TEST_F(XcomTransport, SerializeTooManySynodes) { |
| 45 | u_int constexpr nr_synodes = MAX_SYNODE_ARRAY + 1; |
| 46 | |
| 47 | app_data_ptr a = new_app_data(); |
| 48 | a->body.c_t = get_synode_app_data_type; |
| 49 | a->body.app_u_u.synodes.synode_no_array_len = nr_synodes; |
| 50 | a->body.app_u_u.synodes.synode_no_array_val = |
| 51 | static_cast<synode_no *>(std::calloc(nr_synodes, sizeof(synode_no))); |
| 52 | |
| 53 | pax_msg *p = pax_msg_new(null_synode, nullptr); |
| 54 | p->a = a; |
| 55 | p->to = VOID_NODE_NO; |
| 56 | p->op = client_msg; |
| 57 | |
| 58 | uint32_t buflen = 0; |
| 59 | char *buf = nullptr; |
| 60 | ASSERT_EQ(serialize_msg(p, x_1_6, &buflen, &buf), 0); |
| 61 | |
| 62 | p->refcnt = 1; |
| 63 | unchecked_replace_pax_msg(&p, nullptr); |
| 64 | |
| 65 | std::free(buf); |
| 66 | } |
| 67 | |
| 68 | TEST_F(XcomTransport, is_new_node_eligible_for_ipv6) { |
| 69 | char const *invalid_address = "127.0.0.257:123456"; |
| 70 | node_address node{const_cast<char *>(invalid_address), |
| 71 | {{0, nullptr}}, |
| 72 | {x_1_0, x_1_0}, |
| 73 | P_PROP | P_ACC | P_LEARN}; |
| 74 | site_def site{null_synode, |
| 75 | null_synode, |
| 76 | VOID_NODE_NO, |
| 77 | {1, &node}, |
| 78 | {nullptr}, |
| 79 | {0}, |
| 80 | 0, |
| 81 | {0, nullptr}, |
| 82 | {0, nullptr}, |
| 83 | 0, |
| 84 | x_1_0, |
| 85 | {null_synode}, |
| 86 | 0.0, |
| 87 | EVENT_HORIZON_MIN, |
| 88 | 0, |
| 89 | {0, nullptr}, |
| 90 | nullptr}; |
| 91 | /* Any protocol version besides x_1_6 is okay as long as it is older than |
| 92 | MY_XCOM_PROTO */ |
| 93 | ASSERT_EQ(1, is_new_node_eligible_for_ipv6(x_1_6, &site)); |
| 94 | } |
| 95 | |
| 96 | TEST_F(XcomTransport, get_ip_and_port) { |
| 97 | /* Has 512 characters + \0. */ |
| 98 | char const *hostname_that_is_too_big = |
| 99 | "uDmNoeWItHSKUkullwFTkTYclXzEAZwcOKvezkHTxCaoCBkrrMFJfARWdmnpvVHSokbOKcHf" |
| 100 | "TKZqkZFysAFvTMoGsqBMkTUvcSFFosMSeQqYCtqOtOtCNxMVAonZlFosAxIFWzATRzIUAKGQ" |
| 101 | "WFEHEJDkWqJYTSOBGLIUJTqrxDbCOGYPSiCymVxeZPmuXHCpcFHzEiGfsHxHffvuDPyMIgfp" |
| 102 | "YfSFRhylIYwTrafXooTigiDdNhVkMrtJRmNGUCPHFMCBXxhioyEydKNZhVUROJmYrqQMQZaC" |
| 103 | "iueRmJKatxHiiWYqshHxNiHHShxRURWiymUXRIPMOHOBUhXjqfJIyqtygobpDmVGbAqynnRR" |
| 104 | "ukByXEegTTFfyHsvKiFJixFttmxHrxKZblGmkPhcUHzPVJcpzmWPXiPtatPxVTmOioqvmAom" |
| 105 | "cFUQEufzYBrxVneufgdJOlvlPaBgiyPlAzmXDzwYyxXujyKATWBjiGWatqiYCgiSGWkcIoAS" |
| 106 | "uYsTnWeR"; |
| 107 | char ip[IP_MAX_SIZE]; |
| 108 | xcom_port port = 0; |
| 109 | ASSERT_EQ(1, get_ip_and_port(const_cast<char *>(hostname_that_is_too_big), ip, |
| 110 | &port)); |
| 111 | |
| 112 | char const *malformed_ipv6_1 = "[ ]"; |
| 113 | ASSERT_EQ(1, |
| 114 | get_ip_and_port(const_cast<char *>(malformed_ipv6_1), ip, &port)); |
| 115 | |
| 116 | char const *malformed_ipv6_2 = "[::::::::]"; |
| 117 | ASSERT_EQ(1, |
| 118 | get_ip_and_port(const_cast<char *>(malformed_ipv6_2), ip, &port)); |
| 119 | } |
| 120 | |
| 121 | /* Validate that we can correctly deserialize a `gcs_snapshot_op` message |
| 122 | serialized by the XCom implementation that only knows up to protocol x_1_6. |
| 123 | |
| 124 | Note that this test embeds the byte array `buf` of the serialized message |
| 125 | directly into the test source code. Nevertheless the source contains the code |
| 126 | that was used to generate the embedded array commented out. |
| 127 | */ |
| 128 | TEST_F(XcomTransport, GcsSnapshotOpCrossVersionSerialization) { |
| 129 | char const b_str[] = "uuid"; |
| 130 | // blob b; |
| 131 | // b.data.data_len = sizeof b_str / sizeof b_str[0]; |
| 132 | // b.data.data_val = b_str; |
| 133 | // |
| 134 | // x_proto_range xpr; |
| 135 | // xpr.min_proto = x_1_0; |
| 136 | // xpr.max_proto = x_1_6; |
| 137 | // |
| 138 | // node_address na; |
| 139 | char const na_str[] = "127.0.0.1:12345"; |
| 140 | // na.address = na_str; |
| 141 | // na.uuid = b; |
| 142 | // na.proto = xpr; |
| 143 | // |
| 144 | // node_list nl; |
| 145 | // nl.node_list_len = 1; |
| 146 | // nl.node_list_val = &na; |
| 147 | // |
| 148 | synode_no const start = {2, 2, 2}; |
| 149 | synode_no const boot_key = {1, 1, 1}; |
| 150 | xcom_event_horizon const event_horizon = 42; |
| 151 | // config c; |
| 152 | // c.start = start; |
| 153 | // c.boot_key = boot_key; |
| 154 | // c.nodes = nl; |
| 155 | // c.event_horizon = event_horizon; |
| 156 | |
| 157 | // config_ptr cs_val[] = {&c}; |
| 158 | // configs cs; |
| 159 | // cs.configs_len = sizeof cs_val / sizeof cs_val[0]; |
| 160 | // cs.configs_val = cs_val; |
| 161 | // |
| 162 | char const as_str[] = "app_snap"; |
| 163 | // blob as; |
| 164 | // as.data.data_len = sizeof as_str / sizeof as_str[0]; |
| 165 | // as.data.data_val = as_str; |
| 166 | // |
| 167 | synode_no const log_start = {3, 3, 3}; |
| 168 | // gcs_snapshot gs; |
| 169 | // gs.log_start = log_start; |
| 170 | // gs.cfg = cs; |
| 171 | // gs.app_snap = as; |
| 172 | |
| 173 | // pax_msg *p = pax_msg_new_0(null_synode); |
| 174 | // p->op = gcs_snapshot_op; |
| 175 | // p->gcs_snap = &gs; |
| 176 | |
| 177 | // uint32_t buflen = 0; |
| 178 | // unsigned char *buf = 0; |
| 179 | // int ok = serialize_msg(p, x_1_6, &buflen, (char **)&buf); |
| 180 | // ASSERT_TRUE(ok); |
| 181 | |
| 182 | // std::printf("buflen=%" PRIu32 "\n", buflen); |
| 183 | // std::printf("begin\n"); |
| 184 | // for (uint32_t i = 0; i < buflen; i++) { |
| 185 | // std::printf(" 0x%hhx,", *(buf + i)); |
| 186 | // } |
| 187 | // std::printf("\nend\n"); |
| 188 | |
| 189 | unsigned char buf[] = { |
| 190 | 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0xf4, 0x0, 0x0, 0x0, 0x0, |
| 191 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0, |
| 192 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 193 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 194 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 195 | 0x0, 0x0, 0x0, 0x15, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 196 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 197 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 198 | 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, |
| 199 | 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x1, |
| 200 | 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, |
| 201 | 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x1, |
| 202 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, |
| 203 | 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0xf, 0x31, 0x32, 0x37, 0x2e, |
| 204 | 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x3a, 0x31, 0x32, 0x33, 0x34, 0x35, 0x0, |
| 205 | 0x0, 0x0, 0x0, 0x5, 0x75, 0x75, 0x69, 0x64, 0x0, 0x0, 0x0, 0x0, |
| 206 | 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0x2a, |
| 207 | 0x0, 0x0, 0x0, 0x9, 0x61, 0x70, 0x70, 0x5f, 0x73, 0x6e, 0x61, 0x70, |
| 208 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 209 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 210 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa, |
| 211 | 0x0, 0x0, 0x0, 0x0}; |
| 212 | uint32_t buflen = 256; |
| 213 | |
| 214 | xcom_proto proto = read_protoversion(VERS_PTR(buf)); |
| 215 | ASSERT_EQ(proto, x_1_6); |
| 216 | uint32_t msgsize = 0; |
| 217 | x_msg_type x_type = x_normal; |
| 218 | unsigned int tag = 0; |
| 219 | get_header_1_0(buf, &msgsize, &x_type, &tag); |
| 220 | ASSERT_EQ(msgsize + MSG_HDR_SIZE, buflen); |
| 221 | ASSERT_EQ(x_type, x_normal); |
| 222 | |
| 223 | pax_msg *p_received = pax_msg_new_0(null_synode); |
| 224 | int deserialize_ok = |
| 225 | deserialize_msg(p_received, x_1_6, (char *)buf + MSG_HDR_SIZE, msgsize); |
| 226 | ASSERT_TRUE(deserialize_ok); |
| 227 | |
| 228 | ASSERT_EQ(p_received->op, gcs_snapshot_op); |
| 229 | ASSERT_TRUE(synode_eq(p_received->gcs_snap->log_start, log_start)); |
| 230 | ASSERT_EQ(p_received->gcs_snap->cfg.configs_len, 1); |
| 231 | ASSERT_TRUE( |
| 232 | synode_eq(p_received->gcs_snap->cfg.configs_val[0]->start, start)); |
| 233 | ASSERT_TRUE( |
| 234 | synode_eq(p_received->gcs_snap->cfg.configs_val[0]->boot_key, boot_key)); |
| 235 | ASSERT_EQ(p_received->gcs_snap->cfg.configs_val[0]->nodes.node_list_len, 1); |
| 236 | ASSERT_EQ(strcmp(p_received->gcs_snap->cfg.configs_val[0] |
| 237 | ->nodes.node_list_val[0] |
| 238 | .address, |
| 239 | na_str), |
| 240 | 0); |
| 241 | ASSERT_EQ(strcmp(p_received->gcs_snap->cfg.configs_val[0] |
| 242 | ->nodes.node_list_val[0] |
| 243 | .uuid.data.data_val, |
| 244 | b_str), |
| 245 | 0); |
| 246 | ASSERT_EQ(p_received->gcs_snap->cfg.configs_val[0] |
| 247 | ->nodes.node_list_val[0] |
| 248 | .proto.min_proto, |
| 249 | x_1_0); |
| 250 | ASSERT_EQ(p_received->gcs_snap->cfg.configs_val[0] |
| 251 | ->nodes.node_list_val[0] |
| 252 | .proto.max_proto, |
| 253 | x_1_6); |
| 254 | ASSERT_EQ(p_received->gcs_snap->cfg.configs_val[0]->event_horizon, |
| 255 | event_horizon); |
| 256 | ASSERT_EQ(strcmp(p_received->gcs_snap->app_snap.data.data_val, as_str), 0); |
| 257 | |
| 258 | delete_pax_msg(p_received); |
| 259 | } |
| 260 | } // namespace xcom_transport_unittest |