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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Copyright (c) 2014, Vsevolod Stakhov
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. *
  12. * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
  13. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
  16. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  19. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #include "config.h"
  24. #include "rspamd.h"
  25. #include "shingles.h"
  26. #include "ottery.h"
  27. #include <math.h>
  28. static void
  29. generate_random_string (char *begin, size_t len)
  30. {
  31. gsize i;
  32. for (i = 0; i < len; i ++) {
  33. begin[i] = ottery_rand_range ('z' - 'a') + 'a';
  34. }
  35. }
  36. static GArray *
  37. generate_fuzzy_words (gsize cnt, gsize max_len)
  38. {
  39. GArray *res;
  40. gsize i, wlen;
  41. rspamd_ftok_t w;
  42. char *t;
  43. res = g_array_sized_new (FALSE, FALSE, sizeof (rspamd_ftok_t), cnt);
  44. for (i = 0; i < cnt; i ++) {
  45. wlen = ottery_rand_range (max_len) + 1;
  46. w.len = wlen;
  47. t = g_malloc (wlen);
  48. generate_random_string (t, wlen);
  49. w.begin = t;
  50. g_array_append_val (res, w);
  51. }
  52. return res;
  53. }
  54. static void
  55. permute_vector (GArray *in, gdouble prob)
  56. {
  57. gsize i, total = 0;
  58. rspamd_ftok_t *w;
  59. for (i = 0; i < in->len; i ++) {
  60. if (ottery_rand_unsigned () <= G_MAXUINT * prob) {
  61. w = &g_array_index (in, rspamd_ftok_t, i);
  62. generate_random_string ((gchar *)w->begin, w->len);
  63. total ++;
  64. }
  65. }
  66. msg_debug ("generated %z permutations of %ud words", total, in->len);
  67. }
  68. static void
  69. free_fuzzy_words (GArray *ar)
  70. {
  71. gsize i;
  72. rspamd_ftok_t *w;
  73. for (i = 0; i < ar->len; i ++) {
  74. w = &g_array_index (ar, rspamd_ftok_t, i);
  75. g_free ((gpointer)w->begin);
  76. }
  77. }
  78. static void
  79. test_case (gsize cnt, gsize max_len, gdouble perm_factor)
  80. {
  81. GArray *input;
  82. struct rspamd_shingle *sgl, *sgl_permuted;
  83. gdouble res;
  84. guchar key[16];
  85. gdouble ts1, ts2;
  86. ottery_rand_bytes (key, sizeof (key));
  87. input = generate_fuzzy_words (cnt, max_len);
  88. ts1 = rspamd_get_ticks ();
  89. sgl = rspamd_shingles_generate (input, key, NULL,
  90. rspamd_shingles_default_filter, NULL);
  91. ts2 = rspamd_get_ticks ();
  92. permute_vector (input, perm_factor);
  93. sgl_permuted = rspamd_shingles_generate (input, key, NULL,
  94. rspamd_shingles_default_filter, NULL);
  95. res = rspamd_shingles_compare (sgl, sgl_permuted);
  96. msg_debug ("percentage of common shingles: %.3f, generate time: %hd usec",
  97. res, (gint)(ts1 - ts2) * 1000);
  98. g_assert_cmpfloat (fabs ((1.0 - res) - sqrt (perm_factor)), <=, 0.20);
  99. free_fuzzy_words (input);
  100. g_free (sgl);
  101. g_free (sgl_permuted);
  102. }
  103. void
  104. rspamd_shingles_test_func (void)
  105. {
  106. //test_case (5, 100, 0.5);
  107. test_case (200, 10, 0.1);
  108. test_case (500, 20, 0.01);
  109. test_case (5000, 20, 0.01);
  110. test_case (5000, 15, 0);
  111. test_case (5000, 30, 1.0);
  112. }