| 1 | /* Copyright (c) 2012, 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/field.h" |
| 27 | #include "unittest/gunit/fake_table.h" |
| 28 | #include "unittest/gunit/field_temporal_utils.h" |
| 29 | #include "unittest/gunit/test_utils.h" |
| 30 | |
| 31 | namespace field_datetime_unittests { |
| 32 | |
| 33 | using my_testing::Mock_error_handler; |
| 34 | using my_testing::Server_initializer; |
| 35 | |
| 36 | class FieldDatetimeTest : public ::testing::Test { |
| 37 | protected: |
| 38 | void SetUp() override { initializer.SetUp(); } |
| 39 | void TearDown() override { initializer.TearDown(); } |
| 40 | |
| 41 | THD *thd() { return initializer.thd(); } |
| 42 | |
| 43 | Server_initializer initializer; |
| 44 | |
| 45 | Field_set *create_field_set(TYPELIB *tl); |
| 46 | }; |
| 47 | |
| 48 | class Mock_field_datetime : public Field_datetime { |
| 49 | public: |
| 50 | Mock_field_datetime() |
| 51 | : Field_datetime(nullptr, // ptr_arg |
| 52 | &Field::dummy_null_buffer, // null_ptr_arg |
| 53 | 1, // null_bit_arg |
| 54 | Field::NONE, // auto_flags_arg |
| 55 | "field_name") // field_name_arg |
| 56 | {} |
| 57 | |
| 58 | void make_writable() { bitmap_set_bit(table->write_set, field_index()); } |
| 59 | void make_readable() { bitmap_set_bit(table->read_set, field_index()); } |
| 60 | }; |
| 61 | |
| 62 | class Mock_field_datetimef : public Field_datetimef { |
| 63 | public: |
| 64 | explicit Mock_field_datetimef(uint scale) |
| 65 | : Field_datetimef(nullptr, // ptr_arg |
| 66 | &Field::dummy_null_buffer, // null_ptr_arg |
| 67 | 1, // null_bit_arg |
| 68 | Field::NONE, // unireg_check_arg |
| 69 | "field_name", // field_name_arg |
| 70 | scale) {} |
| 71 | |
| 72 | void make_writable() { bitmap_set_bit(table->write_set, field_index()); } |
| 73 | void make_readable() { bitmap_set_bit(table->read_set, field_index()); } |
| 74 | }; |
| 75 | |
| 76 | TEST_F(FieldDatetimeTest, StoreLegalStringValues) { |
| 77 | char buff[MAX_FIELD_WIDTH]; |
| 78 | String str(buff, sizeof(buff), &my_charset_bin); |
| 79 | String unused; |
| 80 | |
| 81 | Mock_field_datetime field_dt; |
| 82 | Fake_TABLE table(&field_dt); |
| 83 | table.in_use = thd(); |
| 84 | field_dt.make_writable(); |
| 85 | field_dt.make_readable(); |
| 86 | thd()->check_for_truncated_fields = CHECK_FIELD_WARN; |
| 87 | |
| 88 | { |
| 89 | SCOPED_TRACE(""); |
| 90 | test_store_string(&field_dt, STRING_WITH_LEN("2001-01-01 00:00:01"), 0, |
| 91 | "2001-01-01 00:00:01", 0, TYPE_OK); |
| 92 | } |
| 93 | { |
| 94 | SCOPED_TRACE(""); |
| 95 | test_store_string(&field_dt, STRING_WITH_LEN("0000-00-00 00:00:00"), 0, |
| 96 | "0000-00-00 00:00:00", 0, TYPE_OK); |
| 97 | } |
| 98 | { |
| 99 | SCOPED_TRACE(""); |
| 100 | test_store_string(&field_dt, STRING_WITH_LEN("0001-00-00 00:00:00"), 0, |
| 101 | "0001-00-00 00:00:00", 0, TYPE_OK); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Test for MODE_TIME_TRUNCATE_FRACTIONAL. |
| 106 | TEST_F(FieldDatetimeTest, TestTruncFrational) { |
| 107 | char buff[MAX_FIELD_WIDTH]; |
| 108 | String str(buff, sizeof(buff), &my_charset_bin); |
| 109 | String unused; |
| 110 | // fsp=6 |
| 111 | Mock_field_datetimef field_dt(6); |
| 112 | Fake_TABLE table(&field_dt); |
| 113 | table.in_use = thd(); |
| 114 | field_dt.make_writable(); |
| 115 | field_dt.make_readable(); |
| 116 | |
| 117 | // fsp=0 |
| 118 | Mock_field_datetimef field_dt0(0); |
| 119 | Fake_TABLE table0(&field_dt0); |
| 120 | table0.in_use = thd(); |
| 121 | field_dt0.make_writable(); |
| 122 | field_dt0.make_readable(); |
| 123 | |
| 124 | thd()->check_for_truncated_fields = CHECK_FIELD_WARN; |
| 125 | |
| 126 | { |
| 127 | SCOPED_TRACE(""); |
| 128 | test_store_string(&field_dt, STRING_WITH_LEN("2001-01-01 00:00:01.9999999"), |
| 129 | MODE_TIME_TRUNCATE_FRACTIONAL, |
| 130 | "2001-01-01 00:00:01.999999", 0, TYPE_OK); |
| 131 | } |
| 132 | { |
| 133 | SCOPED_TRACE(""); |
| 134 | test_store_string(&field_dt, STRING_WITH_LEN("2001-01-01 00:00:01.999995"), |
| 135 | MODE_TIME_TRUNCATE_FRACTIONAL, |
| 136 | "2001-01-01 00:00:01.999995", 0, TYPE_OK); |
| 137 | } |
| 138 | { |
| 139 | SCOPED_TRACE(""); |
| 140 | test_store_string(&field_dt, STRING_WITH_LEN("0001-00-00 00:00:00"), |
| 141 | MODE_TIME_TRUNCATE_FRACTIONAL, |
| 142 | "0001-00-00 00:00:00.000000", 0, TYPE_OK); |
| 143 | } |
| 144 | |
| 145 | { |
| 146 | SCOPED_TRACE(""); |
| 147 | test_store_string( |
| 148 | &field_dt0, STRING_WITH_LEN("2001-01-01 00:00:01.9999999"), |
| 149 | MODE_TIME_TRUNCATE_FRACTIONAL, "2001-01-01 00:00:01", 0, TYPE_OK); |
| 150 | } |
| 151 | { |
| 152 | SCOPED_TRACE(""); |
| 153 | test_store_string(&field_dt0, STRING_WITH_LEN("2001-01-01 00:00:01.9"), |
| 154 | MODE_TIME_TRUNCATE_FRACTIONAL, "2001-01-01 00:00:01", 0, |
| 155 | TYPE_OK); |
| 156 | } |
| 157 | { |
| 158 | SCOPED_TRACE(""); |
| 159 | test_store_string(&field_dt0, STRING_WITH_LEN("0001-00-00 00:00:00.99"), |
| 160 | MODE_TIME_TRUNCATE_FRACTIONAL, "0001-00-00 00:00:00", 0, |
| 161 | TYPE_OK); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | TEST_F(FieldDatetimeTest, StoreIllegalStringValues) { |
| 166 | Mock_field_datetime field_dt; |
| 167 | Fake_TABLE table(&field_dt); |
| 168 | table.in_use = thd(); |
| 169 | field_dt.make_writable(); |
| 170 | field_dt.make_readable(); |
| 171 | thd()->check_for_truncated_fields = CHECK_FIELD_WARN; |
| 172 | |
| 173 | // Bad year |
| 174 | { |
| 175 | SCOPED_TRACE(""); |
| 176 | test_store_string(&field_dt, STRING_WITH_LEN("99999-01-01 00:00:01"), 0, |
| 177 | "0000-00-00 00:00:00", WARN_DATA_TRUNCATED, |
| 178 | TYPE_ERR_BAD_VALUE); |
| 179 | } |
| 180 | |
| 181 | // Bad month |
| 182 | { |
| 183 | SCOPED_TRACE(""); |
| 184 | test_store_string(&field_dt, STRING_WITH_LEN("2001-13-01 00:00:01"), 0, |
| 185 | "0000-00-00 00:00:00", WARN_DATA_TRUNCATED, |
| 186 | TYPE_ERR_BAD_VALUE); |
| 187 | } |
| 188 | |
| 189 | // Bad day |
| 190 | { |
| 191 | SCOPED_TRACE(""); |
| 192 | test_store_string(&field_dt, STRING_WITH_LEN("2001-01-32 00:00:01"), 0, |
| 193 | "0000-00-00 00:00:00", WARN_DATA_TRUNCATED, |
| 194 | TYPE_ERR_BAD_VALUE); |
| 195 | } |
| 196 | |
| 197 | // Bad hour |
| 198 | { |
| 199 | SCOPED_TRACE(""); |
| 200 | test_store_string(&field_dt, STRING_WITH_LEN("2001-01-01 72:00:01"), 0, |
| 201 | "0000-00-00 00:00:00", WARN_DATA_TRUNCATED, |
| 202 | TYPE_ERR_BAD_VALUE); |
| 203 | } |
| 204 | |
| 205 | // Bad minute |
| 206 | { |
| 207 | SCOPED_TRACE(""); |
| 208 | test_store_string(&field_dt, STRING_WITH_LEN("2001-01-01 00:72:01"), 0, |
| 209 | "0000-00-00 00:00:00", WARN_DATA_TRUNCATED, |
| 210 | TYPE_ERR_BAD_VALUE); |
| 211 | } |
| 212 | |
| 213 | // Bad second |
| 214 | { |
| 215 | SCOPED_TRACE(""); |
| 216 | test_store_string(&field_dt, STRING_WITH_LEN("2001-01-01 00:00:72"), 0, |
| 217 | "0000-00-00 00:00:00", WARN_DATA_TRUNCATED, |
| 218 | TYPE_ERR_BAD_VALUE); |
| 219 | } |
| 220 | |
| 221 | // Not a day |
| 222 | { |
| 223 | SCOPED_TRACE(""); |
| 224 | test_store_string(&field_dt, STRING_WITH_LEN("foo"), 0, |
| 225 | "0000-00-00 00:00:00", WARN_DATA_TRUNCATED, |
| 226 | TYPE_ERR_BAD_VALUE); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // Store zero date using different combinations of SQL modes |
| 231 | static const int no_modes = 3; |
| 232 | static const sql_mode_t strict_modes[no_modes] = { |
| 233 | MODE_STRICT_TRANS_TABLES, MODE_STRICT_ALL_TABLES, |
| 234 | MODE_STRICT_TRANS_TABLES | MODE_STRICT_ALL_TABLES}; |
| 235 | |
| 236 | /** |
| 237 | Strictness mode test 1: |
| 238 | |
| 239 | Try storing dates with zeroes when no zero-restrictions apply |
| 240 | (neither NO_ZERO_DATE or NO_ZERO_IN_DATE are set). There should be |
| 241 | no errors, warnings or notes. |
| 242 | */ |
| 243 | TEST_F(FieldDatetimeTest, StoreZeroDateSqlModeNoZeroRestrictions) { |
| 244 | Mock_field_datetime field_dt; |
| 245 | Fake_TABLE table(&field_dt); |
| 246 | table.in_use = thd(); |
| 247 | field_dt.make_writable(); |
| 248 | field_dt.make_readable(); |
| 249 | thd()->check_for_truncated_fields = CHECK_FIELD_WARN; |
| 250 | |
| 251 | for (int i = 0; i < no_modes; i++) { |
| 252 | SCOPED_TRACE(""); |
| 253 | store_zero_in_sql_mode(&field_dt, STRING_WITH_LEN("0000-00-00 00:00:00"), |
| 254 | "0000-00-00 00:00:00", TYPE_OK, strict_modes[i], 0); |
| 255 | } |
| 256 | |
| 257 | for (int i = 0; i < no_modes; i++) { |
| 258 | SCOPED_TRACE(""); |
| 259 | store_zero_in_sql_mode(&field_dt, STRING_WITH_LEN("0000-01-01 00:00:00"), |
| 260 | "0000-01-01 00:00:00", TYPE_OK, strict_modes[i], 0); |
| 261 | } |
| 262 | |
| 263 | for (int i = 0; i < no_modes; i++) { |
| 264 | SCOPED_TRACE(""); |
| 265 | store_zero_in_sql_mode(&field_dt, STRING_WITH_LEN("2001-00-01 00:00:00"), |
| 266 | "2001-00-01 00:00:00", TYPE_OK, strict_modes[i], 0); |
| 267 | } |
| 268 | |
| 269 | for (int i = 0; i < no_modes; i++) { |
| 270 | SCOPED_TRACE(""); |
| 271 | store_zero_in_sql_mode(&field_dt, STRING_WITH_LEN("2001-01-00 00:00:00"), |
| 272 | "2001-01-00 00:00:00", TYPE_OK, strict_modes[i], 0); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | static const type_conversion_status nozero_expected_status[] = { |
| 277 | TYPE_ERR_BAD_VALUE, TYPE_ERR_BAD_VALUE, TYPE_ERR_BAD_VALUE}; |
| 278 | |
| 279 | /** |
| 280 | Strictness mode test 2: |
| 281 | |
| 282 | Try storing dates with zeroes when NO_ZERO_DATE flag is set. There |
| 283 | should be no errors, warnings or notes unless the entire date is |
| 284 | zero: "0000-00-00" |
| 285 | */ |
| 286 | TEST_F(FieldDatetimeTest, StoreZeroDateSqlModeNoZeroDate) { |
| 287 | Mock_field_datetime field_dt; |
| 288 | Fake_TABLE table(&field_dt); |
| 289 | table.in_use = thd(); |
| 290 | field_dt.make_writable(); |
| 291 | field_dt.make_readable(); |
| 292 | thd()->check_for_truncated_fields = CHECK_FIELD_WARN; |
| 293 | |
| 294 | // With "MODE_NO_ZERO_DATE" set - Errors if date is all null |
| 295 | for (int i = 0; i < no_modes; i++) { |
| 296 | SCOPED_TRACE(""); |
| 297 | store_zero_in_sql_mode(&field_dt, STRING_WITH_LEN("0000-00-00 00:00:00"), |
| 298 | "0000-00-00 00:00:00", nozero_expected_status[i], |
| 299 | MODE_NO_ZERO_DATE | strict_modes[i], |
| 300 | ER_TRUNCATED_WRONG_VALUE); |
| 301 | } |
| 302 | |
| 303 | // Zero year, month or day is fine |
| 304 | for (int i = 0; i < no_modes; i++) { |
| 305 | SCOPED_TRACE(""); |
| 306 | store_zero_in_sql_mode(&field_dt, STRING_WITH_LEN("0000-01-01 00:00:00"), |
| 307 | "0000-01-01 00:00:00", TYPE_OK, |
| 308 | MODE_NO_ZERO_DATE | strict_modes[i], 0); |
| 309 | } |
| 310 | |
| 311 | for (int i = 0; i < no_modes; i++) { |
| 312 | SCOPED_TRACE(""); |
| 313 | store_zero_in_sql_mode(&field_dt, STRING_WITH_LEN("2001-00-01 00:00:00"), |
| 314 | "2001-00-01 00:00:00", TYPE_OK, |
| 315 | MODE_NO_ZERO_DATE | strict_modes[i], 0); |
| 316 | } |
| 317 | |
| 318 | for (int i = 0; i < no_modes; i++) { |
| 319 | SCOPED_TRACE(""); |
| 320 | store_zero_in_sql_mode(&field_dt, STRING_WITH_LEN("2001-01-00 00:00:00"), |
| 321 | "2001-01-00 00:00:00", TYPE_OK, |
| 322 | MODE_NO_ZERO_DATE | strict_modes[i], 0); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | Strictness mode test 3: |
| 328 | |
| 329 | Try storing dates with zeroes when NO_ZERO_IN_DATE flag is set. There |
| 330 | should be no errors unless either month or day is zero. |
| 331 | */ |
| 332 | TEST_F(FieldDatetimeTest, StoreZeroDateSqlModeNoZeroInDate) { |
| 333 | Mock_field_datetime field_dt; |
| 334 | Fake_TABLE table(&field_dt); |
| 335 | table.in_use = thd(); |
| 336 | field_dt.make_writable(); |
| 337 | field_dt.make_readable(); |
| 338 | thd()->check_for_truncated_fields = CHECK_FIELD_WARN; |
| 339 | |
| 340 | // With "MODE_NO_ZERO_IN_DATE" set - Entire date zero is ok |
| 341 | for (int i = 0; i < no_modes; i++) { |
| 342 | SCOPED_TRACE(""); |
| 343 | store_zero_in_sql_mode(&field_dt, STRING_WITH_LEN("0000-00-00 00:00:00"), |
| 344 | "0000-00-00 00:00:00", TYPE_OK, |
| 345 | MODE_NO_ZERO_IN_DATE | strict_modes[i], 0); |
| 346 | } |
| 347 | |
| 348 | // Year 0 is valid in strict mode too |
| 349 | for (int i = 0; i < no_modes; i++) { |
| 350 | SCOPED_TRACE(""); |
| 351 | store_zero_in_sql_mode(&field_dt, STRING_WITH_LEN("0000-01-01 00:00:00"), |
| 352 | "0000-01-01 00:00:00", TYPE_OK, |
| 353 | MODE_NO_ZERO_IN_DATE | strict_modes[i], 0); |
| 354 | } |
| 355 | |
| 356 | // Month 0 is NOT valid in strict mode, stores all-zero date |
| 357 | for (int i = 0; i < no_modes; i++) { |
| 358 | SCOPED_TRACE(""); |
| 359 | store_zero_in_sql_mode(&field_dt, STRING_WITH_LEN("2001-00-01 00:00:00"), |
| 360 | "0000-00-00 00:00:00", nozero_expected_status[i], |
| 361 | MODE_NO_ZERO_IN_DATE | strict_modes[i], |
| 362 | ER_TRUNCATED_WRONG_VALUE); |
| 363 | } |
| 364 | |
| 365 | // Day 0 is NOT valid in strict mode, stores all-zero date |
| 366 | for (int i = 0; i < no_modes; i++) { |
| 367 | SCOPED_TRACE(""); |
| 368 | store_zero_in_sql_mode(&field_dt, STRING_WITH_LEN("2001-01-00 00:00:00"), |
| 369 | "0000-00-00 00:00:00", nozero_expected_status[i], |
| 370 | MODE_NO_ZERO_IN_DATE | strict_modes[i], |
| 371 | ER_TRUNCATED_WRONG_VALUE); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | } // namespace field_datetime_unittests |