Debian Codegraph @

test/lua/unit/rfc2047.lua

raw
1 --[[
2 Copyright (c) 2022, Vsevolod Stakhov <[email protected]>
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10
11 2. Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 ]]--
26
27 context("RFC2047 decoding", function()
28 local ok, ffi = pcall(require, "ffi")
29 if not ok then
30 ffi = require("cffi")
31 end
32
33 ffi.cdef[[
34 const char * rspamd_mime_header_decode (void *pool, const char *in, size_t inlen);
35 void * rspamd_mempool_new_ (size_t sz, const char *name, int flags, const char *strloc);
36 void rspamd_mempool_delete (void *pool);
37 ]]
38
39 test("Decode rfc2047 tokens", function()
40 -- Test -> expected
41 local cases = {
42 {"=?US-ASCII*EN?Q?Keith_Moore?= <[email protected]>", "Keith Moore <[email protected]>"},
43 {[[=?windows-1251?Q?=C2=FB_=F1=EC=EE=E6=E5=F2=E5_=F5=E0=F0?=
44 =?windows-1251?Q?=E0=EA=F2=E5=F0=E8=E7=EE=E2=E0=F2=FC=F1?=
45 =?windows-1251?Q?=FF_=E7=EE=F0=EA=E8=EC_=E7=F0=E5=ED=E8?=
46 =?windows-1251?Q?=E5=EC?=]], "Вы сможете характеризоваться зорким зрением"},
47 {'v=1; a=rsa-sha256; c=relaxed/relaxed; d=yoni.za.org; s=testdkim1;',
48 'v=1; a=rsa-sha256; c=relaxed/relaxed; d=yoni.za.org; s=testdkim1;'},
49 {"=?windows-1251?B?xO7q8+zl7fIuc2NyLnV1ZQ==?=", "Документ.scr.uue"},
50 {"=?UTF-8?Q?=20wie=20ist=20es=20Ihnen=20ergangen?.pdf?=", " wie ist es Ihnen ergangen?.pdf"}, -- ? inside
51 {"=?UTF-8?Q?=20wie=20ist=20es=20Ihnen=20ergangen??=", " wie ist es Ihnen ergangen?"}, -- ending ? inside
52 }
53
54 local pool = ffi.C.rspamd_mempool_new_(4096, "lua", 0, "rfc2047.lua:49")
55
56 for _,c in ipairs(cases) do
57 local res = ffi.C.rspamd_mime_header_decode(pool, c[1], #c[1])
58 res = ffi.string(res)
59 assert_not_nil(res, "cannot decode " .. c[1])
60 assert_rspamd_eq({actual = res, expect = c[2]})
61
62 end
63
64 ffi.C.rspamd_mempool_delete(pool)
65 end)
66 if os.getenv("RSPAMD_LUA_EXPENSIVE_TESTS") then
67 test("Fuzz test for rfc2047 tokens", function()
68 local util = require("rspamd_util")
69 local pool = ffi.C.rspamd_mempool_new_(4096, "lua", 0, "rfc2047.lua:63")
70 local str = "Тест Тест Тест Тест Тест"
71
72 for _ = 0,1000 do
73 local r1 = math.random()
74 local r2 = math.random()
75 -- Use math.floor for Lua 5.4 compatibility (string.sub requires integers)
76 local sl1 = math.floor(#str / 2.0 * r1)
77 local sl2 = math.floor(#str / 2.0 * r2)
78
79 local s1 = tostring(util.encode_base64(string.sub(str, 1, sl1)))
80 local s2 = tostring(util.encode_base64(string.sub(str, sl1 + 1, sl2)))
81 local s3 = tostring(util.encode_base64(string.sub(str, sl2 + 1)))
82
83 if #s1 > 0 and #s2 > 0 and #s3 > 0 then
84 local s = string.format('=?UTF-8?B?%s?= =?UTF-8?B?%s?= =?UTF-8?B?%s?=',
85 s1, s2, s3)
86 local res = ffi.C.rspamd_mime_header_decode(pool, s, #s)
87 res = ffi.string(res)
88 assert_not_nil(res, "cannot decode " .. s)
89 assert_rspamd_eq({actual = res, expect = str})
90 end
91 end
92
93 ffi.C.rspamd_mempool_delete(pool)
94 end)
95 end
96 end)