Debian Codegraph @

test/lua/unit/logger.lua

raw
1 context("Logger unit tests", function()
2 test("Logger functions", function()
3 local log = require "rspamd_logger"
4
5 local cases = {
6 { 'string', 'string' },
7 { '%1', 'string', 'string' },
8 { '%1', '1.1', 1.1 },
9 { '%1', '1', 1 },
10 { '%1', 'true', true },
11 { '%1', '{[1] = 1, [2] = test}', { 1, 'test' } },
12 { '%1', '{[1] = 1, [2] = 2.1, [k2] = test}', { 1, 2.1, k2 = 'test' } },
13 { '%s', 'true', true },
14 }
15
16 for _, c in ipairs(cases) do
17 local s
18 if c[3] then
19 s = log.slog(c[1], c[3])
20 else
21 s = log.slog(c[1])
22 end
23 assert_equal(s, c[2], string.format("'%s' doesn't match with '%s'",
24 c[2], s))
25 end
26 end)
27
28 test("Logger graceful error handling", function()
29 local log = require "rspamd_logger"
30
31 -- Test missing arguments
32 local missing_arg_cases = {
33 { '%1', '<MISSING ARGUMENT>' },
34 { '%0', '<MISSING ARGUMENT>' }, -- %0 is invalid since Lua args are 1-indexed
35 { '%2', '<MISSING ARGUMENT>', 'arg1' },
36 { '%1 %2', 'arg1 <MISSING ARGUMENT>', 'arg1' },
37 { 'prefix %1 %3 suffix', 'prefix arg1 <MISSING ARGUMENT> suffix', 'arg1' },
38 }
39
40 for _, c in ipairs(missing_arg_cases) do
41 local s
42 if c[3] then
43 s = log.slog(c[1], c[3])
44 else
45 s = log.slog(c[1])
46 end
47 assert_equal(s, c[2], string.format("Missing arg test: '%s' doesn't match with '%s'",
48 c[2], s))
49 end
50
51 -- Test extra arguments
52 local extra_arg_cases = {
53 { '%1', 'arg1 <EXTRA 1 ARGUMENTS>', 'arg1', 'extra1' },
54 { '%1', 'arg1 <EXTRA 2 ARGUMENTS>', 'arg1', 'extra1', 'extra2' },
55 { '%s', 'arg1 <EXTRA 1 ARGUMENTS>', 'arg1', 'extra1' },
56 { 'prefix %1 suffix', 'prefix arg1 suffix <EXTRA 1 ARGUMENTS>', 'arg1', 'extra1' },
57 }
58
59 for _, c in ipairs(extra_arg_cases) do
60 local s
61 if c[4] and c[5] then
62 s = log.slog(c[1], c[3], c[4], c[5])
63 elseif c[4] then
64 s = log.slog(c[1], c[3], c[4])
65 else
66 s = log.slog(c[1], c[3])
67 end
68 assert_equal(s, c[2], string.format("Extra arg test: '%s' doesn't match with '%s'",
69 c[2], s))
70 end
71
72 -- Test literal percent sequences (should pass through as-is)
73 local literal_cases = {
74 { '%-1', '%-1' },
75 { '%abc', '%abc' }, -- Should pass through as literal since it's not a valid number
76 { '%', '%' }, -- Single percent should pass through
77 }
78
79 for _, c in ipairs(literal_cases) do
80 local s = log.slog(c[1])
81 assert_equal(s, c[2], string.format("Literal test: '%s' doesn't match with '%s'",
82 c[2], s))
83 end
84
85 -- Test mixed scenarios
86 local mixed_cases = {
87 { '%1 %3', 'arg1 <MISSING ARGUMENT> <EXTRA 1 ARGUMENTS>', 'arg1', 'extra1' },
88 { '%2 %4', 'extra1 <MISSING ARGUMENT> <EXTRA 1 ARGUMENTS>', 'arg1', 'extra1' },
89 }
90
91 for _, c in ipairs(mixed_cases) do
92 local s
93 if c[4] then
94 s = log.slog(c[1], c[3], c[4])
95 else
96 s = log.slog(c[1], c[3])
97 end
98 assert_equal(s, c[2], string.format("Mixed test: '%s' doesn't match with '%s'",
99 c[2], s))
100 end
101 end)
102
103 test("Logger type specifiers", function()
104 local log = require "rspamd_logger"
105
106 -- Test %d (signed integer)
107 local int_cases = {
108 { '%d', '100', 100 },
109 { '%d', '100', 100.5 }, -- Should truncate to integer
110 { '%d', '-42', -42 },
111 { '%d', '0', 0 },
112 { '%1d', '100', 100 },
113 { 'count=%d', 'count=100', 100 },
114 { 'count=%1d', 'count=100', 100 },
115 { '%d items', '100 items', 100 },
116 }
117
118 for _, c in ipairs(int_cases) do
119 local s = log.slog(c[1], c[3])
120 assert_equal(s, c[2], string.format("Int format test: '%s' doesn't match with '%s'",
121 c[2], s))
122 end
123
124 -- Test %ud (unsigned integer)
125 local uint_cases = {
126 { '%ud', '100', 100 },
127 { '%1ud', '100', 100 },
128 { 'size=%ud bytes', 'size=1024 bytes', 1024 },
129 }
130
131 for _, c in ipairs(uint_cases) do
132 local s = log.slog(c[1], c[3])
133 assert_equal(s, c[2], string.format("Unsigned int format test: '%s' doesn't match with '%s'",
134 c[2], s))
135 end
136
137 -- Test %f (float) - smart formatting without trailing zeros
138 local float_cases = {
139 { '%f', '1.5', 1.5 },
140 { '%f', '100.0', 100 },
141 { '%f', '-42.75', -42.75 },
142 { '%1f', '1.5', 1.5 },
143 { 'pi=%f', 'pi=3.14', 3.14 },
144 }
145
146 for _, c in ipairs(float_cases) do
147 local s = log.slog(c[1], c[3])
148 assert_equal(s, c[2], string.format("Float format test: '%s' doesn't match with '%s'",
149 c[2], s))
150 end
151
152 -- Test %.Nf (float with precision)
153 local precision_cases = {
154 { '%.2f', '1.50', 1.5 },
155 { '%.3f', '3.145', 3.145 },
156 { '%.1f', '100.0', 100 },
157 { '%.0f', '42', 42.0 },
158 { 'price=%.2f', 'price=19.99', 19.99 },
159 }
160
161 for _, c in ipairs(precision_cases) do
162 local s = log.slog(c[1], c[3])
163 assert_equal(s, c[2], string.format("Precision format test: '%s' doesn't match with '%s'",
164 c[2], s))
165 end
166
167 -- Test mixed type specifiers
168 local mixed_type_cases = {
169 { 'count=%1d, price=%.2f, name=%3', 'count=100, price=1.50, name=string', 100, 1.5, 'string' },
170 { '%d %f %s', '42 3.14 test', 42, 3.14, 'test' },
171 { 'int=%d, float=%.3f, str=%s', 'int=100, float=1.500, str=hello', 100, 1.5, 'hello' },
172 }
173
174 for _, c in ipairs(mixed_type_cases) do
175 local s = log.slog(c[1], c[3], c[4], c[5])
176 assert_equal(s, c[2], string.format("Mixed type format test: '%s' doesn't match with '%s'",
177 c[2], s))
178 end
179
180 -- Test type conversion from strings
181 local string_conversion_cases = {
182 { '%d', '42', '42' }, -- String to int
183 { '%f', '3.14', '3.14' }, -- String to float
184 { '%.2f', '3.14', '3.14' }, -- String to float with precision
185 }
186
187 for _, c in ipairs(string_conversion_cases) do
188 local s = log.slog(c[1], c[3])
189 assert_equal(s, c[2], string.format("String conversion test: '%s' doesn't match with '%s'",
190 c[2], s))
191 end
192
193 -- Test fallback for non-numeric types
194 local fallback_cases = {
195 { '%d', '0', nil }, -- nil should become 0
196 { '%f', '0.0', nil }, -- nil should become 0.0
197 { '%.2f', '0.00', nil }, -- nil with precision should become 0.00
198 }
199
200 for _, c in ipairs(fallback_cases) do
201 local s = log.slog(c[1], c[3])
202 assert_equal(s, c[2], string.format("Fallback test: '%s' doesn't match with '%s'",
203 c[2], s))
204 end
205
206 -- Test %% escaping
207 local escape_cases = {
208 { '%%', '%' },
209 { '100%%', '100%' },
210 { 'price=%.2f%%', 'price=19.99%', 19.99 },
211 { '%1 is %%d not %d', '100 is %d not 42', 100, 42 },
212 }
213
214 for _, c in ipairs(escape_cases) do
215 local s
216 if c[4] then
217 s = log.slog(c[1], c[3], c[4])
218 elseif c[3] then
219 s = log.slog(c[1], c[3])
220 else
221 s = log.slog(c[1])
222 end
223 assert_equal(s, c[2], string.format("Escape test: '%s' doesn't match with '%s'",
224 c[2], s))
225 end
226 end)
227 end)