| 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 "sql/sql_class.h" |
| 28 | #include "unittest/gunit/fake_table.h" |
| 29 | #include "unittest/gunit/field_temporal_utils.h" |
| 30 | #include "unittest/gunit/test_utils.h" |
| 31 | |
| 32 | namespace field_date_unittests { |
| 33 | |
| 34 | using my_testing::Mock_error_handler; |
| 35 | using my_testing::Server_initializer; |
| 36 | |
| 37 | class FieldDateTest : public ::testing::Test { |
| 38 | protected: |
| 39 | void SetUp() override { initializer.SetUp(); } |
| 40 | void TearDown() override { initializer.TearDown(); } |
| 41 | |
| 42 | THD *thd() { return initializer.thd(); } |
| 43 | |
| 44 | Server_initializer initializer; |
| 45 | |
| 46 | Field_set *create_field_set(TYPELIB *tl); |
| 47 | |
| 48 | // Store zero date using different combinations of SQL modes |
| 49 | static const int no_modes = 3; |
| 50 | static const sql_mode_t strict_modes[no_modes]; |
| 51 | |
| 52 | static const type_conversion_status nozero_expected_status[]; |
| 53 | }; |
| 54 | |
| 55 | const sql_mode_t FieldDateTest::strict_modes[no_modes] = { |
| 56 | MODE_STRICT_TRANS_TABLES, MODE_STRICT_ALL_TABLES, |
| 57 | MODE_STRICT_TRANS_TABLES | MODE_STRICT_ALL_TABLES}; |
| 58 | |
| 59 | const type_conversion_status FieldDateTest::nozero_expected_status[] = { |
| 60 | TYPE_ERR_BAD_VALUE, TYPE_ERR_BAD_VALUE, TYPE_ERR_BAD_VALUE}; |
| 61 | |
| 62 | class Mock_field_date : public Field_newdate { |
| 63 | public: |
| 64 | Mock_field_date() |
| 65 | : Field_newdate(nullptr, // ptr_arg |
| 66 | &Field::dummy_null_buffer, // null_ptr_arg |
| 67 | 1, // null_bit_arg |
| 68 | Field::NONE, // auto_flags_arg |
| 69 | "field_name") // field_name_arg |
| 70 | {} |
| 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(FieldDateTest, StoreLegalStringValues) { |
| 77 | Mock_field_date field_date; |
| 78 | Fake_TABLE table(&field_date); |
| 79 | table.in_use = thd(); |
| 80 | field_date.make_writable(); |
| 81 | field_date.make_readable(); |
| 82 | |
| 83 | { |
| 84 | SCOPED_TRACE(""); |
| 85 | test_store_string(&field_date, STRING_WITH_LEN("2001-01-01"), 0, |
| 86 | "2001-01-01", 0, TYPE_OK); |
| 87 | } |
| 88 | { |
| 89 | SCOPED_TRACE(""); |
| 90 | test_store_string(&field_date, STRING_WITH_LEN("0000-00-00"), 0, |
| 91 | "0000-00-00", 0, TYPE_OK); |
| 92 | } |
| 93 | { |
| 94 | SCOPED_TRACE(""); |
| 95 | test_store_string(&field_date, STRING_WITH_LEN("0001-00-00"), 0, |
| 96 | "0001-00-00", 0, TYPE_OK); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | TEST_F(FieldDateTest, StoreIllegalStringValues) { |
| 101 | Mock_field_date field_date; |
| 102 | Fake_TABLE table(&field_date); |
| 103 | table.in_use = thd(); |
| 104 | field_date.make_writable(); |
| 105 | field_date.make_readable(); |
| 106 | thd()->check_for_truncated_fields = CHECK_FIELD_WARN; |
| 107 | |
| 108 | // Truncates time |
| 109 | { |
| 110 | SCOPED_TRACE(""); |
| 111 | test_store_string(&field_date, STRING_WITH_LEN("2001-01-01 00:00:01"), 0, |
| 112 | "2001-01-01", WARN_DATA_TRUNCATED, |
| 113 | TYPE_NOTE_TIME_TRUNCATED); |
| 114 | } |
| 115 | |
| 116 | // Bad year |
| 117 | { |
| 118 | SCOPED_TRACE(""); |
| 119 | test_store_string(&field_date, STRING_WITH_LEN("99999-01-01"), 0, |
| 120 | "0000-00-00", WARN_DATA_TRUNCATED, TYPE_ERR_BAD_VALUE); |
| 121 | } |
| 122 | |
| 123 | // Bad month |
| 124 | { |
| 125 | SCOPED_TRACE(""); |
| 126 | test_store_string(&field_date, STRING_WITH_LEN("2001-13-01"), 0, |
| 127 | "0000-00-00", WARN_DATA_TRUNCATED, TYPE_ERR_BAD_VALUE); |
| 128 | } |
| 129 | |
| 130 | // Bad day |
| 131 | { |
| 132 | SCOPED_TRACE(""); |
| 133 | test_store_string(&field_date, STRING_WITH_LEN("2001-01-32"), 0, |
| 134 | "0000-00-00", WARN_DATA_TRUNCATED, TYPE_ERR_BAD_VALUE); |
| 135 | } |
| 136 | |
| 137 | // Not a date |
| 138 | { |
| 139 | SCOPED_TRACE(""); |
| 140 | test_store_string(&field_date, STRING_WITH_LEN("foo"), 0, "0000-00-00", |
| 141 | WARN_DATA_TRUNCATED, TYPE_ERR_BAD_VALUE); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | Strictness mode test 1: |
| 147 | |
| 148 | Try storing dates with zeroes when no zero-restrictions apply |
| 149 | (neither NO_ZERO_DATE or NO_ZERO_IN_DATE are set). There should be |
| 150 | no errors, warnings or notes. |
| 151 | */ |
| 152 | TEST_F(FieldDateTest, StoreZeroDateSqlModeNoZeroRestrictions) { |
| 153 | Mock_field_date field_date; |
| 154 | Fake_TABLE table(&field_date); |
| 155 | table.in_use = thd(); |
| 156 | field_date.make_writable(); |
| 157 | field_date.make_readable(); |
| 158 | thd()->check_for_truncated_fields = CHECK_FIELD_WARN; |
| 159 | |
| 160 | for (int i = 0; i < no_modes; i++) { |
| 161 | SCOPED_TRACE(""); |
| 162 | store_zero_in_sql_mode(&field_date, STRING_WITH_LEN("0000-00-00"), |
| 163 | "0000-00-00", TYPE_OK, strict_modes[i], 0); |
| 164 | } |
| 165 | |
| 166 | for (int i = 0; i < no_modes; i++) { |
| 167 | SCOPED_TRACE(""); |
| 168 | store_zero_in_sql_mode(&field_date, STRING_WITH_LEN("0000-01-01"), |
| 169 | "0000-01-01", TYPE_OK, strict_modes[i], 0); |
| 170 | } |
| 171 | |
| 172 | for (int i = 0; i < no_modes; i++) { |
| 173 | SCOPED_TRACE(""); |
| 174 | store_zero_in_sql_mode(&field_date, STRING_WITH_LEN("2001-00-01"), |
| 175 | "2001-00-01", TYPE_OK, strict_modes[i], 0); |
| 176 | } |
| 177 | |
| 178 | for (int i = 0; i < no_modes; i++) { |
| 179 | SCOPED_TRACE(""); |
| 180 | store_zero_in_sql_mode(&field_date, STRING_WITH_LEN("2001-01-00"), |
| 181 | "2001-01-00", TYPE_OK, strict_modes[i], 0); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | Strictness mode test 2: |
| 187 | |
| 188 | Try storing dates with zeroes when NO_ZERO_DATE flag is set. There |
| 189 | should be no errors, warnings or notes unless the entire date is |
| 190 | zero: "0000-00-00" |
| 191 | */ |
| 192 | TEST_F(FieldDateTest, StoreZeroDateSqlModeNoZeroDate) { |
| 193 | Mock_field_date field_date; |
| 194 | Fake_TABLE table(&field_date); |
| 195 | table.in_use = thd(); |
| 196 | field_date.make_writable(); |
| 197 | field_date.make_readable(); |
| 198 | thd()->check_for_truncated_fields = CHECK_FIELD_WARN; |
| 199 | |
| 200 | // With "MODE_NO_ZERO_DATE" set - Errors if date is all null |
| 201 | for (int i = 0; i < no_modes; i++) { |
| 202 | SCOPED_TRACE(""); |
| 203 | store_zero_in_sql_mode(&field_date, STRING_WITH_LEN("0000-00-00"), |
| 204 | "0000-00-00", nozero_expected_status[i], |
| 205 | MODE_NO_ZERO_DATE | strict_modes[i], |
| 206 | ER_TRUNCATED_WRONG_VALUE); |
| 207 | } |
| 208 | |
| 209 | // Zero year, month or day is fine |
| 210 | for (int i = 0; i < no_modes; i++) { |
| 211 | SCOPED_TRACE(""); |
| 212 | store_zero_in_sql_mode(&field_date, STRING_WITH_LEN("0000-01-01"), |
| 213 | "0000-01-01", TYPE_OK, |
| 214 | MODE_NO_ZERO_DATE | strict_modes[i], 0); |
| 215 | } |
| 216 | |
| 217 | for (int i = 0; i < no_modes; i++) { |
| 218 | SCOPED_TRACE(""); |
| 219 | store_zero_in_sql_mode(&field_date, STRING_WITH_LEN("2001-00-01"), |
| 220 | "2001-00-01", TYPE_OK, |
| 221 | MODE_NO_ZERO_DATE | strict_modes[i], 0); |
| 222 | } |
| 223 | |
| 224 | for (int i = 0; i < no_modes; i++) { |
| 225 | SCOPED_TRACE(""); |
| 226 | store_zero_in_sql_mode(&field_date, STRING_WITH_LEN("2001-01-00"), |
| 227 | "2001-01-00", TYPE_OK, |
| 228 | MODE_NO_ZERO_DATE | strict_modes[i], 0); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | Strictness mode test 3: |
| 234 | |
| 235 | Try storing dates with zeroes when NO_ZERO_IN_DATE flag is set. There |
| 236 | should be no errors unless either month or day is zero. |
| 237 | */ |
| 238 | TEST_F(FieldDateTest, StoreZeroDateSqlModeNoZeroInDate) { |
| 239 | Mock_field_date field_date; |
| 240 | Fake_TABLE table(&field_date); |
| 241 | table.in_use = thd(); |
| 242 | field_date.make_writable(); |
| 243 | field_date.make_readable(); |
| 244 | thd()->check_for_truncated_fields = CHECK_FIELD_WARN; |
| 245 | |
| 246 | // With "MODE_NO_ZERO_IN_DATE" set - Entire date zero is ok |
| 247 | for (int i = 0; i < no_modes; i++) { |
| 248 | SCOPED_TRACE(""); |
| 249 | store_zero_in_sql_mode(&field_date, STRING_WITH_LEN("0000-00-00"), |
| 250 | "0000-00-00", TYPE_OK, |
| 251 | MODE_NO_ZERO_IN_DATE | strict_modes[i], 0); |
| 252 | } |
| 253 | |
| 254 | // Year 0 is valid in strict mode too |
| 255 | for (int i = 0; i < no_modes; i++) { |
| 256 | SCOPED_TRACE(""); |
| 257 | store_zero_in_sql_mode(&field_date, STRING_WITH_LEN("0000-01-01"), |
| 258 | "0000-01-01", TYPE_OK, |
| 259 | MODE_NO_ZERO_IN_DATE | strict_modes[i], 0); |
| 260 | } |
| 261 | |
| 262 | // Month 0 is NOT valid in strict mode, stores all-zero date |
| 263 | for (int i = 0; i < no_modes; i++) { |
| 264 | SCOPED_TRACE(""); |
| 265 | store_zero_in_sql_mode(&field_date, STRING_WITH_LEN("2001-00-01"), |
| 266 | "0000-00-00", nozero_expected_status[i], |
| 267 | MODE_NO_ZERO_IN_DATE | strict_modes[i], |
| 268 | ER_TRUNCATED_WRONG_VALUE); |
| 269 | } |
| 270 | |
| 271 | // Day 0 is NOT valid in strict mode, stores all-zero date |
| 272 | for (int i = 0; i < no_modes; i++) { |
| 273 | SCOPED_TRACE(""); |
| 274 | store_zero_in_sql_mode(&field_date, STRING_WITH_LEN("2001-01-00"), |
| 275 | "0000-00-00", nozero_expected_status[i], |
| 276 | MODE_NO_ZERO_IN_DATE | strict_modes[i], |
| 277 | ER_TRUNCATED_WRONG_VALUE); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | } // namespace field_date_unittests |