forked from MahletNigusse/write-after-read-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield_long-t.cc
391 lines (335 loc) · 11.5 KB
/
field_long-t.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
// First include (the generated) my_config.h, to get correct platform defines.
#include "my_config.h"
#include <gtest/gtest.h>
#include "test_utils.h"
#include "fake_table.h"
#include "field.h"
namespace field_long_unittest {
using my_testing::Server_initializer;
using my_testing::Mock_error_handler;
class FieldLongTest : public ::testing::Test
{
protected:
virtual void SetUp() { initializer.SetUp(); }
virtual void TearDown() { initializer.TearDown(); }
THD *thd() { return initializer.thd(); }
Server_initializer initializer;
Field_set *create_field_set(TYPELIB *tl);
};
class Mock_field_long : public Field_long
{
uchar buffer[PACK_LENGTH];
uchar null_byte;
void initialize()
{
ptr= buffer;
memset(buffer, 0, PACK_LENGTH);
null_byte= '\0';
set_null_ptr(&null_byte, 1);
}
public:
Mock_field_long()
: Field_long(0, // ptr_arg
8, // len_arg
NULL, // null_ptr_arg
1, // null_bit_arg
Field::NONE, // unireg_check_arg
"field_name", // field_name_arg
false, // zero_arg
false) // unsigned_arg
{
initialize();
}
void make_writable() { bitmap_set_bit(table->write_set, field_index); }
void make_readable() { bitmap_set_bit(table->read_set, field_index); }
};
void test_store_long(Field_long *field,
const longlong store_value,
const longlong expected_result,
const int expected_error_no,
const type_conversion_status expected_status)
{
Mock_error_handler error_handler(field->table->in_use, expected_error_no);
type_conversion_status err= field->store(store_value, false); // signed
EXPECT_EQ(expected_result, field->val_int());
EXPECT_FALSE(field->is_null());
EXPECT_EQ(expected_status, err);
EXPECT_EQ((expected_error_no == 0 ? 0 : 1), error_handler.handle_called());
}
void test_store_string(Field_long *field,
const char *store_value, const int length,
const longlong expected_result,
const int expected_error_no,
const type_conversion_status expected_status)
{
Mock_error_handler error_handler(field->table->in_use, expected_error_no);
type_conversion_status err= field->store(store_value, length,
&my_charset_latin1);
EXPECT_EQ(expected_result, field->val_int());
EXPECT_FALSE(field->is_null());
EXPECT_EQ(expected_status, err);
EXPECT_EQ((expected_error_no == 0 ? 0 : 1), error_handler.handle_called());
}
TEST_F(FieldLongTest, StoreLegalIntValues)
{
Mock_field_long field_long;
Fake_TABLE table(&field_long);
table.in_use= thd();
field_long.make_writable();
field_long.make_readable();
thd()->count_cuted_fields= CHECK_FIELD_WARN;
SCOPED_TRACE(""); test_store_long(&field_long, 0, 0, 0, TYPE_OK);
SCOPED_TRACE(""); test_store_long(&field_long, 5, 5, 0, TYPE_OK);
SCOPED_TRACE(""); test_store_long(&field_long, -1, -1, 0, TYPE_OK);
{
SCOPED_TRACE("");
test_store_long(&field_long, INT_MIN32, INT_MIN32, 0, TYPE_OK);
}
{
SCOPED_TRACE("");
test_store_long(&field_long, INT_MAX32, INT_MAX32, 0, TYPE_OK);
}
{
Mock_error_handler error_handler(thd(), 0);
type_conversion_status err;
err= set_field_to_null(&field_long);
EXPECT_EQ(0, field_long.val_int());
EXPECT_TRUE(field_long.is_null());
EXPECT_EQ(TYPE_OK, err);
field_long.set_notnull();
EXPECT_EQ(0, field_long.val_int());
EXPECT_FALSE(field_long.is_null());
// None of the above should generate warnings
EXPECT_EQ(0, error_handler.handle_called());
}
}
// Values higher and lower than valid range for the Field_long
TEST_F(FieldLongTest, StoreOutOfRangeIntValues)
{
Mock_field_long field_long;
Fake_TABLE table(&field_long);
table.in_use= thd();
field_long.make_writable();
field_long.make_readable();
thd()->count_cuted_fields= CHECK_FIELD_WARN;
// Field_long is signed
{
SCOPED_TRACE("");
test_store_long(&field_long, INT_MAX32 + 1LL, INT_MAX32,
ER_WARN_DATA_OUT_OF_RANGE,
TYPE_WARN_OUT_OF_RANGE);
}
{
SCOPED_TRACE("");
test_store_long(&field_long, INT_MIN32 - 1LL, INT_MIN32,
ER_WARN_DATA_OUT_OF_RANGE,
TYPE_WARN_OUT_OF_RANGE);
}
// Field_long is unsigned
{
SCOPED_TRACE("");
field_long.unsigned_flag= true;
}
{
SCOPED_TRACE("");
test_store_long(&field_long, -1LL, 0, ER_WARN_DATA_OUT_OF_RANGE,
TYPE_WARN_OUT_OF_RANGE);
}
{
SCOPED_TRACE("");
test_store_long(&field_long, INT_MIN32, 0, ER_WARN_DATA_OUT_OF_RANGE,
TYPE_WARN_OUT_OF_RANGE);
}
}
TEST_F(FieldLongTest, StoreLegalStringValues)
{
Mock_field_long field_long;
Fake_TABLE table(&field_long);
table.in_use= thd();
field_long.make_writable();
field_long.make_readable();
thd()->count_cuted_fields= CHECK_FIELD_WARN;
const char min_int[]= "-2147483648";
const char max_int[]= "2147483647";
const char max_int_plus1[]= "2147483648";
const char max_uint[]= "4294967295";
// Field_long is signed
{
SCOPED_TRACE("");
test_store_string(&field_long, STRING_WITH_LEN("0"), 0, 0, TYPE_OK);
}
{
SCOPED_TRACE("");
test_store_string(&field_long, STRING_WITH_LEN("1"), 1, 0, TYPE_OK);
}
{
SCOPED_TRACE("");
test_store_string(&field_long, STRING_WITH_LEN("-1"), -1, 0, TYPE_OK);
}
{
SCOPED_TRACE("");
test_store_string(&field_long, STRING_WITH_LEN(max_int), INT_MAX32,
0, TYPE_OK);
}
{
SCOPED_TRACE("");
test_store_string(&field_long, STRING_WITH_LEN(min_int), INT_MIN32,
0, TYPE_OK);
}
// Field_long is unsigned
field_long.unsigned_flag= true;
{
SCOPED_TRACE("");
test_store_string(&field_long, STRING_WITH_LEN(max_int_plus1),
INT_MAX32 + 1LL,
0, TYPE_OK);
}
{
SCOPED_TRACE("");
test_store_string(&field_long, STRING_WITH_LEN(max_uint), UINT_MAX32,
0, TYPE_OK);
}
}
TEST_F(FieldLongTest, StoreIllegalStringValues)
{
Mock_field_long field_long;
Fake_TABLE table(&field_long);
table.in_use= thd();
field_long.make_writable();
field_long.make_readable();
thd()->count_cuted_fields= CHECK_FIELD_WARN;
const char max_int_plus1[]= "2147483648";
const char min_int_minus1[]= "-2147483649";
const char very_high[]= "999999999999999";
const char very_low[]= "-999999999999999";
// Field_long is signed - Stored value is INT_MIN32/INT_MAX32
// depending on sign of string to store
{
SCOPED_TRACE("");
test_store_string(&field_long, STRING_WITH_LEN(max_int_plus1), INT_MAX32,
ER_WARN_DATA_OUT_OF_RANGE,
TYPE_WARN_OUT_OF_RANGE);
}
{
SCOPED_TRACE("");
test_store_string(&field_long, STRING_WITH_LEN(very_high), INT_MAX32,
ER_WARN_DATA_OUT_OF_RANGE,
TYPE_WARN_OUT_OF_RANGE);
}
{
SCOPED_TRACE("");
test_store_string(&field_long, STRING_WITH_LEN(min_int_minus1), INT_MIN32,
ER_WARN_DATA_OUT_OF_RANGE,
TYPE_WARN_OUT_OF_RANGE);
}
{
SCOPED_TRACE("");
test_store_string(&field_long, STRING_WITH_LEN(very_low), INT_MIN32,
ER_WARN_DATA_OUT_OF_RANGE,
TYPE_WARN_OUT_OF_RANGE);
}
// Field_long is unsigned - Stored value is 0/UINT_MAX32
// depending on sign of string to store
const char min_int[]= "-2147483648";
const char max_uint_plus1[]= "4294967296";
field_long.unsigned_flag= true;
{
SCOPED_TRACE("");
test_store_string(&field_long, STRING_WITH_LEN(max_uint_plus1), UINT_MAX32,
ER_WARN_DATA_OUT_OF_RANGE,
TYPE_WARN_OUT_OF_RANGE);
}
{
SCOPED_TRACE("");
test_store_string(&field_long, STRING_WITH_LEN(very_high), UINT_MAX32,
ER_WARN_DATA_OUT_OF_RANGE,
TYPE_WARN_OUT_OF_RANGE);
}
{
SCOPED_TRACE("");
test_store_string(&field_long, STRING_WITH_LEN("-1"), 0,
ER_WARN_DATA_OUT_OF_RANGE,
TYPE_WARN_OUT_OF_RANGE);
}
{
SCOPED_TRACE("");
test_store_string(&field_long, STRING_WITH_LEN(min_int), 0,
ER_WARN_DATA_OUT_OF_RANGE,
TYPE_WARN_OUT_OF_RANGE);
}
{
SCOPED_TRACE("");
test_store_string(&field_long, STRING_WITH_LEN(very_low), 0,
ER_WARN_DATA_OUT_OF_RANGE,
TYPE_WARN_OUT_OF_RANGE);
}
// Invalid value
{
SCOPED_TRACE("");
test_store_string(&field_long, STRING_WITH_LEN("foo"), 0,
ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
TYPE_ERR_BAD_VALUE);
}
}
TEST_F(FieldLongTest, StoreNullValue)
{
Mock_field_long field_long;
Fake_TABLE table(&field_long);
table.in_use= thd();
field_long.make_writable();
field_long.make_readable();
thd()->count_cuted_fields= CHECK_FIELD_WARN;
type_conversion_status err;
// Save NULL value in a field that can have NULL value
{
Mock_error_handler error_handler(thd(), 0);
err= set_field_to_null(&field_long);
EXPECT_EQ(0, field_long.val_int());
EXPECT_EQ(TYPE_OK, err);
err= set_field_to_null_with_conversions(&field_long, true);
EXPECT_EQ(0, field_long.val_int());
EXPECT_EQ(TYPE_OK, err);
err= set_field_to_null_with_conversions(&field_long, false);
EXPECT_EQ(0, field_long.val_int());
EXPECT_EQ(TYPE_OK, err);
EXPECT_EQ(0, error_handler.handle_called());
}
// Save NULL value in a field that can NOT have NULL value
field_long.set_null_ptr(NULL, 0);
{
Mock_error_handler error_handler(thd(), 0);
// Save NULL value in a field that can be set to NULL temporary
field_long.set_tmp_nullable();
err= set_field_to_null(&field_long);
EXPECT_EQ(0, field_long.val_int());
EXPECT_EQ(TYPE_OK, err);
EXPECT_EQ(0, error_handler.handle_called());
field_long.reset_tmp_nullable();
}
{
Mock_error_handler error_handler(thd(), 0);
err= set_field_to_null_with_conversions(&field_long, true);
EXPECT_EQ(0, field_long.val_int());
EXPECT_EQ(TYPE_ERR_NULL_CONSTRAINT_VIOLATION, err);
EXPECT_EQ(0, error_handler.handle_called());
}
{
Mock_error_handler error_handler(thd(), ER_BAD_NULL_ERROR);
err= set_field_to_null_with_conversions(&field_long, false);
EXPECT_EQ(0, field_long.val_int());
EXPECT_EQ(TYPE_OK, err);
EXPECT_EQ(1, error_handler.handle_called());
}
}
}