summaryrefslogtreecommitdiffstats
path: root/test/functional/lua/miltertest
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 21:30:40 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 21:30:40 +0000
commit133a45c109da5310add55824db21af5239951f93 (patch)
treeba6ac4c0a950a0dda56451944315d66409923918 /test/functional/lua/miltertest
parentInitial commit. (diff)
downloadrspamd-133a45c109da5310add55824db21af5239951f93.tar.xz
rspamd-133a45c109da5310add55824db21af5239951f93.zip
Adding upstream version 3.8.1.upstream/3.8.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'test/functional/lua/miltertest')
-rw-r--r--test/functional/lua/miltertest/combined.lua35
-rw-r--r--test/functional/lua/miltertest/data.lua26
-rw-r--r--test/functional/lua/miltertest/data_dkim.lua23
-rw-r--r--test/functional/lua/miltertest/dkim_many.lua11
-rw-r--r--test/functional/lua/miltertest/dkim_one.lua11
-rw-r--r--test/functional/lua/miltertest/lib.lua119
-rw-r--r--test/functional/lua/miltertest/mt1.lua11
-rw-r--r--test/functional/lua/miltertest/mt2.lua11
-rw-r--r--test/functional/lua/miltertest/mt3.lua12
-rw-r--r--test/functional/lua/miltertest/mt4.lua11
10 files changed, 270 insertions, 0 deletions
diff --git a/test/functional/lua/miltertest/combined.lua b/test/functional/lua/miltertest/combined.lua
new file mode 100644
index 0000000..69fa2d6
--- /dev/null
+++ b/test/functional/lua/miltertest/combined.lua
@@ -0,0 +1,35 @@
+-- Combine tests
+
+dofile './lib.lua'
+dofile './data.lua'
+
+setup()
+
+local old_setup = setup
+local old_teardown = teardown
+
+local empty_function = function() end
+setup = empty_function
+teardown = empty_function
+
+local function shuffle(tbl)
+ local size = #tbl
+ for i = size, 1, -1 do
+ local rand = math.random(size)
+ tbl[i], tbl[rand] = tbl[rand], tbl[i]
+ end
+ return tbl
+end
+
+local files = {'mt1.lua','mt2.lua','mt3.lua','mt4.lua'}
+local num_files = #files
+for i = 1, num_files do
+ table.insert(files, files[i])
+end
+files = shuffle(files)
+
+for _, f in ipairs(files) do
+ dofile(f)
+end
+
+old_teardown()
diff --git a/test/functional/lua/miltertest/data.lua b/test/functional/lua/miltertest/data.lua
new file mode 100644
index 0000000..84b953f
--- /dev/null
+++ b/test/functional/lua/miltertest/data.lua
@@ -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")
diff --git a/test/functional/lua/miltertest/data_dkim.lua b/test/functional/lua/miltertest/data_dkim.lua
new file mode 100644
index 0000000..15adf15
--- /dev/null
+++ b/test/functional/lua/miltertest/data_dkim.lua
@@ -0,0 +1,23 @@
+multi_hdrs = {
+ ['Message-ID'] = '<a44q4StVFY04V4_4gOMYXjTgMDvmlSFzZxnoyJPHFwM@cacophony.za.org>',
+ ['From'] = 'Rspamd <foo@cacophony.za.org>',
+ ['To'] = 'nerf@example.org',
+ ['Subject'] = 'dkim test message',
+ ['User-Agent'] = 'Vi IMproved 8.1',
+ ['Content-Type'] = 'text/plain; charset=utf-8;',
+ ['MIME-Version'] = '1.0',
+ ['Date'] = 'Sat, 02 Feb 2019 10:34:54 +0000',
+}
+
+single_hdr = {
+ ['Message-ID'] = '<a44q4StVFY04V4_4gOMYXjTgMDvmlSFzZxnoyJPHFwM@cacophony.za.org>',
+ ['From'] = 'Rspamd <foo@invalid.za.org>',
+ ['To'] = 'nerf@example.org',
+ ['Subject'] = 'dkim test message',
+ ['User-Agent'] = 'Vi IMproved 8.1',
+ ['Content-Type'] = 'text/plain; charset=utf-8;',
+ ['MIME-Version'] = '1.0',
+ ['Date'] = 'Sat, 02 Feb 2019 10:34:54 +0000',
+}
+
+innocuous_msg = 'hello'
diff --git a/test/functional/lua/miltertest/dkim_many.lua b/test/functional/lua/miltertest/dkim_many.lua
new file mode 100644
index 0000000..70a3a1b
--- /dev/null
+++ b/test/functional/lua/miltertest/dkim_many.lua
@@ -0,0 +1,11 @@
+print('Check we get multiple dkim signatures')
+
+dofile './lib.lua'
+dofile './data_dkim.lua'
+
+setup()
+
+send_message(innocuous_msg, multi_hdrs, 'test-id', 'foo@cacophony.za.org', {'nerf@example.org'})
+check_headers(2)
+
+teardown()
diff --git a/test/functional/lua/miltertest/dkim_one.lua b/test/functional/lua/miltertest/dkim_one.lua
new file mode 100644
index 0000000..0c7def8
--- /dev/null
+++ b/test/functional/lua/miltertest/dkim_one.lua
@@ -0,0 +1,11 @@
+print('Check we get single dkim signature')
+
+dofile './lib.lua'
+dofile './data_dkim.lua'
+
+setup()
+
+send_message(innocuous_msg, single_hdr, 'test-id', 'foo@invalid.za.org', {'nerf@example.org'})
+check_headers(1)
+
+teardown()
diff --git a/test/functional/lua/miltertest/lib.lua b/test/functional/lua/miltertest/lib.lua
new file mode 100644
index 0000000..44dc76e
--- /dev/null
+++ b/test/functional/lua/miltertest/lib.lua
@@ -0,0 +1,119 @@
+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()
+ if conn then
+ mt.disconnect(conn)
+ end
+ conn = nil
+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
+
+function check_headers(count)
+ for i=0, count-1 do
+ local hdr = mt.getheader(conn, "DKIM-Signature", i)
+ if not hdr then
+ error (string.format("Signature %s not added", i))
+ end
+ end
+end
diff --git a/test/functional/lua/miltertest/mt1.lua b/test/functional/lua/miltertest/mt1.lua
new file mode 100644
index 0000000..019a852
--- /dev/null
+++ b/test/functional/lua/miltertest/mt1.lua
@@ -0,0 +1,11 @@
+print('Check we will accept a message')
+
+dofile './lib.lua'
+dofile './data.lua'
+
+setup()
+
+send_message(innocuous_msg, innocuous_hdrs, 'test-id', 'nerf@example.org', {'nerf@example.org'})
+check_accept()
+
+teardown()
diff --git a/test/functional/lua/miltertest/mt2.lua b/test/functional/lua/miltertest/mt2.lua
new file mode 100644
index 0000000..1c8fa83
--- /dev/null
+++ b/test/functional/lua/miltertest/mt2.lua
@@ -0,0 +1,11 @@
+print('Check we will reject a message')
+
+dofile './lib.lua'
+dofile './data.lua'
+
+setup()
+
+send_message(gtube)
+check_gtube()
+
+teardown()
diff --git a/test/functional/lua/miltertest/mt3.lua b/test/functional/lua/miltertest/mt3.lua
new file mode 100644
index 0000000..6b30126
--- /dev/null
+++ b/test/functional/lua/miltertest/mt3.lua
@@ -0,0 +1,12 @@
+print('Check we will rewrite subjects')
+
+dofile './lib.lua'
+dofile './data.lua'
+
+setup()
+
+send_message(gtube_rw_subject)
+check_accept()
+check_subject_rw()
+
+teardown()
diff --git a/test/functional/lua/miltertest/mt4.lua b/test/functional/lua/miltertest/mt4.lua
new file mode 100644
index 0000000..300cf69
--- /dev/null
+++ b/test/functional/lua/miltertest/mt4.lua
@@ -0,0 +1,11 @@
+print('Check we will defer messages')
+
+dofile './lib.lua'
+dofile './data.lua'
+
+setup()
+
+send_message(innocuous_msg, innocuous_hdrs, 'test-id', 'defer@example.org', {'nerf@example.org'})
+check_defer()
+
+teardown()