2
Copyright (c) 2007, 2009, Oracle and/or its affiliates. All rights reserved.
2
Copyright (C) 2007-2008 MySQL AB, 2008 Sun Microsystems, Inc
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
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.
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.
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
15
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
46
43
* how to handle > 16M ?
48
45
void test_mysqld_proto_header(void) {
50
46
unsigned char header[4];
51
guint32 length = 1256;
54
h.str = (char *)header;
55
h.len = sizeof(header);
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));
49
g_assert(0 == network_mysqld_proto_set_header(header, length, 0));
50
g_assert(length == network_mysqld_proto_get_header(header));
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);
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);
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);
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);
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);
181
171
g_string_truncate(packet.data, 0);
182
172
g_assert(0 == network_mysqld_proto_append_int64(packet.data, length));
183
173
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")));
174
g_assert(0 == memcmp(packet.data->str, C("\xfa\xff\xff\xff\xff\x00\x00\x00")));
185
175
g_assert_cmpint(0, ==, network_mysqld_proto_get_int64(&packet, &value64));
186
176
g_assert_cmpuint(length, ==, value64);
191
181
g_assert_cmpint(0, ==, network_mysqld_proto_append_int8(packet.data, 0xa5));
192
182
g_assert_cmpint(packet.data->len, ==, 1);
193
183
g_assert_cmpint(packet.offset, ==, 0);
194
g_assert_cmpint(TRUE, ==, g_memeq(S(packet.data), C("\xa5")));
184
g_assert_cmpint(0, ==, memcmp(packet.data->str, C("\xa5")));
196
186
g_assert_cmpint(0, ==, network_mysqld_proto_append_int8(packet.data, 0x5a));
197
187
g_assert_cmpint(packet.data->len, ==, 2);
198
188
g_assert_cmpint(packet.offset, ==, 0);
199
g_assert_cmpint(TRUE, ==, g_memeq(S(packet.data), C("\xa5\x5a")));
189
g_assert_cmpint(0, ==, memcmp(packet.data->str, C("\xa5\x5a")));
200
190
g_assert_cmpint(0, ==, network_mysqld_proto_get_int8(&packet, &value8));
201
191
g_assert_cmpint(packet.offset, ==, 1);
202
192
g_assert_cmpint(0xa5, ==, value8);
381
371
g_string_free(packet.data, TRUE);
384
void test_mysqld_proto_gstring(void) {
385
network_packet packet;
386
GString *value = g_string_new(NULL);
388
packet.data = g_string_new(NULL);
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);
396
g_string_assign_len(packet.data, C("012345")); /* no trailing \0 */
397
g_assert_cmpint(0, !=, network_mysqld_proto_get_gstring(&packet, value));
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);
405
g_string_free(value, TRUE);
406
g_string_free(packet.data, TRUE);
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);
417
network_mysqld_proto_password_hash(hashed_password, S(cleartext));
418
network_mysqld_proto_password_hash(double_hashed, S(hashed_password));
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")));
425
g_string_assign_len(challenge, C("01234567890123456789"));
427
network_mysqld_proto_password_scramble(response,
431
g_assert_cmpint(TRUE, ==, network_mysqld_proto_password_check(
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);
443
374
int main(int argc, char **argv) {
444
375
g_test_init(&argc, &argv, NULL);
445
376
g_test_bug_base("http://bugs.mysql.com/");
448
379
g_test_add_func("/core/mysqld-proto-lenenc-int", test_mysqld_proto_lenenc_int);
449
380
g_test_add_func("/core/mysqld-proto-int", test_mysqld_proto_int);
450
381
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);
453
383
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);
456
385
return g_test_run();