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

context("SMTP address check functions", function()
  local logger = require("rspamd_logger")
  local ffi = require("ffi")
  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 user_len;
    unsigned domain_len;
    unsigned name_len;
    int flags;
  };
  struct rspamd_email_address * rspamd_email_address_from_smtp (const char *str, unsigned len);
  void rspamd_email_address_unref (struct rspamd_email_address *addr);
  ]]

  test("Parse addrs", function()
    local cases_valid = {
      {'<>', {addr = ''}},
      {'<a@example.com>', {user = 'a', domain = 'example.com', addr = 'a@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'}},
      {'"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'}},

    }

    each(function(case)
      local st = ffi.C.rspamd_email_address_from_smtp(case[1], #case[1])

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

      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_unref(st)
    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>>',
      '<a@example.com><>',
    }

    each(function(case)
      local st = ffi.C.rspamd_email_address_from_smtp(case, #case)

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