summaryrefslogtreecommitdiffstats
path: root/unittest/sql/mf_iocache-t.cc
blob: c72e6b7c7e295f47857cc6ba05f38dd6f464b73f (plain)
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/* Copyright (C) 2015 MariaDB

   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-1335 USA */

#include <my_global.h>
#include <my_sys.h>
#include <my_crypt.h>
#include <tap.h>

/*** tweaks and stubs for encryption code to compile ***************/
#define KEY_SIZE (128/8)

my_bool encrypt_tmp_files;
int init_io_cache_encryption();

uint encryption_key_get_latest_version_func(uint)
{
  return 1;
}

uint encryption_key_id_exists_func(uint)
{
  return 1;
}

uint encryption_key_version_exists_func(uint, uint)
{
  return 1;
}

uint encryption_key_get_func(uint, uint, uchar* key, uint* size)
{
  if (*size < KEY_SIZE)
  {
    *size= KEY_SIZE;
    return ENCRYPTION_KEY_BUFFER_TOO_SMALL;
  }
  memset(key, KEY_SIZE, *size= KEY_SIZE);
  return 0;
}

uint encryption_ctx_size_func(unsigned int, unsigned int)
{
  return MY_AES_CTX_SIZE;
}


#ifdef HAVE_EncryptAes128Gcm
enum my_aes_mode aes_mode= MY_AES_GCM;
#else
enum my_aes_mode aes_mode= MY_AES_CBC;
#endif

int encryption_ctx_init_func(void *ctx, const unsigned char* key, unsigned int klen,
                                const unsigned char* iv, unsigned int ivlen,
                                int flags, unsigned int key_id,
                                unsigned int key_version)
{
  return my_aes_crypt_init(ctx, aes_mode, flags, key, klen, iv, ivlen);
}

uint encryption_encrypted_length_func(unsigned int slen, unsigned int key_id, unsigned int key_version)
{
  return my_aes_get_size(aes_mode, slen);
}

struct encryption_service_st encryption_handler=
{
  encryption_key_get_latest_version_func,
  encryption_key_get_func,
  encryption_ctx_size_func,
  encryption_ctx_init_func,
  my_aes_crypt_update,
  my_aes_crypt_finish,
  encryption_encrypted_length_func
};

void sql_print_information(const char *format, ...)
{
}

void sql_print_error(const char *format, ...)
{
}

/*** end of encryption tweaks and stubs ****************************/

PRAGMA_DISABLE_CHECK_STACK_FRAME

static IO_CACHE info;
#define CACHE_SIZE 16384

#define INFO_TAIL ", pos_in_file = %llu, pos_in_mem = %lu", \
    info.pos_in_file, (ulong) ((info.type == READ_CACHE ? info.read_pos : info.write_pos) - info.request_pos)

#define FILL 0x5A

int data_bad(const uchar *buf, size_t len)
{
  const uchar *end= buf + len;
  while (buf < end)
    if (*buf++ != FILL)
      return 1;
  return 0;
}

void temp_io_cache()
{
  int res;
  uchar buf[CACHE_SIZE + 200];
  memset(buf, FILL, sizeof(buf));

  diag("temp io_cache with%s encryption", encrypt_tmp_files?"":"out");

  init_io_cache_encryption();  
  
  res= open_cached_file(&info, 0, 0, CACHE_SIZE, 0);
  ok(res == 0, "open_cached_file" INFO_TAIL);

  res= my_b_write(&info, buf, 100);
  ok(res == 0 && info.pos_in_file == 0, "small write" INFO_TAIL );

  res= my_b_write(&info, buf, sizeof(buf));
  ok(res == 0 && info.pos_in_file == CACHE_SIZE, "large write" INFO_TAIL);

  res= reinit_io_cache(&info, WRITE_CACHE, 250, 0, 0);
  ok(res == 0, "reinit with rewind" INFO_TAIL);

  res= my_b_write(&info, buf, sizeof(buf));
  ok(res == 0, "large write" INFO_TAIL);

  res= my_b_flush_io_cache(&info, 1);
  ok(res == 0, "flush" INFO_TAIL);

  res= reinit_io_cache(&info, READ_CACHE, 0, 0, 0);
  ok(res == 0, "reinit READ_CACHE" INFO_TAIL);

  res= (int)my_pread(info.file, buf, 50, 50, MYF(MY_NABP));
  ok(res == 0 && data_bad(buf, 50) == encrypt_tmp_files,
     "file must be %sreadable", encrypt_tmp_files ?"un":"");

  res= my_b_read(&info, buf, 50) || data_bad(buf, 50);
  ok(res == 0 && info.pos_in_file == 0, "small read" INFO_TAIL);

  res= my_b_read(&info, buf, sizeof(buf)) || data_bad(buf, sizeof(buf));
  ok(res == 0 && info.pos_in_file == CACHE_SIZE, "large read" INFO_TAIL);

  close_cached_file(&info);
}

void mdev9044()
{
  int res;
  uchar buf[CACHE_SIZE + 200];

  diag("MDEV-9044 Binlog corruption in Galera");

  res= open_cached_file(&info, 0, 0, CACHE_SIZE, 0);
  ok(res == 0, "open_cached_file" INFO_TAIL);

  res= my_b_write(&info, USTRING_WITH_LEN("first write\0"));
  ok(res == 0, "first write" INFO_TAIL);

  res= my_b_flush_io_cache(&info, 1);
  ok(res == 0, "flush" INFO_TAIL);

  res= reinit_io_cache(&info, WRITE_CACHE, 0, 0, 0);
  ok(res == 0, "reinit WRITE_CACHE" INFO_TAIL);

  res= my_b_write(&info, USTRING_WITH_LEN("second write\0"));
  ok(res == 0, "second write" INFO_TAIL );

  res= reinit_io_cache(&info, READ_CACHE, 0, 0, 0);
  ok(res == 0, "reinit READ_CACHE" INFO_TAIL);

  res= (int)my_b_fill(&info);
  ok(res == 0, "fill" INFO_TAIL);

  res= reinit_io_cache(&info, READ_CACHE, 0, 0, 0);
  ok(res == 0, "reinit READ_CACHE" INFO_TAIL);

  res= my_b_read(&info, buf, sizeof(buf));
  ok(res == 1 && strcmp((char*)buf, "second write") == 0, "read '%s'", buf);

  close_cached_file(&info);
}

/* 2 Reads (with my_b_fill) in cache makes second read to fail */
void mdev10259()
{
  int res;
  uchar buf[200];
  memset(buf, FILL, sizeof(buf));

  diag("MDEV-10259- mysqld crash with certain statement length and order with"
         " Galera and encrypt-tmp-files=1");

  init_io_cache_encryption();

  res= open_cached_file(&info, 0, 0, CACHE_SIZE, 0);
  ok(res == 0, "open_cached_file" INFO_TAIL);

  res= my_b_write(&info, buf, sizeof(buf));
  ok(res == 0 && info.pos_in_file == 0, "200 write" INFO_TAIL);

  res= my_b_flush_io_cache(&info, 1);
  ok(res == 0, "flush" INFO_TAIL);

  my_off_t saved_pos=  my_b_tell(&info);
  res= reinit_io_cache(&info, READ_CACHE, 0, 0, 0);
  ok(res == 0, "reinit READ_CACHE" INFO_TAIL);

  size_t s= my_b_fill(&info);
  ok(s == 200, "fill" INFO_TAIL);

  s= my_b_fill(&info);
  ok(s == 0, "fill" INFO_TAIL);

  s= my_b_fill(&info);
  ok(s == 0, "fill" INFO_TAIL);

  res= reinit_io_cache(&info, WRITE_CACHE, saved_pos, 0, 0);
  ok(res == 0, "reinit WRITE_CACHE" INFO_TAIL);

  res= reinit_io_cache(&info, READ_CACHE, 0, 0, 0);
  ok(res == 0, "reinit READ_CACHE" INFO_TAIL);

  ok(200 == my_b_bytes_in_cache(&info),"my_b_bytes_in_cache == 200");

  s= my_b_fill(&info);
  ok(s == 0, "fill" INFO_TAIL);

  s= my_b_fill(&info);
  ok(s == 0, "fill" INFO_TAIL);

  s= my_b_fill(&info);
  ok(s == 0, "fill" INFO_TAIL);

  res= reinit_io_cache(&info, WRITE_CACHE, saved_pos, 0, 0);
  ok(res == 0, "reinit WRITE_CACHE" INFO_TAIL);

  res= reinit_io_cache(&info, READ_CACHE, 0, 0, 0);
  ok(res == 0, "reinit READ_CACHE" INFO_TAIL);

  ok(200 == my_b_bytes_in_cache(&info),"my_b_bytes_in_cache == 200");

  res= my_b_read(&info, buf, sizeof(buf)) || data_bad(buf, sizeof(buf));
  ok(res == 0 && info.pos_in_file == 0, "large read" INFO_TAIL);

  close_cached_file(&info);

}

void mdev14014()
{
  int res;
  uchar buf_o[200];
  uchar buf_i[200];
  memset(buf_i,    0, sizeof( buf_i));
  memset(buf_o, FILL, sizeof(buf_o));

  diag("MDEV-14014 Dump thread reads past last 'officially' written byte");

  init_io_cache_encryption();

  res= open_cached_file(&info, 0, 0, CACHE_SIZE, 0);
  ok(res == 0, "open_cached_file" INFO_TAIL);

  res= my_b_write(&info, buf_o, sizeof(buf_o));
  ok(res == 0, "buffer is written" INFO_TAIL);

  res= my_b_flush_io_cache(&info, 1);
  ok(res == 0, "flush" INFO_TAIL);

  res= reinit_io_cache(&info, READ_CACHE, 0, 0, 0);
  ok(res == 0, "reinit READ_CACHE" INFO_TAIL);

  info.end_of_file= 100;
  res= my_b_read(&info, buf_i, sizeof(buf_i));
  ok(res == 1 && buf_i[100] == 0 && buf_i[200-1] == 0,
     "short read leaves buf_i[100..200-1] == 0");

  close_cached_file(&info);
}

void mdev17133()
{
  my_off_t res;
  int k;
  const int eof_iter=4, read_iter= 4;
  uchar buf_i[1024*256];      // read
  uchar buf_o[sizeof(buf_i)]; // write
  const size_t eof_block_size= sizeof(buf_o) / eof_iter;
  const size_t read_size= eof_block_size / read_iter;
  size_t total;

  srand((uint) time(NULL));
  memset(buf_i,    0, sizeof( buf_i));
  memset(buf_o, FILL, sizeof(buf_o));

  diag("MDEV-17133 Dump thread reads from the past");

  init_io_cache_encryption();

  res= open_cached_file(&info, 0, 0, CACHE_SIZE, 0);
  ok(res == 0, "open_cached_file" INFO_TAIL);

  res= my_b_write(&info, buf_o, sizeof(buf_o));
  ok(res == 0, "buffer is written" INFO_TAIL);
  res= my_b_tell(&info);
  ok(res == sizeof(buf_o), "cache size as expected");

  res= my_b_flush_io_cache(&info, 1);
  ok(res == 0, "flush" INFO_TAIL);
  res= reinit_io_cache(&info, READ_CACHE, 0, 0, 0);
  ok(res == 0, "reinit READ_CACHE" INFO_TAIL);

  // read the written data by chunks of variable size eof_iter times
  for (k= eof_iter, info.end_of_file=0, total= 0; k; k--)
  {
    int i;
    size_t curr_read_size;
    info.end_of_file=
      k == 1 ? sizeof(buf_o) :
      MY_MIN(sizeof(buf_o),
             info.end_of_file + eof_block_size +
             // plus 25% of block for randomization to the average
             eof_block_size/4 - rand() % (eof_block_size/2));

    // read a chunk by blocks of variable size read_iter times
    // the last block completes the current chunk
    for (i= 0; i < read_iter; i++, total += curr_read_size)
    {
      char buf_check[eof_block_size];
      size_t a,b;

      a= (size_t)(info.end_of_file - total);
      b= read_size + read_size/4 - rand() % (read_size/2);
      curr_read_size= (i == read_iter - 1) ? a :
        MY_MIN(a, b);

      DBUG_ASSERT(curr_read_size <= info.end_of_file - total);

      res= my_b_read(&info, buf_i + total, MY_MIN(19, curr_read_size));
      ok(res == 0, "read of 19");
      // mark read bytes in the used part of the cache buffer
      memset(info.buffer, 0, info.read_pos - info.buffer);

      // random size 2nd read
      res= my_b_read(&info, buf_i + total + MY_MIN(19, curr_read_size),
                     19 >= curr_read_size ? 0 : curr_read_size - 19);
      ok(res == 0, "rest of read %zu", curr_read_size - 19);
      // mark read bytes in the used part of the cache buffer
      memset(info.buffer, 0, info.read_pos - info.buffer);

      // check that no marked bytes are read
      memset(buf_check, FILL, curr_read_size);
      ok(memcmp(buf_i + total, buf_check, curr_read_size) == 0,
         "read correct data");
    }
    ok(info.pos_in_file + (info.read_end - info.buffer) == info.end_of_file,
       "cache is read up to eof");
    ok(total == info.end_of_file, "total matches eof");
  }
  ok(total == sizeof(buf_i), "read total size match");
  ok(buf_i[sizeof(buf_i) - 1] == FILL, "data read correctly");

  close_cached_file(&info);
}


void mdev10963()
{
  int res;
  uint n_checks= 8;
  uchar buf[1024 * 512];
  uint n_frag= sizeof(buf)/(2 * CACHE_SIZE);
  FILE *file;
  myf my_flags= MYF(MY_WME);
  const char *file_name="cache.log";

  memset(buf, FILL, sizeof(buf));
  diag("MDEV-10963 Fragmented BINLOG query");

  init_io_cache_encryption();
  srand((uint) time(NULL));

  /* copying source */
  res= open_cached_file(&info, 0, 0, CACHE_SIZE, 0);
  ok(res == 0, "open_cached_file" INFO_TAIL);
  res= my_b_write(&info, buf, sizeof(buf));

  ulonglong total_size= my_b_tell(&info);
  ok(res == 0 && total_size == sizeof(buf), "cache is written");

  /* destination */
  file= my_fopen(file_name, O_RDWR | O_TRUNC | O_CREAT, my_flags);
  ok(my_fileno(file) > 0, "opened file fd = %d", my_fileno(file));

  /*
    For n_checks times verify a sequence of copying with random fragment
    size ranging from zero to about the double of the cache read buffer size.
  */
  for (; n_checks; n_checks--, rewind(file))
  {
    // copied size is an estimate can be incremeneted to greater than total_size
    ulong copied_size= 0;

    res= reinit_io_cache(&info, READ_CACHE, 0L, FALSE, FALSE);
    ok(res == 0, "cache turned to read");

    for (ulong i= 0, curr_size= 0; i < n_frag; i++, copied_size += curr_size)
    {
      curr_size= rand() % (2 * (total_size - copied_size) / (n_frag - i));

      DBUG_ASSERT(curr_size <= total_size - copied_size || i == n_frag - 1);

      res= my_b_copy_to_file(&info, file, curr_size);
      ok(res == 0, "%lu of the cache copied to file", curr_size);
    }
    /*
      Regardless of total_size <> copied_size the function succeeds:
      when total_size < copied_size the huge overflowed value of the last
      argument is ignored because nothing already left uncopied in the cache.
    */
    res= my_b_copy_to_file(&info, file, (size_t) total_size - copied_size);
    ok(res == 0, "%llu of the cache copied to file", total_size - copied_size);
    ok(my_ftell(file, my_flags) == sizeof(buf),
       "file written in %d fragments", n_frag+1);

    res= reinit_io_cache(&info, WRITE_CACHE, total_size, 0, 0);
    ok(res == 0 && my_b_tell(&info) == sizeof(buf), "cache turned to write");
  }
  close_cached_file(&info);
  my_fclose(file, my_flags);
  my_delete(file_name, MYF(MY_WME));
}

int main(int argc __attribute__((unused)),char *argv[])
{
  MY_INIT(argv[0]);
  plan(277);

  /* temp files with and without encryption */
  encrypt_tmp_files= 1;
  temp_io_cache();

  encrypt_tmp_files= 0;
  temp_io_cache();

  /* regression tests */
  mdev9044();

  encrypt_tmp_files= 1;
  mdev10259();
  encrypt_tmp_files= 0;

  mdev14014();
  mdev17133();
  mdev10963();

  my_end(0);
  return exit_status();
}

PRAGMA_REENABLE_CHECK_STACK_FRAME