#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);
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);
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);
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);
}
struct reader_state {
struct tevent_context *ev;
int fd;
- uint8_t buf[1024];
+ uint8_t *buf;
+ size_t buflen;
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;
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);
}
}
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;
}
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);
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,
return true;
}
-int main(void)
+static void reader_test(bool fixed)
{
int fd[2];
int ret;
exit(1);
}
- reader(fd[0]);
+ reader(fd[0], fixed);
+}
+
+int main(void)
+{
+ reader_test(true);
+ reader_test(false);
return 0;
}
#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)
{
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);
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);
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);