| 1 | /* |
| 2 | * Copyright (c) 2017, 2026, Oracle and/or its affiliates. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License, version 2.0, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is designed to work with certain software (including |
| 9 | * but not limited to OpenSSL) that is licensed under separate terms, |
| 10 | * as designated in a particular file or component or in included license |
| 11 | * documentation. The authors of MySQL hereby grant you an additional |
| 12 | * permission to link the program and your derivative works with the |
| 13 | * separately licensed software that they have either included with |
| 14 | * the program or referenced in the documentation. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License, version 2.0, for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | #include <string> |
| 27 | |
| 28 | #include "unittest/gunit/xplugin/xcl/message_helpers.h" |
| 29 | #include "unittest/gunit/xplugin/xcl/protocol_t.h" |
| 30 | |
| 31 | namespace xcl { |
| 32 | namespace test { |
| 33 | |
| 34 | struct Auth_details { |
| 35 | using Authenticate_start = ::Mysqlx::Session::AuthenticateStart; |
| 36 | using Authenticate_continue = ::Mysqlx::Session::AuthenticateContinue; |
| 37 | using Authenticate_ok = ::Mysqlx::Session::AuthenticateOk; |
| 38 | |
| 39 | std::string m_auth_name; |
| 40 | Message_from_str<::Mysqlx::Session::AuthenticateStart> m_start_message; |
| 41 | Message_from_str<::Mysqlx::Session::AuthenticateContinue> m_continue_message; |
| 42 | }; |
| 43 | |
| 44 | struct Auth_test_messages { |
| 45 | const std::string expected_user = "user"; |
| 46 | const std::string expected_pass = "pass"; |
| 47 | const std::string expected_schema = "schema"; |
| 48 | |
| 49 | const Message_from_str<Auth_details::Authenticate_start> msg_auth_start_plain{ |
| 50 | "mech_name: \"PLAIN\" " |
| 51 | "auth_data: \"schema\\0user\\0pass\" "}; |
| 52 | |
| 53 | const Message_from_str<Auth_details::Authenticate_start> |
| 54 | msg_auth_start_mysql41{"mech_name: \"MYSQL41\" "}; |
| 55 | |
| 56 | const Message_from_str<Auth_details::Authenticate_start> |
| 57 | msg_auth_start_sha256_memory{"mech_name: \"SHA256_MEMORY\" "}; |
| 58 | |
| 59 | const Message_from_str<Auth_details::Authenticate_continue> |
| 60 | msg_auth_cont_mysql41{ |
| 61 | "auth_data: \"schema\\0user\\0" |
| 62 | "*ACFC0C3FA7F3C1F39849B44177D8B82C7F75E0D1\""}; |
| 63 | |
| 64 | const Message_from_str<Auth_details::Authenticate_continue> |
| 65 | msg_auth_cont_sha256_memory{ |
| 66 | "auth_data: \"schema\\0user\\0" |
| 67 | "8F7B8968AA5212A7FD28DAC463913BE1444D61BE7C891A242" |
| 68 | "BA133460774D5CD\""}; |
| 69 | }; |
| 70 | |
| 71 | class Xcl_protocol_impl_tests_auth |
| 72 | : public Xcl_protocol_impl_tests, |
| 73 | public ::testing::WithParamInterface<Auth_details> { |
| 74 | public: |
| 75 | void assert_authenticate(const std::string &mech, |
| 76 | const int32_t error_code = 0) { |
| 77 | auto error = m_sut->execute_authenticate(m_messages.expected_user, |
| 78 | m_messages.expected_pass, |
| 79 | m_messages.expected_schema, mech); |
| 80 | |
| 81 | ASSERT_EQ(error_code, error.error()); |
| 82 | } |
| 83 | |
| 84 | Auth_test_messages m_messages; |
| 85 | }; |
| 86 | |
| 87 | TEST_F(Xcl_protocol_impl_tests_auth, execute_authenticate_invalid_method) { |
| 88 | assert_authenticate("INVALID", CR_X_INVALID_AUTH_METHOD); |
| 89 | assert_authenticate("plain", CR_X_INVALID_AUTH_METHOD); |
| 90 | assert_authenticate("mysql41", CR_X_INVALID_AUTH_METHOD); |
| 91 | assert_authenticate("sha256_memory", CR_X_INVALID_AUTH_METHOD); |
| 92 | } |
| 93 | |
| 94 | TEST_F(Xcl_protocol_impl_tests_auth, execute_authenticate_plain_method) { |
| 95 | expect_write_message(Auth_test_messages().msg_auth_start_plain.get()); |
| 96 | expect_read_message_without_payload(Auth_details::Authenticate_ok()); |
| 97 | |
| 98 | assert_authenticate("PLAIN"); |
| 99 | } |
| 100 | |
| 101 | TEST_F(Xcl_protocol_impl_tests_auth, |
| 102 | execute_authenticate_plain_method_error_msg) { |
| 103 | const int32_t expected_error_code = 30001; |
| 104 | auto msg_error = Server_message<::Mysqlx::Error>::make_required(); |
| 105 | |
| 106 | msg_error.set_code(expected_error_code); |
| 107 | |
| 108 | expect_write_message(Auth_test_messages().msg_auth_start_plain.get()); |
| 109 | expect_read_message(msg_error); |
| 110 | |
| 111 | assert_authenticate("PLAIN", expected_error_code); |
| 112 | } |
| 113 | |
| 114 | TEST_F(Xcl_protocol_impl_tests_auth, |
| 115 | execute_authenticate_plain_method_unexpected_msg) { |
| 116 | auto msg_stmt_ok = |
| 117 | Server_message<::Mysqlx::Sql::StmtExecuteOk>::make_required(); |
| 118 | |
| 119 | expect_write_message(Auth_test_messages().msg_auth_start_plain.get()); |
| 120 | expect_read_message_without_payload(msg_stmt_ok); |
| 121 | |
| 122 | assert_authenticate("PLAIN", CR_MALFORMED_PACKET); |
| 123 | } |
| 124 | |
| 125 | TEST_F(Xcl_protocol_impl_tests_auth, |
| 126 | execute_authenticate_plain_method_io_error_at_write) { |
| 127 | const int32_t expected_error_code = 30002; |
| 128 | |
| 129 | expect_write_message(Auth_test_messages().msg_auth_start_plain.get(), |
| 130 | expected_error_code); |
| 131 | |
| 132 | assert_authenticate("PLAIN", expected_error_code); |
| 133 | } |
| 134 | |
| 135 | TEST_F(Xcl_protocol_impl_tests_auth, |
| 136 | execute_authenticate_plain_method_io_error_at_read) { |
| 137 | const int32_t expected_error_code = 30003; |
| 138 | |
| 139 | expect_write_message(Auth_test_messages().msg_auth_start_plain.get()); |
| 140 | expect_read_message_without_payload(Auth_details::Authenticate_ok(), |
| 141 | expected_error_code); |
| 142 | |
| 143 | assert_authenticate("PLAIN", expected_error_code); |
| 144 | } |
| 145 | |
| 146 | TEST_F(Xcl_protocol_impl_tests_auth, |
| 147 | execute_authenticate_mysql41_method_too_short_auth_data) { |
| 148 | auto msg_auth_cont_s = |
| 149 | Server_message<Auth_details::Authenticate_continue>::make_required(); |
| 150 | msg_auth_cont_s.set_auth_data("TOOSHORT"); |
| 151 | |
| 152 | { |
| 153 | InSequence s; |
| 154 | expect_write_message(m_messages.msg_auth_start_mysql41.get()); |
| 155 | expect_read_message(msg_auth_cont_s); |
| 156 | expect_write_message(m_messages.msg_auth_cont_mysql41.get()); |
| 157 | expect_read_message_without_payload(Auth_details::Authenticate_ok()); |
| 158 | } |
| 159 | |
| 160 | assert_authenticate("MYSQL41"); |
| 161 | } |
| 162 | |
| 163 | TEST_P(Xcl_protocol_impl_tests_auth, |
| 164 | execute_authenticate_challenge_response_method) { |
| 165 | auto msg_auth_cont_s = |
| 166 | Server_message<Auth_details::Authenticate_continue>::make_required(); |
| 167 | |
| 168 | { |
| 169 | InSequence s; |
| 170 | |
| 171 | expect_write_message(GetParam().m_start_message.get()); |
| 172 | expect_read_message(msg_auth_cont_s); |
| 173 | expect_write_message(GetParam().m_continue_message.get()); |
| 174 | expect_read_message_without_payload(Auth_details::Authenticate_ok()); |
| 175 | } |
| 176 | |
| 177 | assert_authenticate(GetParam().m_auth_name); |
| 178 | } |
| 179 | |
| 180 | TEST_P(Xcl_protocol_impl_tests_auth, |
| 181 | execute_authenticate_challenge_response_method_recv_error_msg1) { |
| 182 | const int32_t expected_error_code = 30004; |
| 183 | auto msg_error = Server_message<::Mysqlx::Error>::make_required(); |
| 184 | |
| 185 | msg_error.set_code(expected_error_code); |
| 186 | |
| 187 | expect_write_message(GetParam().m_start_message.get()); |
| 188 | expect_read_message(msg_error); |
| 189 | |
| 190 | assert_authenticate(GetParam().m_auth_name, expected_error_code); |
| 191 | } |
| 192 | |
| 193 | TEST_P(Xcl_protocol_impl_tests_auth, |
| 194 | execute_authenticate_challenge_response_method_recv_error_msg2) { |
| 195 | const int32_t expected_error_code = 30005; |
| 196 | auto msg_auth_cont_s = |
| 197 | Server_message<Auth_details::Authenticate_continue>::make_required(); |
| 198 | |
| 199 | auto msg_error = Server_message<::Mysqlx::Error>::make_required(); |
| 200 | |
| 201 | msg_error.set_code(expected_error_code); |
| 202 | |
| 203 | { |
| 204 | InSequence s; |
| 205 | |
| 206 | expect_write_message(GetParam().m_start_message.get()); |
| 207 | expect_read_message(msg_auth_cont_s); |
| 208 | expect_write_message(GetParam().m_continue_message.get()); |
| 209 | expect_read_message(msg_error); |
| 210 | } |
| 211 | |
| 212 | assert_authenticate(GetParam().m_auth_name, expected_error_code); |
| 213 | } |
| 214 | |
| 215 | TEST_P(Xcl_protocol_impl_tests_auth, |
| 216 | execute_authenticate_challenge_response_method_recv_unexpected_msg1) { |
| 217 | auto msg_unexpected = Server_message<::Mysqlx::Ok>::make_required(); |
| 218 | |
| 219 | { |
| 220 | InSequence s; |
| 221 | |
| 222 | expect_write_message(GetParam().m_start_message.get()); |
| 223 | expect_read_message_without_payload(msg_unexpected); |
| 224 | } |
| 225 | |
| 226 | assert_authenticate(GetParam().m_auth_name, CR_MALFORMED_PACKET); |
| 227 | } |
| 228 | |
| 229 | TEST_P(Xcl_protocol_impl_tests_auth, |
| 230 | execute_authenticate_challenge_response_method_recv_unexpected_msg2) { |
| 231 | auto msg_auth_cont_s = |
| 232 | Server_message<Auth_details::Authenticate_continue>::make_required(); |
| 233 | |
| 234 | auto msg_unexpected = Server_message<::Mysqlx::Ok>::make_required(); |
| 235 | |
| 236 | { |
| 237 | InSequence s; |
| 238 | |
| 239 | expect_write_message(GetParam().m_start_message.get()); |
| 240 | expect_read_message(msg_auth_cont_s); |
| 241 | expect_write_message(GetParam().m_continue_message.get()); |
| 242 | expect_read_message_without_payload(msg_unexpected); |
| 243 | } |
| 244 | |
| 245 | assert_authenticate(GetParam().m_auth_name, CR_MALFORMED_PACKET); |
| 246 | } |
| 247 | |
| 248 | TEST_P(Xcl_protocol_impl_tests_auth, |
| 249 | execute_authenticate_challenge_response_method_recv_io_error1) { |
| 250 | const int32_t expected_error_code = 30006; |
| 251 | auto msg_auth_cont_s = |
| 252 | Server_message<Auth_details::Authenticate_continue>::make_required(); |
| 253 | |
| 254 | expect_write_message(GetParam().m_start_message.get()); |
| 255 | expect_read_message_without_payload(msg_auth_cont_s, expected_error_code); |
| 256 | |
| 257 | assert_authenticate(GetParam().m_auth_name, expected_error_code); |
| 258 | } |
| 259 | |
| 260 | TEST_P(Xcl_protocol_impl_tests_auth, |
| 261 | execute_authenticate_challenge_response_method_recv_io_error2) { |
| 262 | const int32_t expected_error_code = 30007; |
| 263 | auto msg_auth_cont_s = |
| 264 | Server_message<Auth_details::Authenticate_continue>::make_required(); |
| 265 | |
| 266 | { |
| 267 | InSequence s; |
| 268 | |
| 269 | expect_write_message(GetParam().m_start_message.get()); |
| 270 | expect_read_message(msg_auth_cont_s); |
| 271 | expect_write_message(GetParam().m_continue_message.get()); |
| 272 | expect_read_message_without_payload(Auth_details::Authenticate_ok(), |
| 273 | expected_error_code); |
| 274 | } |
| 275 | |
| 276 | assert_authenticate(GetParam().m_auth_name, expected_error_code); |
| 277 | } |
| 278 | |
| 279 | TEST_P(Xcl_protocol_impl_tests_auth, |
| 280 | execute_authenticate_challenge_response_method_write_io_error1) { |
| 281 | const int32_t expected_error_code = 30008; |
| 282 | |
| 283 | expect_write_message(GetParam().m_start_message.get(), expected_error_code); |
| 284 | |
| 285 | assert_authenticate(GetParam().m_auth_name, expected_error_code); |
| 286 | } |
| 287 | |
| 288 | TEST_P(Xcl_protocol_impl_tests_auth, |
| 289 | execute_authenticate_challenge_response_method_write_io_error2) { |
| 290 | const int32_t expected_error_code = 30009; |
| 291 | auto msg_auth_cont_s = |
| 292 | Server_message<Auth_details::Authenticate_continue>::make_required(); |
| 293 | |
| 294 | { |
| 295 | InSequence s; |
| 296 | |
| 297 | expect_write_message(GetParam().m_start_message.get()); |
| 298 | expect_read_message(msg_auth_cont_s); |
| 299 | expect_write_message(GetParam().m_continue_message.get(), |
| 300 | expected_error_code); |
| 301 | } |
| 302 | |
| 303 | assert_authenticate(GetParam().m_auth_name, expected_error_code); |
| 304 | } |
| 305 | |
| 306 | INSTANTIATE_TEST_SUITE_P( |
| 307 | InstantiationAuthDetails, Xcl_protocol_impl_tests_auth, |
| 308 | ::testing::Values( |
| 309 | Auth_details{"MYSQL41", Auth_test_messages().msg_auth_start_mysql41, |
| 310 | Auth_test_messages().msg_auth_cont_mysql41}, |
| 311 | Auth_details{"SHA256_MEMORY", |
| 312 | Auth_test_messages().msg_auth_start_sha256_memory, |
| 313 | Auth_test_messages().msg_auth_cont_sha256_memory})); |
| 314 | |
| 315 | } // namespace test |
| 316 | } // namespace xcl |