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.

rspamd_util.lua 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. context("Rspamd util for lua - check generic functions", function()
  2. local util = require 'rspamd_util'
  3. local cases = {
  4. {
  5. input = "test1",
  6. result = false,
  7. mixed_script = false,
  8. range_start = 0x0000,
  9. range_end = 0x017f
  10. },
  11. {
  12. input = "test test xxx",
  13. result = false,
  14. mixed_script = false,
  15. range_start = 0x0000,
  16. range_end = 0x017f
  17. },
  18. {
  19. input = "АбЫрвАлг",
  20. result = true,
  21. mixed_script = false,
  22. range_start = 0x0000,
  23. range_end = 0x017f
  24. },
  25. {
  26. input = "АбЫрвАлг example",
  27. result = true,
  28. mixed_script = true,
  29. range_start = 0x0000,
  30. range_end = 0x017f
  31. },
  32. {
  33. input = "example ąłśćżłóę",
  34. result = false,
  35. mixed_script = false,
  36. range_start = 0x0000,
  37. range_end = 0x017f
  38. },
  39. {
  40. input = "ąłśćżłóę АбЫрвАлг",
  41. result = true,
  42. mixed_script = true,
  43. range_start = 0x0000,
  44. range_end = 0x017f
  45. },
  46. }
  47. for i,c in ipairs(cases) do
  48. test("is_utf_outside_range, test case #" .. i, function()
  49. local actual = util.is_utf_outside_range(c.input, c.range_start, c.range_end)
  50. assert_equal(c.result, actual)
  51. end)
  52. end
  53. test("is_utf_outside_range, check cache", function ()
  54. cache_size = 20
  55. for i = 1,cache_size do
  56. local res = util.is_utf_outside_range("a", 0x0000, 0x0000+i)
  57. end
  58. end)
  59. test("is_utf_outside_range, check empty string", function ()
  60. assert_error(util.is_utf_outside_range)
  61. end)
  62. test("get_string_stats, test case", function()
  63. local res = util.get_string_stats("this is test 99")
  64. assert_equal(res["letters"], 10)
  65. assert_equal(res["digits"], 2)
  66. end)
  67. for i,c in ipairs(cases) do
  68. test("is_utf_mixed_script, test case #" .. i, function()
  69. local actual = util.is_utf_mixed_script(c.input)
  70. assert_equal(c.mixed_script, actual)
  71. end)
  72. end
  73. test("is_utf_mixed_script, invalid utf str should return errror", function()
  74. assert_error(util.is_utf_mixed_script,'\200\213\202')
  75. end)
  76. test("is_utf_mixed_script, empty str should return errror", function()
  77. assert_error(util.is_utf_mixed_script,'\200\213\202')
  78. end)
  79. end)
  80. context("Rspamd string utility", function()
  81. local ffi = require 'ffi'
  82. ffi.cdef[[
  83. char ** rspamd_string_len_split (const char *in, size_t len,
  84. const char *spill, int max_elts, void *pool);
  85. void g_strfreev (char **str_array);
  86. ]]
  87. local NULL = ffi.new 'void*'
  88. local cases = {
  89. {'', ';,', {}},
  90. {'', '', {}},
  91. {'a', ';,', {'a'}},
  92. {'a', '', {'a'}},
  93. {'a;b', ';', {'a', 'b'}},
  94. {'a;;b', ';', {'a', 'b'}},
  95. {';a;;b;', ';', {'a', 'b'}},
  96. {'ab', ';', {'ab'}},
  97. {'a,;b', ',', {'a', ';b'}},
  98. {'a,;b', ';,', {'a', 'b'}},
  99. {',a,;b', ';,', {'a', 'b'}},
  100. {',,;', ';,', {}},
  101. {',,;a', ';,', {'a'}},
  102. {'a,,;', ';,', {'a'}},
  103. }
  104. for i,case in ipairs(cases) do
  105. test("rspamd_string_len_split: case " .. tostring(i), function()
  106. local ret = ffi.C.rspamd_string_len_split(case[1], #case[1],
  107. case[2], -1, NULL)
  108. local actual = {}
  109. while ret[#actual] ~= NULL do
  110. actual[#actual + 1] = ffi.string(ret[#actual])
  111. end
  112. assert_rspamd_table_eq({
  113. expect = case[3],
  114. actual = actual
  115. })
  116. ffi.C.g_strfreev(ret)
  117. end)
  118. end
  119. end)