Debian Codegraph @

test/lua/unit/lua_mime.message_to_ucl.lua

raw
1
2 --[=========[ ******************* message ******************* ]=========]
3 local cases = {
4 { message = [[
5 Received: from mail0.mindspring.com (unknown [1.1.1.1])
6 (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits))
7 (No client certificate requested)
8 by mail.example.com (Postfix) with ESMTPS id 88A0C6B332
9 for <[email protected]>; Wed, 24 Nov 2021 19:05:43 +0000 (GMT)
10 From: <>
11 To: <[email protected]>
12 Subject: test
13 Content-Type: multipart/alternative;
14 boundary="_000_6be055295eab48a5af7ad4022f33e2d0_"
15
16 --_000_6be055295eab48a5af7ad4022f33e2d0_
17 Content-Type: text/plain; charset="utf-8"
18 Content-Transfer-Encoding: 7bit
19
20 Hello world
21
22
23 --_000_6be055295eab48a5af7ad4022f33e2d0_
24 Content-Type: text/html; charset="utf-8"
25
26 <html><body>
27 lol
28 </html>
29 ]],
30 expected = [[{
31 "parts": [
32 {
33 "type": "multipart/alternative",
34 "multipart_boundary": "_000_6be055295eab48a5af7ad4022f33e2d0_",
35 "size": 0,
36 "headers": []
37 },
38 {
39 "content": "Hello world\n\n\n",
40 "size": 14,
41 "type": "text/plain",
42 "boundary": "_000_6be055295eab48a5af7ad4022f33e2d0_",
43 "detected_type": "text/plain",
44 "headers": [
45 {
46 "order": 0,
47 "raw": "Content-Type: text/plain; charset=\"utf-8\"\n",
48 "empty_separator": false,
49 "value": "text/plain; charset=\"utf-8\"",
50 "separator": " ",
51 "decoded": "text/plain; charset=\"utf-8\"",
52 "name": "Content-Type",
53 "tab_separated": false
54 },
55 {
56 "order": 1,
57 "raw": "Content-Transfer-Encoding: 7bit\n",
58 "empty_separator": false,
59 "value": "7bit",
60 "separator": " ",
61 "decoded": "7bit",
62 "name": "Content-Transfer-Encoding",
63 "tab_separated": false
64 }
65 ]
66 },
67 {
68 "content": "<html><body>\nlol\n</html>\n",
69 "size": 25,
70 "type": "text/html",
71 "boundary": "_000_6be055295eab48a5af7ad4022f33e2d0_",
72 "detected_type": "text/html",
73 "headers": [
74 {
75 "order": 0,
76 "raw": "Content-Type: text/html; charset=\"utf-8\"\n",
77 "empty_separator": false,
78 "value": "text/html; charset=\"utf-8\"",
79 "separator": " ",
80 "decoded": "text/html; charset=\"utf-8\"",
81 "name": "Content-Type",
82 "tab_separated": false
83 }
84 ]
85 }
86 ],
87 "newlines": "lf",
88 "digest": "043cf1a314d0a1af95951d6aec932faf",
89 "envelope": {
90 "recipients_smtp": [
91 {
92 "addr": "[email protected]",
93 "raw": "<[email protected]>",
94 "flags": {
95 "valid": true
96 },
97 "user": "test1",
98 "name": "Test1",
99 "domain": "example.com"
100 },
101 {
102 "addr": "[email protected]",
103 "raw": "<[email protected]>",
104 "flags": {
105 "valid": true
106 },
107 "user": "test2",
108 "name": "Test2",
109 "domain": "example.com"
110 }
111 ],
112 "from_smtp": {
113 "addr": "[email protected]",
114 "raw": "<[email protected]>",
115 "flags": {
116 "valid": true
117 },
118 "user": "test",
119 "name": "Test",
120 "domain": "example.com"
121 },
122 "helo": "hello mail",
123 "from_ip": "198.172.22.91"
124 },
125 "size": 666,
126 "headers": [
127 {
128 "order": 0,
129 "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 <[email protected]>; Wed, 24 Nov 2021 19:05:43 +0000 (GMT)\n",
130 "empty_separator": false,
131 "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 <[email protected]>; Wed, 24 Nov 2021 19:05:43 +0000 (GMT)",
132 "separator": " ",
133 "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 <[email protected]>; Wed, 24 Nov 2021 19:05:43 +0000 (GMT)",
134 "name": "Received",
135 "tab_separated": false
136 },
137 {
138 "order": 1,
139 "raw": "From: <>\n",
140 "empty_separator": false,
141 "value": "<>",
142 "separator": " ",
143 "decoded": "<>",
144 "name": "From",
145 "tab_separated": false
146 },
147 {
148 "order": 2,
149 "raw": "To: <[email protected]>\n",
150 "empty_separator": false,
151 "value": "<[email protected]>",
152 "separator": " ",
153 "decoded": "<[email protected]>",
154 "name": "To",
155 "tab_separated": false
156 },
157 {
158 "order": 3,
159 "raw": "Subject: test\n",
160 "empty_separator": false,
161 "value": "test",
162 "separator": " ",
163 "decoded": "test",
164 "name": "Subject",
165 "tab_separated": false
166 },
167 {
168 "order": 4,
169 "raw": "Content-Type: multipart/alternative;\n boundary=\"_000_6be055295eab48a5af7ad4022f33e2d0_\"\n",
170 "empty_separator": false,
171 "value": "multipart/alternative; boundary=\"_000_6be055295eab48a5af7ad4022f33e2d0_\"",
172 "separator": " ",
173 "decoded": "multipart/alternative; boundary=\"_000_6be055295eab48a5af7ad4022f33e2d0_\"",
174 "name": "Content-Type",
175 "tab_separated": false
176 }
177 ]
178 }]]
179 },
180 {
181 message = [[
182 From: <>
183 Content-Type: multipart/mixed; boundary="-"
184
185 123
186 ---
187 321
188
189 --
190 WBR yours
191 --
192 a
193 -----------------
194 b
195 ------
196 c
197 -
198 d
199 --------
200 f
201 -----
202 ]],
203 expected = [[
204 {
205 "parts": [
206 {
207 "type": "multipart/mixed",
208 "multipart_boundary": "-",
209 "size": 0,
210 "headers": []
211 },
212 {
213 "content": "--\nWBR yours\n--\na\n-----------------\nb\n------\nc\n-\nd\n--------\nf\n",
214 "size": 62,
215 "type": "text/plain",
216 "boundary": "-",
217 "detected_type": "text/plain",
218 "headers": []
219 }
220 ],
221 "newlines": "lf",
222 "digest": "1a680eb7563f32a2fbf67cf45e90f045",
223 "envelope": {
224 "recipients_smtp": [
225 {
226 "addr": "[email protected]",
227 "raw": "<[email protected]>",
228 "flags": {
229 "valid": true
230 },
231 "user": "test1",
232 "name": "Test1",
233 "domain": "example.com"
234 },
235 {
236 "addr": "[email protected]",
237 "raw": "<[email protected]>",
238 "flags": {
239 "valid": true
240 },
241 "user": "test2",
242 "name": "Test2",
243 "domain": "example.com"
244 }
245 ],
246 "from_smtp": {
247 "addr": "[email protected]",
248 "raw": "<[email protected]>",
249 "flags": {
250 "valid": true
251 },
252 "user": "test",
253 "name": "Test",
254 "domain": "example.com"
255 },
256 "helo": "hello mail",
257 "from_ip": "198.172.22.91"
258 },
259 "size": 135,
260 "headers": [
261 {
262 "order": 0,
263 "raw": "From: <>\n",
264 "empty_separator": false,
265 "value": "<>",
266 "separator": " ",
267 "decoded": "<>",
268 "name": "From",
269 "tab_separated": false
270 },
271 {
272 "order": 1,
273 "raw": "Content-Type: multipart/mixed; boundary=\"-\"\n",
274 "empty_separator": false,
275 "value": "multipart/mixed; boundary=\"-\"",
276 "separator": " ",
277 "decoded": "multipart/mixed; boundary=\"-\"",
278 "name": "Content-Type",
279 "tab_separated": false
280 }
281 ]
282 }]]
283 }
284 }
285
286 context("Task piecewise split", function()
287 local rspamd_task = require "rspamd_task"
288 local rspamd_util = require "rspamd_util"
289 local rspamd_test_helper = require "rspamd_test_helper"
290 local lua_mime = require "lua_mime"
291 local ucl = require "ucl"
292 local rspamd_parsers = require "rspamd_parsers"
293
294 rspamd_test_helper.init_url_parser()
295 local cfg = rspamd_util.config_from_ucl(rspamd_test_helper.default_config(),
296 "INIT_URL,INIT_LIBS,INIT_SYMCACHE,INIT_VALIDATE,INIT_PRELOAD_MAPS")
297
298 for i,case in ipairs(cases) do
299 test("Simple message split case " .. tostring(i), function()
300 local res,task = rspamd_task.load_from_string(case.message, cfg)
301
302 if not res or not task then
303 assert_true(false, "failed to load message")
304 end
305
306 task:set_from('smtp', rspamd_parsers.parse_mail_address("Test <[email protected]>")[1])
307 task:set_recipients('smtp', {
308 rspamd_parsers.parse_mail_address("Test1 <[email protected]>")[1],
309 rspamd_parsers.parse_mail_address("Test2 <[email protected]>")[1]
310 }, 'rewrite')
311 task:set_from_ip("198.172.22.91")
312 task:set_user("cool user name")
313 task:set_helo("hello mail")
314 task:process_message()
315 local parser = ucl.parser()
316 local res = parser:parse_string(case.expected)
317 assert_true(res)
318 local expected = parser:get_object()
319 local ucl_object = lua_mime.message_to_ucl(task, true)
320 local schema = lua_mime.message_to_ucl_schema()
321 local ok, err = schema:check(ucl_object)
322 assert_true(ok, err)
323 assert_rspamd_table_eq({
324 actual = ucl_object,
325 expect = expected
326 })
327 task:destroy()
328 end)
329 end
330
331 end)