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_shingles_test.c 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*-
  2. * Copyright 2016 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. #include "config.h"
  17. #include "rspamd.h"
  18. #include "shingles.h"
  19. #include "ottery.h"
  20. #include <math.h>
  21. static const gchar *
  22. algorithm_to_string (enum rspamd_shingle_alg alg)
  23. {
  24. const gchar *ret = "unknown";
  25. switch (alg) {
  26. case RSPAMD_SHINGLES_OLD:
  27. ret = "siphash";
  28. break;
  29. case RSPAMD_SHINGLES_XXHASH:
  30. ret = "xxhash";
  31. break;
  32. case RSPAMD_SHINGLES_MUMHASH:
  33. ret = "mumhash";
  34. break;
  35. case RSPAMD_SHINGLES_FAST:
  36. ret = "fasthash";
  37. break;
  38. }
  39. return ret;
  40. }
  41. static void
  42. generate_random_string (char *begin, size_t len)
  43. {
  44. gsize i;
  45. for (i = 0; i < len; i ++) {
  46. begin[i] = ottery_rand_range ('z' - 'a') + 'a';
  47. }
  48. }
  49. static GArray *
  50. generate_fuzzy_words (gsize cnt, gsize max_len)
  51. {
  52. GArray *res;
  53. gsize i, wlen;
  54. rspamd_ftok_t w;
  55. char *t;
  56. res = g_array_sized_new (FALSE, FALSE, sizeof (rspamd_ftok_t), cnt);
  57. for (i = 0; i < cnt; i ++) {
  58. wlen = ottery_rand_range (max_len) + 1;
  59. /* wlen = max_len; */
  60. w.len = wlen;
  61. t = g_malloc (wlen);
  62. generate_random_string (t, wlen);
  63. w.begin = t;
  64. g_array_append_val (res, w);
  65. }
  66. return res;
  67. }
  68. static void
  69. permute_vector (GArray *in, gdouble prob)
  70. {
  71. gsize i, total = 0;
  72. rspamd_ftok_t *w;
  73. for (i = 0; i < in->len; i ++) {
  74. if (ottery_rand_unsigned () <= G_MAXUINT * prob) {
  75. w = &g_array_index (in, rspamd_ftok_t, i);
  76. generate_random_string ((gchar *)w->begin, w->len);
  77. total ++;
  78. }
  79. }
  80. msg_debug ("generated %z permutations of %ud words", total, in->len);
  81. }
  82. static void
  83. free_fuzzy_words (GArray *ar)
  84. {
  85. gsize i;
  86. rspamd_ftok_t *w;
  87. for (i = 0; i < ar->len; i ++) {
  88. w = &g_array_index (ar, rspamd_ftok_t, i);
  89. g_free ((gpointer)w->begin);
  90. }
  91. }
  92. static void
  93. test_case (gsize cnt, gsize max_len, gdouble perm_factor,
  94. enum rspamd_shingle_alg alg)
  95. {
  96. GArray *input;
  97. struct rspamd_shingle *sgl, *sgl_permuted;
  98. gdouble res;
  99. guchar key[16];
  100. gdouble ts1, ts2;
  101. ottery_rand_bytes (key, sizeof (key));
  102. input = generate_fuzzy_words (cnt, max_len);
  103. ts1 = rspamd_get_virtual_ticks ();
  104. sgl = rspamd_shingles_from_text (input, key, NULL,
  105. rspamd_shingles_default_filter, NULL, alg);
  106. ts2 = rspamd_get_virtual_ticks ();
  107. permute_vector (input, perm_factor);
  108. sgl_permuted = rspamd_shingles_from_text (input, key, NULL,
  109. rspamd_shingles_default_filter, NULL, alg);
  110. res = rspamd_shingles_compare (sgl, sgl_permuted);
  111. msg_info ("%s (%z words of %z max len, %.2f perm factor):"
  112. " percentage of common shingles: %.3f, generate time: %.4f sec",
  113. algorithm_to_string (alg), cnt, max_len, perm_factor, res, ts2 - ts1);
  114. //g_assert_cmpfloat (fabs ((1.0 - res) - sqrt (perm_factor)), <=, 0.25);
  115. free_fuzzy_words (input);
  116. g_free (sgl);
  117. g_free (sgl_permuted);
  118. }
  119. static const guint64 expected_old[RSPAMD_SHINGLE_SIZE] = {
  120. 0x2a97e024235cedc5, 0x46238acbcc55e9e0, 0x2378ff151af075b3, 0xde1f29a95cad109,
  121. 0x5d3bbbdb5db5d19f, 0x4d75a0ec52af10a6, 0x215ecd6372e755b5, 0x7b52295758295350,
  122. 0x17387d1beddc7f62, 0x26264ca879ffcada, 0x49d4a65ec0ab9914, 0xa2763e6995350cf,
  123. 0x3f4570231449c13f, 0x3309f857a0e54ee5, 0x24e4c5b561b0fce3, 0x1f153e3b275bfd1b,
  124. 0x4d067dbc97c3fd78, 0x9ffa2d076fa4f8bc, 0x3d8907f84b9ffc6c, 0x1cfd664c5262d256,
  125. 0xcdd7e744b699c15, 0x5544a2bbe05124f7, 0x5a4029b5d6a06f7, 0xd5adfbdc756c0e4,
  126. 0xa504b23d9689a67e, 0x15d945f7007de115, 0xbf676c0522a2c51d, 0x1c8d8163ad4b0f93,
  127. 0xa2c4ba20799344d7, 0x27c6f13c02134388, 0xa1d443d31fd5a3, 0x99fbca9f8563080,
  128. };
  129. static const guint64 expected_xxhash[RSPAMD_SHINGLE_SIZE] = {
  130. 0x33b134be11a705a, 0x36e2ea657aa36903, 0x6547b57f7470ce9d, 0x8253eb6d2f8f158e,
  131. 0x1cc99e3cf22388f, 0x2396da27ea36ffe8, 0x1b457d208ad3d96c, 0x2d6ac733d7a2c107,
  132. 0x17849cbed75cc4d1, 0x4dd94e772330e804, 0x39f592fa32014ed4, 0xa2f6229ad356461,
  133. 0x6dc825879a057b37, 0x886b12cef4338b05, 0x8b23af68c186518a, 0x16932b40339aaf02,
  134. 0x412090c6bb0b719c, 0x4d4a88cbdf1935f3, 0x233bcbddb5f67a7, 0x474719442a33dcca,
  135. 0x2da7ec30563e622, 0x7ab90086960e1ad2, 0x3ea2b45582539f75, 0x108cd9287d95a6c5,
  136. 0x69ba7c67c115597, 0x10880860eb75e982, 0x16f3d90e6ab995a6, 0x5f24ea09379b9f5c,
  137. 0x3c2dc04088e8fe54, 0x340b8cf1c6f1227, 0x193bc348ed2e9ce7, 0x68454ef43da9c748,
  138. };
  139. static const guint64 expected_mumhash[RSPAMD_SHINGLE_SIZE] = {
  140. 0x38d35473b80a7fc3, 0x1300531adc2d16a1, 0x26883bc89f78f4bd, 0x57de365ef6d1a62,
  141. 0x773603185fcbb20a, 0x39c6cbd7ebbeaa88, 0x676c7445ad167e70, 0x432315d1ecc4c0b1,
  142. 0x1380b95756dbb078, 0x9ee12832fa53b90e, 0x72970be210f0dd0b, 0x62909bd520f5956,
  143. 0x66196965a45eb32a, 0x2466a9ca5436620e, 0x157b828b10e10f6e, 0x429bb673a523a7e5,
  144. 0x51a6ace94f320f88, 0x23f53a30bd7d7147, 0xbee557664d3bc34c, 0x65730c88cd212a9,
  145. 0x87e72c0cd05fd0e, 0x417a744669baeb3d, 0x78e26f7917829324, 0x439777dcfc25fdf4,
  146. 0x582eac6ff013f00b, 0x1e40aa90e367f4af, 0x301d14a28d6c23a2, 0x34140ecb21b6c69,
  147. 0x390a091c8b4c31b9, 0x2e35fecf9fff0ae7, 0x94322e1a5cf31f1b, 0x33cb9190905e049a,
  148. };
  149. static const guint64 expected_fasthash[RSPAMD_SHINGLE_SIZE] = {
  150. 0x3843a716f94828a6, 0x13fd5386dda3b28d, 0x71cb09de527c40a, 0x5d6f59ffd839c62,
  151. 0x7ce3633acd568476, 0x9014298cbd00167, 0x6708ec29eedb5350, 0x2882931ff2c5c410,
  152. 0x1839d8b947b12571, 0x58f7bc3829173302, 0x4dac8103da51abc4, 0x6c5cbcc6fb1de28,
  153. 0x31fefcef9bafb755, 0x6f2d1a0b1feca401, 0x3e71f3718e520b06, 0x42f6ba11164ab231,
  154. 0x21164d010bd76f4a, 0x4c597ccc7b60f620, 0x2cf1ca3383b77574, 0x54ff9c01660b8add,
  155. 0x2ca344758f40380d, 0x1b962321bd37d0f2, 0x9323bb99c32bc418, 0x375659d0eef2b8f2,
  156. 0x1dbd23a1030084b7, 0x83cb978dee06aa0a, 0x42c97be5b27a7763, 0x3b6d6b7270ed765,
  157. 0x125c12fdba584aed, 0x1c826397afe58763, 0x8bdbe2d43f3eda96, 0x954cda70edf6591f,
  158. };
  159. void
  160. rspamd_shingles_test_func (void)
  161. {
  162. enum rspamd_shingle_alg alg = RSPAMD_SHINGLES_OLD;
  163. struct rspamd_shingle *sgl;
  164. guchar key[16];
  165. GArray *input;
  166. rspamd_ftok_t tok;
  167. int i;
  168. memset (key, 0, sizeof (key));
  169. input = g_array_sized_new (FALSE, FALSE, sizeof (rspamd_ftok_t), 5);
  170. for (i = 0; i < 5; i ++) {
  171. gchar *b = g_alloca (8);
  172. memset (b, 0, 8);
  173. memcpy (b + 1, "test", 4);
  174. b[0] = 'a' + i;
  175. tok.begin = b;
  176. tok.len = 5 + ((i + 1) % 4);
  177. g_array_append_val (input, tok);
  178. }
  179. sgl = rspamd_shingles_from_text (input, key, NULL,
  180. rspamd_shingles_default_filter, NULL, RSPAMD_SHINGLES_OLD);
  181. for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) {
  182. g_assert (sgl->hashes[i] == expected_old[i]);
  183. }
  184. g_free (sgl);
  185. sgl = rspamd_shingles_from_text (input, key, NULL,
  186. rspamd_shingles_default_filter, NULL, RSPAMD_SHINGLES_XXHASH);
  187. for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) {
  188. g_assert (sgl->hashes[i] == expected_xxhash[i]);
  189. }
  190. g_free (sgl);
  191. sgl = rspamd_shingles_from_text (input, key, NULL,
  192. rspamd_shingles_default_filter, NULL, RSPAMD_SHINGLES_MUMHASH);
  193. for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) {
  194. g_assert (sgl->hashes[i] == expected_mumhash[i]);
  195. }
  196. g_free (sgl);
  197. sgl = rspamd_shingles_from_text (input, key, NULL,
  198. rspamd_shingles_default_filter, NULL, RSPAMD_SHINGLES_FAST);
  199. for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) {
  200. g_assert (sgl->hashes[i] == expected_fasthash[i]);
  201. }
  202. g_free (sgl);
  203. for (alg = RSPAMD_SHINGLES_OLD; alg <= RSPAMD_SHINGLES_FAST; alg ++) {
  204. test_case (200, 10, 0.1, alg);
  205. test_case (500, 20, 0.01, alg);
  206. test_case (5000, 20, 0.01, alg);
  207. test_case (5000, 15, 0, alg);
  208. test_case (5000, 30, 1.0, alg);
  209. test_case (50000, 30, 0.02, alg);
  210. test_case (50000, 5, 0.02, alg);
  211. test_case (50000, 16, 0.02, alg);
  212. }
  213. }