]> source.dussan.org Git - rspamd.git/commitdiff
[Test] Test map key values 1458/head
authorAndrew Lewis <nerf@judo.za.org>
Mon, 27 Feb 2017 16:11:44 +0000 (18:11 +0200)
committerAndrew Lewis <nerf@judo.za.org>
Mon, 27 Feb 2017 16:11:44 +0000 (18:11 +0200)
test/functional/cases/101_lua.robot
test/functional/configs/maps/ip2.list [new file with mode: 0644]
test/functional/configs/maps/map.list [new file with mode: 0644]
test/functional/configs/maps/regexp.list
test/functional/lua/maps_kv.lua [new file with mode: 0644]

index 867ecd2cdc582e0fab6eda53f4d446f198838e9c..f3121daf8ae81a074f0da5f8cced9425f4c1b09f 100644 (file)
@@ -6,7 +6,10 @@ Variables       ${TESTDIR}/lib/vars.py
 
 *** Variables ***
 ${CONFIG}       ${TESTDIR}/configs/lua_test.conf
+${MAP_MAP}      ${TESTDIR}/configs/maps/map.list
 ${MESSAGE}      ${TESTDIR}/messages/spam_message.eml
+${RADIX_MAP}    ${TESTDIR}/configs/maps/ip2.list
+${REGEXP_MAP}   ${TESTDIR}/configs/maps/regexp.list
 ${RSPAMD_SCOPE}  Test
 ${URL_TLD}      ${TESTDIR}/../lua/unit/test_tld.dat
 
@@ -45,12 +48,32 @@ Hashes
   ${result} =  Scan Message With Rspamc  ${MESSAGE}
   Check Rspamc  ${result}  TEST_HASHES (1.00)[no worry]
 
+Maps Key Values
+  [Setup]  Lua Replace Setup  ${TESTDIR}/lua/maps_kv.lua
+  [Teardown]  Lua Replace Teardown
+  ${result} =  Scan Message With Rspamc  ${MESSAGE}
+  Check Rspamc  ${result}  RADIX_KV (1.00)[no worry]
+  Should Contain  ${result.stdout}  REGEXP_KV (1.00)[no worry]
+  Should Contain  ${result.stdout}  MAP_KV (1.00)[no worry]
+
 *** Keywords ***
 Lua Setup
   [Arguments]  ${LUA_SCRIPT}
   Set Test Variable  ${LUA_SCRIPT}
   Generic Setup
 
+Lua Replace Setup
+  [Arguments]  ${LUA_SCRIPT_UNESC}
+  ${LUA_SCRIPT} =  Make Temporary File
+  ${lua} =  Get File  ${LUA_SCRIPT_UNESC}
+  ${lua} =  Replace Variables  ${lua}
+  Create File  ${LUA_SCRIPT}  ${lua}
+  Lua Setup  ${LUA_SCRIPT}
+
+Lua Replace Teardown
+  Remove File  ${LUA_SCRIPT}
+  Normal Teardown
+
 TLD Setup
   [Arguments]  ${LUA_SCRIPT}
   Set Test Variable  ${URL_TLD}  ${TESTDIR}/../../contrib/publicsuffix/effective_tld_names.dat
diff --git a/test/functional/configs/maps/ip2.list b/test/functional/configs/maps/ip2.list
new file mode 100644 (file)
index 0000000..7d99d28
--- /dev/null
@@ -0,0 +1,3 @@
+8.8.8.8 test one
+::1 another
+192.168.1.1
diff --git a/test/functional/configs/maps/map.list b/test/functional/configs/maps/map.list
new file mode 100644 (file)
index 0000000..8e68bab
--- /dev/null
@@ -0,0 +1,3 @@
+foo bar
+asdf.example.com value
+asdf
index 0f3ef27bfc764e26825634faa57d0b2819eaa271..ceb2bf5abd850a9d4c3bee00e8ca9291d8f8945b 100644 (file)
@@ -1,2 +1,5 @@
 /^.*@example.com/i
 /^user.*@.*com/i
+/foo/ bar
+/asdf\.example\.com/ value
+/^asdf$/
diff --git a/test/functional/lua/maps_kv.lua b/test/functional/lua/maps_kv.lua
new file mode 100644 (file)
index 0000000..3decc48
--- /dev/null
@@ -0,0 +1,70 @@
+local rspamd_ip = require 'rspamd_ip'
+local rspamd_logger = require 'rspamd_logger'
+
+local radix_map = rspamd_config:add_map ({
+  url = '${RADIX_MAP}',
+  type = 'radix',
+})
+
+local map_map = rspamd_config:add_map ({
+  url = '${MAP_MAP}',
+  type = 'map',
+})
+
+local regexp_map = rspamd_config:add_map ({
+  url = '${REGEXP_MAP}',
+  type = 'regexp',
+})
+
+rspamd_config:register_symbol({
+  name = 'RADIX_KV',
+  score = 1.0,
+  callback = function()
+    local sip = {'8.8.8.8', '::1', '192.168.1.1', '10.0.1.1'}
+    local expected = {'test one', 'another', '1', false}
+    for i = 1, #sip do
+      if (radix_map:get_key(rspamd_ip.from_string(sip[i])) ~= expected[i]) then
+        local rip = rspamd_ip.from_string(sip[i])
+        local val = radix_map:get_key(rip)
+        return true, rspamd_logger.slog('get_key(%s) [%s] -> %s [%s] [expected %s]', rip, type(rip), val, type(val), expected[i])
+      end
+      if (radix_map:get_key(sip[i]) ~= expected[i]) then
+        local val = radix_map:get_key(sip[i])
+        return true, rspamd_logger.slog('get_key(%s) [%s] -> %s [%s] [expected %s]', sip[i], type(sip[i]), val, type(val), expected[i])
+      end
+    end
+    return true, 'no worry'
+  end
+})
+
+rspamd_config:register_symbol({
+  name = 'MAP_KV',
+  score = 1.0,
+  callback = function()
+    local str = {'foo', 'asdf.example.com', 'asdf', 'barf'}
+    local expected = {'bar', 'value', '', false}
+    for i = 1, #str do
+      if (map_map:get_key(str[i]) ~= expected[i]) then
+        local val = map_map:get_key(str[i])
+        return true, rspamd_logger.slog('get_key(%s) [%s] -> %s [%s] [expected %s]', str[i], type(str[i]), val, type(val), expected[i])
+      end
+    end
+    return true, 'no worry'
+  end,
+})
+
+rspamd_config:register_symbol({
+  name = 'REGEXP_KV',
+  score = 1.0,
+  callback = function()
+    local str = {'foo', 'asdf.example.com', 'asdf', 'barf'}
+    local expected = {'bar', 'value', '1', false}
+    for i = 1, #str do
+      if (regexp_map:get_key(str[i]) ~= expected[i]) then
+        local val = regexp_map:get_key(str[i])
+        return true, rspamd_logger.slog('get_key(%s) [%s] -> %s [%s] [expected %s]', str[i], type(str[i]), val, type(val), expected[i])
+      end
+    end
+    return true, 'no worry'
+  end,
+})