~drizzle-trunk/drizzle/development

2465.1.3 by Brian Aker
Include additional libdrizzle tests.
1
/*
2
 * Drizzle Client & Protocol Library
3
 *
4
 * Copyright (C) 2008 Eric Day (eday@oddments.org)
5
 * All rights reserved.
6
 *
2471.1.1 by Mark Atwood
add missing license headers
7
 * 
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions are
10
 * met:
11
 * 
12
 *     * Redistributions of source code must retain the above copyright
13
 * notice, this list of conditions and the following disclaimer.
14
 * 
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
18
 * distribution.
19
 * 
20
 *     * The names of its contributors may not be used to endorse or
21
 * promote products derived from this software without specific prior
22
 * written permission.
23
 * 
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.
2465.1.3 by Brian Aker
Include additional libdrizzle tests.
35
 */
36
37
/**
38
 * @file
39
 * @brief Tests for drizzle_st Structures
40
 */
41
42
#include <libdrizzle-1.0/t/common.h>
43
44
#include <assert.h>
45
#include <unistd.h>
46
47
#define DRIZZLE_TEST_PORT 12399
48
49
static void _log(const char *line, drizzle_verbose_t verbose,
50
                 void *context);
51
static drizzle_return_t _event_watch(drizzle_con_st *con, short events,
52
                                     void *context);
53
54
static int _event_watch_read_bits= 0;
55
static int _event_watch_write_bits= 0;
56
57
int main(void)
58
{
59
  close(STDOUT_FILENO);
60
61
  drizzle_verbose_t verbose;
62
  drizzle_st *drizzle;
63
  drizzle_st drizzle_buffer;
64
  drizzle_st *clone;
65
  drizzle_st clone_buffer;
66
  drizzle_con_st *con;
67
  drizzle_con_st *listen_con;
68
  drizzle_return_t ret;
69
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));
74
75
  drizzle_test("drizzle_version");
76
  printf("  %s\n", drizzle_version());
77
78
  drizzle_test("drizzle_bugreport");
79
  printf("  %s\n", drizzle_bugreport());
80
81
  drizzle_test("drizzle_verbose_name");
82
  for (verbose= DRIZZLE_VERBOSE_NEVER; verbose <= DRIZZLE_VERBOSE_MAX;
83
       verbose++)
84
  {
85
    printf("  %s\n", drizzle_verbose_name(verbose));
86
  }
87
88
  drizzle_test("drizzle_create buffer");
89
  if ((drizzle= drizzle_create(&drizzle_buffer)) == NULL)
90
    drizzle_test_error("returned NULL");
91
92
  drizzle_test("drizzle_free buffer");
93
  drizzle_free(drizzle);
94
95
  drizzle_test("drizzle_create");
96
  if ((drizzle= drizzle_create(NULL)) == NULL)
97
    drizzle_test_error("returned NULL");
98
99
  drizzle_test("drizzle_clone");
100
  if ((clone= drizzle_clone(NULL, drizzle)) == NULL)
101
    drizzle_test_error("drizzle_clone");
102
103
  drizzle_test("drizzle_free");
104
  drizzle_free(clone);
105
106
  drizzle_test("drizzle_clone buffer");
107
  if ((clone= drizzle_clone(&clone_buffer, drizzle)) == NULL)
108
    drizzle_test_error("returned NULL");
109
110
  drizzle_test("drizzle_free buffer");
111
  drizzle_free(clone);
112
113
  drizzle_test("drizzle_error");
114
  if (strcmp(drizzle_error(drizzle), ""))
115
    drizzle_test_error("error not empty");
116
117
  drizzle_test("drizzle_errno");
118
  if (drizzle_errno(drizzle) != 0)
119
    drizzle_test_error("errno not 0");
120
121
  drizzle_test("drizzle_error_code");
122
  if (drizzle_error_code(drizzle) != 0)
123
    drizzle_test_error("error_code not 0");
124
125
  drizzle_test("drizzle_sqlstate");
126
  if (strcmp(drizzle_sqlstate(drizzle), ""))
127
    drizzle_test_error("sqlstate not empty");
128
129
  /* @todo remove this option with new API. */
130
  drizzle_remove_options(drizzle, DRIZZLE_FREE_OBJECTS);
131
132
  drizzle_test("drizzle_options");
133
  if (drizzle_options(drizzle) != DRIZZLE_ALLOCATED)
134
    drizzle_test_error("expected options not set");
135
136
  drizzle_test("drizzle_add_options");
137
  drizzle_add_options(drizzle, DRIZZLE_NON_BLOCKING);
138
139
  drizzle_test("drizzle_options");
140
  if (drizzle_options(drizzle) != (DRIZZLE_ALLOCATED | DRIZZLE_NON_BLOCKING))
141
    drizzle_test_error("expected options not set");
142
143
  drizzle_test("drizzle_remove_options");
144
  drizzle_remove_options(drizzle, DRIZZLE_NON_BLOCKING);
145
146
  drizzle_test("drizzle_options");
147
  if (drizzle_options(drizzle) != DRIZZLE_ALLOCATED)
148
    drizzle_test_error("expected options not set");
149
150
  drizzle_test("drizzle_set_options");
151
  drizzle_set_options(drizzle, DRIZZLE_ALLOCATED | DRIZZLE_NON_BLOCKING);
152
153
  drizzle_test("drizzle_options");
154
  if (drizzle_options(drizzle) != (DRIZZLE_ALLOCATED | DRIZZLE_NON_BLOCKING))
155
    drizzle_test_error("expected options not set");
156
157
  drizzle_test("drizzle_set_options");
158
  drizzle_set_options(drizzle, DRIZZLE_ALLOCATED);
159
160
  drizzle_test("drizzle_options");
161
  if (drizzle_options(drizzle) != DRIZZLE_ALLOCATED)
162
    drizzle_test_error("expected options not set");
163
164
  drizzle_test("drizzle_set_timeout");
165
  drizzle_set_timeout(drizzle, 1234);
166
167
  drizzle_test("drizzle_timeout");
168
  if (drizzle_timeout(drizzle) != 1234)
169
    drizzle_test_error("expected timeout not set");
170
171
  drizzle_test("drizzle_set_verbose");
172
  drizzle_set_verbose(drizzle, DRIZZLE_VERBOSE_CRAZY);
173
174
  drizzle_test("drizzle_verbose");
175
  if (drizzle_verbose(drizzle) != DRIZZLE_VERBOSE_CRAZY)
176
    drizzle_test_error("expected verbose not set");
177
178
  drizzle_test("drizzle_set_log_fn");
179
  drizzle_set_log_fn(drizzle, _log, NULL);
180
181
  drizzle_test("drizzle_set_event_watch_fn");
182
  drizzle_set_event_watch_fn(drizzle, _event_watch, NULL);
183
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);
2465.1.4 by Brian Aker
Move from using 127.0.0.1, to localhost.
187
  drizzle_con_set_tcp(listen_con, "localhost", DRIZZLE_TEST_PORT);
2465.1.3 by Brian Aker
Include additional libdrizzle tests.
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;
193
194
  /* Attempt a non-blocking connection. */
195
  drizzle_add_options(drizzle, DRIZZLE_NON_BLOCKING);
2465.1.4 by Brian Aker
Move from using 127.0.0.1, to localhost.
196
  con= drizzle_con_add_tcp(drizzle, NULL, "localhost", DRIZZLE_TEST_PORT, "user", "pw", "db",
2465.1.3 by Brian Aker
Include additional libdrizzle tests.
197
                           DRIZZLE_CON_NONE);
198
  assert(con != NULL);
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;
206
207
  drizzle_con_free(listen_con);
208
209
  drizzle_test("drizzle_free");
210
  drizzle_free(drizzle);
211
212
  return 0;
213
}
214
215
static void _log(const char *line, drizzle_verbose_t verbose,
216
                 void *context)
217
{
218
  (void) line;
219
  (void) verbose;
220
  (void) context;
221
}
222
223
static drizzle_return_t _event_watch(drizzle_con_st *con, short events,
224
                                     void *context)
225
{
226
  (void) con;
227
  (void) events;
228
  (void) context;
229
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;
235
  } else {
236
    _event_watch_read_bits&= ~(1 << fd);
237
  }
238
  if (events & POLLOUT) {
239
    _event_watch_write_bits|= 1 << fd;
240
  } else {
241
    _event_watch_write_bits&= ~(1 << fd);
242
  }
243
244
  return DRIZZLE_RETURN_OK;
245
}