2
* Drizzle Client & Protocol Library
4
* Copyright (C) 2008 Eric Day (eday@oddments.org)
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions are
11
* * Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
14
* * Redistributions in binary form must reproduce the above
15
* copyright notice, this list of conditions and the following disclaimer
16
* in the documentation and/or other materials provided with the
19
* * The names of its contributors may not be used to endorse or
20
* promote products derived from this software without specific prior
23
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
* @brief Tests for drizzle_st Structures
41
#include <libdrizzle-1.0/t/common.h>
46
#define DRIZZLE_TEST_HOST "localhost"
47
#define DRIZZLE_TEST_PORT 12399
52
SERVER_STATE_HANDSHAKE_WRITE,
53
SERVER_STATE_HANDSHAKE_READ,
54
SERVER_STATE_HANDSHAKE_RESULT,
55
SERVER_STATE_COMMAND_BUFFER,
56
SERVER_STATE_RESULT_HEADER,
57
SERVER_STATE_RESULT_COLUMN_1,
58
SERVER_STATE_RESULT_COLUMN_2,
59
SERVER_STATE_RESULT_COLUMN_EOF,
60
SERVER_STATE_RESULT_ROW_1,
61
SERVER_STATE_RESULT_ROW_1_FIELD_1,
62
SERVER_STATE_RESULT_ROW_1_FIELD_2,
63
SERVER_STATE_RESULT_ROW_EOF,
70
drizzle_result_st result;
71
drizzle_column_st column;
72
drizzle_command_t command;
87
drizzle_result_st result;
90
static void _server(drizzle_con_st *con, server_state_st *state);
91
static void _client(drizzle_con_st *con, client_state_st *state);
98
drizzle_con_st listen_con;
99
drizzle_con_st client;
100
drizzle_con_st server;
101
drizzle_return_t ret;
102
bool server_accepted = false;
103
server_state_st server_state;
104
client_state_st client_state;
106
drizzle_test("drizzle_create");
107
if (drizzle_create(&drizzle) == NULL)
108
drizzle_test_error("returned NULL");
110
drizzle_test("drizzle_con_add_tcp_listen");
111
if (drizzle_con_add_tcp_listen(&drizzle, &listen_con, DRIZZLE_TEST_HOST,
112
DRIZZLE_TEST_PORT, 1,
113
DRIZZLE_CON_NONE) == NULL)
115
drizzle_test_error("returned NULL");
118
drizzle_test("drizzle_con_listen");
119
ret= drizzle_con_listen(&listen_con);
120
if (ret != DRIZZLE_RETURN_OK)
121
drizzle_test_error("returned %s (%d)", drizzle_error(&drizzle), ret);
123
drizzle_test("drizzle_con_add_tcp");
124
if (drizzle_con_add_tcp(&drizzle, &client, DRIZZLE_TEST_HOST,
125
DRIZZLE_TEST_PORT, NULL, NULL, NULL,
126
DRIZZLE_CON_NONE) == NULL)
128
drizzle_test_error("returned NULL");
131
drizzle_test("drizzle_add_options");
132
drizzle_add_options(&drizzle, DRIZZLE_NON_BLOCKING);
134
server_state.state= SERVER_STATE_START;
135
client_state.state= CLIENT_STATE_START;
139
if (!server_accepted)
141
drizzle_test("drizzle_con_accept");
142
(void)drizzle_con_accept(&drizzle, &server, &ret);
143
if (ret == DRIZZLE_RETURN_OK)
144
server_accepted = true;
145
else if (ret != DRIZZLE_RETURN_IO_WAIT)
146
drizzle_test_error("returned %s (%d)", drizzle_error(&drizzle), ret);
150
_server(&server, &server_state);
152
_client(&client, &client_state);
154
if (server_state.state == SERVER_STATE_DONE &&
155
client_state.state == CLIENT_STATE_DONE)
160
drizzle_test("drizzle_con_wait");
161
ret= drizzle_con_wait(&drizzle);
162
if (ret != DRIZZLE_RETURN_OK)
163
drizzle_test_error("returned %s (%d)", drizzle_error(&drizzle), ret);
168
drizzle_test("drizzle_con_free");
169
drizzle_con_free(&server);
172
drizzle_test("drizzle_con_free");
173
drizzle_con_free(&client);
175
drizzle_test("drizzle_con_free");
176
drizzle_con_free(&listen_con);
178
drizzle_test("drizzle_free");
179
drizzle_free(&drizzle);
184
static void _server(drizzle_con_st *con, server_state_st *state)
186
drizzle_return_t ret;
187
const drizzle_field_t const fields[2]=
189
(const drizzle_field_t)"test_field_1",
190
(const drizzle_field_t)"test_field_2"
192
const size_t field_sizes[2]= { 12, 12 };
196
case SERVER_STATE_START:
197
drizzle_con_set_protocol_version(con, 10);
198
drizzle_con_set_server_version(con, "test_version");
199
drizzle_con_set_thread_id(con, 1);
200
drizzle_con_set_scramble(con, (const uint8_t *)"ABCDEFGHIJKLMNOPQRST");
201
drizzle_con_set_capabilities(con, DRIZZLE_CAPABILITIES_NONE);
202
drizzle_con_set_charset(con, 8);
203
drizzle_con_set_status(con, DRIZZLE_CON_STATUS_NONE);
204
drizzle_con_set_max_packet_size(con, DRIZZLE_MAX_PACKET_SIZE);
206
case SERVER_STATE_HANDSHAKE_WRITE:
207
drizzle_test("drizzle_handshake_server_write");
208
ret= drizzle_handshake_server_write(con);
209
if (ret == DRIZZLE_RETURN_IO_WAIT)
211
state->state = SERVER_STATE_HANDSHAKE_WRITE;
214
else if (ret != DRIZZLE_RETURN_OK)
215
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
217
case SERVER_STATE_HANDSHAKE_READ:
218
drizzle_test("drizzle_handshake_client_read");
219
ret= drizzle_handshake_client_read(con);
220
if (ret == DRIZZLE_RETURN_IO_WAIT)
222
state->state = SERVER_STATE_HANDSHAKE_READ;
225
else if (ret != DRIZZLE_RETURN_OK)
226
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
228
drizzle_test("drizzle_result_create");
229
if (drizzle_result_create(con, &state->result) == NULL)
230
drizzle_test_error("returned %s", drizzle_con_error(con));
232
case SERVER_STATE_HANDSHAKE_RESULT:
233
drizzle_test("drizzle_handshake_result_write");
234
ret= drizzle_result_write(con, &state->result, true);
235
if (ret == DRIZZLE_RETURN_IO_WAIT)
237
state->state = SERVER_STATE_HANDSHAKE_RESULT;
240
else if (ret != DRIZZLE_RETURN_OK)
241
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
243
drizzle_result_free(&state->result);
245
case SERVER_STATE_COMMAND_BUFFER:
246
drizzle_test("drizzle_con_command_buffer");
247
state->data= drizzle_con_command_buffer(con, &state->command, &state->total,
249
if (ret == DRIZZLE_RETURN_IO_WAIT)
251
state->state = SERVER_STATE_COMMAND_BUFFER;
254
else if (ret != DRIZZLE_RETURN_OK)
255
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
257
if (state->command != DRIZZLE_COMMAND_QUERY)
258
drizzle_test_error("command was not a query");
260
if (state->total != 6 && !memcmp(state->data, "SELECT", 6))
261
drizzle_test_error("query doesn't match");
263
if (state->data != NULL)
269
drizzle_test("drizzle_result_create");
270
if (drizzle_result_create(con, &state->result) == NULL)
271
drizzle_test_error("returned %s", drizzle_con_error(con));
273
drizzle_result_set_column_count(&state->result, 2);
275
case SERVER_STATE_RESULT_HEADER:
276
drizzle_test("drizzle_handshake_result_write");
277
ret= drizzle_result_write(con, &state->result, false);
278
if (ret == DRIZZLE_RETURN_IO_WAIT)
280
state->state = SERVER_STATE_RESULT_HEADER;
283
else if (ret != DRIZZLE_RETURN_OK)
284
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
286
drizzle_test("drizzle_column_create");
287
if (drizzle_column_create(&state->result, &state->column) == NULL)
288
drizzle_test_error("returned %s", drizzle_con_error(con));
290
drizzle_column_set_catalog(&state->column, "test_catalog");
291
drizzle_column_set_db(&state->column, "test_db");
292
drizzle_column_set_table(&state->column, "test_table");
293
drizzle_column_set_orig_table(&state->column, "test_orig_table");
294
drizzle_column_set_name(&state->column, "test_column_1");
295
drizzle_column_set_orig_name(&state->column, "test_orig_column_1");
296
drizzle_column_set_charset(&state->column, 8);
297
drizzle_column_set_size(&state->column, 32);
298
drizzle_column_set_type(&state->column, DRIZZLE_COLUMN_TYPE_VARCHAR);
299
drizzle_column_set_flags(&state->column, DRIZZLE_COLUMN_FLAGS_NONE);
301
case SERVER_STATE_RESULT_COLUMN_1:
302
drizzle_test("drizzle_column_write");
303
ret= drizzle_column_write(&state->result, &state->column);
304
if (ret == DRIZZLE_RETURN_IO_WAIT)
306
state->state = SERVER_STATE_RESULT_COLUMN_1;
309
else if (ret != DRIZZLE_RETURN_OK)
310
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
312
drizzle_column_set_name(&state->column, "test_column_2");
313
drizzle_column_set_orig_name(&state->column, "test_orig_column_2");
315
case SERVER_STATE_RESULT_COLUMN_2:
316
drizzle_test("drizzle_column_write");
317
ret= drizzle_column_write(&state->result, &state->column);
318
if (ret == DRIZZLE_RETURN_IO_WAIT)
320
state->state = SERVER_STATE_RESULT_COLUMN_2;
323
else if (ret != DRIZZLE_RETURN_OK)
324
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
326
drizzle_test("drizzle_column_free");
327
drizzle_column_free(&state->column);
329
drizzle_result_set_eof(&state->result, true);
331
case SERVER_STATE_RESULT_COLUMN_EOF:
332
drizzle_test("drizzle_handshake_result_write");
333
ret= drizzle_result_write(con, &state->result, false);
334
if (ret == DRIZZLE_RETURN_IO_WAIT)
336
state->state = SERVER_STATE_RESULT_COLUMN_EOF;
339
else if (ret != DRIZZLE_RETURN_OK)
340
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
342
drizzle_result_calc_row_size(&state->result, fields, field_sizes);
344
case SERVER_STATE_RESULT_ROW_1:
345
ret= drizzle_row_write(&state->result);
347
case SERVER_STATE_RESULT_ROW_1_FIELD_1:
348
ret= drizzle_field_write(&state->result, fields[0], field_sizes[0],
351
case SERVER_STATE_RESULT_ROW_1_FIELD_2:
352
ret= drizzle_field_write(&state->result, fields[1], field_sizes[1],
355
case SERVER_STATE_RESULT_ROW_EOF:
356
drizzle_test("drizzle_handshake_result_write");
357
ret= drizzle_result_write(con, &state->result, true);
358
if (ret == DRIZZLE_RETURN_IO_WAIT)
360
state->state = SERVER_STATE_RESULT_ROW_EOF;
363
else if (ret != DRIZZLE_RETURN_OK)
364
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
366
drizzle_result_free(&state->result);
368
state->state = SERVER_STATE_DONE;
370
case SERVER_STATE_DONE:
374
drizzle_test_error("invalid server state");
378
static void _client(drizzle_con_st *con, client_state_st *state)
380
drizzle_return_t ret;
381
drizzle_column_st *column;
387
case CLIENT_STATE_START:
388
drizzle_test("drizzle_query_str");
389
(void)drizzle_query_str(con, &state->result, "SELECT", &ret);
390
if (ret == DRIZZLE_RETURN_IO_WAIT)
392
state->state = CLIENT_STATE_START;
395
else if (ret != DRIZZLE_RETURN_OK)
396
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
398
case CLIENT_STATE_RESULT:
399
drizzle_test("drizzle_result_buffer");
400
ret = drizzle_result_buffer(&state->result);
401
if (ret == DRIZZLE_RETURN_IO_WAIT)
403
state->state = CLIENT_STATE_RESULT;
406
else if (ret != DRIZZLE_RETURN_OK)
407
drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
409
drizzle_test("drizzle_con_protocol_version");
410
if (drizzle_con_protocol_version(con) != 10)
411
drizzle_test_error("no match");
413
drizzle_test("drizzle_con_server_version");
414
if (strcmp(drizzle_con_server_version(con), "test_version"))
415
drizzle_test_error("no match");
417
drizzle_test("drizzle_con_thread_id");
418
if (drizzle_con_thread_id(con) != 1)
419
drizzle_test_error("no match");
421
drizzle_test("drizzle_con_scramble");
422
if (memcmp(drizzle_con_scramble(con), "ABCDEFGHIJKLMNOPQRST",
423
DRIZZLE_MAX_SCRAMBLE_SIZE))
425
drizzle_test_error("no match");
428
/* We default to MySQL protocol right now, which sets this flag. */
429
drizzle_test("drizzle_con_capabilities");
430
if (drizzle_con_capabilities(con) != DRIZZLE_CAPABILITIES_PROTOCOL_41)
431
drizzle_test_error("no match");
433
drizzle_test("drizzle_con_charset");
434
if (drizzle_con_charset(con) != 8)
435
drizzle_test_error("no match");
437
drizzle_test("drizzle_con_status");
438
if (drizzle_con_status(con) != DRIZZLE_CON_STATUS_NONE)
439
drizzle_test_error("no match");
441
drizzle_test("drizzle_con_packet_size");
442
if (drizzle_con_max_packet_size(con) != DRIZZLE_MAX_PACKET_SIZE)
443
drizzle_test_error("no match");
445
drizzle_test("drizzle_column_next");
446
column= drizzle_column_next(&state->result);
448
drizzle_test_error("column not found");
450
drizzle_test("drizzle_column_drizzle_result");
451
if (drizzle_column_drizzle_result(column) != &state->result)
452
drizzle_test_error("no match");
454
drizzle_test("drizzle_column_catalog");
455
if (strcmp(drizzle_column_catalog(column), "test_catalog"))
456
drizzle_test_error("no match");
458
drizzle_test("drizzle_column_db");
459
if (strcmp(drizzle_column_db(column), "test_db"))
460
drizzle_test_error("no match");
462
drizzle_test("drizzle_column_table");
463
if (strcmp(drizzle_column_table(column), "test_table"))
464
drizzle_test_error("no match");
466
drizzle_test("drizzle_column_orig_table");
467
if (strcmp(drizzle_column_orig_table(column), "test_orig_table"))
468
drizzle_test_error("no match");
470
drizzle_test("drizzle_column_name");
471
if (strcmp(drizzle_column_name(column), "test_column_1"))
472
drizzle_test_error("no match");
474
drizzle_test("drizzle_column_orig_name");
475
if (strcmp(drizzle_column_orig_name(column), "test_orig_column_1"))
476
drizzle_test_error("no match");
478
drizzle_test("drizzle_column_charset");
479
if (drizzle_column_charset(column) != 8)
480
drizzle_test_error("no match");
482
drizzle_test("drizzle_column_size");
483
if (drizzle_column_size(column) != 32)
484
drizzle_test_error("no match");
486
drizzle_test("drizzle_column_max_size");
487
if (drizzle_column_max_size(column) != 12)
488
drizzle_test_error("no match");
490
drizzle_test("drizzle_column_type_drizzle");
491
if (drizzle_column_type_drizzle(column) != DRIZZLE_COLUMN_TYPE_DRIZZLE_VARCHAR)
492
drizzle_test_error("no match");
494
drizzle_test("drizzle_column_flags");
495
if (drizzle_column_flags(column) != DRIZZLE_COLUMN_FLAGS_NONE)
496
drizzle_test_error("no match");
498
drizzle_test("drizzle_column_next");
499
column= drizzle_column_next(&state->result);
501
drizzle_test_error("column not found");
503
drizzle_test("drizzle_column_name");
504
if (strcmp(drizzle_column_name(column), "test_column_2"))
505
drizzle_test_error("no match");
507
drizzle_test("drizzle_column_next");
508
column= drizzle_column_next(&state->result);
510
drizzle_test_error("column found");
512
drizzle_test("drizzle_column_prev");
513
column= drizzle_column_prev(&state->result);
515
drizzle_test_error("column not found");
517
drizzle_test("drizzle_column_name");
518
if (strcmp(drizzle_column_name(column), "test_column_2"))
519
drizzle_test_error("no match");
521
drizzle_test("drizzle_row_next");
522
row= drizzle_row_next(&state->result);
524
drizzle_test_error("row not found");
526
if (strcmp(row[0], "test_field_1") || strcmp(row[1], "test_field_2"))
527
drizzle_test_error("no match");
529
drizzle_test("drizzle_row_field_sizes");
530
field_sizes= drizzle_row_field_sizes(&state->result);
531
if (field_sizes[0] != 12 || field_sizes[1] != 12)
532
drizzle_test_error("no match");
534
drizzle_test("drizzle_row_prev");
535
row = drizzle_row_prev(&state->result);
537
drizzle_test_error("row not found");
539
if (strcmp(row[0], "test_field_1") || strcmp(row[1], "test_field_2"))
540
drizzle_test_error("no match");
542
drizzle_test("drizzle_row_next");
543
row = drizzle_row_next(&state->result);
545
drizzle_test("drizzle_row_index");
546
row = drizzle_row_index(&state->result, 0);
548
drizzle_test_error("row not found");
550
if (strcmp(row[0], "test_field_1") || strcmp(row[1], "test_field_2"))
551
drizzle_test_error("no match");
553
drizzle_test("drizzle_row_index");
554
row = drizzle_row_index(&state->result, 1);
556
drizzle_test_error("row found");
558
drizzle_test("drizzle_result_free");
559
drizzle_result_free(&state->result);
561
state->state = CLIENT_STATE_DONE;
563
case CLIENT_STATE_DONE:
567
drizzle_test_error("invalid client state");