From: Vsevolod Stakhov Date: Thu, 23 Feb 2023 12:10:23 +0000 (+0000) Subject: [Test] Add a unit test X-Git-Tag: 3.5~38 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d8dbd4de8405be7867d6d95ef42de2350296dec0;p=rspamd.git [Test] Add a unit test --- diff --git a/test/lua/unit/lua_mime.message_to_ucl.lua b/test/lua/unit/lua_mime.message_to_ucl.lua index e4fe1781a..e88fb7271 100644 --- a/test/lua/unit/lua_mime.message_to_ucl.lua +++ b/test/lua/unit/lua_mime.message_to_ucl.lua @@ -1,6 +1,7 @@ --[=========[ ******************* message ******************* ]=========] -local msg = [[ +local cases = { + { message = [[ Received: from mail0.mindspring.com (unknown [1.1.1.1]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) @@ -25,39 +26,8 @@ Content-Type: text/html; charset="utf-8" lol -]] - -context("Task piecewise split", function() - local rspamd_task = require "rspamd_task" - local rspamd_util = require "rspamd_util" - local rspamd_test_helper = require "rspamd_test_helper" - local lua_mime = require "lua_mime" - local ucl = require "ucl" - local rspamd_parsers = require "rspamd_parsers" - - rspamd_test_helper.init_url_parser() - local cfg = rspamd_util.config_from_ucl(rspamd_test_helper.default_config(), - "INIT_URL,INIT_LIBS,INIT_SYMCACHE,INIT_VALIDATE,INIT_PRELOAD_MAPS") - - test("Simple message split", function() - local res,task = rspamd_task.load_from_string(msg, cfg) - - if not res or not task then - assert_true(false, "failed to load message") - end - - task:set_from('smtp', rspamd_parsers.parse_mail_address("Test ")[1]) - task:set_recipients('smtp', { - rspamd_parsers.parse_mail_address("Test1 ")[1], - rspamd_parsers.parse_mail_address("Test2 ")[1] - }, 'rewrite') - task:set_from_ip("198.172.22.91") - task:set_user("cool user name") - task:set_helo("hello mail") - task:process_message() - - local expected_json = [[ -{ +]], + expected = [[{ "parts": [ { "type": "multipart/alternative", @@ -205,20 +175,156 @@ context("Task piecewise split", function() "tab_separated": false } ] +}]] + }, + { + message = [[ +From: <> +Content-Type: multipart/mixed; boundary="-" + +123 +--- +321 + +-- +WBR yours +-- +a +----------------- +b +------ +c +- +d +-------- +f +----- +]], + expected = [[ +{ + "parts": [ + { + "type": "multipart/mixed", + "multipart_boundary": "-", + "size": 0, + "headers": [] + }, + { + "content": "--\nWBR yours\n--\na\n-----------------\nb\n------\nc\n-\nd\n--------\nf\n", + "size": 62, + "type": "text/plain", + "boundary": "-", + "detected_type": "text/plain", + "headers": [] + } + ], + "newlines": "lf", + "digest": "1a680eb7563f32a2fbf67cf45e90f045", + "envelope": { + "recipients_smtp": [ + { + "addr": "test1@example.com", + "raw": "", + "flags": { + "valid": true + }, + "user": "test1", + "name": "Test1", + "domain": "example.com" + }, + { + "addr": "test2@example.com", + "raw": "", + "flags": { + "valid": true + }, + "user": "test2", + "name": "Test2", + "domain": "example.com" + } + ], + "from_smtp": { + "addr": "test@example.com", + "raw": "", + "flags": { + "valid": true + }, + "user": "test", + "name": "Test", + "domain": "example.com" + }, + "helo": "hello mail", + "from_ip": "198.172.22.91" + }, + "size": 135, + "headers": [ + { + "order": 0, + "raw": "From: <>\n", + "empty_separator": false, + "value": "<>", + "separator": " ", + "decoded": "<>", + "name": "From", + "tab_separated": false + }, + { + "order": 1, + "raw": "Content-Type: multipart/mixed; boundary=\"-\"\n", + "empty_separator": false, + "value": "multipart/mixed; boundary=\"-\"", + "separator": " ", + "decoded": "multipart/mixed; boundary=\"-\"", + "name": "Content-Type", + "tab_separated": false + } + ] +}]] + } } -]] - local parser = ucl.parser() - local res = parser:parse_string(expected_json) - assert_true(res) - local expected = parser:get_object() - local ucl_object = lua_mime.message_to_ucl(task, true) - local schema = lua_mime.message_to_ucl_schema() - assert_true(schema(ucl_object)) - assert_rspamd_table_eq({ - actual = ucl_object, - expect = expected - }) - task:destroy() - end) + +context("Task piecewise split", function() + local rspamd_task = require "rspamd_task" + local rspamd_util = require "rspamd_util" + local rspamd_test_helper = require "rspamd_test_helper" + local lua_mime = require "lua_mime" + local ucl = require "ucl" + local rspamd_parsers = require "rspamd_parsers" + + rspamd_test_helper.init_url_parser() + local cfg = rspamd_util.config_from_ucl(rspamd_test_helper.default_config(), + "INIT_URL,INIT_LIBS,INIT_SYMCACHE,INIT_VALIDATE,INIT_PRELOAD_MAPS") + + for i,case in ipairs(cases) do + test("Simple message split case " .. tostring(i), function() + local res,task = rspamd_task.load_from_string(case.message, cfg) + + if not res or not task then + assert_true(false, "failed to load message") + end + + task:set_from('smtp', rspamd_parsers.parse_mail_address("Test ")[1]) + task:set_recipients('smtp', { + rspamd_parsers.parse_mail_address("Test1 ")[1], + rspamd_parsers.parse_mail_address("Test2 ")[1] + }, 'rewrite') + task:set_from_ip("198.172.22.91") + task:set_user("cool user name") + task:set_helo("hello mail") + task:process_message() + local parser = ucl.parser() + local res = parser:parse_string(case.expected) + assert_true(res) + local expected = parser:get_object() + local ucl_object = lua_mime.message_to_ucl(task, true) + local schema = lua_mime.message_to_ucl_schema() + assert_true(schema(ucl_object)) + assert_rspamd_table_eq({ + actual = ucl_object, + expect = expected + }) + task:destroy() + end) + end end) \ No newline at end of file