Fix server messages (clean up errors in general).
[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 <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_hint(rc == MEMCACHED_BUFFERED or rc == MEMCACHED_NOTFOUND, memcached_last_error_message(memc));
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_got(MEMCACHED_NOTFOUND, rc, memcached_last_error_message(memc));
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_got(MEMCACHED_SUCCESS, rc, memcached_strerror(NULL, rc));
94 test_compare_got(MEMCACHED_SUCCESS, memcached_last_error(memc), memcached_last_error_message(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_hint(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 memcached_last_error_message(memc));
121
122 size_t string_length;
123 uint32_t flags;
124 memcached_return_t rc;
125 char *string= memcached_get(memc,
126 test_literal_param(__func__),
127 &string_length, &flags, &rc);
128
129 test_compare(MEMCACHED_SUCCESS, rc);
130 test_true(string);
131 test_compare(value.size(), string_length);
132 test_memcmp(string, &value[0], string_length);
133
134 free(string);
135
136 return TEST_SUCCESS;
137 }
138
139 test_return_t get_test4(memcached_st *memc)
140 {
141 size_t value_length= 8191;
142
143 libtest::vchar_t value;
144 value.reserve(value_length);
145 for (uint32_t x= 0; x < value_length; x++)
146 {
147 value.push_back(char(x % 127));
148 }
149
150 test_compare_hint(return_value_based_on_buffering(memc),
151 memcached_set(memc,
152 test_literal_param(__func__),
153 &value[0], value.size(),
154 time_t(0), uint32_t(0)),
155 memcached_last_error_message(memc));
156
157 for (uint32_t x= 0; x < 10; x++)
158 {
159 uint32_t flags;
160 size_t string_length;
161 memcached_return_t rc;
162 char *string= memcached_get(memc,
163 test_literal_param(__func__),
164 &string_length, &flags, &rc);
165
166 test_compare(MEMCACHED_SUCCESS, rc);
167 test_true(string);
168 test_compare(value.size(), string_length);
169 test_memcmp(string, &value[0], string_length);
170 free(string);
171 }
172
173 return TEST_SUCCESS;
174 }
175
176 /*
177 * This test verifies that memcached_read_one_response doesn't try to
178 * dereference a NIL-pointer if you issue a multi-get and don't read out all
179 * responses before you execute a storage command.
180 */
181 test_return_t get_test5(memcached_st *memc)
182 {
183 /*
184 ** Request the same key twice, to ensure that we hash to the same server
185 ** (so that we have multiple response values queued up) ;-)
186 */
187 const char *keys[]= { "key", "key" };
188 size_t lengths[]= { 3, 3 };
189 uint32_t flags;
190 size_t rlen;
191
192 test_compare(return_value_based_on_buffering(memc),
193 memcached_set(memc, keys[0], lengths[0],
194 keys[0], lengths[0],
195 time_t(0), uint32_t(0)));
196 test_compare(MEMCACHED_SUCCESS, memcached_mget(memc, keys, lengths, test_array_length(keys)));
197
198 memcached_result_st results_obj;
199 memcached_result_st *results= memcached_result_create(memc, &results_obj);
200 test_true(results);
201
202 memcached_return_t rc;
203 results= memcached_fetch_result(memc, &results_obj, &rc);
204 test_true(results);
205
206 memcached_result_free(&results_obj);
207
208 /* Don't read out the second result, but issue a set instead.. */
209 test_compare(MEMCACHED_SUCCESS, memcached_set(memc, keys[0], lengths[0], keys[0], lengths[0], 0, 0));
210
211 char *val= memcached_get_by_key(memc, keys[0], lengths[0], "yek", 3,
212 &rlen, &flags, &rc);
213 test_false(val);
214 test_compare(MEMCACHED_NOTFOUND, rc);
215 val= memcached_get(memc, keys[0], lengths[0], &rlen, &flags, &rc);
216 test_true(val);
217 test_compare(MEMCACHED_SUCCESS, rc);
218 free(val);
219
220 return TEST_SUCCESS;
221 }