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