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