| 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 "my_inttypes.h" |
| 28 | #include "sql/field.h" |
| 29 | #include "sql/my_decimal.h" |
| 30 | #include "sql/protocol.h" |
| 31 | #include "sql/sql_class.h" |
| 32 | #include "sql/sql_time.h" |
| 33 | #include "unittest/gunit/fake_table.h" |
| 34 | #include "unittest/gunit/mysys_util.h" |
| 35 | #include "unittest/gunit/test_utils.h" |
| 36 | |
| 37 | namespace field_unittests { |
| 38 | |
| 39 | using my_testing::Mock_error_handler; |
| 40 | using my_testing::Server_initializer; |
| 41 | |
| 42 | class FieldTest : public ::testing::Test { |
| 43 | protected: |
| 44 | void SetUp() override { initializer.SetUp(); } |
| 45 | void TearDown() override { initializer.TearDown(); } |
| 46 | |
| 47 | THD *thd() { return initializer.thd(); } |
| 48 | |
| 49 | Server_initializer initializer; |
| 50 | |
| 51 | Field_set *create_field_set(TYPELIB *tl); |
| 52 | }; |
| 53 | |
| 54 | static void compareMysqlTime(const MYSQL_TIME &first, |
| 55 | const MYSQL_TIME &second) { |
| 56 | EXPECT_EQ(first.year, second.year); |
| 57 | EXPECT_EQ(first.month, second.month); |
| 58 | EXPECT_EQ(first.day, second.day); |
| 59 | EXPECT_EQ(first.hour, second.hour); |
| 60 | EXPECT_EQ(first.minute, second.minute); |
| 61 | EXPECT_EQ(first.second, second.second); |
| 62 | EXPECT_EQ(first.second_part, second.second_part); |
| 63 | EXPECT_EQ(first.neg, second.neg); |
| 64 | EXPECT_EQ(first.time_type, second.time_type); |
| 65 | } |
| 66 | |
| 67 | class Mock_table : public TABLE { |
| 68 | public: |
| 69 | Mock_table(THD *thd) { |
| 70 | null_row = false; |
| 71 | read_set = nullptr; |
| 72 | in_use = thd; |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | // A mock Protocol class to be able to test Field::send_binary |
| 77 | // It just verifies that store_time has been passed what is expected |
| 78 | class Mock_protocol : public Protocol { |
| 79 | private: |
| 80 | MYSQL_TIME t; |
| 81 | uint p; |
| 82 | |
| 83 | public: |
| 84 | Mock_protocol(THD *) {} |
| 85 | |
| 86 | bool store_time(const MYSQL_TIME &time, uint precision) override { |
| 87 | t = time; |
| 88 | p = precision; |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | void verify_time(const MYSQL_TIME &time, uint precision) { |
| 93 | compareMysqlTime(time, t); |
| 94 | EXPECT_EQ(precision, p); |
| 95 | } |
| 96 | |
| 97 | // Lots of functions that require implementation |
| 98 | int read_packet() override { return 0; } |
| 99 | ulong get_client_capabilities() override { return 0; } |
| 100 | bool has_client_capability(unsigned long) override { return false; } |
| 101 | void end_partial_result_set() override {} |
| 102 | int shutdown(bool server_shutdown [[maybe_unused]] = false) override { |
| 103 | return 0; |
| 104 | } |
| 105 | SSL_handle get_ssl() { return nullptr; } |
| 106 | void start_row() override {} |
| 107 | bool end_row() override { return false; } |
| 108 | bool connection_alive() const override { return false; } |
| 109 | void abort_row() override {} |
| 110 | uint get_rw_status() override { return 0; } |
| 111 | bool get_compression() override { return false; } |
| 112 | bool start_result_metadata(uint, uint, const CHARSET_INFO *) override { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | char *get_compression_algorithm() override { return nullptr; } |
| 117 | uint get_compression_level() override { return 0; } |
| 118 | |
| 119 | bool store_ps_status(ulong, uint, uint, ulong) override { return false; } |
| 120 | bool send_parameters(List<Item_param> *, bool) override { return false; } |
| 121 | |
| 122 | void send_num_fields(uint) {} |
| 123 | void send_num_rows(uint) {} |
| 124 | bool send_field_metadata(Send_field *, const CHARSET_INFO *) override { |
| 125 | return false; |
| 126 | } |
| 127 | bool send_ok(uint, uint, ulonglong, ulonglong, const char *) override { |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | bool send_eof(uint, uint) override { return false; } |
| 132 | bool send_error(uint, const char *, const char *) override { return false; } |
| 133 | bool end_result_metadata() override { return false; } |
| 134 | |
| 135 | bool store_null() override { return false; } |
| 136 | bool store_tiny(longlong, uint32) override { return false; } |
| 137 | bool store_short(longlong, uint32) override { return false; } |
| 138 | bool store_long(longlong, uint32) override { return false; } |
| 139 | bool store_longlong(longlong, bool, uint32) override { return false; } |
| 140 | bool store_decimal(const my_decimal *, uint, uint) override { return false; } |
| 141 | bool store_string(const char *, size_t, const CHARSET_INFO *) override { |
| 142 | return false; |
| 143 | } |
| 144 | bool store_float(float, uint32, uint32) override { return false; } |
| 145 | bool store_double(double, uint32, uint32) override { return false; } |
| 146 | bool store_datetime(const MYSQL_TIME &, uint) override { return false; } |
| 147 | bool store_date(const MYSQL_TIME &) override { return false; } |
| 148 | bool store_field(const Field *) override { return false; } |
| 149 | enum enum_protocol_type type() const override { return PROTOCOL_LOCAL; } |
| 150 | enum enum_vio_type connection_type() const override { return NO_VIO_TYPE; } |
| 151 | int get_command(COM_DATA *, enum_server_command *) override { return -1; } |
| 152 | bool flush() override { return true; } |
| 153 | }; |
| 154 | |
| 155 | TEST_F(FieldTest, FieldTimef) { |
| 156 | uchar fieldBuf[7]; |
| 157 | MysqlTime time(0, 0, 0, 12, 23, 12, 123400, false, MYSQL_TIMESTAMP_TIME); |
| 158 | |
| 159 | Field_timef *field = new (thd()->mem_root) |
| 160 | Field_timef(fieldBuf + 1, fieldBuf, false, Field::NONE, "f1", 4); |
| 161 | // Test public member functions |
| 162 | EXPECT_EQ(4UL, field->decimals()); // TS-TODO |
| 163 | EXPECT_EQ(MYSQL_TYPE_TIME, field->type()); |
| 164 | EXPECT_EQ(MYSQL_TYPE_TIME2, field->binlog_type()); |
| 165 | |
| 166 | longlong packed = TIME_to_longlong_packed(time); |
| 167 | |
| 168 | EXPECT_EQ(0, field->store_packed(packed)); |
| 169 | EXPECT_DOUBLE_EQ(122312.1234, field->val_real()); |
| 170 | EXPECT_EQ(122312, field->val_int()); |
| 171 | EXPECT_EQ(packed, field->val_time_temporal()); |
| 172 | |
| 173 | my_decimal decval; |
| 174 | my_decimal *dec = field->val_decimal(&decval); |
| 175 | double res; |
| 176 | my_decimal2double(0, dec, &res); |
| 177 | EXPECT_DOUBLE_EQ(122312.1234, res); |
| 178 | |
| 179 | EXPECT_EQ(5UL, field->pack_length()); |
| 180 | EXPECT_EQ(5UL, field->pack_length_from_metadata(4)); |
| 181 | EXPECT_EQ(5UL, field->row_pack_length()); |
| 182 | |
| 183 | String str(7); |
| 184 | field->sql_type(str); |
| 185 | EXPECT_STREQ("time(4)", str.c_ptr_safe()); |
| 186 | |
| 187 | EXPECT_EQ(1, field->zero_pack()); |
| 188 | EXPECT_EQ(&my_charset_bin, field->sort_charset()); |
| 189 | |
| 190 | // Test clone |
| 191 | Field *copy = field->clone(thd()->mem_root); |
| 192 | EXPECT_EQ(field->decimals(), copy->decimals()); |
| 193 | EXPECT_EQ(field->type(), copy->type()); |
| 194 | EXPECT_DOUBLE_EQ(field->val_real(), copy->val_real()); |
| 195 | EXPECT_EQ(field->val_int(), copy->val_int()); |
| 196 | EXPECT_EQ(field->val_time_temporal(), copy->val_time_temporal()); |
| 197 | EXPECT_EQ(0, field->cmp(field->field_ptr(), copy->field_ptr())); |
| 198 | |
| 199 | // Test reset |
| 200 | EXPECT_EQ(0, field->reset()); |
| 201 | EXPECT_DOUBLE_EQ(0.0, field->val_real()); |
| 202 | EXPECT_EQ(0, field->val_int()); |
| 203 | |
| 204 | // Test inherited member functions |
| 205 | // Functions inherited from Field_time_common |
| 206 | field->store_time(&time, 4); |
| 207 | EXPECT_EQ(4UL, field->decimals()); |
| 208 | EXPECT_EQ(MYSQL_TYPE_TIME, field->type()); |
| 209 | EXPECT_DOUBLE_EQ(122312.1234, field->val_real()); |
| 210 | EXPECT_EQ(122312, field->val_int()); |
| 211 | EXPECT_EQ(packed, field->val_time_temporal()); |
| 212 | |
| 213 | String timeStr(15); |
| 214 | EXPECT_STREQ("12:23:12.1234", field->val_str(&timeStr, &timeStr)->c_ptr()); |
| 215 | |
| 216 | field->store_time(&time, 0); |
| 217 | EXPECT_DOUBLE_EQ(122312.1234, field->val_real()); // Correct? |
| 218 | |
| 219 | MYSQL_TIME dateTime; |
| 220 | MysqlTime bigTime(0, 0, 0, 123, 45, 45, 555500, false, MYSQL_TIMESTAMP_TIME); |
| 221 | EXPECT_EQ(0, field->store_time(&bigTime, 4)); |
| 222 | EXPECT_FALSE(field->get_date(&dateTime, 0)); |
| 223 | |
| 224 | make_datetime((Date_time_format *)nullptr, &dateTime, &timeStr, 6); |
| 225 | // Skip 'yyyy-mm-dd ' since that will depend on current time zone. |
| 226 | EXPECT_STREQ("03:45:45.555500", timeStr.c_ptr() + 11); |
| 227 | |
| 228 | MYSQL_TIME t; |
| 229 | EXPECT_FALSE(field->get_time(&t)); |
| 230 | compareMysqlTime(bigTime, t); |
| 231 | |
| 232 | Mock_protocol protocol(thd()); |
| 233 | EXPECT_EQ(protocol.connection_type(), NO_VIO_TYPE); |
| 234 | EXPECT_FALSE(field->send_to_protocol(&protocol)); |
| 235 | protocol.verify_time(bigTime, 4); |
| 236 | |
| 237 | // Function inherited from Field_temporal |
| 238 | EXPECT_TRUE(is_temporal_type(field->type())); |
| 239 | EXPECT_EQ(STRING_RESULT, field->result_type()); |
| 240 | EXPECT_EQ(15UL, field->max_display_length()); |
| 241 | EXPECT_TRUE(field->str_needs_quotes()); |
| 242 | |
| 243 | // Not testing is_equal() yet, will require a mock TABLE object |
| 244 | // Create_field cf(field, field); |
| 245 | // EXPECT_TRUE(field->is_equal(&cf)); |
| 246 | |
| 247 | EXPECT_EQ(DECIMAL_RESULT, field->numeric_context_result_type()); |
| 248 | EXPECT_EQ(INT_RESULT, field->cmp_type()); |
| 249 | EXPECT_EQ(INT_RESULT, field->cmp_type()); |
| 250 | EXPECT_EQ(DERIVATION_NUMERIC, field->derivation()); |
| 251 | EXPECT_EQ(&my_charset_numeric, field->charset()); |
| 252 | EXPECT_TRUE(field->can_be_compared_as_longlong()); |
| 253 | EXPECT_TRUE(field->binary()); |
| 254 | // Below is not tested, because of ASSERT |
| 255 | // EXPECT_EQ(TIMESTAMP_NO_AUTO_SET, field->get_auto_set_type()); |
| 256 | |
| 257 | // Not testing make_field, it also needs a mock TABLE object |
| 258 | |
| 259 | EXPECT_EQ(TYPE_OK, field->store("12:23:12.123456", 15, &my_charset_numeric)); |
| 260 | EXPECT_DOUBLE_EQ(122312.1235, field->val_real()); |
| 261 | |
| 262 | EXPECT_EQ(TYPE_OK, field->store_decimal(dec)); |
| 263 | EXPECT_DOUBLE_EQ(122312.1234, field->val_real()); |
| 264 | |
| 265 | EXPECT_EQ(TYPE_OK, field->store(-234545, false)); |
| 266 | EXPECT_DOUBLE_EQ(-234545.0, field->val_real()); |
| 267 | |
| 268 | { |
| 269 | // Test that store() with a to big number gives right error |
| 270 | Mock_error_handler error_handler(thd(), ER_TRUNCATED_WRONG_VALUE); |
| 271 | EXPECT_EQ(TYPE_WARN_OUT_OF_RANGE, field->store(0x80000000, true)); |
| 272 | // Test that error handler was actually called |
| 273 | EXPECT_EQ(1, error_handler.handle_called()); |
| 274 | // Test that field contains expecte max time value |
| 275 | EXPECT_DOUBLE_EQ(8385959, field->val_real()); // Max time value |
| 276 | } |
| 277 | |
| 278 | EXPECT_EQ(TYPE_OK, field->store(1234545.555555)); |
| 279 | EXPECT_DOUBLE_EQ(1234545.5556, field->val_real()); |
| 280 | |
| 281 | // Some of the functions inherited from Field |
| 282 | Field *f = field; |
| 283 | EXPECT_EQ(TYPE_OK, f->store_time(&time, MYSQL_TIMESTAMP_TIME)); |
| 284 | EXPECT_DOUBLE_EQ(122312.1234, f->val_real()); // Why decimals here? |
| 285 | EXPECT_STREQ("12:23:12.1234", f->val_str(&timeStr)->c_ptr()); |
| 286 | EXPECT_STREQ("122312", f->val_int_as_str(&timeStr, false)->c_ptr()); |
| 287 | EXPECT_TRUE(f->eq(copy)); |
| 288 | EXPECT_TRUE(f->eq_def(copy)); |
| 289 | |
| 290 | // Not testing store(const char, uint, const CHARSET_INFO *, |
| 291 | // enum_check_fields) it requires a mock table |
| 292 | |
| 293 | Mock_table m_table(thd()); |
| 294 | f->table = &m_table; |
| 295 | my_timeval tv; |
| 296 | int warnings = 0; |
| 297 | EXPECT_FALSE(f->get_timestamp(&tv, &warnings)); |
| 298 | EXPECT_EQ(123400, tv.m_tv_usec); |
| 299 | |
| 300 | destroy(field); |
| 301 | } |
| 302 | |
| 303 | TEST_F(FieldTest, FieldTimefCompare) { |
| 304 | const int nFields = 7; |
| 305 | uchar fieldBufs[nFields][7]; |
| 306 | |
| 307 | MysqlTime times[nFields] = { |
| 308 | {0, 0, 0, 12, 23, 12, 100000, true, MYSQL_TIMESTAMP_TIME}, |
| 309 | {0, 0, 0, 0, 0, 0, 10000, true, MYSQL_TIMESTAMP_TIME}, |
| 310 | {0, 0, 0, 0, 0, 0, 0, false, MYSQL_TIMESTAMP_TIME}, |
| 311 | {0, 0, 0, 0, 0, 0, 999900, false, MYSQL_TIMESTAMP_TIME}, |
| 312 | {0, 0, 0, 0, 0, 0, 999990, false, MYSQL_TIMESTAMP_TIME}, |
| 313 | {0, 0, 0, 11, 59, 59, 999999, false, MYSQL_TIMESTAMP_TIME}, |
| 314 | {0, 0, 0, 12, 00, 00, 100000, false, MYSQL_TIMESTAMP_TIME}}; |
| 315 | |
| 316 | Field *fields[nFields]; |
| 317 | uchar sortStrings[nFields][6]; |
| 318 | for (int i = 0; i < nFields; ++i) { |
| 319 | char fieldName[3]; |
| 320 | sprintf(fieldName, "f%c", i); |
| 321 | fields[i] = new (thd()->mem_root) Field_timef( |
| 322 | fieldBufs[i] + 1, fieldBufs[i], false, Field::NONE, fieldName, 6); |
| 323 | |
| 324 | longlong packed = TIME_to_longlong_packed(times[i]); |
| 325 | EXPECT_EQ(0, fields[i]->store_packed(packed)); |
| 326 | fields[i]->make_sort_key(sortStrings[i], fields[i]->pack_length()); |
| 327 | } |
| 328 | |
| 329 | for (int i = 0; i < nFields; ++i) |
| 330 | for (int j = 0; j < nFields; ++j) { |
| 331 | String tmp; |
| 332 | if (i < j) { |
| 333 | EXPECT_GT( |
| 334 | 0, memcmp(sortStrings[i], sortStrings[j], fields[i]->pack_length())) |
| 335 | << fields[i]->val_str(&tmp)->c_ptr() << " < " |
| 336 | << fields[j]->val_str(&tmp)->c_ptr(); |
| 337 | EXPECT_GT( |
| 338 | 0, fields[i]->cmp(fields[i]->field_ptr(), fields[j]->field_ptr())) |
| 339 | << fields[i]->val_str(&tmp)->c_ptr() << " < " |
| 340 | << fields[j]->val_str(&tmp)->c_ptr(); |
| 341 | } else if (i > j) { |
| 342 | EXPECT_LT( |
| 343 | 0, memcmp(sortStrings[i], sortStrings[j], fields[i]->pack_length())) |
| 344 | << fields[i]->val_str(&tmp)->c_ptr() << " > " |
| 345 | << fields[j]->val_str(&tmp)->c_ptr(); |
| 346 | EXPECT_LT( |
| 347 | 0, fields[i]->cmp(fields[i]->field_ptr(), fields[j]->field_ptr())) |
| 348 | << fields[i]->val_str(&tmp)->c_ptr() << " > " |
| 349 | << fields[j]->val_str(&tmp)->c_ptr(); |
| 350 | } else { |
| 351 | EXPECT_EQ( |
| 352 | 0, memcmp(sortStrings[i], sortStrings[j], fields[i]->pack_length())) |
| 353 | << fields[i]->val_str(&tmp)->c_ptr() << " = " |
| 354 | << fields[j]->val_str(&tmp)->c_ptr(); |
| 355 | EXPECT_EQ( |
| 356 | 0, fields[i]->cmp(fields[i]->field_ptr(), fields[j]->field_ptr())) |
| 357 | << fields[i]->val_str(&tmp)->c_ptr() << " = " |
| 358 | << fields[j]->val_str(&tmp)->c_ptr(); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | TEST_F(FieldTest, FieldTime) { |
| 364 | uchar fieldBuf[7]; |
| 365 | MysqlTime bigTime(0, 0, 0, 123, 45, 45, 555500, false, MYSQL_TIMESTAMP_TIME); |
| 366 | |
| 367 | Field_time *field = new (thd()->mem_root) |
| 368 | Field_time(fieldBuf + 1, fieldBuf, false, Field::NONE, "f1"); |
| 369 | EXPECT_EQ(0, field->store_time(&bigTime, 4)); |
| 370 | MYSQL_TIME t; |
| 371 | EXPECT_FALSE(field->get_time(&t)); |
| 372 | compareMysqlTime(bigTime, t); |
| 373 | } |
| 374 | |
| 375 | const char *type_names3[] = {"one", "two", "three", nullptr}; |
| 376 | unsigned int type_lengths3[] = {3U, 3U, 5U, 0U}; |
| 377 | TYPELIB tl3 = {3, "tl3", type_names3, type_lengths3}; |
| 378 | |
| 379 | const char *type_names4[] = {"one", "two", "three", "four", nullptr}; |
| 380 | unsigned int type_lengths4[] = {3U, 3U, 5U, 4U, 0U}; |
| 381 | TYPELIB tl4 = {4, "tl4", type_names4, type_lengths4}; |
| 382 | |
| 383 | Field_set *FieldTest::create_field_set(TYPELIB *tl) { |
| 384 | Field_set *f = |
| 385 | new (thd()->mem_root) Field_set(nullptr, // ptr_arg |
| 386 | 42, // len_arg |
| 387 | nullptr, // null_ptr_arg |
| 388 | '\0', // null_bit_arg |
| 389 | Field::NONE, // auto_flags_arg |
| 390 | "f1", // field_name_arg |
| 391 | 1, // packlength_arg |
| 392 | tl, // typelib_arg |
| 393 | &my_charset_latin1); // charset_arg |
| 394 | f->table = new Fake_TABLE(f); |
| 395 | return f; |
| 396 | } |
| 397 | |
| 398 | // Bug#13871079 RQG_MYISAM_DML_ALTER_VALGRIND FAILS ON VALGRIND PN PB2 |
| 399 | TEST_F(FieldTest, CopyFieldSet) { |
| 400 | int err; |
| 401 | char fields[] = "one,two"; |
| 402 | uint64_t typeset = find_typeset(fields, &tl3, &err); |
| 403 | EXPECT_EQ(0, err); |
| 404 | |
| 405 | // Using two different TYPELIBs will set cf->do_copy to do_field_string(). |
| 406 | Field_set *f_to = create_field_set(&tl3); |
| 407 | bitmap_set_all(f_to->table->write_set); |
| 408 | uchar to_fieldval = 0; |
| 409 | f_to->set_field_ptr(&to_fieldval); |
| 410 | |
| 411 | Field_set *f_from = create_field_set(&tl4); |
| 412 | bitmap_set_all(f_from->table->write_set); |
| 413 | bitmap_set_all(f_from->table->read_set); |
| 414 | uchar from_fieldval = static_cast<uchar>(typeset); |
| 415 | f_from->set_field_ptr(&from_fieldval); |
| 416 | |
| 417 | Copy_field *cf = new (thd()->mem_root) Copy_field; |
| 418 | cf->set(f_to, f_from); |
| 419 | cf->invoke_do_copy(); |
| 420 | |
| 421 | // Copy_field DTOR is not invoked in all contexts, so we may leak memory. |
| 422 | EXPECT_FALSE(cf->tmp.is_alloced()); |
| 423 | |
| 424 | delete static_cast<Fake_TABLE *>(f_to->table); |
| 425 | delete static_cast<Fake_TABLE *>(f_from->table); |
| 426 | } |
| 427 | |
| 428 | /* |
| 429 | Tests that make_sort_key() is well behaved and does not cause buffer |
| 430 | overruns nor writes a too short key. We stop at the first error seen. |
| 431 | |
| 432 | - field - The field whose make_sort_key() method we test. |
| 433 | |
| 434 | - from - A buffer of size field->pack_length() that we will trick |
| 435 | the field into using as its record buffer. |
| 436 | |
| 437 | - expected - A buffer of size field->pack_length() + 1, the first n |
| 438 | bytes of which make_sort_key() is expected to fill out. The n + 1:th |
| 439 | byte is expected to be untouched. We try all possible values of n. |
| 440 | |
| 441 | - min_key_length - Some Field classes assert on a certain minimum |
| 442 | key length. To avoid that, pass the minimum length here. |
| 443 | */ |
| 444 | void test_make_sort_key(Field *field, uchar *from, const uchar *expected, |
| 445 | int min_key_length) { |
| 446 | const uint pack_length = field->pack_length(); |
| 447 | Fake_TABLE table(field); |
| 448 | table.s->db_low_byte_first = false; |
| 449 | field->set_field_ptr(from); |
| 450 | |
| 451 | for (uint key_length = min_key_length; key_length <= pack_length; |
| 452 | ++key_length) { |
| 453 | uchar buff[MAX_FIELD_WIDTH + 1]; |
| 454 | memset(buff, 'a', pack_length + 1); |
| 455 | field->make_sort_key(buff, key_length); |
| 456 | |
| 457 | // Check for a too short key. |
| 458 | for (uint i = 0; i < key_length; ++i) |
| 459 | ASSERT_FALSE(buff[i] == 'a') << "Too few bytes written at " << i |
| 460 | << " with buffer size " << key_length << "."; |
| 461 | |
| 462 | // Check for wrong result |
| 463 | for (uint i = 0; i < key_length; ++i) |
| 464 | ASSERT_EQ(expected[i], buff[i]) |
| 465 | << "Wrong output at " << i << " with buffer size " << key_length |
| 466 | << " and pack length " << pack_length << "."; |
| 467 | |
| 468 | EXPECT_EQ('a', buff[key_length]) |
| 469 | << "Buffer overrun" |
| 470 | << " with buffer size " << key_length << "."; |
| 471 | } |
| 472 | |
| 473 | // Try key_length == pack_length |
| 474 | uchar buff[MAX_FIELD_WIDTH]; |
| 475 | memset(buff, 'a', pack_length + 1); |
| 476 | field->make_sort_key(buff, pack_length); |
| 477 | EXPECT_EQ('a', buff[pack_length]) << "Buffer overrun"; |
| 478 | } |
| 479 | |
| 480 | // Convenience function for large values. |
| 481 | void test_make_sort_key(Field *field) { |
| 482 | const int pack_length = field->pack_length(); |
| 483 | |
| 484 | uchar from[MAX_FIELD_WIDTH]; |
| 485 | memset(from, 'b', pack_length); |
| 486 | |
| 487 | uchar to[MAX_FIELD_WIDTH + 1]; |
| 488 | memset(to, 'b', pack_length + 1); |
| 489 | |
| 490 | test_make_sort_key(field, from, to, 1); |
| 491 | } |
| 492 | |
| 493 | extern "C" { |
| 494 | static size_t mock_strnxfrm(const CHARSET_INFO *, uchar *, size_t, uint, |
| 495 | const uchar *, size_t, uint); |
| 496 | } |
| 497 | |
| 498 | class Mock_collation : public MY_COLLATION_HANDLER { |
| 499 | public: |
| 500 | Mock_collation() { strnxfrm = mock_strnxfrm; } |
| 501 | }; |
| 502 | |
| 503 | class Mock_charset : public CHARSET_INFO { |
| 504 | Mock_collation mock_collation; |
| 505 | |
| 506 | public: |
| 507 | mutable bool strnxfrm_called; |
| 508 | Mock_charset() : strnxfrm_called(false) { |
| 509 | cset = &my_charset_8bit_handler; |
| 510 | coll = &mock_collation; |
| 511 | mbmaxlen = 1; |
| 512 | pad_attribute = PAD_SPACE; |
| 513 | } |
| 514 | ~Mock_charset() { EXPECT_TRUE(strnxfrm_called); } |
| 515 | }; |
| 516 | |
| 517 | static size_t mock_strnxfrm(const CHARSET_INFO *charset, uchar *, size_t dstlen, |
| 518 | uint, const uchar *, size_t, uint) { |
| 519 | // CHARSET_INFO is not polymorphic, hence the abomination. |
| 520 | static_cast<const Mock_charset *>(charset)->strnxfrm_called = true; |
| 521 | return dstlen; |
| 522 | } |
| 523 | |
| 524 | static void test_integer_field(Field *field) { |
| 525 | uchar from[MAX_FIELD_WIDTH], expected[MAX_FIELD_WIDTH]; |
| 526 | const int pack_length = field->pack_length(); |
| 527 | for (int i = 0; i < pack_length; ++i) { |
| 528 | from[i] = '0' + i; |
| 529 | #ifdef WORDS_BIGENDIAN |
| 530 | expected[i] = '0' + i; |
| 531 | #else |
| 532 | expected[pack_length - 1 - i] = '0' + i; |
| 533 | #endif |
| 534 | } |
| 535 | test_make_sort_key(field, from, expected, pack_length); |
| 536 | } |
| 537 | |
| 538 | // Tests all make_sort_key() implementations. |
| 539 | |
| 540 | // We keep the same order of classes here as in field.h in order to make it |
| 541 | // easy to manually verify that all field types have been tested. |
| 542 | |
| 543 | TEST_F(FieldTest, MakeSortKey) { |
| 544 | { |
| 545 | SCOPED_TRACE("Field_decimal"); |
| 546 | Field_decimal fd(nullptr, 64, nullptr, '\0', Field::NONE, "", 0, false, |
| 547 | false); |
| 548 | test_make_sort_key(&fd); |
| 549 | } |
| 550 | { |
| 551 | SCOPED_TRACE("Field_new_decimal"); |
| 552 | Field_new_decimal fnd(64, true, "", 0, false); |
| 553 | test_make_sort_key(&fnd); |
| 554 | } |
| 555 | { |
| 556 | SCOPED_TRACE("Field_tiny"); |
| 557 | Field_tiny ft(nullptr, 0, nullptr, '\0', Field::NONE, "", false, true); |
| 558 | test_make_sort_key(&ft); |
| 559 | } |
| 560 | { |
| 561 | SCOPED_TRACE("Field_short"); |
| 562 | Field_short fs(0, "", true); |
| 563 | test_integer_field(&fs); |
| 564 | } |
| 565 | { |
| 566 | SCOPED_TRACE("Field_long"); |
| 567 | Field_long fl(0, false, "", true); |
| 568 | test_integer_field(&fl); |
| 569 | } |
| 570 | { |
| 571 | SCOPED_TRACE("Field_longlong"); |
| 572 | Field_longlong fll(nullptr, 64, nullptr, '\0', Field::NONE, "", false, |
| 573 | true); |
| 574 | test_integer_field(&fll); |
| 575 | } |
| 576 | { |
| 577 | SCOPED_TRACE("Field_float"); |
| 578 | Field_float ff(nullptr, 0, nullptr, '\0', Field::NONE, "", 0, false, false); |
| 579 | float from = 0.0; |
| 580 | uchar to[] = {128, 0, 0, 0}; |
| 581 | test_make_sort_key(&ff, reinterpret_cast<uchar *>(&from), to, 4); |
| 582 | } |
| 583 | { |
| 584 | SCOPED_TRACE("Field_double"); |
| 585 | Field_double fd(nullptr, 0, nullptr, '\0', Field::NONE, "", 0, false, |
| 586 | false); |
| 587 | double from = 0.0; |
| 588 | uchar expected[] = {128, 0, 0, 0, 0, 0, 0, 0}; |
| 589 | test_make_sort_key(&fd, reinterpret_cast<uchar *>(&from), expected, 8); |
| 590 | } |
| 591 | { |
| 592 | SCOPED_TRACE("Field_null"); |
| 593 | CHARSET_INFO cs; |
| 594 | cs.state = MY_CHARSET_UNDEFINED; // Avoid valgrind warning. |
| 595 | cs.mbmaxlen = 1; |
| 596 | Field_null fn(nullptr, 0, Field::NONE, "", &cs); |
| 597 | EXPECT_TRUE(fn.is_nullable()); |
| 598 | EXPECT_TRUE(fn.is_null()); |
| 599 | test_make_sort_key(&fn); |
| 600 | } |
| 601 | { |
| 602 | SCOPED_TRACE("Field_timestamp"); |
| 603 | Field_timestamp fts(false, ""); |
| 604 | test_integer_field(&fts); |
| 605 | } |
| 606 | { |
| 607 | SCOPED_TRACE("Field_timestampf"); |
| 608 | Field_timestampf ftsf(nullptr, nullptr, 0, Field::NONE, "", |
| 609 | DATETIME_MAX_DECIMALS); |
| 610 | test_make_sort_key(&ftsf); |
| 611 | } |
| 612 | { |
| 613 | SCOPED_TRACE("field_newdate"); |
| 614 | Field_newdate fnd(false, ""); |
| 615 | uchar from[] = {'3', '2', '1'}; |
| 616 | uchar expected[] = {'1', '2', '3'}; |
| 617 | test_make_sort_key(&fnd, from, expected, 3); |
| 618 | } |
| 619 | { |
| 620 | SCOPED_TRACE("Field_time"); |
| 621 | Field_time ft(""); |
| 622 | uchar from[] = {3, 2, 1}; |
| 623 | uchar expected[] = {129, 2, 3}; |
| 624 | test_make_sort_key(&ft, from, expected, 3); |
| 625 | } |
| 626 | { |
| 627 | SCOPED_TRACE("Field_timef"); |
| 628 | Field_timef ftf(false, "", 0); |
| 629 | test_make_sort_key(&ftf); |
| 630 | } |
| 631 | { |
| 632 | SCOPED_TRACE("Field_datetime"); |
| 633 | Field_datetime fdt(nullptr, nullptr, '\0', Field::NONE, nullptr); |
| 634 | test_integer_field(&fdt); |
| 635 | } |
| 636 | { |
| 637 | SCOPED_TRACE("Field_string"); |
| 638 | Mock_charset mock_charset; |
| 639 | Field_string fs(nullptr, 0, nullptr, '\0', Field::NONE, "", &mock_charset); |
| 640 | uchar to; |
| 641 | fs.make_sort_key(&to, 666); |
| 642 | } |
| 643 | { |
| 644 | SCOPED_TRACE("Field_varstring"); |
| 645 | Mock_charset mock_charset; |
| 646 | Fake_TABLE_SHARE fake_share(0); |
| 647 | uchar ptr[8] = {0, 0, 0, 0, 0, 0, 0, 0}; |
| 648 | Field_varstring fvs(ptr, 0, 0, nullptr, '\0', Field::NONE, "", &fake_share, |
| 649 | &mock_charset); |
| 650 | uchar to; |
| 651 | fvs.make_sort_key(&to, 666); |
| 652 | } |
| 653 | { |
| 654 | SCOPED_TRACE("Field_blob"); |
| 655 | CHARSET_INFO cs; |
| 656 | cs.state = MY_CHARSET_UNDEFINED; // Avoid valgrind warning. |
| 657 | cs.mbmaxlen = 1; |
| 658 | Field_blob fb(0, false, "", &cs, false); |
| 659 | } |
| 660 | { |
| 661 | SCOPED_TRACE("Field_enum"); |
| 662 | for (int pack_length = 1; pack_length <= 8; ++pack_length) |
| 663 | for (int key_length = 1; key_length <= 8; ++key_length) { |
| 664 | Field_enum fe(nullptr, 0, nullptr, '\0', Field::NONE, "", pack_length, |
| 665 | nullptr, &my_charset_bin); |
| 666 | uchar from[] = {'1', '2', '3', '4', '5', '6', '7', '8'}; |
| 667 | uchar expected[] = |
| 668 | #ifdef WORDS_BIGENDIAN |
| 669 | {'1', '2', '3', '4', '5', '6', '7', '8'}; |
| 670 | test_make_sort_key(&fe, from, expected, key_length); |
| 671 | #else |
| 672 | {'8', '7', '6', '5', '4', '3', '2', '1'}; |
| 673 | test_make_sort_key(&fe, from, expected + 8 - pack_length, key_length); |
| 674 | #endif |
| 675 | } |
| 676 | } |
| 677 | { |
| 678 | SCOPED_TRACE("Field_bit"); |
| 679 | Field_bit fb(nullptr, 0, nullptr, '\0', nullptr, '\0', Field::NONE, ""); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | void testCopyInteger(bool is_big_endian, bool is_unsigned) { |
| 684 | const uchar from_template[] = {'1', '2', '3', '4', '5', '6', '7', '8'}, |
| 685 | expected_for_big_endian[] = {'1', '2', '3', '4', |
| 686 | '5', '6', '7', '8'}, |
| 687 | expected_for_little_endian[] = {'8', '7', '6', '5', |
| 688 | '4', '3', '2', '1'}; |
| 689 | |
| 690 | const size_t max_length = sizeof(from_template); |
| 691 | for (uint from_length = 1; from_length < max_length; ++from_length) |
| 692 | for (uint to_length = 1; to_length <= from_length; ++to_length) { |
| 693 | uchar to[] = {'0', '0', '0', '0', '0', '0', '0', '0', '0'}; |
| 694 | uchar from[max_length]; |
| 695 | memcpy(from, from_template, max_length); |
| 696 | |
| 697 | if (is_big_endian) |
| 698 | copy_integer<true>(to, to_length, from, from_length, is_unsigned); |
| 699 | else |
| 700 | copy_integer<false>(to, to_length, from, from_length, is_unsigned); |
| 701 | |
| 702 | EXPECT_EQ('0', to[to_length]) |
| 703 | << "Buffer overrun @ position " << to_length << "."; |
| 704 | |
| 705 | ASSERT_EQ(is_unsigned ? 0 : 128, to[0] & 128) |
| 706 | << "Sign bit should" << (is_unsigned ? " not" : "") << " be flipped"; |
| 707 | |
| 708 | const uchar *expected = |
| 709 | is_big_endian ? expected_for_big_endian |
| 710 | : expected_for_little_endian + max_length - from_length; |
| 711 | |
| 712 | for (uint i = 1; i < to_length; ++i) { |
| 713 | ASSERT_FALSE(to[i] == '\0') |
| 714 | << "Too few bytes written @ position " << i |
| 715 | << " when copying a size " << from_length << " integer into a size " |
| 716 | << to_length << " integer."; |
| 717 | |
| 718 | ASSERT_EQ(expected[i], to[i]) |
| 719 | << "Result differs at position " << i << " when copying a size " |
| 720 | << from_length << " integer into a size " << to_length |
| 721 | << " integer."; |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | // Test of the copy_integer<>() function. |
| 727 | TEST_F(FieldTest, copyInteger) { |
| 728 | { |
| 729 | SCOPED_TRACE("Big endian unsigned"); |
| 730 | testCopyInteger(true, true); |
| 731 | } |
| 732 | { |
| 733 | SCOPED_TRACE("Big endian signed"); |
| 734 | testCopyInteger(true, false); |
| 735 | } |
| 736 | { |
| 737 | SCOPED_TRACE("Little endian unsigned"); |
| 738 | testCopyInteger(false, true); |
| 739 | } |
| 740 | { |
| 741 | SCOPED_TRACE("Little endian signed"); |
| 742 | testCopyInteger(false, false); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | } // namespace field_unittests |
| 747 | |
| 748 | #include "unittest/gunit/field_date-t.cc" |
| 749 | #include "unittest/gunit/field_datetime-t.cc" |
| 750 | #include "unittest/gunit/field_long-t.cc" |
| 751 | #include "unittest/gunit/field_newdecimal-t.cc" |
| 752 | #include "unittest/gunit/field_timestamp-t.cc" |