commit d71ef8c: [Test] Add unit test for message to ucl function

Vsevolod Stakhov vsevolod at highsecure.ru
Wed Nov 24 20:49:06 UTC 2021


Author: Vsevolod Stakhov
Date: 2021-11-24 20:45:27 +0000
URL: https://github.com/rspamd/rspamd/commit/d71ef8ca42ff672bac112a4b89ea0259586d196c (HEAD -> master)

[Test] Add unit test for message to ucl function

---
 lualib/lua_mime.lua                       |   9 +-
 test/lua/unit/lua_mime.message_to_ucl.lua | 188 ++++++++++++++++++++++++++++++
 2 files changed, 194 insertions(+), 3 deletions(-)

diff --git a/lualib/lua_mime.lua b/lualib/lua_mime.lua
index 8100a4f3e..fe82fb717 100644
--- a/lualib/lua_mime.lua
+++ b/lualib/lua_mime.lua
@@ -573,11 +573,14 @@ exports.modify_headers = function(task, hdr_alterations)
 end
 
 --[[[
--- @function lua_mime.message_to_ucl(task)
+-- @function lua_mime.message_to_ucl(task, [stringify_content])
 -- Exports a message to an ucl object
 --]]
-exports.message_to_ucl = function(task)
+exports.message_to_ucl = function(task, stringify_content)
   local E = {}
+
+  local maybe_stringify_f = stringify_content and
+    tostring or function(t) return t  end
   local result = {
     size = task:get_size(),
     digest = task:get_digest(),
@@ -613,7 +616,7 @@ exports.message_to_ucl = function(task)
         type = string.format('%s/%s', part:get_type()),
         detected_type = string.format('%s/%s', part:get_detected_type()),
         filename = part:get_filename(),
-        content = part:get_content(),
+        content = maybe_stringify_f(part:get_content()),
         headers =  part:get_headers(true) or E,
         boundary = part:get_enclosing_boundary()
       }
diff --git a/test/lua/unit/lua_mime.message_to_ucl.lua b/test/lua/unit/lua_mime.message_to_ucl.lua
new file mode 100644
index 000000000..c97f38724
--- /dev/null
+++ b/test/lua/unit/lua_mime.message_to_ucl.lua
@@ -0,0 +1,188 @@
+
+--[=========[ *******************  message  ******************* ]=========]
+local msg = [[
+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)
+	by mail.example.com (Postfix) with ESMTPS id 88A0C6B332
+	for <example at example.com>; Wed, 24 Nov 2021 19:05:43 +0000 (GMT)
+From: <>
+To: <nobody at example.com>
+Subject: test
+Content-Type: multipart/alternative;
+    boundary="_000_6be055295eab48a5af7ad4022f33e2d0_"
+
+--_000_6be055295eab48a5af7ad4022f33e2d0_
+Content-Type: text/plain; charset="utf-8"
+Content-Transfer-Encoding: 7bit
+
+Hello world
+
+
+--_000_6be055295eab48a5af7ad4022f33e2d0_
+Content-Type: text/html; charset="utf-8"
+
+<html><body>
+lol
+</html>
+]]
+
+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"
+
+  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:process_message()
+
+    local expected_json = [[
+{
+    "parts": [
+        {
+            "content": "Hello world\n\n\n",
+            "size": 14,
+            "type": "text/plain",
+            "boundary": "_000_6be055295eab48a5af7ad4022f33e2d0_",
+            "detected_type": "text/plain",
+            "headers": [
+                [
+                    {
+                        "order": 0,
+                        "raw": "Content-Type: text/plain; charset=\"utf-8\"\n",
+                        "empty_separator": false,
+                        "value": "text/plain; charset=\"utf-8\"",
+                        "separator": " ",
+                        "decoded": "text/plain; charset=\"utf-8\"",
+                        "name": "Content-Type",
+                        "tab_separated": false
+                    }
+                ],
+                [
+                    {
+                        "order": 1,
+                        "raw": "Content-Transfer-Encoding: 7bit\n",
+                        "empty_separator": false,
+                        "value": "7bit",
+                        "separator": " ",
+                        "decoded": "7bit",
+                        "name": "Content-Transfer-Encoding",
+                        "tab_separated": false
+                    }
+                ]
+            ]
+        },
+        {
+            "content": "<html><body>\nlol\n</html>\n",
+            "size": 25,
+            "type": "text/html",
+            "boundary": "_000_6be055295eab48a5af7ad4022f33e2d0_",
+            "detected_type": "text/html",
+            "headers": [
+                [
+                    {
+                        "order": 0,
+                        "raw": "Content-Type: text/html; charset=\"utf-8\"\n",
+                        "empty_separator": false,
+                        "value": "text/html; charset=\"utf-8\"",
+                        "separator": " ",
+                        "decoded": "text/html; charset=\"utf-8\"",
+                        "name": "Content-Type",
+                        "tab_separated": false
+                    }
+                ]
+            ]
+        }
+    ],
+    "newlines": "lf",
+    "digest": "043cf1a314d0a1af95951d6aec932faf",
+    "envelope": [],
+    "size": 666,
+    "headers": [
+        [
+            {
+                "order": 0,
+                "raw": "Received: from mail0.mindspring.com (unknown [1.1.1.1])\n\t(using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby mail.example.com (Postfix) with ESMTPS id 88A0C6B332\n\tfor <example at example.com>; Wed, 24 Nov 2021 19:05:43 +0000 (GMT)\n",
+                "empty_separator": false,
+                "value": "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) by mail.example.com (Postfix) with ESMTPS id 88A0C6B332 for <example at example.com>; Wed, 24 Nov 2021 19:05:43 +0000 (GMT)",
+                "separator": " ",
+                "decoded": "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) by mail.example.com (Postfix) with ESMTPS id 88A0C6B332 for <example at example.com>; Wed, 24 Nov 2021 19:05:43 +0000 (GMT)",
+                "name": "Received",
+                "tab_separated": false
+            }
+        ],
+        [
+            {
+                "order": 1,
+                "raw": "From: <>\n",
+                "empty_separator": false,
+                "value": "<>",
+                "separator": " ",
+                "decoded": "<>",
+                "name": "From",
+                "tab_separated": false
+            }
+        ],
+        [
+            {
+                "order": 2,
+                "raw": "To: <nobody at example.com>\n",
+                "empty_separator": false,
+                "value": "<nobody at example.com>",
+                "separator": " ",
+                "decoded": "<nobody at example.com>",
+                "name": "To",
+                "tab_separated": false
+            }
+        ],
+        [
+            {
+                "order": 3,
+                "raw": "Subject: test\n",
+                "empty_separator": false,
+                "value": "test",
+                "separator": " ",
+                "decoded": "test",
+                "name": "Subject",
+                "tab_separated": false
+            }
+        ],
+        [
+            {
+                "order": 4,
+                "raw": "Content-Type: multipart/alternative;\n    boundary=\"_000_6be055295eab48a5af7ad4022f33e2d0_\"\n",
+                "empty_separator": false,
+                "value": "multipart/alternative; boundary=\"_000_6be055295eab48a5af7ad4022f33e2d0_\"",
+                "separator": " ",
+                "decoded": "multipart/alternative; boundary=\"_000_6be055295eab48a5af7ad4022f33e2d0_\"",
+                "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)
+    assert_rspamd_table_eq({
+      actual = ucl_object,
+      expect = expected
+    })
+    task:destroy()
+  end)
+
+end)
\ No newline at end of file


More information about the Commits mailing list