2
* Drizzle Client & Protocol Library
4
* Copyright (C) 2008 Eric Day (eday@oddments.org)
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions are
12
* * Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
15
* * Redistributions in binary form must reproduce the above
16
* copyright notice, this list of conditions and the following disclaimer
17
* in the documentation and/or other materials provided with the
20
* * The names of its contributors may not be used to endorse or
21
* promote products derived from this software without specific prior
24
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
* @brief Tests for drizzle_st Structures
42
#include <libdrizzle-1.0/t/common.h>
47
#define DRIZZLE_TEST_PORT 12399
49
static void _log(const char *line, drizzle_verbose_t verbose,
51
static drizzle_return_t _event_watch(drizzle_con_st *con, short events,
54
static int _event_watch_read_bits= 0;
55
static int _event_watch_write_bits= 0;
61
drizzle_verbose_t verbose;
63
drizzle_st drizzle_buffer;
65
drizzle_st clone_buffer;
67
drizzle_con_st *listen_con;
70
printf("sizeof(drizzle_st) = %zu\n", sizeof(drizzle_st));
71
printf("sizeof(drizzle_return_t) = %zu\n", sizeof(drizzle_return_t));
72
printf("sizeof(drizzle_verbose_t) = %zu\n", sizeof(drizzle_verbose_t));
73
printf("sizeof(drizzle_options_t) = %zu\n", sizeof(drizzle_options_t));
75
drizzle_test("drizzle_version");
76
printf(" %s\n", drizzle_version());
78
drizzle_test("drizzle_bugreport");
79
printf(" %s\n", drizzle_bugreport());
81
drizzle_test("drizzle_verbose_name");
82
for (verbose= DRIZZLE_VERBOSE_NEVER; verbose <= DRIZZLE_VERBOSE_MAX;
85
printf(" %s\n", drizzle_verbose_name(verbose));
88
drizzle_test("drizzle_create buffer");
89
if ((drizzle= drizzle_create(&drizzle_buffer)) == NULL)
90
drizzle_test_error("returned NULL");
92
drizzle_test("drizzle_free buffer");
93
drizzle_free(drizzle);
95
drizzle_test("drizzle_create");
96
if ((drizzle= drizzle_create(NULL)) == NULL)
97
drizzle_test_error("returned NULL");
99
drizzle_test("drizzle_clone");
100
if ((clone= drizzle_clone(NULL, drizzle)) == NULL)
101
drizzle_test_error("drizzle_clone");
103
drizzle_test("drizzle_free");
106
drizzle_test("drizzle_clone buffer");
107
if ((clone= drizzle_clone(&clone_buffer, drizzle)) == NULL)
108
drizzle_test_error("returned NULL");
110
drizzle_test("drizzle_free buffer");
113
drizzle_test("drizzle_error");
114
if (strcmp(drizzle_error(drizzle), ""))
115
drizzle_test_error("error not empty");
117
drizzle_test("drizzle_errno");
118
if (drizzle_errno(drizzle) != 0)
119
drizzle_test_error("errno not 0");
121
drizzle_test("drizzle_error_code");
122
if (drizzle_error_code(drizzle) != 0)
123
drizzle_test_error("error_code not 0");
125
drizzle_test("drizzle_sqlstate");
126
if (strcmp(drizzle_sqlstate(drizzle), ""))
127
drizzle_test_error("sqlstate not empty");
129
/* @todo remove this option with new API. */
130
drizzle_remove_options(drizzle, DRIZZLE_FREE_OBJECTS);
132
drizzle_test("drizzle_options");
133
if (drizzle_options(drizzle) != DRIZZLE_ALLOCATED)
134
drizzle_test_error("expected options not set");
136
drizzle_test("drizzle_add_options");
137
drizzle_add_options(drizzle, DRIZZLE_NON_BLOCKING);
139
drizzle_test("drizzle_options");
140
if (drizzle_options(drizzle) != (DRIZZLE_ALLOCATED | DRIZZLE_NON_BLOCKING))
141
drizzle_test_error("expected options not set");
143
drizzle_test("drizzle_remove_options");
144
drizzle_remove_options(drizzle, DRIZZLE_NON_BLOCKING);
146
drizzle_test("drizzle_options");
147
if (drizzle_options(drizzle) != DRIZZLE_ALLOCATED)
148
drizzle_test_error("expected options not set");
150
drizzle_test("drizzle_set_options");
151
drizzle_set_options(drizzle, DRIZZLE_ALLOCATED | DRIZZLE_NON_BLOCKING);
153
drizzle_test("drizzle_options");
154
if (drizzle_options(drizzle) != (DRIZZLE_ALLOCATED | DRIZZLE_NON_BLOCKING))
155
drizzle_test_error("expected options not set");
157
drizzle_test("drizzle_set_options");
158
drizzle_set_options(drizzle, DRIZZLE_ALLOCATED);
160
drizzle_test("drizzle_options");
161
if (drizzle_options(drizzle) != DRIZZLE_ALLOCATED)
162
drizzle_test_error("expected options not set");
164
drizzle_test("drizzle_set_timeout");
165
drizzle_set_timeout(drizzle, 1234);
167
drizzle_test("drizzle_timeout");
168
if (drizzle_timeout(drizzle) != 1234)
169
drizzle_test_error("expected timeout not set");
171
drizzle_test("drizzle_set_verbose");
172
drizzle_set_verbose(drizzle, DRIZZLE_VERBOSE_CRAZY);
174
drizzle_test("drizzle_verbose");
175
if (drizzle_verbose(drizzle) != DRIZZLE_VERBOSE_CRAZY)
176
drizzle_test_error("expected verbose not set");
178
drizzle_test("drizzle_set_log_fn");
179
drizzle_set_log_fn(drizzle, _log, NULL);
181
drizzle_test("drizzle_set_event_watch_fn");
182
drizzle_set_event_watch_fn(drizzle, _event_watch, NULL);
184
/* Create a listening connection to verify that event_watch_fn gets called. */
185
listen_con= drizzle_con_create(drizzle, NULL);
186
assert(listen_con != NULL);
187
drizzle_con_set_tcp(listen_con, "localhost", DRIZZLE_TEST_PORT);
188
ret= drizzle_con_listen(listen_con);
189
assert(ret == DRIZZLE_RETURN_OK);
190
if (_event_watch_read_bits == 0)
191
drizzle_test_error("event_watch_fn not called to wait for connections");
192
_event_watch_read_bits= 0;
194
/* Attempt a non-blocking connection. */
195
drizzle_add_options(drizzle, DRIZZLE_NON_BLOCKING);
196
con= drizzle_con_add_tcp(drizzle, NULL, "localhost", DRIZZLE_TEST_PORT, "user", "pw", "db",
199
ret= drizzle_con_connect(con);
200
assert(ret == DRIZZLE_RETURN_IO_WAIT);
201
if (_event_watch_read_bits == 0 && _event_watch_write_bits == 0)
202
drizzle_test_error("event_watch_fn not called to wait for I/O");
203
drizzle_con_free(con);
204
_event_watch_read_bits= 0;
205
_event_watch_write_bits= 0;
207
drizzle_con_free(listen_con);
209
drizzle_test("drizzle_free");
210
drizzle_free(drizzle);
215
static void _log(const char *line, drizzle_verbose_t verbose,
223
static drizzle_return_t _event_watch(drizzle_con_st *con, short events,
230
/* fake register the file descriptor */
231
int fd= drizzle_con_fd(con);
232
assert(0 <= fd && fd < (int) sizeof(_event_watch_read_bits) * 8);
233
if (events & POLLIN) {
234
_event_watch_read_bits|= 1 << fd;
236
_event_watch_read_bits&= ~(1 << fd);
238
if (events & POLLOUT) {
239
_event_watch_write_bits|= 1 << fd;
241
_event_watch_write_bits&= ~(1 << fd);
244
return DRIZZLE_RETURN_OK;