| 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 | #include <sys/types.h> |
| 26 | |
| 27 | #include "sql/item.h" |
| 28 | #include "sql/item_timefunc.h" |
| 29 | #include "sql/rpl_handler.h" // delegates_init() |
| 30 | #include "sql/sql_class.h" |
| 31 | #include "sql/tztime.h" |
| 32 | #include "unittest/gunit/mock_field_datetime.h" |
| 33 | #include "unittest/gunit/mock_field_timestamp.h" |
| 34 | #include "unittest/gunit/mock_field_timestampf.h" |
| 35 | #include "unittest/gunit/test_utils.h" |
| 36 | |
| 37 | namespace item_func_now_local_unittest { |
| 38 | |
| 39 | using my_testing::Mock_error_handler; |
| 40 | using my_testing::Server_initializer; |
| 41 | |
| 42 | const int CURRENT_TIMESTAMP_WHOLE_SECONDS = 123456; |
| 43 | const int CURRENT_TIMESTAMP_FRACTIONAL_SECONDS = 654321; |
| 44 | |
| 45 | /* |
| 46 | Test of the interface of Item_func_now_local. |
| 47 | */ |
| 48 | class ItemFuncNowLocalTest : public ::testing::Test { |
| 49 | protected: |
| 50 | void SetUp() override { |
| 51 | initializer.SetUp(); |
| 52 | timeval now = {CURRENT_TIMESTAMP_WHOLE_SECONDS, |
| 53 | CURRENT_TIMESTAMP_FRACTIONAL_SECONDS}; |
| 54 | get_thd()->set_time(&now); |
| 55 | } |
| 56 | |
| 57 | void TearDown() override { initializer.TearDown(); } |
| 58 | |
| 59 | THD *get_thd() { return initializer.thd(); } |
| 60 | |
| 61 | Server_initializer initializer; |
| 62 | }; |
| 63 | |
| 64 | /* |
| 65 | Tests that the THD start time is stored correctly in a Field_timestamp using |
| 66 | the Item::save_in_field() interface. |
| 67 | */ |
| 68 | TEST_F(ItemFuncNowLocalTest, saveInField) { |
| 69 | Item_func_now_local *item = new Item_func_now_local(0); |
| 70 | Mock_field_timestamp f; |
| 71 | |
| 72 | EXPECT_FALSE(item->resolve_type(get_thd())); |
| 73 | f.make_writable(); |
| 74 | item->save_in_field(&f, true); |
| 75 | |
| 76 | EXPECT_EQ(get_thd()->query_start_timeval_trunc(0).m_tv_sec, |
| 77 | f.to_timeval().m_tv_sec); |
| 78 | // CURRENT_TIMESTAMP should truncate. |
| 79 | EXPECT_EQ(0, f.to_timeval().m_tv_usec); |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | Tests that Item_func_now_local::store_in() goes through the optimized |
| 84 | interface Field::store_timestamp() on a Field_timestamp. |
| 85 | */ |
| 86 | TEST_F(ItemFuncNowLocalTest, storeInTimestamp) { |
| 87 | Mock_field_timestamp f; |
| 88 | Item_func_now_local::store_in(&f); |
| 89 | |
| 90 | EXPECT_EQ(get_thd()->query_start_timeval_trunc(0).m_tv_sec, |
| 91 | f.to_timeval().m_tv_sec); |
| 92 | // CURRENT_TIMESTAMP should truncate. |
| 93 | EXPECT_EQ(0, f.to_timeval().m_tv_usec); |
| 94 | EXPECT_TRUE(f.store_timestamp_called); |
| 95 | } |
| 96 | |
| 97 | int powers_of_10[DATETIME_MAX_DECIMALS + 1] = {1, 10, 100, 1000, |
| 98 | 10000, 100000, 1000000}; |
| 99 | |
| 100 | /* |
| 101 | Truncates the number n to a precision of ( DATETIME_MAX_DECIMALS - scale ). |
| 102 | */ |
| 103 | int truncate(int n, int scale) { |
| 104 | EXPECT_TRUE(scale >= 0); |
| 105 | EXPECT_TRUE(scale <= DATETIME_MAX_DECIMALS); |
| 106 | return (n / powers_of_10[DATETIME_MAX_DECIMALS - scale]) * |
| 107 | powers_of_10[DATETIME_MAX_DECIMALS - scale]; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | Tests that Item_func_now_local::store_in() goes through the optimized |
| 112 | interface Field_temporal_with_date_and_time::store_timestamp_internal() on a |
| 113 | Field_timestampf. |
| 114 | |
| 115 | We also test that the CURRENT_TIMESTAMP value gets truncated, not rounded. |
| 116 | */ |
| 117 | TEST_F(ItemFuncNowLocalTest, storeInTimestampf) { |
| 118 | for (ulong scale = 0; scale <= DATETIME_MAX_DECIMALS; ++scale) { |
| 119 | Mock_field_timestampf f(Field::NONE, scale); |
| 120 | f.make_writable(); |
| 121 | Item_func_now_local::store_in(&f); |
| 122 | |
| 123 | EXPECT_EQ(get_thd()->query_start_timeval_trunc(0).m_tv_sec, |
| 124 | f.to_timeval().m_tv_sec); |
| 125 | // CURRENT_TIMESTAMP should truncate. |
| 126 | EXPECT_EQ(truncate(CURRENT_TIMESTAMP_FRACTIONAL_SECONDS, scale), |
| 127 | f.to_timeval().m_tv_usec); |
| 128 | EXPECT_TRUE(f.store_timestamp_internal_called); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | Tests that Item_func_now_local::store_in() works correctly even though it does |
| 134 | not use the optimized interface. |
| 135 | */ |
| 136 | TEST_F(ItemFuncNowLocalTest, storeInDatetime) { |
| 137 | Mock_field_datetime f; |
| 138 | MYSQL_TIME now_time; |
| 139 | THD *thd = get_thd(); |
| 140 | timeval now = {1313677243, |
| 141 | 1234}; // Thu Aug 18 16:20:43 CEST 2011 and 1234 ms |
| 142 | thd->set_time(&now); |
| 143 | |
| 144 | Item_func_now_local::store_in(&f); |
| 145 | thd->variables.time_zone->gmt_sec_to_TIME( |
| 146 | &now_time, {thd->start_time.tv_sec, thd->start_time.tv_usec}); |
| 147 | MYSQL_TIME stored_time; |
| 148 | f.get_time(&stored_time); |
| 149 | |
| 150 | EXPECT_EQ(now_time.year, stored_time.year); |
| 151 | EXPECT_EQ(now_time.month, stored_time.month); |
| 152 | EXPECT_EQ(now_time.day, stored_time.day); |
| 153 | EXPECT_EQ(now_time.hour, stored_time.hour); |
| 154 | EXPECT_EQ(now_time.minute, stored_time.minute); |
| 155 | EXPECT_EQ(now_time.second, stored_time.second); |
| 156 | // CURRENT_TIMESTAMP truncates. |
| 157 | EXPECT_EQ(0u, stored_time.second_part); |
| 158 | EXPECT_EQ(now_time.neg, stored_time.neg); |
| 159 | EXPECT_EQ(now_time.time_type, stored_time.time_type); |
| 160 | } |
| 161 | |
| 162 | } // namespace item_func_now_local_unittest |