Debian Codegraph @

test/lua/unit/metadata_exporter_structured.lua

raw
1 -- Unit tests for metadata_exporter structured formatter features
2 -- Tests: UUID v7, zstd compression, detected MIME types
3
4 local rspamd_util = require "rspamd_util"
5 local rspamd_text = require "rspamd_text"
6 local rspamd_zstd = require "rspamd_zstd"
7 local ucl = require "ucl"
8
9 context("UUID v7 validation", function()
10 -- UUID v7 format: xxxxxxxx-xxxx-7xxx-xxxx-xxxxxxxxxxxx
11 -- - 48-bit millisecond timestamp prefix
12 -- - Version 7 (0111) in bits 48-51 (position 14 in string = '7')
13 -- - Variant 10 in bits 64-65 (position 19 in string = 8,9,a,b)
14 -- - 74 random bits
15
16 local function hex_to_int(hex)
17 local n = 0
18 for i = 1, #hex do
19 local c = hex:sub(i, i):lower()
20 local digit = c:byte() - (c:match("%d") and 48 or 87)
21 n = n * 16 + digit
22 end
23 return n
24 end
25
26 local function uuid_timestamp_ms(uuid)
27 -- Extract first 12 hex chars (48 bits) as millisecond timestamp
28 local hex = uuid:sub(1, 8) .. uuid:sub(10, 13)
29 return hex_to_int(hex)
30 end
31
32 test("task:get_uuid() returns valid UUID v7 format", function()
33 local rspamd_task = require "rspamd_task"
34 local msg = [[
35 From: <[email protected]>
36 To: <[email protected]>
37 Subject: UUID test
38
39 Test body.
40 ]]
41 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
42 assert_true(res, "failed to load message")
43 task:process_message()
44
45 local uuid = task:get_uuid()
46 assert_not_nil(uuid, "task:get_uuid() returned nil")
47 assert_equal(#uuid, 36, "UUID should be 36 characters")
48
49 -- Check UUID format: 8-4-4-4-12 hex digits with dashes
50 assert_match("^%x%x%x%x%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%x%x%x%x%x%x%x%x$",
51 uuid, "UUID format invalid")
52
53 task:destroy()
54 end)
55
56 test("UUID v7 timestamp is recent", function()
57 local rspamd_task = require "rspamd_task"
58 local msg = [[
59 From: <[email protected]>
60 To: <[email protected]>
61 Subject: Timestamp test
62
63 Test.
64 ]]
65 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
66 assert_true(res)
67 task:process_message()
68
69 local uuid = task:get_uuid()
70 local uuid_ms = uuid_timestamp_ms(uuid)
71 local now_ms = math.floor(rspamd_util.get_time() * 1000)
72
73 -- UUID timestamp should be within 5 seconds of now
74 local diff = math.abs(now_ms - uuid_ms)
75 assert_true(diff < 5000, "UUID timestamp differs from current time by " .. diff .. "ms")
76
77 task:destroy()
78 end)
79
80 test("UUID v7 version bits are correct", function()
81 local rspamd_task = require "rspamd_task"
82 local msg = [[
83 From: <[email protected]>
84 To: <[email protected]>
85 Subject: Version test
86
87 Test.
88 ]]
89 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
90 assert_true(res)
91 task:process_message()
92
93 local uuid = task:get_uuid()
94
95 -- UUID v7 format: xxxxxxxx-xxxx-7xxx-xxxx-xxxxxxxxxxxx
96 -- Positions: 123456789012345678901234567890123456
97 -- Version nibble is at position 15 (after 2nd dash)
98 local version_char = uuid:sub(15, 15)
99 -- Version 7 means the high nibble is 7, so char is '7'
100 assert_equal(version_char, "7", "UUID version nibble should be '7', got: " .. version_char .. " uuid=" .. uuid)
101
102 -- Variant bits: first char of fourth group (position 20) should be 8, 9, a, or b
103 -- xxxxxxxx-xxxx-xxxx-8xxx-...
104 local variant_char = uuid:sub(20, 20)
105 assert_match("^[89ab]$", variant_char, "UUID variant should be 10xx (8, 9, a, or b), got: " .. variant_char .. " uuid=" .. uuid)
106
107 task:destroy()
108 end)
109
110 test("UUIDs are unique across tasks", function()
111 local rspamd_task = require "rspamd_task"
112 local msg = [[
113 From: <[email protected]>
114 To: <[email protected]>
115 Subject: Uniqueness test
116
117 Test.
118 ]]
119 local uuids = {}
120
121 for i = 1, 10 do
122 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
123 assert_true(res)
124 task:process_message()
125 local uuid = task:get_uuid()
126 assert_not_nil(uuid)
127 assert_nil(uuids[uuid], "Duplicate UUID generated: " .. uuid)
128 uuids[uuid] = true
129 task:destroy()
130 end
131
132 -- Verify we got 10 unique UUIDs
133 local count = 0
134 for _ in pairs(uuids) do count = count + 1 end
135 assert_equal(count, 10, "Expected 10 unique UUIDs")
136 end)
137 end)
138
139 context("zstd compression in structured formatter", function()
140 test("rspamd_util.zstd_compress produces valid compressed data", function()
141 local original = "Hello, World! This is a test string for compression."
142 local compressed = rspamd_util.zstd_compress(original)
143
144 assert_not_nil(compressed, "zstd_compress returned nil")
145 assert_true(compressed:len() > 0, "Compressed data should not be empty")
146 -- zstd magic number: 0xFD2FB528 (little-endian: 28 B5 2F FD)
147 -- compressed is a rspamd_text, need to get bytes
148 local bytes = compressed:bytes()
149 assert_equal(bytes[1], 0x28, "Invalid zstd magic byte 1")
150 assert_equal(bytes[2], 0xB5, "Invalid zstd magic byte 2")
151 assert_equal(bytes[3], 0x2F, "Invalid zstd magic byte 3")
152 assert_equal(bytes[4], 0xFD, "Invalid zstd magic byte 4")
153 end)
154
155 test("zstd compression round-trip preserves data", function()
156 local cases = {
157 "simple string",
158 string.rep("x", 1000), -- repetitive data
159 "Mixed 123 Numbers! And symbols: @#$%^&*()",
160 }
161
162 local cctx = rspamd_zstd.compress_ctx()
163 local dctx = rspamd_zstd.decompress_ctx()
164
165 for i, original in ipairs(cases) do
166 local compressed = rspamd_util.zstd_compress(original)
167 assert_not_nil(compressed, "Case " .. i .. ": zstd_compress returned nil")
168
169 -- Use streaming API for decompression (matches existing test patterns)
170 local decompressed = dctx:stream(compressed, 'end')
171 assert_rspamd_eq({
172 actual = decompressed,
173 expect = rspamd_text.fromstring(original)
174 })
175 end
176 end)
177
178 test("zstd compression reduces size for repetitive data", function()
179 local original = string.rep("abcdefghij", 1000) -- 10000 bytes of repetitive data
180 local compressed = rspamd_util.zstd_compress(original)
181
182 assert_true(compressed:len() < #original,
183 "Compressed size (" .. compressed:len() .. ") should be less than original (" .. #original .. ")")
184 end)
185 end)
186
187 context("Structured formatter output validation", function()
188 local rspamd_task = require "rspamd_task"
189
190 test("structured output contains required fields", function()
191 local msg = [[
192 From: <[email protected]>
193 To: <[email protected]>
194 Subject: Test message
195 Message-ID: <[email protected]>
196
197 This is the body text.
198 ]]
199 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
200 assert_true(res)
201 task:process_message()
202
203 local uuid = task:get_uuid()
204 assert_not_nil(uuid, "UUID should not be nil")
205 assert_equal(#uuid, 36, "UUID should be 36 characters")
206
207 -- Verify we can get basic task info
208 local subject = task:get_subject()
209 assert_equal(subject, "Test message", "Subject mismatch")
210
211 local msg_id = task:get_message_id()
212 assert_not_nil(msg_id, "Message-ID should not be nil")
213
214 task:destroy()
215 end)
216
217 test("msgpack format is valid", function()
218 local test_data = {
219 uuid = "01234567-89ab-7def-8000-000000000000",
220 text = "Sample text",
221 attachments = {
222 {
223 filename = "test.txt",
224 content_type = "text/plain",
225 size = 100,
226 }
227 },
228 }
229
230 local msgpack = ucl.to_format(test_data, "msgpack")
231 assert_not_nil(msgpack, "msgpack encoding failed")
232 assert_true(msgpack:len() > 0, "msgpack output should not be empty")
233
234 -- Verify we can decode it back using ucl.parser
235 local parser = ucl.parser()
236 local ok, err = parser:parse_string(msgpack, "msgpack")
237 assert_true(ok, "msgpack parsing failed: " .. tostring(err))
238
239 local obj = parser:get_object_wrapped()
240 assert_equal(obj:at("uuid"):unwrap(), test_data.uuid, "UUID mismatch after round-trip")
241 end)
242 end)
243
244 context("Detected MIME types", function()
245 local rspamd_task = require "rspamd_task"
246
247 test("get_detected_type returns nil for plain text", function()
248 local msg = [[
249 From: <[email protected]>
250 To: <[email protected]>
251 Subject: Plain text
252 Content-Type: text/plain
253
254 Just plain text.
255 ]]
256 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
257 assert_true(res)
258 task:process_message()
259
260 local parts = task:get_parts()
261 assert_not_nil(parts)
262 assert_true(#parts > 0)
263
264 -- Text parts typically don't have detected types different from announced
265 for _, part in ipairs(parts) do
266 local detected_type, detected_subtype = part:get_detected_type()
267 -- May be nil for plain text, which is expected
268 -- The important thing is the API works
269 if detected_type then
270 assert_not_nil(detected_subtype, "detected_subtype should be present if detected_type is")
271 end
272 end
273
274 task:destroy()
275 end)
276
277 test("get_type returns announced MIME type", function()
278 local msg = [[
279 From: <[email protected]>
280 To: <[email protected]>
281 Subject: HTML message
282 Content-Type: text/html; charset=utf-8
283
284 <html><body>HTML content</body></html>
285 ]]
286 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
287 assert_true(res)
288 task:process_message()
289
290 local parts = task:get_parts()
291 for _, part in ipairs(parts) do
292 local mime_type, mime_subtype = part:get_type()
293 if mime_type then
294 assert_equal(mime_type, "text", "Expected text type")
295 assert_equal(mime_subtype, "html", "Expected html subtype")
296 end
297 end
298
299 task:destroy()
300 end)
301 end)