Debian Codegraph @

test/lua/unit/utf.lua

raw
1 -- Test utf routines
2
3 context("UTF8 check functions", function()
4 local ok, ffi = pcall(require, "ffi")
5 if not ok then
6 ffi = require("cffi")
7 end
8 ffi.cdef [[
9 unsigned int rspamd_str_lc_utf8 (char *str, unsigned int size);
10 unsigned int rspamd_str_lc (char *str, unsigned int size);
11 void rspamd_fast_utf8_library_init (unsigned flags);
12 void ottery_rand_bytes(void *buf, size_t n);
13 double rspamd_get_ticks(int allow);
14 size_t rspamd_fast_utf8_validate (const char *data, size_t len);
15 size_t rspamd_fast_utf8_validate_ref (const char *data, size_t len);
16 char * rspamd_str_make_utf_valid (const char *src, size_t slen, size_t *dstlen, void *);
17 ]]
18
19 local cases = {
20 { "АбЫрвАлг", "абырвалг" },
21 { "АAБBвc", "аaбbвc" },
22 --{"STRASSE", "straße"}, XXX: NYI
23 { "KEÇİ", "keçi" },
24 }
25
26 for i, c in ipairs(cases) do
27 test("UTF lowercase " .. tostring(i), function()
28 local buf = ffi.new("char[?]", #c[1] + 1)
29 ffi.copy(buf, c[1])
30 local nlen = ffi.C.rspamd_str_lc_utf8(buf, #c[1])
31 local s = ffi.string(buf, nlen)
32 assert_equal(s, c[2])
33 end)
34 end
35
36 cases = {
37 { "AbCdEf", "abcdef" },
38 { "A", "a" },
39 { "AaAa", "aaaa" },
40 { "AaAaAaAa", "aaaaaaaa" }
41 }
42
43 for i, c in ipairs(cases) do
44 test("ASCII lowercase " .. tostring(i), function()
45 local buf = ffi.new("char[?]", #c[1] + 1)
46 ffi.copy(buf, c[1])
47 ffi.C.rspamd_str_lc(buf, #c[1])
48 local s = ffi.string(buf)
49 assert_equal(s, c[2])
50 end)
51 end
52
53 cases = {
54 { 'тест', 'тест' },
55 { '\200\213\202', '���' },
56 { 'тест\200\213\202test', 'тест���test' },
57 { '\200\213\202test', '���test' },
58 { '\200\213\202test\200\213\202', '���test���' },
59 { 'тест\200\213\202test\200\213\202', 'тест���test���' },
60 { 'тест\200\213\202test\200\213\202тест', 'тест���test���тест' },
61 }
62
63 local NULL = ffi.new 'void*'
64 for i, c in ipairs(cases) do
65 test("Unicode make valid " .. tostring(i), function()
66 local buf = ffi.new("char[?]", #c[1] + 1)
67 ffi.copy(buf, c[1])
68
69 local s = ffi.string(ffi.C.rspamd_str_make_utf_valid(buf, #c[1], NULL, NULL))
70 local function to_hex(s)
71 return (s:gsub('.', function(c)
72 return string.format('%02X', string.byte(c))
73 end))
74 end
75 print(to_hex(s))
76 print(to_hex(c[2]))
77 assert_equal(s, c[2])
78 end)
79 end
80
81 -- Helper to convert cdata numbers to Lua numbers (cffi-lua compatibility)
82 local function cdata_to_number(v)
83 -- Try tonumber first (works in LuaJIT and for small values)
84 local n = tonumber(v)
85 if n then return n end
86 -- For 64-bit integers in cffi-lua, convert via string
87 return tonumber(tostring(v):match("^(%d+)"))
88 end
89
90 -- Enable sse and avx2
91 ffi.C.rspamd_fast_utf8_library_init(3)
92 local valid_cases = {
93 "a",
94 "\xc3\xb1",
95 "\xe2\x82\xa1",
96 "\xf0\x90\x8c\xbc",
97 "안녕하세요, 세상"
98 }
99 for i, c in ipairs(valid_cases) do
100 test("Unicode validate success: " .. tostring(i), function()
101 local buf = ffi.new("char[?]", #c + 1)
102 ffi.copy(buf, c)
103
104 local ret = ffi.C.rspamd_fast_utf8_validate(buf, #c)
105 assert_equal(cdata_to_number(ret), 0)
106 end)
107 end
108 local invalid_cases = {
109 "\xc3\x28",
110 "\xa0\xa1",
111 "\xe2\x28\xa1",
112 "\xe2\x82\x28",
113 "\xf0\x28\x8c\xbc",
114 "\xf0\x90\x28\xbc",
115 "\xf0\x28\x8c\x28",
116 "\xc0\x9f",
117 "\xf5\xff\xff\xff",
118 "\xed\xa0\x81",
119 "\xf8\x90\x80\x80\x80",
120 "123456789012345\xed",
121 "123456789012345\xf1",
122 "123456789012345\xc2",
123 "\xC2\x7F"
124 }
125 for i, c in ipairs(invalid_cases) do
126 test("Unicode validate fail: " .. tostring(i), function()
127 local buf = ffi.new("char[?]", #c + 1)
128 ffi.copy(buf, c)
129
130 local ret = ffi.C.rspamd_fast_utf8_validate(buf, #c)
131 assert_not_equal(cdata_to_number(ret), 0)
132 end)
133 end
134
135 if os.getenv("RSPAMD_LUA_EXPENSIVE_TESTS") then
136 local speed_iters = 10000
137 local function test_size(buflen, is_valid, impl)
138 local logger = require "rspamd_logger"
139 local test_str
140 if is_valid then
141 test_str = table.concat(valid_cases)
142 else
143 test_str = table.concat(valid_cases) .. table.concat(invalid_cases)
144 end
145
146 local buf = ffi.new("char[?]", buflen)
147 if #test_str < buflen then
148 local t = {}
149 local len = #test_str
150 while len < buflen do
151 t[#t + 1] = test_str
152 len = len + #test_str
153 end
154 test_str = table.concat(t)
155 end
156 ffi.copy(buf, test_str:sub(1, buflen))
157
158 local tm = 0
159
160 for _ = 1, speed_iters do
161 if impl == 'ref' then
162 local t1 = ffi.C.rspamd_get_ticks(1)
163 ffi.C.rspamd_fast_utf8_validate_ref(buf, buflen)
164 local t2 = ffi.C.rspamd_get_ticks(1)
165 tm = tm + (t2 - t1)
166 elseif impl == 'opt' then
167 local t1 = ffi.C.rspamd_get_ticks(1)
168 ffi.C.rspamd_fast_utf8_validate(buf, buflen)
169 local t2 = ffi.C.rspamd_get_ticks(1)
170 tm = tm + (t2 - t1)
171 end
172 end
173
174 logger.messagex("%s utf8 %s check (valid = %s): %s ticks per iter, %s ticks per byte",
175 impl, buflen, is_valid,
176 tm / speed_iters, tm / speed_iters / buflen)
177
178 return 0
179 end
180
181 for _, sz in ipairs({ 78, 512, 65535 }) do
182 test(string.format("Utf8 test %s %d buffer, %s", 'ref', sz, 'valid'), function()
183 local res = test_size(sz, true, 'ref')
184 assert_equal(res, 0)
185 end)
186 test(string.format("Utf8 test %s %d buffer, %s", 'ref', sz, 'invalid'), function()
187 local res = test_size(sz, false, 'ref')
188 assert_equal(res, 0)
189 end)
190 test(string.format("Utf8 test %s %d buffer, %s", 'opt', sz, 'valid'), function()
191 local res = test_size(sz, true, 'opt')
192 assert_equal(res, 0)
193 end)
194 test(string.format("Utf8 test %s %d buffer, %s", 'opt', sz, 'invalid'), function()
195 local res = test_size(sz, false, 'opt')
196 assert_equal(res, 0)
197 end)
198 end
199 end
200
201 end)