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
18
#include "tests/common.h"
20
#define DRIZZLE_TEST_HOST "127.0.0.1"
21
#define DRIZZLE_TEST_PORT 12399
26
SERVER_STATE_HANDSHAKE_WRITE,
27
SERVER_STATE_HANDSHAKE_READ,
28
SERVER_STATE_HANDSHAKE_RESULT,
29
SERVER_STATE_COMMAND_BUFFER,
30
SERVER_STATE_RESULT_HEADER,
31
SERVER_STATE_RESULT_COLUMN_1,
32
SERVER_STATE_RESULT_COLUMN_2,
33
SERVER_STATE_RESULT_COLUMN_EOF,
34
SERVER_STATE_RESULT_ROW_1,
35
SERVER_STATE_RESULT_ROW_1_FIELD_1,
36
SERVER_STATE_RESULT_ROW_1_FIELD_2,
37
SERVER_STATE_RESULT_ROW_EOF,
44
drizzle_result_st result;
45
drizzle_column_st column;
46
drizzle_command_t command;
61
drizzle_result_st result;
64
static void _server(drizzle_con_st *con, server_state_st *state);
65
static void _client(drizzle_con_st *con, client_state_st *state);
70
drizzle_con_st listen_con;
71
drizzle_con_st client;
72
drizzle_con_st server;
74
bool server_accepted = false;
75
server_state_st server_state;
76
client_state_st client_state;
78
drizzle_test("drizzle_create");
79
if (drizzle_create(&drizzle) == NULL)
80
drizzle_test_error("returned NULL");
82
drizzle_test("drizzle_con_add_tcp_listen");
83
if (drizzle_con_add_tcp_listen(&drizzle, &listen_con, DRIZZLE_TEST_HOST,
85
DRIZZLE_CON_NONE) == NULL)
87
drizzle_test_error("returned NULL");
90
drizzle_test("drizzle_con_listen");
91
ret= drizzle_con_listen(&listen_con);
92
if (ret != DRIZZLE_RETURN_OK)
93
drizzle_test_error("returned %s (%d)", drizzle_error(&drizzle), ret);
95
drizzle_test("drizzle_con_add_tcp");
96
if (drizzle_con_add_tcp(&drizzle, &client, DRIZZLE_TEST_HOST,
97
DRIZZLE_TEST_PORT, NULL, NULL, NULL,
98
DRIZZLE_CON_NONE) == NULL)
100
drizzle_test_error("returned NULL");
103
drizzle_test("drizzle_add_options");
104
drizzle_add_options(&drizzle, DRIZZLE_NON_BLOCKING);
106
server_state.state= SERVER_STATE_START;
107
client_state.state= CLIENT_STATE_START;
111
if (!server_accepted)
113
drizzle_test("drizzle_con_accept");
114
(void)drizzle_con_accept(&drizzle, &server, &ret);
115
if (ret == DRIZZLE_RETURN_OK)
116
server_accepted = true;
117
else if (ret != DRIZZLE_RETURN_IO_WAIT)
118
drizzle_test_error("returned %s (%d)", drizzle_error(&drizzle), ret);
122
_server(&server, &server_state);
124
_client(&client, &client_state);
126
if (server_state.state == SERVER_STATE_DONE &&
127
client_state.state == CLIENT_STATE_DONE)
132
drizzle_test("drizzle_con_wait");
133
ret= drizzle_con_wait(&drizzle);
134
if (ret != DRIZZLE_RETURN_OK)
135
drizzle_test_error("returned %s (%d)", drizzle_error(&drizzle), ret);
140
drizzle_test("drizzle_con_free");
141
drizzle_con_free(&server);
144
drizzle_test("drizzle_con_free");
145
drizzle_con_free(&client);
147
drizzle_test("drizzle_con_free");
148
drizzle_con_free(&listen_con);
150
drizzle_test("drizzle_free");
151
drizzle_free(&drizzle);
156
static void _server(drizzle_con_st *con, server_state_st *state)
158
drizzle_return_t ret;
159
const drizzle_field_t const fields[2]=
161
(const drizzle_field_t)"test_field_1",
162
(const drizzle_field_t)"test_field_2"
164
const size_t field_sizes[2]= { 12, 12 };
168
case SERVER_STATE_START:
169
drizzle_con_set_protocol_version(con, 10);
170
drizzle_con_set_server_version(con, "test_version");
171
drizzle_con_set_thread_id(con, 1);
172
drizzle_con_set_scramble(con, (const uint8_t *)"ABCDEFGHIJKLMNOPQRST");
173
drizzle_con_set_capabilities(con, DRIZZLE_CAPABILITIES_NONE);
174
drizzle_con_set_charset(con, 8);
175
drizzle_con_set_status(con, DRIZZLE_CON_STATUS_NONE);
176
drizzle_con_set_max_packet_size(con, DRIZZLE_MAX_PACKET_SIZE);
178
case SERVER_STATE_HANDSHAKE_WRITE:
179
drizzle_test("drizzle_handshake_server_write");
180
ret= drizzle_handshake_server_write(con);
181
if (ret == DRIZZLE_RETURN_IO_WAIT)
183
state->state = SERVER_STATE_HANDSHAKE_WRITE;
186
else if (ret != DRIZZLE_RETURN_OK)
187
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
189
case SERVER_STATE_HANDSHAKE_READ:
190
drizzle_test("drizzle_handshake_client_read");
191
ret= drizzle_handshake_client_read(con);
192
if (ret == DRIZZLE_RETURN_IO_WAIT)
194
state->state = SERVER_STATE_HANDSHAKE_READ;
197
else if (ret != DRIZZLE_RETURN_OK)
198
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
200
drizzle_test("drizzle_result_create");
201
if (drizzle_result_create(con, &state->result) == NULL)
202
drizzle_test_error("returned %s", drizzle_con_error(con));
204
case SERVER_STATE_HANDSHAKE_RESULT:
205
drizzle_test("drizzle_handshake_result_write");
206
ret= drizzle_result_write(con, &state->result, true);
207
if (ret == DRIZZLE_RETURN_IO_WAIT)
209
state->state = SERVER_STATE_HANDSHAKE_RESULT;
212
else if (ret != DRIZZLE_RETURN_OK)
213
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
215
drizzle_result_free(&state->result);
217
case SERVER_STATE_COMMAND_BUFFER:
218
drizzle_test("drizzle_con_command_buffer");
219
state->data= drizzle_con_command_buffer(con, &state->command, &state->total,
221
if (ret == DRIZZLE_RETURN_IO_WAIT)
223
state->state = SERVER_STATE_COMMAND_BUFFER;
226
else if (ret != DRIZZLE_RETURN_OK)
227
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
229
if (state->command != DRIZZLE_COMMAND_QUERY)
230
drizzle_test_error("command was not a query");
232
if (state->total != 6 && !memcmp(state->data, "SELECT", 6))
233
drizzle_test_error("query doesn't match");
235
if (state->data != NULL)
241
drizzle_test("drizzle_result_create");
242
if (drizzle_result_create(con, &state->result) == NULL)
243
drizzle_test_error("returned %s", drizzle_con_error(con));
245
drizzle_result_set_column_count(&state->result, 2);
247
case SERVER_STATE_RESULT_HEADER:
248
drizzle_test("drizzle_handshake_result_write");
249
ret= drizzle_result_write(con, &state->result, false);
250
if (ret == DRIZZLE_RETURN_IO_WAIT)
252
state->state = SERVER_STATE_RESULT_HEADER;
255
else if (ret != DRIZZLE_RETURN_OK)
256
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
258
drizzle_test("drizzle_column_create");
259
if (drizzle_column_create(&state->result, &state->column) == NULL)
260
drizzle_test_error("returned %s", drizzle_con_error(con));
262
drizzle_column_set_catalog(&state->column, "test_catalog");
263
drizzle_column_set_db(&state->column, "test_db");
264
drizzle_column_set_table(&state->column, "test_table");
265
drizzle_column_set_orig_table(&state->column, "test_orig_table");
266
drizzle_column_set_name(&state->column, "test_column_1");
267
drizzle_column_set_orig_name(&state->column, "test_orig_column_1");
268
drizzle_column_set_charset(&state->column, 8);
269
drizzle_column_set_size(&state->column, 32);
270
drizzle_column_set_type(&state->column, DRIZZLE_COLUMN_TYPE_VARCHAR);
271
drizzle_column_set_flags(&state->column, DRIZZLE_COLUMN_FLAGS_NONE);
273
case SERVER_STATE_RESULT_COLUMN_1:
274
drizzle_test("drizzle_column_write");
275
ret= drizzle_column_write(&state->result, &state->column);
276
if (ret == DRIZZLE_RETURN_IO_WAIT)
278
state->state = SERVER_STATE_RESULT_COLUMN_1;
281
else if (ret != DRIZZLE_RETURN_OK)
282
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
284
drizzle_column_set_name(&state->column, "test_column_2");
285
drizzle_column_set_orig_name(&state->column, "test_orig_column_2");
287
case SERVER_STATE_RESULT_COLUMN_2:
288
drizzle_test("drizzle_column_write");
289
ret= drizzle_column_write(&state->result, &state->column);
290
if (ret == DRIZZLE_RETURN_IO_WAIT)
292
state->state = SERVER_STATE_RESULT_COLUMN_2;
295
else if (ret != DRIZZLE_RETURN_OK)
296
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
298
drizzle_test("drizzle_column_free");
299
drizzle_column_free(&state->column);
301
drizzle_result_set_eof(&state->result, true);
303
case SERVER_STATE_RESULT_COLUMN_EOF:
304
drizzle_test("drizzle_handshake_result_write");
305
ret= drizzle_result_write(con, &state->result, false);
306
if (ret == DRIZZLE_RETURN_IO_WAIT)
308
state->state = SERVER_STATE_RESULT_COLUMN_EOF;
311
else if (ret != DRIZZLE_RETURN_OK)
312
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
314
drizzle_result_calc_row_size(&state->result, fields, field_sizes);
316
case SERVER_STATE_RESULT_ROW_1:
317
ret= drizzle_row_write(&state->result);
319
case SERVER_STATE_RESULT_ROW_1_FIELD_1:
320
ret= drizzle_field_write(&state->result, fields[0], field_sizes[0],
323
case SERVER_STATE_RESULT_ROW_1_FIELD_2:
324
ret= drizzle_field_write(&state->result, fields[1], field_sizes[1],
327
case SERVER_STATE_RESULT_ROW_EOF:
328
drizzle_test("drizzle_handshake_result_write");
329
ret= drizzle_result_write(con, &state->result, true);
330
if (ret == DRIZZLE_RETURN_IO_WAIT)
332
state->state = SERVER_STATE_RESULT_ROW_EOF;
335
else if (ret != DRIZZLE_RETURN_OK)
336
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
338
drizzle_result_free(&state->result);
340
state->state = SERVER_STATE_DONE;
342
case SERVER_STATE_DONE:
346
drizzle_test_error("invalid server state");
350
static void _client(drizzle_con_st *con, client_state_st *state)
352
drizzle_return_t ret;
353
drizzle_column_st *column;
359
case CLIENT_STATE_START:
360
drizzle_test("drizzle_query_str");
361
(void)drizzle_query_str(con, &state->result, "SELECT", &ret);
362
if (ret == DRIZZLE_RETURN_IO_WAIT)
364
state->state = CLIENT_STATE_START;
367
else if (ret != DRIZZLE_RETURN_OK)
368
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
370
case CLIENT_STATE_RESULT:
371
drizzle_test("drizzle_result_buffer");
372
ret = drizzle_result_buffer(&state->result);
373
if (ret == DRIZZLE_RETURN_IO_WAIT)
375
state->state = CLIENT_STATE_RESULT;
378
else if (ret != DRIZZLE_RETURN_OK)
379
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
381
drizzle_test("drizzle_con_protocol_version");
382
if (drizzle_con_protocol_version(con) != 10)
383
drizzle_test_error("no match");
385
drizzle_test("drizzle_con_server_version");
386
if (strcmp(drizzle_con_server_version(con), "test_version"))
387
drizzle_test_error("no match");
389
drizzle_test("drizzle_con_thread_id");
390
if (drizzle_con_thread_id(con) != 1)
391
drizzle_test_error("no match");
393
drizzle_test("drizzle_con_scramble");
394
if (memcmp(drizzle_con_scramble(con), "ABCDEFGHIJKLMNOPQRST",
395
DRIZZLE_MAX_SCRAMBLE_SIZE))
397
drizzle_test_error("no match");
400
/* We default to MySQL protocol right now, which sets this flag. */
401
drizzle_test("drizzle_con_capabilities");
402
if (drizzle_con_capabilities(con) != DRIZZLE_CAPABILITIES_PROTOCOL_41)
403
drizzle_test_error("no match");
405
drizzle_test("drizzle_con_charset");
406
if (drizzle_con_charset(con) != 8)
407
drizzle_test_error("no match");
409
drizzle_test("drizzle_con_status");
410
if (drizzle_con_status(con) != DRIZZLE_CON_STATUS_NONE)
411
drizzle_test_error("no match");
413
drizzle_test("drizzle_con_packet_size");
414
if (drizzle_con_max_packet_size(con) != DRIZZLE_MAX_PACKET_SIZE)
415
drizzle_test_error("no match");
417
drizzle_test("drizzle_column_next");
418
column= drizzle_column_next(&state->result);
420
drizzle_test_error("column not found");
422
drizzle_test("drizzle_column_drizzle_result");
423
if (drizzle_column_drizzle_result(column) != &state->result)
424
drizzle_test_error("no match");
426
drizzle_test("drizzle_column_catalog");
427
if (strcmp(drizzle_column_catalog(column), "test_catalog"))
428
drizzle_test_error("no match");
430
drizzle_test("drizzle_column_db");
431
if (strcmp(drizzle_column_db(column), "test_db"))
432
drizzle_test_error("no match");
434
drizzle_test("drizzle_column_table");
435
if (strcmp(drizzle_column_table(column), "test_table"))
436
drizzle_test_error("no match");
438
drizzle_test("drizzle_column_orig_table");
439
if (strcmp(drizzle_column_orig_table(column), "test_orig_table"))
440
drizzle_test_error("no match");
442
drizzle_test("drizzle_column_name");
443
if (strcmp(drizzle_column_name(column), "test_column_1"))
444
drizzle_test_error("no match");
446
drizzle_test("drizzle_column_orig_name");
447
if (strcmp(drizzle_column_orig_name(column), "test_orig_column_1"))
448
drizzle_test_error("no match");
450
drizzle_test("drizzle_column_charset");
451
if (drizzle_column_charset(column) != 8)
452
drizzle_test_error("no match");
454
drizzle_test("drizzle_column_size");
455
if (drizzle_column_size(column) != 32)
456
drizzle_test_error("no match");
458
drizzle_test("drizzle_column_max_size");
459
if (drizzle_column_max_size(column) != 12)
460
drizzle_test_error("no match");
462
drizzle_test("drizzle_column_type_drizzle");
463
if (drizzle_column_type_drizzle(column) != DRIZZLE_COLUMN_TYPE_DRIZZLE_VARCHAR)
464
drizzle_test_error("no match");
466
drizzle_test("drizzle_column_flags");
467
if (drizzle_column_flags(column) != DRIZZLE_COLUMN_FLAGS_NONE)
468
drizzle_test_error("no match");
470
drizzle_test("drizzle_column_next");
471
column= drizzle_column_next(&state->result);
473
drizzle_test_error("column not found");
475
drizzle_test("drizzle_column_name");
476
if (strcmp(drizzle_column_name(column), "test_column_2"))
477
drizzle_test_error("no match");
479
drizzle_test("drizzle_column_next");
480
column= drizzle_column_next(&state->result);
482
drizzle_test_error("column found");
484
drizzle_test("drizzle_column_prev");
485
column= drizzle_column_prev(&state->result);
487
drizzle_test_error("column not found");
489
drizzle_test("drizzle_column_name");
490
if (strcmp(drizzle_column_name(column), "test_column_2"))
491
drizzle_test_error("no match");
493
drizzle_test("drizzle_row_next");
494
row= drizzle_row_next(&state->result);
496
drizzle_test_error("row not found");
498
if (strcmp(row[0], "test_field_1") || strcmp(row[1], "test_field_2"))
499
drizzle_test_error("no match");
501
drizzle_test("drizzle_row_field_sizes");
502
field_sizes= drizzle_row_field_sizes(&state->result);
503
if (field_sizes[0] != 12 || field_sizes[1] != 12)
504
drizzle_test_error("no match");
506
drizzle_test("drizzle_row_prev");
507
row = drizzle_row_prev(&state->result);
509
drizzle_test_error("row not found");
511
if (strcmp(row[0], "test_field_1") || strcmp(row[1], "test_field_2"))
512
drizzle_test_error("no match");
514
drizzle_test("drizzle_row_next");
515
row = drizzle_row_next(&state->result);
517
drizzle_test("drizzle_row_index");
518
row = drizzle_row_index(&state->result, 0);
520
drizzle_test_error("row not found");
522
if (strcmp(row[0], "test_field_1") || strcmp(row[1], "test_field_2"))
523
drizzle_test_error("no match");
525
drizzle_test("drizzle_row_index");
526
row = drizzle_row_index(&state->result, 1);
528
drizzle_test_error("row found");
530
drizzle_test("drizzle_result_free");
531
drizzle_result_free(&state->result);
533
state->state = CLIENT_STATE_DONE;
535
case CLIENT_STATE_DONE:
539
drizzle_test_error("invalid client state");