Skip to content

Commit

Permalink
[Test] Add functional tests for ssl in lua_tcp
Browse files Browse the repository at this point in the history
  • Loading branch information
vstakhov committed May 31, 2019
1 parent 1e3d20e commit cc7f49b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/lua/lua_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -4872,7 +4872,8 @@ lua_task_get_settings_id (lua_State *L)
guint32 *hp;

if (task != NULL) {
hp = rspamd_mempool_get_variable (task->task_pool, "settings_hash");
hp = rspamd_mempool_get_variable (task->task_pool,
RSPAMD_MEMPOOL_SETTINGS_HASH);

if (hp) {
lua_pushnumber (L, *hp);
Expand Down
21 changes: 16 additions & 5 deletions test/functional/cases/230_tcp.robot
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
*** Settings ***
Test Setup Http Setup
Test Teardown Http Teardown
Test Setup Servers Setup
Test Teardown Servers Teardown
Library Process
Library ${TESTDIR}/lib/rspamd.py
Resource ${TESTDIR}/lib/rspamd.robot
Expand All @@ -19,6 +19,10 @@ Simple TCP request
Check Rspamc ${result} HTTP_ASYNC_RESPONSE
Check Rspamc ${result} HTTP_ASYNC_RESPONSE_2

SSL TCP request
${result} = Scan Message With Rspamc ${MESSAGE}
Check Rspamc ${result} TCP_SSL_RESPONSE (0.00)[test]
Check Rspamc ${result} TCP_SSL_RESPONSE_2 (0.00)[test2]

Sync API TCP request
${result} = Scan Message With Rspamc ${MESSAGE}
Expand All @@ -41,20 +45,27 @@ Lua Setup
Set Global Variable ${LUA_SCRIPT}
Generic Setup

Http Setup
Servers Setup
Run Dummy Http
Run Dummy Ssl
Lua Setup ${TESTDIR}/lua/tcp.lua

Http Teardown
Servers Teardown
${http_pid} = Get File /tmp/dummy_http.pid
Shutdown Process With Children ${http_pid}
${ssl_pid} = Get File /tmp/dummy_ssl.pid
Shutdown Process With Children ${ssl_pid}
Normal Teardown

Run Dummy Http
[Arguments]
${result} = Start Process ${TESTDIR}/util/dummy_http.py
Wait Until Created /tmp/dummy_http.pid
Wait Until Created /tmp/dummy_http.pid timeout=2 second

Run Dummy Ssl
[Arguments]
${result} = Start Process ${TESTDIR}/util/dummy_ssl.py ${TESTDIR}/util/server.pem
Wait Until Created /tmp/dummy_ssl.pid timeout=2 second

Check url
[Arguments] ${url} ${method} @{expect_results}
Expand Down
14 changes: 6 additions & 8 deletions test/functional/lib/rspamd.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import demjson
import grp
import os
import os.path
Expand All @@ -25,7 +24,7 @@
import httplib

def Check_JSON(j):
d = demjson.decode(j, strict=True)
d = json.loads(j, strict=True)
assert len(d) > 0
assert 'error' not in d
return d
Expand All @@ -38,7 +37,7 @@ def save_run_results(directory, filenames):
suite_name = BuiltIn().get_variable_value("${SUITE_NAME}")
test_name = BuiltIn().get_variable_value("${TEST NAME}")
if test_name is None:
# this is suite-level tear down
# this is suite-level tear down
destination_directory = "%s/robot-save/%s" % (current_directory, suite_name)
else:
destination_directory = "%s/robot-save/%s/%s" % (current_directory, suite_name, test_name)
Expand Down Expand Up @@ -185,19 +184,18 @@ def shutdown_process(process):
process.wait(TERM_TIMEOUT)
return
except psutil.TimeoutExpired:
logger.info( "PID {} is not termianated in {} seconds, sending SIGKILL..." %
(process.pid, TERM_TIMEOUT))
logger.info( "PID {} is not terminated in {} seconds, sending SIGKILL...".format(process.pid, TERM_TIMEOUT))
try:
# send SIGKILL
process.kill()
except psutil.NoSuchProcess:
# process exited just befor we tried to kill
# process exited just before we tried to kill
return

try:
process.wait(KILL_WAIT)
except psutil.TimeoutExpired:
raise RuntimeError("Failed to shutdown process %d (%s)" % (process.pid, process.name()))
raise RuntimeError("Failed to shutdown process {} ({})".format(process.pid, process.name()))


def shutdown_process_with_children(pid):
Expand Down Expand Up @@ -229,7 +227,7 @@ def get_file_if_exists(file_path):
return myfile.read()
return None

# copy-paste from
# copy-paste from
# https://hg.python.org/cpython/file/6860263c05b3/Lib/shutil.py#l1068
# As soon as we move to Python 3, this should be removed in favor of shutil.which()
def python3_which(cmd, mode=os.F_OK | os.X_OK, path=None):
Expand Down
33 changes: 33 additions & 0 deletions test/functional/lua/tcp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,33 @@ local function http_simple_tcp_async_symbol(task)
})
end

local function http_simple_tcp_ssl_symbol(task)
logger.errx(task, 'ssl_tcp_symbol: begin')
local function ssl_get_cb(err, data, conn)
logger.errx(task, 'ssl_get_cb: got reply: %s, error: %s, conn: %s', data, err, conn)
task:insert_result('TCP_SSL_RESPONSE_2', 1.0, tostring(data):gsub('%s', ''))
end
local function ssl_read_post_cb(err, conn)
logger.errx(task, 'ssl_read_post_cb: write done: error: %s, conn: %s', err, conn)
conn:add_read(ssl_get_cb)
end
local function ssl_read_cb(err, data, conn)
logger.errx(task, 'ssl_read_cb: got reply: %s, error: %s, conn: %s', data, err, conn)
conn:add_write(ssl_read_post_cb, "test2\n")
task:insert_result('TCP_SSL_RESPONSE', 1.0, tostring(data):gsub('%s', ''))
end
rspamd_tcp:request({
task = task,
callback = ssl_read_cb,
host = '127.0.0.1',
data = {'test\n'},
read = true,
ssl = true,
ssl_noverify = true,
port = 14433,
})
end

local function http_simple_tcp_symbol(task)
logger.errx(task, 'connect_sync, before')

Expand Down Expand Up @@ -178,6 +205,12 @@ rspamd_config:register_symbol({
callback = http_simple_tcp_async_symbol,
no_squeeze = true
})
rspamd_config:register_symbol({
name = 'SIMPLE_TCP_ASYNC_SSL_TEST',
score = 1.0,
callback = http_simple_tcp_ssl_symbol,
no_squeeze = true
})
rspamd_config:register_symbol({
name = 'SIMPLE_TCP_TEST',
score = 1.0,
Expand Down

0 comments on commit cc7f49b

Please sign in to comment.