| 1 | /* |
| 2 | * Copyright (c) 2018, 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 <my_byteorder.h> |
| 30 | |
| 31 | #include "plugin/x/src/prepare_param_handler.h" |
| 32 | #include "plugin/x/src/xpl_error.h" |
| 33 | #include "unittest/gunit/xplugin/xpl/assert_error_code.h" |
| 34 | #include "unittest/gunit/xplugin/xpl/mock/session.h" |
| 35 | #include "unittest/gunit/xplugin/xpl/mysqlx_pb_wrapper.h" |
| 36 | |
| 37 | namespace xpl { |
| 38 | namespace test { |
| 39 | |
| 40 | using Arg_list = Prepare_param_handler::Arg_list; |
| 41 | using Param_list = Prepare_param_handler::Param_list; |
| 42 | using Placeholder_list = Prepare_param_handler::Placeholder_list; |
| 43 | using Param_value_list = Prepare_param_handler::Param_value_list; |
| 44 | using Param_svalue_list = Prepare_param_handler::Param_svalue_list; |
| 45 | |
| 46 | class Prepare_param_handler_base_test : public testing::Test { |
| 47 | public: |
| 48 | Placeholder_list m_placeholders; |
| 49 | Prepare_param_handler m_handler{m_placeholders}; |
| 50 | }; |
| 51 | |
| 52 | struct Param_check_argument_placeholder_consistency { |
| 53 | int expect_error_code; |
| 54 | std::size_t args_size; |
| 55 | Placeholder_list phs; |
| 56 | }; |
| 57 | |
| 58 | Param_check_argument_placeholder_consistency |
| 59 | check_argument_placeholder_consistency_param[] = { |
| 60 | {ER_X_SUCCESS, 0, {}}, |
| 61 | {ER_X_SUCCESS, 1, {0}}, |
| 62 | {ER_X_SUCCESS, 1, {0, 0, 0}}, |
| 63 | {ER_X_SUCCESS, 2, {1, 0, 1}}, |
| 64 | {ER_X_SUCCESS, 3, {0, 1, 2}}, |
| 65 | {ER_X_SUCCESS, 3, {1, 2, 0}}, |
| 66 | {ER_X_SUCCESS, 3, {2, 1, 0}}, |
| 67 | {ER_X_SUCCESS, 3, {2, 0, 1}}, |
| 68 | {ER_X_SUCCESS, 3, {1, 0, 2}}, |
| 69 | {ER_X_SUCCESS, 3, {0, 2, 1}}, |
| 70 | {ER_X_PREPARED_EXECUTE_ARGUMENT_CONSISTENCY, 0, {0}}, |
| 71 | {ER_X_PREPARED_EXECUTE_ARGUMENT_CONSISTENCY, 1, {2}}, |
| 72 | }; |
| 73 | |
| 74 | class Prepare_param_handler_argument_consistency_test |
| 75 | : public Prepare_param_handler_base_test, |
| 76 | public testing::WithParamInterface< |
| 77 | Param_check_argument_placeholder_consistency> {}; |
| 78 | |
| 79 | TEST_P(Prepare_param_handler_argument_consistency_test, |
| 80 | check_argument_placeholder_consistency) { |
| 81 | const Param_check_argument_placeholder_consistency ¶m = GetParam(); |
| 82 | m_placeholders = param.phs; |
| 83 | ASSERT_ERROR_CODE( |
| 84 | param.expect_error_code, |
| 85 | m_handler.check_argument_placeholder_consistency(param.args_size, 0)); |
| 86 | } |
| 87 | |
| 88 | INSTANTIATE_TEST_SUITE_P( |
| 89 | Prepare_command_handler, Prepare_param_handler_argument_consistency_test, |
| 90 | testing::ValuesIn(check_argument_placeholder_consistency_param)); |
| 91 | |
| 92 | namespace { |
| 93 | |
| 94 | class Value { |
| 95 | public: |
| 96 | Value() = default; |
| 97 | explicit Value(const unsigned v) { |
| 98 | int8store(buf.data(), static_cast<uint64_t>(v)); |
| 99 | } |
| 100 | explicit Value(const int v) { |
| 101 | int8store(buf.data(), static_cast<int64_t>(v)); |
| 102 | } |
| 103 | explicit Value(const double v) { float8store(buf.data(), v); } |
| 104 | explicit Value(const float v) { float4store(buf.data(), v); } |
| 105 | explicit Value(const bool v) { buf[0] = v ? 1 : 0; } |
| 106 | operator const Param_value_list::value_type &() const { return buf; } |
| 107 | |
| 108 | private: |
| 109 | Param_value_list::value_type buf{{0}}; |
| 110 | }; |
| 111 | |
| 112 | } // namespace |
| 113 | |
| 114 | struct Param_prepare_parameters { |
| 115 | int expect_error_code; |
| 116 | Param_list expect_params; |
| 117 | Param_value_list expect_param_values; |
| 118 | Param_svalue_list expect_param_svalues; |
| 119 | Arg_list args; |
| 120 | Placeholder_list phs; |
| 121 | }; |
| 122 | |
| 123 | #define NLL \ |
| 124 | { true, MYSQL_TYPE_NULL, false, nullptr, 0ul, nullptr, 0ul } |
| 125 | #define SIN \ |
| 126 | { false, MYSQL_TYPE_LONGLONG, false, nullptr, sizeof(int64_t), nullptr, 0ul } |
| 127 | #define UIN \ |
| 128 | { false, MYSQL_TYPE_LONGLONG, true, nullptr, sizeof(uint64_t), nullptr, 0ul } |
| 129 | #define STR(len) \ |
| 130 | { false, MYSQL_TYPE_STRING, false, nullptr, len, nullptr, 0ul } |
| 131 | #define DBL \ |
| 132 | { false, MYSQL_TYPE_DOUBLE, false, nullptr, sizeof(double), nullptr, 0ul } |
| 133 | #define FLT \ |
| 134 | { false, MYSQL_TYPE_FLOAT, false, nullptr, sizeof(float), nullptr, 0ul } |
| 135 | #define BOL \ |
| 136 | { false, MYSQL_TYPE_TINY, false, nullptr, 1ul, nullptr, 0ul } |
| 137 | |
| 138 | #define RAW(id) \ |
| 139 | { id, Placeholder_info::Type::k_raw } |
| 140 | #define JSN(id) \ |
| 141 | { id, Placeholder_info::Type::k_json } |
| 142 | |
| 143 | using Octets = Scalar::Octets; |
| 144 | using String = Scalar::String; |
| 145 | |
| 146 | Param_prepare_parameters prepare_parameters_param[] = { |
| 147 | {ER_X_SUCCESS, {}, {}, {}, {}, {}}, |
| 148 | {ER_X_SUCCESS, {NLL}, {}, {}, Any_list{Scalar::Null()}, {RAW(0)}}, |
| 149 | {ER_X_SUCCESS, {STR(4)}, {}, {"null"}, Any_list{Scalar::Null()}, {JSN(0)}}, |
| 150 | {ER_X_SUCCESS, {SIN}, {Value(-1)}, {}, Any_list{Scalar(-1)}, {RAW(0)}}, |
| 151 | {ER_X_SUCCESS, {SIN}, {Value(-1)}, {}, Any_list{Scalar(-1)}, {JSN(0)}}, |
| 152 | {ER_X_SUCCESS, {UIN}, {Value(1u)}, {}, Any_list{Scalar(1u)}, {RAW(0)}}, |
| 153 | {ER_X_SUCCESS, {UIN}, {Value(1u)}, {}, Any_list{Scalar(1u)}, {JSN(0)}}, |
| 154 | {ER_X_SUCCESS, {STR(3)}, {}, {}, Any_list{String("abc")}, {RAW(0)}}, |
| 155 | {ER_X_SUCCESS, |
| 156 | {STR(5)}, |
| 157 | {}, |
| 158 | {"\"abc\""}, |
| 159 | Any_list{String("abc")}, |
| 160 | {JSN(0)}}, |
| 161 | {ER_X_SUCCESS, {STR(3)}, {}, {}, Any_list{Octets("abc")}, {RAW(0)}}, |
| 162 | {ER_X_SUCCESS, |
| 163 | {STR(3)}, |
| 164 | {}, |
| 165 | {}, |
| 166 | Any_list{Octets("abc", Octets::Content_type::k_json)}, |
| 167 | {RAW(0)}}, |
| 168 | {ER_X_SUCCESS, |
| 169 | {STR(5)}, |
| 170 | {}, |
| 171 | {"\"abc\""}, |
| 172 | Any_list{Octets("abc")}, |
| 173 | {JSN(0)}}, |
| 174 | {ER_X_SUCCESS, |
| 175 | {STR(3)}, |
| 176 | {}, |
| 177 | {"abc"}, |
| 178 | Any_list{Octets("abc", Octets::Content_type::k_json)}, |
| 179 | {JSN(0)}}, |
| 180 | {ER_X_SUCCESS, {DBL}, {Value(1.1)}, {}, Any_list{Scalar(1.1)}, {RAW(0)}}, |
| 181 | {ER_X_SUCCESS, {DBL}, {Value(1.1)}, {}, Any_list{Scalar(1.1)}, {JSN(0)}}, |
| 182 | {ER_X_SUCCESS, {FLT}, {Value(1.1f)}, {}, Any_list{Scalar(1.1f)}, {RAW(0)}}, |
| 183 | {ER_X_SUCCESS, {FLT}, {Value(1.1f)}, {}, Any_list{Scalar(1.1f)}, {JSN(0)}}, |
| 184 | {ER_X_SUCCESS, {BOL}, {Value(true)}, {}, Any_list{Scalar(true)}, {RAW(0)}}, |
| 185 | {ER_X_SUCCESS, |
| 186 | {BOL}, |
| 187 | {Value(false)}, |
| 188 | {}, |
| 189 | Any_list{Scalar(false)}, |
| 190 | {RAW(0)}}, |
| 191 | {ER_X_SUCCESS, {STR(4)}, {}, {"true"}, Any_list{Scalar(true)}, {JSN(0)}}, |
| 192 | {ER_X_SUCCESS, {STR(5)}, {}, {"false"}, Any_list{Scalar(false)}, {JSN(0)}}, |
| 193 | {ER_X_SUCCESS, |
| 194 | {UIN, SIN}, |
| 195 | {Value(2u), Value(1)}, |
| 196 | {}, |
| 197 | Any_list{Scalar(2u), Scalar(1)}, |
| 198 | {RAW(0), RAW(1)}}, |
| 199 | {ER_X_SUCCESS, |
| 200 | {SIN, UIN}, |
| 201 | {Value(1), Value(2u)}, |
| 202 | {}, |
| 203 | Any_list{Scalar(2u), Scalar(1)}, |
| 204 | {RAW(1), RAW(0)}}, |
| 205 | {ER_X_SUCCESS, |
| 206 | {SIN, SIN, SIN}, |
| 207 | {Value(1), Value(1), Value(1)}, |
| 208 | {}, |
| 209 | Any_list{Scalar(1)}, |
| 210 | {RAW(0), RAW(0), RAW(0)}}, |
| 211 | {ER_X_SUCCESS, |
| 212 | {NLL, SIN, NLL}, |
| 213 | {Value(1)}, |
| 214 | {}, |
| 215 | Any_list{Scalar::Null(), Scalar(1)}, |
| 216 | {RAW(0), RAW(1), RAW(0)}}, |
| 217 | {ER_X_SUCCESS, |
| 218 | {NLL, STR(2), STR(3)}, |
| 219 | {}, |
| 220 | {}, |
| 221 | Any_list{Scalar::String("ab"), Scalar::Octets("abc"), Scalar::Null()}, |
| 222 | {RAW(2), RAW(0), RAW(1)}}, |
| 223 | {ER_X_PREPARED_EXECUTE_ARGUMENT_NOT_SUPPORTED, |
| 224 | {}, |
| 225 | {}, |
| 226 | {}, |
| 227 | Any_list{Any::Object()}, |
| 228 | {RAW(0)}}, |
| 229 | {ER_X_PREPARED_EXECUTE_ARGUMENT_NOT_SUPPORTED, |
| 230 | {}, |
| 231 | {}, |
| 232 | {}, |
| 233 | Any_list{Any::Array()}, |
| 234 | {RAW(0)}}, |
| 235 | {ER_X_SUCCESS, |
| 236 | {SIN}, |
| 237 | {Value(1)}, |
| 238 | {}, |
| 239 | Any_list{Scalar(1), Any::Array()}, |
| 240 | {RAW(0)}}, |
| 241 | {ER_X_SUCCESS, |
| 242 | {BOL, STR(4)}, |
| 243 | {Value(true)}, |
| 244 | {"true"}, |
| 245 | Any_list{Scalar(true)}, |
| 246 | {RAW(0), JSN(0)}}, |
| 247 | }; |
| 248 | |
| 249 | class Prepare_command_handler_prepare_parameters_test |
| 250 | : public Prepare_param_handler_base_test, |
| 251 | public testing::WithParamInterface<Param_prepare_parameters> {}; |
| 252 | |
| 253 | MATCHER(Eq_param, "") { |
| 254 | using ::testing::get; |
| 255 | // intentionally skip comparison of "value" value; |
| 256 | return get<0>(arg).null_bit == get<1>(arg).null_bit && |
| 257 | get<0>(arg).type == get<1>(arg).type && |
| 258 | get<0>(arg).unsigned_type == get<1>(arg).unsigned_type && |
| 259 | get<0>(arg).length == get<1>(arg).length; |
| 260 | } |
| 261 | |
| 262 | TEST_P(Prepare_command_handler_prepare_parameters_test, prepare_parameters) { |
| 263 | const auto ¶m = GetParam(); |
| 264 | m_placeholders = param.phs; |
| 265 | ASSERT_ERROR_CODE(param.expect_error_code, |
| 266 | m_handler.prepare_parameters(param.args)); |
| 267 | EXPECT_THAT(m_handler.get_params(), |
| 268 | testing::Pointwise(Eq_param(), param.expect_params)); |
| 269 | EXPECT_EQ(param.expect_param_svalues, m_handler.get_string_values()); |
| 270 | EXPECT_EQ(param.expect_param_values, m_handler.get_values()); |
| 271 | } |
| 272 | |
| 273 | INSTANTIATE_TEST_SUITE_P(Prepare_command_handler, |
| 274 | Prepare_command_handler_prepare_parameters_test, |
| 275 | testing::ValuesIn(prepare_parameters_param)); |
| 276 | |
| 277 | } // namespace test |
| 278 | } // namespace xpl |