Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

make test more portable (for windows)

Change-Id: I94dfe679df6ab1d746823a729b965fec27a931ff
Reviewed-on: http://review.couchbase.org/13197
Tested-by: Sergey Avseyev <sergey.avseyev@gmail.com>
Reviewed-by: Trond Norbye <trond.norbye@gmail.com>
  • Loading branch information...
commit 463f12dcea0aaf83e8481be4fe2c150720c8e8cb 1 parent 10210c3
@avsej avsej authored trondn committed
View
19 NMakefile
@@ -16,14 +16,14 @@
CC_NODEBUG = cl.exe /O2 /DNDEBUG /MD /LD
CC_DEBUG = cl.exe /Od /D_DEBUG /RTC1 /Z7 /LDd /W3 /MDd
-CFLAGS = /Iinclude /Iwin32 /nologo /W3 /EHsc /DWIN32 /FD /c -D_CRT_SECURE_NO_WARNINGS -DBUILDING_LIBVBUCKET=1
+CFLAGS = /Iinclude /I. /Iwin32 /nologo /W3 /EHsc /DWIN32 /FD /c -D_CRT_SECURE_NO_WARNINGS -DBUILDING_LIBVBUCKET=1
LDFLAGS = /nologo /dll /incremental:no /opt:ref,icf /version:1.0
LD = link.exe
RM = del
-libvbucket_SOURCES= src\crc32.c src\cJSON.c src\vbucket.c src/ketama.c
+libvbucket_SOURCES= src\crc32.c src\cJSON.c src\vbucket.c src\ketama.c src\rfc1321\md5c.c
vbucketkeygen_SOURCES = src\vbucketkeygen.c
-testketama_SOURCES = tests\testketama.c
+testketama_SOURCES = tests\testketama.c src\ketama.c src\rfc1321\md5c.c
regression_SOURCES = tests\regression.c
testapp_SOURCES = tests\testapp.c
vbuckettool_SOURCES=src\vbuckettool.c
@@ -34,11 +34,11 @@ vbuckettool_SOURCES=src\vbuckettool.c
libvbucket_la_OBJECTS=$(libvbucket_SOURCES:.c=.obj)
libvbucket_la_debug_OBJECTS=$(libvbucket_SOURCES:.c=.dobj)
-vbucketkeygen_OBJECTS=$(vbuckettool_SOURCES:.c=.obj)
-testketama_OBJECTS=$(testketama_SOURCES:.c=.obj)
-regression_OBJECTS=$(regression_SOURCES:.c=.obj)
-testapp_OBJECTS=$(testapp_SOURCES:.c=.obj)
-vbuckettool_OBJECTS=$(vbuckettool_SOURCES:.c=.obj)
+vbucketkeygen_OBJECTS=$(vbuckettool_SOURCES:.c=.dobj)
+testketama_OBJECTS=$(testketama_SOURCES:.c=.dobj)
+regression_OBJECTS=$(regression_SOURCES:.c=.dobj)
+testapp_OBJECTS=$(testapp_SOURCES:.c=.dobj)
+vbuckettool_OBJECTS=$(vbuckettool_SOURCES:.c=.dobj)
all_OBJS=$(vbucketkeygen_OBJECTS) $(testketama_OBJECTS) \
$(testketama_OBJECTS) $(regression_OBJECTS) \
@@ -53,7 +53,8 @@ all: libvbucket_debug.dll \
$(bin_PROGRAMS)
check: $(check_PROGRAMS)
- @echo "Todo: port the tests and run them"
+ set srcdir=.
+ for %%t in ($(check_PROGRAMS)) do %t
libvbucket.dll: $(libvbucket_la_OBJECTS)
$(LD) $(LDFLAGS) /out:libvbucket.dll $(libvbucket_la_OBJECTS)
View
28 src/vbucket.c
@@ -487,13 +487,30 @@ static int parse_from_memory(VBUCKET_CONFIG_HANDLE handle, const char *data) {
return ret;
}
+static int do_read_file(FILE *fp, char *data, size_t size)
+{
+ size_t offset = 0;
+ size_t nread;
+
+ do {
+ nread = fread(data + offset, 1, size, fp);
+ if (nread != (size_t)-1 && nread != 0) {
+ offset += nread;
+ size -= nread;
+ } else {
+ return -1;
+ }
+ } while (size > 0);
+
+ return 0;
+}
+
static int parse_from_file(VBUCKET_CONFIG_HANDLE handle, const char *filename)
{
long size;
char *data;
- size_t nread;
int ret;
- FILE *f = fopen(filename, "r");
+ FILE *f = fopen(filename, "rb");
if (f == NULL) {
char msg[1024];
snprintf(msg, sizeof(msg), "Unable to open file \"%s\": %s", filename,
@@ -514,15 +531,14 @@ static int parse_from_file(VBUCKET_CONFIG_HANDLE handle, const char *filename)
data = calloc(size+1, sizeof(char));
if (data == NULL) {
char msg[1024];
- snprintf(msg, sizeof(msg), "Filed to allocate buffer to read: \"%s\"", filename);
+ snprintf(msg, sizeof(msg), "Failed to allocate buffer to read: \"%s\"", filename);
handle->errmsg = strdup(msg);
fclose(f);
return -1;
}
- nread = fread(data, sizeof(char), size+1, f);
- if (nread != (size_t)size) {
+ if (do_read_file(f, data, size) == -1) {
char msg[1024];
- snprintf(msg, sizeof(msg), "Filed to read entire file: \"%s\": %s",
+ snprintf(msg, sizeof(msg), "Failed to read entire file: \"%s\": %s",
filename, strerror(errno));
handle->errmsg = strdup(msg);
fclose(f);
View
84 tests/regression.c
@@ -1,6 +1,4 @@
#undef NDEBUG
-#include <dirent.h>
-#include <limits.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
@@ -10,61 +8,47 @@
#include "macros.h"
+const char *test_cases[] = {
+ "regression-bug2112",
+ NULL
+};
+
int main(void) {
char *root = getenv("srcdir");
if (root != NULL) {
- char buffer[PATH_MAX];
- DIR *dp;
- struct dirent *de;
-
- sprintf(buffer, "%s/tests/config", root);
- dp = opendir(buffer);
- if (dp == NULL) {
- fprintf(stderr, "Skipping regression check\nFailed to open %s: %s\n",
- buffer, strerror(errno));
- return 0;
- }
-
- while ((de = readdir(dp)) != NULL) {
- if (strncmp(de->d_name, "regression-bug", 14) == 0 || strncmp(de->d_name, "mb-", 3) == 0) {
- VBUCKET_CONFIG_HANDLE h;
- sprintf(buffer, "%s/tests/config/%s", root, de->d_name);
- fprintf(stderr, "Running regression test for: %s\n", de->d_name);
- h = vbucket_config_parse_file(buffer);
- assert(h != NULL);
-
- if (strcmp(de->d_name, "mb-3161") == 0) {
- assert(strcmp(vbucket_config_get_user(h), "foo") == 0);
- assert(strcmp(vbucket_config_get_password(h), "bar") == 0);
- } else {
- char key1[9] = "00000000";
- int v, m, num_replicas;
- const char *master;
-
- fprintf(stderr, "Testing a key can be hashed.");
- v = vbucket_get_vbucket_by_key(h, key1, strlen(key1));
- m = vbucket_get_master(h, v);
- master = vbucket_config_get_server(h, m);
- fprintf(stderr, " key: %s vBucketId: %d master: %s", key1, v, master);
- num_replicas = vbucket_config_get_num_replicas(h);
- if (num_replicas > 0) {
- int j;
- fprintf(stderr, " replicas:");
- for (j = 0; j < num_replicas; j++) {
- int r = vbucket_get_replica(h, v, j);
- if (r >= 0) {
- const char *replica = vbucket_config_get_server(h, r);
- fprintf(stderr, " %s", replica == NULL ? "(null)" : replica);
- }
- }
+ char buffer[FILENAME_MAX];
+ VBUCKET_CONFIG_HANDLE h;
+ char key1[9] = "00000000";
+ int v, m, num_replicas, ff;
+ const char *master;
+
+ for (ff = 0; test_cases[ff] != NULL; ++ff) {
+ sprintf(buffer, "%s/tests/config/%s", root, test_cases[ff]);
+ fprintf(stderr, "Running regression test for: %s\n", test_cases[ff]);
+ h = vbucket_config_create();
+ assert(vbucket_config_parse(h, LIBVBUCKET_SOURCE_FILE, buffer) == 0);
+
+ fprintf(stderr, "Testing a key can be hashed.");
+ v = vbucket_get_vbucket_by_key(h, key1, strlen(key1));
+ m = vbucket_get_master(h, v);
+ master = vbucket_config_get_server(h, m);
+ fprintf(stderr, " key: %s vBucketId: %d master: %s", key1, v, master);
+ num_replicas = vbucket_config_get_num_replicas(h);
+ if (num_replicas > 0) {
+ int j;
+ fprintf(stderr, " replicas:");
+ for (j = 0; j < num_replicas; j++) {
+ int r = vbucket_get_replica(h, v, j);
+ if (r >= 0) {
+ const char *replica = vbucket_config_get_server(h, r);
+ fprintf(stderr, " %s", replica == NULL ? "(null)" : replica);
}
- fprintf(stderr, "\n");
}
- vbucket_config_destroy(h);
}
+ fprintf(stderr, "\n");
+ vbucket_config_destroy(h);
}
- closedir(dp);
}
- return 0;
+ exit(EXIT_SUCCESS);
}
View
9 tests/testapp.c
@@ -3,7 +3,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <limits.h>
#include <sys/stat.h>
#include <libvbucket/vbucket.h>
@@ -43,13 +42,13 @@ static const struct vb_st vbuckets[] =
};
static char *configPath(const char *fname) {
- static char buffer[PATH_MAX];
+ static char buffer[FILENAME_MAX];
char *root = getenv("srcdir");
struct stat st;
- sprintf(buffer, "%s/tests/config/testapp-%s", root, fname);
+ snprintf(buffer, FILENAME_MAX, "%s/tests/config/testapp-%s", root, fname);
if (stat(buffer, &st) == -1) {
- sprintf(buffer, "%s/tests/config/%s", root, fname);
+ snprintf(buffer, FILENAME_MAX, "%s/tests/config/%s", root, fname);
if (stat(buffer, &st) == -1) {
fprintf(stderr, "cannot find config %s\n", fname);
abort();
@@ -288,5 +287,5 @@ int main(void) {
testConfigUserPassword();
testConfigCouchApiBase();
testConfigDiffKetamaSame();
- return 0;
+ exit(EXIT_SUCCESS);
}
View
70 tests/testketama.c
@@ -1,8 +1,6 @@
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#undef NDEBUG
-#include <dirent.h>
-#include <limits.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
@@ -26,58 +24,50 @@ static void read_checksum(const char *path, unsigned char *result) {
}
}
+const char *test_cases[] = {
+ "ketama-eight-nodes",
+ "ketama-ordered-eight-nodes",
+ NULL
+};
+
int main(void) {
char *root = getenv("srcdir");
- DIR *dp;
const char *host;
- char buffer[PATH_MAX];
+ char buffer[FILENAME_MAX];
char key[NKEY];
- int idx, i, len;
- struct dirent *de;
+ int idx, i, len, ff;
VBUCKET_CONFIG_HANDLE vb;
unsigned char checksum[16];
unsigned char expected[16];
void *ctx;
if (root != NULL) {
- sprintf(buffer, "%s/tests/config", root);
- dp = opendir(buffer);
- if (dp == NULL) {
- fprintf(stderr, "Skipping ketama check\nFailed to open %s: %s\n",
- buffer, strerror(errno));
- return 0;
- }
-
- while ((de = readdir(dp)) != NULL) {
- if (strncmp(de->d_name, "ketama", 6) == 0 && strchr(de->d_name, '.') == NULL) {
- sprintf(buffer, "%s/tests/config/%s", root, de->d_name);
- fprintf(stderr, "Running ketama test for: %s\n", de->d_name);
- vb = vbucket_config_parse_file(buffer);
- assert(vb != NULL);
-
- /* check if it conforms to libketama results */
- sprintf(buffer, "%s/tests/config/%s.md5sum", root, de->d_name);
- read_checksum(buffer, expected);
- memset(checksum, 0, 16);
- ctx = NULL;
+ for (ff = 0; test_cases[ff] != NULL; ++ff) {
+ snprintf(buffer, FILENAME_MAX, "%s/tests/config/%s", root, test_cases[ff]);
+ fprintf(stderr, "Running ketama test for: %s\n", test_cases[ff]);
+ vb = vbucket_config_create();
+ assert(vbucket_config_parse(vb, LIBVBUCKET_SOURCE_FILE, buffer) == 0);
+ /* check if it conforms to libketama results */
+ snprintf(buffer, FILENAME_MAX,"%s/tests/config/%s.md5sum", root, test_cases[ff]);
+ read_checksum(buffer, expected);
+ memset(checksum, 0, 16);
+ ctx = NULL;
- for (i = 0; i < 1000000; i++) {
- len = sprintf(key, "%d", i);
- vbucket_map(vb, key, len, NULL, &idx);
- host = vbucket_config_get_server(vb, idx);
- ctx = hash_md5_update(ctx, host, strlen(host));
- }
- hash_md5_final(ctx, checksum);
-
- for (i = 0; i < 16; i++) {
- assert(checksum[i] == expected[i]);
- }
+ for (i = 0; i < 1000000; i++) {
+ len = snprintf(key, NKEY, "%d", i);
+ vbucket_map(vb, key, len, NULL, &idx);
+ host = vbucket_config_get_server(vb, idx);
+ ctx = hash_md5_update(ctx, host, strlen(host));
+ }
+ hash_md5_final(ctx, checksum);
- vbucket_config_destroy(vb);
+ for (i = 0; i < 16; i++) {
+ assert(checksum[i] == expected[i]);
}
+
+ vbucket_config_destroy(vb);
}
- closedir(dp);
}
- return 0;
+ exit(EXIT_SUCCESS);
}
Please sign in to comment.
Something went wrong with that request. Please try again.