| 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/src/xpl_error.h" |
| 30 | #include "unittest/gunit/xplugin/xpl/admin_cmd_handler_t.h" |
| 31 | #include "unittest/gunit/xplugin/xpl/assert_error_code.h" |
| 32 | |
| 33 | namespace xpl { |
| 34 | namespace test { |
| 35 | |
| 36 | using ::testing::HasSubstr; |
| 37 | using ::testing::StrEq; |
| 38 | |
| 39 | class Admin_command_handler_create_collection |
| 40 | : public Admin_command_handler_test {}; |
| 41 | |
| 42 | TEST_F(Admin_command_handler_create_collection, |
| 43 | create_collection_empty_schema) { |
| 44 | set_arguments(Any::Object{{"schema:", ""}, COLLECTION_NAME}); |
| 45 | ASSERT_ERROR_CODE(ER_X_CMD_NUM_ARGUMENTS, |
| 46 | command->create_collection(m_args.get())); |
| 47 | } |
| 48 | |
| 49 | TEST_F(Admin_command_handler_create_collection, |
| 50 | create_collection_missing_schema) { |
| 51 | set_arguments(Any::Object{COLLECTION_NAME}); |
| 52 | ASSERT_ERROR_CODE(ER_X_CMD_NUM_ARGUMENTS, |
| 53 | command->create_collection(m_args.get())); |
| 54 | } |
| 55 | |
| 56 | TEST_F(Admin_command_handler_create_collection, |
| 57 | create_collection_which_already_exists) { |
| 58 | set_arguments(Any::Object{SCHEMA, COLLECTION_NAME}); |
| 59 | EXPECT_CALL(mock_data_context, |
| 60 | execute(StrEq("SELECT @@lower_case_table_names"), _, _)); |
| 61 | EXPECT_CALL(mock_data_context, |
| 62 | execute_sql(HasSubstr("CREATE TABLE `xtest`.`test_coll`"), _, _)) |
| 63 | .WillOnce(Return(ngs::Error_code{ER_TABLE_EXISTS_ERROR, ""})); |
| 64 | ASSERT_ERROR_CODE(ER_TABLE_EXISTS_ERROR, |
| 65 | command->create_collection(m_args.get())); |
| 66 | } |
| 67 | |
| 68 | class Admin_command_handler_ensure_collection |
| 69 | : public Admin_command_handler_test {}; |
| 70 | |
| 71 | TEST_F(Admin_command_handler_ensure_collection, |
| 72 | ensure_collection_empty_schema) { |
| 73 | set_arguments(Any::Object{{"schema", ""}, COLLECTION_NAME}); |
| 74 | EXPECT_CALL(mock_data_context, |
| 75 | execute(StrEq("SELECT @@lower_case_table_names"), _, _)); |
| 76 | EXPECT_CALL(mock_data_context, |
| 77 | execute_sql(HasSubstr("CREATE TABLE `test_coll`"), _, _)); |
| 78 | ASSERT_ERROR_CODE(ER_X_SUCCESS, command->ensure_collection(m_args.get())); |
| 79 | } |
| 80 | |
| 81 | TEST_F(Admin_command_handler_ensure_collection, |
| 82 | ensure_collection_missing_schema) { |
| 83 | set_arguments(Any::Object{COLLECTION_NAME}); |
| 84 | EXPECT_CALL(mock_data_context, |
| 85 | execute(StrEq("SELECT @@lower_case_table_names"), _, _)); |
| 86 | EXPECT_CALL(mock_data_context, |
| 87 | execute_sql(HasSubstr("CREATE TABLE `test_coll`"), _, _)); |
| 88 | ASSERT_ERROR_CODE(ER_X_SUCCESS, command->ensure_collection(m_args.get())); |
| 89 | } |
| 90 | |
| 91 | TEST_F(Admin_command_handler_ensure_collection, |
| 92 | create_collection_which_already_exists) { |
| 93 | set_arguments(Any::Object{SCHEMA, COLLECTION_NAME}); |
| 94 | EXPECT_CALL(mock_data_context, |
| 95 | execute(StrEq("SELECT @@lower_case_table_names"), _, _)); |
| 96 | EXPECT_CALL(mock_data_context, |
| 97 | is_sql_mode_set(StrEq("NO_BACKSLASH_ESCAPES"))); |
| 98 | EXPECT_CALL(mock_data_context, |
| 99 | execute_sql(HasSubstr("CREATE TABLE `xtest`.`test_coll`"), _, _)) |
| 100 | .WillOnce(Return(ngs::Error_code{ER_TABLE_EXISTS_ERROR, ""})); |
| 101 | EXPECT_CALL(mock_data_context, |
| 102 | execute(HasSubstr("SELECT COUNT(*) AS cnt"), _, _)); |
| 103 | ASSERT_ERROR_CODE(ER_X_INVALID_COLLECTION, |
| 104 | command->ensure_collection(m_args.get())); |
| 105 | } |
| 106 | |
| 107 | using Admin_command_method_t = ngs::Error_code (Admin_command_handler::*)( |
| 108 | Admin_command_handler::Command_arguments *); |
| 109 | |
| 110 | class Admin_command_handler_create_or_ensure_collection |
| 111 | : public Admin_command_handler_test, |
| 112 | public ::testing::WithParamInterface<Admin_command_method_t> {}; |
| 113 | |
| 114 | TEST_P(Admin_command_handler_create_or_ensure_collection, |
| 115 | create_collection_empty_collection_name) { |
| 116 | set_arguments(Any::Object{SCHEMA, {"name:", ""}}); |
| 117 | ASSERT_ERROR_CODE(ER_X_CMD_NUM_ARGUMENTS, |
| 118 | ((*command).*GetParam())(m_args.get())); |
| 119 | } |
| 120 | |
| 121 | TEST_P(Admin_command_handler_create_or_ensure_collection, |
| 122 | create_collection_missing_collection_name) { |
| 123 | set_arguments(Any::Object{SCHEMA}); |
| 124 | ASSERT_ERROR_CODE(ER_X_CMD_NUM_ARGUMENTS, |
| 125 | ((*command).*GetParam())(m_args.get())); |
| 126 | } |
| 127 | |
| 128 | TEST_P(Admin_command_handler_create_or_ensure_collection, |
| 129 | create_collection_valid) { |
| 130 | set_arguments(Any::Object{SCHEMA, COLLECTION_NAME}); |
| 131 | EXPECT_CALL(mock_data_context, |
| 132 | execute(StrEq("SELECT @@lower_case_table_names"), _, _)); |
| 133 | EXPECT_CALL(mock_data_context, |
| 134 | execute_sql(HasSubstr("CREATE TABLE `xtest`.`test_coll`"), _, _)); |
| 135 | ASSERT_ERROR_CODE(ER_X_SUCCESS, ((*command).*GetParam())(m_args.get())); |
| 136 | } |
| 137 | |
| 138 | TEST_P(Admin_command_handler_create_or_ensure_collection, |
| 139 | create_collection_with_invalid_obj) { |
| 140 | set_arguments(Any::Object{SCHEMA, COLLECTION_NAME, {"foo:", "bar"}}); |
| 141 | ASSERT_NE(ER_X_SUCCESS, ((*command).*GetParam())(m_args.get()).error); |
| 142 | } |
| 143 | |
| 144 | TEST_P(Admin_command_handler_create_or_ensure_collection, |
| 145 | create_collection_with_invalid_validation_option) { |
| 146 | set_validation_details({{"foo", Scalar::String{"bar"}}}); |
| 147 | ASSERT_NE(ER_X_SUCCESS, ((*command).*GetParam())(m_args.get()).error); |
| 148 | } |
| 149 | |
| 150 | INSTANTIATE_TEST_SUITE_P( |
| 151 | Instantiation_create_collection_common, |
| 152 | Admin_command_handler_create_or_ensure_collection, |
| 153 | ::testing::Values(&Admin_command_handler_stub::create_collection, |
| 154 | &Admin_command_handler_stub::ensure_collection)); |
| 155 | |
| 156 | class Admin_command_handler_check_validation |
| 157 | : public Admin_command_handler_test, |
| 158 | public ::testing::WithParamInterface<Admin_command_method_t> {}; |
| 159 | |
| 160 | TEST_P(Admin_command_handler_check_validation, invalid_option) { |
| 161 | set_arguments(Any::Object{ |
| 162 | SCHEMA, COLLECTION_NAME, {"options", Any::Object{{"bar", "baz"}}}}); |
| 163 | ASSERT_ERROR_CODE(ER_X_CMD_INVALID_ARGUMENT, |
| 164 | ((*command).*GetParam())(m_args.get())); |
| 165 | } |
| 166 | |
| 167 | TEST_P(Admin_command_handler_check_validation, empty_options) { |
| 168 | set_arguments( |
| 169 | Any::Object{SCHEMA, COLLECTION_NAME, {"options", Any::Object{}}}); |
| 170 | EXPECT_CALL(mock_data_context, |
| 171 | execute(StrEq("SELECT @@lower_case_table_names"), _, _)); |
| 172 | EXPECT_CALL(mock_data_context, |
| 173 | execute_sql(HasSubstr("CREATE TABLE `xtest`.`test_coll`"), _, _)); |
| 174 | ASSERT_ERROR_CODE(ER_X_SUCCESS, ((*command).*GetParam())(m_args.get())); |
| 175 | } |
| 176 | |
| 177 | TEST_P(Admin_command_handler_check_validation, |
| 178 | create_collection_with_validation_obj) { |
| 179 | set_validation_details({{"schema", Any::Object{{"description", "obj"}}}}); |
| 180 | EXPECT_CALL(mock_data_context, |
| 181 | execute(StrEq("SELECT @@lower_case_table_names"), _, _)); |
| 182 | EXPECT_CALL(mock_data_context, |
| 183 | execute_sql(HasSubstr("CREATE TABLE `xtest`.`test_coll`"), _, _)); |
| 184 | ASSERT_ERROR_CODE(ER_X_SUCCESS, ((*command).*GetParam())(m_args.get())); |
| 185 | } |
| 186 | |
| 187 | TEST_P(Admin_command_handler_check_validation, |
| 188 | create_collection_with_validation_string) { |
| 189 | set_validation_details( |
| 190 | {{"schema", Scalar::String{"{\"description\": \"value\"}"}}}); |
| 191 | EXPECT_CALL(mock_data_context, |
| 192 | execute(StrEq("SELECT @@lower_case_table_names"), _, _)); |
| 193 | EXPECT_CALL(mock_data_context, |
| 194 | execute_sql(HasSubstr("CREATE TABLE `xtest`.`test_coll`"), _, _)); |
| 195 | ASSERT_ERROR_CODE(ER_X_SUCCESS, ((*command).*GetParam())(m_args.get())); |
| 196 | } |
| 197 | |
| 198 | TEST_P(Admin_command_handler_check_validation, |
| 199 | create_collection_with_invalid_validation_schema_obj) { |
| 200 | set_validation_details({{"schema", Any::Object{{"unknown", "value"}}}}); |
| 201 | ASSERT_ERROR_CODE(ER_X_INVALID_VALIDATION_SCHEMA, |
| 202 | ((*command).*GetParam())(m_args.get())); |
| 203 | } |
| 204 | |
| 205 | TEST_P(Admin_command_handler_check_validation, |
| 206 | create_collection_with_invalid_validation_schema_string) { |
| 207 | set_validation_details( |
| 208 | {{"schema", Scalar::String{"{\"unknown\": \"value\"}"}}}); |
| 209 | ASSERT_ERROR_CODE(ER_X_INVALID_VALIDATION_SCHEMA, |
| 210 | ((*command).*GetParam())(m_args.get())); |
| 211 | } |
| 212 | |
| 213 | TEST_P(Admin_command_handler_check_validation, |
| 214 | create_collection_with_invalid_validation_json_obj) { |
| 215 | set_validation_details({{"schema", Any::Object{{"\"", "invalid json"}}}}); |
| 216 | ASSERT_ERROR_CODE(ER_X_INVALID_VALIDATION_SCHEMA, |
| 217 | ((*command).*GetParam())(m_args.get())); |
| 218 | } |
| 219 | |
| 220 | TEST_P(Admin_command_handler_check_validation, |
| 221 | create_collection_with_invalid_validation_json_string) { |
| 222 | set_validation_details({{"schema", Scalar::String{"{\"invalid json\"}"}}}); |
| 223 | ASSERT_ERROR_CODE(ER_X_INVALID_VALIDATION_SCHEMA, |
| 224 | ((*command).*GetParam())(m_args.get())); |
| 225 | } |
| 226 | |
| 227 | TEST_P(Admin_command_handler_check_validation, |
| 228 | create_collection_with_missing_validation_schema) { |
| 229 | set_arguments( |
| 230 | Any::Object{SCHEMA, |
| 231 | COLLECTION_NAME, |
| 232 | {"options", Any::Object{{"validation", Any::Object{}}}}}); |
| 233 | EXPECT_CALL(mock_data_context, |
| 234 | execute(StrEq("SELECT @@lower_case_table_names"), _, _)); |
| 235 | EXPECT_CALL(mock_data_context, |
| 236 | execute_sql(HasSubstr("CREATE TABLE `xtest`.`test_coll`"), _, _)); |
| 237 | ASSERT_ERROR_CODE(ER_X_SUCCESS, ((*command).*GetParam())(m_args.get())); |
| 238 | } |
| 239 | |
| 240 | TEST_P(Admin_command_handler_check_validation, |
| 241 | create_collection_with_empty_validation_schema_object) { |
| 242 | set_validation_details({{"schema", Any::Object{}}}); |
| 243 | EXPECT_CALL(mock_data_context, |
| 244 | execute(StrEq("SELECT @@lower_case_table_names"), _, _)); |
| 245 | EXPECT_CALL(mock_data_context, |
| 246 | execute_sql(HasSubstr("GENERATED ALWAYS AS ('{}')"), _, _)); |
| 247 | ASSERT_ERROR_CODE(ER_X_SUCCESS, ((*command).*GetParam())(m_args.get())); |
| 248 | } |
| 249 | |
| 250 | TEST_P(Admin_command_handler_check_validation, |
| 251 | create_collection_with_empty_validation_schema_string) { |
| 252 | set_validation_details({{"schema", Scalar::String{"{}"}}}); |
| 253 | EXPECT_CALL(mock_data_context, |
| 254 | execute(StrEq("SELECT @@lower_case_table_names"), _, _)); |
| 255 | EXPECT_CALL(mock_data_context, |
| 256 | execute_sql(HasSubstr("GENERATED ALWAYS AS ('{}')"), _, _)); |
| 257 | ASSERT_ERROR_CODE(ER_X_SUCCESS, ((*command).*GetParam())(m_args.get())); |
| 258 | } |
| 259 | |
| 260 | TEST_P(Admin_command_handler_check_validation, |
| 261 | create_collection_with_validation_level_set_to_strict) { |
| 262 | set_validation_details({{"schema", Any::Object{}}, {"level", "strict"}}); |
| 263 | EXPECT_CALL(mock_data_context, |
| 264 | execute(StrEq("SELECT @@lower_case_table_names"), _, _)); |
| 265 | EXPECT_CALL(mock_data_context, |
| 266 | execute_sql(HasSubstr("CHECK " |
| 267 | "(JSON_SCHEMA_VALID(_json_schema," |
| 268 | " doc)) ENFORCED)"), |
| 269 | _, _)); |
| 270 | ASSERT_ERROR_CODE(ER_X_SUCCESS, ((*command).*GetParam())(m_args.get())); |
| 271 | } |
| 272 | |
| 273 | TEST_P(Admin_command_handler_check_validation, |
| 274 | create_collection_with_validation_level_set_to_off) { |
| 275 | set_validation_details({{"schema", Any::Object{}}, {"level", "off"}}); |
| 276 | EXPECT_CALL(mock_data_context, |
| 277 | execute(StrEq("SELECT @@lower_case_table_names"), _, _)); |
| 278 | EXPECT_CALL(mock_data_context, |
| 279 | execute_sql(HasSubstr("CHECK (JSON_SCHEMA_VALID(_json_" |
| 280 | "schema, doc)) NOT ENFORCED)"), |
| 281 | _, _)); |
| 282 | ASSERT_ERROR_CODE(ER_X_SUCCESS, ((*command).*GetParam())(m_args.get())); |
| 283 | } |
| 284 | |
| 285 | TEST_P(Admin_command_handler_check_validation, |
| 286 | create_collection_with_validation_level_case_insensitive) { |
| 287 | set_validation_details({{"schema", Any::Object{}}, {"level", "Strict"}}); |
| 288 | EXPECT_CALL(mock_data_context, |
| 289 | execute(StrEq("SELECT @@lower_case_table_names"), _, _)); |
| 290 | EXPECT_CALL(mock_data_context, |
| 291 | execute_sql(HasSubstr("CHECK (JSON_SCHEMA_VALID(_json_" |
| 292 | "schema, doc)) ENFORCED)"), |
| 293 | _, _)); |
| 294 | ASSERT_ERROR_CODE(ER_X_SUCCESS, ((*command).*GetParam())(m_args.get())); |
| 295 | } |
| 296 | |
| 297 | TEST_P(Admin_command_handler_check_validation, |
| 298 | create_collection_with_validation_level_invalid_value) { |
| 299 | set_validation_details({{"schema", Any::Object{}}, {"level", "foo"}}); |
| 300 | ASSERT_ERROR_CODE(ER_X_CMD_ARGUMENT_VALUE, |
| 301 | ((*command).*GetParam())(m_args.get())); |
| 302 | } |
| 303 | |
| 304 | INSTANTIATE_TEST_SUITE_P( |
| 305 | Instantiation_create_collection_with_validation, |
| 306 | Admin_command_handler_check_validation, |
| 307 | ::testing::Values(&Admin_command_handler_stub::create_collection, |
| 308 | &Admin_command_handler_stub::ensure_collection)); |
| 309 | |
| 310 | } // namespace test |
| 311 | } // namespace xpl |