1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
/* Copyright (c) 2011, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include <gtest/gtest.h>
#include "sql/item.h"
#include "sql/rpl_handler.h" // delegates_init()
#include "sql/sql_class.h"
#include "sql/sql_table.h"
#include "unittest/gunit/mock_create_field.h"
#include "unittest/gunit/mock_field_timestamp.h"
#include "unittest/gunit/test_utils.h"
namespace sql_table_unittest {
using my_testing::Mock_error_handler;
using my_testing::Server_initializer;
/*
Test of functionality in the file sql_table.cc
*/
class SqlTableTest : public ::testing::Test {
protected:
void SetUp() override { initializer.SetUp(); }
void TearDown() override { initializer.TearDown(); }
THD *get_thd() { return initializer.thd(); }
Server_initializer initializer;
};
/*
Test of promote_first_timestamp_column(). We pass it a list of two TIMESTAMP
NOT NULL columns, the first of which should be promoted to DEFAULT
CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP. The second column should not
be promoted.
*/
TEST_F(SqlTableTest, PromoteFirstTimestampColumn1) {
Mock_create_field column_1_definition(MYSQL_TYPE_TIMESTAMP, nullptr, nullptr);
Mock_create_field column_2_definition(MYSQL_TYPE_TIMESTAMP, nullptr, nullptr);
column_1_definition.flags |= NOT_NULL_FLAG;
column_2_definition.flags |= NOT_NULL_FLAG;
List<Create_field> definitions;
definitions.push_front(&column_1_definition);
definitions.push_back(&column_2_definition);
promote_first_timestamp_column(&definitions);
EXPECT_EQ((Field::DEFAULT_NOW | Field::ON_UPDATE_NOW),
column_1_definition.auto_flags);
EXPECT_EQ(Field::NONE, column_2_definition.auto_flags);
}
/*
Test of promote_first_timestamp_column(). We pass it a list of two TIMESTAMP
NOT NULL columns, the first of which should be promoted to DEFAULT
CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP. The second column should not
be promoted.
*/
TEST_F(SqlTableTest, PromoteFirstTimestampColumn2) {
Mock_create_field column_1_definition(MYSQL_TYPE_TIMESTAMP2, nullptr,
nullptr);
Mock_create_field column_2_definition(MYSQL_TYPE_TIMESTAMP2, nullptr,
nullptr);
column_1_definition.flags |= NOT_NULL_FLAG;
column_2_definition.flags |= NOT_NULL_FLAG;
List<Create_field> definitions;
definitions.push_front(&column_1_definition);
definitions.push_back(&column_2_definition);
promote_first_timestamp_column(&definitions);
EXPECT_EQ((Field::DEFAULT_NOW | Field::ON_UPDATE_NOW),
column_1_definition.auto_flags);
EXPECT_EQ(Field::NONE, column_2_definition.auto_flags);
}
/*
Test of promote_first_timestamp_column(). We pass it a list of two columns,
one TIMESTAMP NULL DEFAULT 1, and one TIMESTAMP NOT NULL. No promotion
should take place.
*/
TEST_F(SqlTableTest, PromoteFirstTimestampColumn3) {
Item_string *item_str = new Item_string("1", 1, &my_charset_latin1);
Mock_create_field column_1_definition(MYSQL_TYPE_TIMESTAMP, item_str,
nullptr);
Mock_create_field column_2_definition(MYSQL_TYPE_TIMESTAMP, nullptr, nullptr);
column_2_definition.flags |= NOT_NULL_FLAG;
List<Create_field> definitions;
definitions.push_front(&column_1_definition);
definitions.push_back(&column_2_definition);
promote_first_timestamp_column(&definitions);
EXPECT_EQ(Field::NONE, column_1_definition.auto_flags);
EXPECT_EQ(Field::NONE, column_2_definition.auto_flags);
}
/*
This is a test case based on innobase_init()
There was an out-of-bounds read when converting "-@" to a table name.
*/
TEST_F(SqlTableTest, FileNameToTableName) {
struct PackStuff {
char foo1;
char str[3];
char foo2;
};
PackStuff foo;
memcpy(foo.str, "-@", 3);
MEM_NOACCESS(&foo.foo1, 1);
MEM_NOACCESS(&foo.foo2, 1);
const char test_filename[] = "-@";
char test_tablename[sizeof test_filename - 1];
// This one used to fail with AddressSanitizer
size_t name_length;
name_length = filename_to_tablename(test_filename, test_tablename,
sizeof(test_tablename)
#ifndef NDEBUG
,
true
#endif
);
EXPECT_EQ((sizeof(test_tablename)) - 1, name_length);
// This one used to fail if compiled with -DHAVE_VALGRIND
name_length =
filename_to_tablename(foo.str, test_tablename, sizeof(test_tablename)
#ifndef NDEBUG
,
true
#endif
);
EXPECT_EQ((sizeof(test_tablename)) - 1, name_length);
}
} // namespace sql_table_unittest
|