| 1 | /* |
| 2 | * Copyright (c) 2019, 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 <gmock/gmock.h> |
| 27 | #include <gtest/gtest.h> |
| 28 | |
| 29 | #include "plugin/x/protocol/encoders/encoding_buffer.h" |
| 30 | #include "plugin/x/protocol/encoders/encoding_pool.h" |
| 31 | #include "plugin/x/protocol/encoders/encoding_xmessages.h" |
| 32 | #include "plugin/x/protocol/encoders/encoding_xrow.h" |
| 33 | #include "plugin/x/src/ngs/protocol/page_pool.h" |
| 34 | #include "plugin/x/src/xpl_error.h" |
| 35 | #include "unittest/gunit/xplugin/xpl/admin_cmd_handler_t.h" |
| 36 | #include "unittest/gunit/xplugin/xpl/assert_error_code.h" |
| 37 | |
| 38 | namespace xpl { |
| 39 | namespace test { |
| 40 | |
| 41 | using ::testing::HasSubstr; |
| 42 | using ::testing::MatchesRegex; |
| 43 | using ::testing::ReturnRef; |
| 44 | using ::testing::StrEq; |
| 45 | |
| 46 | class Admin_cmd_get_collection_options_test |
| 47 | : public Admin_command_handler_test {}; |
| 48 | |
| 49 | TEST_F(Admin_cmd_get_collection_options_test, empty_schema) { |
| 50 | set_arguments(Any::Object{ |
| 51 | {"schema", ""}, COLLECTION_NAME, {"options", Any::Array{"validation"}}}); |
| 52 | ASSERT_ERROR_CODE(ER_X_BAD_SCHEMA, |
| 53 | command->get_collection_options(m_args.get())); |
| 54 | } |
| 55 | |
| 56 | TEST_F(Admin_cmd_get_collection_options_test, missing_schema) { |
| 57 | set_arguments(Any::Object{COLLECTION_NAME}); |
| 58 | ASSERT_ERROR_CODE(ER_X_CMD_NUM_ARGUMENTS, |
| 59 | command->get_collection_options(m_args.get())); |
| 60 | } |
| 61 | |
| 62 | TEST_F(Admin_cmd_get_collection_options_test, empty_collection_name) { |
| 63 | set_arguments( |
| 64 | Any::Object{SCHEMA, {"name", ""}, {"options", Any::Array{"validation"}}}); |
| 65 | ASSERT_ERROR_CODE(ER_X_BAD_TABLE, |
| 66 | command->get_collection_options(m_args.get())); |
| 67 | } |
| 68 | |
| 69 | TEST_F(Admin_cmd_get_collection_options_test, missing_collection_name) { |
| 70 | set_arguments(Any::Object{SCHEMA}); |
| 71 | ASSERT_ERROR_CODE(ER_X_CMD_NUM_ARGUMENTS, |
| 72 | command->get_collection_options(m_args.get())); |
| 73 | } |
| 74 | |
| 75 | TEST_F(Admin_cmd_get_collection_options_test, invalid_object) { |
| 76 | set_arguments(Any::Object{SCHEMA, COLLECTION_NAME, {"foo:", "bar"}}); |
| 77 | ASSERT_ERROR_CODE(ER_X_CMD_NUM_ARGUMENTS, |
| 78 | command->get_collection_options(m_args.get())); |
| 79 | } |
| 80 | |
| 81 | TEST_F(Admin_cmd_get_collection_options_test, empty_option_array) { |
| 82 | set_arguments( |
| 83 | Any::Object{SCHEMA, COLLECTION_NAME, {"options", Any::Array{}}}); |
| 84 | ASSERT_ERROR_CODE(ER_X_CMD_ARGUMENT_VALUE, |
| 85 | command->get_collection_options(m_args.get())); |
| 86 | } |
| 87 | |
| 88 | TEST_F(Admin_cmd_get_collection_options_test, one_invalid_option_array) { |
| 89 | set_arguments( |
| 90 | Any::Object{SCHEMA, COLLECTION_NAME, {"options", Any::Array{"foo"}}}); |
| 91 | ASSERT_ERROR_CODE(ER_X_COLLECTION_OPTION_DOESNT_EXISTS, |
| 92 | command->get_collection_options(m_args.get())); |
| 93 | } |
| 94 | |
| 95 | TEST_F(Admin_cmd_get_collection_options_test, valid_and_invalid_option_array) { |
| 96 | set_arguments(Any::Object{ |
| 97 | SCHEMA, COLLECTION_NAME, {"options", Any::Array{"foo", "validation"}}}); |
| 98 | ASSERT_ERROR_CODE(ER_X_COLLECTION_OPTION_DOESNT_EXISTS, |
| 99 | command->get_collection_options(m_args.get())); |
| 100 | } |
| 101 | |
| 102 | TEST_F(Admin_cmd_get_collection_options_test, |
| 103 | get_validation_user_has_no_access) { |
| 104 | set_arguments(Any::Object{ |
| 105 | SCHEMA, COLLECTION_NAME, {"options", Any::Array{"validation"}}}); |
| 106 | EXPECT_CALL(mock_data_context, |
| 107 | execute(StrEq("SELECT 1 FROM `xtest`.`test_coll` LIMIT 1"), _, _)) |
| 108 | .WillOnce(Return(ngs::Error_code{ER_TABLEACCESS_DENIED_ERROR, ""})); |
| 109 | ASSERT_ERROR_CODE(ER_TABLEACCESS_DENIED_ERROR, |
| 110 | command->get_collection_options(m_args.get())); |
| 111 | } |
| 112 | |
| 113 | TEST_F(Admin_cmd_get_collection_options_test, get_validation_ok) { |
| 114 | set_arguments(Any::Object{ |
| 115 | SCHEMA, COLLECTION_NAME, {"options", Any::Array{"validation"}}}); |
| 116 | EXPECT_CALL( |
| 117 | mock_data_context, |
| 118 | execute(StrEq("SELECT 1 FROM `xtest`.`test_coll` LIMIT 1"), _, _)); |
| 119 | EXPECT_CALL(mock_data_context, |
| 120 | execute(StrEq("SELECT @@lower_case_table_names"), _, _)); |
| 121 | EXPECT_CALL( |
| 122 | mock_data_context, |
| 123 | execute(HasSubstr("SELECT IF(COUNT(*),\"strict\",\"off\") FROM " |
| 124 | "information_schema.TABLE_CONSTRAINTS WHERE " |
| 125 | "TABLE_SCHEMA='xtest' AND TABLE_NAME='test_coll'"), |
| 126 | _, _)); |
| 127 | EXPECT_CALL( |
| 128 | mock_data_context, |
| 129 | execute(StrEq("SELECT GENERATION_EXPRESSION FROM " |
| 130 | "information_schema.COLUMNS WHERE TABLE_SCHEMA='xtest' AND " |
| 131 | "TABLE_NAME='test_coll' AND COLUMN_NAME='_json_schema';"), |
| 132 | _, _)); |
| 133 | EXPECT_CALL(mock_encoder, send_column_metadata(_)); |
| 134 | EXPECT_CALL(mock_encoder, start_row()); |
| 135 | ngs::Memory_block_pool memory_block_pool{{10, 4096}}; |
| 136 | protocol::Encoding_pool pool{10, &memory_block_pool}; |
| 137 | protocol::Encoding_buffer xproto_buffer{&pool}; |
| 138 | protocol::XMessage_encoder xproto_encoder{&xproto_buffer}; |
| 139 | protocol::XRow_encoder row_builder{&xproto_encoder}; |
| 140 | EXPECT_CALL(mock_encoder, row_builder()).WillOnce(Return(&row_builder)); |
| 141 | EXPECT_CALL(mock_encoder, send_row()); |
| 142 | EXPECT_CALL(mock_encoder, send_result_fetch_done()); |
| 143 | ASSERT_ERROR_CODE(ER_X_SUCCESS, |
| 144 | command->get_collection_options(m_args.get())); |
| 145 | } |
| 146 | |
| 147 | } // namespace test |
| 148 | } // namespace xpl |