~jan-kneschke/mysql-proxy/packet-tracking-assertions

« back to all changes in this revision

Viewing changes to tests/unit/t_network_mysqld_packet.c

  • Committer: Kay Roepke
  • Author(s): Jan Kneschke
  • Date: 2008-01-23 22:00:28 UTC
  • Revision ID: kay@mysql.com-20080123220028-hq2xqb69apa75fnx
first round on mysql-shell based on the proxy code

this is mostly a verification if the proxy-code is flexible enough to handle 
all three scenarios of: client, server and forwarding (proxy)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $%BEGINLICENSE%$
2
 
 Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
3
 
 
4
 
 This program is free software; you can redistribute it and/or
5
 
 modify it under the terms of the GNU General Public License as
6
 
 published by the Free Software Foundation; version 2 of the
7
 
 License.
8
 
 
9
 
 This program is distributed in the hope that it will be useful,
10
 
 but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
 
 GNU General Public License for more details.
13
 
 
14
 
 You should have received a copy of the GNU General Public License
15
 
 along with this program; if not, write to the Free Software
16
 
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17
 
 02110-1301  USA
18
 
 
19
 
 $%ENDLICENSE%$ */
20
 
 
21
 
 
22
 
#include <stdio.h>
23
 
#include <stdlib.h>
24
 
#include <string.h>
25
 
 
26
 
#include <glib.h>
27
 
 
28
 
#include "network-mysqld-proto.h"
29
 
#include "network-mysqld-packet.h"
30
 
#include "glib-ext.h"
31
 
 
32
 
#if GLIB_CHECK_VERSION(2, 16, 0)
33
 
#define C(x) x, sizeof(x) - 1
34
 
#define S(x) x->str, x->len
35
 
 
36
 
/**
37
 
 * Tests for the MySQL Protocol Codec functions
38
 
 * @ingroup proto
39
 
 */
40
 
 
41
 
/*@{*/
42
 
void t_ok_packet_new(void) {
43
 
        network_mysqld_ok_packet_t *ok_packet;
44
 
 
45
 
        ok_packet = network_mysqld_ok_packet_new();
46
 
        g_assert(ok_packet);
47
 
 
48
 
        network_mysqld_ok_packet_free(ok_packet);
49
 
}
50
 
 
51
 
void t_ok_packet_append(void) {
52
 
        network_mysqld_ok_packet_t *ok_packet;
53
 
        network_packet *packet;
54
 
 
55
 
        ok_packet = network_mysqld_ok_packet_new();
56
 
        packet = network_packet_new();
57
 
        packet->data = g_string_new(NULL);
58
 
 
59
 
        /* check if a empty ok-packet is encoded correctly */
60
 
        g_assert_cmpint(0, ==, network_mysqld_proto_append_ok_packet(packet->data, ok_packet));
61
 
        g_assert_cmpint(7, ==, packet->data->len);
62
 
        g_assert_cmpint(TRUE, ==, g_memeq(S(packet->data), C("\x00\x00\x00\x00\x00\x00\x00")));
63
 
 
64
 
        g_assert_cmpint(0, ==, network_mysqld_proto_get_ok_packet(packet, ok_packet));
65
 
 
66
 
        /* check if encoding and decoding works */
67
 
        ok_packet->warnings = 1;
68
 
        ok_packet->server_status = 2;
69
 
        ok_packet->insert_id = 3;
70
 
        ok_packet->affected_rows = 4;
71
 
 
72
 
        g_string_truncate(packet->data, 0);
73
 
        packet->offset = 0;
74
 
 
75
 
        g_assert_cmpint(0, ==, network_mysqld_proto_append_ok_packet(packet->data, ok_packet));
76
 
        g_assert_cmpint(7, ==, packet->data->len);
77
 
        g_assert_cmpint(TRUE, ==, g_memeq(S(packet->data), C("\x00\x04\x03\x02\x00\x01\x00")));
78
 
        
79
 
        network_mysqld_ok_packet_free(ok_packet);
80
 
 
81
 
        ok_packet = network_mysqld_ok_packet_new();
82
 
        g_assert_cmpint(0, ==, network_mysqld_proto_get_ok_packet(packet, ok_packet));
83
 
        g_assert_cmpint(1, ==, ok_packet->warnings);
84
 
        g_assert_cmpint(2, ==, ok_packet->server_status);
85
 
        g_assert_cmpint(3, ==, ok_packet->insert_id);
86
 
        g_assert_cmpint(4, ==, ok_packet->affected_rows);
87
 
 
88
 
        network_mysqld_ok_packet_free(ok_packet);
89
 
 
90
 
        /* check if too-short packet is denied */
91
 
        ok_packet = network_mysqld_ok_packet_new();
92
 
        g_string_truncate(packet->data, 0);
93
 
        packet->offset = 0;
94
 
        g_assert_cmpint(-1, ==, network_mysqld_proto_get_ok_packet(packet, ok_packet));
95
 
 
96
 
        network_mysqld_ok_packet_free(ok_packet);
97
 
 
98
 
        g_string_free(packet->data, TRUE);
99
 
        network_packet_free(packet);
100
 
}
101
 
 
102
 
void t_err_packet_new(void) {
103
 
        network_mysqld_err_packet_t *err_packet;
104
 
 
105
 
        err_packet = network_mysqld_err_packet_new();
106
 
        g_assert(err_packet);
107
 
 
108
 
        network_mysqld_err_packet_free(err_packet);
109
 
}
110
 
 
111
 
void t_err_packet_append(void) {
112
 
        network_mysqld_err_packet_t *err_packet;
113
 
        network_packet *packet;
114
 
 
115
 
        err_packet = network_mysqld_err_packet_new();
116
 
        packet = network_packet_new();
117
 
        packet->data = g_string_new(NULL);
118
 
 
119
 
        /* check if a empty ok-packet is encoded correctly */
120
 
        g_assert_cmpint(0, ==, network_mysqld_proto_append_err_packet(packet->data, err_packet));
121
 
        g_assert_cmpint(9, ==, packet->data->len);
122
 
        g_assert_cmpint(TRUE, ==, g_memeq(S(packet->data), C("\xff\x00\x00#07000")));
123
 
 
124
 
        g_assert_cmpint(0, ==, network_mysqld_proto_get_err_packet(packet, err_packet));
125
 
 
126
 
        /* check if encoding and decoding works */
127
 
        err_packet->errcode = 3;
128
 
        g_string_assign_len(err_packet->errmsg, C("test"));
129
 
        g_string_assign_len(err_packet->sqlstate, C("01234"));
130
 
 
131
 
        g_string_truncate(packet->data, 0);
132
 
        packet->offset = 0;
133
 
 
134
 
        g_assert_cmpint(0, ==, network_mysqld_proto_append_err_packet(packet->data, err_packet));
135
 
        g_assert_cmpint(13, ==, packet->data->len);
136
 
        g_assert_cmpint(TRUE, ==, g_memeq(S(packet->data), C("\xff\x03\x00#01234test")));
137
 
        
138
 
        network_mysqld_err_packet_free(err_packet);
139
 
 
140
 
        err_packet = network_mysqld_err_packet_new();
141
 
        g_assert_cmpint(0, ==, network_mysqld_proto_get_err_packet(packet, err_packet));
142
 
        g_assert_cmpint(3, ==, err_packet->errcode);
143
 
        g_assert_cmpstr("01234", ==, err_packet->sqlstate->str);
144
 
        g_assert_cmpstr("test", ==, err_packet->errmsg->str);
145
 
 
146
 
        network_mysqld_err_packet_free(err_packet);
147
 
 
148
 
        /* check if too-short packet is denied */
149
 
        err_packet = network_mysqld_err_packet_new();
150
 
        g_string_truncate(packet->data, 0);
151
 
        packet->offset = 0;
152
 
        g_assert_cmpint(-1, ==, network_mysqld_proto_get_err_packet(packet, err_packet));
153
 
 
154
 
        network_mysqld_err_packet_free(err_packet);
155
 
 
156
 
        g_string_free(packet->data, TRUE);
157
 
        network_packet_free(packet);
158
 
}
159
 
 
160
 
void t_eof_packet_new(void) {
161
 
        network_mysqld_eof_packet_t *eof_packet;
162
 
 
163
 
        eof_packet = network_mysqld_eof_packet_new();
164
 
        g_assert(eof_packet);
165
 
 
166
 
        network_mysqld_eof_packet_free(eof_packet);
167
 
}
168
 
 
169
 
void t_eof_packet_append(void) {
170
 
        network_mysqld_eof_packet_t *eof_packet;
171
 
        network_packet *packet;
172
 
 
173
 
        eof_packet = network_mysqld_eof_packet_new();
174
 
        packet = network_packet_new();
175
 
        packet->data = g_string_new(NULL);
176
 
 
177
 
        /* check if a empty ok-packet is encoded correctly */
178
 
        g_assert_cmpint(0, ==, network_mysqld_proto_append_eof_packet(packet->data, eof_packet));
179
 
        g_assert_cmpint(5, ==, packet->data->len);
180
 
        g_assert_cmpint(TRUE, ==, g_memeq(S(packet->data), C("\xfe\x00\x00\x00\x00")));
181
 
 
182
 
        g_assert_cmpint(0, ==, network_mysqld_proto_get_eof_packet(packet, eof_packet));
183
 
 
184
 
        /* check if encoding and decoding works */
185
 
        eof_packet->warnings = 1;
186
 
        eof_packet->server_status = 2;
187
 
 
188
 
        g_string_truncate(packet->data, 0);
189
 
        packet->offset = 0;
190
 
 
191
 
        g_assert_cmpint(0, ==, network_mysqld_proto_append_eof_packet(packet->data, eof_packet));
192
 
        g_assert_cmpint(5, ==, packet->data->len);
193
 
        g_assert_cmpint(TRUE, ==, g_memeq(S(packet->data), C("\xfe\x01\x00\x02\x00")));
194
 
        
195
 
        network_mysqld_eof_packet_free(eof_packet);
196
 
 
197
 
        eof_packet = network_mysqld_eof_packet_new();
198
 
        g_assert_cmpint(0, ==, network_mysqld_proto_get_eof_packet(packet, eof_packet));
199
 
        g_assert_cmpint(1, ==, eof_packet->warnings);
200
 
        g_assert_cmpint(2, ==, eof_packet->server_status);
201
 
 
202
 
        network_mysqld_eof_packet_free(eof_packet);
203
 
 
204
 
        /* check if too-short packet is denied */
205
 
        eof_packet = network_mysqld_eof_packet_new();
206
 
        g_string_truncate(packet->data, 0);
207
 
        packet->offset = 0;
208
 
        g_assert_cmpint(-1, ==, network_mysqld_proto_get_eof_packet(packet, eof_packet));
209
 
 
210
 
        network_mysqld_eof_packet_free(eof_packet);
211
 
 
212
 
        g_string_free(packet->data, TRUE);
213
 
        network_packet_free(packet);
214
 
}
215
 
 
216
 
void test_mysqld_handshake(void) {
217
 
        const char raw_packet[] = "J\0\0\0"
218
 
                "\n"
219
 
                "5.0.45-Debian_1ubuntu3.3-log\0"
220
 
                "w\0\0\0"
221
 
                "\"L;!3|8@"
222
 
                "\0"
223
 
                ",\242" /* 0x2c 0xa2 */
224
 
                "\10"
225
 
                "\2\0"
226
 
                "\0\0\0\0\0\0\0\0\0\0\0\0\0"
227
 
                "vV,s#PLjSA+Q"
228
 
                "\0";
229
 
        network_mysqld_auth_challenge *shake;
230
 
        network_packet packet;
231
 
 
232
 
        shake = network_mysqld_auth_challenge_new();
233
 
        
234
 
        packet.data = g_string_new(NULL);
235
 
        packet.offset = 0;
236
 
        g_string_append_len(packet.data, C(raw_packet));
237
 
 
238
 
        g_assert_cmpint(packet.data->len, ==, 78);
239
 
 
240
 
        g_assert_cmpint(0, ==, network_mysqld_proto_skip_network_header(&packet));
241
 
        g_assert_cmpint(0, ==, network_mysqld_proto_get_auth_challenge(&packet, shake));
242
 
 
243
 
        g_assert(shake->server_version == 50045);
244
 
        g_assert(shake->thread_id == 119);
245
 
        g_assert(shake->server_status == 
246
 
                        SERVER_STATUS_AUTOCOMMIT);
247
 
        g_assert(shake->charset == 8);
248
 
        g_assert(shake->capabilities ==
249
 
                        (CLIENT_CONNECT_WITH_DB |
250
 
                        CLIENT_LONG_FLAG |
251
 
 
252
 
                        CLIENT_COMPRESS |
253
 
 
254
 
                        CLIENT_PROTOCOL_41 |
255
 
 
256
 
                        CLIENT_TRANSACTIONS |
257
 
                        CLIENT_SECURE_CONNECTION));
258
 
 
259
 
        g_assert(shake->challenge->len == 20);
260
 
        g_assert(0 == memcmp(shake->challenge->str, "\"L;!3|8@vV,s#PLjSA+Q", shake->challenge->len));
261
 
 
262
 
        /* ... and back */
263
 
        g_string_truncate(packet.data, 0);
264
 
        g_string_append_len(packet.data, C("J\0\0\0"));
265
 
        network_mysqld_proto_append_auth_challenge(packet.data, shake);
266
 
 
267
 
        g_assert_cmpint(packet.data->len, ==, sizeof(raw_packet) - 1);
268
 
 
269
 
        g_assert(0 == memcmp(packet.data->str, raw_packet, packet.data->len));
270
 
 
271
 
        network_mysqld_auth_challenge_free(shake);
272
 
        g_string_free(packet.data, TRUE);
273
 
}
274
 
 
275
 
void test_mysqld_auth_empty_pw(void) {
276
 
        const char raw_packet[] = 
277
 
                "&\0\0\1\205\246\3\0\0\0\0\1\10\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0root\0\0"
278
 
                ;
279
 
        GString *packet;
280
 
        network_mysqld_auth_response *auth;
281
 
 
282
 
        auth = network_mysqld_auth_response_new();
283
 
        g_string_assign(auth->username, "root");
284
 
        auth->capabilities    = 
285
 
                (CLIENT_LONG_PASSWORD |
286
 
                CLIENT_LONG_FLAG |
287
 
                CLIENT_LOCAL_FILES | 
288
 
                CLIENT_PROTOCOL_41 |
289
 
                CLIENT_INTERACTIVE |
290
 
                CLIENT_TRANSACTIONS |
291
 
                CLIENT_SECURE_CONNECTION |
292
 
                CLIENT_MULTI_STATEMENTS |
293
 
                CLIENT_MULTI_RESULTS); 
294
 
        auth->max_packet_size = 1 << 24;
295
 
        auth->charset         = 8;
296
 
        
297
 
        packet = g_string_new(NULL);
298
 
 
299
 
        network_mysqld_proto_append_int8(packet, 38);
300
 
        network_mysqld_proto_append_int8(packet, 0);
301
 
        network_mysqld_proto_append_int8(packet, 0);
302
 
        network_mysqld_proto_append_int8(packet, 1);
303
 
 
304
 
        g_assert(0 == network_mysqld_proto_append_auth_response(packet, auth));
305
 
 
306
 
#if 0
307
 
        g_message("%s: packet->len = %d, packet is: %d", G_STRLOC, packet->len, sizeof(raw_packet) - 1);
308
 
#endif
309
 
 
310
 
        g_assert(packet->len == sizeof(raw_packet) - 1);
311
 
 
312
 
#if 0
313
 
        for (i = 0; i < packet->len; i++) {
314
 
                g_message("%s: [%d] %02x %c= %02x", G_STRLOC, i, packet->str[i], packet->str[i] == raw_packet[i] ? '=' : '!', raw_packet[i]);
315
 
        }
316
 
#endif
317
 
 
318
 
        g_assert(0 == memcmp(packet->str, raw_packet, sizeof(raw_packet) - 1));
319
 
 
320
 
        network_mysqld_auth_response_free(auth);
321
 
 
322
 
        g_string_free(packet, TRUE);
323
 
}
324
 
 
325
 
void test_mysqld_auth_with_pw(void) {
326
 
        const char raw_packet[] = 
327
 
                ":\0\0\1"
328
 
                "\205\246\3\0"
329
 
                "\0\0\0\1"
330
 
                "\10"
331
 
                "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
332
 
                "root\0"
333
 
                "\24\241\304\260>\255\1:F,\256\337K\323\340\4\273\354I\256\204"
334
 
                ;
335
 
        const char raw_challenge[] = 
336
 
                        "%@R[SoWC"      /* part 1 */
337
 
                        "+L|LG_+R={tV"; /* part 2 */
338
 
 
339
 
        GString *packet, *challenge, *hashed_password;
340
 
        network_mysqld_auth_response *auth;
341
 
 
342
 
        auth = network_mysqld_auth_response_new();
343
 
        g_string_assign(auth->username, "root");
344
 
        auth->capabilities    = 
345
 
                CLIENT_LONG_PASSWORD |
346
 
                CLIENT_LONG_FLAG |
347
 
                CLIENT_LOCAL_FILES | 
348
 
                CLIENT_PROTOCOL_41 |
349
 
                CLIENT_INTERACTIVE |
350
 
                CLIENT_TRANSACTIONS |
351
 
                CLIENT_SECURE_CONNECTION |
352
 
                CLIENT_MULTI_STATEMENTS |
353
 
                CLIENT_MULTI_RESULTS; 
354
 
        auth->max_packet_size = 1 << 24;
355
 
        auth->charset         = 8;
356
 
 
357
 
        challenge = g_string_new(NULL);
358
 
        hashed_password = g_string_new(NULL);
359
 
        g_string_append_len(challenge, raw_challenge, sizeof(raw_challenge) - 1);
360
 
 
361
 
        network_mysqld_proto_password_hash(hashed_password, C("123"));
362
 
        network_mysqld_proto_password_scramble(auth->response, S(challenge), S(hashed_password));
363
 
 
364
 
        g_string_free(hashed_password, TRUE);
365
 
        
366
 
        packet = g_string_new(NULL);
367
 
 
368
 
        network_mysqld_proto_append_int8(packet, 58);
369
 
        network_mysqld_proto_append_int8(packet, 0);
370
 
        network_mysqld_proto_append_int8(packet, 0);
371
 
        network_mysqld_proto_append_int8(packet, 1);
372
 
 
373
 
        g_assert(0 == network_mysqld_proto_append_auth_response(packet, auth));
374
 
        g_assert(packet->len == sizeof(raw_packet) - 1);
375
 
 
376
 
#if 0
377
 
        for (i = 0; i < packet->len; i++) {
378
 
                g_message("%s: [%d] %02x %c= %02x", G_STRLOC, i, packet->str[i], packet->str[i] == raw_packet[i] ? '=' : '!', raw_packet[i]);
379
 
        }
380
 
#endif
381
 
 
382
 
        g_assert(0 == memcmp(packet->str, raw_packet, sizeof(raw_packet) - 1));
383
 
 
384
 
        network_mysqld_auth_response_free(auth);
385
 
 
386
 
        g_string_free(packet, TRUE);
387
 
        g_string_free(challenge, TRUE);
388
 
}
389
 
 
390
 
/**
391
 
 * @test
392
 
 *   network_mysqld_auth_response_new() and network_mysqld_auth_response_free() 
393
 
 *   don't cause a crash 
394
 
 */
395
 
void t_auth_response_new() {
396
 
        network_mysqld_auth_response *shake;
397
 
 
398
 
        shake = network_mysqld_auth_response_new();
399
 
        g_assert(shake);
400
 
 
401
 
        network_mysqld_auth_response_free(shake);
402
 
}
403
 
 
404
 
/**
405
 
 * @test
406
 
 *   network_mysqld_proto_get_auth_response() can decode a string
407
 
 *   network_mysqld_proto_append_auth_response() can encode the result
408
 
 *     of the network_mysqld_proto_get_auth_response() 
409
 
 */
410
 
void t_mysqld_get_auth_response(void) {
411
 
        const char raw_packet[] = 
412
 
                "\205\246\3\0"
413
 
                "\0\0\0\1"
414
 
                "\10"
415
 
                "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
416
 
                "root\0"
417
 
                "\24\241\304\260>\255\1:F,\256\337K\323\340\4\273\354I\256\204"
418
 
                ;
419
 
 
420
 
        network_mysqld_auth_response *auth;
421
 
        network_packet packet;
422
 
        int err = 0;
423
 
 
424
 
        auth = network_mysqld_auth_response_new();
425
 
        packet.data = g_string_new_len(C(raw_packet));
426
 
        packet.offset = 0;
427
 
        
428
 
        err = err || network_mysqld_proto_get_auth_response(&packet, auth);
429
 
 
430
 
        g_assert_cmpint(err, ==, 0);
431
 
 
432
 
        g_assert(auth->username);
433
 
        g_assert_cmpint(auth->username->len, ==, 4);
434
 
        g_assert_cmpstr(auth->username->str, ==, "root");
435
 
 
436
 
        g_assert_cmpuint(auth->capabilities, ==,
437
 
                CLIENT_LONG_PASSWORD |
438
 
                CLIENT_LONG_FLAG |
439
 
                CLIENT_LOCAL_FILES | 
440
 
                CLIENT_PROTOCOL_41 |
441
 
                CLIENT_INTERACTIVE |
442
 
                CLIENT_TRANSACTIONS |
443
 
                CLIENT_SECURE_CONNECTION |
444
 
                CLIENT_MULTI_STATEMENTS |
445
 
                CLIENT_MULTI_RESULTS); 
446
 
        g_assert_cmpuint(auth->max_packet_size, ==, 1 << 24);
447
 
        g_assert_cmpuint(auth->charset        , ==, 8);
448
 
 
449
 
        g_string_truncate(packet.data, 0);
450
 
        packet.offset = 0;
451
 
 
452
 
        err = err || network_mysqld_proto_append_auth_response(packet.data, auth);
453
 
        g_assert_cmpint(err, ==, 0);
454
 
 
455
 
        g_assert_cmpint(packet.data->len, ==, sizeof(raw_packet) - 1);
456
 
        g_assert_cmpint(TRUE, ==, g_memeq(S(packet.data), raw_packet, packet.data->len));
457
 
 
458
 
        network_mysqld_auth_response_free(auth);
459
 
 
460
 
        /* empty auth struct */
461
 
        g_string_truncate(packet.data, 0);
462
 
        packet.offset = 0;
463
 
 
464
 
        auth = network_mysqld_auth_response_new();
465
 
        err = err || network_mysqld_proto_append_auth_response(packet.data, auth);
466
 
        g_assert_cmpint(err, ==, 0);
467
 
        network_mysqld_auth_response_free(auth);
468
 
 
469
 
        g_string_free(packet.data, TRUE);
470
 
}
471
 
 
472
 
/**
473
 
 * @test
474
 
 *   network_mysqld_proto_get_auth_response() can decode a pre-4.0 packet
475
 
 *   network_mysqld_proto_append_auth_response() can encode the result
476
 
 *     of the network_mysqld_proto_get_auth_response() 
477
 
 */
478
 
void t_mysqld_get_auth_response_pre_41(void) {
479
 
        const char raw_packet[] = 
480
 
                "\205$"
481
 
                "\0\0\0"
482
 
                "root\0"
483
 
                ;
484
 
 
485
 
        network_mysqld_auth_response *auth;
486
 
        network_packet packet;
487
 
        int err = 0;
488
 
 
489
 
        auth = network_mysqld_auth_response_new();
490
 
        packet.data = g_string_new_len(C(raw_packet));
491
 
        packet.offset = 0;
492
 
 
493
 
        err = err || network_mysqld_proto_get_auth_response(&packet, auth);
494
 
 
495
 
        g_assert_cmpint(err, ==, 0);
496
 
 
497
 
        g_assert(auth->username);
498
 
        g_assert_cmpint(auth->username->len, ==, 4);
499
 
        g_assert_cmpstr(auth->username->str, ==, "root");
500
 
 
501
 
        g_assert_cmpuint(auth->capabilities, ==,
502
 
                CLIENT_LONG_PASSWORD |
503
 
                CLIENT_LONG_FLAG |
504
 
                CLIENT_LOCAL_FILES | 
505
 
                CLIENT_INTERACTIVE |
506
 
                CLIENT_TRANSACTIONS 
507
 
                ); 
508
 
        g_assert_cmpuint(auth->max_packet_size, ==, 0);
509
 
 
510
 
        g_string_truncate(packet.data, 0);
511
 
        packet.offset = 0;
512
 
 
513
 
        err = err || network_mysqld_proto_append_auth_response(packet.data, auth);
514
 
        g_assert_cmpint(err, ==, 0);
515
 
 
516
 
        g_assert_cmpint(packet.data->len, ==, sizeof(raw_packet) - 1);
517
 
        g_assert_cmpint(TRUE, ==, g_memeq(S(packet.data), raw_packet, packet.data->len));
518
 
 
519
 
        network_mysqld_auth_response_free(auth);
520
 
 
521
 
        /* empty auth struct */
522
 
        g_string_truncate(packet.data, 0);
523
 
        packet.offset = 0;
524
 
 
525
 
        auth = network_mysqld_auth_response_new();
526
 
        err = err || network_mysqld_proto_append_auth_response(packet.data, auth);
527
 
        g_assert_cmpint(err, ==, 0);
528
 
        network_mysqld_auth_response_free(auth);
529
 
 
530
 
        g_string_free(packet.data, TRUE);
531
 
}
532
 
 
533
 
/**
534
 
 * @test
535
 
 *   network_mysqld_proto_get_auth_response() can decode a broken pre-4.0 packet
536
 
 */
537
 
void t_mysqld_get_auth_response_no_term(void) {
538
 
        const char raw_packet[] = 
539
 
                "\205$"
540
 
                "\0\0\0"
541
 
                "root\0" /* missing trailing \0 */
542
 
                "foo"
543
 
                ;
544
 
 
545
 
        network_mysqld_auth_response *auth;
546
 
        network_packet packet;
547
 
        int err = 0;
548
 
 
549
 
        auth = network_mysqld_auth_response_new();
550
 
        packet.data = g_string_new_len(C(raw_packet));
551
 
        packet.offset = 0;
552
 
 
553
 
        err = err || network_mysqld_proto_get_auth_response(&packet, auth);
554
 
 
555
 
        g_assert_cmpint(err, !=, 0);
556
 
 
557
 
        network_mysqld_auth_response_free(auth);
558
 
 
559
 
        g_string_free(packet.data, TRUE);
560
 
}
561
 
 
562
 
 
563
 
typedef struct {
564
 
        const char *s;
565
 
        size_t s_len;
566
 
} strings;
567
 
 
568
 
void t_resultset_fields_works(void) {
569
 
        strings packets[] = {
570
 
                { C("\1\0\0\1\2") }, /* 2 fields */
571
 
                { C("6\0\0\2\3def\0\6STATUS\0\rVariable_name\rVariable_name\f\10\0P\0\0\0\375\1\0\0\0\0") },
572
 
                { C("&\0\0\3\3def\0\6STATUS\0\5Value\5Value\f\10\0\0\2\0\0\375\1\0\0\0\0") },
573
 
                { C("\5\0\0\4\376\0\0\"\0") }, /* EOF */
574
 
                { C("\23\0\0\5\17Aborted_clients\00298") },
575
 
                { C("\5\0\0\4\376\0\0\"\0") }, /* EOF */
576
 
                { NULL, 0 }
577
 
        };
578
 
        int i;
579
 
        network_queue *q;
580
 
        GPtrArray *fields;
581
 
 
582
 
        q = network_queue_new();
583
 
 
584
 
        for (i = 0; packets[i].s; i++) {
585
 
                network_queue_append(q, g_string_new_len(packets[i].s, packets[i].s_len));
586
 
        }
587
 
 
588
 
        fields = g_ptr_array_new();
589
 
        g_assert(NULL != network_mysqld_proto_get_fielddefs(q->chunks->head, fields));
590
 
 
591
 
        network_queue_free(q);
592
 
}
593
 
 
594
 
void t_resultset_fields_parse_err(void) {
595
 
        strings packets[] = {
596
 
                { C("\1\0\0\1\377") }, /* err-packet */
597
 
                { C("6\0\0\2\3def\0\6STATUS\0\rVariable_name\rVariable_name\f\10\0P\0\0\0\375\1\0\0\0\0") },
598
 
                { C("&\0\0\3\3def\0\6STATUS\0\5Value\5Value\f\10\0\0\2\0\0\375\1\0\0\0\0") },
599
 
                { C("\5\0\0\4\376\0\0\"\0") }, /* EOF */
600
 
                { C("\23\0\0\5\17Aborted_clients\00298") },
601
 
                { C("\5\0\0\4\376\0\0\"\0") }, /* EOF */
602
 
                { NULL, 0 }
603
 
        };
604
 
        int i;
605
 
        network_queue *q;
606
 
        GPtrArray *fields;
607
 
 
608
 
        q = network_queue_new();
609
 
 
610
 
        for (i = 0; packets[i].s; i++) {
611
 
                network_queue_append(q, g_string_new_len(packets[i].s, packets[i].s_len));
612
 
        }
613
 
 
614
 
        fields = g_ptr_array_new();
615
 
        g_assert(NULL == network_mysqld_proto_get_fielddefs(q->chunks->head, fields));
616
 
 
617
 
        network_queue_free(q);
618
 
}
619
 
 
620
 
void t_resultset_fields_parse_null(void) {
621
 
        strings packets[] = {
622
 
                { C("\1\0\0\1\373") }, /* NULL */
623
 
                { C("6\0\0\2\3def\0\6STATUS\0\rVariable_name\rVariable_name\f\10\0P\0\0\0\375\1\0\0\0\0") },
624
 
                { C("&\0\0\3\3def\0\6STATUS\0\5Value\5Value\f\10\0\0\2\0\0\375\1\0\0\0\0") },
625
 
                { C("\5\0\0\4\376\0\0\"\0") }, /* EOF */
626
 
                { C("\23\0\0\5\17Aborted_clients\00298") },
627
 
                { C("\5\0\0\4\376\0\0\"\0") }, /* EOF */
628
 
                { NULL, 0 }
629
 
        };
630
 
        int i;
631
 
        network_queue *q;
632
 
        GPtrArray *fields;
633
 
 
634
 
        q = network_queue_new();
635
 
 
636
 
        for (i = 0; packets[i].s; i++) {
637
 
                network_queue_append(q, g_string_new_len(packets[i].s, packets[i].s_len));
638
 
        }
639
 
 
640
 
        fields = g_ptr_array_new();
641
 
        g_assert(NULL == network_mysqld_proto_get_fielddefs(q->chunks->head, fields));
642
 
 
643
 
        network_queue_free(q);
644
 
}
645
 
 
646
 
void t_resultset_fields_parse_low(void) {
647
 
        strings packets[] = {
648
 
                { C("\1\0\0\1\2") }, /* 2 fields */
649
 
                { C("6\0\0\2\3def\0\6STATUS\0\rVariable_name\rVariable_name\f\10\0P\0\0\0\375\1\0\0\0\0") },
650
 
                { C("\5\0\0\4\376\0\0\"\0") }, /* EOF */
651
 
                { C("\23\0\0\5\17Aborted_clients\00298") },
652
 
                { C("\5\0\0\4\376\0\0\"\0") }, /* EOF */
653
 
                { NULL, 0 }
654
 
        };
655
 
        int i;
656
 
        network_queue *q;
657
 
        GPtrArray *fields;
658
 
 
659
 
        q = network_queue_new();
660
 
 
661
 
        for (i = 0; packets[i].s; i++) {
662
 
                network_queue_append(q, g_string_new_len(packets[i].s, packets[i].s_len));
663
 
        }
664
 
 
665
 
        fields = g_ptr_array_new();
666
 
        g_assert(NULL == network_mysqld_proto_get_fielddefs(q->chunks->head, fields));
667
 
 
668
 
        network_queue_free(q);
669
 
}
670
 
 
671
 
void t_resultset_fields_parse_high(void) {
672
 
        strings packets[] = {
673
 
                { C("\1\0\0\1\2") }, /* 2 fields */
674
 
                { C("6\0\0\2\3def\0\6STATUS\0\rVariable_name\rVariable_name\f\10\0P\0\0\0\375\1\0\0\0\0") },
675
 
                { C("&\0\0\3\3def\0\6STATUS\0\5Value\5Value\f\10\0\0\2\0\0\375\1\0\0\0\0") },
676
 
                { C("&\0\0\3\3def\0\6STATUS\0\5Value\5Value\f\10\0\0\2\0\0\375\1\0\0\0\0") },
677
 
                { C("\5\0\0\4\376\0\0\"\0") }, /* EOF */
678
 
                { C("\23\0\0\5\17Aborted_clients\00298") },
679
 
                { C("\5\0\0\4\376\0\0\"\0") }, /* EOF */
680
 
                { NULL, 0 }
681
 
        };
682
 
        int i;
683
 
        network_queue *q;
684
 
        GPtrArray *fields;
685
 
 
686
 
        q = network_queue_new();
687
 
 
688
 
        for (i = 0; packets[i].s; i++) {
689
 
                network_queue_append(q, g_string_new_len(packets[i].s, packets[i].s_len));
690
 
        }
691
 
 
692
 
        fields = g_ptr_array_new();
693
 
        g_assert(NULL == network_mysqld_proto_get_fielddefs(q->chunks->head, fields));
694
 
 
695
 
        network_queue_free(q);
696
 
}
697
 
 
698
 
 
699
 
/**
700
 
 * @cond
701
 
 *   don't include the main() function the docs
702
 
 */
703
 
int main(int argc, char **argv) {
704
 
        g_test_init(&argc, &argv, NULL);
705
 
        g_test_bug_base("http://bugs.mysql.com/");
706
 
 
707
 
        g_test_add_func("/core/ok-packet-new", t_ok_packet_new);
708
 
        g_test_add_func("/core/ok-packet-append", t_ok_packet_append);
709
 
        g_test_add_func("/core/eof-packet-new", t_eof_packet_new);
710
 
        g_test_add_func("/core/eof-packet-append", t_eof_packet_append);
711
 
        g_test_add_func("/core/err-packet-new", t_err_packet_new);
712
 
        g_test_add_func("/core/err-packet-append", t_err_packet_append);
713
 
 
714
 
        g_test_add_func("/core/mysqld-proto-handshake", test_mysqld_handshake);
715
 
 
716
 
        g_test_add_func("/core/mysqld-proto-pw-empty", test_mysqld_auth_empty_pw);
717
 
        g_test_add_func("/core/mysqld-proto-pw", test_mysqld_auth_with_pw);
718
 
        
719
 
        g_test_add_func("/core/mysqld-proto-auth-response-new", t_auth_response_new);
720
 
        g_test_add_func("/core/mysqld-proto-get-auth-response", t_mysqld_get_auth_response);
721
 
        g_test_add_func("/core/mysqld-proto-get-auth-response-pre-4.1", t_mysqld_get_auth_response_pre_41);
722
 
        g_test_add_func("/core/mysqld-proto-get-auth-response-no-term", t_mysqld_get_auth_response_no_term);
723
 
        
724
 
        g_test_add_func("/core/resultset-fields", t_resultset_fields_works);
725
 
        g_test_add_func("/core/resultset-fields-broken-proto-err", t_resultset_fields_parse_err);
726
 
        g_test_add_func("/core/resultset-fields-broken-proto-null", t_resultset_fields_parse_null);
727
 
        g_test_add_func("/core/resultset-fields-broken-proto-field-count-low", t_resultset_fields_parse_low);
728
 
        g_test_add_func("/core/resultset-fields-broken-proto-field-count-high", t_resultset_fields_parse_high);
729
 
 
730
 
        return g_test_run();
731
 
}
732
 
/** @endcond */
733
 
#else
734
 
int main() {
735
 
        return 77;
736
 
}
737
 
#endif