| 1 | /* |
| 2 | Copyright (c) 2013, 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 | /** Unit test case for the function explain_filename(). */ |
| 27 | #include "my_config.h" |
| 28 | |
| 29 | #include <gtest/gtest.h> |
| 30 | |
| 31 | #include "mysqld_error.h" |
| 32 | #include "sql/derror.h" |
| 33 | #include "sql/sql_class.h" |
| 34 | #include "sql/sql_locale.h" |
| 35 | #include "sql/sql_table.h" |
| 36 | |
| 37 | namespace explain_filename_unittest { |
| 38 | |
| 39 | const int BUFLEN = 1000; |
| 40 | |
| 41 | char to[BUFLEN]; |
| 42 | char from[BUFLEN]; |
| 43 | |
| 44 | class PartitionTest : public ::testing::Test { |
| 45 | protected: |
| 46 | void SetUp() override { |
| 47 | // Save global settings. |
| 48 | m_charset = system_charset_info; |
| 49 | m_locale = my_default_lc_messages; |
| 50 | |
| 51 | system_charset_info = &my_charset_utf8mb3_bin; |
| 52 | my_default_lc_messages = &my_locale_en_US; |
| 53 | |
| 54 | /* Populate the necessary error messages */ |
| 55 | MY_LOCALE_ERRMSGS *errmsgs = my_default_lc_messages->errmsgs; |
| 56 | EXPECT_FALSE(errmsgs->replace_msg(ER_DATABASE_NAME, "Database")); |
| 57 | EXPECT_FALSE(errmsgs->replace_msg(ER_TABLE_NAME, "Table")); |
| 58 | EXPECT_FALSE(errmsgs->replace_msg(ER_PARTITION_NAME, "Partition")); |
| 59 | EXPECT_FALSE(errmsgs->replace_msg(ER_SUBPARTITION_NAME, "Subpartition")); |
| 60 | EXPECT_FALSE(errmsgs->replace_msg(ER_TEMPORARY_NAME, "Temporary")); |
| 61 | EXPECT_FALSE(errmsgs->replace_msg(ER_RENAMED_NAME, "Renamed")); |
| 62 | } |
| 63 | |
| 64 | void TearDown() override { |
| 65 | // Restore global settings. |
| 66 | system_charset_info = m_charset; |
| 67 | my_default_lc_messages = m_locale; |
| 68 | } |
| 69 | |
| 70 | private: |
| 71 | CHARSET_INFO *m_charset; |
| 72 | MY_LOCALE *m_locale; |
| 73 | }; |
| 74 | |
| 75 | void test_1(const char *in, const char *exp, enum_explain_filename_mode mode) { |
| 76 | char out[BUFLEN]; |
| 77 | |
| 78 | size_t len1 = explain_filename(nullptr, in, out, BUFLEN, mode); |
| 79 | |
| 80 | /* expected output and actual output must be same */ |
| 81 | bool pass = (strcmp(exp, out) == 0); |
| 82 | |
| 83 | /* length returned by explain_filename is fine */ |
| 84 | bool length = (len1 == strlen(exp)); |
| 85 | |
| 86 | EXPECT_EQ((pass && length), true); |
| 87 | if (pass && length) { |
| 88 | // pass |
| 89 | } else { |
| 90 | ADD_FAILURE() << "input file name: '" << in << "' explain output: '" << out |
| 91 | << "'" << std::endl; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | TEST_F(PartitionTest, ExplainFilename) { |
| 96 | test_1("test/t1.ibd", "Database `test`, Table `t1.ibd`", EXPLAIN_ALL_VERBOSE); |
| 97 | |
| 98 | test_1("test/t1.ibd", "`test`.`t1.ibd`", EXPLAIN_PARTITIONS_VERBOSE); |
| 99 | |
| 100 | test_1("test/t1.ibd", "`test`.`t1.ibd`", EXPLAIN_PARTITIONS_AS_COMMENT); |
| 101 | |
| 102 | test_1("test/t1#TMP#", "Database `test`, Table `t1#TMP#`", |
| 103 | EXPLAIN_ALL_VERBOSE); |
| 104 | |
| 105 | test_1("test/#sql-2882.ibd", "Database `test`, Table `#sql-2882.ibd`", |
| 106 | EXPLAIN_ALL_VERBOSE); |
| 107 | |
| 108 | test_1("test/t1#REN#", "Database `test`, Table `t1#REN#`", |
| 109 | EXPLAIN_ALL_VERBOSE); |
| 110 | |
| 111 | test_1("test/t1@0023REN@0023", "Database `test`, Table `t1#REN#`", |
| 112 | EXPLAIN_ALL_VERBOSE); |
| 113 | |
| 114 | test_1("test/t1#p#p1", "Database `test`, Table `t1`, Partition `p1`", |
| 115 | EXPLAIN_ALL_VERBOSE); |
| 116 | |
| 117 | test_1("test/t1#P#p1", "`test`.`t1` /* Partition `p1` */", |
| 118 | EXPLAIN_PARTITIONS_AS_COMMENT); |
| 119 | |
| 120 | test_1("test/t1#P#p1@00231", "`test`.`t1` /* Partition `p1#1` */", |
| 121 | EXPLAIN_PARTITIONS_AS_COMMENT); |
| 122 | |
| 123 | test_1("test/t1#P#p1#SP#sp1", |
| 124 | "`test`.`t1` /* Partition `p1`, Subpartition `sp1` */", |
| 125 | EXPLAIN_PARTITIONS_AS_COMMENT); |
| 126 | |
| 127 | test_1("test/t1#p1#SP#sp1", "`test`.`t1#p1#SP#sp1`", |
| 128 | EXPLAIN_PARTITIONS_AS_COMMENT); |
| 129 | |
| 130 | test_1("test/t1#p#p1@00232#SP#sp1@00231#REN#", |
| 131 | "`test`.`t1` /* Renamed Partition `p1#2`, Subpartition `sp1#1` */", |
| 132 | EXPLAIN_PARTITIONS_AS_COMMENT); |
| 133 | |
| 134 | test_1("test/t1#p#p1#SP#sp1#TMP#", |
| 135 | "`test`.`t1` /* Temporary Partition `p1`, Subpartition `sp1` */", |
| 136 | EXPLAIN_PARTITIONS_AS_COMMENT); |
| 137 | |
| 138 | test_1("test/#sql-t1#P#p1#SP#sp1#TMP#", |
| 139 | "`test`.`#sql-t1#P#p1#SP#sp1#TMP#` /* Temporary Partition `p1`, " |
| 140 | "Subpartition `sp1` */", |
| 141 | EXPLAIN_PARTITIONS_AS_COMMENT); |
| 142 | |
| 143 | test_1( |
| 144 | "test/#sql-t1#P#p1#SP#sp1", |
| 145 | "`test`.`#sql-t1#P#p1#SP#sp1` /* Partition `p1`, Subpartition `sp1` */", |
| 146 | EXPLAIN_PARTITIONS_AS_COMMENT); |
| 147 | |
| 148 | test_1("test/#sqlx-33", "`test`.`#sqlx-33`", EXPLAIN_PARTITIONS_AS_COMMENT); |
| 149 | |
| 150 | test_1("test/#mysql50#t", "`test`.`#mysql50#t`", |
| 151 | EXPLAIN_PARTITIONS_AS_COMMENT); |
| 152 | |
| 153 | test_1("#mysql50#t", "`#mysql50#t`", EXPLAIN_PARTITIONS_AS_COMMENT); |
| 154 | |
| 155 | test_1("@0023t", "`#t`", EXPLAIN_PARTITIONS_AS_COMMENT); |
| 156 | |
| 157 | test_1("test/t@0023", "`test`.`t#`", EXPLAIN_PARTITIONS_AS_COMMENT); |
| 158 | |
| 159 | /* |
| 160 | If a character not allowed in my_charset_filename is encountered, |
| 161 | then it will not be converted to system_charset_info! |
| 162 | */ |
| 163 | test_1("test/t@0023#", "`test`.`t@0023#`", EXPLAIN_PARTITIONS_AS_COMMENT); |
| 164 | } |
| 165 | |
| 166 | } // namespace explain_filename_unittest |