Debian Codegraph @

test/lua/unit/rspamd_text.lua

raw
1 context("Rspamd_text:byte() test", function()
2 local rspamd_text = require "rspamd_text"
3
4 local str = 'OMG'
5 local txt = rspamd_text.fromstring(str)
6 local fmt = 'case rspamd_text:byte(%s,%s)'
7 local cases = {
8 {'1', 'nil'},
9 {'nil', '1'},
10 }
11
12 for start = -4, 4 do
13 for stop = -4, 4 do
14 table.insert(cases, {tostring(start), tostring(stop)})
15 end
16 end
17
18 for _, case in ipairs(cases) do
19 local name = string.format(fmt, case[1], case[2])
20 test(name, function()
21 local txt_bytes = {txt:byte(tonumber(case[1]), tonumber(case[2]))}
22 local str_bytes = {str:byte(tonumber(case[1]), tonumber(case[2]))}
23 assert_rspamd_table_eq({
24 expect = str_bytes,
25 actual = txt_bytes
26 })
27 end)
28 end
29 end)
30
31 context("Rspamd_text:find() test", function()
32 local rspamd_text = require "rspamd_text"
33
34 local cases = {
35 {{'foobarfoo', 'f'}, {1, 1}},
36 {{'foobarfoo', 'foo'}, {1, 3}},
37 {{'foobarfoo', 'bar'}, {4, 6}},
38 {{'foobarfoo', 'baz'}, nil},
39 {{'foobarfoo', 'rfoo'}, {6, 9}},
40 {{'foo', 'bar'}, nil},
41 {{'x', 'xxxx'}, nil},
42 {{'', ''}, {1, 0}},
43 {{'', '_'}, nil},
44 {{'x', ''}, {1, 0}},
45 }
46
47 for _, case in ipairs(cases) do
48 local name = string.format('case rspamd_text:find(%s,%s)', case[1][1], case[1][2])
49 test(name, function()
50 local t = rspamd_text.fromstring(case[1][1])
51 local s,e = t:find(case[1][2])
52
53 if case[2] then
54 assert_rspamd_table_eq({
55 expect = case[2],
56 actual = {s, e}
57 })
58 else
59 assert_nil(s)
60 end
61 local ss,ee = string.find(case[1][1], case[1][2], 1, true)
62 assert_rspamd_table_eq({
63 expect = { ss, ee },
64 actual = { s, e }
65 })
66 end)
67 -- Compare with vanila lua
68 name = string.format('case lua string vs rspamd_text:find(%s,%s)', case[1][1], case[1][2])
69 test(name, function()
70 local t = rspamd_text.fromstring(case[1][1])
71 local s,e = t:find(case[1][2])
72 local ss,ee = string.find(case[1][1], case[1][2], 1, true)
73 assert_rspamd_table_eq({
74 expect = { ss, ee },
75 actual = { s, e }
76 })
77 end)
78 end
79 end)
80
81 context("Rspamd_text:normalize_newlines() test", function()
82 local rspamd_text = require "rspamd_text"
83
84 -- No normalization needed
85 test("already CRLF - no change", function()
86 local t = rspamd_text.fromstring("line1\r\nline2\r\nline3\r\n")
87 local orig_ptr = t:ptr()
88 t:normalize_newlines("crlf")
89 assert_equal(t:ptr(), orig_ptr, "should return same text when no change needed")
90 assert_equal(t:str(), "line1\r\nline2\r\nline3\r\n")
91 end)
92
93 test("already LF only - no change for LF mode", function()
94 local t = rspamd_text.fromstring("line1\nline2\nline3\n")
95 local orig_ptr = t:ptr()
96 t:normalize_newlines("lf")
97 assert_equal(t:ptr(), orig_ptr, "should return same text when no change needed")
98 assert_equal(t:str(), "line1\nline2\nline3\n")
99 end)
100
101 test("no newlines at all - no change", function()
102 local t = rspamd_text.fromstring("just some text without newlines")
103 local orig_ptr = t:ptr()
104 t:normalize_newlines("crlf")
105 assert_equal(t:ptr(), orig_ptr, "should return same text when no newlines")
106 assert_equal(t:str(), "just some text without newlines")
107 end)
108
109 -- LF to CRLF conversion
110 test("LF to CRLF: simple", function()
111 local t = rspamd_text.fromstring("line1\nline2\n")
112 t:normalize_newlines("crlf")
113 assert_equal(t:str(), "line1\r\nline2\r\n")
114 end)
115
116 test("LF to CRLF: bare LF after CRLF stays CRLF", function()
117 local t = rspamd_text.fromstring("line1\r\nline2\n")
118 t:normalize_newlines("crlf")
119 assert_equal(t:str(), "line1\r\nline2\r\n")
120 end)
121
122 test("LF to CRLF: multiple bare LFs", function()
123 local t = rspamd_text.fromstring("a\nb\nc\n")
124 t:normalize_newlines("crlf")
125 assert_equal(t:str(), "a\r\nb\r\nc\r\n")
126 end)
127
128 test("LF to CRLF: text starting with LF", function()
129 local t = rspamd_text.fromstring("\nfirst line\n")
130 t:normalize_newlines("crlf")
131 assert_equal(t:str(), "\r\nfirst line\r\n")
132 end)
133
134 test("LF to CRLF: text ending with LF", function()
135 local t = rspamd_text.fromstring("last line\n")
136 t:normalize_newlines("crlf")
137 assert_equal(t:str(), "last line\r\n")
138 end)
139
140 test("LF to CRLF: consecutive LFs", function()
141 local t = rspamd_text.fromstring("line1\n\nline3")
142 t:normalize_newlines("crlf")
143 assert_equal(t:str(), "line1\r\n\r\nline3")
144 end)
145
146 -- CRLF to LF conversion
147 test("CRLF to LF: simple", function()
148 local t = rspamd_text.fromstring("line1\r\nline2\r\n")
149 t:normalize_newlines("lf")
150 assert_equal(t:str(), "line1\nline2\n")
151 end)
152
153 test("CRLF to LF: mixed CRLF and LF", function()
154 local t = rspamd_text.fromstring("line1\r\nline2\nline3\r\n")
155 t:normalize_newlines("lf")
156 assert_equal(t:str(), "line1\nline2\nline3\n")
157 end)
158
159 test("CRLF to LF: multiple consecutive CRLF", function()
160 local t = rspamd_text.fromstring("a\r\n\r\nb")
161 t:normalize_newlines("lf")
162 assert_equal(t:str(), "a\n\nb")
163 end)
164
165 -- Weird line endings
166 test("CR only (not followed by LF) preserved in CRLF mode", function()
167 local t = rspamd_text.fromstring("line1\rline2\n")
168 t:normalize_newlines("crlf")
169 assert_equal(t:str(), "line1\rline2\r\n")
170 end)
171
172 test("CR only preserved in LF mode", function()
173 local t = rspamd_text.fromstring("line1\rline2\r\n")
174 t:normalize_newlines("lf")
175 assert_equal(t:str(), "line1\rline2\n")
176 end)
177
178 test("multiple CRs before LF", function()
179 local t = rspamd_text.fromstring("line\r\r\nline2")
180 t:normalize_newlines("lf")
181 assert_equal(t:str(), "line\r\nline2")
182 end)
183
184 -- Inconsistent line endings
185 test("mixed CRLF and LF to CRLF", function()
186 local t = rspamd_text.fromstring("line1\r\nline2\nline3\r\nline4\n")
187 t:normalize_newlines("crlf")
188 assert_equal(t:str(), "line1\r\nline2\r\nline3\r\nline4\r\n")
189 end)
190
191 test("mixed CRLF and LF to LF", function()
192 local t = rspamd_text.fromstring("line1\r\nline2\nline3\r\nline4\n")
193 t:normalize_newlines("lf")
194 assert_equal(t:str(), "line1\nline2\nline3\nline4\n")
195 end)
196
197 -- Only line endings
198 test("single LF to CRLF", function()
199 local t = rspamd_text.fromstring("\n")
200 t:normalize_newlines("crlf")
201 assert_equal(t:str(), "\r\n")
202 end)
203
204 test("single CRLF to LF", function()
205 local t = rspamd_text.fromstring("\r\n")
206 t:normalize_newlines("lf")
207 assert_equal(t:str(), "\n")
208 end)
209
210 test("multiple LF only to CRLF", function()
211 local t = rspamd_text.fromstring("\n\n\n")
212 t:normalize_newlines("crlf")
213 assert_equal(t:str(), "\r\n\r\n\r\n")
214 end)
215
216 test("multiple CRLF only to LF", function()
217 local t = rspamd_text.fromstring("\r\n\r\n\r\n")
218 t:normalize_newlines("lf")
219 assert_equal(t:str(), "\n\n\n")
220 end)
221
222 -- Edge cases
223 test("empty string - no change", function()
224 local t = rspamd_text.fromstring("")
225 local orig_ptr = t:ptr()
226 t:normalize_newlines("crlf")
227 assert_equal(t:ptr(), orig_ptr)
228 assert_equal(t:str(), "")
229 end)
230
231 test("single character without newline", function()
232 local t = rspamd_text.fromstring("x")
233 local orig_ptr = t:ptr()
234 t:normalize_newlines("crlf")
235 assert_equal(t:ptr(), orig_ptr)
236 assert_equal(t:str(), "x")
237 end)
238
239 test("single character with newline", function()
240 local t = rspamd_text.fromstring("x\n")
241 t:normalize_newlines("crlf")
242 assert_equal(t:str(), "x\r\n")
243 end)
244
245 test("large text with many newlines", function()
246 local lines = {}
247 for i = 1, 1000 do
248 lines[i] = "line" .. i
249 end
250 local input = table.concat(lines, "\n")
251 local expected = table.concat(lines, "\r\n")
252 local t = rspamd_text.fromstring(input)
253 t:normalize_newlines("crlf")
254 assert_equal(t:str(), expected)
255 end)
256
257 test("text with null bytes", function()
258 local t = rspamd_text.fromstring("line1\n\x00line2\n")
259 t:normalize_newlines("crlf")
260 assert_equal(t:str(), "line1\r\n\x00line2\r\n")
261 end)
262
263 -- Mode parameter variations
264 test("mode 'crlf' works", function()
265 local t = rspamd_text.fromstring("a\n")
266 t:normalize_newlines("crlf")
267 assert_equal(t:str(), "a\r\n")
268 end)
269
270 test("mode 'windows' works (alias for crlf)", function()
271 local t = rspamd_text.fromstring("a\n")
272 t:normalize_newlines("windows")
273 assert_equal(t:str(), "a\r\n")
274 end)
275
276 test("mode 'lf' works", function()
277 local t = rspamd_text.fromstring("a\r\n")
278 t:normalize_newlines("lf")
279 assert_equal(t:str(), "a\n")
280 end)
281
282 test("mode 'unix' works (alias for lf)", function()
283 local t = rspamd_text.fromstring("a\r\n")
284 t:normalize_newlines("unix")
285 assert_equal(t:str(), "a\n")
286 end)
287
288 test("default mode is crlf", function()
289 local t = rspamd_text.fromstring("a\n")
290 t:normalize_newlines()
291 assert_equal(t:str(), "a\r\n")
292 end)
293
294 test("case insensitive mode", function()
295 local t = rspamd_text.fromstring("a\n")
296 t:normalize_newlines("CRLF")
297 assert_equal(t:str(), "a\r\n")
298 end)
299 end)