Merge in updates 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 // Assume we are running under valgrind, and bail
41 if (servers.sasl() and getenv("TESTS_ENVIRONMENT"))
42 {
43 error= TEST_SKIPPED;
44 return NULL;
45 }
46
47
48 in_port_t max_port;
49 for (uint32_t x= 0; x < SERVERS_TO_CREATE; x++)
50 {
51 in_port_t port;
52
53 char variable_buffer[1024];
54 snprintf(variable_buffer, sizeof(variable_buffer), "LIBMEMCACHED_PORT_%u", x);
55
56 char *var;
57 if ((var= getenv(variable_buffer)))
58 {
59 port= in_port_t(atoi(var));
60 }
61 else
62 {
63 port= in_port_t(TEST_PORT_BASE +x);
64 }
65
66 max_port= port;
67 const char *argv[1]= { "memcached" };
68 if (servers.sasl())
69 {
70 if (not server_startup(servers, "memcached-sasl", port, 1, argv))
71 {
72 error= TEST_FAILURE;
73 return NULL;
74 }
75 }
76 else
77 {
78 if (not server_startup(servers, "memcached", port, 1, argv))
79 {
80 error= TEST_FAILURE;
81 return NULL;
82 }
83 }
84 }
85
86 if (servers.socket())
87 {
88 if (servers.sasl())
89 {
90 const char *argv[1]= { "memcached" };
91 if (not servers.start_socket_server("memcached-sasl", max_port +1, 1, argv))
92 {
93 error= TEST_FAILURE;
94 return NULL;
95 }
96 }
97 else
98 {
99 const char *argv[1]= { "memcached" };
100 if (not servers.start_socket_server("memcached", max_port +1, 1, argv))
101 {
102 error= TEST_FAILURE;
103 return NULL;
104 }
105 }
106 }
107
108
109 libmemcached_test_container_st *global_container= new libmemcached_test_container_st(servers);
110 if (global_container == NULL)
111 {
112 error= TEST_MEMORY_ALLOCATION_FAILURE;
113 return NULL;
114 }
115
116 error= TEST_SUCCESS;
117
118 return global_container;
119 }
120
121 static test_return_t world_container_startup(libmemcached_test_container_st *container)
122 {
123 char buffer[BUFSIZ];
124
125 test_compare_got(MEMCACHED_SUCCESS,
126 libmemcached_check_configuration(container->construct.option_string().c_str(), container->construct.option_string().size(),
127 buffer, sizeof(buffer)),
128 container->construct.option_string().c_str());
129
130 test_true(not container->parent);
131 container->parent= memcached(container->construct.option_string().c_str(), container->construct.option_string().size());
132 test_true(container->parent);
133
134 if (container->construct.sasl())
135 {
136 if (memcached_failed(memcached_behavior_set(container->parent, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1)))
137 {
138 memcached_free(container->parent);
139 return TEST_FAILURE;
140 }
141
142 if (memcached_failed(memcached_set_sasl_auth_data(container->parent, container->construct.username().c_str(), container->construct.password().c_str())))
143 {
144 memcached_free(container->parent);
145 return TEST_FAILURE;
146 }
147 }
148
149 for (uint32_t host= 0; host < memcached_server_count(container->parent); ++host)
150 {
151 memcached_server_instance_st instance=
152 memcached_server_instance_by_position(container->parent, host);
153
154 if (instance->type == MEMCACHED_CONNECTION_TCP)
155 {
156 test_true_got(memcached_server_port(instance) >= TEST_PORT_BASE, memcached_server_port(instance));
157 }
158 }
159
160 return TEST_SUCCESS;
161 }
162
163 static test_return_t world_container_shutdown(libmemcached_test_container_st *container)
164 {
165 memcached_free(container->parent);
166 container->parent= NULL;
167
168 return TEST_SUCCESS;
169 }
170
171 static test_return_t world_test_startup(libmemcached_test_container_st *container)
172 {
173 test_true(container);
174 test_true(not container->memc);
175 test_true(container->parent);
176 container->memc= memcached_clone(NULL, container->parent);
177 test_true(container->memc);
178
179 return TEST_SUCCESS;
180 }
181
182 test_return_t world_flush(libmemcached_test_container_st *container);
183 test_return_t world_flush(libmemcached_test_container_st *container)
184 {
185 test_true(container->memc);
186 memcached_flush(container->memc, 0);
187 memcached_quit(container->memc);
188
189 return TEST_SUCCESS;
190 }
191
192 static test_return_t world_pre_run(libmemcached_test_container_st *container)
193 {
194 test_true(container->memc);
195 for (uint32_t loop= 0; loop < memcached_server_list_count(container->memc->servers); loop++)
196 {
197 memcached_server_instance_st instance=
198 memcached_server_instance_by_position(container->memc, loop);
199
200 test_compare(-1, instance->fd);
201 test_compare(0U, instance->cursor_active);
202 }
203
204 return TEST_SUCCESS;
205 }
206
207
208 static test_return_t world_post_run(libmemcached_test_container_st *container)
209 {
210 test_true(container->memc);
211
212 return TEST_SUCCESS;
213 }
214
215 static test_return_t world_on_error(test_return_t test_state, libmemcached_test_container_st *container)
216 {
217 (void)test_state;
218 test_true(container->memc);
219 memcached_free(container->memc);
220 container->memc= NULL;
221
222 return TEST_SUCCESS;
223 }
224
225 static bool world_destroy(void *object)
226 {
227 libmemcached_test_container_st *container= (libmemcached_test_container_st *)object;
228 #if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT
229 if (LIBMEMCACHED_WITH_SASL_SUPPORT)
230 {
231 sasl_done();
232 }
233 #endif
234
235 delete container;
236
237 return TEST_SUCCESS;
238 }
239
240 typedef test_return_t (*libmemcached_test_callback_fn)(memcached_st *);
241
242 static test_return_t _runner_default(libmemcached_test_callback_fn func, libmemcached_test_container_st *container)
243 {
244 if (func)
245 {
246 test_true(container);
247 test_true(container->memc);
248 return func(container->memc);
249 }
250
251 return TEST_SUCCESS;
252 }
253
254 static test_return_t _pre_runner_default(libmemcached_test_callback_fn func, libmemcached_test_container_st *container)
255 {
256 if (func)
257 {
258 return func(container->parent);
259 }
260
261 return TEST_SUCCESS;
262 }
263
264 static test_return_t _post_runner_default(libmemcached_test_callback_fn func, libmemcached_test_container_st *container)
265 {
266 if (func)
267 {
268 return func(container->parent);
269 }
270
271 return TEST_SUCCESS;
272 }
273
274 class LibmemcachedRunner : public Runner {
275 public:
276 test_return_t run(test_callback_fn* func, void *object)
277 {
278 return _runner_default(libmemcached_test_callback_fn(func), (libmemcached_test_container_st*)object);
279 }
280
281 test_return_t pre(test_callback_fn* func, void *object)
282 {
283 return _pre_runner_default(libmemcached_test_callback_fn(func), (libmemcached_test_container_st*)object);
284 }
285
286 test_return_t post(test_callback_fn* func, void *object)
287 {
288 return _post_runner_default(libmemcached_test_callback_fn(func), (libmemcached_test_container_st*)object);
289 }
290 };
291
292 static LibmemcachedRunner defualt_libmemcached_runner;