~drizzle-trunk/drizzle/development

1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
1
/*
2
 * Drizzle Client & Protocol Library
3
 *
4
 * Copyright (C) 2008 Eric Day (eday@oddments.org)
5
 * All rights reserved.
6
 *
7
 * Use and distribution licensed under the BSD license.  See
1799.2.4 by Monty Taylor
Made BSD files reference root BSD file.
8
 * the COPYING.BSD file in the root source directory for full text.
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
9
 */
10
11
/**
12
 * @file
13
 * @brief Tests for drizzle_st Structures
14
 */
15
1712.1.4 by Monty Taylor
Fixed unittesty stuff.
16
#include "common.h"
17
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
18
#include <assert.h>
19
20
#define DRIZZLE_TEST_HOST "127.0.0.1"
21
#define DRIZZLE_TEST_PORT 12399
22
23
typedef enum
24
{
25
  SERVER_STATE_START,
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,
38
  SERVER_STATE_DONE
39
} server_state_t;
40
41
typedef struct
42
{
43
  server_state_t state;
44
  drizzle_result_st result;
45
  drizzle_column_st column;
46
  drizzle_command_t command;
47
  char *data;
48
  size_t total;
49
} server_state_st;
50
51
typedef enum
52
{
53
  CLIENT_STATE_START,
54
  CLIENT_STATE_RESULT,
55
  CLIENT_STATE_DONE
56
} client_state_t;
57
58
typedef struct
59
{
60
  client_state_t state;
61
  drizzle_result_st result;
62
} client_state_st;
63
64
static void _server(drizzle_con_st *con, server_state_st *state);
65
static void _client(drizzle_con_st *con, client_state_st *state);
66
67
int main(void)
68
{
69
  drizzle_st drizzle;
70
  drizzle_con_st listen_con;
71
  drizzle_con_st client;
72
  drizzle_con_st server;
73
  drizzle_return_t ret;
74
  bool server_accepted = false;
75
  server_state_st server_state;
76
  client_state_st client_state;
77
78
  drizzle_test("drizzle_create");
79
  if (drizzle_create(&drizzle) == NULL)
80
    drizzle_test_error("returned NULL");
81
82
  drizzle_test("drizzle_con_add_tcp_listen");
83
  if (drizzle_con_add_tcp_listen(&drizzle, &listen_con, DRIZZLE_TEST_HOST,
84
                                 DRIZZLE_TEST_PORT, 1,
85
                                 DRIZZLE_CON_NONE) == NULL)
86
  {
87
    drizzle_test_error("returned NULL");
88
  }
89
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);
94
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)
99
  {
100
    drizzle_test_error("returned NULL");
101
  }
102
103
  drizzle_test("drizzle_add_options");
104
  drizzle_add_options(&drizzle, DRIZZLE_NON_BLOCKING);
105
106
  server_state.state= SERVER_STATE_START;
107
  client_state.state= CLIENT_STATE_START;
108
109
  while (true)
110
  {
111
    if (!server_accepted)
112
    {
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);
119
    }
120
121
    if (server_accepted)
122
      _server(&server, &server_state);
123
124
    _client(&client, &client_state);
125
126
    if (server_state.state == SERVER_STATE_DONE &&
127
        client_state.state == CLIENT_STATE_DONE)
128
    {
129
      break;
130
    }
131
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);
136
  }
137
138
  if (server_accepted)
139
  {
140
    drizzle_test("drizzle_con_free");
141
    drizzle_con_free(&server);
142
  }
143
144
  drizzle_test("drizzle_con_free");
145
  drizzle_con_free(&client);
146
147
  drizzle_test("drizzle_con_free");
148
  drizzle_con_free(&listen_con);
149
150
  drizzle_test("drizzle_free");
151
  drizzle_free(&drizzle);
152
153
  return 0;
154
}
155
156
static void _server(drizzle_con_st *con, server_state_st *state)
157
{
158
  drizzle_return_t ret;
159
  const drizzle_field_t const fields[2]=
160
  {
161
    (const drizzle_field_t)"test_field_1",
162
    (const drizzle_field_t)"test_field_2"
163
  };
164
  const size_t field_sizes[2]= { 12, 12 };
165
166
  switch(state->state)
167
  {
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);
177
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)
182
    {
183
      state->state = SERVER_STATE_HANDSHAKE_WRITE;
184
      return;
185
    }
186
    else if (ret != DRIZZLE_RETURN_OK)
187
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
188
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)
193
    {
194
      state->state = SERVER_STATE_HANDSHAKE_READ;
195
      return;
196
    }
197
    else if (ret != DRIZZLE_RETURN_OK)
198
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
199
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));
203
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)
208
    {
209
      state->state = SERVER_STATE_HANDSHAKE_RESULT;
210
      return;
211
    }
212
    else if (ret != DRIZZLE_RETURN_OK)
213
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
214
215
    drizzle_result_free(&state->result);
216
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,
220
                                            &ret);
221
    if (ret == DRIZZLE_RETURN_IO_WAIT)
222
    {
223
      state->state = SERVER_STATE_COMMAND_BUFFER;
224
      return;
225
    }
226
    else if (ret != DRIZZLE_RETURN_OK)
227
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
228
229
    if (state->command != DRIZZLE_COMMAND_QUERY)
230
      drizzle_test_error("command was not a query");
231
232
    if (state->total != 6 && !memcmp(state->data, "SELECT", 6))
233
      drizzle_test_error("query doesn't match");
234
235
    if (state->data != NULL)
236
    {
237
      free(state->data);
238
      state->data= NULL;
239
    }
240
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));
244
245
    drizzle_result_set_column_count(&state->result, 2);
246
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)
251
    {
252
      state->state = SERVER_STATE_RESULT_HEADER;
253
      return;
254
    }
255
    else if (ret != DRIZZLE_RETURN_OK)
256
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
257
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));
261
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);
272
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)
277
    {
278
      state->state = SERVER_STATE_RESULT_COLUMN_1;
279
      return;
280
    }
281
    else if (ret != DRIZZLE_RETURN_OK)
282
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
283
284
    drizzle_column_set_name(&state->column, "test_column_2");
285
    drizzle_column_set_orig_name(&state->column, "test_orig_column_2");
286
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)
291
    {
292
      state->state = SERVER_STATE_RESULT_COLUMN_2;
293
      return;
294
    }
295
    else if (ret != DRIZZLE_RETURN_OK)
296
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
297
298
    drizzle_test("drizzle_column_free");
299
    drizzle_column_free(&state->column);
300
301
    drizzle_result_set_eof(&state->result, true);
302
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)
307
    {
308
      state->state = SERVER_STATE_RESULT_COLUMN_EOF;
309
      return;
310
    }
311
    else if (ret != DRIZZLE_RETURN_OK)
312
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
313
314
    drizzle_result_calc_row_size(&state->result, fields, field_sizes);
315
316
  case SERVER_STATE_RESULT_ROW_1:
317
    ret= drizzle_row_write(&state->result);
318
319
  case SERVER_STATE_RESULT_ROW_1_FIELD_1:
320
    ret= drizzle_field_write(&state->result, fields[0], field_sizes[0],
321
                             field_sizes[0]);
322
323
  case SERVER_STATE_RESULT_ROW_1_FIELD_2:
324
    ret= drizzle_field_write(&state->result, fields[1], field_sizes[1],
325
                             field_sizes[1]);
326
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)
331
    {
332
      state->state = SERVER_STATE_RESULT_ROW_EOF;
333
      return;
334
    }
335
    else if (ret != DRIZZLE_RETURN_OK)
336
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
337
338
    drizzle_result_free(&state->result);
339
340
    state->state = SERVER_STATE_DONE;
341
342
  case SERVER_STATE_DONE:
343
    return;
344
345
  default:
346
    drizzle_test_error("invalid server state");
347
  }
348
}
349
350
static void _client(drizzle_con_st *con, client_state_st *state)
351
{
352
  drizzle_return_t ret;
353
  drizzle_column_st *column;
354
  drizzle_row_t row;
355
  size_t *field_sizes;
356
357
  switch(state->state)
358
  {
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)
363
    {
364
      state->state = CLIENT_STATE_START;
365
      return;
366
    }
367
    else if (ret != DRIZZLE_RETURN_OK)
368
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
369
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)
374
    {
375
      state->state = CLIENT_STATE_RESULT;
376
      return;
377
    }
378
    else if (ret != DRIZZLE_RETURN_OK)
379
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
380
381
    drizzle_test("drizzle_con_protocol_version");
382
    if (drizzle_con_protocol_version(con) != 10)
383
      drizzle_test_error("no match");
384
385
    drizzle_test("drizzle_con_server_version");
386
    if (strcmp(drizzle_con_server_version(con), "test_version"))
387
      drizzle_test_error("no match");
388
389
    drizzle_test("drizzle_con_thread_id");
390
    if (drizzle_con_thread_id(con) != 1)
391
      drizzle_test_error("no match");
392
393
    drizzle_test("drizzle_con_scramble");
394
    if (memcmp(drizzle_con_scramble(con), "ABCDEFGHIJKLMNOPQRST",
395
               DRIZZLE_MAX_SCRAMBLE_SIZE))
396
    {
397
      drizzle_test_error("no match");
398
    }
399
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");
404
405
    drizzle_test("drizzle_con_charset");
406
    if (drizzle_con_charset(con) != 8)
407
      drizzle_test_error("no match");
408
409
    drizzle_test("drizzle_con_status");
410
    if (drizzle_con_status(con) != DRIZZLE_CON_STATUS_NONE)
411
      drizzle_test_error("no match");
412
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");
416
417
    drizzle_test("drizzle_column_next");
418
    column= drizzle_column_next(&state->result);
419
    if (column == NULL)
420
      drizzle_test_error("column not found");
421
422
    drizzle_test("drizzle_column_drizzle_result");
423
    if (drizzle_column_drizzle_result(column) != &state->result)
424
      drizzle_test_error("no match");
425
426
    drizzle_test("drizzle_column_catalog");
427
    if (strcmp(drizzle_column_catalog(column), "test_catalog"))
428
      drizzle_test_error("no match");
429
430
    drizzle_test("drizzle_column_db");
431
    if (strcmp(drizzle_column_db(column), "test_db"))
432
      drizzle_test_error("no match");
433
434
    drizzle_test("drizzle_column_table");
435
    if (strcmp(drizzle_column_table(column), "test_table"))
436
      drizzle_test_error("no match");
437
438
    drizzle_test("drizzle_column_orig_table");
439
    if (strcmp(drizzle_column_orig_table(column), "test_orig_table"))
440
      drizzle_test_error("no match");
441
442
    drizzle_test("drizzle_column_name");
443
    if (strcmp(drizzle_column_name(column), "test_column_1"))
444
      drizzle_test_error("no match");
445
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");
449
450
    drizzle_test("drizzle_column_charset");
451
    if (drizzle_column_charset(column) != 8)
452
      drizzle_test_error("no match");
453
454
    drizzle_test("drizzle_column_size");
455
    if (drizzle_column_size(column) != 32)
456
      drizzle_test_error("no match");
457
458
    drizzle_test("drizzle_column_max_size");
459
    if (drizzle_column_max_size(column) != 12)
460
      drizzle_test_error("no match");
461
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");
465
466
    drizzle_test("drizzle_column_flags");
467
    if (drizzle_column_flags(column) != DRIZZLE_COLUMN_FLAGS_NONE)
468
      drizzle_test_error("no match");
469
470
    drizzle_test("drizzle_column_next");
471
    column= drizzle_column_next(&state->result);
472
    if (column == NULL)
473
      drizzle_test_error("column not found");
474
475
    drizzle_test("drizzle_column_name");
476
    if (strcmp(drizzle_column_name(column), "test_column_2"))
477
      drizzle_test_error("no match");
478
479
    drizzle_test("drizzle_column_next");
480
    column= drizzle_column_next(&state->result);
481
    if (column != NULL)
482
      drizzle_test_error("column found");
483
484
    drizzle_test("drizzle_column_prev");
485
    column= drizzle_column_prev(&state->result);
486
    if (column == NULL)
487
      drizzle_test_error("column not found");
488
489
    drizzle_test("drizzle_column_name");
490
    if (strcmp(drizzle_column_name(column), "test_column_2"))
491
      drizzle_test_error("no match");
492
493
    drizzle_test("drizzle_row_next");
494
    row= drizzle_row_next(&state->result);
495
    if (row == NULL)
496
      drizzle_test_error("row not found");
497
498
    if (strcmp(row[0], "test_field_1") || strcmp(row[1], "test_field_2"))
499
      drizzle_test_error("no match");
500
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");
505
506
    drizzle_test("drizzle_row_prev");
507
    row = drizzle_row_prev(&state->result);
508
    if (row == NULL)
509
      drizzle_test_error("row not found");
510
511
    if (strcmp(row[0], "test_field_1") || strcmp(row[1], "test_field_2"))
512
      drizzle_test_error("no match");
513
514
    drizzle_test("drizzle_row_next");
515
    row = drizzle_row_next(&state->result);
516
517
    drizzle_test("drizzle_row_index");
518
    row = drizzle_row_index(&state->result, 0);
519
    if (row == NULL)
520
      drizzle_test_error("row not found");
521
522
    if (strcmp(row[0], "test_field_1") || strcmp(row[1], "test_field_2"))
523
      drizzle_test_error("no match");
524
525
    drizzle_test("drizzle_row_index");
526
    row = drizzle_row_index(&state->result, 1);
527
    if (row != NULL)
528
      drizzle_test_error("row found");
529
530
    drizzle_test("drizzle_result_free");
531
    drizzle_result_free(&state->result);
532
533
    state->state = CLIENT_STATE_DONE;
534
535
  case CLIENT_STATE_DONE:
536
    return;
537
538
  default:
539
    drizzle_test_error("invalid client state");
540
  }
541
}