+static memcached_return server_function(memcached_st *ptr __attribute__((unused)),
+ memcached_server_st *server __attribute__((unused)),
+ void *context __attribute__((unused)))
+{
+ /* Do Nothing */
+
+ return MEMCACHED_SUCCESS;
+}
+
+static test_return memcached_server_cursor_test(memcached_st *memc)
+{
+ char *context= "foo bad";
+ memcached_server_function callbacks[1];
+
+ callbacks[0]= server_function;
+ memcached_server_cursor(memc, callbacks, context, 1);
+
+ return 0;
+}
+
+static test_return bad_key_test(memcached_st *memc)
+{
+ memcached_return rc;
+ char *key= "foo bad";
+ char *string;
+ size_t string_length;
+ uint32_t flags;
+ memcached_st *clone;
+ unsigned int set= 1;
+ size_t max_keylen= 0xffff;
+
+ clone= memcached_clone(NULL, memc);
+ assert(clone);
+
+ rc= memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ /* All keys are valid in the binary protocol (except for length) */
+ if (memcached_behavior_get(clone, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 0)
+ {
+ string= memcached_get(clone, key, strlen(key),
+ &string_length, &flags, &rc);
+ assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
+ assert(string_length == 0);
+ assert(!string);
+
+ set= 0;
+ rc= memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
+ assert(rc == MEMCACHED_SUCCESS);
+ string= memcached_get(clone, key, strlen(key),
+ &string_length, &flags, &rc);
+ assert(rc == MEMCACHED_NOTFOUND);
+ assert(string_length == 0);
+ assert(!string);
+
+ /* Test multi key for bad keys */
+ char *keys[] = { "GoodKey", "Bad Key", "NotMine" };
+ size_t key_lengths[] = { 7, 7, 7 };
+ set= 1;
+ rc= memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ rc= memcached_mget(clone, keys, key_lengths, 3);
+ assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
+
+ rc= memcached_mget_by_key(clone, "foo daddy", 9, keys, key_lengths, 1);
+ assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
+
+ max_keylen= 250;
+
+ /* The following test should be moved to the end of this function when the
+ memcached server is updated to allow max size length of the keys in the
+ binary protocol
+ */
+ rc= memcached_callback_set(clone, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ char *longkey= malloc(max_keylen + 1);
+ if (longkey != NULL)
+ {
+ memset(longkey, 'a', max_keylen + 1);
+ string= memcached_get(clone, longkey, max_keylen,
+ &string_length, &flags, &rc);
+ assert(rc == MEMCACHED_NOTFOUND);
+ assert(string_length == 0);
+ assert(!string);
+
+ string= memcached_get(clone, longkey, max_keylen + 1,
+ &string_length, &flags, &rc);
+ assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
+ assert(string_length == 0);
+ assert(!string);
+
+ free(longkey);
+ }
+ }
+
+ /* Make sure zero length keys are marked as bad */
+ set= 1;
+ rc= memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
+ assert(rc == MEMCACHED_SUCCESS);
+ string= memcached_get(clone, key, 0,
+ &string_length, &flags, &rc);
+ assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
+ assert(string_length == 0);
+ assert(!string);
+
+ memcached_free(clone);
+
+ return 0;
+}
+
+#define READ_THROUGH_VALUE "set for me"
+static memcached_return read_through_trigger(memcached_st *memc __attribute__((unused)),
+ char *key __attribute__((unused)),
+ size_t key_length __attribute__((unused)),
+ memcached_result_st *result)
+{
+
+ return memcached_result_set_value(result, READ_THROUGH_VALUE, strlen(READ_THROUGH_VALUE));
+}
+
+static test_return read_through(memcached_st *memc)
+{
+ memcached_return rc;
+ char *key= "foo";
+ char *string;
+ size_t string_length;
+ uint32_t flags;
+
+ string= memcached_get(memc, key, strlen(key),
+ &string_length, &flags, &rc);
+
+ assert(rc == MEMCACHED_NOTFOUND);
+ assert(string_length == 0);
+ assert(!string);
+
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_GET_FAILURE, (void *)read_through_trigger);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ string= memcached_get(memc, key, strlen(key),
+ &string_length, &flags, &rc);
+
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(string_length == strlen(READ_THROUGH_VALUE));
+ assert(!strcmp(READ_THROUGH_VALUE, string));
+ free(string);
+
+ string= memcached_get(memc, key, strlen(key),
+ &string_length, &flags, &rc);
+
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(string_length == strlen(READ_THROUGH_VALUE));
+ assert(!strcmp(READ_THROUGH_VALUE, string));
+ free(string);
+
+ return 0;
+}
+
+static memcached_return delete_trigger(memcached_st *ptr __attribute__((unused)),
+ const char *key,
+ size_t key_length __attribute__((unused)))
+{
+ assert(key);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static test_return delete_through(memcached_st *memc)
+{
+ memcached_trigger_delete_key callback;
+ memcached_return rc;
+
+ callback= delete_trigger;
+
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_DELETE_TRIGGER, callback);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ return 0;
+}
+
+static test_return get_test(memcached_st *memc)