+ return 0;
+}
+
+static test_return string_alloc_with_size(memcached_st *memc)
+{
+ memcached_string_st *string;
+
+ string= memcached_string_create(memc, NULL, 1024);
+ assert(string);
+ memcached_string_free(string);
+
+ return 0;
+}
+
+static test_return string_alloc_with_size_toobig(memcached_st *memc)
+{
+ memcached_string_st *string;
+
+ string= memcached_string_create(memc, NULL, SIZE_MAX);
+ assert(string == NULL);
+
+ return 0;
+}
+
+static test_return string_alloc_append(memcached_st *memc)
+{
+ unsigned int x;
+ char buffer[SMALL_STRING_LEN];
+ memcached_string_st *string;
+
+ /* Ring the bell! */
+ memset(buffer, 6, SMALL_STRING_LEN);
+
+ string= memcached_string_create(memc, NULL, 100);
+ assert(string);
+
+ for (x= 0; x < 1024; x++)
+ {
+ memcached_return rc;
+ rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
+ assert(rc == MEMCACHED_SUCCESS);
+ }
+ memcached_string_free(string);
+
+ return 0;
+}
+
+static test_return string_alloc_append_toobig(memcached_st *memc)
+{
+ memcached_return rc;
+ unsigned int x;
+ char buffer[SMALL_STRING_LEN];
+ memcached_string_st *string;
+
+ /* Ring the bell! */
+ memset(buffer, 6, SMALL_STRING_LEN);
+
+ string= memcached_string_create(memc, NULL, 100);
+ assert(string);
+
+ for (x= 0; x < 1024; x++)
+ {
+ rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
+ assert(rc == MEMCACHED_SUCCESS);
+ }
+ rc= memcached_string_append(string, buffer, SIZE_MAX);
+ assert(rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE);
+ memcached_string_free(string);
+
+ return 0;
+}
+
+static test_return cleanup_pairs(memcached_st *memc __attribute__((unused)))
+{
+ pairs_free(global_pairs);
+
+ return 0;
+}
+
+static test_return generate_pairs(memcached_st *memc __attribute__((unused)))
+{
+ unsigned long long x;
+ global_pairs= pairs_generate(GLOBAL_COUNT, 400);
+ global_count= GLOBAL_COUNT;
+
+ for (x= 0; x < global_count; x++)
+ {
+ global_keys[x]= global_pairs[x].key;
+ global_keys_length[x]= global_pairs[x].key_length;
+ }
+
+ return 0;
+}
+
+static test_return generate_large_pairs(memcached_st *memc __attribute__((unused)))
+{
+ unsigned long long x;
+ global_pairs= pairs_generate(GLOBAL2_COUNT, MEMCACHED_MAX_BUFFER+10);
+ global_count= GLOBAL2_COUNT;
+
+ for (x= 0; x < global_count; x++)
+ {
+ global_keys[x]= global_pairs[x].key;
+ global_keys_length[x]= global_pairs[x].key_length;
+ }
+
+ return 0;
+}
+
+static test_return generate_data(memcached_st *memc)
+{
+ execute_set(memc, global_pairs, global_count);
+
+ return 0;
+}
+
+static test_return generate_data_with_stats(memcached_st *memc)
+{
+ memcached_stat_st *stat_p;
+ memcached_return rc;
+ uint32_t host_index= 0;
+ execute_set(memc, global_pairs, global_count);
+
+ //TODO: hosts used size stats
+ stat_p= memcached_stat(memc, NULL, &rc);
+ assert(stat_p);
+
+ for (host_index= 0; host_index < SERVERS_TO_CREATE; host_index++)
+ {
+ /* This test was changes so that "make test" would work properlly */
+#ifdef DEBUG
+ printf("\nserver %u|%s|%u bytes: %llu\n", host_index, (memc->hosts)[host_index].hostname, (memc->hosts)[host_index].port, (unsigned long long)(stat_p + host_index)->bytes);
+#endif
+ assert((unsigned long long)(stat_p + host_index)->bytes);
+ }
+
+ memcached_stat_free(NULL, stat_p);
+
+ return 0;
+}
+static test_return generate_buffer_data(memcached_st *memc)
+{
+ size_t latch= 0;
+
+ latch= 1;
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, latch);
+ generate_data(memc);
+
+ return 0;
+}
+
+static test_return get_read_count(memcached_st *memc)
+{
+ unsigned int x;
+ memcached_return rc;
+ memcached_st *memc_clone;
+
+ memc_clone= memcached_clone(NULL, memc);
+ assert(memc_clone);
+
+ memcached_server_add_with_weight(memc_clone, "localhost", 6666, 0);
+
+ {
+ char *return_value;
+ size_t return_value_length;
+ uint32_t flags;
+ uint32_t count;
+
+ for (x= count= 0; x < global_count; x++)
+ {
+ return_value= memcached_get(memc_clone, global_keys[x], global_keys_length[x],
+ &return_value_length, &flags, &rc);
+ if (rc == MEMCACHED_SUCCESS)
+ {
+ count++;
+ if (return_value)
+ free(return_value);
+ }
+ }
+ fprintf(stderr, "\t%u -> %u", global_count, count);
+ }
+
+ memcached_free(memc_clone);
+
+ return 0;
+}
+
+static test_return get_read(memcached_st *memc)
+{
+ unsigned int x;
+ memcached_return rc;
+
+ {
+ char *return_value;
+ size_t return_value_length;
+ uint32_t flags;
+
+ for (x= 0; x < global_count; x++)
+ {
+ return_value= memcached_get(memc, global_keys[x], global_keys_length[x],
+ &return_value_length, &flags, &rc);
+ /*
+ assert(return_value);
+ assert(rc == MEMCACHED_SUCCESS);
+ */
+ if (rc == MEMCACHED_SUCCESS && return_value)
+ free(return_value);
+ }
+ }
+
+ return 0;
+}
+
+static test_return mget_read(memcached_st *memc)
+{
+ memcached_return rc;
+
+ rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
+ assert(rc == MEMCACHED_SUCCESS);
+ /* Turn this into a help function */
+ {
+ char return_key[MEMCACHED_MAX_KEY];
+ size_t return_key_length;
+ char *return_value;
+ size_t return_value_length;
+ uint32_t flags;
+
+ while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
+ &return_value_length, &flags, &rc)))
+ {
+ assert(return_value);
+ assert(rc == MEMCACHED_SUCCESS);
+ free(return_value);
+ }
+ }
+
+ return 0;
+}
+
+static test_return mget_read_result(memcached_st *memc)
+{
+ memcached_return rc;
+
+ rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
+ assert(rc == MEMCACHED_SUCCESS);
+ /* Turn this into a help function */
+ {
+ memcached_result_st results_obj;
+ memcached_result_st *results;
+
+ results= memcached_result_create(memc, &results_obj);
+
+ while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
+ {
+ assert(results);
+ assert(rc == MEMCACHED_SUCCESS);
+ }
+
+ memcached_result_free(&results_obj);
+ }
+
+ return 0;
+}
+
+static test_return mget_read_function(memcached_st *memc)
+{
+ memcached_return rc;
+ unsigned int counter;
+ memcached_execute_function callbacks[1];
+
+ rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ callbacks[0]= &callback_counter;
+ counter= 0;
+ rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
+
+ return 0;
+}
+
+static test_return delete_generate(memcached_st *memc)
+{
+ unsigned int x;
+
+ for (x= 0; x < global_count; x++)
+ {
+ (void)memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0);
+ }
+
+ return 0;
+}
+
+static test_return delete_buffer_generate(memcached_st *memc)
+{
+ size_t latch= 0;
+ unsigned int x;
+
+ latch= 1;
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, latch);
+
+ for (x= 0; x < global_count; x++)
+ {
+ (void)memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0);
+ }
+
+ return 0;
+}
+
+static test_return add_host_test1(memcached_st *memc)
+{
+ unsigned int x;
+ memcached_return rc;
+ char servername[]= "0.example.com";
+ memcached_server_st *servers;
+
+ servers= memcached_server_list_append_with_weight(NULL, servername, 400, 0, &rc);
+ assert(servers);
+ assert(1 == memcached_server_list_count(servers));
+
+ for (x= 2; x < 20; x++)
+ {
+ char buffer[SMALL_STRING_LEN];
+
+ snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
+ servers= memcached_server_list_append_with_weight(servers, buffer, 401, 0,
+ &rc);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(x == memcached_server_list_count(servers));
+ }
+
+ rc= memcached_server_push(memc, servers);
+ assert(rc == MEMCACHED_SUCCESS);
+ rc= memcached_server_push(memc, servers);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ memcached_server_list_free(servers);
+
+ return 0;
+}
+
+static memcached_return pre_nonblock(memcached_st *memc)
+{
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return pre_nonblock_binary(memcached_st *memc)
+{
+ memcached_return rc= MEMCACHED_FAILURE;
+ memcached_st *memc_clone;
+
+ memc_clone= memcached_clone(NULL, memc);
+ assert(memc_clone);
+ // The memcached_version needs to be done on a clone, because the server
+ // will not toggle protocol on an connection.
+ memcached_version(memc_clone);
+
+ if (memc_clone->hosts[0].major_version >= 1 && memc_clone->hosts[0].minor_version > 2)
+ {
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
+ rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1);
+ }
+
+ memcached_free(memc_clone);
+ return rc;
+}
+
+static memcached_return pre_murmur(memcached_st *memc)
+{
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MURMUR);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return pre_jenkins(memcached_st *memc)
+{
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_JENKINS);
+
+ return MEMCACHED_SUCCESS;
+}
+
+
+static memcached_return pre_md5(memcached_st *memc)
+{
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MD5);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return pre_crc(memcached_st *memc)
+{
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_CRC);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return pre_hsieh(memcached_st *memc)
+{
+#ifdef HAVE_HSIEH_HASH
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_HSIEH);
+ return MEMCACHED_SUCCESS;
+#else
+ (void) memc;
+ return MEMCACHED_FAILURE;
+#endif
+}
+
+static memcached_return pre_hash_fnv1_64(memcached_st *memc)
+{
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1_64);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return pre_hash_fnv1a_64(memcached_st *memc)
+{
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1A_64);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return pre_hash_fnv1_32(memcached_st *memc)
+{
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1_32);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return pre_hash_fnv1a_32(memcached_st *memc)
+{
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1A_32);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return pre_behavior_ketama(memcached_st *memc)
+{
+ memcached_return rc;
+ uint64_t value;
+
+ rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA, 1);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA);
+ assert(value == 1);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return pre_behavior_ketama_weighted(memcached_st *memc)
+{
+ memcached_return rc;
+ uint64_t value;
+
+ rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
+ assert(value == 1);
+
+ rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_MD5);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH);
+ assert(value == MEMCACHED_HASH_MD5);
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return pre_binary(memcached_st *memc)
+{
+ memcached_return rc= MEMCACHED_FAILURE;
+ memcached_st *memc_clone;
+
+ memc_clone= memcached_clone(NULL, memc);
+ assert(memc_clone);
+ // The memcached_version needs to be done on a clone, because the server
+ // will not toggle protocol on an connection.
+ memcached_version(memc_clone);
+
+ if (memc_clone->hosts[0].major_version >= 1 && memc_clone->hosts[0].minor_version > 2)
+ {
+ rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1);
+ }
+
+ memcached_free(memc_clone);
+ return rc;
+}
+
+static void my_free(memcached_st *ptr __attribute__((unused)), void *mem)
+{
+ free(mem);
+}
+
+static void *my_malloc(memcached_st *ptr __attribute__((unused)), const size_t size)
+{
+ void *ret= malloc(size);
+ if (ret != NULL)
+ memset(ret, 0xff, size);
+
+ return ret;
+}
+
+static void *my_realloc(memcached_st *ptr __attribute__((unused)), void *mem, const size_t size)
+{
+ return realloc(mem, size);
+}
+
+static void *my_calloc(memcached_st *ptr __attribute__((unused)), size_t nelem, const size_t size)
+{
+ return calloc(nelem, size);
+}
+
+static memcached_return set_prefix(memcached_st *memc)
+{
+ memcached_return rc;
+ const char *key= "mine";
+ char *value;
+
+ /* Make sure be default none exists */
+ value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
+ assert(rc == MEMCACHED_FAILURE);
+
+ /* Test a clean set */
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, (void *)key);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
+ assert(memcmp(value, key, 4) == 0);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ /* Test that we can turn it off */
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
+ assert(rc == MEMCACHED_FAILURE);
+
+ /* Now setup for main test */
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, (void *)key);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(memcmp(value, key, 4) == 0);
+
+ /* Set to Zero, and then Set to something too large */
+ {
+ char *long_key;
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
+ assert(rc == MEMCACHED_FAILURE);
+ assert(value == NULL);
+
+ /* Test a long key for failure */
+ /* TODO, extend test to determine based on setting, what result should be */
+ long_key= "Thisismorethentheallottednumberofcharacters";
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
+ //assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
+ long_key= "This is more then the allotted number of characters";
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
+ assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
+
+ /* Test for a bad prefix, but with a short key */
+ rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_VERIFY_KEY, 1);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ long_key= "dog cat";
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
+ assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
+ }
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return deprecated_set_memory_alloc(memcached_st *memc)
+{
+ void *test_ptr= NULL;
+ void *cb_ptr= NULL;
+ {
+ memcached_malloc_function malloc_cb=
+ (memcached_malloc_function)my_malloc;
+ cb_ptr= *(void **)&malloc_cb;
+ memcached_return rc;
+
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, cb_ptr);
+ assert(rc == MEMCACHED_SUCCESS);
+ test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, &rc);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(test_ptr == cb_ptr);
+ }
+
+ {
+ memcached_realloc_function realloc_cb=
+ (memcached_realloc_function)my_realloc;
+ cb_ptr= *(void **)&realloc_cb;
+ memcached_return rc;
+
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, cb_ptr);
+ assert(rc == MEMCACHED_SUCCESS);
+ test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, &rc);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(test_ptr == cb_ptr);
+ }
+
+ {
+ memcached_free_function free_cb=
+ (memcached_free_function)my_free;
+ cb_ptr= *(void **)&free_cb;
+ memcached_return rc;
+
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, cb_ptr);
+ assert(rc == MEMCACHED_SUCCESS);
+ test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, &rc);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(test_ptr == cb_ptr);
+ }
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return set_memory_alloc(memcached_st *memc)
+{
+ memcached_return rc;
+ rc= memcached_set_memory_allocators(memc, NULL, my_free,
+ my_realloc, my_calloc);
+ assert(rc == MEMCACHED_FAILURE);
+
+ rc= memcached_set_memory_allocators(memc, my_malloc, my_free,
+ my_realloc, my_calloc);
+
+ memcached_malloc_function mem_malloc;
+ memcached_free_function mem_free;
+ memcached_realloc_function mem_realloc;
+ memcached_calloc_function mem_calloc;
+ memcached_get_memory_allocators(memc, &mem_malloc, &mem_free,
+ &mem_realloc, &mem_calloc);
+
+ assert(mem_malloc == my_malloc);
+ assert(mem_realloc == my_realloc);
+ assert(mem_calloc == my_calloc);
+ assert(mem_free == my_free);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return enable_consistent(memcached_st *memc)
+{
+ memcached_server_distribution value= MEMCACHED_DISTRIBUTION_CONSISTENT;
+ memcached_hash hash;
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, value);
+ if (pre_hsieh(memc) != MEMCACHED_SUCCESS)
+ return MEMCACHED_FAILURE;
+
+ value= (memcached_server_distribution)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION);
+ assert(value == MEMCACHED_DISTRIBUTION_CONSISTENT);
+
+ hash= (memcached_hash)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
+ assert(hash == MEMCACHED_HASH_HSIEH);
+
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return enable_cas(memcached_st *memc)
+{
+ unsigned int set= 1;
+
+ memcached_version(memc);
+
+ if ((memc->hosts[0].major_version >= 1 && (memc->hosts[0].minor_version == 2 && memc->hosts[0].micro_version >= 4))
+ || memc->hosts[0].minor_version > 2)
+ {
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, set);
+
+ return MEMCACHED_SUCCESS;
+ }
+
+ return MEMCACHED_FAILURE;
+}
+
+static memcached_return check_for_1_2_3(memcached_st *memc)
+{
+ memcached_version(memc);
+
+ if ((memc->hosts[0].major_version >= 1 && (memc->hosts[0].minor_version == 2 && memc->hosts[0].micro_version >= 4))
+ || memc->hosts[0].minor_version > 2)
+ return MEMCACHED_SUCCESS;
+
+ return MEMCACHED_FAILURE;
+}
+
+static memcached_return pre_unix_socket(memcached_st *memc)
+{
+ memcached_return rc;
+ struct stat buf;
+
+ memcached_server_list_free(memc->hosts);
+ memc->hosts= NULL;
+ memc->number_of_hosts= 0;
+
+ if (stat("/tmp/memcached.socket", &buf))
+ return MEMCACHED_FAILURE;
+
+ rc= memcached_server_add_unix_socket_with_weight(memc, "/tmp/memcached.socket", 0);
+
+ return rc;
+}
+
+static memcached_return pre_nodelay(memcached_st *memc)
+{
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 0);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return pre_settimer(memcached_st *memc)
+{
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SND_TIMEOUT, 1000);
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_RCV_TIMEOUT, 1000);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return poll_timeout(memcached_st *memc)
+{
+ size_t timeout;
+
+ timeout= 100;
+
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, timeout);
+
+ timeout= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT);
+
+ assert(timeout == 100);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static test_return noreply_test(memcached_st *memc)
+{
+ memcached_return ret;
+ ret= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 1);
+ assert(ret == MEMCACHED_SUCCESS);
+ ret= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
+ assert(ret == MEMCACHED_SUCCESS);
+ ret= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, 1);
+ assert(ret == MEMCACHED_SUCCESS);
+ assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NOREPLY) == 1);
+ assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS) == 1);
+ assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS) == 1);
+
+ for (int count=0; count < 5; ++count)
+ {
+ for (int x=0; x < 100; ++x)
+ {
+ char key[10];
+ size_t len= (size_t)sprintf(key, "%d", x);
+ switch (count)
+ {
+ case 0:
+ ret=memcached_add(memc, key, len, key, len, 0, 0);
+ break;
+ case 1:
+ ret=memcached_replace(memc, key, len, key, len, 0, 0);
+ break;
+ case 2:
+ ret=memcached_set(memc, key, len, key, len, 0, 0);
+ break;
+ case 3:
+ ret=memcached_append(memc, key, len, key, len, 0, 0);
+ break;
+ case 4:
+ ret=memcached_prepend(memc, key, len, key, len, 0, 0);
+ break;
+ default:
+ assert(count);
+ break;
+ }
+ assert(ret == MEMCACHED_SUCCESS || ret == MEMCACHED_BUFFERED);
+ }
+
+ /*
+ ** NOTE: Don't ever do this in your code! this is not a supported use of the
+ ** API and is _ONLY_ done this way to verify that the library works the
+ ** way it is supposed to do!!!!
+ */
+ int no_msg=0;
+ for (uint32_t x=0; x < memc->number_of_hosts; ++x)
+ no_msg+=(int)(memc->hosts[x].cursor_active);
+
+ assert(no_msg == 0);
+ assert(memcached_flush_buffers(memc) == MEMCACHED_SUCCESS);
+
+ /*
+ ** Now validate that all items was set properly!
+ */
+ for (int x=0; x < 100; ++x)
+ {
+ char key[10];
+ size_t len= (size_t)sprintf(key, "%d", x);
+ size_t length;
+ uint32_t flags;
+ char* value=memcached_get(memc, key, strlen(key),
+ &length, &flags, &ret);
+ assert(ret == MEMCACHED_SUCCESS && value != NULL);
+ switch (count)
+ {
+ case 0: /* FALLTHROUGH */
+ case 1: /* FALLTHROUGH */
+ case 2:
+ assert(strncmp(value, key, len) == 0);
+ assert(len == length);
+ break;
+ case 3:
+ assert(length == len * 2);
+ break;
+ case 4:
+ assert(length == len * 3);
+ break;
+ default:
+ assert(count);
+ break;
+ }
+ free(value);
+ }
+ }
+
+ /* Try setting an illegal cas value (should not return an error to
+ * the caller (because we don't expect a return message from the server)
+ */
+ char* keys[]= {"0"};
+ size_t lengths[]= {1};
+ size_t length;
+ uint32_t flags;
+ memcached_result_st results_obj;
+ memcached_result_st *results;
+ ret= memcached_mget(memc, keys, lengths, 1);
+ assert(ret == MEMCACHED_SUCCESS);
+
+ results= memcached_result_create(memc, &results_obj);
+ assert(results);
+ results= memcached_fetch_result(memc, &results_obj, &ret);
+ assert(results);
+ assert(ret == MEMCACHED_SUCCESS);
+ uint64_t cas= memcached_result_cas(results);
+ memcached_result_free(&results_obj);
+
+ ret= memcached_cas(memc, keys[0], lengths[0], keys[0], lengths[0], 0, 0, cas);
+ assert(ret == MEMCACHED_SUCCESS);
+
+ /*
+ * The item will have a new cas value, so try to set it again with the old
+ * value. This should fail!
+ */
+ ret= memcached_cas(memc, keys[0], lengths[0], keys[0], lengths[0], 0, 0, cas);
+ assert(ret == MEMCACHED_SUCCESS);
+ assert(memcached_flush_buffers(memc) == MEMCACHED_SUCCESS);
+ char* value=memcached_get(memc, keys[0], lengths[0], &length, &flags, &ret);
+ assert(ret == MEMCACHED_SUCCESS && value != NULL);
+ free(value);
+
+ return TEST_SUCCESS;
+}
+
+static test_return analyzer_test(memcached_st *memc)
+{
+ memcached_return rc;
+ memcached_stat_st *memc_stat;
+ memcached_analysis_st *report;
+
+ memc_stat= memcached_stat(memc, NULL, &rc);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(memc_stat);
+
+ report= memcached_analyze(memc, memc_stat, &rc);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(report);
+
+ free(report);
+ memcached_stat_free(NULL, memc_stat);
+
+ return TEST_SUCCESS;
+}
+
+/* Count the objects */
+static memcached_return callback_dump_counter(memcached_st *ptr __attribute__((unused)),
+ const char *key __attribute__((unused)),
+ size_t key_length __attribute__((unused)),
+ void *context)
+{
+ uint32_t *counter= (uint32_t *)context;
+
+ *counter= *counter + 1;
+
+ return MEMCACHED_SUCCESS;
+}
+
+static test_return dump_test(memcached_st *memc)
+{
+ memcached_return rc;
+ uint32_t counter= 0;
+ memcached_dump_func callbacks[1];
+ test_return main_rc;
+
+ callbacks[0]= &callback_dump_counter;
+
+ /* No support for Binary protocol yet */
+ if (memc->flags & MEM_BINARY_PROTOCOL)
+ return TEST_SUCCESS;
+
+ main_rc= set_test3(memc);
+
+ assert (main_rc == TEST_SUCCESS);
+
+ rc= memcached_dump(memc, callbacks, (void *)&counter, 1);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ /* We may have more then 32 if our previous flush has not completed */
+ assert(counter >= 32);
+
+ return TEST_SUCCESS;
+}
+
+#ifdef HAVE_LIBMEMCACHEDUTIL
+static void* connection_release(void *arg) {
+ struct {
+ memcached_pool_st* pool;
+ memcached_st* mmc;
+ } *resource= arg;
+
+ usleep(250);
+ assert(memcached_pool_push(resource->pool, resource->mmc) == MEMCACHED_SUCCESS);
+ return arg;