1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-24 21:34:56 +03:00

ctdb-tests: Improve code coverage in tests

Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
This commit is contained in:
Amitay Isaacs 2016-04-12 16:02:53 +10:00 committed by Martin Schwenke
parent fdaa2310ff
commit c1236b37fd
3 changed files with 86 additions and 12 deletions

View File

@ -23,13 +23,36 @@
#include "common/db_hash.c"
static int record_parser(uint8_t *keybuf, size_t keylen,
uint8_t *databuf, size_t datalen,
void *private_data)
{
int *count = (int *)private_data;
(*count) += 1;
return 0;
}
static void do_test(enum db_hash_type type)
{
struct db_hash_context *dh;
struct db_hash_context *dh = NULL;
TALLOC_CTX *mem_ctx = talloc_new(NULL);
uint8_t key[] = "This is a long key";
uint8_t value[] = "This is a long value";
int ret;
int count = 0;
ret = db_hash_insert(dh, key, sizeof(key), value, sizeof(value));
assert(ret == EINVAL);
ret = db_hash_add(dh, key, sizeof(key), value, sizeof(value));
assert(ret == EINVAL);
ret = db_hash_exists(dh, key, sizeof(key));
assert(ret == EINVAL);
ret = db_hash_delete(dh, key, sizeof(key));
assert(ret == EINVAL);
ret = db_hash_init(mem_ctx, "foobar", 1024, type, &dh);
assert(ret == 0);
@ -40,6 +63,13 @@ static void do_test(enum db_hash_type type)
ret = db_hash_exists(dh, key, sizeof(key));
assert(ret == 0);
ret = db_hash_fetch(dh, key, sizeof(key), NULL, NULL);
assert(ret = EINVAL);
ret = db_hash_fetch(dh, key, sizeof(key), record_parser, &count);
assert(ret == 0);
assert(count == 1);
ret = db_hash_insert(dh, key, sizeof(key), value, sizeof(value));
assert(ret == EEXIST);
@ -67,12 +97,15 @@ static void do_test(enum db_hash_type type)
static void do_traverse_test(enum db_hash_type type)
{
struct db_hash_context *dh;
struct db_hash_context *dh = NULL;
TALLOC_CTX *mem_ctx = talloc_new(NULL);
char key[] = "keyXXXX";
char value[] = "This is some test value";
int count, ret, i;
ret = db_hash_traverse(dh, NULL, NULL, &count);
assert(ret == EINVAL);
ret = db_hash_init(mem_ctx, "foobar", 1024, type, &dh);
assert(ret == 0);
@ -87,6 +120,10 @@ static void do_traverse_test(enum db_hash_type type)
assert(ret == 0);
assert(count == 2000);
ret = db_hash_traverse(dh, record_parser, &count, NULL);
assert(ret == 0);
assert(count == 4000);
talloc_free(dh);
talloc_free(mem_ctx);
}

View File

@ -55,7 +55,8 @@ static void writer(int fd)
struct reader_state {
struct tevent_context *ev;
int fd;
uint8_t buf[1024];
uint8_t *buf;
size_t buflen;
struct tevent_req *subreq;
};
@ -64,7 +65,8 @@ static void reader_done(struct tevent_req *subreq);
static struct tevent_req *reader_send(TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
int fd)
int fd, uint8_t *buf,
size_t buflen)
{
struct tevent_req *req, *subreq;
struct reader_state *state;
@ -76,9 +78,11 @@ static struct tevent_req *reader_send(TALLOC_CTX *mem_ctx,
state->ev = ev;
state->fd = fd;
state->buf = buf;
state->buflen = buflen;
subreq = pkt_read_send(state, state->ev, state->fd, 4,
state->buf, 1024, reader_more, NULL);
state->buf, state->buflen, reader_more, NULL);
if (tevent_req_nomem(subreq, req)) {
tevent_req_post(req, ev);
}
@ -128,7 +132,7 @@ static void reader_done(struct tevent_req *subreq)
}
subreq = pkt_read_send(state, state->ev, state->fd, 4,
state->buf, 1024, reader_more, NULL);
state->buf, state->buflen, reader_more, NULL);
if (tevent_req_nomem(subreq, req)) {
return;
}
@ -167,13 +171,15 @@ static void reader_handler(struct tevent_context *ev, struct tevent_fd *fde,
pkt_read_handler(ev, fde, flags, state->subreq);
}
static void reader(int fd)
static void reader(int fd, bool fixed)
{
TALLOC_CTX *mem_ctx;
struct tevent_context *ev;
struct tevent_fd *fde;
struct tevent_req *req;
int err;
uint8_t *buf = NULL;
size_t buflen = 0;
mem_ctx = talloc_new(NULL);
assert(mem_ctx != NULL);
@ -181,7 +187,13 @@ static void reader(int fd)
ev = tevent_context_init(mem_ctx);
assert(ev != NULL);
req = reader_send(mem_ctx, ev, fd);
if (fixed) {
buflen = 1024;
buf = talloc_size(mem_ctx, buflen);
assert(buf != NULL);
}
req = reader_send(mem_ctx, ev, fd, buf, buflen);
assert(req != NULL);
fde = tevent_add_fd(ev, mem_ctx, fd, TEVENT_FD_READ,
@ -212,7 +224,7 @@ static bool set_nonblocking(int fd)
return true;
}
int main(void)
static void reader_test(bool fixed)
{
int fd[2];
int ret;
@ -236,7 +248,13 @@ int main(void)
exit(1);
}
reader(fd[0]);
reader(fd[0], fixed);
}
int main(void)
{
reader_test(true);
reader_test(false);
return 0;
}

View File

@ -24,7 +24,7 @@
#include "common/db_hash.c"
#include "common/srvid.c"
#define TEST_SRVID 0xFE11223344556677
#define TEST_SRVID 0xBE11223344556677
static void test_handler(uint64_t srvid, TDB_DATA data, void *private_data)
{
@ -34,15 +34,21 @@ static void test_handler(uint64_t srvid, TDB_DATA data, void *private_data)
int main(void)
{
struct srvid_context *srv;
struct srvid_context *srv = NULL;
TALLOC_CTX *mem_ctx = talloc_new(NULL);
TALLOC_CTX *tmp_ctx = talloc_new(NULL);
int ret;
int count = 0;
ret = srvid_register(srv, tmp_ctx, TEST_SRVID, test_handler, &count);
assert(ret == EINVAL);
ret = srvid_init(mem_ctx, &srv);
assert(ret == 0);
ret = srvid_deregister(srv, TEST_SRVID, &count);
assert(ret == ENOENT);
ret = srvid_register(srv, tmp_ctx, TEST_SRVID, test_handler, &count);
assert(ret == 0);
@ -53,6 +59,10 @@ int main(void)
assert(ret == 0);
assert(count == 1);
ret = srvid_dispatch(srv, 0, TEST_SRVID, tdb_null);
assert(ret == 0);
assert(count == 2);
ret = srvid_deregister(srv, TEST_SRVID, NULL);
assert(ret == ENOENT);
@ -69,8 +79,17 @@ int main(void)
ret = srvid_dispatch(srv, TEST_SRVID, 0, tdb_null);
assert(ret == ENOENT);
tmp_ctx = talloc_new(NULL);
assert(tmp_ctx != NULL);
ret = srvid_register(srv, tmp_ctx, TEST_SRVID, test_handler, NULL);
assert(ret == 0);
ret = srvid_register(srv, tmp_ctx, TEST_SRVID, test_handler, &count);
assert(ret == 0);
talloc_free(srv);
assert(talloc_get_size(mem_ctx) == 0);
assert(talloc_get_size(tmp_ctx) == 0);
talloc_free(mem_ctx);