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