| 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 <gmock/gmock.h> |
| 25 | #include <gtest/gtest.h> |
| 26 | #include <stddef.h> |
| 27 | |
| 28 | #include "my_inttypes.h" |
| 29 | #include "sql/sql_data_change.h" |
| 30 | #include "unittest/gunit/fake_table.h" |
| 31 | #include "unittest/gunit/mock_field_timestamp.h" |
| 32 | #include "unittest/gunit/test_utils.h" |
| 33 | |
| 34 | namespace copy_info_unittest { |
| 35 | |
| 36 | using my_testing::Mock_error_handler; |
| 37 | using my_testing::Server_initializer; |
| 38 | |
| 39 | using ::testing::_; |
| 40 | using ::testing::StrictMock; |
| 41 | |
| 42 | /* |
| 43 | Tests for the functionality of the COPY_INFO class. We test all public |
| 44 | interfaces, and some of the protected parts: |
| 45 | |
| 46 | - COPY_INFO::get_function_default_columns, and |
| 47 | - COPY_INFO::get_cached_bitmap |
| 48 | */ |
| 49 | class CopyInfoTest : public ::testing::Test { |
| 50 | protected: |
| 51 | void SetUp() override { initializer.SetUp(); } |
| 52 | void TearDown() override { initializer.TearDown(); } |
| 53 | |
| 54 | Server_initializer initializer; |
| 55 | }; |
| 56 | |
| 57 | /** |
| 58 | This is a simple mock Field class, which verifies that store_timestamp is |
| 59 | called. We inherit Field_long, but the data type does not matter. |
| 60 | */ |
| 61 | class Mock_field : public Field_long { |
| 62 | public: |
| 63 | explicit Mock_field(uchar auto_flags_arg) |
| 64 | : Field_long(nullptr, 0, nullptr, 0, auto_flags_arg, "", false, false) {} |
| 65 | |
| 66 | MOCK_METHOD1(store_timestamp, void(const my_timeval *)); |
| 67 | }; |
| 68 | |
| 69 | /* |
| 70 | Convenience class for creating a COPY_INFO to represent an insert operation. |
| 71 | */ |
| 72 | class Mock_COPY_INFO : public COPY_INFO { |
| 73 | public: |
| 74 | Mock_COPY_INFO(operation_type optype, |
| 75 | mem_root_deque<Item *> *inserted_columns, |
| 76 | enum_duplicates duplicate_handling) |
| 77 | : COPY_INFO(optype, inserted_columns, |
| 78 | true, // manage_defaults |
| 79 | duplicate_handling) {} |
| 80 | |
| 81 | // Import protected member functions, so we can test them. |
| 82 | using COPY_INFO::get_cached_bitmap; |
| 83 | using COPY_INFO::get_function_default_columns; |
| 84 | }; |
| 85 | |
| 86 | /* |
| 87 | Convenience class for creating a COPY_INFO to represent an insert operation. |
| 88 | */ |
| 89 | class Mock_COPY_INFO_insert : public COPY_INFO { |
| 90 | public: |
| 91 | Mock_COPY_INFO_insert() |
| 92 | : COPY_INFO(COPY_INFO::INSERT_OPERATION, |
| 93 | static_cast<mem_root_deque<Item *> *>(nullptr), |
| 94 | true, // manage_defaults |
| 95 | DUP_UPDATE) {} |
| 96 | explicit Mock_COPY_INFO_insert(mem_root_deque<Item *> *fields) |
| 97 | : COPY_INFO(COPY_INFO::INSERT_OPERATION, fields, |
| 98 | true, // manage_defaults |
| 99 | DUP_UPDATE) {} |
| 100 | // Import protected member functions, so we can test them. |
| 101 | using COPY_INFO::get_cached_bitmap; |
| 102 | using COPY_INFO::get_function_default_columns; |
| 103 | }; |
| 104 | |
| 105 | /* |
| 106 | Convenience class for creating a COPY_INFO to represent an update |
| 107 | operation. |
| 108 | */ |
| 109 | class Mock_COPY_INFO_update : public COPY_INFO { |
| 110 | public: |
| 111 | Mock_COPY_INFO_update() |
| 112 | : COPY_INFO(COPY_INFO::UPDATE_OPERATION, nullptr, nullptr) {} |
| 113 | // Import protected member functions, so we can test them. |
| 114 | using COPY_INFO::get_cached_bitmap; |
| 115 | using COPY_INFO::get_function_default_columns; |
| 116 | }; |
| 117 | |
| 118 | /* |
| 119 | Tests that constuctors initialize the stats object properly. |
| 120 | */ |
| 121 | TEST_F(CopyInfoTest, constructors) { |
| 122 | mem_root_deque<Item *> inserted_columns(*THR_MALLOC); |
| 123 | |
| 124 | COPY_INFO insert(COPY_INFO::INSERT_OPERATION, &inserted_columns, |
| 125 | true, // manage_defaults |
| 126 | DUP_UPDATE); |
| 127 | |
| 128 | EXPECT_EQ(0U, insert.stats.records); |
| 129 | EXPECT_EQ(0U, insert.stats.deleted); |
| 130 | EXPECT_EQ(0U, insert.stats.updated); |
| 131 | EXPECT_EQ(0U, insert.stats.copied); |
| 132 | EXPECT_EQ(0U, insert.stats.error_count); |
| 133 | EXPECT_EQ(0U, insert.stats.touched); |
| 134 | |
| 135 | mem_root_deque<Item *> columns(*THR_MALLOC); |
| 136 | mem_root_deque<Item *> values(*THR_MALLOC); |
| 137 | COPY_INFO update(COPY_INFO::UPDATE_OPERATION, &columns, &values); |
| 138 | |
| 139 | EXPECT_EQ(0U, update.stats.records); |
| 140 | EXPECT_EQ(0U, update.stats.deleted); |
| 141 | EXPECT_EQ(0U, update.stats.updated); |
| 142 | EXPECT_EQ(0U, update.stats.copied); |
| 143 | EXPECT_EQ(0U, update.stats.error_count); |
| 144 | EXPECT_EQ(0U, update.stats.touched); |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | Tests the accessors when the COPY_INFO represents an insert operation. |
| 149 | */ |
| 150 | TEST_F(CopyInfoTest, insertAccessors) { |
| 151 | mem_root_deque<Item *> inserted_columns(*THR_MALLOC); |
| 152 | |
| 153 | COPY_INFO insert(COPY_INFO::INSERT_OPERATION, &inserted_columns, |
| 154 | true, // manage_defaults |
| 155 | DUP_REPLACE); |
| 156 | |
| 157 | EXPECT_EQ(COPY_INFO::INSERT_OPERATION, insert.get_operation_type()); |
| 158 | EXPECT_EQ(&inserted_columns, insert.get_changed_columns()); |
| 159 | EXPECT_EQ(static_cast<mem_root_deque<Item *> *>(nullptr), |
| 160 | insert.get_changed_columns2()); |
| 161 | EXPECT_TRUE(insert.get_manage_defaults()); |
| 162 | EXPECT_EQ(DUP_REPLACE, insert.get_duplicate_handling()); |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | Tests the accessors when the COPY_INFO represents a load data infile |
| 167 | operation. |
| 168 | */ |
| 169 | TEST_F(CopyInfoTest, loadDataAccessors) { |
| 170 | mem_root_deque<Item *> inserted_columns(*THR_MALLOC); |
| 171 | mem_root_deque<Item *> inserted_columns2(*THR_MALLOC); |
| 172 | |
| 173 | COPY_INFO load_data(COPY_INFO::INSERT_OPERATION, &inserted_columns, |
| 174 | &inserted_columns2, |
| 175 | true, // manage_defaults |
| 176 | DUP_UPDATE, 123); |
| 177 | |
| 178 | EXPECT_EQ(COPY_INFO::INSERT_OPERATION, load_data.get_operation_type()); |
| 179 | EXPECT_EQ(&inserted_columns, load_data.get_changed_columns()); |
| 180 | EXPECT_EQ(&inserted_columns2, load_data.get_changed_columns2()); |
| 181 | EXPECT_TRUE(load_data.get_manage_defaults()); |
| 182 | EXPECT_EQ(DUP_UPDATE, load_data.get_duplicate_handling()); |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | Tests the accessors when the COPY_INFO represents an update operation. |
| 187 | */ |
| 188 | TEST_F(CopyInfoTest, updateAccessors) { |
| 189 | mem_root_deque<Item *> columns(*THR_MALLOC); |
| 190 | mem_root_deque<Item *> values(*THR_MALLOC); |
| 191 | |
| 192 | COPY_INFO update(COPY_INFO::UPDATE_OPERATION, &columns, &values); |
| 193 | |
| 194 | EXPECT_EQ(COPY_INFO::UPDATE_OPERATION, update.get_operation_type()); |
| 195 | EXPECT_EQ(&columns, update.get_changed_columns()); |
| 196 | EXPECT_EQ(static_cast<mem_root_deque<Item *> *>(nullptr), |
| 197 | update.get_changed_columns2()); |
| 198 | EXPECT_TRUE(update.get_manage_defaults()); |
| 199 | EXPECT_EQ(DUP_ERROR, update.get_duplicate_handling()); |
| 200 | } |
| 201 | |
| 202 | static Field_long make_field() { |
| 203 | Field_long a(nullptr, 0, nullptr, 0, Field::DEFAULT_NOW, "a", false, false); |
| 204 | return a; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | Test of the lazy instantiation performed by get_function_default_columns(). |
| 209 | |
| 210 | - The bitmap pointer is initially NULL. |
| 211 | |
| 212 | - That calling get_function_default_columns() indeed points the member to a |
| 213 | lazily instantiated bitmap. |
| 214 | |
| 215 | - That on a second call to get_function_default_columns(), a new bitmap is |
| 216 | not allocated. |
| 217 | |
| 218 | We repeat the test for insert and update operations. |
| 219 | */ |
| 220 | TEST_F(CopyInfoTest, getFunctionDefaultColumns) { |
| 221 | Mock_COPY_INFO_insert insert; |
| 222 | Mock_COPY_INFO_update update; |
| 223 | |
| 224 | Field_long a = make_field(); |
| 225 | Fake_TABLE table(&a); |
| 226 | |
| 227 | MY_BITMAP *initial_value = nullptr; |
| 228 | |
| 229 | EXPECT_EQ(initial_value, insert.get_cached_bitmap()); |
| 230 | |
| 231 | insert.get_function_default_columns(&table); |
| 232 | EXPECT_NE(initial_value, insert.get_cached_bitmap()) |
| 233 | << "The output parameter must be set!"; |
| 234 | |
| 235 | const MY_BITMAP *function_default_columns = insert.get_cached_bitmap(); |
| 236 | insert.get_function_default_columns(&table); |
| 237 | EXPECT_EQ(function_default_columns, insert.get_cached_bitmap()) |
| 238 | << "Not supposed to allocate a new bitmap on second call."; |
| 239 | |
| 240 | EXPECT_EQ(initial_value, update.get_cached_bitmap()); |
| 241 | update.get_function_default_columns(&table); |
| 242 | EXPECT_NE(initial_value, update.get_cached_bitmap()) |
| 243 | << "The output parameter must be set!"; |
| 244 | |
| 245 | function_default_columns = update.get_cached_bitmap(); |
| 246 | update.get_function_default_columns(&table); |
| 247 | EXPECT_EQ(function_default_columns, update.get_cached_bitmap()) |
| 248 | << "Not supposed to allocate a new bitmap on second call."; |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | Here we test that calling COPY_INFO::set_function_defaults() indeed causes |
| 253 | store_timestamp to be called on the columns that are not on the list of |
| 254 | assigned_columns. We seize the opportunity to test |
| 255 | COPY_INFO::function_defaults_apply() since we have to call it anyways in |
| 256 | order for set_function_defaults() not to assert. |
| 257 | */ |
| 258 | TEST_F(CopyInfoTest, setFunctionDefaults) { |
| 259 | StrictMock<Mock_field> a(Field::ON_UPDATE_NOW); |
| 260 | StrictMock<Mock_field> b(Field::DEFAULT_NOW | Field::ON_UPDATE_NOW); |
| 261 | StrictMock<Mock_field> c(Field::DEFAULT_NOW | Field::ON_UPDATE_NOW); |
| 262 | |
| 263 | EXPECT_TRUE(a.has_update_default_datetime_value_expression()); |
| 264 | EXPECT_TRUE(b.has_update_default_datetime_value_expression()); |
| 265 | EXPECT_TRUE(c.has_update_default_datetime_value_expression()); |
| 266 | |
| 267 | Fake_TABLE table(&a, &b, &c); |
| 268 | |
| 269 | mem_root_deque<Item *> assigned_columns(*THR_MALLOC); |
| 270 | assigned_columns.push_front(new Item_field(&a)); |
| 271 | |
| 272 | Mock_COPY_INFO insert(COPY_INFO::INSERT_OPERATION, &assigned_columns, |
| 273 | DUP_ERROR); |
| 274 | |
| 275 | ASSERT_FALSE(insert.get_function_default_columns(&table)) << "Out of memory"; |
| 276 | |
| 277 | insert.ignore_last_columns(&table, 1); // 'c' |
| 278 | insert.add_function_default_columns(&table, table.write_set); |
| 279 | EXPECT_FALSE(bitmap_is_set(table.write_set, 0)); |
| 280 | EXPECT_TRUE(bitmap_is_set(table.write_set, 1)); |
| 281 | EXPECT_FALSE(bitmap_is_set(table.write_set, 2)); |
| 282 | |
| 283 | EXPECT_TRUE(insert.function_defaults_apply(&table)) << "They do apply"; |
| 284 | |
| 285 | // We expect store_timestamp() to be called for b and not for c. |
| 286 | // We do not care about the argument to store_timestamp(). |
| 287 | EXPECT_CALL(b, store_timestamp(_)).Times(1); |
| 288 | EXPECT_CALL(c, store_timestamp(_)).Times(0); |
| 289 | EXPECT_FALSE(insert.set_function_defaults(&table)); |
| 290 | } |
| 291 | |
| 292 | } // namespace copy_info_unittest |