aboutsummaryrefslogtreecommitdiffstats
path: root/test/lua/unit/smtp_addr.lua
blob: 16f59d07cb62f9ceaf8a1d3f415ccf6315d82609 (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
-- SMTP address parser tests

context("SMTP address check functions", function()
  local logger = require("rspamd_logger")
  local ffi = require("ffi")
  local util = require("rspamd_util")
  local fun = require "fun"
  ffi.cdef [[
  struct rspamd_email_address {
    const char *raw;
    const char *addr;
    const char *user;
    const char *domain;
    const char *name;

    unsigned raw_len;
    unsigned addr_len;
    unsigned domain_len;
    uint16_t user_len;
    unsigned char flags;
  };
  struct rspamd_email_address * rspamd_email_address_from_smtp (const char *str, unsigned len);
  void rspamd_email_address_free (struct rspamd_email_address *addr);
  ]]

  local cases_valid = {
    { '<>', { addr = '' } },
    { '<a@example.com>', { user = 'a', domain = 'example.com', addr = 'a@example.com' } },
    { '<a-b@example.com>', { user = 'a-b', domain = 'example.com', addr = 'a-b@example.com' } },
    { '<a-b@ex-ample.com>', { user = 'a-b', domain = 'ex-ample.com', addr = 'a-b@ex-ample.com' } },
    { '1367=dec2a6ce-81bd-4fa9-ad02-ec5956466c04=9=1655370@example.220-volt.ru',
      { user = '1367=dec2a6ce-81bd-4fa9-ad02-ec5956466c04=9=1655370',
        domain = 'example.220-volt.ru',
        addr = '1367=dec2a6ce-81bd-4fa9-ad02-ec5956466c04=9=1655370@example.220-volt.ru' } },
    { 'notification+kjdm---m7wwd@facebookmail.com', { user = 'notification+kjdm---m7wwd' } },
    { 'a@example.com', { user = 'a', domain = 'example.com', addr = 'a@example.com' } },
    { 'a+b@example.com', { user = 'a+b', domain = 'example.com', addr = 'a+b@example.com' } },
    { '"a"@example.com', { user = 'a', domain = 'example.com', addr = 'a@example.com' } },
    { '"a+b"@example.com', { user = 'a+b', domain = 'example.com', addr = 'a+b@example.com' } },
    { '"<>"@example.com', { user = '<>', domain = 'example.com', addr = '<>@example.com' } },
    { '<"<>"@example.com>', { user = '<>', domain = 'example.com', addr = '<>@example.com' } },
    { '"\\""@example.com', { user = '"', domain = 'example.com', addr = '"@example.com' } },
    { '"\\"abc"@example.com', { user = '"abc', domain = 'example.com', addr = '"abc@example.com' } },
    { '<@domain1,@domain2,@domain3:abc@example.com>',
      { user = 'abc', domain = 'example.com', addr = 'abc@example.com' } },
    -- SMTP UTF8
    { 'ñ@example.com', { user = 'ñ', domain = 'example.com' } },
    { 'ñ@ололо.лол', { user = 'ñ', domain = 'ололо.лол' } }

  }

  fun.each(function(case)
    test("Parse valid smtp addr: " .. case[1], function()
      local st = ffi.C.rspamd_email_address_from_smtp(case[1], #case[1])

      assert_not_nil(st, "should be able to parse " .. case[1])

      fun.each(function(k, ex)
        if k == 'user' then
          local str = ffi.string(st.user, st.user_len)
          assert_equal(str, ex)
        elseif k == 'domain' then
          local str = ffi.string(st.domain, st.domain_len)
          assert_equal(str, ex)
        elseif k == 'addr' then
          local str = ffi.string(st.addr, st.addr_len)
          assert_equal(str, ex)
        end
      end, case[2])
      ffi.C.rspamd_email_address_free(st)
    end)
  end, cases_valid)

  local cases_invalid = {
    'a',
    'a"b"@example.com',
    'a"@example.com',
    '"a@example.com',
    '<a@example.com',
    'a@example.com>',
    '<a@.example.com>',
    '<a@example.com>>',
    '<a@example.com><>',
  }

  fun.each(function(case)
    test("Parse invalid smtp addr: " .. case, function()
      local st = ffi.C.rspamd_email_address_from_smtp(case, #case)

      assert_nil(st, "should not be able to parse " .. case)
    end)
  end, cases_invalid)

  if os.getenv("RSPAMD_LUA_EXPENSIVE_TESTS") then
    test("Speed test", function()
      local case = '<@domain1,@domain2,@domain3:abc%d@example.com>'
      local niter = 100000
      local total = 0

      for i = 1, niter do
        local ncase = string.format(case, i)
        local t1 = util.get_ticks()
        local st = ffi.C.rspamd_email_address_from_smtp(ncase, #ncase)
        local t2 = util.get_ticks()
        ffi.C.rspamd_email_address_free(st)
        total = total + t2 - t1
      end

      print(string.format('Spend %f seconds in processing addrs', total))
    end)
  end
end)