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

« back to all changes in this revision

Viewing changes to tests/unit/check_mysqld_proto.c

  • Committer: Kay Roepke
  • Date: 2009-01-20 12:04:47 UTC
  • Revision ID: kay@mysql.com-20090120120447-fkoxq5ovwf9iyk4v
add the wrapper script/makefile fu to start mysql-proxy without having to set the environment variables

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* $%BEGINLICENSE%$
2
 
 Copyright (c) 2007, 2009, Oracle and/or its affiliates. All rights reserved.
 
2
 Copyright (C) 2007-2008 MySQL AB, 2008 Sun Microsystems, Inc
3
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.
 
4
 This program is free software; you can redistribute it and/or modify
 
5
 it under the terms of the GNU General Public License as published by
 
6
 the Free Software Foundation; version 2 of the License.
8
7
 
9
8
 This program is distributed in the hope that it will be useful,
10
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
11
 GNU General Public License for more details.
13
12
 
14
13
 You should have received a copy of the GNU General Public License
15
14
 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
 
15
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
16
 
19
17
 $%ENDLICENSE%$ */
20
18
 
31
29
 
32
30
#if GLIB_CHECK_VERSION(2, 16, 0)
33
31
#define C(x) x, sizeof(x) - 1
34
 
#define S(x) x->str, x->len
35
32
 
36
33
/**
37
34
 * Tests for the MySQL Protocol Codec functions
46
43
 * how to handle > 16M ?
47
44
 */
48
45
void test_mysqld_proto_header(void) {
49
 
        GString h;
50
46
        unsigned char header[4];
51
 
        guint32 length = 1256;
52
 
        guint8 id = 25;
53
 
 
54
 
        h.str = (char *)header;
55
 
        h.len = sizeof(header);
56
 
 
57
 
        g_assert(0 == network_mysqld_proto_set_packet_len(&h, length));
58
 
        g_assert(0 == network_mysqld_proto_set_packet_id(&h, id));
59
 
        g_assert(length == network_mysqld_proto_get_packet_len(&h));
60
 
        g_assert(id == network_mysqld_proto_get_packet_id(&h));
 
47
        size_t length = 1256;
 
48
 
 
49
        g_assert(0 == network_mysqld_proto_set_header(header, length, 0));
 
50
        g_assert(length == network_mysqld_proto_get_header(header));
61
51
}
62
52
 
63
53
/**
121
111
 *
122
112
 */
123
113
void test_mysqld_proto_int(void) {
124
 
        guint64 length;
 
114
        guint64 length, value;
125
115
        guint8 value8;
126
116
        guint16 value16;
127
117
        guint32 value32;
138
128
        g_string_truncate(packet.data, 0);
139
129
        g_assert(0 == network_mysqld_proto_append_int8(packet.data, length));
140
130
        g_assert(packet.data->len == 1);
141
 
        g_assert_cmpint(TRUE, ==, g_memeq(S(packet.data), C("\xfa")));
 
131
        g_assert(0 == memcmp(packet.data->str, C("\xfa")));
142
132
        g_assert_cmpint(0, ==, network_mysqld_proto_get_int8(&packet, &value8));
143
133
        g_assert_cmpint(length, ==, value8);
144
134
 
146
136
        g_string_truncate(packet.data, 0);
147
137
        g_assert(0 == network_mysqld_proto_append_int16(packet.data, length));
148
138
        g_assert(packet.data->len == 2);
149
 
        g_assert_cmpint(TRUE, ==, g_memeq(S(packet.data), C("\xfa\xff")));
 
139
        g_assert(0 == memcmp(packet.data->str, C("\xfa\xff")));
150
140
        g_assert_cmpint(0, ==, network_mysqld_proto_get_int16(&packet, &value16));
151
141
        g_assert_cmpint(length, ==, value16);
152
142
 
154
144
        g_string_truncate(packet.data, 0);
155
145
        g_assert(0 == network_mysqld_proto_append_int24(packet.data, length));
156
146
        g_assert(packet.data->len == 3);
157
 
        g_assert_cmpint(TRUE, ==, g_memeq(S(packet.data), C("\xfa\xff\xff")));
 
147
        g_assert(0 == memcmp(packet.data->str, C("\xfa\xff\xff")));
158
148
        g_assert_cmpint(0, ==, network_mysqld_proto_get_int24(&packet, &value32));
159
149
        g_assert_cmpint(length, ==, value32);
160
150
 
162
152
        g_string_truncate(packet.data, 0);
163
153
        g_assert(0 == network_mysqld_proto_append_int32(packet.data, length));
164
154
        g_assert(packet.data->len == 4);
165
 
        g_assert_cmpint(TRUE, ==, g_memeq(S(packet.data), C("\xfa\xff\xff\xff")));
 
155
        g_assert(0 == memcmp(packet.data->str, C("\xfa\xff\xff\xff")));
166
156
        g_assert_cmpint(0, ==, network_mysqld_proto_get_int32(&packet, &value32));
167
157
        g_assert_cmpint(length, ==, value32);
168
158
 
170
160
        g_string_truncate(packet.data, 0);
171
161
        g_assert(0 == network_mysqld_proto_append_int48(packet.data, length));
172
162
        g_assert(packet.data->len == 6);
173
 
        g_assert_cmpint(TRUE, ==, g_memeq(S(packet.data), C("\xfa\xff\xff\xff\x00\x00")));
 
163
        g_assert(0 == memcmp(packet.data->str, C("\xfa\xff\xff\xff\x00\x00")));
174
164
        g_assert_cmpint(0, ==, network_mysqld_proto_get_int48(&packet, &value64));
175
165
        g_assert_cmpint(length, ==, value64);
176
166
 
177
 
        /* we need a ULL declaration here which might not be available, just shift it ourself */
178
 
        length = 0x00ff; 
179
 
        length <<= 32;
180
 
        length |= 0xfffffffaUL; packet.offset = 0;
 
167
        length = 0x00fffffffffaUL; packet.offset = 0;
181
168
        g_string_truncate(packet.data, 0);
182
169
        g_assert(0 == network_mysqld_proto_append_int64(packet.data, length));
183
170
        g_assert(packet.data->len == 8);
184
 
        g_assert_cmpint(TRUE, ==, g_memeq(S(packet.data), C("\xfa\xff\xff\xff\xff\x00\x00\x00")));
 
171
        g_assert(0 == memcmp(packet.data->str, C("\xfa\xff\xff\xff\xff\x00\x00\x00")));
185
172
        g_assert_cmpint(0, ==, network_mysqld_proto_get_int64(&packet, &value64));
186
173
        g_assert_cmpuint(length, ==, value64);
187
174
 
191
178
        g_assert_cmpint(0, ==, network_mysqld_proto_append_int8(packet.data, 0xa5));
192
179
        g_assert_cmpint(packet.data->len, ==, 1);
193
180
        g_assert_cmpint(packet.offset, ==, 0);
194
 
        g_assert_cmpint(TRUE, ==, g_memeq(S(packet.data), C("\xa5")));
 
181
        g_assert_cmpint(0, ==, memcmp(packet.data->str, C("\xa5")));
195
182
 
196
183
        g_assert_cmpint(0, ==, network_mysqld_proto_append_int8(packet.data, 0x5a));
197
184
        g_assert_cmpint(packet.data->len, ==, 2);
198
185
        g_assert_cmpint(packet.offset, ==, 0);
199
 
        g_assert_cmpint(TRUE, ==, g_memeq(S(packet.data), C("\xa5\x5a")));
 
186
        g_assert_cmpint(0, ==, memcmp(packet.data->str, C("\xa5\x5a")));
200
187
        g_assert_cmpint(0, ==, network_mysqld_proto_get_int8(&packet, &value8));
201
188
        g_assert_cmpint(packet.offset, ==, 1);
202
189
        g_assert_cmpint(0xa5, ==, value8);
277
264
 
278
265
        g_string_assign_len(packet->data, C(rotate_packet));
279
266
 
280
 
        g_assert_cmpint(0, ==, network_mysqld_proto_skip_network_header(packet));
281
 
        g_assert_cmpint(0, ==, network_mysqld_proto_get_binlog_status(packet));
282
 
        g_assert_cmpint(0, ==, network_mysqld_proto_get_binlog_event_header(packet, event));
 
267
        network_mysqld_proto_skip_network_header(packet);
 
268
        network_mysqld_proto_get_binlog_status(packet);
 
269
        network_mysqld_proto_get_binlog_event_header(packet, event);
283
270
 
284
 
        g_assert_cmpint(event->server_id, ==, 1);
285
 
        g_assert_cmpint(event->timestamp, ==, 0);
286
 
        g_assert_cmpint(event->flags, ==, 0);
287
271
        g_assert_cmpint(event->event_type, ==, ROTATE_EVENT);
288
272
 
289
 
        g_assert_cmpint(0, ==, network_mysqld_proto_get_binlog_event(packet, binlog, event));
 
273
        network_mysqld_proto_get_binlog_event(packet, binlog, event);
290
274
 
291
275
        g_assert_cmpint(event->event.rotate_event.binlog_pos, ==, 102);
292
276
        g_assert_cmpstr(event->event.rotate_event.binlog_file, ==, "hostname-bin.000009");
381
365
        g_string_free(packet.data, TRUE);
382
366
}
383
367
 
384
 
void test_mysqld_proto_gstring(void) {
385
 
        network_packet packet;
386
 
        GString *value = g_string_new(NULL);
387
 
 
388
 
        packet.data = g_string_new(NULL);
389
 
 
390
 
        packet.offset = 0;
391
 
        g_string_truncate(packet.data, 0);
392
 
        g_assert_cmpint(0, !=, network_mysqld_proto_get_gstring(&packet, value));
393
 
        g_assert_cmpint(0, ==, value->len);
394
 
 
395
 
        packet.offset = 0;
396
 
        g_string_assign_len(packet.data, C("012345")); /* no trailing \0 */
397
 
        g_assert_cmpint(0, !=, network_mysqld_proto_get_gstring(&packet, value));
398
 
 
399
 
        packet.offset = 0;
400
 
        g_string_assign_len(packet.data, C("012345\0"));
401
 
        g_assert_cmpint(0, ==, network_mysqld_proto_get_gstring(&packet, value));
402
 
        g_assert_cmpint(6, ==, value->len);
403
 
        g_assert_cmpstr("012345", ==, value->str);
404
 
 
405
 
        g_string_free(value, TRUE);
406
 
        g_string_free(packet.data, TRUE);
407
 
}
408
 
 
409
 
 
410
 
void test_mysqld_password(void) {
411
 
        GString *cleartext = g_string_new("123");
412
 
        GString *hashed_password = g_string_new(NULL);
413
 
        GString *double_hashed = g_string_new(NULL);
414
 
        GString *challenge = g_string_new(NULL);
415
 
        GString *response = g_string_new(NULL);
416
 
 
417
 
        network_mysqld_proto_password_hash(hashed_password, S(cleartext));
418
 
        network_mysqld_proto_password_hash(double_hashed, S(hashed_password));
419
 
 
420
 
        /* should be the same as SELECT SHA1("123"); */
421
 
        g_assert_cmpint(TRUE, ==, g_memeq(S(hashed_password), C("\x40\xbd\x00\x15\x63\x08\x5f\xc3\x51\x65\x32\x9e\xa1\xff\x5c\x5e\xcb\xdb\xbe\xef")));
422
 
        /* should be the same as SELECT PASSWORD("123"); */
423
 
        g_assert_cmpint(TRUE, ==, g_memeq(S(double_hashed), C("\x23\xAE\x80\x9D\xDA\xCA\xF9\x6A\xF0\xFD\x78\xED\x04\xB6\xA2\x65\xE0\x5A\xA2\x57")));
424
 
 
425
 
        g_string_assign_len(challenge, C("01234567890123456789"));
426
 
 
427
 
        network_mysqld_proto_password_scramble(response, 
428
 
                        S(challenge),
429
 
                        S(hashed_password));
430
 
        
431
 
        g_assert_cmpint(TRUE, ==, network_mysqld_proto_password_check(
432
 
                        S(challenge),
433
 
                        S(response),
434
 
                        S(double_hashed)));
435
 
 
436
 
        g_string_free(response, TRUE);
437
 
        g_string_free(challenge, TRUE);
438
 
        g_string_free(hashed_password, TRUE);
439
 
        g_string_free(double_hashed, TRUE);
440
 
        g_string_free(cleartext, TRUE);
441
 
}
442
 
 
443
368
int main(int argc, char **argv) {
444
369
        g_test_init(&argc, &argv, NULL);
445
370
        g_test_bug_base("http://bugs.mysql.com/");
448
373
        g_test_add_func("/core/mysqld-proto-lenenc-int", test_mysqld_proto_lenenc_int);
449
374
        g_test_add_func("/core/mysqld-proto-int", test_mysqld_proto_int);
450
375
        g_test_add_func("/core/mysqld-proto-gstring-len", test_mysqld_proto_gstring_len);
451
 
        g_test_add_func("/core/mysqld-proto-gstring", test_mysqld_proto_gstring);
452
376
 
453
377
        g_test_add_func("/core/mysqld-proto-binlog-event", test_mysqld_binlog_events);
454
 
        g_test_add_func("/core/mysqld-proto-password", test_mysqld_password);
455
378
 
456
379
        return g_test_run();
457
380
}