Debian Codegraph @

test/lua/unit/lua_util.extract_specific_urls.lua

raw
1
2 local msg, msg_img
3 local logger = require "rspamd_logger"
4 local rspamd_util = require "rspamd_util"
5 local rspamd_task = require "rspamd_task"
6 local util = require 'lua_util'
7 local mpool = require "rspamd_mempool"
8 local fun = require "fun"
9 local url = require "rspamd_url"
10
11 --[=========[ ******************* message ******************* ]=========]
12 msg = [[
13 From: <>
14 To: <[email protected]>
15 Subject: test
16 Content-Type: multipart/alternative;
17 boundary="_000_6be055295eab48a5af7ad4022f33e2d0_"
18
19 --_000_6be055295eab48a5af7ad4022f33e2d0_
20 Content-Type: text/plain; charset="utf-8"
21 Content-Transfer-Encoding: base64
22
23 Hello world
24
25
26 --_000_6be055295eab48a5af7ad4022f33e2d0_
27 Content-Type: text/html; charset="utf-8"
28
29 <html><body>
30 <a href="http://example.net">http://example.net</a>
31 <a href="http://example1.net">http://example1.net</a>
32 <a href="http://example2.net">http://example2.net</a>
33 <a href="http://example3.net">http://example3.net</a>
34 <a href="http://example4.net">http://example4.net</a>
35 <a href="http://domain1.com">http://domain1.com</a>
36 <a href="http://domain2.com">http://domain2.com</a>
37 <a href="http://domain3.com">http://domain3.com</a>
38 <a href="http://domain4.com">http://domain4.com</a>
39 <a href="http://domain5.com">http://domain5.com</a>
40 <a href="http://domain.com">http://example.net/</a>
41 <img src="http://example5.org">hahaha</img>
42 </html>
43 ]]
44 msg_img = [[
45 From: <>
46 To: <[email protected]>
47 Subject: test
48 Content-Type: multipart/alternative;
49 boundary="_000_6be055295eab48a5af7ad4022f33e2d0_"
50
51 --_000_6be055295eab48a5af7ad4022f33e2d0_
52 Content-Type: text/plain; charset="utf-8"
53 Content-Transfer-Encoding: base64
54
55 Hello world
56
57
58 --_000_6be055295eab48a5af7ad4022f33e2d0_
59 Content-Type: text/html; charset="utf-8"
60
61 <html><body>
62 <a href="http://example.net">http://example.net</a>
63 <a href="http://domain.com">http://example.net</a>
64 <img src="http://example5.org">hahaha</img>
65 </html>
66 ]]
67
68 local function prepare_actual_result(actual)
69 return fun.totable(fun.map(
70 function(u) return u:get_raw():gsub('^%w+://', '') end,
71 actual
72 ))
73 end
74
75 context("Lua util - extract_specific_urls plain", function()
76 local test_helper = require "rspamd_test_helper"
77
78 test_helper.init_url_parser()
79
80 local task_object = {
81 urls = {},
82 cache_set = function(self, ...) end,
83 cache_get = function(self, ...) end,
84 get_urls = function(self, need_emails) return self.urls end
85 }
86
87 local url_list = {
88 "google.com",
89 "mail.com",
90 "bizz.com",
91 "bing.com",
92 "example.com",
93 "gov.co.net",
94 "tesco.co.net",
95 "domain1.co.net",
96 "domain2.co.net",
97 "domain3.co.net",
98 "domain4.co.net",
99 "abc.org",
100 "icq.org",
101 "meet.org",
102 "domain1.org",
103 "domain2.org",
104 "domain3.org",
105 "test.com",
106 }
107
108 local cases = {
109 {expect = url_list, filter = nil, limit = 9999, need_emails = true, prefix = 'p'},
110 {expect = {}, filter = (function() return false end), limit = 9999, need_emails = true, prefix = 'p'},
111 {expect = {"domain4.co.net", "test.com", "domain3.org"}, filter = nil, limit = 3, need_emails = true, prefix = 'p'},
112 {
113 expect = {"gov.co.net", "tesco.co.net", "domain1.co.net", "domain2.co.net", "domain3.co.net", "domain4.co.net"},
114 filter = (function(s) return s:get_host():sub(-4) == ".net" end),
115 limit = 9999,
116 need_emails = true,
117 prefix = 'p'
118 },
119 {
120 input = {"a.google.com", "b.google.com", "c.google.com", "a.net", "bb.net", "a.bb.net", "b.bb.net"},
121 expect = {"a.bb.net", "b.google.com", "a.net", "bb.net", "a.google.com"},
122 filter = nil,
123 limit = 9999,
124 esld_limit = 2,
125 need_emails = true,
126 prefix = 'p'
127 },
128 {
129 input = {"[email protected]", "b.google.com", "c.google.com", "a.net", "bb.net", "a.bb.net", "b.bb.net"},
130 expect = {"[email protected]", "a.bb.net", "b.google.com", "a.net", "bb.net"},
131 filter = nil,
132 limit = 9999,
133 esld_limit = 2,
134 need_emails = true,
135 prefix = 'p'
136 }
137 }
138
139 local pool = mpool.create()
140
141 local function prepare_url_list(list)
142 return fun.totable(fun.map(
143 function (u) return url.create(pool, u) end,
144 list or url_list
145 ))
146 end
147
148 for i,c in ipairs(cases) do
149 test("extract_specific_urls, backward compatibility case #" .. i, function()
150 task_object.urls = prepare_url_list(c.input)
151 if (c.esld_limit) then
152 -- not awailable in deprecated version
153 return
154 end
155 local actual = util.extract_specific_urls(task_object, c.limit, c.need_emails, c.filter, c.prefix)
156
157 local actual_result = prepare_actual_result(actual)
158
159 --[[
160 local s = logger.slog("%1 =?= %2", c.expect, actual_result)
161 print(s) --]]
162
163 assert_rspamd_table_eq_sorted({actual = actual_result, expect = c.expect})
164 end)
165
166 test("extract_specific_urls " .. i, function()
167 task_object.urls = prepare_url_list(c.input)
168
169 local actual = util.extract_specific_urls({
170 task = task_object,
171 limit = c.limit,
172 esld_limit = c.esld_limit,
173 need_emails = c.need_emails,
174 filter = c.filter,
175 prefix = c.prefix,
176 })
177
178 local actual_result = prepare_actual_result(actual)
179
180 --[[
181 local s = logger.slog("case[%1] %2 =?= %3", i, c.expect, actual_result)
182 print(s) --]]
183
184 assert_rspamd_table_eq_sorted({actual = actual_result, expect = c.expect})
185 end)
186 end
187
188 test("extract_specific_urls, another case", function()
189 task_object.urls = prepare_url_list {"abc.net", "abc.com", "abc.net", "abc.za.org"}
190 local actual = util.extract_specific_urls(task_object, 3, true)
191
192 local actual_result = prepare_actual_result(actual)
193 --[[
194 local s = logger.slog("%1 =?= %2", c.expect, actual_result)
195 print(s) --]]
196
197 local expect = {"abc.com", "abc.net", "abc.za.org"}
198 assert_rspamd_table_eq_sorted({actual = actual_result, expect = expect})
199 end)
200 end)
201
202 context("Lua util - extract_specific_urls message", function()
203
204 --[[ ******************* kinda functional *************************************** ]]
205
206 local test_helper = require "rspamd_test_helper"
207 local cfg = rspamd_util.config_from_ucl(test_helper.default_config(),
208 "INIT_URL,INIT_LIBS,INIT_SYMCACHE,INIT_VALIDATE,INIT_PRELOAD_MAPS")
209 local res,task = rspamd_task.load_from_string(msg, cfg)
210
211 if not res then
212 assert(false, "failed to load message")
213 end
214
215 if not task:process_message() then
216 assert(false, "failed to process message")
217 end
218
219 test("extract_specific_urls - from email 1 limit", function()
220 local actual = util.extract_specific_urls({
221 task = task,
222 limit = 1,
223 esld_limit = 1,
224 })
225
226 local actual_result = prepare_actual_result(actual)
227
228 --[[
229 local s = logger.slog("case[%1] %2 =?= %3", i, expect, actual_result)
230 print(s) --]]
231
232 assert_rspamd_table_eq_sorted({actual = actual_result, expect = {"domain.com"}})
233
234 end)
235 test("extract_specific_urls - from email 2 limit", function()
236 local actual = util.extract_specific_urls({
237 task = task,
238 limit = 2,
239 esld_limit = 1,
240 })
241
242 local actual_result = prepare_actual_result(actual)
243
244 --[[
245 local s = logger.slog("case[%1] %2 =?= %3", i, expect, actual_result)
246 print(s) --]]
247
248 assert_rspamd_table_eq_sorted({actual = actual_result, expect = {"domain.com", "example.net"}})
249
250 end)
251
252 res,task = rspamd_task.load_from_string(msg_img, rspamd_config)
253
254 if not res then
255 assert_true(false, "failed to load message")
256 end
257
258 if not task:process_message() then
259 assert_true(false, "failed to process message")
260 end
261 test("extract_specific_urls - from email image 1 limit", function()
262 local actual = util.extract_specific_urls({
263 task = task,
264 limit = 1,
265 esld_limit = 1,
266 need_images = false,
267 })
268
269 local actual_result = prepare_actual_result(actual)
270
271 --[[
272 local s = logger.slog("case[%1] %2 =?= %3", i, expect, actual_result)
273 print(s) --]]
274
275 assert_rspamd_table_eq_sorted({actual = actual_result, expect = {"domain.com"}})
276
277 end)
278 test("extract_specific_urls - from email image 2 limit", function()
279 local actual = util.extract_specific_urls({
280 task = task,
281 limit = 2,
282 esld_limit = 1,
283 need_images = false,
284 })
285
286 local actual_result = prepare_actual_result(actual)
287
288 --[[
289 local s = logger.slog("case[%1] %2 =?= %3", i, expect, actual_result)
290 print(s) --]]
291
292 assert_rspamd_table_eq_sorted({actual = actual_result, expect = {"domain.com", "example.net"}})
293
294 end)
295 test("extract_specific_urls - from email image 3 limit, no images", function()
296 local actual = util.extract_specific_urls({
297 task = task,
298 limit = 3,
299 esld_limit = 1,
300 need_images = false,
301 })
302
303 local actual_result = prepare_actual_result(actual)
304
305 --[[
306 local s = logger.slog("case[%1] %2 =?= %3", i, expect, actual_result)
307 print(s) --]]
308
309 assert_rspamd_table_eq_sorted({actual = actual_result, expect = {"domain.com", "example.net"}})
310 end)
311 test("extract_specific_urls - from email image 3 limit, has images", function()
312 local actual = util.extract_specific_urls({
313 task = task,
314 limit = 3,
315 esld_limit = 1,
316 need_images = true,
317 })
318
319 local actual_result = prepare_actual_result(actual)
320
321 --[[
322 local s = logger.slog("case[%1] %2 =?= %3", i, expect, actual_result)
323 print(s) --]]
324
325 assert_rspamd_table_eq_sorted({actual = actual_result,
326 expect = {"domain.com", "example.net", "example5.org"}})
327 end)
328 test("extract_specific_urls - from email image 2 limit, has images", function()
329 local actual = util.extract_specific_urls({
330 task = task,
331 limit = 2,
332 esld_limit = 1,
333 need_images = true,
334 })
335
336 local actual_result = prepare_actual_result(actual)
337
338 --[[
339 local s = logger.slog("case[%1] %2 =?= %3", i, expect, actual_result)
340 print(s) --]]
341
342 assert_rspamd_table_eq_sorted({actual = actual_result,
343 expect = {"domain.com", "example.net"}})
344 end)
345 end)