ci: bsds: rebuild packages after reconfiguration
[m6w6/libmemcached] / tests / libmemcached-1.0 / memcached_get.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include "mem_config.h"
38 #include "libtest/test.hpp"
39
40 /*
41 Test cases
42 */
43
44 #include "libmemcached-1.0/memcached.h"
45 #include "tests/libmemcached-1.0/memcached_get.h"
46 #include "tests/libmemcached-1.0/setup_and_teardowns.h"
47
48 test_return_t get_test(memcached_st *memc)
49 {
50 uint64_t query_id= memcached_query_id(memc);
51 memcached_return_t rc= memcached_delete(memc,
52 test_literal_param(__func__),
53 time_t(0));
54 test_true(rc == MEMCACHED_BUFFERED or rc == MEMCACHED_NOTFOUND);
55 test_compare(query_id +1, memcached_query_id(memc));
56
57 size_t string_length;
58 uint32_t flags;
59 char *string= memcached_get(memc,
60 test_literal_param(__func__),
61 &string_length, &flags, &rc);
62
63 test_compare(MEMCACHED_NOTFOUND, rc);
64 test_false(string_length);
65 test_false(string);
66
67 return TEST_SUCCESS;
68 }
69
70 test_return_t get_test2(memcached_st *memc)
71 {
72 const char *value= "when we sanitize";
73
74 uint64_t query_id= memcached_query_id(memc);
75 test_compare(return_value_based_on_buffering(memc),
76 memcached_set(memc,
77 test_literal_param(__func__),
78 value, strlen(value),
79 time_t(0), uint32_t(0)));
80 test_compare(query_id +1, memcached_query_id(memc));
81
82 query_id= memcached_query_id(memc);
83 test_true(query_id);
84
85 uint32_t flags;
86 size_t string_length;
87 memcached_return_t rc;
88 char *string= memcached_get(memc,
89 test_literal_param(__func__),
90 &string_length, &flags, &rc);
91 test_compare(query_id +1, memcached_query_id(memc));
92
93 test_compare(MEMCACHED_SUCCESS, rc);
94 test_compare(MEMCACHED_SUCCESS, memcached_last_error(memc));
95 test_true(string);
96 test_compare(strlen(value), string_length);
97 test_memcmp(string, value, string_length);
98
99 free(string);
100
101 return TEST_SUCCESS;
102 }
103
104 test_return_t get_test3(memcached_st *memc)
105 {
106 size_t value_length= 8191;
107
108 libtest::vchar_t value;
109 value.reserve(value_length);
110 for (uint32_t x= 0; x < value_length; x++)
111 {
112 value.push_back(char(x % 127));
113 }
114
115 test_compare(return_value_based_on_buffering(memc),
116 memcached_set(memc,
117 test_literal_param(__func__),
118 &value[0], value.size(),
119 time_t(0), uint32_t(0)));
120
121 size_t string_length;
122 uint32_t flags;
123 memcached_return_t rc;
124 char *string= memcached_get(memc,
125 test_literal_param(__func__),
126 &string_length, &flags, &rc);
127
128 test_compare(MEMCACHED_SUCCESS, rc);
129 test_true(string);
130 test_compare(value.size(), string_length);
131 test_memcmp(string, &value[0], string_length);
132
133 free(string);
134
135 return TEST_SUCCESS;
136 }
137
138 test_return_t get_test4(memcached_st *memc)
139 {
140 size_t value_length= 8191;
141
142 libtest::vchar_t value;
143 value.reserve(value_length);
144 for (uint32_t x= 0; x < value_length; x++)
145 {
146 value.push_back(char(x % 127));
147 }
148
149 test_compare(return_value_based_on_buffering(memc),
150 memcached_set(memc,
151 test_literal_param(__func__),
152 &value[0], value.size(),
153 time_t(0), uint32_t(0)));
154
155 for (uint32_t x= 0; x < 10; x++)
156 {
157 uint32_t flags;
158 size_t string_length;
159 memcached_return_t rc;
160 char *string= memcached_get(memc,
161 test_literal_param(__func__),
162 &string_length, &flags, &rc);
163
164 test_compare(MEMCACHED_SUCCESS, rc);
165 test_true(string);
166 test_compare(value.size(), string_length);
167 test_memcmp(string, &value[0], string_length);
168 free(string);
169 }
170
171 return TEST_SUCCESS;
172 }
173
174 /*
175 * This test verifies that memcached_read_one_response doesn't try to
176 * dereference a NIL-pointer if you issue a multi-get and don't read out all
177 * responses before you execute a storage command.
178 */
179 test_return_t get_test5(memcached_st *memc)
180 {
181 /*
182 ** Request the same key twice, to ensure that we hash to the same server
183 ** (so that we have multiple response values queued up) ;-)
184 */
185 const char *keys[]= { "key", "key" };
186 size_t lengths[]= { 3, 3 };
187 uint32_t flags;
188 size_t rlen;
189
190 test_compare(return_value_based_on_buffering(memc),
191 memcached_set(memc, keys[0], lengths[0],
192 keys[0], lengths[0],
193 time_t(0), uint32_t(0)));
194 test_compare(MEMCACHED_SUCCESS, memcached_mget(memc, keys, lengths, test_array_length(keys)));
195
196 memcached_result_st results_obj;
197 memcached_result_st *results= memcached_result_create(memc, &results_obj);
198 test_true(results);
199
200 memcached_return_t rc;
201 results= memcached_fetch_result(memc, &results_obj, &rc);
202 test_true(results);
203
204 memcached_result_free(&results_obj);
205
206 /* Don't read out the second result, but issue a set instead.. */
207 test_compare(MEMCACHED_SUCCESS, memcached_set(memc, keys[0], lengths[0], keys[0], lengths[0], 0, 0));
208
209 char *val= memcached_get_by_key(memc, keys[0], lengths[0], "yek", 3,
210 &rlen, &flags, &rc);
211 test_false(val);
212 test_compare(MEMCACHED_NOTFOUND, rc);
213 val= memcached_get(memc, keys[0], lengths[0], &rlen, &flags, &rc);
214 test_true(val);
215 test_compare(MEMCACHED_SUCCESS, rc);
216 free(val);
217
218 return TEST_SUCCESS;
219 }