1 /* Copyright (c) 2011, 2021, 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 also distributed 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 included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software Foundation,
21    51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
22 
23 // First include (the generated) my_config.h, to get correct platform defines.
24 #include "my_config.h"
25 #include <gtest/gtest.h>
26 
27 #include "mock_field_datetime.h"
28 #include "mock_field_timestamp.h"
29 #include "mock_field_timestampf.h"
30 #include "test_utils.h"
31 #include "item.h"
32 #include "item_timefunc.h"
33 #include "sql_class.h"
34 #include "rpl_handler.h"                        // delegates_init()
35 #include "tztime.h"
36 
37 namespace item_func_now_local_unittest {
38 
39 using my_testing::Server_initializer;
40 using my_testing::Mock_error_handler;
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 {
50 protected:
SetUp()51   virtual void SetUp()
52   {
53     initializer.SetUp();
54     timeval now=
55     {
56       CURRENT_TIMESTAMP_WHOLE_SECONDS, CURRENT_TIMESTAMP_FRACTIONAL_SECONDS
57     };
58     get_thd()->set_time(&now);
59   }
60 
TearDown()61   virtual void TearDown() { initializer.TearDown(); }
62 
get_thd()63   THD *get_thd() { return initializer.thd(); }
64 
65   Server_initializer initializer;
66 };
67 
68 
69 /*
70   Tests that the THD start time is stored correctly in a Field_timestamp using
71   the Item::save_in_field() interface.
72 */
TEST_F(ItemFuncNowLocalTest,saveInField)73 TEST_F(ItemFuncNowLocalTest, saveInField)
74 {
75   Item_func_now_local *item= new Item_func_now_local(0);
76   Mock_field_timestamp f;
77 
78   item->fix_length_and_dec();
79   f.make_writable();
80   item->save_in_field(&f, true);
81 
82   EXPECT_EQ(get_thd()->query_start_timeval().tv_sec, f.to_timeval().tv_sec);
83   // CURRENT_TIMESTAMP should truncate.
84   EXPECT_EQ(0, f.to_timeval().tv_usec);
85 }
86 
87 
88 /*
89   Tests that Item_func_now_local::store_in() goes through the optimized
90   interface Field::store_timestamp() on a Field_timestamp.
91 */
TEST_F(ItemFuncNowLocalTest,storeInTimestamp)92 TEST_F(ItemFuncNowLocalTest, storeInTimestamp)
93 {
94   Mock_field_timestamp f;
95   Item_func_now_local::store_in(&f);
96 
97   EXPECT_EQ(get_thd()->query_start_timeval().tv_sec, f.to_timeval().tv_sec);
98   // CURRENT_TIMESTAMP should truncate.
99   EXPECT_EQ(0, f.to_timeval().tv_usec);
100   EXPECT_TRUE(f.store_timestamp_called);
101 }
102 
103 int powers_of_10[DATETIME_MAX_DECIMALS + 1] =
104 { 1, 10, 100, 1000, 10000, 100000, 1000000 };
105 
106 /*
107   Truncates the number n to a precision of ( DATETIME_MAX_DECIMALS - scale ).
108 */
truncate(int n,int scale)109 int truncate(int n, int scale)
110 {
111   EXPECT_TRUE(scale >= 0);
112   EXPECT_TRUE(scale <= DATETIME_MAX_DECIMALS);
113   return (n / powers_of_10[DATETIME_MAX_DECIMALS - scale]) *
114     powers_of_10[DATETIME_MAX_DECIMALS - scale];
115 }
116 
117 /*
118   Tests that Item_func_now_local::store_in() goes through the optimized
119   interface Field_temporal_with_date_and_time::store_timestamp_internal() on a
120   Field_timestampf.
121 
122   We also test that the CURRENT_TIMESTAMP value gets truncated, not rounded.
123 */
TEST_F(ItemFuncNowLocalTest,storeInTimestampf)124 TEST_F(ItemFuncNowLocalTest, storeInTimestampf)
125 {
126   for(ulong scale= 0; scale <= DATETIME_MAX_DECIMALS; ++scale)
127   {
128     Mock_field_timestampf f(Field::NONE, scale);
129     f.make_writable();
130     Item_func_now_local::store_in(&f);
131 
132     EXPECT_EQ(get_thd()->query_start_timeval().tv_sec, f.to_timeval().tv_sec);
133     // CURRENT_TIMESTAMP should truncate.
134     EXPECT_EQ(truncate(CURRENT_TIMESTAMP_FRACTIONAL_SECONDS, scale),
135               f.to_timeval().tv_usec);
136     EXPECT_TRUE(f.store_timestamp_internal_called);
137   }
138 }
139 
140 
141 /*
142   Tests that Item_func_now_local::store_in() works correctly even though it does
143   not use the optimized interface.
144 */
TEST_F(ItemFuncNowLocalTest,storeInDatetime)145 TEST_F(ItemFuncNowLocalTest, storeInDatetime)
146 {
147   Mock_field_datetime f;
148   MYSQL_TIME now_time;
149   THD *thd= get_thd();
150   timeval now= { 1313677243, 1234 }; // Thu Aug 18 16:20:43 CEST 2011 and 1234 ms
151   thd->set_time(&now);
152 
153   Item_func_now_local::store_in(&f);
154   thd->variables.time_zone->gmt_sec_to_TIME(&now_time, thd->start_time);
155   MYSQL_TIME stored_time;
156   f.get_time(&stored_time);
157 
158   EXPECT_EQ(now_time.year,      stored_time.year);
159   EXPECT_EQ(now_time.month,     stored_time.month);
160   EXPECT_EQ(now_time.day,       stored_time.day);
161   EXPECT_EQ(now_time.hour,      stored_time.hour);
162   EXPECT_EQ(now_time.minute,    stored_time.minute);
163   EXPECT_EQ(now_time.second,    stored_time.second);
164   // CURRENT_TIMESTAMP truncates.
165   EXPECT_EQ(0u,                 stored_time.second_part);
166   EXPECT_EQ(now_time.neg,       stored_time.neg);
167   EXPECT_EQ(now_time.time_type, stored_time.time_type);
168 }
169 
170 }
171