| 1 | /* Copyright (c) 2011, 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 <gtest/gtest.h> |
| 25 | |
| 26 | #include "sql/item.h" |
| 27 | #include "sql/rpl_handler.h" // delegates_init() |
| 28 | #include "sql/sql_class.h" |
| 29 | #include "sql/sql_table.h" |
| 30 | #include "unittest/gunit/mock_create_field.h" |
| 31 | #include "unittest/gunit/mock_field_timestamp.h" |
| 32 | #include "unittest/gunit/test_utils.h" |
| 33 | |
| 34 | namespace sql_table_unittest { |
| 35 | |
| 36 | using my_testing::Mock_error_handler; |
| 37 | using my_testing::Server_initializer; |
| 38 | |
| 39 | /* |
| 40 | Test of functionality in the file sql_table.cc |
| 41 | */ |
| 42 | class SqlTableTest : public ::testing::Test { |
| 43 | protected: |
| 44 | void SetUp() override { initializer.SetUp(); } |
| 45 | void TearDown() override { initializer.TearDown(); } |
| 46 | |
| 47 | THD *get_thd() { return initializer.thd(); } |
| 48 | |
| 49 | Server_initializer initializer; |
| 50 | }; |
| 51 | |
| 52 | /* |
| 53 | Test of promote_first_timestamp_column(). We pass it a list of two TIMESTAMP |
| 54 | NOT NULL columns, the first of which should be promoted to DEFAULT |
| 55 | CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP. The second column should not |
| 56 | be promoted. |
| 57 | */ |
| 58 | TEST_F(SqlTableTest, PromoteFirstTimestampColumn1) { |
| 59 | Mock_create_field column_1_definition(MYSQL_TYPE_TIMESTAMP, nullptr, nullptr); |
| 60 | Mock_create_field column_2_definition(MYSQL_TYPE_TIMESTAMP, nullptr, nullptr); |
| 61 | column_1_definition.flags |= NOT_NULL_FLAG; |
| 62 | column_2_definition.flags |= NOT_NULL_FLAG; |
| 63 | List<Create_field> definitions; |
| 64 | definitions.push_front(&column_1_definition); |
| 65 | definitions.push_back(&column_2_definition); |
| 66 | promote_first_timestamp_column(&definitions); |
| 67 | EXPECT_EQ((Field::DEFAULT_NOW | Field::ON_UPDATE_NOW), |
| 68 | column_1_definition.auto_flags); |
| 69 | EXPECT_EQ(Field::NONE, column_2_definition.auto_flags); |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | Test of promote_first_timestamp_column(). We pass it a list of two TIMESTAMP |
| 74 | NOT NULL columns, the first of which should be promoted to DEFAULT |
| 75 | CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP. The second column should not |
| 76 | be promoted. |
| 77 | */ |
| 78 | TEST_F(SqlTableTest, PromoteFirstTimestampColumn2) { |
| 79 | Mock_create_field column_1_definition(MYSQL_TYPE_TIMESTAMP2, nullptr, |
| 80 | nullptr); |
| 81 | Mock_create_field column_2_definition(MYSQL_TYPE_TIMESTAMP2, nullptr, |
| 82 | nullptr); |
| 83 | column_1_definition.flags |= NOT_NULL_FLAG; |
| 84 | column_2_definition.flags |= NOT_NULL_FLAG; |
| 85 | List<Create_field> definitions; |
| 86 | definitions.push_front(&column_1_definition); |
| 87 | definitions.push_back(&column_2_definition); |
| 88 | promote_first_timestamp_column(&definitions); |
| 89 | EXPECT_EQ((Field::DEFAULT_NOW | Field::ON_UPDATE_NOW), |
| 90 | column_1_definition.auto_flags); |
| 91 | EXPECT_EQ(Field::NONE, column_2_definition.auto_flags); |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | Test of promote_first_timestamp_column(). We pass it a list of two columns, |
| 96 | one TIMESTAMP NULL DEFAULT 1, and one TIMESTAMP NOT NULL. No promotion |
| 97 | should take place. |
| 98 | */ |
| 99 | TEST_F(SqlTableTest, PromoteFirstTimestampColumn3) { |
| 100 | Item_string *item_str = new Item_string("1", 1, &my_charset_latin1); |
| 101 | Mock_create_field column_1_definition(MYSQL_TYPE_TIMESTAMP, item_str, |
| 102 | nullptr); |
| 103 | Mock_create_field column_2_definition(MYSQL_TYPE_TIMESTAMP, nullptr, nullptr); |
| 104 | column_2_definition.flags |= NOT_NULL_FLAG; |
| 105 | List<Create_field> definitions; |
| 106 | definitions.push_front(&column_1_definition); |
| 107 | definitions.push_back(&column_2_definition); |
| 108 | promote_first_timestamp_column(&definitions); |
| 109 | EXPECT_EQ(Field::NONE, column_1_definition.auto_flags); |
| 110 | EXPECT_EQ(Field::NONE, column_2_definition.auto_flags); |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | This is a test case based on innobase_init() |
| 115 | There was an out-of-bounds read when converting "-@" to a table name. |
| 116 | */ |
| 117 | TEST_F(SqlTableTest, FileNameToTableName) { |
| 118 | struct PackStuff { |
| 119 | char foo1; |
| 120 | char str[3]; |
| 121 | char foo2; |
| 122 | }; |
| 123 | PackStuff foo; |
| 124 | memcpy(foo.str, "-@", 3); |
| 125 | MEM_NOACCESS(&foo.foo1, 1); |
| 126 | MEM_NOACCESS(&foo.foo2, 1); |
| 127 | |
| 128 | const char test_filename[] = "-@"; |
| 129 | char test_tablename[sizeof test_filename - 1]; |
| 130 | |
| 131 | // This one used to fail with AddressSanitizer |
| 132 | size_t name_length; |
| 133 | name_length = filename_to_tablename(test_filename, test_tablename, |
| 134 | sizeof(test_tablename) |
| 135 | #ifndef NDEBUG |
| 136 | , |
| 137 | true |
| 138 | #endif |
| 139 | ); |
| 140 | EXPECT_EQ((sizeof(test_tablename)) - 1, name_length); |
| 141 | |
| 142 | // This one used to fail if compiled with -DHAVE_VALGRIND |
| 143 | name_length = |
| 144 | filename_to_tablename(foo.str, test_tablename, sizeof(test_tablename) |
| 145 | #ifndef NDEBUG |
| 146 | , |
| 147 | true |
| 148 | #endif |
| 149 | ); |
| 150 | EXPECT_EQ((sizeof(test_tablename)) - 1, name_length); |
| 151 | } |
| 152 | |
| 153 | } // namespace sql_table_unittest |