| 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/mock_field_long.h" |
| 30 | #include "unittest/gunit/test_utils.h" |
| 31 | |
| 32 | namespace field_long_unittest { |
| 33 | |
| 34 | using my_testing::Mock_error_handler; |
| 35 | using my_testing::Server_initializer; |
| 36 | |
| 37 | class FieldLongTest : 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 | |
| 49 | void test_store_long(Field_long *field, const longlong store_value, |
| 50 | const longlong expected_result, |
| 51 | const int expected_error_no, |
| 52 | const type_conversion_status expected_status) { |
| 53 | Mock_error_handler error_handler(field->table->in_use, expected_error_no); |
| 54 | type_conversion_status err = field->store(store_value, false); // signed |
| 55 | EXPECT_EQ(expected_result, field->val_int()); |
| 56 | EXPECT_FALSE(field->is_null()); |
| 57 | EXPECT_EQ(expected_status, err); |
| 58 | EXPECT_EQ((expected_error_no == 0 ? 0 : 1), error_handler.handle_called()); |
| 59 | } |
| 60 | |
| 61 | void test_store_string(Field_long *field, const char *store_value, |
| 62 | const int length, const longlong expected_result, |
| 63 | const int expected_error_no, |
| 64 | const type_conversion_status expected_status) { |
| 65 | Mock_error_handler error_handler(field->table->in_use, expected_error_no); |
| 66 | type_conversion_status err = |
| 67 | field->store(store_value, length, &my_charset_latin1); |
| 68 | EXPECT_EQ(expected_result, field->val_int()); |
| 69 | EXPECT_FALSE(field->is_null()); |
| 70 | EXPECT_EQ(expected_status, err); |
| 71 | EXPECT_EQ((expected_error_no == 0 ? 0 : 1), error_handler.handle_called()); |
| 72 | } |
| 73 | |
| 74 | TEST_F(FieldLongTest, StoreLegalIntValues) { |
| 75 | Mock_field_long field_long(false); |
| 76 | Fake_TABLE table(&field_long); |
| 77 | table.in_use = thd(); |
| 78 | field_long.make_writable(); |
| 79 | field_long.make_readable(); |
| 80 | thd()->check_for_truncated_fields = CHECK_FIELD_WARN; |
| 81 | |
| 82 | SCOPED_TRACE(""); |
| 83 | test_store_long(&field_long, 0, 0, 0, TYPE_OK); |
| 84 | SCOPED_TRACE(""); |
| 85 | test_store_long(&field_long, 5, 5, 0, TYPE_OK); |
| 86 | SCOPED_TRACE(""); |
| 87 | test_store_long(&field_long, -1, -1, 0, TYPE_OK); |
| 88 | |
| 89 | { |
| 90 | SCOPED_TRACE(""); |
| 91 | test_store_long(&field_long, INT_MIN32, INT_MIN32, 0, TYPE_OK); |
| 92 | } |
| 93 | { |
| 94 | SCOPED_TRACE(""); |
| 95 | test_store_long(&field_long, INT_MAX32, INT_MAX32, 0, TYPE_OK); |
| 96 | } |
| 97 | |
| 98 | { |
| 99 | Mock_error_handler error_handler(thd(), 0); |
| 100 | type_conversion_status err; |
| 101 | err = set_field_to_null(&field_long); |
| 102 | |
| 103 | EXPECT_EQ(0, field_long.val_int()); |
| 104 | EXPECT_TRUE(field_long.is_null()); |
| 105 | EXPECT_EQ(TYPE_OK, err); |
| 106 | |
| 107 | field_long.set_notnull(); |
| 108 | EXPECT_EQ(0, field_long.val_int()); |
| 109 | EXPECT_FALSE(field_long.is_null()); |
| 110 | |
| 111 | // None of the above should generate warnings |
| 112 | EXPECT_EQ(0, error_handler.handle_called()); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // Values higher and lower than valid range for the Field_long |
| 117 | TEST_F(FieldLongTest, StoreOutOfRangeIntValues) { |
| 118 | // Field_long is signed |
| 119 | { |
| 120 | Mock_field_long field_long(false); |
| 121 | Fake_TABLE table(&field_long); |
| 122 | table.in_use = thd(); |
| 123 | field_long.make_writable(); |
| 124 | field_long.make_readable(); |
| 125 | thd()->check_for_truncated_fields = CHECK_FIELD_WARN; |
| 126 | |
| 127 | { |
| 128 | SCOPED_TRACE(""); |
| 129 | test_store_long(&field_long, INT_MAX32 + 1LL, INT_MAX32, |
| 130 | ER_WARN_DATA_OUT_OF_RANGE, TYPE_WARN_OUT_OF_RANGE); |
| 131 | } |
| 132 | { |
| 133 | SCOPED_TRACE(""); |
| 134 | test_store_long(&field_long, INT_MIN32 - 1LL, INT_MIN32, |
| 135 | ER_WARN_DATA_OUT_OF_RANGE, TYPE_WARN_OUT_OF_RANGE); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // Field_long is unsigned |
| 140 | { |
| 141 | Mock_field_long field_long(true); |
| 142 | Fake_TABLE table(&field_long); |
| 143 | table.in_use = thd(); |
| 144 | field_long.make_writable(); |
| 145 | field_long.make_readable(); |
| 146 | thd()->check_for_truncated_fields = CHECK_FIELD_WARN; |
| 147 | |
| 148 | { |
| 149 | SCOPED_TRACE(""); |
| 150 | test_store_long(&field_long, -1LL, 0, ER_WARN_DATA_OUT_OF_RANGE, |
| 151 | TYPE_WARN_OUT_OF_RANGE); |
| 152 | } |
| 153 | { |
| 154 | SCOPED_TRACE(""); |
| 155 | test_store_long(&field_long, INT_MIN32, 0, ER_WARN_DATA_OUT_OF_RANGE, |
| 156 | TYPE_WARN_OUT_OF_RANGE); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | TEST_F(FieldLongTest, StoreLegalStringValues) { |
| 162 | const char min_int[] = "-2147483648"; |
| 163 | const char max_int[] = "2147483647"; |
| 164 | const char max_int_plus1[] = "2147483648"; |
| 165 | const char max_uint[] = "4294967295"; |
| 166 | thd()->check_for_truncated_fields = CHECK_FIELD_WARN; |
| 167 | |
| 168 | // Field_long is signed |
| 169 | { |
| 170 | Mock_field_long field_long(false); |
| 171 | |
| 172 | Fake_TABLE table(&field_long); |
| 173 | table.in_use = thd(); |
| 174 | field_long.make_writable(); |
| 175 | field_long.make_readable(); |
| 176 | |
| 177 | { |
| 178 | SCOPED_TRACE(""); |
| 179 | test_store_string(&field_long, STRING_WITH_LEN("0"), 0, 0, TYPE_OK); |
| 180 | } |
| 181 | { |
| 182 | SCOPED_TRACE(""); |
| 183 | test_store_string(&field_long, STRING_WITH_LEN("1"), 1, 0, TYPE_OK); |
| 184 | } |
| 185 | { |
| 186 | SCOPED_TRACE(""); |
| 187 | test_store_string(&field_long, STRING_WITH_LEN("-1"), -1, 0, TYPE_OK); |
| 188 | } |
| 189 | { |
| 190 | SCOPED_TRACE(""); |
| 191 | test_store_string(&field_long, STRING_WITH_LEN(max_int), INT_MAX32, 0, |
| 192 | TYPE_OK); |
| 193 | } |
| 194 | { |
| 195 | SCOPED_TRACE(""); |
| 196 | test_store_string(&field_long, STRING_WITH_LEN(min_int), INT_MIN32, 0, |
| 197 | TYPE_OK); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // Field_long is unsigned |
| 202 | { |
| 203 | Mock_field_long field_long(true); |
| 204 | |
| 205 | Fake_TABLE table(&field_long); |
| 206 | table.in_use = thd(); |
| 207 | field_long.make_writable(); |
| 208 | field_long.make_readable(); |
| 209 | |
| 210 | { |
| 211 | SCOPED_TRACE(""); |
| 212 | test_store_string(&field_long, STRING_WITH_LEN(max_int_plus1), |
| 213 | INT_MAX32 + 1LL, 0, TYPE_OK); |
| 214 | } |
| 215 | { |
| 216 | SCOPED_TRACE(""); |
| 217 | test_store_string(&field_long, STRING_WITH_LEN(max_uint), UINT_MAX32, 0, |
| 218 | TYPE_OK); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | TEST_F(FieldLongTest, StoreIllegalStringValues) { |
| 224 | // Field_long is signed - Stored value is INT_MIN32/INT_MAX32 |
| 225 | // depending on sign of string to store |
| 226 | const char very_high[] = "999999999999999"; |
| 227 | const char very_low[] = "-999999999999999"; |
| 228 | thd()->check_for_truncated_fields = CHECK_FIELD_WARN; |
| 229 | { |
| 230 | Mock_field_long field_long(false); |
| 231 | |
| 232 | Fake_TABLE table(&field_long); |
| 233 | table.in_use = thd(); |
| 234 | field_long.make_writable(); |
| 235 | field_long.make_readable(); |
| 236 | |
| 237 | const char max_int_plus1[] = "2147483648"; |
| 238 | const char min_int_minus1[] = "-2147483649"; |
| 239 | |
| 240 | { |
| 241 | SCOPED_TRACE(""); |
| 242 | test_store_string(&field_long, STRING_WITH_LEN(max_int_plus1), INT_MAX32, |
| 243 | ER_WARN_DATA_OUT_OF_RANGE, TYPE_WARN_OUT_OF_RANGE); |
| 244 | } |
| 245 | { |
| 246 | SCOPED_TRACE(""); |
| 247 | test_store_string(&field_long, STRING_WITH_LEN(very_high), INT_MAX32, |
| 248 | ER_WARN_DATA_OUT_OF_RANGE, TYPE_WARN_OUT_OF_RANGE); |
| 249 | } |
| 250 | { |
| 251 | SCOPED_TRACE(""); |
| 252 | test_store_string(&field_long, STRING_WITH_LEN(min_int_minus1), INT_MIN32, |
| 253 | ER_WARN_DATA_OUT_OF_RANGE, TYPE_WARN_OUT_OF_RANGE); |
| 254 | } |
| 255 | { |
| 256 | SCOPED_TRACE(""); |
| 257 | test_store_string(&field_long, STRING_WITH_LEN(very_low), INT_MIN32, |
| 258 | ER_WARN_DATA_OUT_OF_RANGE, TYPE_WARN_OUT_OF_RANGE); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | // Field_long is unsigned - Stored value is 0/UINT_MAX32 |
| 263 | // depending on sign of string to store |
| 264 | { |
| 265 | Mock_field_long field_long(true); |
| 266 | |
| 267 | Fake_TABLE table(&field_long); |
| 268 | table.in_use = thd(); |
| 269 | field_long.make_writable(); |
| 270 | field_long.make_readable(); |
| 271 | |
| 272 | const char min_int[] = "-2147483648"; |
| 273 | const char max_uint_plus1[] = "4294967296"; |
| 274 | |
| 275 | { |
| 276 | SCOPED_TRACE(""); |
| 277 | test_store_string(&field_long, STRING_WITH_LEN(max_uint_plus1), |
| 278 | UINT_MAX32, ER_WARN_DATA_OUT_OF_RANGE, |
| 279 | TYPE_WARN_OUT_OF_RANGE); |
| 280 | } |
| 281 | { |
| 282 | SCOPED_TRACE(""); |
| 283 | test_store_string(&field_long, STRING_WITH_LEN(very_high), UINT_MAX32, |
| 284 | ER_WARN_DATA_OUT_OF_RANGE, TYPE_WARN_OUT_OF_RANGE); |
| 285 | } |
| 286 | { |
| 287 | SCOPED_TRACE(""); |
| 288 | test_store_string(&field_long, STRING_WITH_LEN("-1"), 0, |
| 289 | ER_WARN_DATA_OUT_OF_RANGE, TYPE_WARN_OUT_OF_RANGE); |
| 290 | } |
| 291 | { |
| 292 | SCOPED_TRACE(""); |
| 293 | test_store_string(&field_long, STRING_WITH_LEN(min_int), 0, |
| 294 | ER_WARN_DATA_OUT_OF_RANGE, TYPE_WARN_OUT_OF_RANGE); |
| 295 | } |
| 296 | { |
| 297 | SCOPED_TRACE(""); |
| 298 | test_store_string(&field_long, STRING_WITH_LEN(very_low), 0, |
| 299 | ER_WARN_DATA_OUT_OF_RANGE, TYPE_WARN_OUT_OF_RANGE); |
| 300 | } |
| 301 | |
| 302 | // Invalid value |
| 303 | { |
| 304 | SCOPED_TRACE(""); |
| 305 | test_store_string(&field_long, STRING_WITH_LEN("foo"), 0, |
| 306 | ER_TRUNCATED_WRONG_VALUE_FOR_FIELD, TYPE_ERR_BAD_VALUE); |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | TEST_F(FieldLongTest, StoreNullValue) { |
| 312 | Mock_field_long field_long(false); |
| 313 | |
| 314 | Fake_TABLE table(&field_long); |
| 315 | table.in_use = thd(); |
| 316 | field_long.make_writable(); |
| 317 | field_long.make_readable(); |
| 318 | thd()->check_for_truncated_fields = CHECK_FIELD_WARN; |
| 319 | |
| 320 | type_conversion_status err; |
| 321 | |
| 322 | // Save NULL value in a field that can have NULL value |
| 323 | { |
| 324 | Mock_error_handler error_handler(thd(), 0); |
| 325 | err = set_field_to_null(&field_long); |
| 326 | EXPECT_EQ(0, field_long.val_int()); |
| 327 | EXPECT_EQ(TYPE_OK, err); |
| 328 | |
| 329 | err = set_field_to_null_with_conversions(&field_long, true); |
| 330 | EXPECT_EQ(0, field_long.val_int()); |
| 331 | EXPECT_EQ(TYPE_OK, err); |
| 332 | |
| 333 | err = set_field_to_null_with_conversions(&field_long, false); |
| 334 | EXPECT_EQ(0, field_long.val_int()); |
| 335 | EXPECT_EQ(TYPE_OK, err); |
| 336 | |
| 337 | EXPECT_EQ(0, error_handler.handle_called()); |
| 338 | } |
| 339 | |
| 340 | // Save NULL value in a field that can NOT have NULL value |
| 341 | field_long.set_null_ptr(nullptr, 0); |
| 342 | { |
| 343 | Mock_error_handler error_handler(thd(), 0); |
| 344 | // Save NULL value in a field that can be set to NULL temporary |
| 345 | field_long.set_tmp_nullable(); |
| 346 | err = set_field_to_null(&field_long); |
| 347 | EXPECT_EQ(0, field_long.val_int()); |
| 348 | EXPECT_EQ(TYPE_OK, err); |
| 349 | EXPECT_EQ(0, error_handler.handle_called()); |
| 350 | field_long.reset_tmp_nullable(); |
| 351 | } |
| 352 | |
| 353 | { |
| 354 | Mock_error_handler error_handler(thd(), 0); |
| 355 | err = set_field_to_null_with_conversions(&field_long, true); |
| 356 | EXPECT_EQ(0, field_long.val_int()); |
| 357 | EXPECT_EQ(TYPE_ERR_NULL_CONSTRAINT_VIOLATION, err); |
| 358 | EXPECT_EQ(0, error_handler.handle_called()); |
| 359 | } |
| 360 | |
| 361 | { |
| 362 | Mock_error_handler error_handler(thd(), ER_BAD_NULL_ERROR); |
| 363 | err = set_field_to_null_with_conversions(&field_long, false); |
| 364 | EXPECT_EQ(0, field_long.val_int()); |
| 365 | EXPECT_EQ(TYPE_OK, err); |
| 366 | EXPECT_EQ(1, error_handler.handle_called()); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | } // namespace field_long_unittest |