Update hardening rules.
[m6w6/libmemcached] / tests / libmemcached-1.0 / haldenbrand.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached Client and Server
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 * 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 #include <config.h>
39 #include <libtest/test.hpp>
40
41 #include "tests/libmemcached-1.0/haldenbrand.h"
42 #include "tests/libmemcached-1.0/fetch_all_results.h"
43
44 /* Test case provided by Cal Haldenbrand */
45 #define HALDENBRAND_KEY_COUNT 3000U // * 1024576
46 #define HALDENBRAND_FLAG_KEY 99 // * 1024576
47
48 test_return_t haldenbrand_TEST1(memcached_st *memc)
49 {
50 /* We just keep looking at the same values over and over */
51 srandom(10);
52
53 test_compare(MEMCACHED_SUCCESS,
54 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, true));
55 test_compare(MEMCACHED_SUCCESS,
56 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, true));
57
58
59 /* add key */
60 unsigned long long total= 0;
61 for (uint32_t x= 0 ; total < 20 * 1024576 ; x++ )
62 {
63 uint32_t size= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
64 char randomstuff[6 * 1024];
65 memset(randomstuff, 0, 6 * 1024);
66 test_true(size < 6 * 1024); /* Being safe here */
67
68 for (uint32_t j= 0 ; j < size ;j++)
69 {
70 randomstuff[j] = (signed char) ((rand() % 26) + 97);
71 }
72
73 total+= size;
74 char key[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH +1];
75 int key_length= snprintf(key, sizeof(key), "%u", x);
76 test_compare(MEMCACHED_SUCCESS,
77 memcached_set(memc, key, key_length,
78 randomstuff, strlen(randomstuff),
79 time_t(0), HALDENBRAND_FLAG_KEY));
80 }
81 test_true(total > HALDENBRAND_KEY_COUNT);
82
83 return TEST_SUCCESS;
84 }
85
86 /* Test case provided by Cal Haldenbrand */
87 test_return_t haldenbrand_TEST2(memcached_st *memc)
88 {
89 test_compare(MEMCACHED_SUCCESS,
90 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, true));
91
92 test_compare(MEMCACHED_SUCCESS,
93 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, true));
94
95 #if 0
96 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, 20 * 1024576));
97 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, 20 * 1024576));
98 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
99 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
100
101 for (x= 0, errors= 0; total < 20 * 1024576 ; x++);
102 #endif
103
104 size_t total_value_length= 0;
105 for (uint32_t x= 0, errors= 0; total_value_length < 24576 ; x++)
106 {
107 uint32_t flags= 0;
108 size_t val_len= 0;
109
110 char key[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH +1];
111 int key_length= snprintf(key, sizeof(key), "%u", x);
112
113 memcached_return_t rc;
114 char *getval= memcached_get(memc, key, key_length, &val_len, &flags, &rc);
115 if (memcached_failed(rc))
116 {
117 if (rc == MEMCACHED_NOTFOUND)
118 {
119 errors++;
120 }
121 else
122 {
123 test_true(rc);
124 }
125
126 continue;
127 }
128 test_compare(uint32_t(HALDENBRAND_FLAG_KEY), flags);
129 test_true(getval);
130
131 total_value_length+= val_len;
132 errors= 0;
133 ::free(getval);
134 }
135
136 return TEST_SUCCESS;
137 }
138
139 /* Do a large mget() over all the keys we think exist */
140 test_return_t haldenbrand_TEST3(memcached_st *memc)
141 {
142 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, true));
143 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, true));
144
145 #ifdef NOT_YET
146 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, 20 * 1024576);
147 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, 20 * 1024576);
148 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
149 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
150 #endif
151
152 std::vector<size_t> key_lengths;
153 key_lengths.resize(HALDENBRAND_KEY_COUNT);
154 std::vector<char *> keys;
155 keys.resize(key_lengths.size());
156 for (uint32_t x= 0; x < key_lengths.size(); x++)
157 {
158 char key[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH +1];
159 int key_length= snprintf(key, sizeof(key), "%u", x);
160 test_true(key_length > 0 and key_length < MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH +1);
161 keys[x]= strdup(key);
162 key_lengths[x]= key_length;
163 }
164
165 test_compare(MEMCACHED_SUCCESS,
166 memcached_mget(memc, &keys[0], &key_lengths[0], key_lengths.size()));
167
168 unsigned int keys_returned;
169 test_compare(TEST_SUCCESS, fetch_all_results(memc, keys_returned));
170 test_compare(HALDENBRAND_KEY_COUNT, keys_returned);
171
172 for (libtest::vchar_ptr_t::iterator iter= keys.begin();
173 iter != keys.end();
174 iter++)
175 {
176 ::free(*iter);
177 }
178
179
180 return TEST_SUCCESS;
181 }
182