| 1 |
context("Regexp unit tests", function() |
| 2 |
local re = require("rspamd_regexp") |
| 3 |
|
| 4 |
test("Regexp creation", function() |
| 5 |
assert_not_nil(re.create_cached('/test$/m')) |
| 6 |
assert_not_nil(re.create_cached('^test$', 'm')) |
| 7 |
assert_not_nil(re.create_cached('m,test,m')) |
| 8 |
assert_not_nil(re.create_cached('m|test|m')) |
| 9 |
end) |
| 10 |
test("Regexp match", function() |
| 11 |
local cases = { |
| 12 |
{'/Тест/iu', 'тест', true}, |
| 13 |
{'/test$/m', '123test', true}, |
| 14 |
{'/^test$/m', '123test', false}, |
| 15 |
{'m,test,', 'test', true}, |
| 16 |
{'m,test,', 'test123', false}, |
| 17 |
{'m{https?://[^/?\\s]+?:\\d+(?<!:80)(?<!:443)(?<!:8080)(?:/|\\s|$)}', '', false}, |
| 18 |
{'/test/i', 'TeSt123', true}, |
| 19 |
-- Raw regexp |
| 20 |
{'/\\S<[-\\w\\.]+\\@[-\\w\\.]+>/r', 'some<[email protected]>', true}, |
| 21 |
-- Cyrillic utf8 letter |
| 22 |
{'/\\S<[-\\w\\.]+\\@[-\\w\\.]+>/r', 'some<example@exаmple.com>', false}, |
| 23 |
} |
| 24 |
|
| 25 |
for _,c in ipairs(cases) do |
| 26 |
local r = re.create_cached(c[1]) |
| 27 |
assert_not_nil(r, "cannot parse " .. c[1]) |
| 28 |
local res = r:match(c[2]) |
| 29 |
|
| 30 |
assert_equal(res, c[3], string.format("'%s' doesn't match with '%s'", |
| 31 |
c[2], c[1])) |
| 32 |
end |
| 33 |
end) |
| 34 |
|
| 35 |
test("Regexp capture", function() |
| 36 |
local cases = { |
| 37 |
{'Body=(\\S+)(?: Fuz1=(\\S+))?(?: Fuz2=(\\S+))?', |
| 38 |
'mc-filter4 1120; Body=1 Fuz1=2 Fuz2=3', |
| 39 |
{'Body=1 Fuz1=2 Fuz2=3', '1', '2', '3'}}, |
| 40 |
{'Body=(\\S+)(?: Fuz1=(\\S+))?(?: Fuz2=(\\S+))?', |
| 41 |
'mc-filter4 1120; Body=1 Fuz1=2', {'Body=1 Fuz1=2', '1', '2'}}, |
| 42 |
{'Body=(\\S+)(?: Fuz1=(\\S+))?(?: Fuz2=(\\S+))?', |
| 43 |
'mc-filter4 1120; Body=1 Fuz1=2 mc-filter4 1120; Body=1 Fuz1=2 Fuz2=3', |
| 44 |
{'Body=1 Fuz1=2', '1', '2'}, {'Body=1 Fuz1=2 Fuz2=3', '1', '2', '3'}}, |
| 45 |
} |
| 46 |
for _,c in ipairs(cases) do |
| 47 |
local r = re.create_cached(c[1]) |
| 48 |
assert_not_nil(r, "cannot parse " .. c[1]) |
| 49 |
local res = r:search(c[2], false, true) |
| 50 |
|
| 51 |
assert_not_nil(res, "cannot find pattern") |
| 52 |
|
| 53 |
for k = 3, #c do |
| 54 |
for n,m in ipairs(c[k]) do |
| 55 |
assert_equal(res[k - 2][n], c[k][n], string.format("'%s' doesn't match with '%s'", |
| 56 |
c[k][n], res[k - 2][n])) |
| 57 |
end |
| 58 |
end |
| 59 |
end |
| 60 |
end) |
| 61 |
|
| 62 |
test("Regexp split", function() |
| 63 |
local cases = { |
| 64 |
{'\\s', 'one', {'one'}}, -- one arg |
| 65 |
{'\\s', 'one two', {'one', 'two'}}, -- trivial |
| 66 |
{'/,/i', '1,2', {'1', '2'}}, -- trivial |
| 67 |
{'\\s', 'one two', {'one', 'two'}}, -- multiple delimiters |
| 68 |
{'\\s', ' one two ', {'one', 'two'}}, -- multiple delimiters |
| 69 |
{'\\s', ' one ', {'one'}}, -- multiple delimiters |
| 70 |
{'[:,]', ',,,:::one,two,,', {'one', 'two'}}, -- multiple delimiters |
| 71 |
{'[\\|\\s]', '16265 | 1.1.1.0/22 | TR | ripencc | 2014-02-28', |
| 72 |
{'16265', '1.1.1.0/22', 'TR', 'ripencc', '2014-02-28'}}, -- practical |
| 73 |
{'|', '16265 | 1.1.1.0/22 | TR | ripencc | 2014-02-28', {}} -- bad re |
| 74 |
} |
| 75 |
|
| 76 |
for _,c in ipairs(cases) do |
| 77 |
local r = re.create_cached(c[1]) |
| 78 |
assert_not_nil(r, "cannot parse " .. c[1]) |
| 79 |
|
| 80 |
local res = r:split(c[2]) |
| 81 |
assert_not_nil(res, "cannot split " .. c[2]) |
| 82 |
|
| 83 |
for i,r in ipairs(c[3]) do |
| 84 |
assert_equal(res[i], r) |
| 85 |
end |
| 86 |
end |
| 87 |
end) |
| 88 |
|
| 89 |
end |
| 90 |
) |