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
20
#define DRIZZLE_TEST_PORT 12399
22
static void _log(const char *line, drizzle_verbose_t verbose,
24
static drizzle_return_t _event_watch(drizzle_con_st *con, short events,
27
static int _event_watch_read_bits= 0;
28
static int _event_watch_write_bits= 0;
32
drizzle_verbose_t verbose;
34
drizzle_st drizzle_buffer;
36
drizzle_st clone_buffer;
38
drizzle_con_st *listen_con;
41
printf("sizeof(drizzle_st) = %zu\n", sizeof(drizzle_st));
42
printf("sizeof(drizzle_return_t) = %zu\n", sizeof(drizzle_return_t));
43
printf("sizeof(drizzle_verbose_t) = %zu\n", sizeof(drizzle_verbose_t));
44
printf("sizeof(drizzle_options_t) = %zu\n", sizeof(drizzle_options_t));
46
drizzle_test("drizzle_version");
47
printf(" %s\n", drizzle_version());
49
drizzle_test("drizzle_bugreport");
50
printf(" %s\n", drizzle_bugreport());
52
drizzle_test("drizzle_verbose_name");
53
for (verbose= DRIZZLE_VERBOSE_NEVER; verbose <= DRIZZLE_VERBOSE_MAX;
56
printf(" %s\n", drizzle_verbose_name(verbose));
59
drizzle_test("drizzle_create buffer");
60
if ((drizzle= drizzle_create(&drizzle_buffer)) == NULL)
61
drizzle_test_error("returned NULL");
63
drizzle_test("drizzle_free buffer");
64
drizzle_free(drizzle);
66
drizzle_test("drizzle_create");
67
if ((drizzle= drizzle_create(NULL)) == NULL)
68
drizzle_test_error("returned NULL");
70
drizzle_test("drizzle_clone");
71
if ((clone= drizzle_clone(NULL, drizzle)) == NULL)
72
drizzle_test_error("drizzle_clone");
74
drizzle_test("drizzle_free");
77
drizzle_test("drizzle_clone buffer");
78
if ((clone= drizzle_clone(&clone_buffer, drizzle)) == NULL)
79
drizzle_test_error("returned NULL");
81
drizzle_test("drizzle_free buffer");
84
drizzle_test("drizzle_error");
85
if (strcmp(drizzle_error(drizzle), ""))
86
drizzle_test_error("error not empty");
88
drizzle_test("drizzle_errno");
89
if (drizzle_errno(drizzle) != 0)
90
drizzle_test_error("errno not 0");
92
drizzle_test("drizzle_error_code");
93
if (drizzle_error_code(drizzle) != 0)
94
drizzle_test_error("error_code not 0");
96
drizzle_test("drizzle_sqlstate");
97
if (strcmp(drizzle_sqlstate(drizzle), ""))
98
drizzle_test_error("sqlstate not empty");
100
/* @todo remove this option with new API. */
101
drizzle_remove_options(drizzle, DRIZZLE_FREE_OBJECTS);
103
drizzle_test("drizzle_options");
104
if (drizzle_options(drizzle) != DRIZZLE_ALLOCATED)
105
drizzle_test_error("expected options not set");
107
drizzle_test("drizzle_add_options");
108
drizzle_add_options(drizzle, DRIZZLE_NON_BLOCKING);
110
drizzle_test("drizzle_options");
111
if (drizzle_options(drizzle) != (DRIZZLE_ALLOCATED | DRIZZLE_NON_BLOCKING))
112
drizzle_test_error("expected options not set");
114
drizzle_test("drizzle_remove_options");
115
drizzle_remove_options(drizzle, DRIZZLE_NON_BLOCKING);
117
drizzle_test("drizzle_options");
118
if (drizzle_options(drizzle) != DRIZZLE_ALLOCATED)
119
drizzle_test_error("expected options not set");
121
drizzle_test("drizzle_set_options");
122
drizzle_set_options(drizzle, DRIZZLE_ALLOCATED | DRIZZLE_NON_BLOCKING);
124
drizzle_test("drizzle_options");
125
if (drizzle_options(drizzle) != (DRIZZLE_ALLOCATED | DRIZZLE_NON_BLOCKING))
126
drizzle_test_error("expected options not set");
128
drizzle_test("drizzle_set_options");
129
drizzle_set_options(drizzle, DRIZZLE_ALLOCATED);
131
drizzle_test("drizzle_options");
132
if (drizzle_options(drizzle) != DRIZZLE_ALLOCATED)
133
drizzle_test_error("expected options not set");
135
drizzle_test("drizzle_set_timeout");
136
drizzle_set_timeout(drizzle, 1234);
138
drizzle_test("drizzle_timeout");
139
if (drizzle_timeout(drizzle) != 1234)
140
drizzle_test_error("expected timeout not set");
142
drizzle_test("drizzle_set_verbose");
143
drizzle_set_verbose(drizzle, DRIZZLE_VERBOSE_CRAZY);
145
drizzle_test("drizzle_verbose");
146
if (drizzle_verbose(drizzle) != DRIZZLE_VERBOSE_CRAZY)
147
drizzle_test_error("expected verbose not set");
149
drizzle_test("drizzle_set_log_fn");
150
drizzle_set_log_fn(drizzle, _log, NULL);
152
drizzle_test("drizzle_set_event_watch_fn");
153
drizzle_set_event_watch_fn(drizzle, _event_watch, NULL);
155
/* Create a listening connection to verify that event_watch_fn gets called. */
156
listen_con= drizzle_con_create(drizzle, NULL);
157
assert(listen_con != NULL);
158
drizzle_con_set_tcp(listen_con, "127.0.0.1", DRIZZLE_TEST_PORT);
159
ret= drizzle_con_listen(listen_con);
160
assert(ret == DRIZZLE_RETURN_OK);
161
if (_event_watch_read_bits == 0)
162
drizzle_test_error("event_watch_fn not called to wait for connections");
163
_event_watch_read_bits= 0;
165
/* Attempt a non-blocking connection. */
166
drizzle_add_options(drizzle, DRIZZLE_NON_BLOCKING);
167
con= drizzle_con_add_tcp(drizzle, NULL, "127.0.0.1", DRIZZLE_TEST_PORT, "user", "pw", "db",
170
ret= drizzle_con_connect(con);
171
assert(ret == DRIZZLE_RETURN_IO_WAIT);
172
if (_event_watch_read_bits == 0 && _event_watch_write_bits == 0)
173
drizzle_test_error("event_watch_fn not called to wait for I/O");
174
drizzle_con_free(con);
175
_event_watch_read_bits= 0;
176
_event_watch_write_bits= 0;
178
drizzle_con_free(listen_con);
180
drizzle_test("drizzle_free");
181
drizzle_free(drizzle);
186
static void _log(const char *line, drizzle_verbose_t verbose,
194
static drizzle_return_t _event_watch(drizzle_con_st *con, short events,
201
/* fake register the file descriptor */
202
int fd= drizzle_con_fd(con);
203
assert(0 <= fd && fd < (int) sizeof(_event_watch_read_bits) * 8);
204
if (events & POLLIN) {
205
_event_watch_read_bits|= 1 << fd;
207
_event_watch_read_bits&= ~(1 << fd);
209
if (events & POLLOUT) {
210
_event_watch_write_bits|= 1 << fd;
212
_event_watch_write_bits&= ~(1 << fd);
215
return DRIZZLE_RETURN_OK;