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.

smtp_addr.lua 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. -- SMTP address parser tests
  2. context("SMTP address check functions", function()
  3. local logger = require("rspamd_logger")
  4. local ffi = require("ffi")
  5. local util = require("rspamd_util")
  6. require "fun" ()
  7. ffi.cdef[[
  8. struct rspamd_email_address {
  9. const char *raw;
  10. const char *addr;
  11. const char *user;
  12. const char *domain;
  13. const char *name;
  14. unsigned raw_len;
  15. unsigned addr_len;
  16. unsigned domain_len;
  17. uint16_t user_len;
  18. unsigned char flags;
  19. };
  20. struct rspamd_email_address * rspamd_email_address_from_smtp (const char *str, unsigned len);
  21. void rspamd_email_address_free (struct rspamd_email_address *addr);
  22. ]]
  23. local cases_valid = {
  24. {'<>', {addr = ''}},
  25. {'<a@example.com>', {user = 'a', domain = 'example.com', addr = 'a@example.com'}},
  26. {'<a-b@example.com>', {user = 'a-b', domain = 'example.com', addr = 'a-b@example.com'}},
  27. {'<a-b@ex-ample.com>', {user = 'a-b', domain = 'ex-ample.com', addr = 'a-b@ex-ample.com'}},
  28. {'1367=dec2a6ce-81bd-4fa9-ad02-ec5956466c04=9=1655370@example.220-volt.ru',
  29. {user = '1367=dec2a6ce-81bd-4fa9-ad02-ec5956466c04=9=1655370',
  30. domain = 'example.220-volt.ru',
  31. addr = '1367=dec2a6ce-81bd-4fa9-ad02-ec5956466c04=9=1655370@example.220-volt.ru'}},
  32. {'notification+kjdm---m7wwd@facebookmail.com', {user = 'notification+kjdm---m7wwd'}},
  33. {'a@example.com', {user = 'a', domain = 'example.com', addr = 'a@example.com'}},
  34. {'a+b@example.com', {user = 'a+b', domain = 'example.com', addr = 'a+b@example.com'}},
  35. {'"a"@example.com', {user = 'a', domain = 'example.com', addr = 'a@example.com'}},
  36. {'"a+b"@example.com', {user = 'a+b', domain = 'example.com', addr = 'a+b@example.com'}},
  37. {'"<>"@example.com', {user = '<>', domain = 'example.com', addr = '<>@example.com'}},
  38. {'<"<>"@example.com>', {user = '<>', domain = 'example.com', addr = '<>@example.com'}},
  39. {'"\\""@example.com', {user = '"', domain = 'example.com', addr = '"@example.com'}},
  40. {'"\\"abc"@example.com', {user = '"abc', domain = 'example.com', addr = '"abc@example.com'}},
  41. {'<@domain1,@domain2,@domain3:abc@example.com>',
  42. {user = 'abc', domain = 'example.com', addr = 'abc@example.com'}},
  43. }
  44. each(function(case)
  45. test("Parse valid smtp addr: " .. case[1], function()
  46. local st = ffi.C.rspamd_email_address_from_smtp(case[1], #case[1])
  47. assert_not_nil(st, "should be able to parse " .. case[1])
  48. each(function(k, ex)
  49. if k == 'user' then
  50. local str = ffi.string(st.user, st.user_len)
  51. assert_equal(str, ex)
  52. elseif k == 'domain' then
  53. local str = ffi.string(st.domain, st.domain_len)
  54. assert_equal(str, ex)
  55. elseif k == 'addr' then
  56. local str = ffi.string(st.addr, st.addr_len)
  57. assert_equal(str, ex)
  58. end
  59. end, case[2])
  60. ffi.C.rspamd_email_address_free(st)
  61. end)
  62. end, cases_valid)
  63. local cases_invalid = {
  64. 'a',
  65. 'a"b"@example.com',
  66. 'a"@example.com',
  67. '"a@example.com',
  68. '<a@example.com',
  69. 'a@example.com>',
  70. '<a@.example.com>',
  71. '<a@example.com>>',
  72. '<a@example.com><>',
  73. }
  74. each(function(case)
  75. test("Parse invalid smtp addr: " .. case, function()
  76. local st = ffi.C.rspamd_email_address_from_smtp(case, #case)
  77. assert_nil(st, "should not be able to parse " .. case)
  78. end)
  79. end, cases_invalid)
  80. test("Speed test", function()
  81. local case = '<@domain1,@domain2,@domain3:abc%d@example.com>'
  82. local niter = 100000
  83. local total = 0
  84. for i = 1,niter do
  85. local ncase = string.format(case, i)
  86. local t1 = util.get_ticks()
  87. local st = ffi.C.rspamd_email_address_from_smtp(ncase, #ncase)
  88. local t2 = util.get_ticks()
  89. ffi.C.rspamd_email_address_free(st)
  90. total = total + t2 - t1
  91. end
  92. print(string.format('Spend %f seconds in processing addrs', total))
  93. end)
  94. end)