Debian Codegraph @

test/lua/unit/quoted_printable.lua

raw
1 context("Quoted-Printable encoding", function()
2 local rspamd_util = require "rspamd_util"
3 -- These test cases are derived from https://github.com/mathiasbynens/quoted-printable
4 local cases = {
5 {
6 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=',
7 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=3D',
8 'Exactly 73 chars of which the last one is `=`'
9 },
10 {
11 'If you believe that truth=beauty, then surely mathematics is the most beautiful branch of philosophy.',
12 'If you believe that truth=3Dbeauty, then surely mathematics is the most bea=\r\nutiful branch of philosophy.',
13 'Equals sign'
14 },
15 {
16 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.',
17 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy =\r\nnibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wi=\r\nsi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lo=\r\nbortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure d=\r\nolor in hendrerit in vulputate velit esse molestie consequat, vel illum dol=\r\nore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio digni=\r\nssim qui blandit praesent luptatum zzril delenit augue duis dolore te feuga=\r\nit nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue=\r\n nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non=\r\n habent claritatem insitam; est usus legentis in iis qui facit eorum clarit=\r\natem. Investigationes demonstraverunt lectores legere me lius quod ii legun=\r\nt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem =\r\nconsuetudium lectorum. Mirum est notare quam littera gothica, quam nunc put=\r\namus parum claram, anteposuerit litterarum formas humanitatis per seacula q=\r\nuarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur par=\r\num clari, fiant sollemnes in futurum.',
18 '76-char line limit',
19 },
20 {
21 'foo ',
22 'foo=20',
23 'Trailing space'
24 },
25 {
26 'foo\t',
27 'foo=09',
28 'Trailing tab'
29 },
30
31 {
32 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=',
33 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=\r\n=3D',
34 'Exactly 74 chars of which the last one is `=`'
35 },
36 {
37 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=',
38 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=\r\n=3D',
39 'Exactly 75 chars of which the last one is `=`'
40 },
41 {
42 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=',
43 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=\r\n=3D',
44 'Exactly 76 chars of which the last one is `=`',
45 },
46 {
47 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=',
48 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=\r\nx=3D',
49 'Exactly 77 chars of which the last one is `=`'
50 },
51 {
52 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ',
53 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=20',
54 'Exactly 73 chars of which the last one is a space'
55 },
56 {
57 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ',
58 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=20',
59 'Exactly 74 chars of which the last one is a space'
60 },
61 {
62 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ',
63 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx =\r\n',
64 'Exactly 75 chars of which the last one is a space'
65 },
66 {
67 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ',
68 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=\r\n=20',
69 'Exactly 76 chars of which the last one is a space'
70 },
71 {
72 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ',
73 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=\r\nx=20',
74 'Exactly 77 chars of which the last one is a space'
75 },
76 {
77 'fdafadsf\r\n-- • Test\r\n',
78 'fdafadsf\r\n-- =E2=80=A2 Test\r\n',
79 'Newlines',
80 },
81 }
82 for _,c in ipairs(cases) do
83 test("QP sanity test case: " .. c[3], function()
84 local res = {
85 expect = c[1],
86 actual = tostring(rspamd_util.decode_qp((rspamd_util.encode_qp(c[1], 76))))
87 }
88 assert_rspamd_eq(res)
89 end)
90 test("QP encoding test case: " .. c[3], function()
91 local res = {
92 expect = c[2],
93 actual = tostring(rspamd_util.encode_qp(c[1], 76))
94 }
95 assert_rspamd_eq(res)
96 end)
97 end
98 -- Decode issues
99 cases = {
100 {
101 'Mailscape External Mail Flow Outbound Test=',
102 'Mailscape External Mail Flow Outbound Test=',
103 'asan found'
104 },
105 {
106 'foo=\n\nbar',
107 'foo\nbar',
108 'Soft newline followed by hard newline (LF)',
109 },
110 {
111 'foo=\r\n\r\nbar',
112 'foo\r\nbar',
113 'Soft newline followed by hard newline (CRLF)',
114 },
115 {
116 '=gB',
117 '=gB',
118 'Second character is okay, the first character is garbage'
119 },
120 {
121 '=bG',
122 '=bG',
123 'First character okay, the second character is rubbish'
124 }
125 }
126
127 for _,c in ipairs(cases) do
128 test("QP decoding test case: " .. c[3], function()
129 local res = {
130 expect = c[2],
131 actual = tostring(rspamd_util.decode_qp(c[1]))
132 }
133 assert_rspamd_eq(res)
134 end)
135 end
136
137
138 if os.getenv("RSPAMD_LUA_EXPENSIVE_TESTS") then
139 -- Fuzz testing
140 local charset = {}
141 for i = 0, 255 do table.insert(charset, string.char(i)) end
142
143 local function random_string(length)
144
145 if length > 0 then
146 return random_string(length - 1) .. charset[math.random(1, #charset)]
147 else
148 return ""
149 end
150 end
151 for _,l in ipairs({10, 100, 1000, 10000}) do
152 test("QP fuzz test max length " .. tostring(l), function()
153 for _=1,100 do
154 local inp = random_string(math.random() * l + 1)
155 local res = {
156 expect = inp,
157 actual = tostring(rspamd_util.decode_qp((rspamd_util.encode_qp(inp, 0))))
158 }
159 assert_rspamd_eq(res)
160 end
161 end)
162 end
163 end
164 end)