]> source.dussan.org Git - rspamd.git/commitdiff
[Test] Add tests for lua_udp
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 21 Jan 2019 17:04:05 +0000 (17:04 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 21 Jan 2019 17:04:05 +0000 (17:04 +0000)
test/functional/cases/310_udp.robot [new file with mode: 0644]
test/functional/lua/udp.lua [new file with mode: 0644]
test/functional/util/dummy_udp.py [changed mode: 0644->0755]

diff --git a/test/functional/cases/310_udp.robot b/test/functional/cases/310_udp.robot
new file mode 100644 (file)
index 0000000..2ecd1cf
--- /dev/null
@@ -0,0 +1,46 @@
+*** Settings ***
+Test Setup      UDP Setup
+Test Teardown   UDP Teardown
+Library         Process
+Library         ${TESTDIR}/lib/rspamd.py
+Resource        ${TESTDIR}/lib/rspamd.robot
+Variables       ${TESTDIR}/lib/vars.py
+
+*** Variables ***
+${URL_TLD}      ${TESTDIR}/../lua/unit/test_tld.dat
+${CONFIG}       ${TESTDIR}/configs/lua_test.conf
+${MESSAGE}      ${TESTDIR}/messages/spam_message.eml
+${RSPAMD_SCOPE}  Test
+
+*** Test Cases ***
+Simple UDP request
+  ${result} =  Scan Message With Rspamc  ${MESSAGE}
+  Check Rspamc  ${result}  UDP_SUCCESS (0.00)[helloworld]
+
+Sendonly UDP request
+  ${result} =  Scan Message With Rspamc  ${MESSAGE}
+  Check Rspamc  ${result}  UDP_SENDTO
+
+Errored UDP request
+  ${result} =  Scan Message With Rspamc  ${MESSAGE}
+  Check Rspamc  ${result}  UDP_FAIL (0.00)[read timeout]
+
+*** Keywords ***
+Lua Setup
+  [Arguments]  ${LUA_SCRIPT}
+  Set Global Variable  ${LUA_SCRIPT}
+  Generic Setup
+
+UDP Setup
+  Run Dummy UDP
+  Lua Setup  ${TESTDIR}/lua/udp.lua
+
+UDP Teardown
+  ${udp_pid} =  Get File  /tmp/dummy_udp.pid
+  Shutdown Process With Children  ${udp_pid}
+  Normal Teardown
+
+Run Dummy UDP
+  [Arguments]
+  ${result} =  Start Process  ${TESTDIR}/util/dummy_udp.py  5005
+  Wait Until Created  /tmp/dummy_udp.pid
diff --git a/test/functional/lua/udp.lua b/test/functional/lua/udp.lua
new file mode 100644 (file)
index 0000000..0ed4b15
--- /dev/null
@@ -0,0 +1,81 @@
+--[[[
+-- Just a test for UDP API
+--]]
+
+local rspamd_udp = require "rspamd_udp"
+local logger = require "rspamd_logger"
+
+-- [[ old fashioned callback api ]]
+local function simple_udp_async_symbol(task)
+  logger.errx(task, 'udp_symbol: begin')
+  local function udp_cb(success, data)
+    logger.errx(task, 'udp_cb: got reply: %s', data)
+
+    if success then
+      task:insert_result('UDP_SUCCESS', 1.0, data)
+    else
+      task:insert_result('UDP_FAIL', 1.0, data)
+    end
+  end
+  rspamd_udp:sendto({
+    task = task,
+    callback = udp_cb,
+    host = '127.0.0.1',
+    data = {'hello', 'world'},
+    port = 5005,
+  })
+end
+
+rspamd_config:register_symbol({
+  name = 'UDP_SUCCESS',
+  score = 0.0,
+  callback = simple_udp_async_symbol,
+})
+
+local function send_only_udp(task)
+  logger.errx(task, 'udp_symbol_sendonly: begin')
+  if rspamd_udp:sendto({
+    task = task,
+    host = '127.0.0.1',
+    data = {'hoho'},
+    port = 5005,
+  }) then
+
+    task:insert_result('UDP_SENDTO', 1.0)
+  end
+end
+
+rspamd_config:register_symbol({
+  name = 'UDP_SENDTO',
+  score = 0.0,
+  callback = send_only_udp,
+})
+
+local function udp_failed_cb(task)
+  logger.errx(task, 'udp_failed_cb: begin')
+  local function udp_cb(success, data)
+    logger.errx(task, 'udp_failed_cb: got reply: %s', data)
+
+    if success then
+      task:insert_result('UDP_SUCCESS', 1.0, data)
+    else
+      task:insert_result('UDP_FAIL', 1.0, data)
+    end
+  end
+  rspamd_udp:sendto({
+    task = task,
+    callback = udp_cb,
+    host = '127.0.0.1',
+    data = {'hello', 'world'},
+    port = 5006,
+    retransmits = 2,
+    timeout = 0.1,
+  })
+end
+
+rspamd_config:register_symbol({
+  name = 'UDP_FAIL',
+  score = 0.0,
+  callback = udp_failed_cb,
+})
+-- ]]
old mode 100644 (file)
new mode 100755 (executable)
index 9946b50..4d904e3
@@ -1,7 +1,11 @@
+#!/usr/bin/env python
+
 import socket
 import sys
+import dummy_killer
 
 UDP_IP = "127.0.0.1"
+PID = "/tmp/dummy_udp.pid"
 
 if __name__ == "__main__":
     alen = len(sys.argv)
@@ -12,6 +16,7 @@ if __name__ == "__main__":
     sock = socket.socket(socket.AF_INET, # Internet
                          socket.SOCK_DGRAM) # UDP
     sock.bind((UDP_IP, port))
+    dummy_killer.write_pid(PID)
 
     while True:
         data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes