提交 e8a376a8 authored 作者: Viktor Krikun's avatar Viktor Krikun 提交者: Travis Cross

First draft implementation of random-access cache

(refs #25)
上级 be03cdc2
......@@ -34,8 +34,8 @@ extern "C"
*/
typedef uint8_t zrtp_cache_id_t[24];
#define ZRTP_MITMCACHE_ELEM_LENGTH (sizeof(zrtp_cache_id_t) + sizeof(zrtp_string64_t))
#define ZRTP_CACHE_ELEM_LENGTH (sizeof(zrtp_cache_elem_t) - sizeof(mlist_t))
#define ZRTP_MITMCACHE_ELEM_LENGTH ( sizeof(zrtp_cache_id_t) + sizeof(zrtp_string64_t) )
#define ZRTP_CACHE_ELEM_LENGTH ( sizeof(zrtp_cache_elem_t) - sizeof(mlist_t) - (sizeof(uint32_t)*2) )
#define ZFONE_CACHE_NAME_LENGTH 256
/**
......@@ -51,10 +51,12 @@ typedef struct zrtp_cache_elem
uint32_t verified; /** Verified flag for the cache value */
uint32_t lastused_at; /** Last usage time-stamp in seconds */
uint32_t ttl; /** Cache TTL since lastused_at in seconds */
uint32_t secure_since; /** Secure since date in seconds. Utility field. Doen't required by libzrtp. */
uint32_t secure_since; /** Secure since date in seconds. Utility field. Don't required by libzrtp. */
char name[ZFONE_CACHE_NAME_LENGTH]; /** name of the user associated with this cache entry */
uint32_t name_length; /** cache name lengths */
uint32_t presh_counter; /** number of Preshared streams made since last DH echange */
uint32_t presh_counter; /** number of Preshared streams made since last DH exchange */
uint32_t _index; /** cache element index in the cache file */
uint32_t _is_dirty; /** dirty flag means the entry has unsaved changes */
mlist_t _mlist;
} zrtp_cache_elem_t;
......
......@@ -359,6 +359,7 @@ struct zrtp_global_t
/** RNG unit initialization flag. */
uint8_t rand_initialized;
/** Full path to ZRTP cache file. */
zrtp_string256_t def_cache_path;
/** This object is used to protect the shared RNG hash zrtp#rand_ctx */
......
......@@ -11,33 +11,47 @@ TOP_SRCDIR=$(top_srcdir)/../..
INCLUDES = -I$(TOP_SRCDIR)/include \
-I$(TOP_SRCDIR)/include/enterprise \
-I$(TOP_SRCDIR)/. \
-I$(TOP_SRCDIR)/test \
-I$(TOP_SRCDIR)/test/cmockery \
-I$(TOP_SRCDIR)/third_party/bgaes \
-I$(TOP_SRCDIR)/third_party/bnlib
check_PROGRAMS = libzrtp_test
#check_PROGRAMS = cache_test libzrtp_test
check_PROGRAMS = cache_test
### ZRTP Cache testing
cache_test_SOURCES = $(TOP_SRCDIR)/test/cmockery/cmockery.c \
$(TOP_SRCDIR)/test/cache_test.c
cache_test_LDADD = ../libzrtp.a $(TOP_SRCDIR)/third_party/bnlib/libbn.a -lpthread
### ZRTP high-level test-case
libzrtp_test_SOURCES = $(TOP_SRCDIR)/test/pc/zrtp_test_core.c \
$(TOP_SRCDIR)/test/pc/zrtp_test_crypto.c \
$(TOP_SRCDIR)/test/pc/zrtp_test_queue.c \
$(TOP_SRCDIR)/test/pc/zrtp_test_ui.c
libzrtp_test_LDADD = ../libzrtp.a \
$(TOP_SRCDIR)/third_party/bnlib/libbn.a -lpthread
libzrtp_test_LDADD = ../libzrtp.a $(TOP_SRCDIR)/third_party/bnlib/libbn.a -lpthread
SUBDIRS = .
check:
@echo ""
@echo "*========================================================================*"
@echo "* starting libZRTP tests *"
@echo "*========================================================================*"
@echo ""
@./libzrtp_test
@echo ""
@echo "*========================================================================*"
@echo "* In case you have a test FAILED send the generated log file *"
@echo "* with your comment to <zrtp_support@zfoneproject.com>. *"
@echo "*========================================================================*"
@echo ""
check:
# @ ./cache_test
# check:
# @echo ""
# @echo "*========================================================================*"
# @echo "* starting libZRTP tests *"
# @echo "*========================================================================*"
# @echo ""
# @./libzrtp_test
#
# @echo ""
# @echo "*========================================================================*"
# @echo "* In case you have a test FAILED send the generated log file *"
# @echo "* with your comment to <zrtp_support@zfoneproject.com>. *"
# @echo "*========================================================================*"
# @echo ""
差异被折叠。
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <string.h>
#include <stdio.h>
#include "zrtp.h"
#include "cmockery/cmockery.h"
#define TEST_CACHE_PATH "./zrtp_cache_test.dat"
static zrtp_global_t g_zrtp_cfg;
void cache_setup() {
remove(TEST_CACHE_PATH);
ZSTR_SET_EMPTY(g_zrtp_cfg.def_cache_path);
/* Configure and Initialize ZRTP cache */
zrtp_zstrcpyc(ZSTR_GV(g_zrtp_cfg.def_cache_path), TEST_CACHE_PATH);
}
void cache_teardown() {
zrtp_def_cache_down();
}
static void init_rs_secret_(zrtp_shared_secret_t *sec) {
ZSTR_SET_EMPTY(sec->value);
sec->_cachedflag = 0;
sec->ttl = 0;
sec->lastused_at = 0;
}
/*
* Simply init ZRTP cache with empty or non-existing filer and close it.
* The app should not crash and trigger no errors.
*/
void cache_init_store_empty_test() {
zrtp_status_t status;
/* It should NOT crash and return OK. */
status = zrtp_def_cache_init(&g_zrtp_cfg);
assert_int_equal(status, zrtp_status_ok);
zrtp_def_cache_down();
}
/*
* Add few entries to the empty cache, flush it and then load again. Check if
* all the entries were restored successfully.
*/
void cache_add2empty_test() {
zrtp_status_t status;
int intres;
zrtp_string16_t zid_my = ZSTR_INIT_WITH_CONST_CSTRING("000000000_01");
zrtp_string16_t zid_a = ZSTR_INIT_WITH_CONST_CSTRING("000000000_02");
zrtp_string16_t zid_b = ZSTR_INIT_WITH_CONST_CSTRING("000000000_03");
zrtp_string16_t zid_c = ZSTR_INIT_WITH_CONST_CSTRING("000000000_04");
zrtp_string16_t zid_mitm1 = ZSTR_INIT_WITH_CONST_CSTRING("000000000_04");
zrtp_shared_secret_t rs_my4a, rs_my4b, rs_my4c, rs_my4mitm1;
zrtp_shared_secret_t rs_my4a_r, rs_my4b_r, rs_my4c_r, rs_my4mitm1_r;
init_rs_secret_(&rs_my4a); init_rs_secret_(&rs_my4b);
init_rs_secret_(&rs_my4c); init_rs_secret_(&rs_my4mitm1);
init_rs_secret_(&rs_my4a_r); init_rs_secret_(&rs_my4b_r);
init_rs_secret_(&rs_my4c_r); init_rs_secret_(&rs_my4mitm1_r);
printf("Open empty cache file for.\n");
status = zrtp_def_cache_init(&g_zrtp_cfg);
assert_int_equal(status, zrtp_status_ok);
/* Test if cache-init does bot corrupt config. */
assert_false(strncmp(g_zrtp_cfg.def_cache_path.buffer, TEST_CACHE_PATH, strlen(TEST_CACHE_PATH)));
/* Add few values into it */
printf("Add few test entries.\n");
status = zrtp_def_cache_put(ZSTR_GV(zid_my), ZSTR_GV(zid_a), &rs_my4a);
assert_int_equal(status, zrtp_status_ok);
status = zrtp_def_cache_put(ZSTR_GV(zid_my), ZSTR_GV(zid_b), &rs_my4b);
assert_int_equal(status, zrtp_status_ok);
status = zrtp_def_cache_put_mitm(ZSTR_GV(zid_my), ZSTR_GV(zid_mitm1), &rs_my4mitm1);
assert_int_equal(status, zrtp_status_ok);
status = zrtp_def_cache_put(ZSTR_GV(zid_my), ZSTR_GV(zid_c), &rs_my4c);
assert_int_equal(status, zrtp_status_ok);
/* Close the cache, it should be flushed to the file. */
printf("Close the cache.\n");
zrtp_def_cache_down();
/* Test if cache-close does bot corrupt config. */
assert_false(strncmp(g_zrtp_cfg.def_cache_path.buffer, TEST_CACHE_PATH, strlen(TEST_CACHE_PATH)));
/* Now, let's open the cache again and check if all the previously added values were restored successfully */
printf("And open it again, it should contain all the stored values.\n");
status = zrtp_def_cache_init(&g_zrtp_cfg);
assert_int_equal(status, zrtp_status_ok);
status = zrtp_def_cache_get(ZSTR_GV(zid_my), ZSTR_GV(zid_a), &rs_my4a_r, 0);
assert_int_equal(status, zrtp_status_ok);
assert_false(zrtp_zstrcmp(ZSTR_GV(rs_my4a_r.value), ZSTR_GV(rs_my4a.value)));
/* Test if cache-close does bot corrupt config. */
assert_false(strncmp(g_zrtp_cfg.def_cache_path.buffer, TEST_CACHE_PATH, strlen(TEST_CACHE_PATH)));
}
int main(void) {
const UnitTest tests[] = {
//unit_test_setup_teardown(cache_init_store_empty_test, cache_setup, cache_teardown),
unit_test_setup_teardown(cache_add2empty_test, cache_setup, cache_teardown),
};
return run_tests(tests);
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论