Created
November 5, 2014 20:03
-
-
Save dterei/d5f4e1fdbb27bc577c07 to your computer and use it in GitHub Desktop.
Libmemcached SASL authentication test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Test that libmemcached is built with SASL support. | |
*/ | |
#include <stdio.h> | |
#include <libmemcached/memcached.h> | |
const char* key = "abc"; | |
const char* value = "value"; | |
// test basic get/set operation works. | |
void test_getset(memcached_st* cache) | |
{ | |
char* r_value; | |
uint32_t flags = 0; | |
uint32_t r_flags = 0; | |
size_t val_length; | |
memcached_return_t rc; | |
rc = memcached_set(cache, key, strlen(key), value, strlen(value), (time_t)0, flags); | |
if (rc == MEMCACHED_TIMEOUT) { | |
fprintf(stderr, "Set timeout\n"); | |
return; | |
} else if (rc != MEMCACHED_SUCCESS) { | |
fprintf(stderr, "Set failed: %s\n", memcached_strerror(cache, rc)); | |
return; | |
} | |
r_value = memcached_get(cache, key, strlen(key), &val_length, &r_flags, &rc); | |
if (rc == MEMCACHED_TIMEOUT) { | |
fprintf(stderr, "Get timeout\n"); | |
return; | |
} else if (rc != MEMCACHED_SUCCESS) { | |
fprintf(stderr, "Get failed: %s\n", memcached_strerror(cache, rc)); | |
return; | |
} | |
if (strcmp(value, r_value) != 0) { | |
fprintf(stderr, "Get returned bad value! (%s != %s)!\n", value, r_value); | |
} | |
if (r_flags != flags) { | |
fprintf(stderr, "Get returned bad flags! (%u != %u)!\n", flags, r_flags); | |
} | |
fprintf(stdout, "Get/Set success!\n"); | |
} | |
// connect with SASL. | |
void authTest(const char* user, const char* pass, const char* server) | |
{ | |
memcached_server_st *servers = NULL; | |
memcached_return_t rc; | |
memcached_st *cache; | |
cache = memcached_create(NULL); | |
rc = memcached_set_sasl_auth_data(cache, user, pass); | |
if (rc != MEMCACHED_SUCCESS) | |
fprintf(stderr, "Couldn't setup SASL auth: %s\n", memcached_strerror(cache, rc)); | |
rc = memcached_behavior_set(cache, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1); | |
if (rc != MEMCACHED_SUCCESS) | |
fprintf(stderr, "Couldn't use the binary protocol: %s\n", memcached_strerror(cache, rc)); | |
rc = memcached_behavior_set(cache, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, 10000); | |
if (rc != MEMCACHED_SUCCESS) | |
fprintf(stderr, "Couldn't set the connect timeout: %s\n", memcached_strerror(cache, rc)); | |
servers = memcached_server_list_append(servers, server, 11211, &rc); | |
rc = memcached_server_push(cache, servers); | |
if (rc != MEMCACHED_SUCCESS) | |
fprintf(stderr, "Couldn't add server: %s\n", memcached_strerror(cache, rc)); | |
test_getset(cache); | |
memcached_free(cache); | |
} | |
// start program. | |
int main(int argv, char *args[]) | |
{ | |
if (argv != 4) { | |
fprintf(stderr, "ERROR: usage => %s [username] [password] [server]\n", args[0]); | |
return 1; | |
} | |
authTest(args[1], args[2], args[3]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment