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_cxx_unit_utils.hxx 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*-
  2. * Copyright 2021 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* Detached unit tests for the utils */
  17. #ifndef RSPAMD_RSPAMD_CXX_UNIT_UTILS_HXX
  18. #define RSPAMD_RSPAMD_CXX_UNIT_UTILS_HXX
  19. #define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL
  20. #include "doctest/doctest.h"
  21. #include "libmime/mime_headers.h"
  22. #include <vector>
  23. #include <utility>
  24. #include <string>
  25. extern "C" long rspamd_http_parse_keepalive_timeout (const rspamd_ftok_t *tok);
  26. TEST_SUITE("rspamd_utils") {
  27. TEST_CASE("rspamd_strip_smtp_comments_inplace")
  28. {
  29. std::vector<std::pair<std::string, std::string>> cases{
  30. {"abc", "abc"},
  31. {"abc(foo)", "abc"},
  32. {"abc(foo()", "abc"},
  33. {"abc(foo))", "abc)"},
  34. {"abc(foo(bar))", "abc"},
  35. {"(bar)abc(foo)", "abc"},
  36. {"ab(ololo)c(foo)", "abc"},
  37. {"ab(olo\\)lo)c(foo)", "abc"},
  38. {"ab(trol\\\1lo)c(foo)", "abc"},
  39. {"\\ab(trol\\\1lo)c(foo)", "abc"},
  40. {"", ""},
  41. {"<test_id@example.net> (added by postmaster@example.net)", "<test_id@example.net> "}
  42. };
  43. for (const auto &c : cases) {
  44. SUBCASE (("strip comments in " + c.first).c_str()) {
  45. auto *cpy = new char[c.first.size()];
  46. memcpy(cpy, c.first.data(), c.first.size());
  47. auto nlen = rspamd_strip_smtp_comments_inplace(cpy, c.first.size());
  48. CHECK(std::string{cpy, nlen} == c.second);
  49. delete[] cpy;
  50. }
  51. }
  52. }
  53. TEST_CASE("rspamd_http_parse_keepalive_timeout")
  54. {
  55. std::vector<std::pair<std::string, long>> cases {
  56. {"timeout=5, max=1000", 5},
  57. {"max=1000, timeout=5", 5},
  58. {"max=1000, timeout=", -1},
  59. {"max=1000, timeout=0", 0},
  60. {"max=1000, timeout=-5", -1},
  61. {"timeout=5", 5},
  62. {" timeout=5; ", 5},
  63. {"timeout = 5", 5},
  64. };
  65. for (const auto &c : cases) {
  66. SUBCASE (("parse http keepalive header " + c.first).c_str()) {
  67. rspamd_ftok_t t;
  68. t.begin = c.first.data();
  69. t.len = c.first.size();
  70. auto res = rspamd_http_parse_keepalive_timeout(&t);
  71. CHECK(res == c.second);
  72. }
  73. }
  74. }
  75. }
  76. #endif