#include "mem_config.h"
-#ifdef HAVE_POLL_H
-# include <poll.h>
-#else
-# include "poll/poll.h"
-#endif
-
#include <cassert>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
-#include <ctype.h>
+#include <cctype>
#include <fcntl.h>
-#include <inttypes.h>
-#include <pthread.h>
-#include <signal.h>
+#include <cinttypes>
+#include <ciso646>
+#include <csignal>
#include <sys/types.h>
-#include <unistd.h>
+#if HAVE_UNISTD_H
+# include <unistd.h>
+#endif
-#include "libmemcached-1.0/memcached.h"
+#include "p9y/getopt.hpp"
+#include "p9y/socket.hpp"
+#include "p9y/poll.hpp"
-#include "libmemcached/socket.hpp"
+#include "libmemcached-1.0/memcached.h"
#include "libmemcachedprotocol-0.0/binary.h"
#include "libmemcached/byteorder.h"
-#include "utilities.h"
#include <vector>
hints.ai_socktype = SOCK_STREAM;
int error = getaddrinfo(hostname, port, &hints, &ai);
- if (error != 0) {
+ if (error) {
if (error != EAI_SYSTEM) {
fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(error));
} else {
int flags = fcntl(sock, F_GETFL, 0);
if (flags == -1) {
perror("Failed to get socket flags");
- memcached_close_socket(sock);
+ closesocket(sock);
return INVALID_SOCKET;
}
if ((flags & O_NONBLOCK) != O_NONBLOCK) {
if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1) {
perror("Failed to set socket to nonblocking mode");
- memcached_close_socket(sock);
+ closesocket(sock);
return INVALID_SOCKET;
}
}
static memcached_socket_t connect_server(const char *hostname, const char *port) {
struct addrinfo *ai = lookuphost(hostname, port);
sock = INVALID_SOCKET;
- if (ai != NULL) {
+ if (ai) {
if ((sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) != INVALID_SOCKET) {
if (connect(sock, ai->ai_addr, ai->ai_addrlen) == SOCKET_ERROR) {
fprintf(stderr, "Failed to connect socket: %s\n", strerror(get_socket_errno()));
return sock;
}
-static ssize_t timeout_io_op(memcached_socket_t fd, short direction, void *buf, size_t len) {
+static ssize_t timeout_io_op(memcached_socket_t fd, short direction, const char *buf, size_t len) {
ssize_t ret;
if (direction == POLLOUT) {
ret = send(fd, buf, len, 0);
} else {
- ret = recv(fd, buf, len, 0);
+ ret = recv(fd, const_cast<char *>(buf), len, 0);
}
-
- if (ret == SOCKET_ERROR && get_socket_errno() == EWOULDBLOCK) {
+ int local_errno = get_socket_errno();
+ if (ret == SOCKET_ERROR && local_errno == EWOULDBLOCK || (EAGAIN != EWOULDBLOCK && local_errno == EAGAIN)) {
struct pollfd fds;
memset(&fds, 0, sizeof(struct pollfd));
fds.events = direction;
if (direction == POLLOUT) {
ret = send(fd, buf, len, 0);
} else {
- ret = recv(fd, buf, len, 0);
+ ret = recv(fd, const_cast<char *>(buf), len, 0);
}
} else if (err == 0) {
errno = ETIMEDOUT;
do {
size_t num_bytes = len - offset;
- ssize_t nw = timeout_io_op(sock, POLLOUT, (void *) (ptr + offset), num_bytes);
+ ssize_t nw = timeout_io_op(sock, POLLOUT, (ptr + offset), num_bytes);
if (nw == -1) {
verify(get_socket_errno() == EINTR || get_socket_errno() == EAGAIN);
} else {
off_t key_offset = sizeof(protocol_binary_request_no_extras) + 8;
memcpy(cmd->bytes + key_offset, key, keylen);
- if (dta != NULL)
+ if (dta)
memcpy(cmd->bytes + key_offset + keylen, dta, dtalen);
}
off_t key_offset = sizeof(protocol_binary_request_no_extras);
- if (key != NULL)
+ if (key)
memcpy(cmd->bytes + key_offset, key, keylen);
- if (dta != NULL)
+ if (dta)
memcpy(cmd->bytes + key_offset + keylen, dta, dtalen);
}
cmd->flush.message.header.request.opcode = cc;
cmd->flush.message.header.request.opaque = 0xdeadbeef;
- if (exptime != 0 || use_extra) {
+ if (exptime || use_extra) {
cmd->flush.message.header.request.extlen = 4;
cmd->flush.message.body.expiration = htonl(exptime);
cmd->flush.message.header.request.bodylen = 4;
verify(rsp->plain.message.header.response.keylen == 0);
verify(rsp->plain.message.header.response.extlen == 0);
verify(rsp->plain.message.header.response.bodylen == 0);
- verify(rsp->plain.message.header.response.cas != 0);
+ verify(rsp->plain.message.header.response.cas);
break;
case PROTOCOL_BINARY_CMD_FLUSH:
case PROTOCOL_BINARY_CMD_NOOP:
verify(rsp->plain.message.header.response.keylen == 0);
verify(rsp->plain.message.header.response.extlen == 0);
verify(rsp->plain.message.header.response.bodylen == 8);
- verify(rsp->plain.message.header.response.cas != 0);
+ verify(rsp->plain.message.header.response.cas);
break;
case PROTOCOL_BINARY_CMD_STAT:
case PROTOCOL_BINARY_CMD_VERSION:
verify(rsp->plain.message.header.response.keylen == 0);
verify(rsp->plain.message.header.response.extlen == 0);
- verify(rsp->plain.message.header.response.bodylen != 0);
+ verify(rsp->plain.message.header.response.bodylen);
verify(rsp->plain.message.header.response.cas == 0);
break;
case PROTOCOL_BINARY_CMD_GETQ:
verify(rsp->plain.message.header.response.keylen == 0);
verify(rsp->plain.message.header.response.extlen == 4);
- verify(rsp->plain.message.header.response.cas != 0);
+ verify(rsp->plain.message.header.response.cas);
break;
case PROTOCOL_BINARY_CMD_GETK:
case PROTOCOL_BINARY_CMD_GETKQ:
- verify(rsp->plain.message.header.response.keylen != 0);
+ verify(rsp->plain.message.header.response.keylen);
verify(rsp->plain.message.header.response.extlen == 4);
- verify(rsp->plain.message.header.response.cas != 0);
+ verify(rsp->plain.message.header.response.cas);
break;
default:
execute(recv_packet(&rsp));
verify(
validate_response_header(&rsp, PROTOCOL_BINARY_CMD_STAT, PROTOCOL_BINARY_RESPONSE_SUCCESS));
- } while (rsp.plain.message.header.response.keylen != 0);
+ } while (rsp.plain.message.header.response.keylen);
return TEST_PASS;
}
static enum test_return receive_response(const char *msg) {
char buffer[80];
execute(receive_line(buffer, sizeof(buffer)));
- if (strcmp(msg, buffer) != 0) {
+ if (strcmp(msg, buffer)) {
fprintf(stderr, "[%s]\n", buffer);
}
verify(strcmp(msg, buffer) == 0);
execute(receive_line(buffer, sizeof(buffer)));
verify(strncmp(buffer, "VALUE ", 6) == 0);
char *end = strchr(buffer + 6, ' ');
- verify(end != NULL);
+ verify(end);
if (end) {
*end = '\0';
}
*key = strdup(buffer + 6);
- verify(*key != NULL);
+ verify(*key);
char *ptr = end + 1;
errno = 0;
verify(errno == 0);
verify(ptr != end);
verify(val == 0);
- verify(end != NULL);
+ verify(end);
errno = 0;
*ndata = (ssize_t) strtoul(end, &end, 10); /* size */
verify(errno == 0);
verify(ptr != end);
- verify(end != NULL);
+ verify(end);
while (end and *end != '\n' and isspace(*end)) ++end;
verify(end and *end == '\n');
*value = static_cast<char *>(malloc((size_t) *ndata));
- verify(*value != NULL);
+ verify(*value);
execute(retry_read(*value, (size_t) *ndata));
verify(errno == 0);
verify(ptr != end);
verify(val == 0);
- verify(end != NULL);
+ verify(end);
errno = 0;
val = strtoul(end, &end, 10); /* size */
verify(errno == 0);
verify(ptr != end);
verify(val == datasize);
- verify(end != NULL);
+ verify(end);
while (end and *end != '\n' and isspace(*end)) {
++end;
}
static enum test_return ascii_get_item(const char *key, const char *value, bool exist) {
char buffer[1024];
size_t datasize = 0;
- if (value != NULL) {
+ if (value) {
datasize = strlen(value);
}
verify(errno == 0);
verify(ptr != end);
verify(val == 0);
- verify(end != NULL);
+ verify(end);
errno = 0;
val = strtoul(end, &end, 10); /* size */
verify(errno == 0);
verify(ptr != end);
verify(val == datasize);
- verify(end != NULL);
+ verify(end);
errno = 0;
*cas = strtoul(end, &end, 10); /* cas */
verify(errno == 0);
verify(ptr != end);
verify(val == datasize);
- verify(end != NULL);
+ verify(end);
while (end and *end != '\n' and isspace(*end)) {
++end;
unsigned long *cas) {
char buffer[1024];
size_t datasize = 0;
- if (value != NULL) {
+ if (value) {
datasize = strlen(value);
}
char buffer[1024];
do {
execute(receive_line(buffer, sizeof(buffer)));
- } while (strcmp(buffer, "END\r\n") != 0);
+ } while (strcmp(buffer, "END\r\n"));
return TEST_PASS_RECONNECT;
}
break;
case 'q':
- close_stdio();
+ //close_stdio();
break;
case 'P':
return EXIT_FAILURE;
}
- initialize_sockets();
+ #ifdef _WIN32
+ WSADATA wsaData;
+ if (WSAStartup(MAKEWORD(2, 2), &wsaData)) {
+ fprintf(stderr, "Socket Initialization Error.\n");
+ return EXIT_FAILURE;
+ }
+#endif // _WIN32
+
sock = connect_server(hostname, port);
if (sock == INVALID_SOCKET) {
- fprintf(stderr, "Failed to connect to <%s:%s>: %s\n", hostname ?: "(null)", port ?: "(null)",
+ fprintf(stderr, "Failed to connect to <%s:%s>: %s\n", hostname, port,
strerror(get_socket_errno()));
return EXIT_FAILURE;
}
- for (int ii = 0; testcases[ii].description != NULL; ++ii) {
- if (testname != NULL && strcmp(testcases[ii].description, testname) != 0) {
+ for (int ii = 0; testcases[ii].description; ++ii) {
+ if (testname && strcmp(testcases[ii].description, testname)) {
continue;
}
if (prompt) {
fprintf(stdout, "\nPress <return> when you are ready? ");
char buffer[80] = {0};
- if (fgets(buffer, sizeof(buffer), stdin) != NULL) {
+ if (fgets(buffer, sizeof(buffer), stdin)) {
if (strncmp(buffer, "skip", 4) == 0) {
fprintf(stdout, "%-40s%s\n", testcases[ii].description, status_msg[TEST_SKIP]);
fflush(stdout);
if (reconnect) {
closesocket(sock);
if ((sock = connect_server(hostname, port)) == INVALID_SOCKET) {
- fprintf(stderr, "Failed to connect to <%s:%s>: %s\n", hostname ?: "(null)",
- port ?: "(null)", strerror(get_socket_errno()));
+ fprintf(stderr, "Failed to connect to <%s:%s>: %s\n", hostname,
+ port, strerror(get_socket_errno()));
fprintf(stderr, "%d of %d tests failed\n", failed, total);
return EXIT_FAILURE;
}
fprintf(stderr, "%d of %d tests failed\n", failed, total);
}
+#ifdef _WIN32
+ WSACleanup();
+#endif
+
return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}