| 1 |
local msg |
| 2 |
context("Selectors test", function() |
| 3 |
local rspamd_task = require "rspamd_task" |
| 4 |
local logger = require "rspamd_logger" |
| 5 |
local lua_selectors = require "lua_selectors" |
| 6 |
local test_helper = require "rspamd_test_helper" |
| 7 |
local cfg = rspamd_config |
| 8 |
local task |
| 9 |
|
| 10 |
test_helper.init_url_parser() |
| 11 |
|
| 12 |
before(function() |
| 13 |
local res |
| 14 |
res,task = rspamd_task.load_from_string(msg, cfg) |
| 15 |
task:set_from_ip("198.172.22.91") |
| 16 |
task:set_user("cool user name") |
| 17 |
task:set_helo("hello mail") |
| 18 |
task:set_request_header("hdr1", "value1") |
| 19 |
task:process_message() |
| 20 |
task:get_mempool():set_variable("int_var", 1) |
| 21 |
task:get_mempool():set_variable("str_var", "str 1") |
| 22 |
if not res then |
| 23 |
assert_true(false, "failed to load message") |
| 24 |
end |
| 25 |
end) |
| 26 |
|
| 27 |
local function check_selector(selector_string) |
| 28 |
local sels = lua_selectors.parse_selector(cfg, selector_string) |
| 29 |
local elts = lua_selectors.process_selectors(task, sels) |
| 30 |
local res = lua_selectors.combine_selectors(task, elts, ':') |
| 31 |
return res |
| 32 |
end |
| 33 |
|
| 34 |
local cases = { |
| 35 |
["rcpts + weekend"] = { |
| 36 |
selector = "rcpts:addr.take_n(5).lower;time('message', '!%w').in(6, 7).id('weekends')", |
| 37 |
expect = { |
| 38 |
"[email protected]:weekends", |
| 39 |
"[email protected]:weekends"}}, |
| 40 |
|
| 41 |
["weekend + rcpts"] = { |
| 42 |
selector = "time('message', '!%w').in(6, 7).id('weekends');rcpts:addr.take_n(5).lower", |
| 43 |
expect = { |
| 44 |
"weekends:[email protected]", |
| 45 |
"weekends:[email protected]"}}, |
| 46 |
|
| 47 |
["id(rcpt) + rcpts + weekend"] = { |
| 48 |
selector = "id('rcpt');rcpts:addr.take_n(5).lower;time('message', '!%w').in(6, 7).id('weekends')", |
| 49 |
expect = { |
| 50 |
"rcpt:[email protected]:weekends", |
| 51 |
"rcpt:[email protected]:weekends"}}, |
| 52 |
|
| 53 |
["id(rcpt) + id(2) rcpts + weekend"] = { |
| 54 |
selector = "id('rcpt'); id(2); rcpts:addr.take_n(5).lower; time('message', '!%w').in(6, 7).id('weekends')", |
| 55 |
expect = { |
| 56 |
"rcpt:2:[email protected]:weekends", |
| 57 |
"rcpt:2:[email protected]:weekends"} |
| 58 |
}, |
| 59 |
|
| 60 |
-- There are two rcpts but only one url in the message |
| 61 |
-- resulting table size is the size of the smallest table |
| 62 |
["id(rcpt) + id(2) + rcpts and urls + weekend"] = { |
| 63 |
selector = "id('rcpt'); id(2); rcpts:addr.take_n(5).lower; id('urls'); urls:get_host; time('message', '!%w').in(6, 7).id('weekends')", |
| 64 |
expect = { "rcpt:2:[email protected]:urls:example.net:weekends"} |
| 65 |
}, |
| 66 |
["url + apply_methods"] = { |
| 67 |
selector = "urls.apply_methods('get_host', 'get_path').join_tables('/')", |
| 68 |
expect = {"example.net/path"} |
| 69 |
}, |
| 70 |
} |
| 71 |
|
| 72 |
for case_name, case in pairs(cases) do |
| 73 |
test("case " .. case_name, function() |
| 74 |
local elts = check_selector(case.selector) |
| 75 |
assert_not_nil(elts) |
| 76 |
assert_rspamd_table_eq({actual = elts, expect = case.expect}) |
| 77 |
end) |
| 78 |
end |
| 79 |
end) |
| 80 |
|
| 81 |
|
| 82 |
--[=========[ ******************* message ******************* ]=========] |
| 83 |
msg = [[ |
| 84 |
Received: from ca-18-193-131.service.infuturo.it ([151.18.193.131] helo=User) |
| 85 |
by server.chat-met-vreemden.nl with esmtpa (Exim 4.76) |
| 86 |
(envelope-from <[email protected]>) |
| 87 |
id 1ZC1sl-0006b4-TU; Mon, 06 Jul 2015 10:36:08 +0200 |
| 88 |
From: <[email protected]> |
| 89 |
To: <[email protected]>, <[email protected]> |
| 90 |
Date: Sat, 22 Sep 2018 14:36:51 +0100 (BST) |
| 91 |
subject: Second, lower-cased header subject |
| 92 |
Subject: Test subject |
| 93 |
Content-Type: multipart/alternative; |
| 94 |
boundary="_000_6be055295eab48a5af7ad4022f33e2d0_" |
| 95 |
|
| 96 |
--_000_6be055295eab48a5af7ad4022f33e2d0_ |
| 97 |
Content-Type: text/plain; charset="utf-8" |
| 98 |
Content-Transfer-Encoding: base64 |
| 99 |
|
| 100 |
Hello world |
| 101 |
|
| 102 |
|
| 103 |
--_000_6be055295eab48a5af7ad4022f33e2d0_ |
| 104 |
Content-Type: text/html; charset="utf-8" |
| 105 |
|
| 106 |
<html><body> |
| 107 |
<a href="http://example.net/path?query">http://example.net/path?query</a> |
| 108 |
<a href="mailto:[email protected]">mail me</a> |
| 109 |
</html> |
| 110 |
|
| 111 |
|
| 112 |
--_000_6be055295eab48a5af7ad4022f33e2d0_ |
| 113 |
Content-Type: application/zip; name=f.zip |
| 114 |
Content-Disposition: attachment; size=166; filename=f.zip |
| 115 |
Content-Transfer-Encoding: base64 |
| 116 |
|
| 117 |
UEsDBAoAAAAAAINe6kgAAAAAAAAAAAAAAAAIABwAZmFrZS5leGVVVAkAA8YaglfGGoJXdXgLAAEE |
| 118 |
6AMAAAToAwAAUEsBAh4DCgAAAAAAg17qSAAAAAAAAAAAAAAAAAgAGAAAAAAAAAAAALSBAAAAAGZh |
| 119 |
a2UuZXhlVVQFAAPGGoJXdXgLAAEE6AMAAAToAwAAUEsFBgAAAAABAAEATgAAAEIAAAAAAA== |
| 120 |
|
| 121 |
|
| 122 |
--_000_6be055295eab48a5af7ad4022f33e2d0_ |
| 123 |
Content-Type: application/zip; name=f.zip |
| 124 |
Content-Disposition: attachment; size=166; filename=f2.zip |
| 125 |
Content-Transfer-Encoding: base64 |
| 126 |
|
| 127 |
UEsDBAoAAAAAAINe6kgAAAAAAAAAAAAAAAAIABwAZmFrZS5leGVVVAkAA8YaglfGGoJXdXgLAAEE |
| 128 |
6AMAAAToAwAAUEsBAh4DCgAAAAAAg17qSAAAAAAAAAAAAAAAAAgAGAAAAAAAAAAAALSBAAAAAGZh |
| 129 |
a2UuZXhlVVQFAAPGGoJXdXgLAAEE6AMAAAToAwAAUEsFBgAAAAABAAEATgAAAEIAAAAAAA== |
| 130 |
]] |