1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
local msg
context("Selectors test", function()
local rspamd_task = require "rspamd_task"
local logger = require "rspamd_logger"
local lua_selectors = require "lua_selectors"
local ffi = require "ffi"
local cfg = rspamd_config
local task
ffi.cdef[[
void rspamd_url_init (const char *tld_file);
unsigned ottery_rand_range(unsigned top);
void rspamd_http_normalize_path_inplace(char *path, size_t len, size_t *nlen);
]]
local test_dir = string.gsub(debug.getinfo(1).source, "^@(.+/)[^/]+$", "%1")
ffi.C.rspamd_url_init(string.format('%s/%s', test_dir, "test_tld.dat"))
before(function()
local res
res,task = rspamd_task.load_from_string(msg, cfg)
task:set_from_ip("198.172.22.91")
task:set_user("cool user name")
task:set_helo("hello mail")
task:set_request_header("hdr1", "value1")
task:process_message()
task:get_mempool():set_variable("int_var", 1)
task:get_mempool():set_variable("str_var", "str 1")
if not res then
assert_true(false, "failed to load message")
end
end)
local function check_selector(selector_string)
local sels = lua_selectors.parse_selector(cfg, selector_string)
local elts = lua_selectors.process_selectors(task, sels)
return elts
end
local cases = {
["ip"] = {
selector = "ip",
expect = {"198.172.22.91"}},
["header Subject"] = {
selector = "header(Subject)",
expect = {"Second, lower-cased header subject"}},
["header Subject lower"] = {
selector = "header(Subject).lower",
expect = {"second, lower-cased header subject"}},
["header full Subject lower"] = {
selector = "header(Subject, 'full').lower",
expect = {{"second, lower-cased header subject", "test subject"}}},
["header full strong Subject"] = {
selector = "header(Subject, 'full,strong')",
expect = {{"Test subject"}}},
["header full strong lower-cased Subject"] = {
selector = "header(subject, 'full,strong')",
expect = {{"Second, lower-cased header subject"}}},
["digest"] = {
selector = "digest",
expect = {"2216397bc061bb6968e1836f3680fed0"}},
["user"] = {
selector = "user",
expect = {"cool user name"}},
["from"] = {
selector = "from",
expect = {"whoknows@nowhere.com"}},
["rcpts"] = {
selector = "rcpts",
expect = {{"nobody@example.com", "no-one@example.com"}}},
["1st rcpts"] = {
selector = "rcpts.nth(1)",
expect = {"nobody@example.com"}},
["lower rcpts"] = {
selector = "rcpts.lower.first",
expect = {"nobody@example.com"}},
["first rcpts"] = {
selector = "rcpts.first",
expect = {"nobody@example.com"}},
["first addr rcpts"] = {
selector = "rcpts:addr.first",
expect = {"nobody@example.com"}},
["to"] = {
selector = "to",
expect = {"nobody@example.com"}},
["attachments"] = {
selector = "attachments",
expect = {{"ce112d07c52ae649f9646f3d0b5aaab5d4834836d771c032d1a75059d31fed84f38e00c0b205918f6d354934c2055d33d19d045f783a62561f467728ebcf0160",
"ce112d07c52ae649f9646f3d0b5aaab5d4834836d771c032d1a75059d31fed84f38e00c0b205918f6d354934c2055d33d19d045f783a62561f467728ebcf0160"
}}},
["attachments id"] = {
selector = "attachments.id",
expect = {""}},
["files"] = {
selector = "files",
expect = {{"f.zip", "f2.zip"}}},
["helo"] = {
selector = "helo",
expect = {"hello mail"}},
["received by hostname"] = {
selector = "received:by_hostname",
expect = {{"server.chat-met-vreemden.nl"}}},
["urls"] = {
selector = "urls",
expect = {{"http://example.net"}}},
["emails"] = {
selector = "emails",
expect = {{"mailto://test@example.net"}}},
["pool_var str, default type"] = {
selector = [[pool_var("str_var")]],
expect = {"str 1"}},
["pool_var str"] = {
selector = [[pool_var("str_var", 'string')]],
expect = {"str 1"}},
["pool_var double"] = {
selector = [[pool_var("int_var", 'double')]],
expect = {"1"}},
["time"] = {
selector = "time",
expect = {"1537364211"}},
["request_header"] = {
selector = "request_header(hdr1)",
expect = {"value1"}},
}
for case_name, case in pairs(cases) do
test("case " .. case_name, function()
local elts = check_selector(case.selector)
assert_not_nil(elts)
assert_rspamd_table_eq({actual = elts, expect = case.expect})
end)
end
end)
--[=========[ ******************* message ******************* ]=========]
msg = [[
Received: from ca-18-193-131.service.infuturo.it ([151.18.193.131] helo=User)
by server.chat-met-vreemden.nl with esmtpa (Exim 4.76)
(envelope-from <upwest201diana@outlook.com>)
id 1ZC1sl-0006b4-TU; Mon, 06 Jul 2015 10:36:08 +0200
From: <whoknows@nowhere.com>
To: <nobody@example.com>, <no-one@example.com>
Date: Wed, 19 Sep 2018 14:36:51 +0100 (BST)
subject: Second, lower-cased header subject
Subject: Test subject
Content-Type: multipart/alternative;
boundary="_000_6be055295eab48a5af7ad4022f33e2d0_"
--_000_6be055295eab48a5af7ad4022f33e2d0_
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64
Hello world
--_000_6be055295eab48a5af7ad4022f33e2d0_
Content-Type: text/html; charset="utf-8"
<html><body>
<a href="http://example.net">http://example.net</a>
<a href="mailto:test@example.net">mail me</a>
</html>
--_000_6be055295eab48a5af7ad4022f33e2d0_
Content-Type: application/zip; name=f.zip
Content-Disposition: attachment; size=166; filename=f.zip
Content-Transfer-Encoding: base64
UEsDBAoAAAAAAINe6kgAAAAAAAAAAAAAAAAIABwAZmFrZS5leGVVVAkAA8YaglfGGoJXdXgLAAEE
6AMAAAToAwAAUEsBAh4DCgAAAAAAg17qSAAAAAAAAAAAAAAAAAgAGAAAAAAAAAAAALSBAAAAAGZh
a2UuZXhlVVQFAAPGGoJXdXgLAAEE6AMAAAToAwAAUEsFBgAAAAABAAEATgAAAEIAAAAAAA==
--_000_6be055295eab48a5af7ad4022f33e2d0_
Content-Type: application/zip; name=f.zip
Content-Disposition: attachment; size=166; filename=f2.zip
Content-Transfer-Encoding: base64
UEsDBAoAAAAAAINe6kgAAAAAAAAAAAAAAAAIABwAZmFrZS5leGVVVAkAA8YaglfGGoJXdXgLAAEE
6AMAAAToAwAAUEsBAh4DCgAAAAAAg17qSAAAAAAAAAAAAAAAAAgAGAAAAAAAAAAAALSBAAAAAGZh
a2UuZXhlVVQFAAPGGoJXdXgLAAEE6AMAAAToAwAAUEsFBgAAAAABAAEATgAAAEIAAAAAAA==
]]
|