From 01817dc286f00cce8555a9e9e896a1ea5c513bc9 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sat, 28 Feb 2015 22:23:28 +0000 Subject: [PATCH] Write unit test for redis substitution and fix a bug found. --- src/libstat/backends/redis.c | 10 +++++---- test/lua/unit/redis_stat.lua | 39 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 test/lua/unit/redis_stat.lua diff --git a/src/libstat/backends/redis.c b/src/libstat/backends/redis.c index 9d05bc591..1b35802a1 100644 --- a/src/libstat/backends/redis.c +++ b/src/libstat/backends/redis.c @@ -50,7 +50,10 @@ struct redis_stat_runtime { #define GET_TASK_ELT(task, elt) (task == NULL ? NULL : (task)->elt) -static gsize +/* + * Non-static for lua unit testing + */ +gsize rspamd_redis_expand_object (const gchar *pattern, struct rspamd_statfile_config *stcf, struct rspamd_task *task, @@ -158,7 +161,8 @@ rspamd_redis_expand_object (const gchar *pattern, *target = rspamd_mempool_alloc (task->task_pool, tlen + 1); d = *target; - end = d + tlen; + end = d + tlen + 1; + d[tlen] = '\0'; p = pattern; state = just_char; @@ -245,8 +249,6 @@ rspamd_redis_expand_object (const gchar *pattern, } } - *d = '\0'; - return tlen; } diff --git a/test/lua/unit/redis_stat.lua b/test/lua/unit/redis_stat.lua new file mode 100644 index 000000000..8f772ae75 --- /dev/null +++ b/test/lua/unit/redis_stat.lua @@ -0,0 +1,39 @@ + + +context("Redis statistics unit tests", function() + local task = require("rspamd_task") + local ffi = require("ffi") + ffi.cdef[[ + struct rspamd_statfile_config { + const char *symbol; + const char *label; + void *opts; + int is_spam; + const char *backend; + void *data; + }; + unsigned long rspamd_redis_expand_object(const char *pattern, + struct rspamd_statfile_config *stcf, + struct rspamd_task *task, + char **target); + struct rspamd_task * rspamd_task_new(struct rspamd_worker *worker); + ]] + + test("Substitute redis values", function() + local cases = { + {"%s%l", "symbollabel"}, + {"%s%%", "symbol%"}, + {"%s%u", "symbol"}, + {"%s%W", "symbolW"} + } + local stcf = ffi.new("struct rspamd_statfile_config", + {symbol="symbol",label="label"}) + local t = ffi.C.rspamd_task_new(nil) + for _,c in ipairs(cases) do + local pbuf = ffi.new 'char *[1]' + local sz = ffi.C.rspamd_redis_expand_object(c[1], stcf, t, pbuf) + local s = ffi.string(pbuf[0]) + assert_equal(s, c[2]) + end + end) +end) \ No newline at end of file -- 2.39.5