2
* Drizzle Client & Protocol Library
4
* Copyright (C) 2008 Eric Day (eday@oddments.org)
7
* Use and distribution licensed under the BSD license. See
8
* the COPYING file in this directory for full text.
13
* @brief Tests for drizzle_st Structures
16
#include <libdrizzle-1.0/t/common.h>
21
#define DRIZZLE_TEST_HOST "localhost"
22
#define DRIZZLE_TEST_PORT 12399
27
SERVER_STATE_HANDSHAKE_WRITE,
28
SERVER_STATE_HANDSHAKE_READ,
29
SERVER_STATE_HANDSHAKE_RESULT,
30
SERVER_STATE_COMMAND_BUFFER,
31
SERVER_STATE_RESULT_HEADER,
32
SERVER_STATE_RESULT_COLUMN_1,
33
SERVER_STATE_RESULT_COLUMN_2,
34
SERVER_STATE_RESULT_COLUMN_EOF,
35
SERVER_STATE_RESULT_ROW_1,
36
SERVER_STATE_RESULT_ROW_1_FIELD_1,
37
SERVER_STATE_RESULT_ROW_1_FIELD_2,
38
SERVER_STATE_RESULT_ROW_EOF,
45
drizzle_result_st result;
46
drizzle_column_st column;
47
drizzle_command_t command;
62
drizzle_result_st result;
65
static void _server(drizzle_con_st *con, server_state_st *state);
66
static void _client(drizzle_con_st *con, client_state_st *state);
73
drizzle_con_st listen_con;
74
drizzle_con_st client;
75
drizzle_con_st server;
77
bool server_accepted = false;
78
server_state_st server_state;
79
client_state_st client_state;
81
drizzle_test("drizzle_create");
82
if (drizzle_create(&drizzle) == NULL)
83
drizzle_test_error("returned NULL");
85
drizzle_test("drizzle_con_add_tcp_listen");
86
if (drizzle_con_add_tcp_listen(&drizzle, &listen_con, DRIZZLE_TEST_HOST,
88
DRIZZLE_CON_NONE) == NULL)
90
drizzle_test_error("returned NULL");
93
drizzle_test("drizzle_con_listen");
94
ret= drizzle_con_listen(&listen_con);
95
if (ret != DRIZZLE_RETURN_OK)
96
drizzle_test_error("returned %s (%d)", drizzle_error(&drizzle), ret);
98
drizzle_test("drizzle_con_add_tcp");
99
if (drizzle_con_add_tcp(&drizzle, &client, DRIZZLE_TEST_HOST,
100
DRIZZLE_TEST_PORT, NULL, NULL, NULL,
101
DRIZZLE_CON_NONE) == NULL)
103
drizzle_test_error("returned NULL");
106
drizzle_test("drizzle_add_options");
107
drizzle_add_options(&drizzle, DRIZZLE_NON_BLOCKING);
109
server_state.state= SERVER_STATE_START;
110
client_state.state= CLIENT_STATE_START;
114
if (!server_accepted)
116
drizzle_test("drizzle_con_accept");
117
(void)drizzle_con_accept(&drizzle, &server, &ret);
118
if (ret == DRIZZLE_RETURN_OK)
119
server_accepted = true;
120
else if (ret != DRIZZLE_RETURN_IO_WAIT)
121
drizzle_test_error("returned %s (%d)", drizzle_error(&drizzle), ret);
125
_server(&server, &server_state);
127
_client(&client, &client_state);
129
if (server_state.state == SERVER_STATE_DONE &&
130
client_state.state == CLIENT_STATE_DONE)
135
drizzle_test("drizzle_con_wait");
136
ret= drizzle_con_wait(&drizzle);
137
if (ret != DRIZZLE_RETURN_OK)
138
drizzle_test_error("returned %s (%d)", drizzle_error(&drizzle), ret);
143
drizzle_test("drizzle_con_free");
144
drizzle_con_free(&server);
147
drizzle_test("drizzle_con_free");
148
drizzle_con_free(&client);
150
drizzle_test("drizzle_con_free");
151
drizzle_con_free(&listen_con);
153
drizzle_test("drizzle_free");
154
drizzle_free(&drizzle);
159
static void _server(drizzle_con_st *con, server_state_st *state)
161
drizzle_return_t ret;
162
const drizzle_field_t const fields[2]=
164
(const drizzle_field_t)"test_field_1",
165
(const drizzle_field_t)"test_field_2"
167
const size_t field_sizes[2]= { 12, 12 };
171
case SERVER_STATE_START:
172
drizzle_con_set_protocol_version(con, 10);
173
drizzle_con_set_server_version(con, "test_version");
174
drizzle_con_set_thread_id(con, 1);
175
drizzle_con_set_scramble(con, (const uint8_t *)"ABCDEFGHIJKLMNOPQRST");
176
drizzle_con_set_capabilities(con, DRIZZLE_CAPABILITIES_NONE);
177
drizzle_con_set_charset(con, 8);
178
drizzle_con_set_status(con, DRIZZLE_CON_STATUS_NONE);
179
drizzle_con_set_max_packet_size(con, DRIZZLE_MAX_PACKET_SIZE);
181
case SERVER_STATE_HANDSHAKE_WRITE:
182
drizzle_test("drizzle_handshake_server_write");
183
ret= drizzle_handshake_server_write(con);
184
if (ret == DRIZZLE_RETURN_IO_WAIT)
186
state->state = SERVER_STATE_HANDSHAKE_WRITE;
189
else if (ret != DRIZZLE_RETURN_OK)
190
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
192
case SERVER_STATE_HANDSHAKE_READ:
193
drizzle_test("drizzle_handshake_client_read");
194
ret= drizzle_handshake_client_read(con);
195
if (ret == DRIZZLE_RETURN_IO_WAIT)
197
state->state = SERVER_STATE_HANDSHAKE_READ;
200
else if (ret != DRIZZLE_RETURN_OK)
201
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
203
drizzle_test("drizzle_result_create");
204
if (drizzle_result_create(con, &state->result) == NULL)
205
drizzle_test_error("returned %s", drizzle_con_error(con));
207
case SERVER_STATE_HANDSHAKE_RESULT:
208
drizzle_test("drizzle_handshake_result_write");
209
ret= drizzle_result_write(con, &state->result, true);
210
if (ret == DRIZZLE_RETURN_IO_WAIT)
212
state->state = SERVER_STATE_HANDSHAKE_RESULT;
215
else if (ret != DRIZZLE_RETURN_OK)
216
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
218
drizzle_result_free(&state->result);
220
case SERVER_STATE_COMMAND_BUFFER:
221
drizzle_test("drizzle_con_command_buffer");
222
state->data= drizzle_con_command_buffer(con, &state->command, &state->total,
224
if (ret == DRIZZLE_RETURN_IO_WAIT)
226
state->state = SERVER_STATE_COMMAND_BUFFER;
229
else if (ret != DRIZZLE_RETURN_OK)
230
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
232
if (state->command != DRIZZLE_COMMAND_QUERY)
233
drizzle_test_error("command was not a query");
235
if (state->total != 6 && !memcmp(state->data, "SELECT", 6))
236
drizzle_test_error("query doesn't match");
238
if (state->data != NULL)
244
drizzle_test("drizzle_result_create");
245
if (drizzle_result_create(con, &state->result) == NULL)
246
drizzle_test_error("returned %s", drizzle_con_error(con));
248
drizzle_result_set_column_count(&state->result, 2);
250
case SERVER_STATE_RESULT_HEADER:
251
drizzle_test("drizzle_handshake_result_write");
252
ret= drizzle_result_write(con, &state->result, false);
253
if (ret == DRIZZLE_RETURN_IO_WAIT)
255
state->state = SERVER_STATE_RESULT_HEADER;
258
else if (ret != DRIZZLE_RETURN_OK)
259
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
261
drizzle_test("drizzle_column_create");
262
if (drizzle_column_create(&state->result, &state->column) == NULL)
263
drizzle_test_error("returned %s", drizzle_con_error(con));
265
drizzle_column_set_catalog(&state->column, "test_catalog");
266
drizzle_column_set_db(&state->column, "test_db");
267
drizzle_column_set_table(&state->column, "test_table");
268
drizzle_column_set_orig_table(&state->column, "test_orig_table");
269
drizzle_column_set_name(&state->column, "test_column_1");
270
drizzle_column_set_orig_name(&state->column, "test_orig_column_1");
271
drizzle_column_set_charset(&state->column, 8);
272
drizzle_column_set_size(&state->column, 32);
273
drizzle_column_set_type(&state->column, DRIZZLE_COLUMN_TYPE_VARCHAR);
274
drizzle_column_set_flags(&state->column, DRIZZLE_COLUMN_FLAGS_NONE);
276
case SERVER_STATE_RESULT_COLUMN_1:
277
drizzle_test("drizzle_column_write");
278
ret= drizzle_column_write(&state->result, &state->column);
279
if (ret == DRIZZLE_RETURN_IO_WAIT)
281
state->state = SERVER_STATE_RESULT_COLUMN_1;
284
else if (ret != DRIZZLE_RETURN_OK)
285
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
287
drizzle_column_set_name(&state->column, "test_column_2");
288
drizzle_column_set_orig_name(&state->column, "test_orig_column_2");
290
case SERVER_STATE_RESULT_COLUMN_2:
291
drizzle_test("drizzle_column_write");
292
ret= drizzle_column_write(&state->result, &state->column);
293
if (ret == DRIZZLE_RETURN_IO_WAIT)
295
state->state = SERVER_STATE_RESULT_COLUMN_2;
298
else if (ret != DRIZZLE_RETURN_OK)
299
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
301
drizzle_test("drizzle_column_free");
302
drizzle_column_free(&state->column);
304
drizzle_result_set_eof(&state->result, true);
306
case SERVER_STATE_RESULT_COLUMN_EOF:
307
drizzle_test("drizzle_handshake_result_write");
308
ret= drizzle_result_write(con, &state->result, false);
309
if (ret == DRIZZLE_RETURN_IO_WAIT)
311
state->state = SERVER_STATE_RESULT_COLUMN_EOF;
314
else if (ret != DRIZZLE_RETURN_OK)
315
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
317
drizzle_result_calc_row_size(&state->result, fields, field_sizes);
319
case SERVER_STATE_RESULT_ROW_1:
320
ret= drizzle_row_write(&state->result);
322
case SERVER_STATE_RESULT_ROW_1_FIELD_1:
323
ret= drizzle_field_write(&state->result, fields[0], field_sizes[0],
326
case SERVER_STATE_RESULT_ROW_1_FIELD_2:
327
ret= drizzle_field_write(&state->result, fields[1], field_sizes[1],
330
case SERVER_STATE_RESULT_ROW_EOF:
331
drizzle_test("drizzle_handshake_result_write");
332
ret= drizzle_result_write(con, &state->result, true);
333
if (ret == DRIZZLE_RETURN_IO_WAIT)
335
state->state = SERVER_STATE_RESULT_ROW_EOF;
338
else if (ret != DRIZZLE_RETURN_OK)
339
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
341
drizzle_result_free(&state->result);
343
state->state = SERVER_STATE_DONE;
345
case SERVER_STATE_DONE:
349
drizzle_test_error("invalid server state");
353
static void _client(drizzle_con_st *con, client_state_st *state)
355
drizzle_return_t ret;
356
drizzle_column_st *column;
362
case CLIENT_STATE_START:
363
drizzle_test("drizzle_query_str");
364
(void)drizzle_query_str(con, &state->result, "SELECT", &ret);
365
if (ret == DRIZZLE_RETURN_IO_WAIT)
367
state->state = CLIENT_STATE_START;
370
else if (ret != DRIZZLE_RETURN_OK)
371
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
373
case CLIENT_STATE_RESULT:
374
drizzle_test("drizzle_result_buffer");
375
ret = drizzle_result_buffer(&state->result);
376
if (ret == DRIZZLE_RETURN_IO_WAIT)
378
state->state = CLIENT_STATE_RESULT;
381
else if (ret != DRIZZLE_RETURN_OK)
382
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
384
drizzle_test("drizzle_con_protocol_version");
385
if (drizzle_con_protocol_version(con) != 10)
386
drizzle_test_error("no match");
388
drizzle_test("drizzle_con_server_version");
389
if (strcmp(drizzle_con_server_version(con), "test_version"))
390
drizzle_test_error("no match");
392
drizzle_test("drizzle_con_thread_id");
393
if (drizzle_con_thread_id(con) != 1)
394
drizzle_test_error("no match");
396
drizzle_test("drizzle_con_scramble");
397
if (memcmp(drizzle_con_scramble(con), "ABCDEFGHIJKLMNOPQRST",
398
DRIZZLE_MAX_SCRAMBLE_SIZE))
400
drizzle_test_error("no match");
403
/* We default to MySQL protocol right now, which sets this flag. */
404
drizzle_test("drizzle_con_capabilities");
405
if (drizzle_con_capabilities(con) != DRIZZLE_CAPABILITIES_PROTOCOL_41)
406
drizzle_test_error("no match");
408
drizzle_test("drizzle_con_charset");
409
if (drizzle_con_charset(con) != 8)
410
drizzle_test_error("no match");
412
drizzle_test("drizzle_con_status");
413
if (drizzle_con_status(con) != DRIZZLE_CON_STATUS_NONE)
414
drizzle_test_error("no match");
416
drizzle_test("drizzle_con_packet_size");
417
if (drizzle_con_max_packet_size(con) != DRIZZLE_MAX_PACKET_SIZE)
418
drizzle_test_error("no match");
420
drizzle_test("drizzle_column_next");
421
column= drizzle_column_next(&state->result);
423
drizzle_test_error("column not found");
425
drizzle_test("drizzle_column_drizzle_result");
426
if (drizzle_column_drizzle_result(column) != &state->result)
427
drizzle_test_error("no match");
429
drizzle_test("drizzle_column_catalog");
430
if (strcmp(drizzle_column_catalog(column), "test_catalog"))
431
drizzle_test_error("no match");
433
drizzle_test("drizzle_column_db");
434
if (strcmp(drizzle_column_db(column), "test_db"))
435
drizzle_test_error("no match");
437
drizzle_test("drizzle_column_table");
438
if (strcmp(drizzle_column_table(column), "test_table"))
439
drizzle_test_error("no match");
441
drizzle_test("drizzle_column_orig_table");
442
if (strcmp(drizzle_column_orig_table(column), "test_orig_table"))
443
drizzle_test_error("no match");
445
drizzle_test("drizzle_column_name");
446
if (strcmp(drizzle_column_name(column), "test_column_1"))
447
drizzle_test_error("no match");
449
drizzle_test("drizzle_column_orig_name");
450
if (strcmp(drizzle_column_orig_name(column), "test_orig_column_1"))
451
drizzle_test_error("no match");
453
drizzle_test("drizzle_column_charset");
454
if (drizzle_column_charset(column) != 8)
455
drizzle_test_error("no match");
457
drizzle_test("drizzle_column_size");
458
if (drizzle_column_size(column) != 32)
459
drizzle_test_error("no match");
461
drizzle_test("drizzle_column_max_size");
462
if (drizzle_column_max_size(column) != 12)
463
drizzle_test_error("no match");
465
drizzle_test("drizzle_column_type_drizzle");
466
if (drizzle_column_type_drizzle(column) != DRIZZLE_COLUMN_TYPE_DRIZZLE_VARCHAR)
467
drizzle_test_error("no match");
469
drizzle_test("drizzle_column_flags");
470
if (drizzle_column_flags(column) != DRIZZLE_COLUMN_FLAGS_NONE)
471
drizzle_test_error("no match");
473
drizzle_test("drizzle_column_next");
474
column= drizzle_column_next(&state->result);
476
drizzle_test_error("column not found");
478
drizzle_test("drizzle_column_name");
479
if (strcmp(drizzle_column_name(column), "test_column_2"))
480
drizzle_test_error("no match");
482
drizzle_test("drizzle_column_next");
483
column= drizzle_column_next(&state->result);
485
drizzle_test_error("column found");
487
drizzle_test("drizzle_column_prev");
488
column= drizzle_column_prev(&state->result);
490
drizzle_test_error("column not found");
492
drizzle_test("drizzle_column_name");
493
if (strcmp(drizzle_column_name(column), "test_column_2"))
494
drizzle_test_error("no match");
496
drizzle_test("drizzle_row_next");
497
row= drizzle_row_next(&state->result);
499
drizzle_test_error("row not found");
501
if (strcmp(row[0], "test_field_1") || strcmp(row[1], "test_field_2"))
502
drizzle_test_error("no match");
504
drizzle_test("drizzle_row_field_sizes");
505
field_sizes= drizzle_row_field_sizes(&state->result);
506
if (field_sizes[0] != 12 || field_sizes[1] != 12)
507
drizzle_test_error("no match");
509
drizzle_test("drizzle_row_prev");
510
row = drizzle_row_prev(&state->result);
512
drizzle_test_error("row not found");
514
if (strcmp(row[0], "test_field_1") || strcmp(row[1], "test_field_2"))
515
drizzle_test_error("no match");
517
drizzle_test("drizzle_row_next");
518
row = drizzle_row_next(&state->result);
520
drizzle_test("drizzle_row_index");
521
row = drizzle_row_index(&state->result, 0);
523
drizzle_test_error("row not found");
525
if (strcmp(row[0], "test_field_1") || strcmp(row[1], "test_field_2"))
526
drizzle_test_error("no match");
528
drizzle_test("drizzle_row_index");
529
row = drizzle_row_index(&state->result, 1);
531
drizzle_test_error("row found");
533
drizzle_test("drizzle_result_free");
534
drizzle_result_free(&state->result);
536
state->state = CLIENT_STATE_DONE;
538
case CLIENT_STATE_DONE:
542
drizzle_test_error("invalid client state");