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_PORT 12399
23
static void _log(const char *line, drizzle_verbose_t verbose,
25
static drizzle_return_t _event_watch(drizzle_con_st *con, short events,
28
static int _event_watch_read_bits= 0;
29
static int _event_watch_write_bits= 0;
35
drizzle_verbose_t verbose;
37
drizzle_st drizzle_buffer;
39
drizzle_st clone_buffer;
41
drizzle_con_st *listen_con;
44
printf("sizeof(drizzle_st) = %zu\n", sizeof(drizzle_st));
45
printf("sizeof(drizzle_return_t) = %zu\n", sizeof(drizzle_return_t));
46
printf("sizeof(drizzle_verbose_t) = %zu\n", sizeof(drizzle_verbose_t));
47
printf("sizeof(drizzle_options_t) = %zu\n", sizeof(drizzle_options_t));
49
drizzle_test("drizzle_version");
50
printf(" %s\n", drizzle_version());
52
drizzle_test("drizzle_bugreport");
53
printf(" %s\n", drizzle_bugreport());
55
drizzle_test("drizzle_verbose_name");
56
for (verbose= DRIZZLE_VERBOSE_NEVER; verbose <= DRIZZLE_VERBOSE_MAX;
59
printf(" %s\n", drizzle_verbose_name(verbose));
62
drizzle_test("drizzle_create buffer");
63
if ((drizzle= drizzle_create(&drizzle_buffer)) == NULL)
64
drizzle_test_error("returned NULL");
66
drizzle_test("drizzle_free buffer");
67
drizzle_free(drizzle);
69
drizzle_test("drizzle_create");
70
if ((drizzle= drizzle_create(NULL)) == NULL)
71
drizzle_test_error("returned NULL");
73
drizzle_test("drizzle_clone");
74
if ((clone= drizzle_clone(NULL, drizzle)) == NULL)
75
drizzle_test_error("drizzle_clone");
77
drizzle_test("drizzle_free");
80
drizzle_test("drizzle_clone buffer");
81
if ((clone= drizzle_clone(&clone_buffer, drizzle)) == NULL)
82
drizzle_test_error("returned NULL");
84
drizzle_test("drizzle_free buffer");
87
drizzle_test("drizzle_error");
88
if (strcmp(drizzle_error(drizzle), ""))
89
drizzle_test_error("error not empty");
91
drizzle_test("drizzle_errno");
92
if (drizzle_errno(drizzle) != 0)
93
drizzle_test_error("errno not 0");
95
drizzle_test("drizzle_error_code");
96
if (drizzle_error_code(drizzle) != 0)
97
drizzle_test_error("error_code not 0");
99
drizzle_test("drizzle_sqlstate");
100
if (strcmp(drizzle_sqlstate(drizzle), ""))
101
drizzle_test_error("sqlstate not empty");
103
/* @todo remove this option with new API. */
104
drizzle_remove_options(drizzle, DRIZZLE_FREE_OBJECTS);
106
drizzle_test("drizzle_options");
107
if (drizzle_options(drizzle) != DRIZZLE_ALLOCATED)
108
drizzle_test_error("expected options not set");
110
drizzle_test("drizzle_add_options");
111
drizzle_add_options(drizzle, DRIZZLE_NON_BLOCKING);
113
drizzle_test("drizzle_options");
114
if (drizzle_options(drizzle) != (DRIZZLE_ALLOCATED | DRIZZLE_NON_BLOCKING))
115
drizzle_test_error("expected options not set");
117
drizzle_test("drizzle_remove_options");
118
drizzle_remove_options(drizzle, DRIZZLE_NON_BLOCKING);
120
drizzle_test("drizzle_options");
121
if (drizzle_options(drizzle) != DRIZZLE_ALLOCATED)
122
drizzle_test_error("expected options not set");
124
drizzle_test("drizzle_set_options");
125
drizzle_set_options(drizzle, DRIZZLE_ALLOCATED | DRIZZLE_NON_BLOCKING);
127
drizzle_test("drizzle_options");
128
if (drizzle_options(drizzle) != (DRIZZLE_ALLOCATED | DRIZZLE_NON_BLOCKING))
129
drizzle_test_error("expected options not set");
131
drizzle_test("drizzle_set_options");
132
drizzle_set_options(drizzle, DRIZZLE_ALLOCATED);
134
drizzle_test("drizzle_options");
135
if (drizzle_options(drizzle) != DRIZZLE_ALLOCATED)
136
drizzle_test_error("expected options not set");
138
drizzle_test("drizzle_set_timeout");
139
drizzle_set_timeout(drizzle, 1234);
141
drizzle_test("drizzle_timeout");
142
if (drizzle_timeout(drizzle) != 1234)
143
drizzle_test_error("expected timeout not set");
145
drizzle_test("drizzle_set_verbose");
146
drizzle_set_verbose(drizzle, DRIZZLE_VERBOSE_CRAZY);
148
drizzle_test("drizzle_verbose");
149
if (drizzle_verbose(drizzle) != DRIZZLE_VERBOSE_CRAZY)
150
drizzle_test_error("expected verbose not set");
152
drizzle_test("drizzle_set_log_fn");
153
drizzle_set_log_fn(drizzle, _log, NULL);
155
drizzle_test("drizzle_set_event_watch_fn");
156
drizzle_set_event_watch_fn(drizzle, _event_watch, NULL);
158
/* Create a listening connection to verify that event_watch_fn gets called. */
159
listen_con= drizzle_con_create(drizzle, NULL);
160
assert(listen_con != NULL);
161
drizzle_con_set_tcp(listen_con, "localhost", DRIZZLE_TEST_PORT);
162
ret= drizzle_con_listen(listen_con);
163
assert(ret == DRIZZLE_RETURN_OK);
164
if (_event_watch_read_bits == 0)
165
drizzle_test_error("event_watch_fn not called to wait for connections");
166
_event_watch_read_bits= 0;
168
/* Attempt a non-blocking connection. */
169
drizzle_add_options(drizzle, DRIZZLE_NON_BLOCKING);
170
con= drizzle_con_add_tcp(drizzle, NULL, "localhost", DRIZZLE_TEST_PORT, "user", "pw", "db",
173
ret= drizzle_con_connect(con);
174
assert(ret == DRIZZLE_RETURN_IO_WAIT);
175
if (_event_watch_read_bits == 0 && _event_watch_write_bits == 0)
176
drizzle_test_error("event_watch_fn not called to wait for I/O");
177
drizzle_con_free(con);
178
_event_watch_read_bits= 0;
179
_event_watch_write_bits= 0;
181
drizzle_con_free(listen_con);
183
drizzle_test("drizzle_free");
184
drizzle_free(drizzle);
189
static void _log(const char *line, drizzle_verbose_t verbose,
197
static drizzle_return_t _event_watch(drizzle_con_st *con, short events,
204
/* fake register the file descriptor */
205
int fd= drizzle_con_fd(con);
206
assert(0 <= fd && fd < (int) sizeof(_event_watch_read_bits) * 8);
207
if (events & POLLIN) {
208
_event_watch_read_bits|= 1 << fd;
210
_event_watch_read_bits&= ~(1 << fd);
212
if (events & POLLOUT) {
213
_event_watch_write_bits|= 1 << fd;
215
_event_watch_write_bits&= ~(1 << fd);
218
return DRIZZLE_RETURN_OK;