1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
context("Rspamd util for lua - check generic functions", function()
local util = require 'rspamd_util'
local cases = {
{
input = "test1",
result = false,
mixed_script = false,
range_start = 0x0000,
range_end = 0x017f
},
{
input = "test test xxx",
result = false,
mixed_script = false,
range_start = 0x0000,
range_end = 0x017f
},
{
input = "АбЫрвАлг",
result = true,
mixed_script = false,
range_start = 0x0000,
range_end = 0x017f
},
{
input = "АбЫрвАлг example",
result = true,
mixed_script = true,
range_start = 0x0000,
range_end = 0x017f
},
{
input = "example ąłśćżłóę",
result = false,
mixed_script = false,
range_start = 0x0000,
range_end = 0x017f
},
{
input = "ąłśćżłóę АбЫрвАлг",
result = true,
mixed_script = true,
range_start = 0x0000,
range_end = 0x017f
},
}
for i,c in ipairs(cases) do
test("is_utf_outside_range, test case #" .. i, function()
local actual = util.is_utf_outside_range(c.input, c.range_start, c.range_end)
assert_equal(c.result, actual)
end)
end
test("is_utf_outside_range, check cache", function ()
cache_size = 20
for i = 1,cache_size do
local res = util.is_utf_outside_range("a", 0x0000, 0x0000+i)
end
end)
test("is_utf_outside_range, check empty string", function ()
assert_error(util.is_utf_outside_range)
end)
test("get_string_stats, test case", function()
local res = util.get_string_stats("this is test 99")
assert_equal(res["letters"], 10)
assert_equal(res["digits"], 2)
end)
-- Tests for get_text_quality
test("get_text_quality, empty string", function()
local res = util.get_text_quality("")
assert_equal(res.total, 0)
assert_equal(res.letters, 0)
assert_equal(res.words, 0)
end)
test("get_text_quality, simple ASCII text", function()
local res = util.get_text_quality("Hello World")
assert_equal(res.total, 11)
assert_equal(res.letters, 10)
assert_equal(res.spaces, 1)
assert_equal(res.words, 2)
assert_equal(res.word_chars, 10)
assert_equal(res.uppercase, 2) -- H, W
assert_equal(res.lowercase, 8)
assert_equal(res.ascii_chars, 11)
assert_equal(res.non_ascii_chars, 0)
assert_equal(res.latin_vowels, 3) -- e, o, o
assert_equal(res.latin_consonants, 7) -- H, l, l, W, r, l, d
end)
test("get_text_quality, Russian (Cyrillic) text", function()
local res = util.get_text_quality("Привет мир")
assert_equal(res.letters, 9)
assert_equal(res.spaces, 1)
assert_equal(res.words, 2)
assert_equal(res.non_ascii_chars, 9) -- all Cyrillic letters
assert_equal(res.ascii_chars, 1) -- space
assert_equal(res.latin_vowels, 0)
assert_equal(res.latin_consonants, 0)
assert_equal(res.script_transitions, 0) -- all same script
end)
test("get_text_quality, mixed Latin and Cyrillic (script transitions)", function()
local res = util.get_text_quality("Hello Привет")
assert_equal(res.letters, 11)
assert_equal(res.words, 2)
assert_true(res.script_transitions > 0) -- at least one transition
assert_true(res.latin_vowels > 0)
assert_true(res.latin_consonants > 0)
end)
test("get_text_quality, digits only", function()
local res = util.get_text_quality("12345")
assert_equal(res.total, 5)
assert_equal(res.digits, 5)
assert_equal(res.letters, 0)
assert_equal(res.words, 0)
assert_equal(res.printable, 5)
end)
test("get_text_quality, punctuation", function()
local res = util.get_text_quality("Hello, World!")
assert_equal(res.punctuation, 2) -- comma and exclamation
assert_equal(res.words, 2)
end)
test("get_text_quality, double spaces", function()
local res = util.get_text_quality("Hello World Test")
assert_equal(res.double_spaces, 3) -- 2 in " " + 1 extra in " "
assert_equal(res.spaces, 5)
end)
test("get_text_quality, uppercase text", function()
local res = util.get_text_quality("HELLO WORLD")
assert_equal(res.uppercase, 10)
assert_equal(res.lowercase, 0)
end)
test("get_text_quality, lowercase text", function()
local res = util.get_text_quality("hello world")
assert_equal(res.uppercase, 0)
assert_equal(res.lowercase, 10)
end)
test("get_text_quality, single characters (no words)", function()
local res = util.get_text_quality("A B C D E")
assert_equal(res.letters, 5)
assert_equal(res.words, 0) -- single letters don't count as words
assert_equal(res.word_chars, 0)
end)
test("get_text_quality, vowels vs consonants", function()
local res = util.get_text_quality("aeiou")
assert_equal(res.latin_vowels, 5)
assert_equal(res.latin_consonants, 0)
res = util.get_text_quality("bcdfg")
assert_equal(res.latin_vowels, 0)
assert_equal(res.latin_consonants, 5)
end)
test("get_text_quality, emojis", function()
local res = util.get_text_quality("Hello 👋 World")
assert_equal(res.emojis, 1)
assert_equal(res.words, 2) -- Hello and World
end)
test("get_text_quality, mixed content", function()
local res = util.get_text_quality("Test123! Hello...")
assert_equal(res.letters, 9) -- Test + Hello
assert_equal(res.digits, 3)
assert_equal(res.punctuation, 4) -- ! and ...
assert_equal(res.words, 2) -- Test and Hello
end)
for i,c in ipairs(cases) do
test("is_utf_mixed_script, test case #" .. i, function()
local actual = util.is_utf_mixed_script(c.input)
assert_equal(c.mixed_script, actual)
end)
end
test("is_utf_mixed_script, invalid utf str should return errror", function()
assert_error(util.is_utf_mixed_script,'\200\213\202')
end)
test("is_utf_mixed_script, empty str should return errror", function()
assert_error(util.is_utf_mixed_script,'\200\213\202')
end)
end)
context("Rspamd string utility", function()
local ok, ffi = pcall(require, "ffi")
if not ok then
ffi = require("cffi")
end
ffi.cdef[[
char ** rspamd_string_len_split (const char *in, size_t len,
const char *spill, int max_elts, void *pool);
void g_strfreev (char **str_array);
]]
local NULL = ffi.new 'void*'
local cases = {
{'', ';,', {}},
{'', '', {}},
{'a', ';,', {'a'}},
{'a', '', {'a'}},
{'a;b', ';', {'a', 'b'}},
{'a;;b', ';', {'a', 'b'}},
{';a;;b;', ';', {'a', 'b'}},
{'ab', ';', {'ab'}},
{'a,;b', ',', {'a', ';b'}},
{'a,;b', ';,', {'a', 'b'}},
{',a,;b', ';,', {'a', 'b'}},
{',,;', ';,', {}},
{',,;a', ';,', {'a'}},
{'a,,;', ';,', {'a'}},
}
for i,case in ipairs(cases) do
test("rspamd_string_len_split: case " .. tostring(i), function()
local ret = ffi.C.rspamd_string_len_split(case[1], #case[1],
case[2], -1, NULL)
local actual = {}
while ret[#actual] ~= NULL do
actual[#actual + 1] = ffi.string(ret[#actual])
end
assert_rspamd_table_eq({
expect = case[3],
actual = actual
})
ffi.C.g_strfreev(ret)
end)
end
end)
|