You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

regxep.lua 3.2KB

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