Commit a1eda135 authored by Grigorii Demidov's avatar Grigorii Demidov

tests: modification of cache insertion test

parent fd1c2cbb
......@@ -20,11 +20,19 @@
#include "tests/test.h"
#include "lib/cache.h"
#include <stdlib.h>
#include <time.h>
mm_ctx_t global_mm;
namedb_txn_t global_txn;
knot_rrset_t global_rr;
const char *global_env;
struct kr_cache_entry global_fake_ce;
#define NAMEDB_INTS 256
#define NAMEDB_DATA_SIZE (NAMEDB_INTS * sizeof(int))
uint8_t namedb_data[NAMEDB_DATA_SIZE];
namedb_val_t global_namedb_data = {namedb_data, NAMEDB_DATA_SIZE};
#define CACHE_SIZE 10 * 4096
#define CACHE_TTL 10
......@@ -48,14 +56,40 @@ static void test_abort(namedb_txn_t *txn)
return;
}
/* Fake api to simulate failures */
/* Stub for find */
static int test_find(namedb_txn_t *txn, namedb_val_t *key, namedb_val_t *val, unsigned flags)
{
val->data = &global_fake_ce;
return KNOT_EOK;
}
/* Stub for insert */
static int test_ins(namedb_txn_t *txn, namedb_val_t *key, namedb_val_t *val, unsigned flags)
{
int err = KNOT_EINVAL, i, res_cmp;
struct kr_cache_entry *header = val->data;
if (val->len == sizeof (*header) + NAMEDB_DATA_SIZE)
{
header = val->data;
res_cmp = memcmp(header->data,namedb_data,NAMEDB_DATA_SIZE);
if (header->timestamp == global_fake_ce.timestamp &&
header->ttl == global_fake_ce.ttl &&
header->ttl == global_fake_ce.ttl &&
res_cmp == 0)
err = KNOT_EOK;
}
return err;
}
/* Fake api */
static const namedb_api_t *namedb_lmdb_api_fake(void)
{
static const namedb_api_t api_fake = {
"lmdb_api_fake",
test_init_failure, NULL,
NULL, test_commit_failure, test_abort,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, test_find, test_ins, NULL,
NULL, NULL, NULL, NULL, NULL, NULL
};
......@@ -67,16 +101,27 @@ static const namedb_api_t *namedb_lmdb_api_fake(void)
static void test_invalid(void **state)
{
const namedb_api_t *(*kr_cache_storage_saved)(void);
void *ret_open;
int ret_commit;
void *ret_open, *ret_cache_peek;
int ret_commit, timestamp = CACHE_TIME;
assert_int_not_equal(kr_cache_txn_begin(NULL, &global_txn, 0), 0);
assert_int_not_equal(kr_cache_txn_begin(&global_env, NULL, 0), 0);
assert_int_not_equal(kr_cache_txn_commit(NULL), 0);
assert_null(kr_cache_peek(NULL, KR_CACHE_USER, "", KNOT_RRTYPE_TSIG, &timestamp));
assert_null(kr_cache_peek(&global_txn, 0, "", KNOT_RRTYPE_TSIG, &timestamp));
assert_null(kr_cache_peek(&global_txn, KR_CACHE_USER, NULL, KNOT_RRTYPE_TSIG, &timestamp));
assert_int_not_equal(kr_cache_peek_rr(NULL, NULL, NULL), 0);
assert_int_not_equal(kr_cache_peek_rr(&global_txn, NULL, NULL), 0);
assert_int_not_equal(kr_cache_insert_rr(&global_txn, NULL, 0), 0);
assert_int_not_equal(kr_cache_insert_rr(NULL, NULL, 0), 0);
assert_int_not_equal(kr_cache_insert(NULL, KR_CACHE_USER, "",
KNOT_RRTYPE_TSIG, &global_fake_ce, global_namedb_data), 0);
assert_int_not_equal(kr_cache_insert(&global_txn, 0, "",
KNOT_RRTYPE_TSIG, &global_fake_ce, global_namedb_data), 0);
assert_int_not_equal(kr_cache_insert(&global_txn, KR_CACHE_USER, NULL,
KNOT_RRTYPE_TSIG, &global_fake_ce, global_namedb_data), 0);
assert_int_not_equal(kr_cache_insert(&global_txn, KR_CACHE_USER, "",
KNOT_RRTYPE_TSIG, NULL, global_namedb_data), 0);
assert_int_not_equal(kr_cache_remove(&global_txn, 0, NULL, 0), 0);
assert_int_not_equal(kr_cache_remove(&global_txn, KR_CACHE_RR, NULL, 0), 0);
assert_int_not_equal(kr_cache_remove(NULL, 0, NULL, 0), 0);
......@@ -84,14 +129,17 @@ static void test_invalid(void **state)
/* save original api */
kr_cache_storage_saved = kr_cache_storage;
/* fake to simulate failures */
/* fake to simulate failures or constant success */
kr_cache_storage_set(namedb_lmdb_api_fake);
/* call kr_cache_peek() with no time constraint */
ret_cache_peek = kr_cache_peek(&global_txn, KR_CACHE_USER, "", KNOT_RRTYPE_TSIG, 0);
ret_open = kr_cache_open(NULL, NULL);
ret_commit = kr_cache_txn_commit(&global_txn);
/* restore */
kr_cache_storage_set(kr_cache_storage_saved);
assert_int_equal(ret_cache_peek, &global_fake_ce);
assert_null(ret_open);
assert_int_not_equal(ret_commit, KNOT_EOK);
}
......@@ -134,6 +182,8 @@ static namedb_txn_t *test_txn_rdonly(void **state)
/* Test cache write */
static void test_insert(void **state)
{
const namedb_api_t *(*kr_cache_storage_saved)(void);
int i, ret_cache_insert;
test_random_rr(&global_rr, CACHE_TTL);
namedb_txn_t *txn = test_txn_write(state);
......@@ -145,6 +195,24 @@ static void test_insert(void **state)
}
assert_int_equal(ret, KNOT_EOK);
memset(&global_fake_ce,0xAA,sizeof(global_fake_ce));
srand(time(NULL));
for (i = 0; i < NAMEDB_DATA_SIZE; i += 4)
{
int r = rand();
namedb_data[i] = r;
namedb_data[i + 1] = r >> 8;
namedb_data[i + 2] = r >> 16;
namedb_data[i + 3] = r >> 24;
}
kr_cache_storage_saved = kr_cache_storage;
kr_cache_storage_set(namedb_lmdb_api_fake);
ret_cache_insert = kr_cache_insert(&global_txn, KR_CACHE_USER, "",
KNOT_RRTYPE_TSIG, &global_fake_ce, global_namedb_data);
kr_cache_storage_set(kr_cache_storage_saved);
assert_int_equal(ret_cache_insert, KNOT_EOK);
}
/* Test cache read */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment