Browse Source

[Test] Extend milter tests

tags/1.7.0
Andrew Lewis 6 years ago
parent
commit
f608f7d32a

+ 24
- 7
test/functional/cases/180_milter.robot View File

@@ -8,16 +8,33 @@ Variables ${TESTDIR}/lib/vars.py

*** Variables ***
${RSPAMD_SCOPE} Suite
${URL_TLD} ${TESTDIR}/../lua/unit/test_tld.dat
${URL_TLD} ${TESTDIR}/../lua/unit/test_tld.dat

*** Test Cases ***
SIMPLE MILTER TEST
${result} = Run Process miltertest -Dport\=${PORT_PROXY} -Dhost\=${LOCAL_ADDR} -s ${TESTDIR}/lua/miltertest/mt1.lua
Follow Rspamd Log
Should Match Regexp ${result.stderr} ^$
Should Match Regexp ${result.stdout} ^$
Should Be Equal As Integers ${result.rc} 0 msg=${result.stdout} values=false
ACCEPT
Milter Test mt1.lua

REJECT
Milter Test mt2.lua

REWRITE SUBJECT
Milter Test mt3.lua

DEFER
Milter Test mt4.lua

COMBINED TEST
[Tags] isbroken
Milter Test combined.lua

*** Keywords ***
Milter Setup
Generic Setup CONFIG=${TESTDIR}/configs/milter.conf

Milter Test
[Arguments] ${mtlua}
${result} = Run Process miltertest -Dport\=${PORT_PROXY} -Dhost\=${LOCAL_ADDR} -s ${TESTDIR}/lua/miltertest/${mtlua}
... cwd=${TESTDIR}/lua/miltertest
Follow Rspamd Log
Should Match Regexp ${result.stderr} ^$
Should Be Equal As Integers ${result.rc} 0 msg=${result.stdout} values=false

+ 1
- 0
test/functional/configs/milter.conf View File

@@ -51,6 +51,7 @@ modules {
path = "${TESTDIR}/../../src/plugins/lua/"
}
lua = "${INSTALLROOT}/share/rspamd/rules/rspamd.lua"
lua = "${TESTDIR}/lua/params.lua"
milter_headers {
extended_spam_headers = true;
skip_local = false;

+ 20
- 0
test/functional/lua/miltertest/combined.lua View File

@@ -0,0 +1,20 @@
-- Combine tests

require './lib'
require './data'

setup()

local old_setup = setup
local old_teardown = teardown

local empty_function = function() end
setup = empty_function
teardown = empty_function

dofile('mt1.lua')
dofile('mt2.lua')
dofile('mt3.lua')
dofile('mt4.lua')

old_teardown()

+ 26
- 0
test/functional/lua/miltertest/data.lua View File

@@ -0,0 +1,26 @@
innocuous_hdrs = {
['Message-ID'] = '<20180202155326.Horde.GfEWpxCo_Dip2xJswIpQNgK@example.org>',
['From'] = 'Andrew Lewis <nerf@example.org>',
['To'] = 'nerf@example.org',
['Subject'] = 'innocuous test message',
['User-Agent'] = 'Horde Application Framework 5',
['Content-Type'] = 'text/plain; charset=utf-8; format=flowed; DelSp=Yes',
['MIME-Version'] = '1.0',
['Content-Disposition'] = 'inline',
['Date'] = 'Fri, 02 Feb 2018 15:53:26 +0200',
}

default_hdrs = {
['Subject'] = 'spam message',
}

innocuous_msg = 'Hello Rupert'

gtube = [[lo

XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X

thx]]

gtube_add_header = string.gsub(gtube, "XJS", "YJS")
gtube_rw_subject = string.gsub(gtube, "XJS", "ZJS")

+ 107
- 0
test/functional/lua/miltertest/lib.lua View File

@@ -0,0 +1,107 @@
function setup(c_ip, helo, hn)
if not c_ip then c_ip = "127.0.0.1" end
if not helo then helo = "it.is.i" end
if not hn then hn = "localhost" end
conn = mt.connect("inet:" .. port .. "@" .. host)
if conn == nil then
error "mt.connect() failed"
end
if mt.conninfo(conn, hn, c_ip) then
error "mt.conninfo() failed"
end
if mt.getreply(conn) ~= SMFIR_CONTINUE then
error "mt.conninfo() unexpected reply"
end
if mt.helo(conn, helo) then
error "mt.helo() failed"
end
if mt.getreply(conn) ~= SMFIR_CONTINUE then
error "mt.helo() unexpected reply"
end
end

function teardown()
mt.disconnect(conn)
end

function send_message(body, hdrs, id, sender, rcpts)
mt.macro(conn, SMFIC_MAIL, "i", id or "test-id")
if mt.mailfrom(conn, sender or "sender@example.com") then
error "mt.mailfrom() failed"
end
if mt.getreply(conn) ~= SMFIR_CONTINUE then
error "mt.mailfrom() unexpected reply"
end
if not rcpts then
rcpts = {"rcpt@example.com"}
end
for _, r in ipairs(rcpts) do
mt.rcptto(conn, r)
end
if not hdrs then
hdrs = default_hdrs
end
if not hdrs['From'] then
hdrs['From'] = sender or "sender@example.com"
end
for k, v in pairs(hdrs) do
if mt.header(conn, k, v) then
error (string.format("mt.header(%s) failed", k))
end
end
if mt.eoh(conn) then
error "mt.eoh() failed"
end
if mt.getreply(conn) ~= SMFIR_CONTINUE then
error "mt.eoh() unexpected reply"
end
if mt.bodystring(conn, body .. "\r\n") then
error "mt.bodystring() failed"
end
if mt.getreply(conn) ~= SMFIR_CONTINUE then
error "mt.bodystring() unexpected reply"
end
if mt.eom(conn) then
error "mt.eom() failed"
end
end

function check_accept()
local rc = mt.getreply(conn)
if rc ~= SMFIR_ACCEPT then
error (string.format("mt.eom() unexpected reply: %s", rc))
end
end

function check_gtube(code, ecode, msg)
if not mt.eom_check(conn, MT_SMTPREPLY, code or '554', ecode or '5.7.1', msg or 'Gtube pattern') then
error "mt.eom_check() failed"
end
local rc = mt.getreply(conn)
if rc ~= SMFIR_REPLYCODE then
error (string.format("mt.eom() unexpected reply: %s", rc))
end
end

function check_defer(code, ecode, msg)
if not mt.eom_check(conn, MT_SMTPREPLY, code or '451', ecode or '4.7.1', msg or 'Try much later') then
error "mt.eom_check() failed"
end
local rc = mt.getreply(conn)
if rc ~= SMFIR_REPLYCODE then
error (string.format("mt.eom() unexpected reply: %s", rc))
end
end

function check_subject_rw(subj, tmpl)
if not subj then
subj = default_hdrs['Subject']
end
if not tmpl then
tmpl = "*** SPAM *** %s"
end
local new_subj = string.format(tmpl, subj)
if not mt.eom_check(conn, MT_HDRCHANGE, "Subject", new_subj) then
error "subject not rewritten"
end
end

+ 7
- 54
test/functional/lua/miltertest/mt1.lua View File

@@ -1,59 +1,11 @@
print('Check we will accept a message')

conn = mt.connect("inet:" .. port .. "@" .. host)
if conn == nil then
error "mt.connect() failed"
end
if mt.conninfo(conn, "localhost", "127.0.0.1") then
error "mt.conninfo() failed"
end
if mt.getreply(conn) ~= SMFIR_CONTINUE then
error "mt.conninfo() unexpected reply"
end
require './lib'
require './data'

if mt.helo(conn, "it.is.i") then
error "mt.helo() failed"
end
if mt.getreply(conn) ~= SMFIR_CONTINUE then
error "mt.helo() unexpected reply"
end
mt.macro(conn, SMFIC_MAIL, "i", "test-id")
if mt.mailfrom(conn, "sender@example.com") then
error "mt.mailfrom() failed"
end
if mt.getreply(conn) ~= SMFIR_CONTINUE then
error "mt.mailfrom() unexpected reply"
end
mt.rcptto(conn, "rcpt@example.com")
setup()

if mt.header(conn, "From", "honest@sender") then
error "mt.header(From) failed"
end
send_message(innocuous_msg, innocuous_hdrs, 'test-id', 'nerf@example.org', {'nerf@example.org'})
check_accept()

if mt.getreply(conn) ~= SMFIR_CONTINUE then
error "mt.header(From) unexpected reply"
end

if mt.eoh(conn) then
error "mt.eoh() failed"
end

if mt.getreply(conn) ~= SMFIR_CONTINUE then
error "mt.eoh() unexpected reply"
end

if mt.bodystring(conn, "This is a simple test!\r\n") then
error "mt.bodystring() failed"
end
if mt.getreply(conn) ~= SMFIR_CONTINUE then
error "mt.bodystring() unexpected reply"
end

if mt.eom(conn) then
error "mt.eom() failed"
end
if mt.getreply(conn) ~= SMFIR_ACCEPT then
error "mt.eom() unexpected reply"
end

mt.disconnect(conn)
teardown()

+ 11
- 0
test/functional/lua/miltertest/mt2.lua View File

@@ -0,0 +1,11 @@
print('Check we will reject a message')

require './lib'
require './data'

setup()

send_message(gtube)
check_gtube()

teardown()

+ 12
- 0
test/functional/lua/miltertest/mt3.lua View File

@@ -0,0 +1,12 @@
print('Check we will rewrite subjects')

require './lib'
require './data'

setup()

send_message(gtube_rw_subject)
check_accept()
check_subject_rw()

teardown()

+ 11
- 0
test/functional/lua/miltertest/mt4.lua View File

@@ -0,0 +1,11 @@
print('Check we will defer messages')

require './lib'
require './data'

setup()

send_message(innocuous_msg, innocuous_hdrs, 'test-id', 'defer@example.org', {'nerf@example.org'})
check_defer()

teardown()

+ 80
- 0
test/functional/lua/params.lua View File

@@ -0,0 +1,80 @@
rspamd_config.TEST_RCPT = {
callback = function(task)
local l = {}
local rcpts = task:get_recipients(1)
if not rcpts then return end
for _, r in ipairs(rcpts) do
table.insert(l, r['addr'])
end
table.sort(l)
local t = table.concat(l, ",")
return true, t
end
}

rspamd_config.TEST_HELO = {
callback = function(task)
local helo = task:get_helo()
if not helo then return end
return true, helo
end
}

rspamd_config.TEST_HOSTNAME = {
callback = function(task)
local h = task:get_hostname()
if not h then return end
return true, h
end
}

rspamd_config.TEST_SMTP_FROM = {
callback = function(task)
local f = task:get_from('smtp')
if not (f and f[1] and f[1].addr) then return end
return true, f[1].addr
end
}

rspamd_config.TEST_MTA_TAG = {
callback = function(task)
local h = task:get_request_header('MTA-Tag')
if not h then return end
return true, tostring(h)
end
}

rspamd_config.TEST_USER = {
callback = function(task)
local u = task:get_user()
if not u then return end
return true, u
end
}

rspamd_config.TEST_QUEUEID = {
callback = function(task)
local q = task:get_queue_id()
if not q then return end
return true, q
end
}

rspamd_config.TEST_IPADDR = {
callback = function(task)
local i = task:get_from_ip()
if not (i and i:is_valid()) then return end
return true, tostring(i)
end
}

rspamd_config.FORCE_DEFER = {
callback = function(task)
local f = task:get_from('smtp')
if not (f and f[1] and f[1].addr) then return end
if f[1].addr == "defer@example.org" then
task:set_pre_result('soft reject', 'Try much later')
return true
end
end
}

Loading…
Cancel
Save