| 1 | /* |
| 2 | * Copyright (c) 2015, 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 <gtest/gtest.h> |
| 27 | #include <list> |
| 28 | #include <stdexcept> |
| 29 | |
| 30 | #include "plugin/x/src/ngs/error_code.h" |
| 31 | #include "plugin/x/src/query_string_builder.h" |
| 32 | |
| 33 | namespace xpl { |
| 34 | |
| 35 | namespace test { |
| 36 | |
| 37 | namespace { |
| 38 | |
| 39 | class Assign_list { |
| 40 | public: |
| 41 | typedef std::string Value_type; |
| 42 | typedef std::list<Value_type> Container_type; |
| 43 | |
| 44 | explicit Assign_list(const Value_type &value) { (*this)(value); } |
| 45 | |
| 46 | Assign_list &operator()(const Value_type &value) { |
| 47 | m_values.push_back(value); |
| 48 | return *this; |
| 49 | } |
| 50 | |
| 51 | operator Container_type() { return m_values; } |
| 52 | |
| 53 | private: |
| 54 | Container_type m_values; |
| 55 | }; |
| 56 | |
| 57 | } // namespace |
| 58 | |
| 59 | class Query_string_builder_testsuite : public ::testing::Test { |
| 60 | public: |
| 61 | Query_string_builder query; |
| 62 | }; |
| 63 | |
| 64 | TEST_F(Query_string_builder_testsuite, |
| 65 | format_doesNothing_whenEmptyStringAndNoArgsPushed) { |
| 66 | query.format(); |
| 67 | |
| 68 | ASSERT_STREQ("", query.get().c_str()); |
| 69 | } |
| 70 | |
| 71 | TEST_F(Query_string_builder_testsuite, |
| 72 | format_doesThrowsException_whenEmptyFormat) { |
| 73 | ASSERT_THROW(query.format() % "Test", ngs::Error_code); |
| 74 | |
| 75 | ASSERT_STREQ("", query.get().c_str()); |
| 76 | } |
| 77 | |
| 78 | TEST_F(Query_string_builder_testsuite, |
| 79 | format_doesThrowsException_whenNoTagFormat) { |
| 80 | const char *format_query = "QUERY WITHOUT TAG"; |
| 81 | |
| 82 | query.put(format_query); |
| 83 | ASSERT_THROW(query.format() % "Test", ngs::Error_code); |
| 84 | |
| 85 | ASSERT_STREQ(format_query, query.get().c_str()); |
| 86 | } |
| 87 | |
| 88 | TEST_F(Query_string_builder_testsuite, |
| 89 | format_doesThrowsException_whenAfterFillingFirstTag) { |
| 90 | query.put("SELECT ? FROM table").format() % "test"; |
| 91 | |
| 92 | ASSERT_THROW(query.format() % "test1", ngs::Error_code); |
| 93 | ASSERT_THROW(query.format() % "test2", ngs::Error_code); |
| 94 | |
| 95 | ASSERT_STREQ("SELECT 'test' FROM table", query.get().c_str()); |
| 96 | } |
| 97 | |
| 98 | TEST_F(Query_string_builder_testsuite, |
| 99 | format_fillsTwoTagsInQueryAndSkipsTagInValue_whenTwoTagValue) { |
| 100 | query.put("SELECT ? FROM ?"); |
| 101 | query.format() % "?" % "test"; |
| 102 | |
| 103 | ASSERT_STREQ("SELECT '?' FROM 'test'", query.get().c_str()); |
| 104 | } |
| 105 | |
| 106 | TEST_F(Query_string_builder_testsuite, format_fillNumericValues) { |
| 107 | query.put("SELECT *,? FROM t WHERE x=?").format() % 1 % 1.1; |
| 108 | |
| 109 | ASSERT_STREQ("SELECT *,1 FROM t WHERE x=1.1", query.get().c_str()); |
| 110 | } |
| 111 | |
| 112 | struct Query_and_expected { |
| 113 | Query_and_expected(std::string query, std::string expected, std::string value) |
| 114 | : m_query(query), m_expected(expected), m_value(value) {} |
| 115 | |
| 116 | std::string m_query; |
| 117 | std::string m_expected; |
| 118 | std::string m_value; |
| 119 | }; |
| 120 | |
| 121 | ::std::ostream &operator<<(::std::ostream &os, |
| 122 | const Query_and_expected &query_and_expected) { |
| 123 | return os << "Query:" << query_and_expected.m_query |
| 124 | << " expected:" << query_and_expected.m_expected |
| 125 | << " value:" << query_and_expected.m_value << std::endl; |
| 126 | } |
| 127 | |
| 128 | class Query_string_builder_param_testsuite |
| 129 | : public Query_string_builder_testsuite, |
| 130 | public ::testing::WithParamInterface<Query_and_expected> { |
| 131 | public: |
| 132 | }; |
| 133 | |
| 134 | TEST_P(Query_string_builder_param_testsuite, |
| 135 | format_putStringValueInsideQuery_whenOneTagInFormat) { |
| 136 | const char *value = GetParam().m_value.c_str(); |
| 137 | const char *format_query = GetParam().m_query.c_str(); |
| 138 | const char *expected_query = GetParam().m_expected.c_str(); |
| 139 | |
| 140 | query.put(format_query).format() % value; |
| 141 | |
| 142 | ASSERT_STREQ(expected_query, query.get().c_str()); |
| 143 | } |
| 144 | |
| 145 | INSTANTIATE_TEST_SUITE_P( |
| 146 | InstantiationPositiveTest, Query_string_builder_param_testsuite, |
| 147 | ::testing::Values( |
| 148 | Query_and_expected("SELECT ? FROM", "SELECT 'Test' FROM", "Test"), |
| 149 | Query_and_expected("SELECT Next,?", "SELECT Next,'FROM'", "FROM"), |
| 150 | Query_and_expected("?,Back,FROM", "'Select',Back,FROM", "Select"), |
| 151 | Query_and_expected("SELECT Next ?", "SELECT Next ''", ""), |
| 152 | Query_and_expected("SELECT ? From", "SELECT '' From", ""), |
| 153 | Query_and_expected("? From", "'' From", ""), |
| 154 | Query_and_expected("? From", |
| 155 | "'\\'first-word\\' ; \\\"second-word\\\"' From", |
| 156 | "'first-word' ; \"second-word\""))); |
| 157 | |
| 158 | struct Query_and_expected_values { |
| 159 | Query_and_expected_values(std::string query, std::string expected, |
| 160 | std::list<std::string> values) |
| 161 | : m_query(query), m_expected(expected), m_values(values) {} |
| 162 | |
| 163 | std::string m_query; |
| 164 | std::string m_expected; |
| 165 | std::list<std::string> m_values; |
| 166 | }; |
| 167 | |
| 168 | ::std::ostream &operator<<( |
| 169 | ::std::ostream &os, const Query_and_expected_values &query_and_expected) { |
| 170 | os << "Query:" << query_and_expected.m_query |
| 171 | << " expected:" << query_and_expected.m_expected << std::endl |
| 172 | << " ["; |
| 173 | |
| 174 | std::ostream_iterator<std::string> out_it(os, ", "); |
| 175 | std::copy(query_and_expected.m_values.begin(), |
| 176 | query_and_expected.m_values.end(), out_it); |
| 177 | |
| 178 | return os << "]" << std::endl; |
| 179 | } |
| 180 | |
| 181 | class Query_string_builder_multiple_tags_param_testsuite |
| 182 | : public Query_string_builder_testsuite, |
| 183 | public ::testing::WithParamInterface<Query_and_expected_values> { |
| 184 | public: |
| 185 | void SetUp() override { |
| 186 | values = GetParam().m_values; |
| 187 | expected_query = &GetParam().m_expected[0]; |
| 188 | |
| 189 | query.put(GetParam().m_query.c_str()); |
| 190 | } |
| 191 | |
| 192 | std::list<std::string> values; |
| 193 | std::string expected_query; |
| 194 | }; |
| 195 | |
| 196 | TEST_P(Query_string_builder_multiple_tags_param_testsuite, |
| 197 | format_putStringValueInsideQuery_whenMultipleTagsInFormat) { |
| 198 | std::list<std::string>::const_iterator i = values.begin(); |
| 199 | |
| 200 | // In each iteration format creates new object without previous state |
| 201 | // Thus similar test are needed with one call/multiple args |
| 202 | for (; i != values.end(); ++i) query.format() % *i; |
| 203 | |
| 204 | ASSERT_STREQ(expected_query.c_str(), query.get().c_str()); |
| 205 | } |
| 206 | |
| 207 | INSTANTIATE_TEST_SUITE_P( |
| 208 | InstantiationPositiveTests, |
| 209 | Query_string_builder_multiple_tags_param_testsuite, |
| 210 | ::testing::Values( |
| 211 | Query_and_expected_values("SELECT ?,?,? FROM", |
| 212 | "SELECT 'First','Second','Third' FROM", |
| 213 | Assign_list("First")("Second")("Third")), |
| 214 | Query_and_expected_values("SELECT ?,? FROM", |
| 215 | "SELECT 'First','Second' FROM", |
| 216 | Assign_list("First")("Second")), |
| 217 | Query_and_expected_values("SELECT ? FROM ?", |
| 218 | "SELECT 'Second' FROM 'First'", |
| 219 | Assign_list("Second")("First")), |
| 220 | Query_and_expected_values("SELECT ?? FROM t", |
| 221 | "SELECT 'Second''First' FROM t", |
| 222 | Assign_list("Second")("First")), |
| 223 | Query_and_expected_values("SELECT ? FROM ?", "SELECT '?' FROM 'First'", |
| 224 | Assign_list("?")("First")))); |
| 225 | |
| 226 | INSTANTIATE_TEST_SUITE_P( |
| 227 | InstantiationPositiveTestsQueryOrValuesWithEscapedChars, |
| 228 | Query_string_builder_multiple_tags_param_testsuite, |
| 229 | ::testing::Values( |
| 230 | Query_and_expected_values("SELECT ?/*,?*/ FROM t WHERE ?", |
| 231 | "SELECT '\\\"'/*,?*/ FROM t WHERE 'First'", |
| 232 | Assign_list("\"")("First")), |
| 233 | Query_and_expected_values("SELECT ?,? FROM t", |
| 234 | "SELECT '\\'','First' FROM t", |
| 235 | Assign_list("'")("First")), |
| 236 | Query_and_expected_values("SELECT ?,? FROM t", |
| 237 | "SELECT '\\\"','First' FROM t", |
| 238 | Assign_list("\"")("First")), |
| 239 | Query_and_expected_values("SELECT \"?\'?'?\",?,? FROM t", |
| 240 | "SELECT \"?\'?'?\",'','First' FROM t", |
| 241 | Assign_list("")("First")))); |
| 242 | |
| 243 | INSTANTIATE_TEST_SUITE_P( |
| 244 | InstantiationPositiveTestsQueryWithComment, |
| 245 | Query_string_builder_multiple_tags_param_testsuite, |
| 246 | ::testing::Values( |
| 247 | Query_and_expected_values("SELECT ?/*,?*/ FROM t WHERE ?", |
| 248 | "SELECT '\\\"'/*,?*/ FROM t WHERE 'First'", |
| 249 | Assign_list("\"")("First")), |
| 250 | Query_and_expected_values("SELECT ?#,?*\n FROM t WHERE ?", |
| 251 | "SELECT '\\\"'#,?*\n FROM t WHERE 'First'", |
| 252 | Assign_list("\"")("First")), |
| 253 | Query_and_expected_values("SELECT ?-- ,?*\n FROM t WHERE ?", |
| 254 | "SELECT '\\\"'-- ,?*\n FROM t WHERE 'First'", |
| 255 | Assign_list("\"")("First")), |
| 256 | Query_and_expected_values( |
| 257 | "SELECT ?-- ,?*\n FROM -- ?\nt WHERE ?", |
| 258 | "SELECT '\\\"'-- ,?*\n FROM -- ?\nt WHERE 'First'", |
| 259 | Assign_list("\"")("First")), |
| 260 | Query_and_expected_values( |
| 261 | "SELECT ?-- ,?*\n FROM # ?\nt WHERE ?", |
| 262 | "SELECT '\\\"'-- ,?*\n FROM # ?\nt WHERE 'First'", |
| 263 | Assign_list("\"")("First")), |
| 264 | Query_and_expected_values( |
| 265 | "SELECT ?-- ,?*\n FROM /* ?*/t WHERE ?", |
| 266 | "SELECT '\\\"'-- ,?*\n FROM /* ?*/t WHERE 'First'", |
| 267 | Assign_list("\"")("First")))); |
| 268 | |
| 269 | class Query_string_builder_multiple_too_many_tags_param_testsuite |
| 270 | : public Query_string_builder_multiple_tags_param_testsuite {}; |
| 271 | |
| 272 | TEST_P(Query_string_builder_multiple_too_many_tags_param_testsuite, |
| 273 | format_putStringValueInsideQuery_whenMultipleTagsInFormat) { |
| 274 | std::list<std::string>::const_iterator i = values.begin(); |
| 275 | |
| 276 | ASSERT_LT(0u, values.size()); |
| 277 | |
| 278 | // In each iteration format creates new object without previous state |
| 279 | // Thus similar test are needed with one call/multiple args |
| 280 | for (; &*i != &values.back(); ++i) query.format() % *i; |
| 281 | |
| 282 | ASSERT_THROW(query.format() % values.back(), ngs::Error_code); |
| 283 | } |
| 284 | |
| 285 | INSTANTIATE_TEST_SUITE_P( |
| 286 | InstantiationNegativeTest, |
| 287 | Query_string_builder_multiple_too_many_tags_param_testsuite, |
| 288 | ::testing::Values( |
| 289 | Query_and_expected_values("SELECT ?,? FROM", "", |
| 290 | Assign_list("First")("Second")("Third")), |
| 291 | Query_and_expected_values("SELECT ? FROM", "", |
| 292 | Assign_list("First")("Second")))); |
| 293 | |
| 294 | } // namespace test |
| 295 | |
| 296 | } // namespace xpl |