summaryrefslogtreecommitdiffstats
path: root/storage/perfschema/unittest/pfs_connect_attr-t.cc
blob: dee56ebcd514f65e2790c5bc163ebab54093f678 (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
/* Copyright (c) 2008, 2023, Oracle and/or its affiliates.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License, version 2.0,
  as published by the Free Software Foundation.

  This program is also distributed with certain software (including
  but not limited to OpenSSL) that is licensed under separate terms,
  as designated in a particular file or component or in included license
  documentation.  The authors of MySQL hereby grant you an additional
  permission to link the program and your derivative works with the
  separately licensed software that they have included with MySQL.

  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, version 2.0, 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,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */

#include <my_global.h>
#include <my_thread.h>
#include <pfs_server.h>
#include <pfs_instr_class.h>
#include <pfs_instr.h>
#include <pfs_global.h>
#include <tap.h>


#include <string.h>
#include <memory.h>

/* test helpers, to inspect data */
bool read_nth_attr(const char *connect_attrs, uint connect_attrs_length,
                          const CHARSET_INFO *connect_attrs_cs,
                          uint ordinal,
                          char *attr_name, uint max_attr_name,
                          uint *attr_name_length,
                          char *attr_value, uint max_attr_value,
                          uint *attr_value_length);

void test_blob_parser()
{
  char name[100], value[4096];
  unsigned char packet[10000], *ptr;
  uint name_len, value_len, idx, packet_length;
  bool result;
  const CHARSET_INFO *cs= &my_charset_utf8mb3_bin;

  diag("test_blob_parser");

  result= read_nth_attr("", 0, cs, 0,
                        name, 32, &name_len, value, 1024, &value_len);
  ok(result == false, "zero length blob");


  result= read_nth_attr("\x1", 1, cs, 0,
                        name, 32, &name_len, value, 1024, &value_len);
  ok(result == false, "invalid key length");


  result= read_nth_attr("\x2k1\x1", 4, cs, 0,
                        name, 32, &name_len, value, 1024, &value_len);
  ok(result == false, "invalid value length");


  result= read_nth_attr("\x2k1\x2v1", 6, cs, 0,
                        name, 32, &name_len, value, 1024, &value_len);
  ok(result == true, "one pair return");
  ok(name_len == 2, "one pair attr name length");
  ok(!strncmp(name, "k1", name_len), "one pair attr name");
  ok(value_len == 2, "one pair value length");
  ok(!strncmp(value, "v1", value_len), "one pair value");

  result= read_nth_attr("\x2k1\x2v1", 6, cs, 1,
                        name, 32, &name_len, value, 1024, &value_len);
  ok(result == false, "no second arg");

  result= read_nth_attr("\x2k1\x2v1\x2k2\x2v2", 12, cs, 1,
                        name, 32, &name_len, value, 1024, &value_len);
  ok(result == true, "two pairs return");
  ok(name_len == 2, "two pairs attr name length");
  ok(!strncmp(name, "k2", name_len), "two pairs attr name");
  ok(value_len == 2, "two pairs value length");
  ok(!strncmp(value, "v2", value_len), "two pairs value");

  result= read_nth_attr("\x2k1\xff\x2k2\x2v2", 12, cs, 1,
                        name, 32, &name_len, value, 1024, &value_len);
  ok(result == false, "two pairs first value bad return");

  result= read_nth_attr("\x2k1\x2v1\x2k2\x2v2", 10, cs, 1,
                        name, 32, &name_len, value, 1024, &value_len);
  ok(result == false, "two pairs wrong global length");

  result= read_nth_attr("\x21z123456789z123456789z123456789z12\x2v1", 37, cs, 0,
                        name, 32, &name_len, value, 1024, &value_len);
  ok(result == true, "attr name overflow");
  ok(name_len == 32, "attr name overflow length");
  ok(!strncmp(name, "z123456789z123456789z123456789z1", name_len),
     "attr name overflow name");
  ok(value_len == 2, "attr name overflow value length");
  ok(!strncmp(value, "v1", value_len), "attr name overflow value");

  packet[0]= 2;
  packet[1]= 'k';
  packet[2]= '1';
  ptr= net_store_length(packet + 3, 1025);
  for (idx= 0; idx < 1025; idx++)
    *ptr++= '0' + (idx % 10);
  packet_length= (uint) (ptr - packet);
  result= read_nth_attr((char *) packet, packet_length, cs, 0,
                        name, 32, &name_len, value, 1024, &value_len);
  ok(result == true, "attr value overflow");
  ok(name_len == 2, "attr value overflow length");
  ok(!strncmp(name, "k1", name_len), "attr value overflow name");
  ok(value_len == 1024, "attr value overflow value length");
  for (idx= 0; idx < 1024; idx++)
  {
    if (value[idx] != (char) ('0' + (idx % 10)))
      break;
  }
  ok (idx == 1024, "attr value overflow value");

  result= read_nth_attr("\x21z123456789z123456789z123456789z12\x2v1\x2k2\x2v2",
                        43, cs, 1,
                        name, 32, &name_len, value, 1024, &value_len);
  ok(result == true, "prev attr name overflow");
  ok(name_len == 2, "prev attr name overflow length");
  ok(!strncmp(name, "k2", name_len),
     "prev attr name overflow name");
  ok(value_len == 2, "prev attr name overflow value length");
  ok(!strncmp(value, "v2", value_len), "prev attr name overflow value");


  packet[1]= 'k';
  packet[2]= '1';
  packet[3]= 2;
  packet[4]= 'v';
  packet[5]= '1';

  for(idx= 251; idx < 256; idx++)
  {
    packet[0]= idx;
    result= read_nth_attr((char *) packet, 6, cs, 0,
                          name, 32, &name_len, value, 1024, &value_len);
    ok(result == false, "invalid string length %d", idx);
  }

  memset(packet, 0, sizeof(packet));
  for (idx=0; idx < 1660 /* *6 = 9960 */; idx++)
    memcpy(packet + idx * 6, "\x2k1\x2v1", 6);
  result= read_nth_attr((char *) packet, 8192, cs, 1364,
                        name, 32, &name_len, value, 1024, &value_len);
  ok(result == true, "last valid attribute %d", 1364);
  result= read_nth_attr((char *) packet, 8192, cs, 1365,
                        name, 32, &name_len, value, 1024, &value_len);
  ok(result == false, "first attribute that's cut %d", 1365);
}

void test_multibyte_lengths()
{
  char name[100], value[4096];
  uint name_len, value_len;
  bool result;
  const CHARSET_INFO *cs= &my_charset_utf8mb3_bin;

  unsigned char var_len_packet[] = {
    252, 2, 0, 'k', '1',
    253, 2, 0, 0, 'v', '1',
    254, 2, 0, 0, 0, 0, 0, 0, 0, 'k', '2',
    254, 2, 0, 0, 0, 0, 0, 0, 0, 'v', '2'
  };

  result= read_nth_attr((char *) var_len_packet, sizeof(var_len_packet), cs, 0,
                        name, 32, &name_len, value, 1024, &value_len);
  ok(result == true, "multibyte lengths return");
  ok(name_len == 2, "multibyte lengths name length");
  ok(!strncmp(name, "k1", name_len), "multibyte lengths attr name");
  ok(value_len == 2, "multibyte lengths value length");
  ok(!strncmp(value, "v1", value_len), "multibyte lengths value");

  result= read_nth_attr((char *) var_len_packet, sizeof(var_len_packet), cs, 1,
                        name, 32, &name_len, value, 1024, &value_len);
  ok(result == true, "multibyte lengths second attr return");
  ok(name_len == 2, "multibyte lengths second attr name length");
  ok(!strncmp(name, "k2", name_len), "multibyte lengths second attr attr name");
  ok(value_len == 2, "multibyte lengths value length");
  ok(!strncmp(value, "v2", value_len), "multibyte lengths second attr value");
}


void test_utf8_parser()
{
  /* utf8 max byte length per character is 6 */
  char name[33 * 6], value[1024 * 6], packet[1500 * 6], *ptr;
  uint name_len, value_len;
  bool result;
  const CHARSET_INFO *cs= &my_charset_utf8mb3_bin;

  /* note : this is encoded in utf-8 */
  const char *attr1= "Георги";
  const char *val1= "Кодинов";
  const char *attr2= "Пловдив";
  const char *val2= "България";

  ptr= packet;
  *ptr++= strlen(attr1);
  memcpy(ptr, attr1, strlen(attr1));
  ptr+= strlen(attr1);
  *ptr++= strlen(val1);
  memcpy(ptr, val1, strlen(val1));
  ptr+= strlen(val1);

  *ptr++= strlen(attr2);
  memcpy(ptr, attr2, strlen(attr2));
  ptr+= strlen(attr2);
  *ptr++= strlen(val2);
  memcpy(ptr, val2, strlen(val2));
  ptr+= strlen(val2);

  diag("test_utf8_parser attr pair #1");

  result= read_nth_attr((char *) packet, ptr - packet, cs, 0,
                        name, sizeof(name), &name_len,
                        value, sizeof(value), &value_len);
  ok(result == true, "return");
  ok(name_len == strlen(attr1), "name length");
  ok(!strncmp(name, attr1, name_len), "attr name");
  ok(value_len == strlen(val1), "value length");
  ok(!strncmp(value, val1, value_len), "value");

  diag("test_utf8_parser attr pair #2");
  result= read_nth_attr((char *) packet, ptr - packet, cs, 1,
                        name, sizeof(name), &name_len,
                        value, sizeof(value), &value_len);
  ok(result == true, "return");
  ok(name_len == strlen(attr2), "name length");
  ok(!strncmp(name, attr2, name_len), "attr name");
  ok(value_len == strlen(val2), "value length");
  ok(!strncmp(value, val2, value_len), "value");
}


void test_utf8_parser_bad_encoding()
{
  /* utf8 max byte length per character is 3*/
  char name[33 * 3], value[1024 * 3], packet[1500 * 3], *ptr;
  uint name_len, value_len;
  bool result;
  const CHARSET_INFO *cs= &my_charset_utf8mb3_bin;

  /* note : this is encoded in utf-8 */
  const char *attr= "Георги";
  const char *val= "Кодинов";

  ptr= packet;
  *ptr++= strlen(attr);
  memcpy(ptr, attr, strlen(attr));
  ptr[0]= (char)0xFA; // invalid UTF-8 char
  ptr+= strlen(attr);
  *ptr++= strlen(val);
  memcpy(ptr, val, strlen(val));
  ptr+= strlen(val);

  diag("test_utf8_parser_bad_encoding");

  result= read_nth_attr((char *) packet, ptr - packet, cs, 0,
                        name, sizeof(name), &name_len,
                        value, sizeof(value), &value_len);
  ok(result == false, "return");
}

const CHARSET_INFO *cs_cp1251;

void test_cp1251_parser()
{
  /* utf8 max byte length per character is 3*/
  char name[33 * 3], value[1024 * 3], packet[1500 * 3], *ptr;
  uint name_len, value_len;
  bool result;

  /* note : this is Георги in windows-1251 */
  const char *attr1= "\xc3\xe5\xee\xf0\xe3\xe8";
  /* note : this is Кодинов in windows-1251 */
  const char *val1= "\xca\xee\xe4\xe8\xed\xee\xe2";
  /* note : this is Пловдив in windows-1251 */
  const char *attr2= "\xcf\xeb\xee\xe2\xe4\xe8\xe2";
  /* note : this is България in windows-1251 */
  const char *val2= "\xc1\xfa\xeb\xe3\xe0\xf0\xe8\xff";

  ptr= packet;
  *ptr++= strlen(attr1);
  memcpy(ptr, attr1, strlen(attr1));
  ptr+= strlen(attr1);
  *ptr++= strlen(val1);
  memcpy(ptr, val1, strlen(val1));
  ptr+= strlen(val1);

  *ptr++= strlen(attr2);
  memcpy(ptr, attr2, strlen(attr2));
  ptr+= strlen(attr2);
  *ptr++= strlen(val2);
  memcpy(ptr, val2, strlen(val2));
  ptr+= strlen(val2);

  diag("test_cp1251_parser attr pair #1");

  result= read_nth_attr((char *) packet, ptr - packet, cs_cp1251, 0,
                        name, sizeof(name), &name_len,
                        value, sizeof(value), &value_len);
  ok(result == true, "return");
  /* need to compare to the UTF-8 equivalents */
  ok(name_len == strlen("Георги"), "name length");
  ok(!strncmp(name, "Георги", name_len), "attr name");
  ok(value_len == strlen("Кодинов"), "value length");
  ok(!strncmp(value, "Кодинов", value_len), "value");

  diag("test_cp1251_parser attr pair #2");
  result= read_nth_attr((char *) packet, ptr - packet, cs_cp1251, 1,
                        name, sizeof(name), &name_len,
                        value, sizeof(value), &value_len);
  ok(result == true, "return");
  /* need to compare to the UTF-8 equivalents */
  ok(name_len == strlen("Пловдив"), "name length");
  ok(!strncmp(name, "Пловдив", name_len), "attr name");
  ok(value_len == strlen("България"), "value length");
  ok(!strncmp(value, "България", value_len), "value");
}


void do_all_tests()
{
  test_blob_parser();
  test_multibyte_lengths();
  test_utf8_parser();
  test_utf8_parser_bad_encoding();
  test_cp1251_parser();
}

int main(int, char **)
{
  MY_INIT("pfs_connect_attr-t");

  cs_cp1251= get_charset_by_csname("cp1251", MY_CS_PRIMARY, MYF(0));
  if (!cs_cp1251)
    diag("skipping the cp1251 tests : missing character set");
  plan(59 + (cs_cp1251 ? 10 : 0));
  do_all_tests();
  return (exit_status());
}