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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "contrib/libottery/ottery.h"
  23. #include "libcryptobox/cryptobox.h"
  24. #include <vector>
  25. #include <utility>
  26. #include <string>
  27. extern "C" long rspamd_http_parse_keepalive_timeout(const rspamd_ftok_t *tok);
  28. TEST_SUITE("rspamd_utils")
  29. {
  30. TEST_CASE("rspamd_strip_smtp_comments_inplace")
  31. {
  32. std::vector<std::pair<std::string, std::string>> cases{
  33. {"abc", "abc"},
  34. {"abc(foo)", "abc"},
  35. {"abc(foo()", "abc"},
  36. {"abc(foo))", "abc)"},
  37. {"abc(foo(bar))", "abc"},
  38. {"(bar)abc(foo)", "abc"},
  39. {"ab(ololo)c(foo)", "abc"},
  40. {"ab(olo\\)lo)c(foo)", "abc"},
  41. {"ab(trol\\\1lo)c(foo)", "abc"},
  42. {"\\ab(trol\\\1lo)c(foo)", "abc"},
  43. {"", ""},
  44. {"<test_id@example.net> (added by postmaster@example.net)", "<test_id@example.net> "}};
  45. for (const auto &c: cases) {
  46. SUBCASE(("strip comments in " + c.first).c_str())
  47. {
  48. auto *cpy = new char[c.first.size()];
  49. memcpy(cpy, c.first.data(), c.first.size());
  50. auto nlen = rspamd_strip_smtp_comments_inplace(cpy, c.first.size());
  51. CHECK(std::string{cpy, nlen} == c.second);
  52. delete[] cpy;
  53. }
  54. }
  55. }
  56. TEST_CASE("rspamd_http_parse_keepalive_timeout")
  57. {
  58. std::vector<std::pair<std::string, long>> cases{
  59. {"timeout=5, max=1000", 5},
  60. {"max=1000, timeout=5", 5},
  61. {"max=1000, timeout=", -1},
  62. {"max=1000, timeout=0", 0},
  63. {"max=1000, timeout=-5", -1},
  64. {"timeout=5", 5},
  65. {" timeout=5; ", 5},
  66. {"timeout = 5", 5},
  67. };
  68. for (const auto &c: cases) {
  69. SUBCASE(("parse http keepalive header " + c.first).c_str())
  70. {
  71. rspamd_ftok_t t;
  72. t.begin = c.first.data();
  73. t.len = c.first.size();
  74. auto res = rspamd_http_parse_keepalive_timeout(&t);
  75. CHECK(res == c.second);
  76. }
  77. }
  78. }
  79. TEST_CASE("rspamd_fstring_gzip tests")
  80. {
  81. rspamd_fstring_t *fstr;
  82. // Test empty data compression
  83. SUBCASE("Empty data")
  84. {
  85. fstr = rspamd_fstring_new_init("", 0);
  86. gboolean result = rspamd_fstring_gzip(&fstr);
  87. CHECK(result == TRUE);
  88. CHECK(fstr->len == 20);
  89. result = rspamd_fstring_gunzip(&fstr);
  90. CHECK(result == TRUE);
  91. CHECK(fstr->len == 0);
  92. rspamd_fstring_free(fstr);
  93. }
  94. SUBCASE("Non empty data")
  95. {
  96. fstr = RSPAMD_FSTRING_LIT("helohelo");
  97. gboolean result = rspamd_fstring_gzip(&fstr);
  98. CHECK(result == TRUE);
  99. CHECK(fstr->len == 26);
  100. result = rspamd_fstring_gunzip(&fstr);
  101. CHECK(result == TRUE);
  102. CHECK(memcmp(fstr->str, "helohelo", fstr->len) == 0);
  103. CHECK(fstr->len == sizeof("helohelo") - 1);
  104. rspamd_fstring_free(fstr);
  105. }
  106. SUBCASE("Some real compression")
  107. {
  108. fstr = rspamd_fstring_sized_new(sizeof("helohelo") * 1024);
  109. for (int i = 0; i < 1024; i++) {
  110. fstr = rspamd_fstring_append(fstr, "helohelo", sizeof("helohelo") - 1);
  111. }
  112. gboolean result = rspamd_fstring_gzip(&fstr);
  113. CHECK(result == TRUE);
  114. CHECK(fstr->len == 49);
  115. result = rspamd_fstring_gunzip(&fstr);
  116. CHECK(result == TRUE);
  117. CHECK(memcmp(fstr->str, "helohelo", sizeof("helohelo") - 1) == 0);
  118. CHECK(fstr->len == (sizeof("helohelo") - 1) * 1024);
  119. rspamd_fstring_free(fstr);
  120. }
  121. SUBCASE("Random data compression")
  122. {
  123. rspamd_cryptobox_fast_hash_state_t hst;
  124. rspamd_cryptobox_fast_hash_init(&hst, 0);
  125. fstr = rspamd_fstring_sized_new(30 * 1024 * 1024);
  126. for (int i = 0; i < 30 * 1024; i++) {
  127. char tmp[1024];
  128. ottery_rand_bytes(tmp, sizeof(tmp));
  129. fstr = rspamd_fstring_append(fstr, tmp, sizeof(tmp));
  130. rspamd_cryptobox_fast_hash_update(&hst, tmp, sizeof(tmp));
  131. }
  132. auto crc = rspamd_cryptobox_fast_hash(fstr->str, fstr->len, 0);
  133. CHECK(crc == rspamd_cryptobox_fast_hash_final(&hst));
  134. gboolean result = rspamd_fstring_gzip(&fstr);
  135. CHECK(result == TRUE);
  136. // Assuming there are no miracles
  137. CHECK(fstr->len >= 30 * 1024 * 1024);
  138. result = rspamd_fstring_gunzip(&fstr);
  139. CHECK(result == TRUE);
  140. CHECK(fstr->len == 30 * 1024 * 1024);
  141. auto final_crc = rspamd_cryptobox_fast_hash(fstr->str, fstr->len, 0);
  142. CHECK(crc == final_crc);
  143. rspamd_fstring_free(fstr);
  144. }
  145. }
  146. }
  147. #endif