You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rfc2047.lua 3.9KB

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