| 1 |
-- SMTP address parser tests |
| 2 |
|
| 3 |
context("SMTP address check functions", function() |
| 4 |
local logger = require("rspamd_logger") |
| 5 |
local ok, ffi = pcall(require, "ffi") |
| 6 |
if not ok then |
| 7 |
ffi = require("cffi") |
| 8 |
end |
| 9 |
local util = require("rspamd_util") |
| 10 |
local fun = require "fun" |
| 11 |
ffi.cdef [[ |
| 12 |
struct rspamd_email_address { |
| 13 |
const char *raw; |
| 14 |
const char *addr; |
| 15 |
const char *user; |
| 16 |
const char *domain; |
| 17 |
const char *name; |
| 18 |
|
| 19 |
unsigned raw_len; |
| 20 |
unsigned addr_len; |
| 21 |
unsigned domain_len; |
| 22 |
uint16_t user_len; |
| 23 |
unsigned char flags; |
| 24 |
}; |
| 25 |
struct rspamd_email_address * rspamd_email_address_from_smtp (const char *str, unsigned len); |
| 26 |
void rspamd_email_address_free (struct rspamd_email_address *addr); |
| 27 |
]] |
| 28 |
|
| 29 |
local cases_valid = { |
| 30 |
{ '<>', { addr = '' } }, |
| 31 |
{ '<[email protected]>', { user = 'a', domain = 'example.com', addr = '[email protected]' } }, |
| 32 |
{ '<[email protected]>', { user = 'a-b', domain = 'example.com', addr = '[email protected]' } }, |
| 33 |
{ '<[email protected]>', { user = 'a-b', domain = 'ex-ample.com', addr = '[email protected]' } }, |
| 34 |
{ '1367=dec2a6ce-81bd-4fa9-ad02-ec5956466c04=9=1655370@example.220-volt.ru', |
| 35 |
{ user = '1367=dec2a6ce-81bd-4fa9-ad02-ec5956466c04=9=1655370', |
| 36 |
domain = 'example.220-volt.ru', |
| 37 |
addr = '1367=dec2a6ce-81bd-4fa9-ad02-ec5956466c04=9=1655370@example.220-volt.ru' } }, |
| 38 |
{ '[email protected]', { user = 'notification+kjdm---m7wwd' } }, |
| 39 |
{ '[email protected]', { user = 'a', domain = 'example.com', addr = '[email protected]' } }, |
| 40 |
{ '[email protected]', { user = 'a+b', domain = 'example.com', addr = '[email protected]' } }, |
| 41 |
{ '"a"@example.com', { user = 'a', domain = 'example.com', addr = '[email protected]' } }, |
| 42 |
{ '"a+b"@example.com', { user = 'a+b', domain = 'example.com', addr = '[email protected]' } }, |
| 43 |
{ '"<>"@example.com', { user = '<>', domain = 'example.com', addr = '<>@example.com' } }, |
| 44 |
{ '<"<>"@example.com>', { user = '<>', domain = 'example.com', addr = '<>@example.com' } }, |
| 45 |
{ '"\\""@example.com', { user = '"', domain = 'example.com', addr = '"@example.com' } }, |
| 46 |
{ '"\\"abc"@example.com', { user = '"abc', domain = 'example.com', addr = '"[email protected]' } }, |
| 47 |
{ '<@domain1,@domain2,@domain3:[email protected]>', |
| 48 |
{ user = 'abc', domain = 'example.com', addr = '[email protected]' } }, |
| 49 |
-- SMTP UTF8 |
| 50 |
{ 'ñ@example.com', { user = 'ñ', domain = 'example.com' } }, |
| 51 |
{ 'ñ@ололо.лол', { user = 'ñ', domain = 'ололо.лол' } } |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
fun.each(function(case) |
| 56 |
test("Parse valid smtp addr: " .. case[1], function() |
| 57 |
local st = ffi.C.rspamd_email_address_from_smtp(case[1], #case[1]) |
| 58 |
|
| 59 |
assert_not_nil(st, "should be able to parse " .. case[1]) |
| 60 |
|
| 61 |
fun.each(function(k, ex) |
| 62 |
if k == 'user' then |
| 63 |
local str = ffi.string(st.user, st.user_len) |
| 64 |
assert_equal(str, ex) |
| 65 |
elseif k == 'domain' then |
| 66 |
local str = ffi.string(st.domain, st.domain_len) |
| 67 |
assert_equal(str, ex) |
| 68 |
elseif k == 'addr' then |
| 69 |
local str = ffi.string(st.addr, st.addr_len) |
| 70 |
assert_equal(str, ex) |
| 71 |
end |
| 72 |
end, case[2]) |
| 73 |
ffi.C.rspamd_email_address_free(st) |
| 74 |
end) |
| 75 |
end, cases_valid) |
| 76 |
|
| 77 |
local cases_invalid = { |
| 78 |
'a', |
| 79 |
'a"b"@example.com', |
| 80 |
'a"@example.com', |
| 81 |
'"[email protected]', |
| 82 |
'<[email protected]', |
| 83 |
'[email protected]>', |
| 84 |
'<[email protected]>', |
| 85 |
'<[email protected]>>', |
| 86 |
'<[email protected]><>', |
| 87 |
} |
| 88 |
|
| 89 |
-- Helper to check if a cdata pointer is NULL (cffi-lua compatibility) |
| 90 |
local function is_null(ptr) |
| 91 |
if ptr == nil then return true end |
| 92 |
-- For cffi-lua, check if pointer equals NULL |
| 93 |
return ptr == ffi.new('void*') |
| 94 |
end |
| 95 |
|
| 96 |
fun.each(function(case) |
| 97 |
test("Parse invalid smtp addr: " .. case, function() |
| 98 |
local st = ffi.C.rspamd_email_address_from_smtp(case, #case) |
| 99 |
|
| 100 |
assert_true(is_null(st), "should not be able to parse " .. case) |
| 101 |
end) |
| 102 |
end, cases_invalid) |
| 103 |
|
| 104 |
if os.getenv("RSPAMD_LUA_EXPENSIVE_TESTS") then |
| 105 |
test("Speed test", function() |
| 106 |
local case = '<@domain1,@domain2,@domain3:abc%[email protected]>' |
| 107 |
local niter = 100000 |
| 108 |
local total = 0 |
| 109 |
|
| 110 |
for i = 1, niter do |
| 111 |
local ncase = string.format(case, i) |
| 112 |
local t1 = util.get_ticks() |
| 113 |
local st = ffi.C.rspamd_email_address_from_smtp(ncase, #ncase) |
| 114 |
local t2 = util.get_ticks() |
| 115 |
ffi.C.rspamd_email_address_free(st) |
| 116 |
total = total + t2 - t1 |
| 117 |
end |
| 118 |
|
| 119 |
print(string.format('Spend %f seconds in processing addrs', total)) |
| 120 |
end) |
| 121 |
end |
| 122 |
end) |