Update for ifdef for clang. Also fix a bad operation comparison.
[m6w6/libmemcached] / tests / mem_udp.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
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.
35 *
36 */
37
38
39 /*
40 Sample test application.
41 */
42
43 #include <config.h>
44 #include <libtest/test.hpp>
45
46 using namespace libtest;
47
48 #include <libmemcached-1.0/memcached.h>
49 #include <libmemcached/server_instance.h>
50 #include <libmemcached/io.h>
51 #include <libmemcached/udp.hpp>
52 #include <libmemcachedutil-1.0/util.h>
53
54 #include <cstdio>
55 #include <cstdlib>
56 #include <cstring>
57 #include <sys/time.h>
58 #include <sys/types.h>
59 #include <sys/stat.h>
60 #include <signal.h>
61 #include <unistd.h>
62 #include <time.h>
63
64 #include <libtest/server.h>
65
66 #include "libmemcached/instance.h"
67
68 #ifndef __INTEL_COMPILER
69 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
70 #endif
71
72 /**
73 @note This should be testing to see if the server really supports the binary protocol.
74 */
75 static test_return_t pre_binary(memcached_st *memc)
76 {
77 memcached_st *memc_clone= memcached_clone(NULL, memc);
78 test_true(memc_clone);
79
80 // The memcached_version needs to be done on a clone, because the server
81 // will not toggle protocol on an connection.
82 memcached_version(memc_clone);
83
84 test_compare(MEMCACHED_SUCCESS, memcached_version(memc));
85 test_compare(true, libmemcached_util_version_check(memc, 1, 2, 1));
86 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, true));
87 test_compare(true, memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL));
88
89 memcached_free(memc_clone);
90
91 return TEST_SUCCESS;
92 }
93
94 typedef std::vector<uint16_t> Expected;
95
96 static void increment_request_id(uint16_t *id)
97 {
98 (*id)++;
99 if ((*id & UDP_REQUEST_ID_THREAD_MASK) != 0)
100 {
101 *id= 0;
102 }
103 }
104
105 static void get_udp_request_ids(memcached_st *memc, Expected &ids)
106 {
107 for (uint32_t x= 0; x < memcached_server_count(memc); x++)
108 {
109 memcached_server_instance_st instance= memcached_server_instance_by_position(memc, x);
110
111 ids.push_back(get_udp_datagram_request_id((struct udp_datagram_header_st *) ((memcached_server_instance_st )instance)->write_buffer));
112 }
113 }
114
115 static test_return_t post_udp_op_check(memcached_st *memc, Expected& expected_req_ids)
116 {
117 (void)memc;
118 (void)expected_req_ids;
119 #if 0
120 memcached_server_st *cur_server = memcached_server_list(memc);
121 uint16_t *cur_req_ids = get_udp_request_ids(memc);
122
123 for (size_t x= 0; x < memcached_server_count(memc); x++)
124 {
125 test_true(cur_server[x].cursor_active == 0);
126 test_true(cur_req_ids[x] == expected_req_ids[x]);
127 }
128 free(expected_req_ids);
129 free(cur_req_ids);
130
131 #endif
132 return TEST_SUCCESS;
133 }
134
135 /*
136 ** There is a little bit of a hack here, instead of removing
137 ** the servers, I just set num host to 0 and them add then new udp servers
138 **/
139 static test_return_t init_udp(memcached_st *memc)
140 {
141 test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, true));
142
143 return TEST_SUCCESS;
144 }
145
146 static test_return_t init_udp_valgrind(memcached_st *memc)
147 {
148 if (getenv("TESTS_ENVIRONMENT"))
149 {
150 return TEST_SKIPPED;
151 }
152
153 test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, true));
154
155 return TEST_SUCCESS;
156 }
157
158 static test_return_t binary_init_udp(memcached_st *memc)
159 {
160 if (getenv("TESTS_ENVIRONMENT"))
161 {
162 return TEST_SKIPPED;
163 }
164
165 test_skip(TEST_SUCCESS, pre_binary(memc));
166
167 return init_udp(memc);
168 }
169
170 /* Make sure that I cant add a tcp server to a udp client */
171 static test_return_t add_tcp_server_udp_client_test(memcached_st *memc)
172 {
173 (void)memc;
174 #if 0
175 memcached_server_st server;
176 memcached_server_instance_st instance=
177 memcached_server_instance_by_position(memc, 0);
178 memcached_server_clone(&server, &memc->hosts[0]);
179 test_true(memcached_server_remove(&(memc->hosts[0])) == MEMCACHED_SUCCESS);
180 test_true(memcached_server_add(memc, server.hostname, server.port) == MEMCACHED_INVALID_HOST_PROTOCOL);
181 #endif
182 return TEST_SUCCESS;
183 }
184
185 /* Make sure that I cant add a udp server to a tcp client */
186 static test_return_t add_udp_server_tcp_client_test(memcached_st *memc)
187 {
188 (void)memc;
189 #if 0
190 memcached_server_st server;
191 memcached_server_instance_st instance=
192 memcached_server_instance_by_position(memc, 0);
193 memcached_server_clone(&server, &memc->hosts[0]);
194 test_true(memcached_server_remove(&(memc->hosts[0])) == MEMCACHED_SUCCESS);
195
196 memcached_st tcp_client;
197 memcached_create(&tcp_client);
198 test_true(memcached_server_add_udp(&tcp_client, server.hostname, server.port) == MEMCACHED_INVALID_HOST_PROTOCOL);
199 #endif
200
201 return TEST_SUCCESS;
202 }
203
204 static test_return_t version_TEST(memcached_st *memc)
205 {
206 test_compare(MEMCACHED_NOT_SUPPORTED, memcached_version(memc));
207 return TEST_SUCCESS;
208 }
209
210 static test_return_t verbosity_TEST(memcached_st *memc)
211 {
212 test_compare(MEMCACHED_SUCCESS, memcached_verbosity(memc, 0));
213 return TEST_SUCCESS;
214 }
215
216 static test_return_t memcached_get_TEST(memcached_st *memc)
217 {
218 memcached_return_t rc;
219 test_null(memcached_get(memc,
220 test_literal_param(__func__),
221 0, 0, &rc));
222 test_compare(MEMCACHED_NOT_SUPPORTED, rc);
223
224 return TEST_SUCCESS;
225 }
226
227 static test_return_t memcached_mget_execute_by_key_TEST(memcached_st *memc)
228 {
229 char **keys= NULL;
230 size_t *key_length= NULL;
231 test_compare(MEMCACHED_NOT_SUPPORTED,
232 memcached_mget_execute_by_key(memc,
233 test_literal_param(__func__), // Group key
234 keys, key_length, // Actual key
235 0, // Number of keys
236 0, // callbacks
237 0, // context
238 0)); // Number of callbacks
239
240 return TEST_SUCCESS;
241 }
242
243 static test_return_t memcached_stat_TEST(memcached_st *memc)
244 {
245 memcached_return_t rc;
246 test_null(memcached_stat(memc, 0, &rc));
247 test_compare(MEMCACHED_NOT_SUPPORTED, rc);
248
249 return TEST_SUCCESS;
250 }
251
252 static test_return_t set_udp_behavior_test(memcached_st *memc)
253 {
254 memcached_quit(memc);
255
256 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, memc->distribution));
257 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, true));
258 test_compare(true, memc->flags.use_udp);
259 test_compare(false, memc->flags.reply);
260
261 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, false));
262 test_compare(false, memc->flags.use_udp);
263 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, false));
264 test_compare(true, memc->flags.reply);
265
266 return TEST_SUCCESS;
267 }
268
269 static test_return_t udp_set_test(memcached_st *memc)
270 {
271 // Assume we are running under valgrind, and bail
272 if (getenv("TESTS_ENVIRONMENT"))
273 {
274 return TEST_SUCCESS;
275 }
276
277 const unsigned int num_iters= 1025; //request id rolls over at 1024
278
279 test_true(memc);
280
281 for (size_t x= 0; x < num_iters;x++)
282 {
283 Expected expected_ids;
284 get_udp_request_ids(memc, expected_ids);
285 unsigned int server_key= memcached_generate_hash(memc, test_literal_param("foo"));
286 test_true(server_key < memcached_server_count(memc));
287 memcached_server_instance_st instance= memcached_server_instance_by_position(memc, server_key);
288 size_t init_offset= instance->write_buffer_offset;
289
290 test_compare_hint(MEMCACHED_SUCCESS,
291 memcached_set(memc,
292 test_literal_param("foo"),
293 test_literal_param("when we sanitize"),
294 time_t(0), uint32_t(0)),
295 memcached_last_error_message(memc));
296
297 /*
298 NB, the check below assumes that if new write_ptr is less than
299 the original write_ptr that we have flushed. For large payloads, this
300 maybe an invalid assumption, but for the small payload we have it is OK
301 */
302 if (instance->write_buffer_offset < init_offset)
303 {
304 increment_request_id(&expected_ids[server_key]);
305 }
306
307 test_compare(TEST_SUCCESS, post_udp_op_check(memc, expected_ids));
308 }
309
310 return TEST_SUCCESS;
311 }
312
313 static test_return_t udp_buffered_set_test(memcached_st *memc)
314 {
315 test_true(memc);
316 test_compare(MEMCACHED_INVALID_ARGUMENTS,
317 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, true));
318 return TEST_SUCCESS;
319 }
320
321 static test_return_t udp_set_too_big_test(memcached_st *memc)
322 {
323 test_true(memc);
324 Expected expected_ids;
325 get_udp_request_ids(memc, expected_ids);
326
327 std::vector<char> value;
328 value.resize(1024 * 1024 * 10);
329
330 test_compare_hint(MEMCACHED_WRITE_FAILURE,
331 memcached_set(memc,
332 test_literal_param(__func__),
333 &value[0], value.size(),
334 time_t(0), uint32_t(0)),
335 memcached_last_error_message(memc));
336 memcached_quit(memc);
337
338 return post_udp_op_check(memc, expected_ids);
339 }
340
341 static test_return_t udp_delete_test(memcached_st *memc)
342 {
343 test_true(memc);
344
345 //request id rolls over at 1024
346 for (size_t x= 0; x < 1025; x++)
347 {
348 Expected expected_ids;
349 get_udp_request_ids(memc, expected_ids);
350
351 unsigned int server_key= memcached_generate_hash(memc, test_literal_param("foo"));
352 memcached_server_instance_st instance= memcached_server_instance_by_position(memc, server_key);
353 size_t init_offset= instance->write_buffer_offset;
354
355 test_compare(MEMCACHED_SUCCESS,
356 memcached_delete(memc, test_literal_param("foo"), 0));
357
358 if (instance->write_buffer_offset < init_offset)
359 {
360 increment_request_id(&expected_ids[server_key]);
361 }
362
363 test_compare(TEST_SUCCESS, post_udp_op_check(memc, expected_ids));
364 }
365
366 return TEST_SUCCESS;
367 }
368
369 static test_return_t udp_buffered_delete_test(memcached_st *memc)
370 {
371 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
372 return udp_delete_test(memc);
373 }
374
375 static test_return_t udp_verbosity_test(memcached_st *memc)
376 {
377 Expected expected_ids;
378 get_udp_request_ids(memc, expected_ids);
379
380 for (size_t x= 0; x < memcached_server_count(memc); x++)
381 {
382 increment_request_id(&expected_ids[x]);
383 }
384
385 test_compare(MEMCACHED_SUCCESS, memcached_verbosity(memc, 3));
386
387 return post_udp_op_check(memc, expected_ids);
388 }
389
390 static test_return_t udp_quit_test(memcached_st *memc)
391 {
392 Expected expected_ids;
393 memcached_quit(memc);
394
395 return post_udp_op_check(memc, expected_ids);
396 }
397
398 static test_return_t udp_flush_test(memcached_st *memc)
399 {
400 Expected expected_ids;
401 get_udp_request_ids(memc, expected_ids);
402
403 for (size_t x= 0; x < memcached_server_count(memc); x++)
404 {
405 increment_request_id(&expected_ids[x]);
406 }
407 memcached_error_print(memc);
408 test_compare_hint(MEMCACHED_SUCCESS, memcached_flush(memc, 0), memcached_last_error_message(memc));
409
410 return post_udp_op_check(memc, expected_ids);
411 }
412
413 static test_return_t udp_incr_test(memcached_st *memc)
414 {
415 test_compare(MEMCACHED_SUCCESS,
416 memcached_set(memc, test_literal_param("incr"),
417 test_literal_param("1"),
418 (time_t)0, (uint32_t)0));
419
420 Expected expected_ids;
421 get_udp_request_ids(memc, expected_ids);
422
423 unsigned int server_key= memcached_generate_hash(memc, test_literal_param("incr"));
424 increment_request_id(&expected_ids[server_key]);
425
426 uint64_t newvalue;
427 test_compare(MEMCACHED_SUCCESS, memcached_increment(memc, test_literal_param("incr"), 1, &newvalue));
428
429 return post_udp_op_check(memc, expected_ids);
430 }
431
432 static test_return_t udp_decr_test(memcached_st *memc)
433 {
434 test_compare(MEMCACHED_SUCCESS,
435 memcached_set(memc,
436 test_literal_param(__func__),
437 test_literal_param("1"),
438 time_t(0), uint32_t(0)));
439
440 Expected expected_ids;
441 get_udp_request_ids(memc, expected_ids);
442
443 unsigned int server_key= memcached_generate_hash(memc,
444 test_literal_param(__func__));
445 increment_request_id(&expected_ids[server_key]);
446
447 uint64_t newvalue;
448 test_compare(MEMCACHED_SUCCESS, memcached_decrement(memc,
449 test_literal_param(__func__),
450 1, &newvalue));
451
452 return post_udp_op_check(memc, expected_ids);
453 }
454
455
456 static test_return_t udp_stat_test(memcached_st *memc)
457 {
458 memcached_return_t rc;
459 char args[]= "";
460 Expected expected_ids;
461 get_udp_request_ids(memc, expected_ids);
462 memcached_stat_st *rv= memcached_stat(memc, args, &rc);
463 memcached_stat_free(memc, rv);
464 test_compare(MEMCACHED_NOT_SUPPORTED, rc);
465
466 return post_udp_op_check(memc, expected_ids);
467 }
468
469 static test_return_t udp_version_test(memcached_st *memc)
470 {
471 Expected expected_ids;
472 get_udp_request_ids(memc, expected_ids);
473
474 test_compare(MEMCACHED_NOT_SUPPORTED,
475 memcached_version(memc));
476
477 return post_udp_op_check(memc, expected_ids);
478 }
479
480 static test_return_t udp_get_test(memcached_st *memc)
481 {
482 memcached_return_t rc;
483 size_t vlen;
484 Expected expected_ids;
485 get_udp_request_ids(memc, expected_ids);
486 test_null(memcached_get(memc, test_literal_param("foo"), &vlen, (uint32_t)0, &rc));
487 test_compare(MEMCACHED_NOT_SUPPORTED, rc);
488
489 return post_udp_op_check(memc, expected_ids);
490 }
491
492 static test_return_t udp_mixed_io_test(memcached_st *memc)
493 {
494 test_st mixed_io_ops [] ={
495 {"udp_set_test", 0,
496 (test_callback_fn*)udp_set_test},
497 {"udp_set_too_big_test", 0,
498 (test_callback_fn*)udp_set_too_big_test},
499 {"udp_delete_test", 0,
500 (test_callback_fn*)udp_delete_test},
501 {"udp_verbosity_test", 0,
502 (test_callback_fn*)udp_verbosity_test},
503 {"udp_quit_test", 0,
504 (test_callback_fn*)udp_quit_test},
505 #if 0
506 {"udp_flush_test", 0,
507 (test_callback_fn*)udp_flush_test},
508 #endif
509 {"udp_incr_test", 0,
510 (test_callback_fn*)udp_incr_test},
511 {"udp_decr_test", 0,
512 (test_callback_fn*)udp_decr_test},
513 {"udp_version_test", 0,
514 (test_callback_fn*)udp_version_test}
515 };
516
517 for (size_t x= 0; x < 500; x++)
518 {
519 test_st current_op= mixed_io_ops[(random() % 8)];
520 test_compare(TEST_SUCCESS, current_op.test_fn(memc));
521 }
522 return TEST_SUCCESS;
523 }
524
525 test_st compatibility_TESTS[] ={
526 {"version", 0, (test_callback_fn*)version_TEST },
527 {"version", 0, (test_callback_fn*)verbosity_TEST },
528 {"memcached_get()", 0, (test_callback_fn*)memcached_get_TEST },
529 {"memcached_mget_execute_by_key()", 0, (test_callback_fn*)memcached_mget_execute_by_key_TEST },
530 {"memcached_stat()", 0, (test_callback_fn*)memcached_stat_TEST },
531 {0, 0, 0}
532 };
533
534 test_st udp_setup_server_tests[] ={
535 {"set_udp_behavior_test", 0, (test_callback_fn*)set_udp_behavior_test},
536 {"add_tcp_server_udp_client_test", 0, (test_callback_fn*)add_tcp_server_udp_client_test},
537 {"add_udp_server_tcp_client_test", 0, (test_callback_fn*)add_udp_server_tcp_client_test},
538 {0, 0, 0}
539 };
540
541 test_st upd_io_tests[] ={
542 {"udp_set_test", 0, (test_callback_fn*)udp_set_test},
543 {"udp_buffered_set_test", 0, (test_callback_fn*)udp_buffered_set_test},
544 {"udp_set_too_big_test", 0, (test_callback_fn*)udp_set_too_big_test},
545 {"udp_delete_test", 0, (test_callback_fn*)udp_delete_test},
546 {"udp_buffered_delete_test", 0, (test_callback_fn*)udp_buffered_delete_test},
547 {"udp_verbosity_test", 0, (test_callback_fn*)udp_verbosity_test},
548 {"udp_quit_test", 0, (test_callback_fn*)udp_quit_test},
549 {"udp_flush_test", 0, (test_callback_fn*)udp_flush_test},
550 {"udp_incr_test", 0, (test_callback_fn*)udp_incr_test},
551 {"udp_decr_test", 0, (test_callback_fn*)udp_decr_test},
552 {"udp_stat_test", 0, (test_callback_fn*)udp_stat_test},
553 {"udp_version_test", 0, (test_callback_fn*)udp_version_test},
554 {"udp_get_test", 0, (test_callback_fn*)udp_get_test},
555 {"udp_mixed_io_test", 0, (test_callback_fn*)udp_mixed_io_test},
556 {0, 0, 0}
557 };
558
559 collection_st collection[] ={
560 {"udp_setup", (test_callback_fn*)init_udp, 0, udp_setup_server_tests},
561 {"compatibility", (test_callback_fn*)init_udp, 0, compatibility_TESTS},
562 {"udp_io", (test_callback_fn*)init_udp_valgrind, 0, upd_io_tests},
563 {"udp_binary_io", (test_callback_fn*)binary_init_udp, 0, upd_io_tests},
564 {0, 0, 0, 0}
565 };
566
567 #include "tests/libmemcached_world.h"
568
569 void get_world(libtest::Framework* world)
570 {
571 world->collections(collection);
572
573 world->create((test_callback_create_fn*)world_create);
574 world->destroy((test_callback_destroy_fn*)world_destroy);
575
576 world->set_runner(new LibmemcachedRunner);
577 }