Merge in fixes for SASL.
[m6w6/libmemcached] / tests / libmemcached_world.h
1 /* libMemcached Functions Test
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Description: This is the startup bits for any libmemcached test.
9 *
10 */
11
12 #pragma once
13
14 #include <cassert>
15
16 /* The structure we use for the test system */
17 struct libmemcached_test_container_st
18 {
19 server_startup_st& construct;
20 memcached_st *parent;
21 memcached_st *memc;
22
23 libmemcached_test_container_st(server_startup_st &construct_arg) :
24 construct(construct_arg),
25 parent(NULL),
26 memc(NULL)
27 { }
28 };
29
30 #define SERVERS_TO_CREATE 5
31
32 static void *world_create(server_startup_st& servers, test_return_t& error)
33 {
34 if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
35 {
36 error= TEST_SKIPPED;
37 return NULL;
38 }
39
40 in_port_t max_port;
41 for (uint32_t x= 0; x < SERVERS_TO_CREATE; x++)
42 {
43 in_port_t port;
44
45 char variable_buffer[1024];
46 snprintf(variable_buffer, sizeof(variable_buffer), "LIBMEMCACHED_PORT_%u", x);
47
48 char *var;
49 if ((var= getenv(variable_buffer)))
50 {
51 port= in_port_t(atoi(var));
52 }
53 else
54 {
55 port= in_port_t(TEST_PORT_BASE +x);
56 }
57
58 max_port= port;
59 const char *argv[1]= { "memcached" };
60 if (servers.sasl())
61 {
62 if (not server_startup(servers, "memcached-sasl", port, 1, argv))
63 {
64 error= TEST_FAILURE;
65 return NULL;
66 }
67 }
68 else
69 {
70 if (not server_startup(servers, "memcached", port, 1, argv))
71 {
72 error= TEST_FAILURE;
73 return NULL;
74 }
75 }
76 }
77
78 if (servers.socket())
79 {
80 if (servers.sasl())
81 {
82 const char *argv[1]= { "memcached" };
83 if (not servers.start_socket_server("memcached-sasl", max_port +1, 1, argv))
84 {
85 error= TEST_FAILURE;
86 return NULL;
87 }
88 }
89 else
90 {
91 const char *argv[1]= { "memcached" };
92 if (not servers.start_socket_server("memcached", max_port +1, 1, argv))
93 {
94 error= TEST_FAILURE;
95 return NULL;
96 }
97 }
98 }
99
100
101 libmemcached_test_container_st *global_container= new libmemcached_test_container_st(servers);
102 if (global_container == NULL)
103 {
104 error= TEST_MEMORY_ALLOCATION_FAILURE;
105 return NULL;
106 }
107
108 error= TEST_SUCCESS;
109
110 return global_container;
111 }
112
113 static test_return_t world_container_startup(libmemcached_test_container_st *container)
114 {
115 char buffer[BUFSIZ];
116
117 test_compare_got(MEMCACHED_SUCCESS,
118 libmemcached_check_configuration(container->construct.option_string().c_str(), container->construct.option_string().size(),
119 buffer, sizeof(buffer)),
120 container->construct.option_string().c_str());
121
122 test_true(not container->parent);
123 container->parent= memcached(container->construct.option_string().c_str(), container->construct.option_string().size());
124 test_true(container->parent);
125
126 if (container->construct.sasl())
127 {
128 if (memcached_failed(memcached_behavior_set(container->parent, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1)))
129 {
130 memcached_free(container->parent);
131 return TEST_FAILURE;
132 }
133
134 if (memcached_failed(memcached_set_sasl_auth_data(container->parent, container->construct.username().c_str(), container->construct.password().c_str())))
135 {
136 memcached_free(container->parent);
137 return TEST_FAILURE;
138 }
139 }
140
141 for (uint32_t host= 0; host < memcached_server_count(container->parent); ++host)
142 {
143 memcached_server_instance_st instance=
144 memcached_server_instance_by_position(container->parent, host);
145
146 if (instance->type == MEMCACHED_CONNECTION_TCP)
147 {
148 test_true_got(memcached_server_port(instance) >= TEST_PORT_BASE, memcached_server_port(instance));
149 }
150 }
151
152 return TEST_SUCCESS;
153 }
154
155 static test_return_t world_container_shutdown(libmemcached_test_container_st *container)
156 {
157 memcached_free(container->parent);
158 container->parent= NULL;
159
160 return TEST_SUCCESS;
161 }
162
163 static test_return_t world_test_startup(libmemcached_test_container_st *container)
164 {
165 test_true(container);
166 test_true(not container->memc);
167 test_true(container->parent);
168 container->memc= memcached_clone(NULL, container->parent);
169 test_true(container->memc);
170
171 return TEST_SUCCESS;
172 }
173
174 test_return_t world_flush(libmemcached_test_container_st *container);
175 test_return_t world_flush(libmemcached_test_container_st *container)
176 {
177 test_true(container->memc);
178 memcached_flush(container->memc, 0);
179 memcached_quit(container->memc);
180
181 return TEST_SUCCESS;
182 }
183
184 static test_return_t world_pre_run(libmemcached_test_container_st *container)
185 {
186 test_true(container->memc);
187 for (uint32_t loop= 0; loop < memcached_server_list_count(container->memc->servers); loop++)
188 {
189 memcached_server_instance_st instance=
190 memcached_server_instance_by_position(container->memc, loop);
191
192 test_compare(-1, instance->fd);
193 test_compare(0U, instance->cursor_active);
194 }
195
196 return TEST_SUCCESS;
197 }
198
199
200 static test_return_t world_post_run(libmemcached_test_container_st *container)
201 {
202 test_true(container->memc);
203
204 return TEST_SUCCESS;
205 }
206
207 static test_return_t world_on_error(test_return_t test_state, libmemcached_test_container_st *container)
208 {
209 (void)test_state;
210 test_true(container->memc);
211 memcached_free(container->memc);
212 container->memc= NULL;
213
214 return TEST_SUCCESS;
215 }
216
217 static bool world_destroy(void *object)
218 {
219 libmemcached_test_container_st *container= (libmemcached_test_container_st *)object;
220 #if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT
221 if (LIBMEMCACHED_WITH_SASL_SUPPORT)
222 {
223 sasl_done();
224 }
225 #endif
226
227 delete container;
228
229 return TEST_SUCCESS;
230 }
231
232 typedef test_return_t (*libmemcached_test_callback_fn)(memcached_st *);
233
234 static test_return_t _runner_default(libmemcached_test_callback_fn func, libmemcached_test_container_st *container)
235 {
236 if (func)
237 {
238 test_true(container);
239 test_true(container->memc);
240 return func(container->memc);
241 }
242
243 return TEST_SUCCESS;
244 }
245
246 static test_return_t _pre_runner_default(libmemcached_test_callback_fn func, libmemcached_test_container_st *container)
247 {
248 if (func)
249 {
250 return func(container->parent);
251 }
252
253 return TEST_SUCCESS;
254 }
255
256 static test_return_t _post_runner_default(libmemcached_test_callback_fn func, libmemcached_test_container_st *container)
257 {
258 if (func)
259 {
260 return func(container->parent);
261 }
262
263 return TEST_SUCCESS;
264 }
265
266 class LibmemcachedRunner : public Runner {
267 public:
268 test_return_t run(test_callback_fn* func, void *object)
269 {
270 return _runner_default(libmemcached_test_callback_fn(func), (libmemcached_test_container_st*)object);
271 }
272
273 test_return_t pre(test_callback_fn* func, void *object)
274 {
275 return _pre_runner_default(libmemcached_test_callback_fn(func), (libmemcached_test_container_st*)object);
276 }
277
278 test_return_t post(test_callback_fn* func, void *object)
279 {
280 return _post_runner_default(libmemcached_test_callback_fn(func), (libmemcached_test_container_st*)object);
281 }
282 };
283
284 static LibmemcachedRunner defualt_libmemcached_runner;