| 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 <gmock/gmock.h> |
| 25 | #include <gtest/gtest.h> |
| 26 | |
| 27 | #include "plugin/x/src/client.h" |
| 28 | #include "plugin/x/src/prepare_command_handler.h" |
| 29 | #include "plugin/x/src/xpl_error.h" |
| 30 | #include "unittest/gunit/xplugin/xpl/assert_error_code.h" |
| 31 | #include "unittest/gunit/xplugin/xpl/mock/notice_configuration.h" |
| 32 | #include "unittest/gunit/xplugin/xpl/mock/notice_output_queue.h" |
| 33 | #include "unittest/gunit/xplugin/xpl/mock/protocol_encoder.h" |
| 34 | #include "unittest/gunit/xplugin/xpl/mock/session.h" |
| 35 | #include "unittest/gunit/xplugin/xpl/mock/sql_session.h" |
| 36 | |
| 37 | namespace xpl { |
| 38 | namespace test { |
| 39 | |
| 40 | using ::testing::_; |
| 41 | using ::testing::Invoke; |
| 42 | using ::testing::Return; |
| 43 | using ::testing::ReturnRef; |
| 44 | using ::testing::StrictMock; |
| 45 | |
| 46 | class Cursor_test_suite : public ::testing::Test { |
| 47 | public: |
| 48 | void SetUp() override { |
| 49 | EXPECT_CALL(m_mock_session, proto()) |
| 50 | .WillRepeatedly(ReturnRef(m_mock_encoder)); |
| 51 | EXPECT_CALL(m_mock_session, get_notice_output_queue()) |
| 52 | .WillRepeatedly(ReturnRef(m_mock_notice_output_queue)); |
| 53 | EXPECT_CALL(m_mock_session, get_notice_configuration()) |
| 54 | .WillRepeatedly(ReturnRef(m_mock_notice_configuration)); |
| 55 | } |
| 56 | |
| 57 | ngs::Metadata_builder m_meta_builder; |
| 58 | StrictMock<mock::Protocol_encoder> m_mock_encoder; |
| 59 | StrictMock<mock::Session> m_mock_session; |
| 60 | StrictMock<mock::Notice_output_queue> m_mock_notice_output_queue; |
| 61 | StrictMock<mock::Notice_configuration> m_mock_notice_configuration; |
| 62 | StrictMock<mock::Sql_session> m_mock_data_context; |
| 63 | Prepare_command_handler m_handler{&m_mock_session}; |
| 64 | }; |
| 65 | |
| 66 | TEST_F(Cursor_test_suite, add_cursor_more_rows_after_open) { |
| 67 | const auto stmt_id = 42; |
| 68 | const auto cursor_id = 1; |
| 69 | const auto resultset_stmt_id = 2; |
| 70 | |
| 71 | Mysqlx::Cursor::Open open_msg; |
| 72 | open_msg.set_cursor_id(cursor_id); |
| 73 | ::Mysqlx::Prepare::Execute *prepare_execute = |
| 74 | new ::Mysqlx::Prepare::Execute(); |
| 75 | prepare_execute->set_stmt_id(stmt_id); |
| 76 | ::Mysqlx::Cursor::Open_OneOfMessage *one_of_message = |
| 77 | new ::Mysqlx::Cursor::Open_OneOfMessage(); |
| 78 | one_of_message->set_allocated_prepare_execute(prepare_execute); |
| 79 | open_msg.set_allocated_stmt(one_of_message); |
| 80 | |
| 81 | const auto type = Mysqlx::Prepare::Prepare::OneOfMessage::STMT; |
| 82 | m_handler.insert_prepared_statement( |
| 83 | stmt_id, Prepare_command_handler::Prepared_stmt_info{ |
| 84 | resultset_stmt_id, type, {}, 1, true, false, cursor_id}); |
| 85 | |
| 86 | EXPECT_CALL(m_mock_encoder, get_metadata_builder()) |
| 87 | .WillOnce(Return(&m_meta_builder)); |
| 88 | EXPECT_CALL(m_mock_session, data_context()) |
| 89 | .WillRepeatedly(ReturnRef(m_mock_data_context)); |
| 90 | EXPECT_CALL(m_mock_data_context, |
| 91 | execute_prep_stmt(resultset_stmt_id, true, _, _, _)) |
| 92 | .WillRepeatedly(Return(ngs::Success())); |
| 93 | EXPECT_CALL(m_mock_session, |
| 94 | update_status(&ngs::Common_status_variables::m_cursor_open)); |
| 95 | EXPECT_CALL(m_mock_encoder, send_exec_ok()); |
| 96 | |
| 97 | ASSERT_ERROR_CODE(ER_X_SUCCESS, m_handler.execute_cursor_open(open_msg)); |
| 98 | EXPECT_EQ(1, m_handler.cursors_count()); |
| 99 | EXPECT_TRUE(nullptr != m_handler.get_cursor_if_allocated(cursor_id)); |
| 100 | } |
| 101 | |
| 102 | TEST_F(Cursor_test_suite, |
| 103 | add_cursor_already_allocated_and_more_rows_after_open) { |
| 104 | const auto stmt_id = 42; |
| 105 | const auto cursor_id = 1; |
| 106 | const auto resultset_stmt_id = 2; |
| 107 | Mysqlx::Cursor::Open open_msg; |
| 108 | open_msg.set_cursor_id(cursor_id); |
| 109 | ::Mysqlx::Prepare::Execute *prepare_execute = |
| 110 | new ::Mysqlx::Prepare::Execute(); |
| 111 | prepare_execute->set_stmt_id(stmt_id); |
| 112 | ::Mysqlx::Cursor::Open_OneOfMessage *one_of_message = |
| 113 | new ::Mysqlx::Cursor::Open_OneOfMessage(); |
| 114 | one_of_message->set_allocated_prepare_execute(prepare_execute); |
| 115 | open_msg.set_allocated_stmt(one_of_message); |
| 116 | |
| 117 | ngs::Message_request request; |
| 118 | request.reset(Mysqlx::ClientMessages::CURSOR_OPEN, &open_msg); |
| 119 | |
| 120 | EXPECT_CALL(m_mock_encoder, get_metadata_builder()) |
| 121 | .WillRepeatedly(Return(&m_meta_builder)); |
| 122 | |
| 123 | const auto type = Mysqlx::Prepare::Prepare::OneOfMessage::STMT; |
| 124 | m_handler.insert_prepared_statement( |
| 125 | stmt_id, Prepare_command_handler::Prepared_stmt_info{ |
| 126 | resultset_stmt_id, type, {}, 1, true, false, cursor_id}); |
| 127 | m_handler.insert_cursor(cursor_id, stmt_id, false, false); |
| 128 | |
| 129 | EXPECT_CALL(m_mock_session, data_context()) |
| 130 | .WillRepeatedly(ReturnRef(m_mock_data_context)); |
| 131 | EXPECT_CALL(m_mock_data_context, |
| 132 | execute_prep_stmt(resultset_stmt_id, true, _, _, _)) |
| 133 | .WillRepeatedly(Return(ngs::Success())); |
| 134 | EXPECT_CALL(m_mock_session, |
| 135 | update_status(&ngs::Common_status_variables::m_cursor_open)); |
| 136 | EXPECT_CALL(m_mock_encoder, send_exec_ok()); |
| 137 | |
| 138 | EXPECT_EQ(m_handler.cursors_count(), 1); |
| 139 | ASSERT_ERROR_CODE(ER_X_SUCCESS, m_handler.execute_cursor_open(open_msg)); |
| 140 | EXPECT_EQ(m_handler.cursors_count(), 1); |
| 141 | } |
| 142 | |
| 143 | TEST_F(Cursor_test_suite, add_cursor_no_existing_statement) { |
| 144 | Mysqlx::Cursor::Open open_msg; |
| 145 | open_msg.set_cursor_id(1); |
| 146 | ::Mysqlx::Prepare::Execute *prepare_execute = |
| 147 | new ::Mysqlx::Prepare::Execute(); |
| 148 | prepare_execute->set_stmt_id(42); |
| 149 | ::Mysqlx::Cursor::Open_OneOfMessage *one_of_message = |
| 150 | new ::Mysqlx::Cursor::Open_OneOfMessage(); |
| 151 | one_of_message->set_allocated_prepare_execute(prepare_execute); |
| 152 | open_msg.set_allocated_stmt(one_of_message); |
| 153 | |
| 154 | EXPECT_CALL(m_mock_session, |
| 155 | update_status(&ngs::Common_status_variables::m_cursor_open)); |
| 156 | |
| 157 | ASSERT_ERROR_CODE(ER_X_BAD_STATEMENT_ID, |
| 158 | m_handler.execute_cursor_open(open_msg)); |
| 159 | |
| 160 | EXPECT_EQ(m_handler.cursors_count(), 0); |
| 161 | } |
| 162 | |
| 163 | TEST_F(Cursor_test_suite, |
| 164 | add_cursor_statement_has_cursor_already_and_more_rows_after_open) { |
| 165 | const auto stmt_id = 42; |
| 166 | const auto cursor_id = 1; |
| 167 | const auto resultset_stmt_id = 2; |
| 168 | Mysqlx::Cursor::Open open_msg; |
| 169 | open_msg.set_cursor_id(cursor_id); |
| 170 | ::Mysqlx::Prepare::Execute *prepare_execute = |
| 171 | new ::Mysqlx::Prepare::Execute(); |
| 172 | prepare_execute->set_stmt_id(stmt_id); |
| 173 | ::Mysqlx::Cursor::Open_OneOfMessage *one_of_message = |
| 174 | new ::Mysqlx::Cursor::Open_OneOfMessage(); |
| 175 | one_of_message->set_allocated_prepare_execute(prepare_execute); |
| 176 | open_msg.set_allocated_stmt(one_of_message); |
| 177 | |
| 178 | ngs::Message_request request; |
| 179 | request.reset(Mysqlx::ClientMessages::CURSOR_OPEN, &open_msg); |
| 180 | |
| 181 | const auto type = Mysqlx::Prepare::Prepare::OneOfMessage::STMT; |
| 182 | m_handler.insert_prepared_statement( |
| 183 | stmt_id, Prepare_command_handler::Prepared_stmt_info{ |
| 184 | resultset_stmt_id, type, {}, 1, true, true, cursor_id}); |
| 185 | |
| 186 | EXPECT_CALL(m_mock_encoder, get_metadata_builder()) |
| 187 | .WillOnce(Return(&m_meta_builder)); |
| 188 | EXPECT_CALL(m_mock_session, data_context()) |
| 189 | .WillRepeatedly(ReturnRef(m_mock_data_context)); |
| 190 | EXPECT_CALL(m_mock_data_context, |
| 191 | execute_prep_stmt(resultset_stmt_id, true, _, _, _)) |
| 192 | .WillRepeatedly(Return(ngs::Success())); |
| 193 | EXPECT_CALL(m_mock_session, |
| 194 | update_status(&ngs::Common_status_variables::m_cursor_open)); |
| 195 | EXPECT_CALL(m_mock_encoder, send_exec_ok()); |
| 196 | |
| 197 | ASSERT_ERROR_CODE(ER_X_SUCCESS, m_handler.execute_cursor_open(open_msg)); |
| 198 | EXPECT_EQ(m_handler.cursors_count(), 1); |
| 199 | } |
| 200 | |
| 201 | TEST_F(Cursor_test_suite, close_cursor) { |
| 202 | const auto cursor_id = 1; |
| 203 | const auto stmt_id = 42; |
| 204 | Mysqlx::Cursor::Close close_msg; |
| 205 | close_msg.set_cursor_id(cursor_id); |
| 206 | |
| 207 | EXPECT_CALL(m_mock_encoder, get_metadata_builder()) |
| 208 | .WillOnce(Return(&m_meta_builder)); |
| 209 | |
| 210 | m_handler.insert_cursor(cursor_id, stmt_id, false, false); |
| 211 | |
| 212 | EXPECT_CALL(m_mock_session, |
| 213 | update_status(&ngs::Common_status_variables::m_cursor_close)); |
| 214 | EXPECT_CALL(m_mock_encoder, send_ok()); |
| 215 | EXPECT_TRUE(nullptr != m_handler.get_cursor_if_allocated(cursor_id)); |
| 216 | EXPECT_EQ(m_handler.cursors_count(), 1); |
| 217 | ASSERT_ERROR_CODE(ER_X_SUCCESS, m_handler.execute_cursor_close(close_msg)); |
| 218 | EXPECT_EQ(m_handler.cursors_count(), 0); |
| 219 | EXPECT_FALSE(nullptr != m_handler.get_cursor_if_allocated(cursor_id)); |
| 220 | } |
| 221 | |
| 222 | TEST_F(Cursor_test_suite, close_cursor_wrong_cursor_id) { |
| 223 | const auto cursor_id = 1; |
| 224 | Mysqlx::Cursor::Close close_msg; |
| 225 | close_msg.set_cursor_id(cursor_id); |
| 226 | |
| 227 | EXPECT_CALL(m_mock_session, |
| 228 | update_status(&ngs::Common_status_variables::m_cursor_close)); |
| 229 | |
| 230 | ASSERT_ERROR_CODE(ER_X_BAD_CURSOR_ID, |
| 231 | m_handler.execute_cursor_close(close_msg)); |
| 232 | } |
| 233 | |
| 234 | TEST_F(Cursor_test_suite, fetch_cursor) { |
| 235 | const auto cursor_id = 1; |
| 236 | const auto stmt_id = 42; |
| 237 | const auto resultset_stmt_id = 2; |
| 238 | const auto count = 1; |
| 239 | |
| 240 | Mysqlx::Cursor::Fetch fetch_msg; |
| 241 | fetch_msg.set_cursor_id(cursor_id); |
| 242 | fetch_msg.set_fetch_rows(count); |
| 243 | |
| 244 | const auto type = Mysqlx::Prepare::Prepare::OneOfMessage::STMT; |
| 245 | m_handler.insert_prepared_statement( |
| 246 | stmt_id, Prepare_command_handler::Prepared_stmt_info{ |
| 247 | resultset_stmt_id, type, {}, 1, true, true, cursor_id}); |
| 248 | |
| 249 | EXPECT_CALL(m_mock_encoder, get_metadata_builder()) |
| 250 | .WillOnce(Return(&m_meta_builder)); |
| 251 | EXPECT_CALL(m_mock_session, |
| 252 | update_status(&ngs::Common_status_variables::m_cursor_fetch)); |
| 253 | EXPECT_CALL(m_mock_session, data_context()) |
| 254 | .WillRepeatedly(ReturnRef(m_mock_data_context)); |
| 255 | EXPECT_CALL(m_mock_data_context, fetch_cursor(resultset_stmt_id, count, _)) |
| 256 | .WillRepeatedly(Return(ngs::Error_code{})); |
| 257 | m_handler.insert_cursor(cursor_id, stmt_id, false, false); |
| 258 | |
| 259 | EXPECT_CALL(m_mock_encoder, send_exec_ok()); |
| 260 | ASSERT_ERROR_CODE(ER_X_SUCCESS, m_handler.execute_cursor_fetch(fetch_msg)); |
| 261 | } |
| 262 | |
| 263 | TEST_F(Cursor_test_suite, fetch_cursor_wrong_cursor_id) { |
| 264 | const auto cursor_id = 1; |
| 265 | Mysqlx::Cursor::Fetch fetch_msg; |
| 266 | fetch_msg.set_cursor_id(cursor_id); |
| 267 | |
| 268 | EXPECT_CALL(m_mock_session, |
| 269 | update_status(&ngs::Common_status_variables::m_cursor_fetch)); |
| 270 | ASSERT_ERROR_CODE(ER_X_BAD_CURSOR_ID, |
| 271 | m_handler.execute_cursor_fetch(fetch_msg)); |
| 272 | } |
| 273 | |
| 274 | } // namespace test |
| 275 | } // namespace xpl |