| 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 <string> |
| 25 | #include <vector> |
| 26 | |
| 27 | #include <gmock/gmock.h> |
| 28 | #include <gtest/gtest.h> |
| 29 | |
| 30 | #include "plugin/ddl_rewriter/ddl_rewriter.h" |
| 31 | |
| 32 | namespace ddl_rewriter_unittest { |
| 33 | |
| 34 | class DDL_rewriter_test : public ::testing::Test { |
| 35 | public: |
| 36 | DDL_rewriter_test() = default; |
| 37 | ~DDL_rewriter_test() = default; |
| 38 | }; |
| 39 | |
| 40 | struct Query_pair { |
| 41 | const std::string query; |
| 42 | const std::string rewritten_query; |
| 43 | }; |
| 44 | |
| 45 | std::vector<Query_pair> basic_queries{ |
| 46 | {"CREATE TABLE t(i int)", "CREATE TABLE t(i int)"}, |
| 47 | {"CREATE TABLE t(i int) ENCRYPTION='N'", "CREATE TABLE t(i int) "}, |
| 48 | {"CREATE TABLE t(i int)\n" |
| 49 | "ENCRYPTION='N'\n" |
| 50 | "ENGINE=MyISAM", |
| 51 | "CREATE TABLE t(i int) ENGINE=MyISAM"}}; |
| 52 | |
| 53 | std::vector<Query_pair> case_sensitivity_queries{ |
| 54 | {"create TABLE t(i int) ENCRYPTION = 'N'", "create TABLE t(i int) "}, |
| 55 | {"CREATE TABLE t(i int) ENCRYPTION = 'N'", "CREATE TABLE t(i int) "}, |
| 56 | {"CREATE TABLE t(i int) encryption = 'N'", "CREATE TABLE t(i int) "}, |
| 57 | {"CREATE TABLE t(i int) ENCRYPTION = 'n'", "CREATE TABLE t(i int) "}, |
| 58 | {"CREATE TABLE t(i int) ENCRYPTION = 'Y'", "CREATE TABLE t(i int) "}, |
| 59 | {"CREATE TABLE t(i int) ENCRYPTION = 'y'", "CREATE TABLE t(i int) "}, |
| 60 | |
| 61 | {"create TABLE t(i int) DATA DIRECTORY = '\\tmp'", |
| 62 | "create TABLE t(i int) "}, |
| 63 | {"CREATE TABLE t(i int) DATA DIRECTORY = '\\tmp'", |
| 64 | "CREATE TABLE t(i int) "}, |
| 65 | {"CREATE TABLE t(i int) data directory = '\\tmp'", |
| 66 | "CREATE TABLE t(i int) "}, |
| 67 | {"CREATE TABLE t(i int) DATA DIRECTORY = '\\tmp'", |
| 68 | "CREATE TABLE t(i int) "}, |
| 69 | |
| 70 | {"create TABLE t(i int) INDEX DIRECTORY = '\\tmp'", |
| 71 | "create TABLE t(i int) "}, |
| 72 | {"CREATE TABLE t(i int) INDEX DIRECTORY = '\\tmp'", |
| 73 | "CREATE TABLE t(i int) "}, |
| 74 | {"CREATE TABLE t(i int) index directory = '\\tmp'", |
| 75 | "CREATE TABLE t(i int) "}, |
| 76 | {"CREATE TABLE t(i int) INDEX DIRECTORY = '\\tmp'", |
| 77 | "CREATE TABLE t(i int) "}}; |
| 78 | |
| 79 | std::vector<Query_pair> quoting_queries{ |
| 80 | {"CREATE TABLE t(i int) ENCRYPTION = 'N'", "CREATE TABLE t(i int) "}, |
| 81 | {"CREATE TABLE t(i int) ENCRYPTION = \"N\"", "CREATE TABLE t(i int) "}, |
| 82 | |
| 83 | {"CREATE TABLE t(i int) DATA DIRECTORY = '\\tmp'", |
| 84 | "CREATE TABLE t(i int) "}, |
| 85 | {"CREATE TABLE t(i int) DATA DIRECTORY = \"\\tmp\"", |
| 86 | "CREATE TABLE t(i int) "}, |
| 87 | |
| 88 | {"CREATE TABLE t(i int) INDEX DIRECTORY = '\\tmp'", |
| 89 | "CREATE TABLE t(i int) "}, |
| 90 | {"CREATE TABLE t(i int) INDEX DIRECTORY = \"\\tmp\"", |
| 91 | "CREATE TABLE t(i int) "}}; |
| 92 | |
| 93 | std::vector<Query_pair> clause_separation_queries{ |
| 94 | {"CREATE TABLE t(i int) ENCRYPTION = 'N' DATA DIRECTORY = '\\tmp' INDEX " |
| 95 | "DIRECTORY = '\\tmp'", |
| 96 | "CREATE TABLE t(i int) "}, |
| 97 | {"CREATE TABLE t(i int) ENCRYPTION = 'N' DATA DIRECTORY = '\\tmp' INDEX " |
| 98 | "DIRECTORY = '\\tmp'", |
| 99 | "CREATE TABLE t(i int) "}, |
| 100 | {"CREATE TABLE t(i int) ENCRYPTION = 'N',DATA DIRECTORY = '\\tmp',INDEX " |
| 101 | "DIRECTORY = '\\tmp'", |
| 102 | "CREATE TABLE t(i int) "}, |
| 103 | {"CREATE TABLE t(i int) ENCRYPTION = 'N' , DATA DIRECTORY = '\\tmp' , " |
| 104 | "INDEX DIRECTORY = '\\tmp'", |
| 105 | "CREATE TABLE t(i int) "}, |
| 106 | {"CREATE TABLE t(i int) ENCRYPTION = 'N' , DATA DIRECTORY = '\\tmp' , " |
| 107 | "INDEX DIRECTORY = '\\tmp'", |
| 108 | "CREATE TABLE t(i int) "}}; |
| 109 | |
| 110 | std::vector<Query_pair> clause_assignment_queries{ |
| 111 | {"CREATE TABLE t(i int) ENCRYPTION 'N'", "CREATE TABLE t(i int) "}, |
| 112 | {"CREATE TABLE t(i int) ENCRYPTION 'N'", "CREATE TABLE t(i int) "}, |
| 113 | {"CREATE TABLE t(i int) ENCRYPTION='N'", "CREATE TABLE t(i int) "}, |
| 114 | {"CREATE TABLE t(i int) ENCRYPTION ='N'", "CREATE TABLE t(i int) "}, |
| 115 | {"CREATE TABLE t(i int) ENCRYPTION= 'N'", "CREATE TABLE t(i int) "}, |
| 116 | {"CREATE TABLE t(i int) ENCRYPTION = 'N'", "CREATE TABLE t(i int) "}, |
| 117 | {"CREATE TABLE t(i int) ENCRYPTION = 'N'", "CREATE TABLE t(i int) "}}; |
| 118 | |
| 119 | std::vector<Query_pair> multi_line_queries{ |
| 120 | {"CREATE TABLE t(i int)\n" |
| 121 | "ENCRYPTION = 'N' \n" |
| 122 | "DATA DIRECTORY = '\\tmp' INDEX DIRECTORY = '\\tmp',\n" |
| 123 | "ENGINE = MyISAM", |
| 124 | "CREATE TABLE t(i int) ENGINE = MyISAM"}, |
| 125 | {"CREATE TABLE t(i int)\n" |
| 126 | "\n" |
| 127 | "ENCRYPTION = 'N' \n" |
| 128 | ",\n" |
| 129 | "DATA DIRECTORY\n" |
| 130 | "=\n" |
| 131 | "'\\tm\n" |
| 132 | "p', INDEX\n" |
| 133 | "DIRECTORY =\n" |
| 134 | "'\\tmp',\n" |
| 135 | "ENGINE = MyISAM", |
| 136 | "CREATE TABLE t(i int) ENGINE = MyISAM"}}; |
| 137 | |
| 138 | std::vector<Query_pair> partitioned_table_queries{ |
| 139 | {"CREATE TABLE t (i int PRIMARY KEY) ENGINE = InnoDB\n" |
| 140 | " DATA DIRECTORY = '/tmp'\n" |
| 141 | " ENCRYPTION = 'y'\n" |
| 142 | " PARTITION BY LIST (i) (\n" |
| 143 | " PARTITION p0 VALUES IN (0) ENGINE = InnoDB,\n" |
| 144 | " PARTITION p1 VALUES IN (1)\n" |
| 145 | " DATA DIRECTORY = '/tmp',\n" |
| 146 | " ENGINE = InnoDB\n" |
| 147 | " )", |
| 148 | "CREATE TABLE t (i int PRIMARY KEY) ENGINE = InnoDB PARTITION BY LIST (i) " |
| 149 | "(\n" |
| 150 | " PARTITION p0 VALUES IN (0) ENGINE = InnoDB,\n" |
| 151 | " PARTITION p1 VALUES IN (1) ENGINE = InnoDB\n" |
| 152 | " )"}}; |
| 153 | |
| 154 | std::vector<Query_pair> false_pos_queries{ |
| 155 | {"CREATE TABLE t(i int) /* ENCRYPTION='N' */", |
| 156 | "CREATE TABLE t(i int) /* */"}, |
| 157 | {"CREATE TABLE t(`i ENCRYPTION='N' i` int)", "CREATE TABLE t(`i i` int)"}}; |
| 158 | |
| 159 | std::vector<Query_pair> missing_rewrite_queries{ |
| 160 | {" CREATE TABLE t(i int) ENCRYPTION='N'", |
| 161 | " CREATE TABLE t(i int) ENCRYPTION='N'"}}; |
| 162 | |
| 163 | void test_queries(const std::vector<Query_pair> &queries) { |
| 164 | for (auto &q : queries) { |
| 165 | std::string rewrite_result; |
| 166 | bool expect_rewrite = (q.query != q.rewritten_query); |
| 167 | EXPECT_EQ(query_rewritten(q.query, &rewrite_result), expect_rewrite); |
| 168 | if (expect_rewrite) { |
| 169 | EXPECT_EQ(rewrite_result, q.rewritten_query); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | TEST_F(DDL_rewriter_test, DDLQueryTest) { |
| 175 | test_queries(basic_queries); |
| 176 | test_queries(case_sensitivity_queries); |
| 177 | test_queries(quoting_queries); |
| 178 | test_queries(clause_separation_queries); |
| 179 | test_queries(clause_assignment_queries); |
| 180 | test_queries(multi_line_queries); |
| 181 | test_queries(partitioned_table_queries); |
| 182 | test_queries(missing_rewrite_queries); |
| 183 | test_queries(false_pos_queries); |
| 184 | } |
| 185 | |
| 186 | } // namespace ddl_rewriter_unittest |