summaryrefslogtreecommitdiffstats
path: root/test/lua/unit/lua_util.extract_specific_urls.lua
blob: a7e2f9f48ac8c5ba5553a3c49367c8bd71694cd7 (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
local msg, msg_img
local logger = require "rspamd_logger"
local rspamd_util = require "rspamd_util"
local rspamd_task = require "rspamd_task"
local util  = require 'lua_util'
local mpool = require "rspamd_mempool"
local fun   = require "fun"
local url   = require "rspamd_url"

--[=========[ *******************  message  ******************* ]=========]
msg = [[
From: <>
To: <nobody@example.com>
Subject: test
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="http://example1.net">http://example1.net</a>
<a href="http://example2.net">http://example2.net</a>
<a href="http://example3.net">http://example3.net</a>
<a href="http://example4.net">http://example4.net</a>
<a href="http://domain1.com">http://domain1.com</a>
<a href="http://domain2.com">http://domain2.com</a>
<a href="http://domain3.com">http://domain3.com</a>
<a href="http://domain4.com">http://domain4.com</a>
<a href="http://domain5.com">http://domain5.com</a>
<a href="http://domain.com">http://example.net/</a>
<img src="http://example5.org">hahaha</img>
</html>
]]
msg_img = [[
From: <>
To: <nobody@example.com>
Subject: test
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="http://domain.com">http://example.net</a>
<img src="http://example5.org">hahaha</img>
</html>
]]

local function prepare_actual_result(actual)
  return fun.totable(fun.map(
      function(u) return u:get_raw():gsub('^%w+://', '') end,
      actual
  ))
end

context("Lua util - extract_specific_urls plain", function()
  local test_helper = require "rspamd_test_helper"

  test_helper.init_url_parser()

  local task_object = {
    urls      = {},
    cache_set = function(self, ...) end,
    cache_get = function(self, ...) end,
    get_urls  = function(self, need_emails) return self.urls end
  }

  local url_list = {
    "google.com",
    "mail.com",
    "bizz.com",
    "bing.com",
    "example.com",
    "gov.co.net",
    "tesco.co.net",
    "domain1.co.net",
    "domain2.co.net",
    "domain3.co.net",
    "domain4.co.net",
    "abc.org",
    "icq.org",
    "meet.org",
    "domain1.org",
    "domain2.org",
    "domain3.org",
    "test.com",
  }

  local cases = {
    {expect = url_list, filter = nil, limit = 9999, need_emails = true, prefix = 'p'},
    {expect = {}, filter = (function() return false end), limit = 9999, need_emails = true, prefix = 'p'},
    {expect = {"domain4.co.net", "test.com", "domain3.org"}, filter = nil, limit = 3, need_emails = true, prefix = 'p'},
    {
      expect = {"gov.co.net", "tesco.co.net", "domain1.co.net", "domain2.co.net", "domain3.co.net", "domain4.co.net"},
      filter = (function(s) return s:get_host():sub(-4) == ".net" end),
      limit = 9999,
      need_emails = true,
      prefix = 'p'
    },
    {
      input  = {"a.google.com", "b.google.com", "c.google.com", "a.net", "bb.net", "a.bb.net", "b.bb.net"},
      expect = {"a.bb.net", "b.google.com", "a.net", "bb.net", "a.google.com"},
      filter = nil,
      limit = 9999,
      esld_limit = 2,
      need_emails = true,
      prefix = 'p'
    },
    {
      input  = {"abc@a.google.com", "b.google.com", "c.google.com", "a.net", "bb.net", "a.bb.net", "b.bb.net"},
      expect = {"abc@a.google.com", "a.bb.net", "b.google.com", "a.net", "bb.net"},
      filter = nil,
      limit = 9999,
      esld_limit = 2,
      need_emails = true,
      prefix = 'p'
    }
  }

  local pool = mpool.create()

  local function prepare_url_list(list)
    return fun.totable(fun.map(
    function (u) return url.create(pool, u) end,
    list or url_list
    ))
  end

  for i,c in ipairs(cases) do
    test("extract_specific_urls, backward compatibility case #" .. i, function()
      task_object.urls = prepare_url_list(c.input)
      if (c.esld_limit) then
        -- not awailable in deprecated version
        return
      end
      local actual = util.extract_specific_urls(task_object, c.limit, c.need_emails, c.filter, c.prefix)

      local actual_result = prepare_actual_result(actual)

      --[[
        local s = logger.slog("%1 =?= %2", c.expect, actual_result)
        print(s) --]]

      assert_rspamd_table_eq_sorted({actual = actual_result, expect = c.expect})
    end)

    test("extract_specific_urls " .. i, function()
      task_object.urls = prepare_url_list(c.input)

      local actual = util.extract_specific_urls({
        task = task_object,
        limit = c.limit,
        esld_limit = c.esld_limit,
        need_emails = c.need_emails,
        filter = c.filter,
        prefix = c.prefix,
      })

      local actual_result = prepare_actual_result(actual)

      --[[
        local s = logger.slog("case[%1] %2 =?= %3", i, c.expect, actual_result)
        print(s) --]]

      assert_rspamd_table_eq_sorted({actual = actual_result, expect = c.expect})
    end)
  end

  test("extract_specific_urls, another case", function()
    task_object.urls = prepare_url_list {"abc.net", "abc.com", "abc.net", "abc.za.org"}
    local actual = util.extract_specific_urls(task_object, 3, true)

    local actual_result = prepare_actual_result(actual)
    --[[
      local s = logger.slog("%1 =?= %2", c.expect, actual_result)
      print(s) --]]

    local expect = {"abc.com", "abc.net", "abc.za.org"}
    assert_rspamd_table_eq_sorted({actual = actual_result, expect = expect})
  end)
end)

context("Lua util - extract_specific_urls message", function()

--[[ ******************* kinda functional *************************************** ]]

  local test_helper = require "rspamd_test_helper"
  local cfg = rspamd_util.config_from_ucl(test_helper.default_config(),
      "INIT_URL,INIT_LIBS,INIT_SYMCACHE,INIT_VALIDATE,INIT_PRELOAD_MAPS")
  local res,task = rspamd_task.load_from_string(msg, cfg)

  if not res then
    assert(false, "failed to load message")
  end

  if not task:process_message() then
    assert(false, "failed to process message")
  end

  test("extract_specific_urls - from email 1 limit", function()
    local actual = util.extract_specific_urls({
      task = task,
      limit = 1,
      esld_limit = 1,
    })

    local actual_result = prepare_actual_result(actual)

    --[[
      local s = logger.slog("case[%1] %2 =?= %3", i, expect, actual_result)
      print(s) --]]

    assert_rspamd_table_eq_sorted({actual = actual_result, expect = {"domain.com"}})

  end)
  test("extract_specific_urls - from email 2 limit", function()
    local actual = util.extract_specific_urls({
      task = task,
      limit = 2,
      esld_limit = 1,
    })

    local actual_result = prepare_actual_result(actual)

    --[[
      local s = logger.slog("case[%1] %2 =?= %3", i, expect, actual_result)
      print(s) --]]

    assert_rspamd_table_eq_sorted({actual = actual_result, expect = {"domain.com", "example.net"}})

  end)

  res,task = rspamd_task.load_from_string(msg_img, rspamd_config)

  if not res then
    assert_true(false, "failed to load message")
  end

  if not task:process_message() then
    assert_true(false, "failed to process message")
  end
  test("extract_specific_urls - from email image 1 limit", function()
    local actual = util.extract_specific_urls({
      task = task,
      limit = 1,
      esld_limit = 1,
      need_images = false,
    })

    local actual_result = prepare_actual_result(actual)

    --[[
      local s = logger.slog("case[%1] %2 =?= %3", i, expect, actual_result)
      print(s) --]]

    assert_rspamd_table_eq_sorted({actual = actual_result, expect = {"domain.com"}})

  end)
  test("extract_specific_urls - from email image 2 limit", function()
    local actual = util.extract_specific_urls({
      task = task,
      limit = 2,
      esld_limit = 1,
      need_images = false,
    })

    local actual_result = prepare_actual_result(actual)

    --[[
      local s = logger.slog("case[%1] %2 =?= %3", i, expect, actual_result)
      print(s) --]]

    assert_rspamd_table_eq_sorted({actual = actual_result, expect = {"domain.com", "example.net"}})

  end)
  test("extract_specific_urls - from email image 3 limit, no images", function()
    local actual = util.extract_specific_urls({
      task = task,
      limit = 3,
      esld_limit = 1,
      need_images = false,
    })

    local actual_result = prepare_actual_result(actual)

    --[[
      local s = logger.slog("case[%1] %2 =?= %3", i, expect, actual_result)
      print(s) --]]

    assert_rspamd_table_eq_sorted({actual = actual_result, expect = {"domain.com", "example.net"}})
  end)
  test("extract_specific_urls - from email image 3 limit, has images", function()
    local actual = util.extract_specific_urls({
      task = task,
      limit = 3,
      esld_limit = 1,
      need_images = true,
    })

    local actual_result = prepare_actual_result(actual)

    --[[
      local s = logger.slog("case[%1] %2 =?= %3", i, expect, actual_result)
      print(s) --]]

    assert_rspamd_table_eq_sorted({actual = actual_result,
                            expect = {"domain.com", "example.net", "example5.org"}})
  end)
  test("extract_specific_urls - from email image 2 limit, has images", function()
    local actual = util.extract_specific_urls({
      task = task,
      limit = 2,
      esld_limit = 1,
      need_images = true,
    })

    local actual_result = prepare_actual_result(actual)

    --[[
      local s = logger.slog("case[%1] %2 =?= %3", i, expect, actual_result)
      print(s) --]]

    assert_rspamd_table_eq_sorted({actual = actual_result,
                            expect = {"domain.com", "example.net"}})
  end)
end)