diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-07-26 10:49:23 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-07-26 10:49:23 +0100 |
commit | 537a7180a0d5132c11636c4fd8b1450cd99d352c (patch) | |
tree | fb9f8c84955a411bdffbd6371ea32f2716fb3687 /test | |
parent | 5fd7a90fdaa33f52c59bdb0ca84451e5c1e22365 (diff) | |
download | rspamd-537a7180a0d5132c11636c4fd8b1450cd99d352c.tar.gz rspamd-537a7180a0d5132c11636c4fd8b1450cd99d352c.zip |
[Rework] Use clang-format to unify formatting in all sources
No meaningful changes.
Diffstat (limited to 'test')
-rw-r--r-- | test/rspamd_cryptobox_test.c | 269 | ||||
-rw-r--r-- | test/rspamd_cxx_local_ptr.hxx | 508 | ||||
-rw-r--r-- | test/rspamd_cxx_unit.cxx | 17 | ||||
-rw-r--r-- | test/rspamd_cxx_unit_dkim.hxx | 73 | ||||
-rw-r--r-- | test/rspamd_cxx_unit_utils.hxx | 227 | ||||
-rw-r--r-- | test/rspamd_dkim_test.c | 15 | ||||
-rw-r--r-- | test/rspamd_dns_test.c | 83 | ||||
-rw-r--r-- | test/rspamd_heap_test.c | 166 | ||||
-rw-r--r-- | test/rspamd_http_test.c | 481 | ||||
-rw-r--r-- | test/rspamd_lua_pcall_vs_resume_test.c | 97 | ||||
-rw-r--r-- | test/rspamd_lua_test.c | 114 | ||||
-rw-r--r-- | test/rspamd_mem_pool_test.c | 28 | ||||
-rw-r--r-- | test/rspamd_radix_test.c | 313 | ||||
-rw-r--r-- | test/rspamd_rrd_test.c | 70 | ||||
-rw-r--r-- | test/rspamd_shingles_test.c | 321 | ||||
-rw-r--r-- | test/rspamd_statfile_test.c | 3 | ||||
-rw-r--r-- | test/rspamd_test_suite.c | 104 | ||||
-rw-r--r-- | test/rspamd_upstream_test.c | 171 | ||||
-rw-r--r-- | test/rspamd_url_test.c | 95 | ||||
-rw-r--r-- | test/tests.h | 32 |
20 files changed, 1633 insertions, 1554 deletions
diff --git a/test/rspamd_cryptobox_test.c b/test/rspamd_cryptobox_test.c index 15386c52c..e3d85048d 100644 --- a/test/rspamd_cryptobox_test.c +++ b/test/rspamd_cryptobox_test.c @@ -27,68 +27,68 @@ static const int random_fuzz_cnt = 10000; enum rspamd_cryptobox_mode mode = RSPAMD_CRYPTOBOX_MODE_25519; static void * -create_mapping (int mapping_len, guchar **beg, guchar **end) +create_mapping(int mapping_len, guchar **beg, guchar **end) { void *map; - int psize = getpagesize (); + int psize = getpagesize(); - map = mmap (NULL, mapping_len + psize * 3, PROT_READ|PROT_WRITE, - MAP_ANON|MAP_SHARED, -1, 0); - g_assert (map != 0); - memset (map, 0, mapping_len + psize * 3); - mprotect (map, psize, PROT_NONE); + map = mmap(NULL, mapping_len + psize * 3, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_SHARED, -1, 0); + g_assert(map != 0); + memset(map, 0, mapping_len + psize * 3); + mprotect(map, psize, PROT_NONE); /* Misalign pointer */ - *beg = ((guchar *)map) + psize + 1; + *beg = ((guchar *) map) + psize + 1; *end = *beg + mapping_len; - mprotect (*beg + mapping_len - 1 + psize, psize, PROT_NONE); + mprotect(*beg + mapping_len - 1 + psize, psize, PROT_NONE); return map; } static void -check_result (const rspamd_nm_t key, const rspamd_nonce_t nonce, - const rspamd_mac_t mac, guchar *begin, guchar *end) +check_result(const rspamd_nm_t key, const rspamd_nonce_t nonce, + const rspamd_mac_t mac, guchar *begin, guchar *end) { - guint64 *t = (guint64 *)begin; + guint64 *t = (guint64 *) begin; - g_assert (rspamd_cryptobox_decrypt_nm_inplace (begin, end - begin, nonce, key, - mac, mode)); + g_assert(rspamd_cryptobox_decrypt_nm_inplace(begin, end - begin, nonce, key, + mac, mode)); - while (t < (guint64 *)end) { - g_assert (*t == 0); - t ++; + while (t < (guint64 *) end) { + g_assert(*t == 0); + t++; } } static int -create_random_split (struct rspamd_cryptobox_segment *seg, int mseg, - guchar *begin, guchar *end) +create_random_split(struct rspamd_cryptobox_segment *seg, int mseg, + guchar *begin, guchar *end) { gsize remain = end - begin; gint used = 0; while (remain > 0 && used < mseg - 1) { seg->data = begin; - seg->len = ottery_rand_range (remain - 1) + 1; + seg->len = ottery_rand_range(remain - 1) + 1; begin += seg->len; remain -= seg->len; - used ++; - seg ++; + used++; + seg++; } if (remain > 0) { seg->data = begin; seg->len = remain; - used ++; + used++; } return used; } static int -create_realistic_split (struct rspamd_cryptobox_segment *seg, int mseg, - guchar *begin, guchar *end) +create_realistic_split(struct rspamd_cryptobox_segment *seg, int mseg, + guchar *begin, guchar *end) { gsize remain = end - begin; gint used = 0; @@ -97,12 +97,12 @@ create_realistic_split (struct rspamd_cryptobox_segment *seg, int mseg, while (remain > 0 && used < mseg - 1) { seg->data = begin; - if (ottery_rand_uint32 () % 2 == 0) { - seg->len = ottery_rand_range (small_seg) + 1; + if (ottery_rand_uint32() % 2 == 0) { + seg->len = ottery_rand_range(small_seg) + 1; } else { - seg->len = ottery_rand_range (medium_seg) + - small_seg; + seg->len = ottery_rand_range(medium_seg) + + small_seg; } if (seg->len > remain) { seg->len = remain; @@ -110,23 +110,23 @@ create_realistic_split (struct rspamd_cryptobox_segment *seg, int mseg, begin += seg->len; remain -= seg->len; - used ++; - seg ++; + used++; + seg++; } if (remain > 0) { seg->data = begin; seg->len = remain; - used ++; + used++; } return used; } static int -create_constrained_split (struct rspamd_cryptobox_segment *seg, int mseg, - int constraint, - guchar *begin, guchar *end) +create_constrained_split(struct rspamd_cryptobox_segment *seg, int mseg, + int constraint, + guchar *begin, guchar *end) { gsize remain = end - begin; gint used = 0; @@ -139,21 +139,20 @@ create_constrained_split (struct rspamd_cryptobox_segment *seg, int mseg, } begin += seg->len; remain -= seg->len; - used ++; - seg ++; + used++; + seg++; } if (remain > 0) { seg->data = begin; seg->len = remain; - used ++; + used++; } return used; } -void -rspamd_cryptobox_test_func (void) +void rspamd_cryptobox_test_func(void) { void *map; guchar *begin, *end; @@ -165,98 +164,98 @@ rspamd_cryptobox_test_func (void) gint i, cnt, ms; gboolean checked_openssl = FALSE; - map = create_mapping (mapping_size, &begin, &end); + map = create_mapping(mapping_size, &begin, &end); - ottery_rand_bytes (key, sizeof (key)); - ottery_rand_bytes (nonce, sizeof (nonce)); + ottery_rand_bytes(key, sizeof(key)); + ottery_rand_bytes(nonce, sizeof(nonce)); - memset (mac, 0, sizeof (mac)); - seg = g_slice_alloc0 (sizeof (*seg) * max_seg * 10); + memset(mac, 0, sizeof(mac)); + seg = g_slice_alloc0(sizeof(*seg) * max_seg * 10); /* Test baseline */ - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encrypt_nm_inplace (begin, end - begin, nonce, key, mac, - mode); - t2 = rspamd_get_ticks (TRUE); - check_result (key, nonce, mac, begin, end); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encrypt_nm_inplace(begin, end - begin, nonce, key, mac, + mode); + t2 = rspamd_get_ticks(TRUE); + check_result(key, nonce, mac, begin, end); - msg_info ("baseline encryption: %.0f", t2 - t1); + msg_info("baseline encryption: %.0f", t2 - t1); mode = RSPAMD_CRYPTOBOX_MODE_NIST; - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encrypt_nm_inplace (begin, - end - begin, - nonce, - key, - mac, - mode); - t2 = rspamd_get_ticks (TRUE); - check_result (key, nonce, mac, begin, end); - - msg_info ("openssl baseline encryption: %.0f", t2 - t1); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encrypt_nm_inplace(begin, + end - begin, + nonce, + key, + mac, + mode); + t2 = rspamd_get_ticks(TRUE); + check_result(key, nonce, mac, begin, end); + + msg_info("openssl baseline encryption: %.0f", t2 - t1); mode = RSPAMD_CRYPTOBOX_MODE_25519; start: /* A single chunk as vector */ seg[0].data = begin; seg[0].len = end - begin; - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, 1, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, 1, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); - msg_info ("bulk encryption: %.0f", t2 - t1); + msg_info("bulk encryption: %.0f", t2 - t1); /* Two chunks as vector */ seg[0].data = begin; seg[0].len = (end - begin) / 2; seg[1].data = begin + seg[0].len; seg[1].len = (end - begin) - seg[0].len; - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, 2, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, 2, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); - msg_info ("2 equal chunks encryption: %.0f", t2 - t1); + msg_info("2 equal chunks encryption: %.0f", t2 - t1); seg[0].data = begin; seg[0].len = 1; seg[1].data = begin + seg[0].len; seg[1].len = (end - begin) - seg[0].len; - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, 2, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, 2, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); - msg_info ("small and large chunks encryption: %.0f", t2 - t1); + msg_info("small and large chunks encryption: %.0f", t2 - t1); seg[0].data = begin; seg[0].len = (end - begin) - 3; seg[1].data = begin + seg[0].len; seg[1].len = (end - begin) - seg[0].len; - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, 2, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, 2, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); - msg_info ("large and small chunks encryption: %.0f", t2 - t1); + msg_info("large and small chunks encryption: %.0f", t2 - t1); /* Random two chunks as vector */ seg[0].data = begin; - seg[0].len = ottery_rand_range (end - begin - 1) + 1; + seg[0].len = ottery_rand_range(end - begin - 1) + 1; seg[1].data = begin + seg[0].len; seg[1].len = (end - begin) - seg[0].len; - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, 2, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, 2, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); - msg_info ("random 2 chunks encryption: %.0f", t2 - t1); + msg_info("random 2 chunks encryption: %.0f", t2 - t1); /* 3 specific chunks */ seg[0].data = begin; @@ -265,78 +264,78 @@ start: seg[1].len = 2049; seg[2].data = begin + seg[0].len + seg[1].len; seg[2].len = (end - begin) - seg[0].len - seg[1].len; - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, 3, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, 3, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); - msg_info ("small, medium and large chunks encryption: %.0f", t2 - t1); + msg_info("small, medium and large chunks encryption: %.0f", t2 - t1); - cnt = create_random_split (seg, max_seg, begin, end); - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + cnt = create_random_split(seg, max_seg, begin, end); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, cnt, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); - msg_info ("random split of %d chunks encryption: %.0f", cnt, t2 - t1); + msg_info("random split of %d chunks encryption: %.0f", cnt, t2 - t1); - cnt = create_realistic_split (seg, max_seg, begin, end); - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + cnt = create_realistic_split(seg, max_seg, begin, end); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, cnt, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); - msg_info ("realistic split of %d chunks encryption: %.0f", cnt, t2 - t1); + msg_info("realistic split of %d chunks encryption: %.0f", cnt, t2 - t1); - cnt = create_constrained_split (seg, max_seg + 1, 32, begin, end); - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + cnt = create_constrained_split(seg, max_seg + 1, 32, begin, end); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, cnt, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); - msg_info ("constrained split of %d chunks encryption: %.0f", cnt, t2 - t1); + msg_info("constrained split of %d chunks encryption: %.0f", cnt, t2 - t1); - for (i = 0; i < random_fuzz_cnt; i ++) { - ms = ottery_rand_range (i % max_seg * 2) + 1; - cnt = create_random_split (seg, ms, begin, end); - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + for (i = 0; i < random_fuzz_cnt; i++) { + ms = ottery_rand_range(i % max_seg * 2) + 1; + cnt = create_random_split(seg, ms, begin, end); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, cnt, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); if (i % 1000 == 0) { - msg_info ("random fuzz iterations: %d", i); + msg_info("random fuzz iterations: %d", i); } } - for (i = 0; i < random_fuzz_cnt; i ++) { - ms = ottery_rand_range (i % max_seg * 2) + 1; - cnt = create_realistic_split (seg, ms, begin, end); - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + for (i = 0; i < random_fuzz_cnt; i++) { + ms = ottery_rand_range(i % max_seg * 2) + 1; + cnt = create_realistic_split(seg, ms, begin, end); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, cnt, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); if (i % 1000 == 0) { - msg_info ("realistic fuzz iterations: %d", i); + msg_info("realistic fuzz iterations: %d", i); } } - for (i = 0; i < random_fuzz_cnt; i ++) { - ms = ottery_rand_range (i % max_seg * 10) + 1; - cnt = create_constrained_split (seg, ms, i, begin, end); - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + for (i = 0; i < random_fuzz_cnt; i++) { + ms = ottery_rand_range(i % max_seg * 10) + 1; + cnt = create_constrained_split(seg, ms, i, begin, end); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, cnt, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); if (i % 1000 == 0) { - msg_info ("constrained fuzz iterations: %d", i); + msg_info("constrained fuzz iterations: %d", i); } } diff --git a/test/rspamd_cxx_local_ptr.hxx b/test/rspamd_cxx_local_ptr.hxx index 04d2e9ab4..2343a74ff 100644 --- a/test/rspamd_cxx_local_ptr.hxx +++ b/test/rspamd_cxx_local_ptr.hxx @@ -37,7 +37,7 @@ struct deleter_test { *pv = true; } }; -} +}// namespace test_internal namespace std { template<> @@ -47,294 +47,298 @@ struct hash<test_internal::deleter_test> { return 42; } }; -} - -TEST_SUITE("local_ptr") { -using namespace test_internal; - -TEST_CASE("shared_ptr from nullptr") -{ - rspamd::local_shared_ptr<int const> pi(static_cast<int *>(nullptr)); - CHECK((!!pi ? false : true)); - CHECK(!pi); - CHECK(pi.get() == nullptr); - CHECK(pi.use_count() == 1); - CHECK(pi.unique()); - +}// namespace std -} -TEST_CASE("shared_ptr from ptr") -{ - int *p = new int(7); - rspamd::local_shared_ptr<int> pi(p); - CHECK((pi? true: false)); - CHECK(!!pi); - CHECK(pi.get() == p); - CHECK(pi.use_count() == 1); - CHECK(pi.unique()); - CHECK(*pi == 7); -} - -TEST_CASE("shared_ptr copy") +TEST_SUITE("local_ptr") { - rspamd::local_shared_ptr<int> pi; - - rspamd::local_shared_ptr<int> pi2(pi); - CHECK(pi2 == pi); - CHECK((pi2? false: true)); - CHECK(!pi2); - CHECK(pi2.get() == nullptr); - CHECK(pi2.use_count() == pi.use_count()); - - rspamd::local_shared_ptr<int> pi3(pi); - CHECK(pi3 == pi); - CHECK((pi3? false: true)); - CHECK(!pi3); - CHECK(pi3.get() == nullptr); - CHECK(pi3.use_count() == pi.use_count()); - - rspamd::local_shared_ptr<int> pi4(pi3); - CHECK(pi4 == pi3); - CHECK((pi4? false: true)); - CHECK(!pi4); - CHECK(pi4.get() == nullptr); - CHECK(pi4.use_count() == pi3.use_count()); - - int * p = new int(7); - rspamd::local_shared_ptr<int> pi5(p); - - rspamd::local_shared_ptr<int> pi6(pi5); - CHECK(pi5 == pi6); - CHECK((pi6? true: false)); - CHECK(!!pi6); - CHECK(pi6.get() == p); - CHECK(pi6.use_count() == 2); - CHECK(!pi6.unique()); - CHECK(*pi6 == 7); - CHECK(pi6.use_count() == pi6.use_count()); - CHECK(!(pi5 < pi6 || pi5 < pi6)); // shared ownership test - - auto pi7 = pi6; - CHECK(pi5 == pi7); - CHECK((pi7? true: false)); - CHECK(!!pi7); - CHECK(pi7.get() == p); - CHECK(pi7.use_count() == 3); - CHECK(!pi7.unique()); - CHECK(*pi7 == 7); - CHECK(pi7.use_count() == pi7.use_count()); - CHECK(!(pi5 < pi7 || pi5 < pi7)); // shared ownership test -} - -TEST_CASE("shared_ptr move") -{ - rspamd::local_shared_ptr<int> pi(new int); - - rspamd::local_shared_ptr<int> pi2(std::move(pi)); - CHECK(!(pi2 == pi)); - CHECK((!pi2? false: true)); - CHECK(!pi); - CHECK(pi.get() == nullptr); - CHECK(pi2.get() != nullptr); - CHECK(pi.use_count() != pi2.use_count()); - - std::swap(pi, pi2); - CHECK(!(pi2 == pi)); - CHECK((!pi? false: true)); - CHECK(!pi2); - CHECK(pi.get() != nullptr); - CHECK(pi2.get() == nullptr); - CHECK(pi.use_count() != pi2.use_count()); -} - -TEST_CASE("shared_ptr dtor") { - bool t; + using namespace test_internal; + TEST_CASE("shared_ptr from nullptr") { - rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); - - CHECK((!pi ? false : true)); - CHECK(!!pi); - CHECK(pi.get() != nullptr); + rspamd::local_shared_ptr<int const> pi(static_cast<int *>(nullptr)); + CHECK((!!pi ? false : true)); + CHECK(!pi); + CHECK(pi.get() == nullptr); CHECK(pi.use_count() == 1); CHECK(pi.unique()); - CHECK(t == false); } - - CHECK(t == true); - + TEST_CASE("shared_ptr from ptr") { - rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); - - CHECK((!pi ? false : true)); + int *p = new int(7); + rspamd::local_shared_ptr<int> pi(p); + CHECK((pi ? true : false)); CHECK(!!pi); - CHECK(pi.get() != nullptr); + CHECK(pi.get() == p); CHECK(pi.use_count() == 1); CHECK(pi.unique()); - CHECK(t == false); - - rspamd::local_shared_ptr<deleter_test> pi2(pi); - CHECK(pi2 == pi); - CHECK(pi.use_count() == 2); - pi.reset(); - CHECK(!(pi2 == pi)); - CHECK(pi2.use_count() == 1); - CHECK(t == false); - - pi = pi2; - CHECK(pi2 == pi); - CHECK(pi.use_count() == 2); - CHECK(t == false); + CHECK(*pi == 7); } - CHECK(t == true); -} - -TEST_CASE("make_shared dtor") { - bool t; - + TEST_CASE("shared_ptr copy") { - auto pi = rspamd::local_make_shared<deleter_test>(t); + rspamd::local_shared_ptr<int> pi; - CHECK((!pi ? false : true)); - CHECK(!!pi); - CHECK(pi.get() != nullptr); - CHECK(pi.use_count() == 1); - CHECK(pi.unique()); - CHECK(t == false); - - rspamd::local_shared_ptr<deleter_test> pi2(pi); + rspamd::local_shared_ptr<int> pi2(pi); CHECK(pi2 == pi); - CHECK(pi.use_count() == 2); - pi.reset(); - CHECK(!(pi2 == pi)); - CHECK(pi2.use_count() == 1); - CHECK(t == false); - - pi = pi2; - CHECK(pi2 == pi); - CHECK(pi.use_count() == 2); - CHECK(t == false); + CHECK((pi2 ? false : true)); + CHECK(!pi2); + CHECK(pi2.get() == nullptr); + CHECK(pi2.use_count() == pi.use_count()); + + rspamd::local_shared_ptr<int> pi3(pi); + CHECK(pi3 == pi); + CHECK((pi3 ? false : true)); + CHECK(!pi3); + CHECK(pi3.get() == nullptr); + CHECK(pi3.use_count() == pi.use_count()); + + rspamd::local_shared_ptr<int> pi4(pi3); + CHECK(pi4 == pi3); + CHECK((pi4 ? false : true)); + CHECK(!pi4); + CHECK(pi4.get() == nullptr); + CHECK(pi4.use_count() == pi3.use_count()); + + int *p = new int(7); + rspamd::local_shared_ptr<int> pi5(p); + + rspamd::local_shared_ptr<int> pi6(pi5); + CHECK(pi5 == pi6); + CHECK((pi6 ? true : false)); + CHECK(!!pi6); + CHECK(pi6.get() == p); + CHECK(pi6.use_count() == 2); + CHECK(!pi6.unique()); + CHECK(*pi6 == 7); + CHECK(pi6.use_count() == pi6.use_count()); + CHECK(!(pi5 < pi6 || pi5 < pi6));// shared ownership test + + auto pi7 = pi6; + CHECK(pi5 == pi7); + CHECK((pi7 ? true : false)); + CHECK(!!pi7); + CHECK(pi7.get() == p); + CHECK(pi7.use_count() == 3); + CHECK(!pi7.unique()); + CHECK(*pi7 == 7); + CHECK(pi7.use_count() == pi7.use_count()); + CHECK(!(pi5 < pi7 || pi5 < pi7));// shared ownership test } - CHECK(t == true); -} - -TEST_CASE("shared_ptr dtor") { - bool t; - + TEST_CASE("shared_ptr move") { - rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); + rspamd::local_shared_ptr<int> pi(new int); + rspamd::local_shared_ptr<int> pi2(std::move(pi)); + CHECK(!(pi2 == pi)); + CHECK((!pi2 ? false : true)); + CHECK(!pi); + CHECK(pi.get() == nullptr); + CHECK(pi2.get() != nullptr); + CHECK(pi.use_count() != pi2.use_count()); + + std::swap(pi, pi2); + CHECK(!(pi2 == pi)); CHECK((!pi ? false : true)); - CHECK(!!pi); + CHECK(!pi2); CHECK(pi.get() != nullptr); - CHECK(pi.use_count() == 1); - CHECK(pi.unique()); - CHECK(t == false); + CHECK(pi2.get() == nullptr); + CHECK(pi.use_count() != pi2.use_count()); } - CHECK(t == true); - + TEST_CASE("shared_ptr dtor") { - rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); - - CHECK((!pi ? false : true)); - CHECK(!!pi); - CHECK(pi.get() != nullptr); - CHECK(pi.use_count() == 1); - CHECK(pi.unique()); - CHECK(t == false); - - rspamd::local_shared_ptr<deleter_test> pi2(pi); - CHECK(pi2 == pi); - CHECK(pi.use_count() == 2); - pi.reset(); - CHECK(!(pi2 == pi)); - CHECK(pi2.use_count() == 1); - CHECK(t == false); - - pi = pi2; - CHECK(pi2 == pi); - CHECK(pi.use_count() == 2); - CHECK(t == false); + bool t; + + { + rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); + + CHECK((!pi ? false : true)); + CHECK(!!pi); + CHECK(pi.get() != nullptr); + CHECK(pi.use_count() == 1); + CHECK(pi.unique()); + CHECK(t == false); + } + + CHECK(t == true); + + { + rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); + + CHECK((!pi ? false : true)); + CHECK(!!pi); + CHECK(pi.get() != nullptr); + CHECK(pi.use_count() == 1); + CHECK(pi.unique()); + CHECK(t == false); + + rspamd::local_shared_ptr<deleter_test> pi2(pi); + CHECK(pi2 == pi); + CHECK(pi.use_count() == 2); + pi.reset(); + CHECK(!(pi2 == pi)); + CHECK(pi2.use_count() == 1); + CHECK(t == false); + + pi = pi2; + CHECK(pi2 == pi); + CHECK(pi.use_count() == 2); + CHECK(t == false); + } + + CHECK(t == true); } - CHECK(t == true); -} - -TEST_CASE("weak_ptr") { - bool t; - + TEST_CASE("make_shared dtor") { - rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); - - CHECK((!pi ? false : true)); - CHECK(!!pi); - CHECK(pi.get() != nullptr); - CHECK(pi.use_count() == 1); - CHECK(pi.unique()); - CHECK(t == false); - - rspamd::local_weak_ptr<deleter_test> wp(pi); - CHECK(wp.lock().get() != nullptr); - CHECK(pi.use_count() == 1); - CHECK(wp.use_count() == 1); - pi.reset(); - CHECK(pi.use_count() == 0); - CHECK(wp.use_count() == 0); + bool t; + + { + auto pi = rspamd::local_make_shared<deleter_test>(t); + + CHECK((!pi ? false : true)); + CHECK(!!pi); + CHECK(pi.get() != nullptr); + CHECK(pi.use_count() == 1); + CHECK(pi.unique()); + CHECK(t == false); + + rspamd::local_shared_ptr<deleter_test> pi2(pi); + CHECK(pi2 == pi); + CHECK(pi.use_count() == 2); + pi.reset(); + CHECK(!(pi2 == pi)); + CHECK(pi2.use_count() == 1); + CHECK(t == false); + + pi = pi2; + CHECK(pi2 == pi); + CHECK(pi.use_count() == 2); + CHECK(t == false); + } + + CHECK(t == true); } - CHECK(t == true); - - rspamd::local_weak_ptr<deleter_test> wp; + TEST_CASE("shared_ptr dtor") { - rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); - wp = pi; - CHECK(!wp.expired()); - CHECK(wp.lock().get() != nullptr); + bool t; + + { + rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); + + CHECK((!pi ? false : true)); + CHECK(!!pi); + CHECK(pi.get() != nullptr); + CHECK(pi.use_count() == 1); + CHECK(pi.unique()); + CHECK(t == false); + } + + CHECK(t == true); + + { + rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); + + CHECK((!pi ? false : true)); + CHECK(!!pi); + CHECK(pi.get() != nullptr); + CHECK(pi.use_count() == 1); + CHECK(pi.unique()); + CHECK(t == false); + + rspamd::local_shared_ptr<deleter_test> pi2(pi); + CHECK(pi2 == pi); + CHECK(pi.use_count() == 2); + pi.reset(); + CHECK(!(pi2 == pi)); + CHECK(pi2.use_count() == 1); + CHECK(t == false); + + pi = pi2; + CHECK(pi2 == pi); + CHECK(pi.use_count() == 2); + CHECK(t == false); + } + + CHECK(t == true); } - CHECK(t == true); - CHECK(wp.expired()); -} - -TEST_CASE("std::swap") { - bool t; + TEST_CASE("weak_ptr") + { + bool t; + + { + rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); + + CHECK((!pi ? false : true)); + CHECK(!!pi); + CHECK(pi.get() != nullptr); + CHECK(pi.use_count() == 1); + CHECK(pi.unique()); + CHECK(t == false); + + rspamd::local_weak_ptr<deleter_test> wp(pi); + CHECK(wp.lock().get() != nullptr); + CHECK(pi.use_count() == 1); + CHECK(wp.use_count() == 1); + pi.reset(); + CHECK(pi.use_count() == 0); + CHECK(wp.use_count() == 0); + } + + CHECK(t == true); + + rspamd::local_weak_ptr<deleter_test> wp; + { + rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); + wp = pi; + CHECK(!wp.expired()); + CHECK(wp.lock().get() != nullptr); + } + + CHECK(t == true); + CHECK(wp.expired()); + } + TEST_CASE("std::swap") { - rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); - CHECK(pi.use_count() == 1); - CHECK(pi.unique()); - CHECK(t == false); + bool t; + + { + rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); + CHECK(pi.use_count() == 1); + CHECK(pi.unique()); + CHECK(t == false); + + rspamd::local_shared_ptr<deleter_test> pi1; + CHECK(pi1.get() == nullptr); + CHECK(pi1.use_count() == 0); + std::swap(pi1, pi); + CHECK(pi.use_count() == 0); + CHECK(pi.get() == nullptr); + CHECK(pi1.get() != nullptr); + std::swap(pi, pi1); + CHECK(pi.use_count() != 0); + CHECK(pi.get() != nullptr); + CHECK(pi1.get() == nullptr); + } + + CHECK(t == true); + } + TEST_CASE("std::hash") + { + bool v; + deleter_test dt(v); + CHECK(std::hash<deleter_test>()(dt) == 42); + auto pi = rspamd::local_make_shared<deleter_test>(v); rspamd::local_shared_ptr<deleter_test> pi1; - CHECK(pi1.get() == nullptr); - CHECK(pi1.use_count() == 0); - std::swap(pi1, pi); - CHECK(pi.use_count() == 0); - CHECK(pi.get() == nullptr); - CHECK(pi1.get() != nullptr); - std::swap(pi, pi1); - CHECK(pi.use_count() != 0); - CHECK(pi.get() != nullptr); - CHECK(pi1.get() == nullptr); + CHECK(std::hash<decltype(pi)>()(pi) == 42); + // No hash for nullptr, different from std::smart_pointers! + CHECK_THROWS(std::hash<decltype(pi)>()(pi1)); } - - CHECK(t == true); -} - -TEST_CASE("std::hash") { - bool v; - deleter_test dt(v); - CHECK(std::hash<deleter_test>()(dt) == 42); - auto pi = rspamd::local_make_shared<deleter_test>(v); - rspamd::local_shared_ptr<deleter_test> pi1; - CHECK(std::hash<decltype(pi)>()(pi) == 42); - // No hash for nullptr, different from std::smart_pointers! - CHECK_THROWS(std::hash<decltype(pi)>()(pi1)); -} - } -#endif //RSPAMD_RSPAMD_CXX_LOCAL_PTR_HXX +#endif//RSPAMD_RSPAMD_CXX_LOCAL_PTR_HXX diff --git a/test/rspamd_cxx_unit.cxx b/test/rspamd_cxx_unit.cxx index acbbbabce..3dde89d1c 100644 --- a/test/rspamd_cxx_unit.cxx +++ b/test/rspamd_cxx_unit.cxx @@ -27,15 +27,14 @@ static gboolean verbose = false; static const GOptionEntry entries[] = - { - {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, - "Enable verbose logging", NULL}, - {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL} - }; + { + {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, + "Enable verbose logging", NULL}, + {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}}; -int -main(int argc, char **argv) { +int main(int argc, char **argv) +{ struct rspamd_main *rspamd_main; rspamd_mempool_t *pool; struct rspamd_config *cfg; @@ -64,13 +63,13 @@ main(int argc, char **argv) { if (verbose) { rspamd_main->logger = rspamd_log_open_emergency(rspamd_main->server_pool, - RSPAMD_LOG_FLAG_USEC | RSPAMD_LOG_FLAG_ENFORCED | RSPAMD_LOG_FLAG_RSPAMADM); + RSPAMD_LOG_FLAG_USEC | RSPAMD_LOG_FLAG_ENFORCED | RSPAMD_LOG_FLAG_RSPAMADM); rspamd_log_set_log_level(rspamd_main->logger, G_LOG_LEVEL_DEBUG); } else { rspamd_main->logger = rspamd_log_open_emergency(rspamd_main->server_pool, - RSPAMD_LOG_FLAG_RSPAMADM); + RSPAMD_LOG_FLAG_RSPAMADM); rspamd_log_set_log_level(rspamd_main->logger, G_LOG_LEVEL_MESSAGE); } diff --git a/test/rspamd_cxx_unit_dkim.hxx b/test/rspamd_cxx_unit_dkim.hxx index 79556a58e..3f6b71a39 100644 --- a/test/rspamd_cxx_unit_dkim.hxx +++ b/test/rspamd_cxx_unit_dkim.hxx @@ -29,16 +29,17 @@ #include <string> #include <tuple> -TEST_SUITE("rspamd_dkim") { - -TEST_CASE("rspamd_dkim_parse_key") +TEST_SUITE("rspamd_dkim") { - struct test_case { - std::string input; - bool is_valid; - std::string expected_id; - }; - std::vector<test_case> cases{ + + TEST_CASE("rspamd_dkim_parse_key") + { + struct test_case { + std::string input; + bool is_valid; + std::string expected_id; + }; + std::vector<test_case> cases{ {"p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCA" "QEA5CeQZpoPbsS8lG41UI1rxTtOSqPrfgZzhrZsk0t9dIbFTvaoql/FLuYcbdUARc" "5zuyXsDj1eSprOgcPT9PY9RoSUsY8i/jnD49DHXtMfXoBk0J6epNzbZqqWU+" @@ -47,7 +48,7 @@ TEST_CASE("rspamd_dkim_parse_key") "lnNaq5pWei/B8pG6teV+y3t4yay5ZGktALJjlylKHVo2USkVYQTFQ9Ji25m2jupdCd" "kn1FTuYNqh0Nzg3KPQHNVp7mlE7lfwIDAQAB", true, "e40cc5c40ee29cb4f21d95c7f0dc9989"}, - // Spaces before p + // Spaces before p {" p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCA" "QEA5CeQZpoPbsS8lG41UI1rxTtOSqPrfgZzhrZsk0t9dIbFTvaoql/FLuYcbdUARc" "5zuyXsDj1eSprOgcPT9PY9RoSUsY8i/jnD49DHXtMfXoBk0J6epNzbZqqWU+" @@ -64,8 +65,8 @@ TEST_CASE("rspamd_dkim_parse_key") "VKRoF3cK1cFXLo+bgO3sEJgGtvwzPodG0CqVu+gjehrjwdLnPhqyEspfI1IbL" "lnNaq5pWei/B8pG6teV+y3t4yay5ZGktALJjlylKHVo2USkVYQTFQ9Ji25m2jupdCd" "kn1FTuYNqh0Nzg3KPQHNVp7mlE7lfwIDAQAB", - true, "e40cc5c40ee29cb4f21d95c7f0dc9989"}, - // Spaces after p + true, "e40cc5c40ee29cb4f21d95c7f0dc9989"}, + // Spaces after p {"k=rsa; t=s; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCA" "QEA5CeQZpoPbsS8lG41UI1rxTtOSqPrfgZzhrZsk0t9dIbFTvaoql/FLuYcbdUARc" "5zuyXsDj1eSprOgcPT9PY9RoSUsY8i/jnD49DHXtMfXoBk0J6epNzbZqqWU+" @@ -119,8 +120,8 @@ TEST_CASE("rspamd_dkim_parse_key") " VKRoF3cK1cFXLo+bgO3sEJgGtvwzPodG0CqVu+gjehrjwdLnPhqyEspfI1IbL" " lnNaq5pWei/B8pG6teV+y3t4yay5ZGktALJjlylKHVo2USkVYQTFQ9Ji25m2jupdCd" " kn1FTuYNqh0Nzg3KPQHNVp7mlE7lfwIDAQAB", - true, "e40cc5c40ee29cb4f21d95c7f0dc9989"}, - // Invalid RSA + true, "e40cc5c40ee29cb4f21d95c7f0dc9989"}, + // Invalid RSA {"ololo=trololo; k=rsa; t=s; " "p = BADMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCA" "QEA5CeQZpoPbsS8lG41UI1rxTtOSqPrfgZzhrZsk0t9dIbFTvaoql/FLuYcbdUARc" @@ -140,33 +141,33 @@ TEST_CASE("rspamd_dkim_parse_key") "lnNaq5pWei/B8pG6teV+y3t4yay5ZGktALJjlylKHVo2USkVYQTFQ9Ji25m2jupdCd" "kn1FTuYNqh0Nzg3KPQHNVp7mlE7lfwIDAQAB ", false, ""}, - }; + }; - auto cur_test_idx = 0; - for (auto &&c : cases) { - SUBCASE (fmt::format("process DKIM record {}: {}", cur_test_idx++, c.input).c_str()) { - GError *err = nullptr; - gsize klen = c.input.size(); - auto *key = rspamd_dkim_parse_key(c.input.c_str(), &klen, &err); - if (c.is_valid) { - REQUIRE_MESSAGE(key != nullptr, (err ? err->message : "unknown error")); - char hexbuf[RSPAMD_DKIM_KEY_ID_LEN * 2 + 1]; - auto *id = rspamd_dkim_key_id(key); - REQUIRE(id != nullptr); + auto cur_test_idx = 0; + for (auto &&c: cases) { + SUBCASE(fmt::format("process DKIM record {}: {}", cur_test_idx++, c.input).c_str()) + { + GError *err = nullptr; + gsize klen = c.input.size(); + auto *key = rspamd_dkim_parse_key(c.input.c_str(), &klen, &err); + if (c.is_valid) { + REQUIRE_MESSAGE(key != nullptr, (err ? err->message : "unknown error")); + char hexbuf[RSPAMD_DKIM_KEY_ID_LEN * 2 + 1]; + auto *id = rspamd_dkim_key_id(key); + REQUIRE(id != nullptr); - auto hexlen = rspamd_encode_hex_buf(id, RSPAMD_DKIM_KEY_ID_LEN, hexbuf, - sizeof(hexbuf)); - CHECK(hexlen > 0); - CHECK(std::string{hexbuf, (std::size_t) hexlen} == c.expected_id); - rspamd_dkim_key_free(key); - } - else { - CHECK(key == nullptr); + auto hexlen = rspamd_encode_hex_buf(id, RSPAMD_DKIM_KEY_ID_LEN, hexbuf, + sizeof(hexbuf)); + CHECK(hexlen > 0); + CHECK(std::string{hexbuf, (std::size_t) hexlen} == c.expected_id); + rspamd_dkim_key_free(key); + } + else { + CHECK(key == nullptr); + } } } } } -} - #endif diff --git a/test/rspamd_cxx_unit_utils.hxx b/test/rspamd_cxx_unit_utils.hxx index f563f4409..3e53a6d33 100644 --- a/test/rspamd_cxx_unit_utils.hxx +++ b/test/rspamd_cxx_unit_utils.hxx @@ -30,132 +30,135 @@ #include <utility> #include <string> -extern "C" long rspamd_http_parse_keepalive_timeout (const rspamd_ftok_t *tok); +extern "C" long rspamd_http_parse_keepalive_timeout(const rspamd_ftok_t *tok); -TEST_SUITE("rspamd_utils") { - -TEST_CASE("rspamd_strip_smtp_comments_inplace") +TEST_SUITE("rspamd_utils") { - std::vector<std::pair<std::string, std::string>> cases{ - {"abc", "abc"}, - {"abc(foo)", "abc"}, - {"abc(foo()", "abc"}, - {"abc(foo))", "abc)"}, - {"abc(foo(bar))", "abc"}, - {"(bar)abc(foo)", "abc"}, - {"ab(ololo)c(foo)", "abc"}, - {"ab(olo\\)lo)c(foo)", "abc"}, - {"ab(trol\\\1lo)c(foo)", "abc"}, - {"\\ab(trol\\\1lo)c(foo)", "abc"}, - {"", ""}, - {"<test_id@example.net> (added by postmaster@example.net)", "<test_id@example.net> "} - }; - - for (const auto &c: cases) { - SUBCASE (("strip comments in " + c.first).c_str()) { - auto *cpy = new char[c.first.size()]; - memcpy(cpy, c.first.data(), c.first.size()); - auto nlen = rspamd_strip_smtp_comments_inplace(cpy, c.first.size()); - CHECK(std::string{cpy, nlen} == c.second); - delete[] cpy; + + TEST_CASE("rspamd_strip_smtp_comments_inplace") + { + std::vector<std::pair<std::string, std::string>> cases{ + {"abc", "abc"}, + {"abc(foo)", "abc"}, + {"abc(foo()", "abc"}, + {"abc(foo))", "abc)"}, + {"abc(foo(bar))", "abc"}, + {"(bar)abc(foo)", "abc"}, + {"ab(ololo)c(foo)", "abc"}, + {"ab(olo\\)lo)c(foo)", "abc"}, + {"ab(trol\\\1lo)c(foo)", "abc"}, + {"\\ab(trol\\\1lo)c(foo)", "abc"}, + {"", ""}, + {"<test_id@example.net> (added by postmaster@example.net)", "<test_id@example.net> "}}; + + for (const auto &c: cases) { + SUBCASE(("strip comments in " + c.first).c_str()) + { + auto *cpy = new char[c.first.size()]; + memcpy(cpy, c.first.data(), c.first.size()); + auto nlen = rspamd_strip_smtp_comments_inplace(cpy, c.first.size()); + CHECK(std::string{cpy, nlen} == c.second); + delete[] cpy; + } } } -} -TEST_CASE("rspamd_http_parse_keepalive_timeout") -{ - std::vector<std::pair<std::string, long>> cases{ - {"timeout=5, max=1000", 5}, - {"max=1000, timeout=5", 5}, - {"max=1000, timeout=", -1}, - {"max=1000, timeout=0", 0}, - {"max=1000, timeout=-5", -1}, - {"timeout=5", 5}, - {" timeout=5; ", 5}, - {"timeout = 5", 5}, - }; - - for (const auto &c: cases) { - SUBCASE (("parse http keepalive header " + c.first).c_str()) { - rspamd_ftok_t t; - t.begin = c.first.data(); - t.len = c.first.size(); - auto res = rspamd_http_parse_keepalive_timeout(&t); - CHECK(res == c.second); + TEST_CASE("rspamd_http_parse_keepalive_timeout") + { + std::vector<std::pair<std::string, long>> cases{ + {"timeout=5, max=1000", 5}, + {"max=1000, timeout=5", 5}, + {"max=1000, timeout=", -1}, + {"max=1000, timeout=0", 0}, + {"max=1000, timeout=-5", -1}, + {"timeout=5", 5}, + {" timeout=5; ", 5}, + {"timeout = 5", 5}, + }; + + for (const auto &c: cases) { + SUBCASE(("parse http keepalive header " + c.first).c_str()) + { + rspamd_ftok_t t; + t.begin = c.first.data(); + t.len = c.first.size(); + auto res = rspamd_http_parse_keepalive_timeout(&t); + CHECK(res == c.second); + } } } -} -TEST_CASE("rspamd_fstring_gzip tests") -{ - rspamd_fstring_t *fstr; - - // Test empty data compression - SUBCASE("Empty data") { - fstr = rspamd_fstring_new_init("", 0); - gboolean result = rspamd_fstring_gzip(&fstr); - CHECK(result == TRUE); - CHECK(fstr->len == 20); - result = rspamd_fstring_gunzip(&fstr); - CHECK(result == TRUE); - CHECK(fstr->len == 0); - rspamd_fstring_free(fstr); - } + TEST_CASE("rspamd_fstring_gzip tests") + { + rspamd_fstring_t *fstr; + + // Test empty data compression + SUBCASE("Empty data") + { + fstr = rspamd_fstring_new_init("", 0); + gboolean result = rspamd_fstring_gzip(&fstr); + CHECK(result == TRUE); + CHECK(fstr->len == 20); + result = rspamd_fstring_gunzip(&fstr); + CHECK(result == TRUE); + CHECK(fstr->len == 0); + rspamd_fstring_free(fstr); + } - SUBCASE("Non empty data") { - fstr = RSPAMD_FSTRING_LIT("helohelo"); - gboolean result = rspamd_fstring_gzip(&fstr); - CHECK(result == TRUE); - CHECK(fstr->len == 26); - result = rspamd_fstring_gunzip(&fstr); - CHECK(result == TRUE); - CHECK(memcmp(fstr->str, "helohelo", fstr->len) == 0); - CHECK(fstr->len == sizeof("helohelo") - 1); - rspamd_fstring_free(fstr); - } + SUBCASE("Non empty data") + { + fstr = RSPAMD_FSTRING_LIT("helohelo"); + gboolean result = rspamd_fstring_gzip(&fstr); + CHECK(result == TRUE); + CHECK(fstr->len == 26); + result = rspamd_fstring_gunzip(&fstr); + CHECK(result == TRUE); + CHECK(memcmp(fstr->str, "helohelo", fstr->len) == 0); + CHECK(fstr->len == sizeof("helohelo") - 1); + rspamd_fstring_free(fstr); + } - SUBCASE("Some real compression") { - fstr = rspamd_fstring_sized_new(sizeof("helohelo") * 1024); - for (int i = 0; i < 1024; i ++) { - fstr = rspamd_fstring_append(fstr, "helohelo", sizeof("helohelo") - 1); + SUBCASE("Some real compression") + { + fstr = rspamd_fstring_sized_new(sizeof("helohelo") * 1024); + for (int i = 0; i < 1024; i++) { + fstr = rspamd_fstring_append(fstr, "helohelo", sizeof("helohelo") - 1); + } + gboolean result = rspamd_fstring_gzip(&fstr); + CHECK(result == TRUE); + CHECK(fstr->len == 49); + result = rspamd_fstring_gunzip(&fstr); + CHECK(result == TRUE); + CHECK(memcmp(fstr->str, "helohelo", sizeof("helohelo") - 1) == 0); + CHECK(fstr->len == (sizeof("helohelo") - 1) * 1024); + rspamd_fstring_free(fstr); } - gboolean result = rspamd_fstring_gzip(&fstr); - CHECK(result == TRUE); - CHECK(fstr->len == 49); - result = rspamd_fstring_gunzip(&fstr); - CHECK(result == TRUE); - CHECK(memcmp(fstr->str, "helohelo", sizeof("helohelo") - 1) == 0); - CHECK(fstr->len == (sizeof("helohelo") - 1) * 1024); - rspamd_fstring_free(fstr); - } - SUBCASE("Random data compression") { - rspamd_cryptobox_fast_hash_state_t hst; - rspamd_cryptobox_fast_hash_init(&hst, 0); - fstr = rspamd_fstring_sized_new(30 * 1024 * 1024); - for (int i = 0; i < 30 * 1024; i ++) { - char tmp[1024]; - ottery_rand_bytes(tmp, sizeof(tmp)); - fstr = rspamd_fstring_append(fstr, tmp, sizeof(tmp)); - rspamd_cryptobox_fast_hash_update(&hst, tmp, sizeof(tmp)); + SUBCASE("Random data compression") + { + rspamd_cryptobox_fast_hash_state_t hst; + rspamd_cryptobox_fast_hash_init(&hst, 0); + fstr = rspamd_fstring_sized_new(30 * 1024 * 1024); + for (int i = 0; i < 30 * 1024; i++) { + char tmp[1024]; + ottery_rand_bytes(tmp, sizeof(tmp)); + fstr = rspamd_fstring_append(fstr, tmp, sizeof(tmp)); + rspamd_cryptobox_fast_hash_update(&hst, tmp, sizeof(tmp)); + } + auto crc = rspamd_cryptobox_fast_hash(fstr->str, fstr->len, 0); + CHECK(crc == rspamd_cryptobox_fast_hash_final(&hst)); + gboolean result = rspamd_fstring_gzip(&fstr); + CHECK(result == TRUE); + // Assuming there are no miracles + CHECK(fstr->len >= 30 * 1024 * 1024); + result = rspamd_fstring_gunzip(&fstr); + CHECK(result == TRUE); + CHECK(fstr->len == 30 * 1024 * 1024); + auto final_crc = rspamd_cryptobox_fast_hash(fstr->str, fstr->len, 0); + CHECK(crc == final_crc); + rspamd_fstring_free(fstr); } - auto crc = rspamd_cryptobox_fast_hash(fstr->str, fstr->len, 0); - CHECK(crc == rspamd_cryptobox_fast_hash_final(&hst)); - gboolean result = rspamd_fstring_gzip(&fstr); - CHECK(result == TRUE); - // Assuming there are no miracles - CHECK(fstr->len >= 30 * 1024 * 1024); - result = rspamd_fstring_gunzip(&fstr); - CHECK(result == TRUE); - CHECK(fstr->len == 30 * 1024 * 1024); - auto final_crc = rspamd_cryptobox_fast_hash(fstr->str, fstr->len, 0); - CHECK(crc == final_crc); - rspamd_fstring_free(fstr); } - -} - - } #endif diff --git a/test/rspamd_dkim_test.c b/test/rspamd_dkim_test.c index fde27e4ae..57e4d5a2f 100644 --- a/test/rspamd_dkim_test.c +++ b/test/rspamd_dkim_test.c @@ -19,12 +19,12 @@ #include "dkim.h" static const gchar test_dkim_sig[] = "v=1; a=rsa-sha256; c=relaxed/relaxed; " - "d=highsecure.ru; s=dkim; t=1410516996; " - "bh=guFoWYHWVzFRqVyAQebnvPcdm7bUQo7pRHt/uIHD7gs=; " - "h=Message-ID:Date:From:MIME-Version:To:Subject:Content-Type:Content-Transfer-Encoding; " - "b=PCiECkOaPFb99DW+gApgfmdlTUo6XN6YXjnj52Cxoz2FoA857B0ZHFgeQe4JAKHuhW" - "oq3BLHap0GcMTTpSOgfQOKa8Df35Ns11JoOFjdBQ8GpM99kOrJP+vZcT8b7AMfthYm0Kwy" - "D9TjlkpScuoY5LjsWVnijh9dSNVLFqLatzg=;"; + "d=highsecure.ru; s=dkim; t=1410516996; " + "bh=guFoWYHWVzFRqVyAQebnvPcdm7bUQo7pRHt/uIHD7gs=; " + "h=Message-ID:Date:From:MIME-Version:To:Subject:Content-Type:Content-Transfer-Encoding; " + "b=PCiECkOaPFb99DW+gApgfmdlTUo6XN6YXjnj52Cxoz2FoA857B0ZHFgeQe4JAKHuhW" + "oq3BLHap0GcMTTpSOgfQOKa8Df35Ns11JoOFjdBQ8GpM99kOrJP+vZcT8b7AMfthYm0Kwy" + "D9TjlkpScuoY5LjsWVnijh9dSNVLFqLatzg=;"; extern struct ev_loop *event_loop; #if 0 @@ -49,8 +49,7 @@ session_fin (gpointer unused) return TRUE; } #endif -void -rspamd_dkim_test_func (void) +void rspamd_dkim_test_func(void) { #if 0 rspamd_dkim_context_t *ctx; diff --git a/test/rspamd_dns_test.c b/test/rspamd_dns_test.c index 336e58bfb..d77919342 100644 --- a/test/rspamd_dns_test.c +++ b/test/rspamd_dns_test.c @@ -12,93 +12,92 @@ extern struct ev_loop *event_loop; struct rspamd_dns_resolver *resolver; gboolean -session_fin (gpointer unused) +session_fin(gpointer unused) { - ev_break (event_loop, EVBREAK_ALL); + ev_break(event_loop, EVBREAK_ALL); return TRUE; } static void -test_dns_cb (struct rdns_reply *reply, gpointer arg) +test_dns_cb(struct rdns_reply *reply, gpointer arg) { struct rdns_reply_entry *cur; const struct rdns_request_name *name = - rdns_request_get_name (reply->request, NULL); + rdns_request_get_name(reply->request, NULL); - msg_debug ("got reply with code %s for request %s", - rdns_strerror (reply->code), name->name); + msg_debug("got reply with code %s for request %s", + rdns_strerror(reply->code), name->name); if (reply->code == RDNS_RC_NOERROR) { cur = reply->entries; while (cur) { switch (cur->type) { case RDNS_REQUEST_A: - msg_debug ("got ip: %s", inet_ntoa (cur->content.a.addr)); + msg_debug("got ip: %s", inet_ntoa(cur->content.a.addr)); break; case RDNS_REQUEST_PTR: - msg_debug ("got name %s", cur->content.ptr.name); + msg_debug("got name %s", cur->content.ptr.name); break; case RDNS_REQUEST_TXT: - msg_debug ("got txt %s", cur->content.txt.data); + msg_debug("got txt %s", cur->content.txt.data); break; case RDNS_REQUEST_SPF: - msg_debug ("got spf %s", cur->content.txt.data); + msg_debug("got spf %s", cur->content.txt.data); break; case RDNS_REQUEST_SRV: - msg_debug ("got srv pri: %d, weight: %d, port: %d, target: %s", cur->content.srv.weight, - cur->content.srv.priority, cur->content.srv.port, cur->content.srv.target); + msg_debug("got srv pri: %d, weight: %d, port: %d, target: %s", cur->content.srv.weight, + cur->content.srv.priority, cur->content.srv.port, cur->content.srv.target); break; case RDNS_REQUEST_MX: - msg_debug ("got mx %s:%d", cur->content.mx.name, cur->content.mx.priority); + msg_debug("got mx %s:%d", cur->content.mx.name, cur->content.mx.priority); break; } cur = cur->next; } } - if (-- requests == 0) { - session_fin (NULL); + if (--requests == 0) { + session_fin(NULL); } } -void -rspamd_dns_test_func (void) +void rspamd_dns_test_func(void) { struct rspamd_config *cfg; rspamd_mempool_t *pool; struct rspamd_async_session *s; - cfg = (struct rspamd_config *)g_malloc (sizeof (struct rspamd_config)); - bzero (cfg, sizeof (struct rspamd_config)); - cfg->cfg_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL, 0); + cfg = (struct rspamd_config *) g_malloc(sizeof(struct rspamd_config)); + bzero(cfg, sizeof(struct rspamd_config)); + cfg->cfg_pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), NULL, 0); cfg->dns_retransmits = 2; cfg->dns_timeout = 0.5; - pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL, 0); + pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), NULL, 0); - s = rspamd_session_create (pool, session_fin, NULL, NULL, NULL); + s = rspamd_session_create(pool, session_fin, NULL, NULL, NULL); - resolver = rspamd_dns_resolver_init (NULL, event_loop, cfg); + resolver = rspamd_dns_resolver_init(NULL, event_loop, cfg); - requests ++; - g_assert (rspamd_dns_resolver_request (resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_A, "google.com")); - requests ++; - g_assert (rspamd_dns_resolver_request (resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_PTR, "81.19.70.3")); - requests ++; - g_assert (rspamd_dns_resolver_request (resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_MX, "rambler.ru")); - requests ++; - g_assert (rspamd_dns_resolver_request (resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_TXT, "rambler.ru")); - requests ++; - g_assert (rspamd_dns_resolver_request (resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_TXT, "google.com")); - requests ++; - g_assert (rspamd_dns_resolver_request (resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_SPF, "rambler.ru")); - requests ++; - g_assert (rspamd_dns_resolver_request (resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_SRV, - "_xmpp-server._tcp.jabber.org")); - requests ++; - g_assert (rspamd_dns_resolver_request (resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_TXT, "non-existent.arpa")); + requests++; + g_assert(rspamd_dns_resolver_request(resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_A, "google.com")); + requests++; + g_assert(rspamd_dns_resolver_request(resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_PTR, "81.19.70.3")); + requests++; + g_assert(rspamd_dns_resolver_request(resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_MX, "rambler.ru")); + requests++; + g_assert(rspamd_dns_resolver_request(resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_TXT, "rambler.ru")); + requests++; + g_assert(rspamd_dns_resolver_request(resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_TXT, "google.com")); + requests++; + g_assert(rspamd_dns_resolver_request(resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_SPF, "rambler.ru")); + requests++; + g_assert(rspamd_dns_resolver_request(resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_SRV, + "_xmpp-server._tcp.jabber.org")); + requests++; + g_assert(rspamd_dns_resolver_request(resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_TXT, "non-existent.arpa")); - g_assert (resolver != NULL); + g_assert(resolver != NULL); - ev_run (event_loop, 0); + ev_run(event_loop, 0); } diff --git a/test/rspamd_heap_test.c b/test/rspamd_heap_test.c index 711ab1c88..dcc7bbc4d 100644 --- a/test/rspamd_heap_test.c +++ b/test/rspamd_heap_test.c @@ -22,53 +22,51 @@ static const guint niter = 100500; static const guint nrem = 100; -static inline -struct rspamd_min_heap_elt * -new_elt (guint pri) +static inline struct rspamd_min_heap_elt * +new_elt(guint pri) { struct rspamd_min_heap_elt *elt; - elt = g_slice_alloc0 (sizeof (*elt)); + elt = g_slice_alloc0(sizeof(*elt)); elt->pri = pri; return elt; } static gdouble -heap_nelts_test (guint nelts) +heap_nelts_test(guint nelts) { struct rspamd_min_heap *heap; struct rspamd_min_heap_elt *elts; gdouble t1, t2; guint i; - heap = rspamd_min_heap_create (nelts); + heap = rspamd_min_heap_create(nelts); /* Preallocate all elts */ - elts = g_slice_alloc (sizeof (*elts) * nelts); + elts = g_slice_alloc(sizeof(*elts) * nelts); - for (i = 0; i < nelts; i ++) { - elts[i].pri = ottery_rand_uint32 () % G_MAXINT32 + 1; + for (i = 0; i < nelts; i++) { + elts[i].pri = ottery_rand_uint32() % G_MAXINT32 + 1; elts[i].idx = 0; } - t1 = rspamd_get_virtual_ticks (); - for (i = 0; i < nelts; i ++) { - rspamd_min_heap_push (heap, &elts[i]); + t1 = rspamd_get_virtual_ticks(); + for (i = 0; i < nelts; i++) { + rspamd_min_heap_push(heap, &elts[i]); } - for (i = 0; i < nelts; i ++) { - (void)rspamd_min_heap_pop (heap); + for (i = 0; i < nelts; i++) { + (void) rspamd_min_heap_pop(heap); } - t2 = rspamd_get_virtual_ticks (); + t2 = rspamd_get_virtual_ticks(); - g_slice_free1 (sizeof (*elts) * nelts, elts); - rspamd_min_heap_destroy (heap); + g_slice_free1(sizeof(*elts) * nelts, elts); + rspamd_min_heap_destroy(heap); return (t2 - t1); } -void -rspamd_heap_test_func (void) +void rspamd_heap_test_func(void) { struct rspamd_min_heap *heap; struct rspamd_min_heap_elt *elt, *telt; @@ -77,108 +75,108 @@ rspamd_heap_test_func (void) gdouble t[16]; /* Push + update */ - heap = rspamd_min_heap_create (32); - elt = new_elt (2); - elt->data = GINT_TO_POINTER (1); - rspamd_min_heap_push (heap, elt); - elt = new_elt (3); - elt->data = GINT_TO_POINTER (2); - rspamd_min_heap_push (heap, elt); - elt = new_elt (4); - elt->data = GINT_TO_POINTER (3); - rspamd_min_heap_push (heap, elt); - - rspamd_min_heap_update_elt (heap, elt, 0); - elt = rspamd_min_heap_pop (heap); - g_assert (elt->data == GINT_TO_POINTER (3)); - - rspamd_min_heap_destroy (heap); + heap = rspamd_min_heap_create(32); + elt = new_elt(2); + elt->data = GINT_TO_POINTER(1); + rspamd_min_heap_push(heap, elt); + elt = new_elt(3); + elt->data = GINT_TO_POINTER(2); + rspamd_min_heap_push(heap, elt); + elt = new_elt(4); + elt->data = GINT_TO_POINTER(3); + rspamd_min_heap_push(heap, elt); + + rspamd_min_heap_update_elt(heap, elt, 0); + elt = rspamd_min_heap_pop(heap); + g_assert(elt->data == GINT_TO_POINTER(3)); + + rspamd_min_heap_destroy(heap); /* Push + remove */ - heap = rspamd_min_heap_create (32); - elt = new_elt (2); - elt->data = GINT_TO_POINTER (1); - rspamd_min_heap_push (heap, elt); - rspamd_min_heap_remove_elt (heap, elt); - elt = new_elt (3); - elt->data = GINT_TO_POINTER (2); - rspamd_min_heap_push (heap, elt); - elt = rspamd_min_heap_pop (heap); - g_assert (elt->data == GINT_TO_POINTER (2)); - elt = rspamd_min_heap_pop (heap); - g_assert (elt == NULL); + heap = rspamd_min_heap_create(32); + elt = new_elt(2); + elt->data = GINT_TO_POINTER(1); + rspamd_min_heap_push(heap, elt); + rspamd_min_heap_remove_elt(heap, elt); + elt = new_elt(3); + elt->data = GINT_TO_POINTER(2); + rspamd_min_heap_push(heap, elt); + elt = rspamd_min_heap_pop(heap); + g_assert(elt->data == GINT_TO_POINTER(2)); + elt = rspamd_min_heap_pop(heap); + g_assert(elt == NULL); /* Push + push + remove + pop */ - elt = new_elt (2); - elt->data = GINT_TO_POINTER (1); - rspamd_min_heap_push (heap, elt); + elt = new_elt(2); + elt->data = GINT_TO_POINTER(1); + rspamd_min_heap_push(heap, elt); telt = elt; - elt = new_elt (3); - elt->data = GINT_TO_POINTER (2); - rspamd_min_heap_push (heap, elt); - rspamd_min_heap_remove_elt (heap, telt); - elt = rspamd_min_heap_pop (heap); - g_assert (elt->data == GINT_TO_POINTER (2)); - rspamd_min_heap_destroy (heap); + elt = new_elt(3); + elt->data = GINT_TO_POINTER(2); + rspamd_min_heap_push(heap, elt); + rspamd_min_heap_remove_elt(heap, telt); + elt = rspamd_min_heap_pop(heap); + g_assert(elt->data == GINT_TO_POINTER(2)); + rspamd_min_heap_destroy(heap); /* Bulk test */ - heap = rspamd_min_heap_create (32); + heap = rspamd_min_heap_create(32); - for (i = 100; i > 0; i --) { - elt = new_elt (i - 1); - rspamd_min_heap_push (heap, elt); + for (i = 100; i > 0; i--) { + elt = new_elt(i - 1); + rspamd_min_heap_push(heap, elt); } - for (i = 0; i < 100; i ++) { - elt = rspamd_min_heap_pop (heap); - g_assert (elt->pri == i); + for (i = 0; i < 100; i++) { + elt = rspamd_min_heap_pop(heap); + g_assert(elt->pri == i); } - rspamd_min_heap_destroy (heap); + rspamd_min_heap_destroy(heap); /* Fuzz test */ - heap = rspamd_min_heap_create (128); + heap = rspamd_min_heap_create(128); /* Add */ - for (i = 0; i < niter; i ++) { - elt = new_elt (ottery_rand_uint32 () % G_MAXINT32 + 1); - rspamd_min_heap_push (heap, elt); + for (i = 0; i < niter; i++) { + elt = new_elt(ottery_rand_uint32() % G_MAXINT32 + 1); + rspamd_min_heap_push(heap, elt); } /* Remove */ - for (i = 0; i < nrem; i ++) { - elt = rspamd_min_heap_index (heap, ottery_rand_uint32 () % niter); - rspamd_min_heap_remove_elt (heap, elt); + for (i = 0; i < nrem; i++) { + elt = rspamd_min_heap_index(heap, ottery_rand_uint32() % niter); + rspamd_min_heap_remove_elt(heap, elt); } /* Update */ - for (i = 0; i < niter / 10; i ++) { - elt = rspamd_min_heap_index (heap, ottery_rand_uint32 () % (niter - nrem)); - rspamd_min_heap_update_elt (heap, elt, - ottery_rand_uint32 () % G_MAXINT32 + 1); + for (i = 0; i < niter / 10; i++) { + elt = rspamd_min_heap_index(heap, ottery_rand_uint32() % (niter - nrem)); + rspamd_min_heap_update_elt(heap, elt, + ottery_rand_uint32() % G_MAXINT32 + 1); } prev = 0; /* Pop and check invariant */ - for (i = 0; i < niter - nrem; i ++) { - elt = rspamd_min_heap_pop (heap); + for (i = 0; i < niter - nrem; i++) { + elt = rspamd_min_heap_pop(heap); if (prev != 0) { - g_assert (elt->pri >= prev); + g_assert(elt->pri >= prev); } prev = elt->pri; } - rspamd_min_heap_destroy (heap); + rspamd_min_heap_destroy(heap); /* Complexity test (should be O(n * logn) */ - for (i = 1; i <= G_N_ELEMENTS (t); i ++) { - t[i - 1] = heap_nelts_test (0x1 << (i + 4)); + for (i = 1; i <= G_N_ELEMENTS(t); i++) { + t[i - 1] = heap_nelts_test(0x1 << (i + 4)); } - for (i = 1; i <= G_N_ELEMENTS (t); i ++) { - rspamd_printf ("Elements: %d, time: %.4f\n", 0x1 << (i + 4), t[i - 1]); + for (i = 1; i <= G_N_ELEMENTS(t); i++) { + rspamd_printf("Elements: %d, time: %.4f\n", 0x1 << (i + 4), t[i - 1]); } } diff --git a/test/rspamd_http_test.c b/test/rspamd_http_test.c index 1b271a7cc..3d6e3f4ee 100644 --- a/test/rspamd_http_test.c +++ b/test/rspamd_http_test.c @@ -36,80 +36,80 @@ static guint ntests = 3000; static guint nservers = 1; static void -rspamd_server_error (struct rspamd_http_connection_entry *conn_ent, - GError *err) +rspamd_server_error(struct rspamd_http_connection_entry *conn_ent, + GError *err) { - msg_err ("http error occurred: %s", err->message); - g_assert (0); + msg_err("http error occurred: %s", err->message); + g_assert(0); } static void -rspamd_server_finish (struct rspamd_http_connection_entry *conn_ent) +rspamd_server_finish(struct rspamd_http_connection_entry *conn_ent) { /* Do nothing here */ } static void -rspamd_server_accept (gint fd, short what, void *arg) +rspamd_server_accept(gint fd, short what, void *arg) { struct rspamd_http_connection_router *rt = arg; rspamd_inet_addr_t *addr = NULL; gint nfd; if ((nfd = - rspamd_accept_from_socket (fd, &addr, NULL)) == -1) { - msg_warn ("accept failed: %s", strerror (errno)); + rspamd_accept_from_socket(fd, &addr, NULL)) == -1) { + msg_warn("accept failed: %s", strerror(errno)); return; } /* Check for EAGAIN */ if (nfd == 0) { - rspamd_inet_address_free (addr); + rspamd_inet_address_free(addr); return; } - rspamd_inet_address_free (addr); - rspamd_http_router_handle_socket (rt, nfd, NULL); + rspamd_inet_address_free(addr); + rspamd_http_router_handle_socket(rt, nfd, NULL); } static void -rspamd_http_term_handler (gint fd, short what, void *arg) +rspamd_http_term_handler(gint fd, short what, void *arg) { struct ev_loop *ev_base = arg; struct timeval tv = {0, 0}; - event_base_loopexit (ev_base, &tv); + event_base_loopexit(ev_base, &tv); } static void -rspamd_http_server_func (gint fd, const gchar *path, rspamd_inet_addr_t *addr, - struct rspamd_cryptobox_keypair *kp, struct rspamd_keypair_cache *c) +rspamd_http_server_func(gint fd, const gchar *path, rspamd_inet_addr_t *addr, + struct rspamd_cryptobox_keypair *kp, struct rspamd_keypair_cache *c) { struct rspamd_http_connection_router *rt; - struct ev_loop *ev_base = event_init (); + struct ev_loop *ev_base = event_init(); struct event accept_ev, term_ev; - rt = rspamd_http_router_new (rspamd_server_error, rspamd_server_finish, - NULL, ev_base, path, c); - g_assert (rt != NULL); + rt = rspamd_http_router_new(rspamd_server_error, rspamd_server_finish, + NULL, ev_base, path, c); + g_assert(rt != NULL); - rspamd_http_router_set_key (rt, kp); - event_set (&accept_ev, fd, EV_READ | EV_PERSIST, rspamd_server_accept, rt); - event_base_set (ev_base, &accept_ev); - event_add (&accept_ev, NULL); + rspamd_http_router_set_key(rt, kp); + event_set(&accept_ev, fd, EV_READ | EV_PERSIST, rspamd_server_accept, rt); + event_base_set(ev_base, &accept_ev); + event_add(&accept_ev, NULL); - evsignal_set (&term_ev, SIGTERM, rspamd_http_term_handler, ev_base); - event_base_set (ev_base, &term_ev); - event_add (&term_ev, NULL); + evsignal_set(&term_ev, SIGTERM, rspamd_http_term_handler, ev_base); + event_base_set(ev_base, &term_ev); + event_add(&term_ev, NULL); - event_base_loop (ev_base, 0); + event_base_loop(ev_base, 0); } static gint -rspamd_client_body (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg, - const gchar *chunk, gsize len) +rspamd_client_body(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg, + const gchar *chunk, gsize len) { - g_assert (chunk[0] == '\0'); + g_assert(chunk[0] == '\0'); return 0; } @@ -120,36 +120,36 @@ struct client_cbdata { }; static void -rspamd_client_err (struct rspamd_http_connection *conn, GError *err) +rspamd_client_err(struct rspamd_http_connection *conn, GError *err) { - msg_info ("abnormally closing connection from: error: %s", - err->message); + msg_info("abnormally closing connection from: error: %s", + err->message); - g_assert (0); - close (conn->fd); - rspamd_http_connection_unref (conn); + g_assert(0); + close(conn->fd); + rspamd_http_connection_unref(conn); } static gint -rspamd_client_finish (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg) +rspamd_client_finish(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg) { struct client_cbdata *cb = conn->ud; - *(cb->lat) = rspamd_get_ticks (FALSE) * 1000. - cb->ts; - close (conn->fd); - rspamd_http_connection_unref (conn); - g_free (cb); + *(cb->lat) = rspamd_get_ticks(FALSE) * 1000. - cb->ts; + close(conn->fd); + rspamd_http_connection_unref(conn); + g_free(cb); return 0; } static void -rspamd_http_client_func (const gchar *path, rspamd_inet_addr_t *addr, - struct rspamd_cryptobox_keypair *kp, - struct rspamd_cryptobox_pubkey *peer_kp, - struct rspamd_keypair_cache *c, - struct ev_loop *ev_base, double *latency) +rspamd_http_client_func(const gchar *path, rspamd_inet_addr_t *addr, + struct rspamd_cryptobox_keypair *kp, + struct rspamd_cryptobox_pubkey *peer_kp, + struct rspamd_keypair_cache *c, + struct ev_loop *ev_base, double *latency) { struct rspamd_http_message *msg; struct rspamd_http_connection *conn; @@ -157,34 +157,34 @@ rspamd_http_client_func (const gchar *path, rspamd_inet_addr_t *addr, struct client_cbdata *cb; gint fd; - g_assert ((fd = rspamd_inet_address_connect (addr, SOCK_STREAM, TRUE)) != -1); - conn = rspamd_http_connection_new (rspamd_client_body, - rspamd_client_err, - rspamd_client_finish, - RSPAMD_HTTP_CLIENT_SIMPLE, - RSPAMD_HTTP_CLIENT, - c, - NULL); - rspamd_snprintf (urlbuf, sizeof (urlbuf), "http://127.0.0.1/%s", path); - msg = rspamd_http_message_from_url (urlbuf); + g_assert((fd = rspamd_inet_address_connect(addr, SOCK_STREAM, TRUE)) != -1); + conn = rspamd_http_connection_new(rspamd_client_body, + rspamd_client_err, + rspamd_client_finish, + RSPAMD_HTTP_CLIENT_SIMPLE, + RSPAMD_HTTP_CLIENT, + c, + NULL); + rspamd_snprintf(urlbuf, sizeof(urlbuf), "http://127.0.0.1/%s", path); + msg = rspamd_http_message_from_url(urlbuf); - g_assert (conn != NULL && msg != NULL); + g_assert(conn != NULL && msg != NULL); if (kp != NULL) { - g_assert (peer_kp != NULL); - rspamd_http_connection_set_key (conn, kp); - msg->peer_key = rspamd_pubkey_ref (peer_kp); + g_assert(peer_kp != NULL); + rspamd_http_connection_set_key(conn, kp); + msg->peer_key = rspamd_pubkey_ref(peer_kp); } - cb = g_malloc (sizeof (*cb)); - cb->ts = rspamd_get_ticks (FALSE) * 1000.; + cb = g_malloc(sizeof(*cb)); + cb->ts = rspamd_get_ticks(FALSE) * 1000.; cb->lat = latency; - rspamd_http_connection_write_message (conn, msg, NULL, NULL, cb, - fd, NULL, ev_base); + rspamd_http_connection_write_message(conn, msg, NULL, NULL, cb, + fd, NULL, ev_base); } static int -cmpd (const void *p1, const void *p2) +cmpd(const void *p1, const void *p2) { const double *d1 = p1, *d2 = p2; @@ -192,69 +192,68 @@ cmpd (const void *p1, const void *p2) } double -rspamd_http_calculate_mean (double *lats, double *std) +rspamd_http_calculate_mean(double *lats, double *std) { guint i; gdouble mean = 0., dev = 0.; - qsort (lats, ntests * pconns, sizeof (double), cmpd); + qsort(lats, ntests * pconns, sizeof(double), cmpd); - for (i = 0; i < ntests * pconns; i ++) { + for (i = 0; i < ntests * pconns; i++) { mean += lats[i]; } mean /= ntests * pconns; - for (i = 0; i < ntests * pconns; i ++) { + for (i = 0; i < ntests * pconns; i++) { dev += (lats[i] - mean) * (lats[i] - mean); } dev /= ntests * pconns; - *std = sqrt (dev); + *std = sqrt(dev); return mean; } static void -rspamd_http_start_servers (pid_t *sfd, rspamd_inet_addr_t *addr, - struct rspamd_cryptobox_keypair *serv_key, - struct rspamd_keypair_cache *c) +rspamd_http_start_servers(pid_t *sfd, rspamd_inet_addr_t *addr, + struct rspamd_cryptobox_keypair *serv_key, + struct rspamd_keypair_cache *c) { guint i; gint fd; - g_assert ((fd = rspamd_inet_address_listen (addr, SOCK_STREAM, TRUE)) != -1); + g_assert((fd = rspamd_inet_address_listen(addr, SOCK_STREAM, TRUE)) != -1); - for (i = 0; i < nservers; i ++) { - sfd[i] = fork (); - g_assert (sfd[i] != -1); + for (i = 0; i < nservers; i++) { + sfd[i] = fork(); + g_assert(sfd[i] != -1); if (sfd[i] == 0) { - rspamd_http_server_func (fd, "/tmp/", addr, serv_key, c); - exit (EXIT_SUCCESS); + rspamd_http_server_func(fd, "/tmp/", addr, serv_key, c); + exit(EXIT_SUCCESS); } } - close (fd); + close(fd); } static void -rspamd_http_stop_servers (pid_t *sfd) +rspamd_http_stop_servers(pid_t *sfd) { guint i; gint res; for (i = 0; i < nservers; i++) { - kill (sfd[i], SIGTERM); - wait (&res); + kill(sfd[i], SIGTERM); + wait(&res); } } -void -rspamd_http_test_func (void) +void rspamd_http_test_func(void) { - struct ev_loop *ev_base = event_init (); - rspamd_mempool_t *pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL); + struct ev_loop *ev_base = event_init(); + rspamd_mempool_t *pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), NULL); struct rspamd_cryptobox_keypair *serv_key, *client_key; struct rspamd_cryptobox_pubkey *peer_key; struct rspamd_keypair_cache *c; @@ -270,227 +269,227 @@ rspamd_http_test_func (void) double diff, total_diff = 0.0, *latency, mean, std; /* Read environment */ - if ((env = getenv ("RSPAMD_HTTP_CONNS")) != NULL) { - pconns = strtoul (env, NULL, 10); + if ((env = getenv("RSPAMD_HTTP_CONNS")) != NULL) { + pconns = strtoul(env, NULL, 10); } else { return; } - if ((env = getenv ("RSPAMD_HTTP_TESTS")) != NULL) { - ntests = strtoul (env, NULL, 10); + if ((env = getenv("RSPAMD_HTTP_TESTS")) != NULL) { + ntests = strtoul(env, NULL, 10); } - if ((env = getenv ("RSPAMD_HTTP_SIZE")) != NULL) { - file_size = strtoul (env, NULL, 10); + if ((env = getenv("RSPAMD_HTTP_SIZE")) != NULL) { + file_size = strtoul(env, NULL, 10); } - if ((env = getenv ("RSPAMD_HTTP_SERVERS")) != NULL) { - nservers = strtoul (env, NULL, 10); + if ((env = getenv("RSPAMD_HTTP_SERVERS")) != NULL) { + nservers = strtoul(env, NULL, 10); } - rspamd_cryptobox_init (); - rspamd_snprintf (filepath, sizeof (filepath), "/tmp/http-test-XXXXXX"); - g_assert ((fd = mkstemp (filepath)) != -1); + rspamd_cryptobox_init(); + rspamd_snprintf(filepath, sizeof(filepath), "/tmp/http-test-XXXXXX"); + g_assert((fd = mkstemp(filepath)) != -1); - sfd = g_alloca (sizeof (*sfd) * nservers); - latency = g_malloc0 (pconns * ntests * sizeof (gdouble)); + sfd = g_alloca(sizeof(*sfd) * nservers); + latency = g_malloc0(pconns * ntests * sizeof(gdouble)); - buf = g_malloc (file_size); - memset (buf, 0, file_size); - g_assert (write (fd, buf, file_size) == file_size); - g_free (buf); + buf = g_malloc(file_size); + memset(buf, 0, file_size); + g_assert(write(fd, buf, file_size) == file_size); + g_free(buf); - mtx = rspamd_mempool_get_mutex (pool); + mtx = rspamd_mempool_get_mutex(pool); - rspamd_parse_inet_address (&addr, "127.0.0.1", 0); - rspamd_inet_address_set_port (addr, 43898); - serv_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, - RSPAMD_CRYPTOBOX_MODE_25519); - client_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, - RSPAMD_CRYPTOBOX_MODE_25519); - c = rspamd_keypair_cache_new (16); + rspamd_parse_inet_address(&addr, "127.0.0.1", 0); + rspamd_inet_address_set_port(addr, 43898); + serv_key = rspamd_keypair_new(RSPAMD_KEYPAIR_KEX, + RSPAMD_CRYPTOBOX_MODE_25519); + client_key = rspamd_keypair_new(RSPAMD_KEYPAIR_KEX, + RSPAMD_CRYPTOBOX_MODE_25519); + c = rspamd_keypair_cache_new(16); - rspamd_http_start_servers (sfd, addr, serv_key, NULL); - usleep (100000); + rspamd_http_start_servers(sfd, addr, serv_key, NULL); + usleep(100000); /* Do client stuff */ - gperf_profiler_init (NULL, "plain-http-client"); - for (i = 0; i < ntests; i ++) { - for (j = 0; j < pconns; j ++) { - rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, addr, - NULL, NULL, c, ev_base, &latency[i * pconns + j]); + gperf_profiler_init(NULL, "plain-http-client"); + for (i = 0; i < ntests; i++) { + for (j = 0; j < pconns; j++) { + rspamd_http_client_func(filepath + sizeof("/tmp") - 1, addr, + NULL, NULL, c, ev_base, &latency[i * pconns + j]); } - ts1 = rspamd_get_ticks (FALSE); - event_base_loop (ev_base, 0); - ts2 = rspamd_get_ticks (FALSE); + ts1 = rspamd_get_ticks(FALSE); + event_base_loop(ev_base, 0); + ts2 = rspamd_get_ticks(FALSE); diff = (ts2 - ts1) * 1000.0; total_diff += diff; } - gperf_profiler_stop (); + gperf_profiler_stop(); - msg_info ("Made %d connections of size %d in %.6f ms, %.6f cps", - ntests * pconns, - file_size, - total_diff, ntests * pconns / total_diff * 1000.); - mean = rspamd_http_calculate_mean (latency, &std); - msg_info ("Latency: %.6f ms mean, %.6f dev", - mean, std); + msg_info("Made %d connections of size %d in %.6f ms, %.6f cps", + ntests * pconns, + file_size, + total_diff, ntests * pconns / total_diff * 1000.); + mean = rspamd_http_calculate_mean(latency, &std); + msg_info("Latency: %.6f ms mean, %.6f dev", + mean, std); - rspamd_http_stop_servers (sfd); + rspamd_http_stop_servers(sfd); - rspamd_http_start_servers (sfd, addr, serv_key, c); + rspamd_http_start_servers(sfd, addr, serv_key, c); //rspamd_mempool_lock_mutex (mtx); - usleep (100000); - b32_key = rspamd_keypair_print (serv_key, - RSPAMD_KEYPAIR_PUBKEY|RSPAMD_KEYPAIR_BASE32); - g_assert (b32_key != NULL); - peer_key = rspamd_pubkey_from_base32 (b32_key->str, b32_key->len, - RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_25519); - g_assert (peer_key != NULL); + usleep(100000); + b32_key = rspamd_keypair_print(serv_key, + RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32); + g_assert(b32_key != NULL); + peer_key = rspamd_pubkey_from_base32(b32_key->str, b32_key->len, + RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_25519); + g_assert(peer_key != NULL); total_diff = 0.0; - gperf_profiler_init (NULL, "cached-http-client"); - for (i = 0; i < ntests; i ++) { - for (j = 0; j < pconns; j ++) { - rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, addr, - client_key, peer_key, c, ev_base, &latency[i * pconns + j]); + gperf_profiler_init(NULL, "cached-http-client"); + for (i = 0; i < ntests; i++) { + for (j = 0; j < pconns; j++) { + rspamd_http_client_func(filepath + sizeof("/tmp") - 1, addr, + client_key, peer_key, c, ev_base, &latency[i * pconns + j]); } - ts1 = rspamd_get_ticks (FALSE); - event_base_loop (ev_base, 0); - ts2 = rspamd_get_ticks (FALSE); + ts1 = rspamd_get_ticks(FALSE); + event_base_loop(ev_base, 0); + ts2 = rspamd_get_ticks(FALSE); diff = (ts2 - ts1) * 1000.0; total_diff += diff; } - gperf_profiler_stop (); + gperf_profiler_stop(); - msg_info ("Made %d encrypted connections of size %d in %.6f ms, %.6f cps", - ntests * pconns, - file_size, - total_diff, ntests * pconns / total_diff * 1000.); - mean = rspamd_http_calculate_mean (latency, &std); - msg_info ("Latency: %.6f ms mean, %.6f dev", - mean, std); + msg_info("Made %d encrypted connections of size %d in %.6f ms, %.6f cps", + ntests * pconns, + file_size, + total_diff, ntests * pconns / total_diff * 1000.); + mean = rspamd_http_calculate_mean(latency, &std); + msg_info("Latency: %.6f ms mean, %.6f dev", + mean, std); /* Restart server */ - rspamd_http_stop_servers (sfd); + rspamd_http_stop_servers(sfd); /* No keypairs cache */ - rspamd_http_start_servers (sfd, addr, serv_key, NULL); + rspamd_http_start_servers(sfd, addr, serv_key, NULL); - usleep (100000); + usleep(100000); total_diff = 0.0; - gperf_profiler_init (NULL, "fair-http-client"); - for (i = 0; i < ntests; i ++) { - for (j = 0; j < pconns; j ++) { - rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, addr, - client_key, peer_key, c, ev_base, &latency[i * pconns + j]); + gperf_profiler_init(NULL, "fair-http-client"); + for (i = 0; i < ntests; i++) { + for (j = 0; j < pconns; j++) { + rspamd_http_client_func(filepath + sizeof("/tmp") - 1, addr, + client_key, peer_key, c, ev_base, &latency[i * pconns + j]); } - ts1 = rspamd_get_ticks (FALSE); - event_base_loop (ev_base, 0); - ts2 = rspamd_get_ticks (FALSE); + ts1 = rspamd_get_ticks(FALSE); + event_base_loop(ev_base, 0); + ts2 = rspamd_get_ticks(FALSE); diff = (ts2 - ts1) * 1000.0; total_diff += diff; } - gperf_profiler_stop (); + gperf_profiler_stop(); - msg_info ("Made %d uncached encrypted connections of size %d in %.6f ms, %.6f cps", - ntests * pconns, - file_size, - total_diff, ntests * pconns / total_diff * 1000.); - mean = rspamd_http_calculate_mean (latency, &std); - msg_info ("Latency: %.6f ms mean, %.6f dev", - mean, std); + msg_info("Made %d uncached encrypted connections of size %d in %.6f ms, %.6f cps", + ntests * pconns, + file_size, + total_diff, ntests * pconns / total_diff * 1000.); + mean = rspamd_http_calculate_mean(latency, &std); + msg_info("Latency: %.6f ms mean, %.6f dev", + mean, std); /* AES mode */ - serv_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, - RSPAMD_CRYPTOBOX_MODE_NIST); - client_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, - RSPAMD_CRYPTOBOX_MODE_NIST); - c = rspamd_keypair_cache_new (16); + serv_key = rspamd_keypair_new(RSPAMD_KEYPAIR_KEX, + RSPAMD_CRYPTOBOX_MODE_NIST); + client_key = rspamd_keypair_new(RSPAMD_KEYPAIR_KEX, + RSPAMD_CRYPTOBOX_MODE_NIST); + c = rspamd_keypair_cache_new(16); /* Restart server */ - rspamd_http_stop_servers (sfd); + rspamd_http_stop_servers(sfd); /* No keypairs cache */ - rspamd_http_start_servers (sfd, addr, serv_key, c); + rspamd_http_start_servers(sfd, addr, serv_key, c); //rspamd_mempool_lock_mutex (mtx); - usleep (100000); - b32_key = rspamd_keypair_print (serv_key, - RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32); - g_assert (b32_key != NULL); - peer_key = rspamd_pubkey_from_base32 (b32_key->str, b32_key->len, - RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_NIST); - g_assert (peer_key != NULL); + usleep(100000); + b32_key = rspamd_keypair_print(serv_key, + RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32); + g_assert(b32_key != NULL); + peer_key = rspamd_pubkey_from_base32(b32_key->str, b32_key->len, + RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_NIST); + g_assert(peer_key != NULL); total_diff = 0.0; - gperf_profiler_init (NULL, "cached-http-client-aes"); + gperf_profiler_init(NULL, "cached-http-client-aes"); for (i = 0; i < ntests; i++) { for (j = 0; j < pconns; j++) { - rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, - addr, - client_key, - peer_key, - NULL, - ev_base, - &latency[i * pconns + j]); + rspamd_http_client_func(filepath + sizeof("/tmp") - 1, + addr, + client_key, + peer_key, + NULL, + ev_base, + &latency[i * pconns + j]); } - ts1 = rspamd_get_ticks (FALSE); - event_base_loop (ev_base, 0); - ts2 = rspamd_get_ticks (FALSE); + ts1 = rspamd_get_ticks(FALSE); + event_base_loop(ev_base, 0); + ts2 = rspamd_get_ticks(FALSE); diff = (ts2 - ts1) * 1000.0; total_diff += diff; } - gperf_profiler_stop (); - - msg_info ( - "Made %d aes encrypted connections of size %d in %.6f ms, %.6f cps", - ntests * pconns, - file_size, - total_diff, - ntests * pconns / total_diff * 1000.); - mean = rspamd_http_calculate_mean (latency, &std); - msg_info ("Latency: %.6f ms mean, %.6f dev", - mean, std); + gperf_profiler_stop(); + + msg_info( + "Made %d aes encrypted connections of size %d in %.6f ms, %.6f cps", + ntests * pconns, + file_size, + total_diff, + ntests * pconns / total_diff * 1000.); + mean = rspamd_http_calculate_mean(latency, &std); + msg_info("Latency: %.6f ms mean, %.6f dev", + mean, std); /* Restart server */ - rspamd_http_stop_servers (sfd); + rspamd_http_stop_servers(sfd); /* No keypairs cache */ - rspamd_http_start_servers (sfd, addr, serv_key, NULL); + rspamd_http_start_servers(sfd, addr, serv_key, NULL); //rspamd_mempool_lock_mutex (mtx); - usleep (100000); + usleep(100000); total_diff = 0.0; - gperf_profiler_init (NULL, "fair-http-client-aes"); + gperf_profiler_init(NULL, "fair-http-client-aes"); for (i = 0; i < ntests; i++) { for (j = 0; j < pconns; j++) { - rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, - addr, - client_key, - peer_key, - c, - ev_base, - &latency[i * pconns + j]); + rspamd_http_client_func(filepath + sizeof("/tmp") - 1, + addr, + client_key, + peer_key, + c, + ev_base, + &latency[i * pconns + j]); } - ts1 = rspamd_get_ticks (FALSE); - event_base_loop (ev_base, 0); - ts2 = rspamd_get_ticks (FALSE); + ts1 = rspamd_get_ticks(FALSE); + event_base_loop(ev_base, 0); + ts2 = rspamd_get_ticks(FALSE); diff = (ts2 - ts1) * 1000.0; total_diff += diff; } - gperf_profiler_stop (); - - msg_info ( - "Made %d uncached aes encrypted connections of size %d in %.6f ms, %.6f cps", - ntests * pconns, - file_size, - total_diff, - ntests * pconns / total_diff * 1000.); - mean = rspamd_http_calculate_mean (latency, &std); - msg_info ("Latency: %.6f ms mean, %.6f dev", - mean, std); - - close (fd); - unlink (filepath); - rspamd_http_stop_servers (sfd); + gperf_profiler_stop(); + + msg_info( + "Made %d uncached aes encrypted connections of size %d in %.6f ms, %.6f cps", + ntests * pconns, + file_size, + total_diff, + ntests * pconns / total_diff * 1000.); + mean = rspamd_http_calculate_mean(latency, &std); + msg_info("Latency: %.6f ms mean, %.6f dev", + mean, std); + + close(fd); + unlink(filepath); + rspamd_http_stop_servers(sfd); } diff --git a/test/rspamd_lua_pcall_vs_resume_test.c b/test/rspamd_lua_pcall_vs_resume_test.c index c06283de1..0502378c9 100644 --- a/test/rspamd_lua_pcall_vs_resume_test.c +++ b/test/rspamd_lua_pcall_vs_resume_test.c @@ -32,15 +32,15 @@ test_pcall(lua_State *L, gint function_call) { gdouble t1, t2; gint i; - t1 = rspamd_get_virtual_ticks (); + t1 = rspamd_get_virtual_ticks(); - for (i = 0; i < N; i ++) { - lua_rawgeti (L, LUA_REGISTRYINDEX, function_call); - lua_pcall (L, 0, 1, 0); - lua_pop (L, 1); + for (i = 0; i < N; i++) { + lua_rawgeti(L, LUA_REGISTRYINDEX, function_call); + lua_pcall(L, 0, 1, 0); + lua_pop(L, 1); } - t2 = rspamd_get_virtual_ticks (); + t2 = rspamd_get_virtual_ticks(); return t2 - t1; } @@ -50,21 +50,21 @@ test_resume(lua_State *L, gint function_call) { gdouble t1, t2; gint i; - t1 = rspamd_get_virtual_ticks (); + t1 = rspamd_get_virtual_ticks(); - for (i = 0; i < N; i ++) { - lua_rawgeti (L, LUA_REGISTRYINDEX, function_call); + for (i = 0; i < N; i++) { + lua_rawgeti(L, LUA_REGISTRYINDEX, function_call); #if LUA_VERSION_NUM < 502 - lua_resume (L, 0); + lua_resume(L, 0); #elif LUA_VERSION_NUM >= 504 - lua_resume (L, NULL, 0, NULL); + lua_resume(L, NULL, 0, NULL); #else - lua_resume (L, NULL, 0); + lua_resume(L, NULL, 0); #endif - lua_pop (L, 1); + lua_pop(L, 1); } - t2 = rspamd_get_virtual_ticks (); + t2 = rspamd_get_virtual_ticks(); return t2 - t1; } @@ -76,25 +76,25 @@ test_resume_get_thread(gint function_call) gint i; struct thread_entry *ent; - t1 = rspamd_get_virtual_ticks (); + t1 = rspamd_get_virtual_ticks(); - for (i = 0; i < N; i ++) { - ent = lua_thread_pool_get_for_config (rspamd_main->cfg); + for (i = 0; i < N; i++) { + ent = lua_thread_pool_get_for_config(rspamd_main->cfg); - lua_rawgeti (ent->lua_state, LUA_REGISTRYINDEX, function_call); + lua_rawgeti(ent->lua_state, LUA_REGISTRYINDEX, function_call); #if LUA_VERSION_NUM < 502 - lua_resume (ent->lua_state, 0); + lua_resume(ent->lua_state, 0); #elif LUA_VERSION_NUM >= 504 - lua_resume (ent->lua_state, NULL, 0, NULL); + lua_resume(ent->lua_state, NULL, 0, NULL); #else - lua_resume (ent->lua_state, NULL, 0); + lua_resume(ent->lua_state, NULL, 0); #endif - lua_pop (ent->lua_state, 1); + lua_pop(ent->lua_state, 1); - lua_thread_pool_return (rspamd_main->cfg->lua_thread_pool, ent); + lua_thread_pool_return(rspamd_main->cfg->lua_thread_pool, ent); } - t2 = rspamd_get_virtual_ticks (); + t2 = rspamd_get_virtual_ticks(); return t2 - t1; } @@ -106,56 +106,55 @@ test_resume_get_new_thread(gint function_call) gint i; struct thread_entry *ent; - t1 = rspamd_get_virtual_ticks (); + t1 = rspamd_get_virtual_ticks(); - for (i = 0; i < N; i ++) { - ent = lua_thread_pool_get_for_task (rspamd_main->cfg->lua_thread_pool); + for (i = 0; i < N; i++) { + ent = lua_thread_pool_get_for_task(rspamd_main->cfg->lua_thread_pool); - lua_rawgeti (ent->lua_state, LUA_REGISTRYINDEX, function_call); + lua_rawgeti(ent->lua_state, LUA_REGISTRYINDEX, function_call); #if LUA_VERSION_NUM < 502 - lua_resume (ent->lua_state, 0); + lua_resume(ent->lua_state, 0); #elif LUA_VERSION_NUM >= 504 - lua_resume (ent->lua_state, NULL, 0, NULL); + lua_resume(ent->lua_state, NULL, 0, NULL); #else - lua_resume (ent->lua_state, NULL, 0); + lua_resume(ent->lua_state, NULL, 0); #endif - lua_pop (ent->lua_state, 1); + lua_pop(ent->lua_state, 1); /* lua_thread_pool_return (rspamd_main->cfg->lua_thread_pool, ent); */ } - t2 = rspamd_get_virtual_ticks (); + t2 = rspamd_get_virtual_ticks(); return t2 - t1; } -void -rspamd_lua_lua_pcall_vs_resume_test_func (void) +void rspamd_lua_lua_pcall_vs_resume_test_func(void) { lua_State *L = rspamd_main->cfg->lua_state; gchar *lua_src; gdouble t1, reference; - lua_src = g_build_filename (argv0_dirname, lua_src_name, NULL); - if (luaL_dofile (L, lua_src) != 0) { - msg_err ("failed to load test file: %s ", lua_tostring (L, -1)); - g_assert (0); + lua_src = g_build_filename(argv0_dirname, lua_src_name, NULL); + if (luaL_dofile(L, lua_src) != 0) { + msg_err("failed to load test file: %s ", lua_tostring(L, -1)); + g_assert(0); } - g_free (lua_src); + g_free(lua_src); - gint function_call = luaL_ref (L, LUA_REGISTRYINDEX); + gint function_call = luaL_ref(L, LUA_REGISTRYINDEX); - msg_info ("calling"); + msg_info("calling"); reference = t1 = test_pcall(L, function_call); - msg_notice ("pcall stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1/(gdouble)N, t1 / reference); + msg_notice("pcall stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1 / (gdouble) N, t1 / reference); - t1 = test_resume (L, function_call); - msg_notice ("resume stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1/(gdouble)N, t1 / reference); + t1 = test_resume(L, function_call); + msg_notice("resume stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1 / (gdouble) N, t1 / reference); - t1 = test_resume_get_thread (function_call); - msg_notice ("resume+get thread stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1/(gdouble)N, t1 / reference); + t1 = test_resume_get_thread(function_call); + msg_notice("resume+get thread stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1 / (gdouble) N, t1 / reference); - t1 = test_resume_get_new_thread (function_call); - msg_notice ("resume+get [new] thread stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1/(gdouble)N, t1 / reference); + t1 = test_resume_get_new_thread(function_call); + msg_notice("resume+get [new] thread stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1 / (gdouble) N, t1 / reference); } diff --git a/test/rspamd_lua_test.c b/test/rspamd_lua_test.c index cc1e96474..87cc9b7cf 100644 --- a/test/rspamd_lua_test.c +++ b/test/rspamd_lua_test.c @@ -30,122 +30,122 @@ extern gchar *argv0_dirname; extern struct rspamd_main *rspamd_main; static int -traceback (lua_State *L) +traceback(lua_State *L) { - if (!lua_isstring (L, 1)) { + if (!lua_isstring(L, 1)) { return 1; } - lua_getglobal (L, "debug"); + lua_getglobal(L, "debug"); if (!lua_istable(L, -1)) { lua_pop(L, 1); return 1; } - lua_getfield (L, -1, "traceback"); + lua_getfield(L, -1, "traceback"); if (!lua_isfunction(L, -1)) { lua_pop(L, 2); return 1; } - lua_pushvalue (L, 1); - lua_pushinteger (L, 2); + lua_pushvalue(L, 1); + lua_pushinteger(L, 2); lua_call(L, 2, 1); return 1; } _Noreturn void -rspamd_lua_test_func (void) +rspamd_lua_test_func(void) { - lua_State *L = (lua_State *)rspamd_main->cfg->lua_state; + lua_State *L = (lua_State *) rspamd_main->cfg->lua_state; gchar *lua_src, *rp, rp_buf[PATH_MAX], path_buf[PATH_MAX], *tmp, *dir, *pattern; const gchar *old_path; glob_t globbuf; gint i, len; - rspamd_lua_set_env (L, NULL, NULL, NULL); - rspamd_lua_set_globals (rspamd_main->cfg, L); - rspamd_lua_start_gc (rspamd_main->cfg); + rspamd_lua_set_env(L, NULL, NULL, NULL); + rspamd_lua_set_globals(rspamd_main->cfg, L); + rspamd_lua_start_gc(rspamd_main->cfg); if (lua_test_case) { - lua_pushstring (L, lua_test_case); - lua_setglobal (L, "test_pattern"); + lua_pushstring(L, lua_test_case); + lua_setglobal(L, "test_pattern"); } - rspamd_printf ("Starting lua tests\n"); + rspamd_printf("Starting lua tests\n"); - lua_src = g_build_filename (argv0_dirname, lua_src_name, NULL); - if ((rp = realpath (lua_src, rp_buf)) == NULL) { - msg_err ("cannot find path %s: %s", - lua_src, strerror (errno)); - g_assert (0); + lua_src = g_build_filename(argv0_dirname, lua_src_name, NULL); + if ((rp = realpath(lua_src, rp_buf)) == NULL) { + msg_err("cannot find path %s: %s", + lua_src, strerror(errno)); + g_assert(0); } - g_free (lua_src); + g_free(lua_src); - tmp = g_strdup (rp); - dir = dirname (tmp); + tmp = g_strdup(rp); + dir = dirname(tmp); /* Set lua path */ - lua_getglobal (L, "package"); - lua_getfield (L, -1, "path"); - old_path = luaL_checkstring (L, -1); + lua_getglobal(L, "package"); + lua_getfield(L, -1, "path"); + old_path = luaL_checkstring(L, -1); - rspamd_snprintf (path_buf, sizeof (path_buf), "%s;%s/?.lua;%s/unit/?.lua", - old_path, dir, dir); - lua_pop (L, 1); - lua_pushstring (L, path_buf); - lua_setfield (L, -2, "path"); - lua_pop (L, 1); + rspamd_snprintf(path_buf, sizeof(path_buf), "%s;%s/?.lua;%s/unit/?.lua", + old_path, dir, dir); + lua_pop(L, 1); + lua_pushstring(L, path_buf); + lua_setfield(L, -2, "path"); + lua_pop(L, 1); - lua_newtable (L); + lua_newtable(L); globbuf.gl_offs = 0; - len = strlen (dir) + sizeof ("/unit/") + sizeof ("*.lua"); - pattern = g_malloc (len); - rspamd_snprintf (pattern, len, "%s/unit/%s", dir, "*.lua"); + len = strlen(dir) + sizeof("/unit/") + sizeof("*.lua"); + pattern = g_malloc(len); + rspamd_snprintf(pattern, len, "%s/unit/%s", dir, "*.lua"); gint lua_test_len = 0; gint inserted_file = 1; gint path_start; if (lua_test) { - lua_test_len = strlen (lua_test); + lua_test_len = strlen(lua_test); } - if (glob (pattern, GLOB_DOOFFS, NULL, &globbuf) == 0) { - for (i = 0; i < (gint)globbuf.gl_pathc; i++) { + if (glob(pattern, GLOB_DOOFFS, NULL, &globbuf) == 0) { + for (i = 0; i < (gint) globbuf.gl_pathc; i++) { if (lua_test) { - path_start = strlen (globbuf.gl_pathv[i]) - lua_test_len; + path_start = strlen(globbuf.gl_pathv[i]) - lua_test_len; if (path_start < 0 || - strncmp (globbuf.gl_pathv[i] + path_start, lua_test, lua_test_len) != 0) { + strncmp(globbuf.gl_pathv[i] + path_start, lua_test, lua_test_len) != 0) { continue; } } - lua_pushinteger (L, inserted_file); - lua_pushstring (L, globbuf.gl_pathv[i]); - lua_settable (L, -3); + lua_pushinteger(L, inserted_file); + lua_pushstring(L, globbuf.gl_pathv[i]); + lua_settable(L, -3); - inserted_file ++; + inserted_file++; } - globfree (&globbuf); - g_free (pattern); + globfree(&globbuf); + g_free(pattern); } else { - msg_err ("pattern %s doesn't match: %s", pattern, - strerror (errno)); - g_assert (0); + msg_err("pattern %s doesn't match: %s", pattern, + strerror(errno)); + g_assert(0); } - lua_setglobal (L, "tests_list"); - rspamd_lua_set_path (L, NULL, NULL); + lua_setglobal(L, "tests_list"); + rspamd_lua_set_path(L, NULL, NULL); - lua_pushcfunction (L, traceback); - luaL_loadfile (L, rp); + lua_pushcfunction(L, traceback); + luaL_loadfile(L, rp); - if (lua_pcall (L, 0, 0, lua_gettop (L) - 1) != 0) { - msg_err ("run test failed: %s", lua_tostring (L, -1)); - g_assert (0); + if (lua_pcall(L, 0, 0, lua_gettop(L) - 1) != 0) { + msg_err("run test failed: %s", lua_tostring(L, -1)); + g_assert(0); } - exit (EXIT_SUCCESS); + exit(EXIT_SUCCESS); } diff --git a/test/rspamd_mem_pool_test.c b/test/rspamd_mem_pool_test.c index 156b913dc..fb3cf31e0 100644 --- a/test/rspamd_mem_pool_test.c +++ b/test/rspamd_mem_pool_test.c @@ -11,8 +11,7 @@ #define TEST_BUF "test buffer" #define TEST2_BUF "test buffertest buffer" -void -rspamd_mem_pool_test_func (void) +void rspamd_mem_pool_test_func(void) { rspamd_mempool_t *pool; rspamd_mempool_stat_t st; @@ -20,20 +19,19 @@ rspamd_mem_pool_test_func (void) pid_t pid; int ret; - pool = rspamd_mempool_new (sizeof (TEST_BUF), NULL, 0); - tmp = rspamd_mempool_alloc (pool, sizeof (TEST_BUF)); - tmp2 = rspamd_mempool_alloc (pool, sizeof (TEST_BUF) * 2); - tmp3 = rspamd_mempool_alloc_shared (pool, sizeof (TEST_BUF)); + pool = rspamd_mempool_new(sizeof(TEST_BUF), NULL, 0); + tmp = rspamd_mempool_alloc(pool, sizeof(TEST_BUF)); + tmp2 = rspamd_mempool_alloc(pool, sizeof(TEST_BUF) * 2); + tmp3 = rspamd_mempool_alloc_shared(pool, sizeof(TEST_BUF)); - snprintf (tmp, sizeof (TEST_BUF), "%s", TEST_BUF); - snprintf (tmp2, sizeof (TEST_BUF) * 2, "%s", TEST2_BUF); - snprintf (tmp3, sizeof (TEST_BUF), "%s", TEST_BUF); + snprintf(tmp, sizeof(TEST_BUF), "%s", TEST_BUF); + snprintf(tmp2, sizeof(TEST_BUF) * 2, "%s", TEST2_BUF); + snprintf(tmp3, sizeof(TEST_BUF), "%s", TEST_BUF); - g_assert (strncmp (tmp, TEST_BUF, sizeof (TEST_BUF)) == 0); - g_assert (strncmp (tmp2, TEST2_BUF, sizeof (TEST2_BUF)) == 0); - g_assert (strncmp (tmp3, TEST_BUF, sizeof (TEST_BUF)) == 0); - - rspamd_mempool_delete (pool); - rspamd_mempool_stat (&st); + g_assert(strncmp(tmp, TEST_BUF, sizeof(TEST_BUF)) == 0); + g_assert(strncmp(tmp2, TEST2_BUF, sizeof(TEST2_BUF)) == 0); + g_assert(strncmp(tmp3, TEST_BUF, sizeof(TEST_BUF)) == 0); + rspamd_mempool_delete(pool); + rspamd_mempool_stat(&st); } diff --git a/test/rspamd_radix_test.c b/test/rspamd_radix_test.c index 9a637344c..426eb1f54 100644 --- a/test/rspamd_radix_test.c +++ b/test/rspamd_radix_test.c @@ -24,16 +24,15 @@ const gint lookup_cycles = 1 * 1024; const gint lookup_divisor = 10; const uint masks[] = { - 8, - 16, - 24, - 32, - 27, - 29, - 19, - 13, - 22 -}; + 8, + 16, + 24, + 32, + 27, + 29, + 19, + 13, + 22}; struct _tv { const char *ip; @@ -67,78 +66,77 @@ struct _tv { {"130.244.233.150", NULL, "0", 0, 0, 0, 0}, /* Close ip addresses */ - {"1.2.3.1", NULL, "32", 0, 0, 0, 0}, - {"1.2.3.2", NULL, "32", 0, 0, 0, 0}, - {"1.2.3.3", NULL, "32", 0, 0, 0, 0}, + {"1.2.3.1", NULL, "32", 0, 0, 0, 0}, + {"1.2.3.2", NULL, "32", 0, 0, 0, 0}, + {"1.2.3.3", NULL, "32", 0, 0, 0, 0}, {"1.2.3.4", NULL, "32", 0, 0, 0, 0}, - {NULL, NULL, NULL, 0, 0, 0, 0} -}; + {NULL, NULL, NULL, 0, 0, 0, 0}}; static void -rspamd_radix_test_vec (void) +rspamd_radix_test_vec(void) { - radix_compressed_t *tree = radix_create_compressed (NULL); + radix_compressed_t *tree = radix_create_compressed(NULL); struct _tv *t = &test_vec[0]; struct in_addr ina; struct in6_addr in6a; gulong i, val; while (t->ip != NULL) { - t->addr = g_malloc (sizeof (in6a)); - t->naddr = g_malloc (sizeof (in6a)); - if (inet_pton (AF_INET, t->ip, &ina) == 1) { - memcpy (t->addr, &ina, sizeof (ina)); - t->len = sizeof (ina); + t->addr = g_malloc(sizeof(in6a)); + t->naddr = g_malloc(sizeof(in6a)); + if (inet_pton(AF_INET, t->ip, &ina) == 1) { + memcpy(t->addr, &ina, sizeof(ina)); + t->len = sizeof(ina); } - else if (inet_pton (AF_INET6, t->ip, &in6a) == 1) { - memcpy (t->addr, &in6a, sizeof (in6a)); - t->len = sizeof (in6a); + else if (inet_pton(AF_INET6, t->ip, &in6a) == 1) { + memcpy(t->addr, &in6a, sizeof(in6a)); + t->len = sizeof(in6a); } else { - g_assert (0); + g_assert(0); } if (t->nip) { - if (inet_pton (AF_INET, t->nip, &ina) == 1) { - memcpy (t->naddr, &ina, sizeof (ina)); + if (inet_pton(AF_INET, t->nip, &ina) == 1) { + memcpy(t->naddr, &ina, sizeof(ina)); } - else if (inet_pton (AF_INET6, t->nip, &in6a) == 1) { - memcpy (t->naddr, &in6a, sizeof (in6a)); + else if (inet_pton(AF_INET6, t->nip, &in6a) == 1) { + memcpy(t->naddr, &in6a, sizeof(in6a)); } else { - g_assert (0); + g_assert(0); } } - t->mask = t->len * NBBY - strtoul (t->m, NULL, 10); - t ++; + t->mask = t->len * NBBY - strtoul(t->m, NULL, 10); + t++; } t = &test_vec[0]; i = 0; while (t->ip != NULL) { - radix_insert_compressed (tree, t->addr, t->len, t->mask, ++i); - t ++; + radix_insert_compressed(tree, t->addr, t->len, t->mask, ++i); + t++; } i = 0; t = &test_vec[0]; while (t->ip != NULL) { - val = radix_find_compressed (tree, t->addr, t->len); - g_assert (val == ++i); + val = radix_find_compressed(tree, t->addr, t->len); + g_assert(val == ++i); /* g_assert (val != RADIX_NO_VALUE); */ if (t->nip != NULL) { - val = radix_find_compressed (tree, t->naddr, t->len); - g_assert (val != i); + val = radix_find_compressed(tree, t->naddr, t->len); + g_assert(val != i); } - t ++; + t++; } - radix_destroy_compressed (tree); + radix_destroy_compressed(tree); } static void -rspamd_btrie_test_vec (void) +rspamd_btrie_test_vec(void) { rspamd_mempool_t *pool; struct btrie *tree; @@ -148,68 +146,67 @@ rspamd_btrie_test_vec (void) gsize i; gpointer val; - pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), "btrie", 0); - tree = btrie_init (pool); + pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), "btrie", 0); + tree = btrie_init(pool); while (t->ip != NULL) { - t->addr = g_malloc (sizeof (in6a)); - t->naddr = g_malloc (sizeof (in6a)); - if (inet_pton (AF_INET, t->ip, &ina) == 1) { - memcpy (t->addr, &ina, sizeof (ina)); - t->len = sizeof (ina); + t->addr = g_malloc(sizeof(in6a)); + t->naddr = g_malloc(sizeof(in6a)); + if (inet_pton(AF_INET, t->ip, &ina) == 1) { + memcpy(t->addr, &ina, sizeof(ina)); + t->len = sizeof(ina); } - else if (inet_pton (AF_INET6, t->ip, &in6a) == 1) { - memcpy (t->addr, &in6a, sizeof (in6a)); - t->len = sizeof (in6a); + else if (inet_pton(AF_INET6, t->ip, &in6a) == 1) { + memcpy(t->addr, &in6a, sizeof(in6a)); + t->len = sizeof(in6a); } else { - g_assert (0); + g_assert(0); } if (t->nip) { - if (inet_pton (AF_INET, t->nip, &ina) == 1) { - memcpy (t->naddr, &ina, sizeof (ina)); + if (inet_pton(AF_INET, t->nip, &ina) == 1) { + memcpy(t->naddr, &ina, sizeof(ina)); } - else if (inet_pton (AF_INET6, t->nip, &in6a) == 1) { - memcpy (t->naddr, &in6a, sizeof (in6a)); + else if (inet_pton(AF_INET6, t->nip, &in6a) == 1) { + memcpy(t->naddr, &in6a, sizeof(in6a)); } else { - g_assert (0); + g_assert(0); } } - t->mask = strtoul (t->m, NULL, 10); - t ++; + t->mask = strtoul(t->m, NULL, 10); + t++; } t = &test_vec[0]; i = 0; while (t->ip != NULL) { - g_assert (btrie_add_prefix (tree, t->addr, t->mask, - GSIZE_TO_POINTER (++i)) == BTRIE_OKAY); - t ++; + g_assert(btrie_add_prefix(tree, t->addr, t->mask, + GSIZE_TO_POINTER(++i)) == BTRIE_OKAY); + t++; } i = 0; t = &test_vec[0]; while (t->ip != NULL) { - val = btrie_lookup (tree, t->addr, t->len * NBBY); - i ++; + val = btrie_lookup(tree, t->addr, t->len * NBBY); + i++; - g_assert (GPOINTER_TO_SIZE (val) == i); + g_assert(GPOINTER_TO_SIZE(val) == i); if (t->nip != NULL) { - val = btrie_lookup (tree, t->naddr, t->len * NBBY); - g_assert (GPOINTER_TO_SIZE (val) != i); + val = btrie_lookup(tree, t->naddr, t->len * NBBY); + g_assert(GPOINTER_TO_SIZE(val) != i); } - t ++; + t++; } } -void -rspamd_radix_test_func (void) +void rspamd_radix_test_func(void) { struct btrie *btrie; rspamd_mempool_t *pool; - radix_compressed_t *comp_tree = radix_create_compressed (NULL); + radix_compressed_t *comp_tree = radix_create_compressed(NULL); struct { guint32 addr; guint32 mask; @@ -225,158 +222,156 @@ rspamd_radix_test_func (void) /* Test suite for the compressed trie */ - rspamd_btrie_test_vec (); - rspamd_radix_test_vec (); - rspamd_random_seed_fast (); + rspamd_btrie_test_vec(); + rspamd_radix_test_vec(); + rspamd_random_seed_fast(); nelts = max_elts; /* First of all we generate many elements and push them to the array */ - addrs = g_malloc (nelts * sizeof (addrs[0])); - - for (i = 0; i < nelts; i ++) { - addrs[i].addr = ottery_rand_uint32 (); - memset (addrs[i].addr64, 0, 10); - memcpy (addrs[i].addr64 + 12, &addrs[i].addr, 4); - addrs[i].mask = masks[ottery_rand_range(G_N_ELEMENTS (masks) - 1)]; - ottery_rand_bytes (addrs[i].addr6, sizeof(addrs[i].addr6)); + addrs = g_malloc(nelts * sizeof(addrs[0])); + + for (i = 0; i < nelts; i++) { + addrs[i].addr = ottery_rand_uint32(); + memset(addrs[i].addr64, 0, 10); + memcpy(addrs[i].addr64 + 12, &addrs[i].addr, 4); + addrs[i].mask = masks[ottery_rand_range(G_N_ELEMENTS(masks) - 1)]; + ottery_rand_bytes(addrs[i].addr6, sizeof(addrs[i].addr6)); addrs[i].mask6 = ottery_rand_range(128 - 16) + 16; } - pool = rspamd_mempool_new (65536, "btrie6", 0); - btrie = btrie_init (pool); - msg_notice ("btrie performance ipv6 only (%z elts)", nelts); + pool = rspamd_mempool_new(65536, "btrie6", 0); + btrie = btrie_init(pool); + msg_notice("btrie performance ipv6 only (%z elts)", nelts); - ts1 = rspamd_get_ticks (TRUE); - for (i = 0; i < nelts; i ++) { - btrie_add_prefix (btrie, addrs[i].addr6, - addrs[i].mask6, GSIZE_TO_POINTER (i + 1)); + ts1 = rspamd_get_ticks(TRUE); + for (i = 0; i < nelts; i++) { + btrie_add_prefix(btrie, addrs[i].addr6, + addrs[i].mask6, GSIZE_TO_POINTER(i + 1)); } - ts2 = rspamd_get_ticks (TRUE); + ts2 = rspamd_get_ticks(TRUE); diff = (ts2 - ts1); - msg_notice ("Added %hz elements in %.0f ticks (%.2f ticks per element)", - nelts, diff, diff / (double)nelts); + msg_notice("Added %hz elements in %.0f ticks (%.2f ticks per element)", + nelts, diff, diff / (double) nelts); - ts1 = rspamd_get_ticks (TRUE); - for (lc = 0; lc < lookup_cycles && all_good; lc ++) { - for (i = 0; i < nelts / lookup_divisor; i ++) { - check = rspamd_random_uint64_fast () % nelts; + ts1 = rspamd_get_ticks(TRUE); + for (lc = 0; lc < lookup_cycles && all_good; lc++) { + for (i = 0; i < nelts / lookup_divisor; i++) { + check = rspamd_random_uint64_fast() % nelts; - if (btrie_lookup (btrie, addrs[check].addr6, sizeof (addrs[check].addr6) * 8) - == NULL) { + if (btrie_lookup(btrie, addrs[check].addr6, sizeof(addrs[check].addr6) * 8) == NULL) { char ipbuf[INET6_ADDRSTRLEN + 1]; all_good = FALSE; inet_ntop(AF_INET6, addrs[check].addr6, ipbuf, sizeof(ipbuf)); msg_notice("BAD btrie: {\"%s\", NULL, \"%ud\", 0, 0, 0, 0},", - ipbuf, - addrs[check].mask6); + ipbuf, + addrs[check].mask6); all_good = FALSE; } } } - g_assert (all_good); - ts2 = rspamd_get_ticks (TRUE); + g_assert(all_good); + ts2 = rspamd_get_ticks(TRUE); diff = (ts2 - ts1); - msg_notice ("Checked %hz elements in %.0f ticks (%.2f ticks per lookup)", - nelts * lookup_cycles / lookup_divisor, diff, - diff / ((gdouble)nelts * lookup_cycles / lookup_divisor)); - rspamd_mempool_delete (pool); + msg_notice("Checked %hz elements in %.0f ticks (%.2f ticks per lookup)", + nelts * lookup_cycles / lookup_divisor, diff, + diff / ((gdouble) nelts * lookup_cycles / lookup_divisor)); + rspamd_mempool_delete(pool); /* * IPv4 part */ - pool = rspamd_mempool_new (65536, "btrie4", 0); - btrie = btrie_init (pool); - msg_notice ("btrie performance ipv4 only (%z elts)", nelts); - - ts1 = rspamd_get_ticks (TRUE); - for (i = 0; i < nelts; i ++) { - btrie_add_prefix (btrie, (guchar *)&addrs[i].addr, - addrs[i].mask, GSIZE_TO_POINTER (i + 1)); + pool = rspamd_mempool_new(65536, "btrie4", 0); + btrie = btrie_init(pool); + msg_notice("btrie performance ipv4 only (%z elts)", nelts); + + ts1 = rspamd_get_ticks(TRUE); + for (i = 0; i < nelts; i++) { + btrie_add_prefix(btrie, (guchar *) &addrs[i].addr, + addrs[i].mask, GSIZE_TO_POINTER(i + 1)); } - ts2 = rspamd_get_ticks (TRUE); + ts2 = rspamd_get_ticks(TRUE); diff = (ts2 - ts1); - msg_notice ("Added %hz elements in %.0f ticks (%.2f ticks per element)", - nelts, diff, diff / (double)nelts); + msg_notice("Added %hz elements in %.0f ticks (%.2f ticks per element)", + nelts, diff, diff / (double) nelts); - ts1 = rspamd_get_ticks (TRUE); - for (lc = 0; lc < lookup_cycles && all_good; lc ++) { - for (i = 0; i < nelts / lookup_divisor; i ++) { - check = rspamd_random_uint64_fast () % nelts; + ts1 = rspamd_get_ticks(TRUE); + for (lc = 0; lc < lookup_cycles && all_good; lc++) { + for (i = 0; i < nelts / lookup_divisor; i++) { + check = rspamd_random_uint64_fast() % nelts; - if (btrie_lookup (btrie, (guchar *)&addrs[check].addr, sizeof (addrs[check].addr) * 8) - == NULL) { + if (btrie_lookup(btrie, (guchar *) &addrs[check].addr, sizeof(addrs[check].addr) * 8) == NULL) { char ipbuf[INET6_ADDRSTRLEN + 1]; all_good = FALSE; - inet_ntop(AF_INET, (guchar *)&addrs[check].addr, ipbuf, sizeof(ipbuf)); + inet_ntop(AF_INET, (guchar *) &addrs[check].addr, ipbuf, sizeof(ipbuf)); msg_notice("BAD btrie: {\"%s\", NULL, \"%ud\", 0, 0, 0, 0},", - ipbuf, - addrs[check].mask); + ipbuf, + addrs[check].mask); all_good = FALSE; } } } - g_assert (all_good); - ts2 = rspamd_get_ticks (TRUE); + g_assert(all_good); + ts2 = rspamd_get_ticks(TRUE); diff = (ts2 - ts1); - msg_notice ("Checked %hz elements in %.0f ticks (%.2f ticks per lookup)", - nelts * lookup_cycles / lookup_divisor, diff, - diff / ((gdouble)nelts * lookup_cycles / lookup_divisor)); - rspamd_mempool_delete (pool); + msg_notice("Checked %hz elements in %.0f ticks (%.2f ticks per lookup)", + nelts * lookup_cycles / lookup_divisor, diff, + diff / ((gdouble) nelts * lookup_cycles / lookup_divisor)); + rspamd_mempool_delete(pool); /* * IPv4 -> IPv6 mapped */ - pool = rspamd_mempool_new (65536, "btrie4map", 0); - btrie = btrie_init (pool); - msg_notice ("btrie performance ipv4 + ipv6map (%z elts)", nelts); + pool = rspamd_mempool_new(65536, "btrie4map", 0); + btrie = btrie_init(pool); + msg_notice("btrie performance ipv4 + ipv6map (%z elts)", nelts); - ts1 = rspamd_get_ticks (TRUE); - for (i = 0; i < nelts; i ++) { + ts1 = rspamd_get_ticks(TRUE); + for (i = 0; i < nelts; i++) { - btrie_add_prefix (btrie, addrs[i].addr64, - addrs[i].mask + 96, GSIZE_TO_POINTER (i + 1)); + btrie_add_prefix(btrie, addrs[i].addr64, + addrs[i].mask + 96, GSIZE_TO_POINTER(i + 1)); } - ts2 = rspamd_get_ticks (TRUE); + ts2 = rspamd_get_ticks(TRUE); diff = (ts2 - ts1); - msg_notice ("Added %hz elements in %.0f ticks (%.2f ticks per element)", - nelts, diff, diff / (double)nelts); + msg_notice("Added %hz elements in %.0f ticks (%.2f ticks per element)", + nelts, diff, diff / (double) nelts); - ts1 = rspamd_get_ticks (TRUE); - for (lc = 0; lc < lookup_cycles && all_good; lc ++) { - for (i = 0; i < nelts / lookup_divisor; i ++) { - check = rspamd_random_uint64_fast () % nelts; + ts1 = rspamd_get_ticks(TRUE); + for (lc = 0; lc < lookup_cycles && all_good; lc++) { + for (i = 0; i < nelts / lookup_divisor; i++) { + check = rspamd_random_uint64_fast() % nelts; - if (btrie_lookup (btrie, addrs[check].addr64, - sizeof (addrs[check].addr64) * 8) == NULL) { + if (btrie_lookup(btrie, addrs[check].addr64, + sizeof(addrs[check].addr64) * 8) == NULL) { char ipbuf[INET6_ADDRSTRLEN + 1]; all_good = FALSE; - inet_ntop(AF_INET, (guchar *)&addrs[check].addr, ipbuf, sizeof(ipbuf)); + inet_ntop(AF_INET, (guchar *) &addrs[check].addr, ipbuf, sizeof(ipbuf)); msg_notice("BAD btrie: {\"%s\", NULL, \"%ud\", 0, 0, 0, 0},", - ipbuf, - addrs[check].mask); + ipbuf, + addrs[check].mask); all_good = FALSE; } } } - g_assert (all_good); - ts2 = rspamd_get_ticks (TRUE); + g_assert(all_good); + ts2 = rspamd_get_ticks(TRUE); diff = (ts2 - ts1); - msg_notice ("Checked %hz elements in %.0f ticks (%.2f ticks per lookup)", - nelts * lookup_cycles / lookup_divisor, diff, - diff / ((gdouble)nelts * lookup_cycles / lookup_divisor)); - rspamd_mempool_delete (pool); + msg_notice("Checked %hz elements in %.0f ticks (%.2f ticks per lookup)", + nelts * lookup_cycles / lookup_divisor, diff, + diff / ((gdouble) nelts * lookup_cycles / lookup_divisor)); + rspamd_mempool_delete(pool); - g_free (addrs); + g_free(addrs); } diff --git a/test/rspamd_rrd_test.c b/test/rspamd_rrd_test.c index 0fc16edea..00d671192 100644 --- a/test/rspamd_rrd_test.c +++ b/test/rspamd_rrd_test.c @@ -23,10 +23,9 @@ const int rows_cnt = 20; const int pdp_per_cdp = 60; -void -rspamd_rrd_test_func (void) +void rspamd_rrd_test_func(void) { - gchar tmpfile[PATH_MAX]; + gchar tmpfile[PATH_MAX]; struct rrd_rra_def rra[4]; struct rrd_ds_def ds[2]; GArray ar; @@ -36,70 +35,67 @@ rspamd_rrd_test_func (void) gint i; gdouble t[2], cnt = 0.0; - rspamd_snprintf (tmpfile, sizeof (tmpfile), "/tmp/rspamd_rrd.rrd"); - unlink (tmpfile); + rspamd_snprintf(tmpfile, sizeof(tmpfile), "/tmp/rspamd_rrd.rrd"); + unlink(tmpfile); /* Create sample rrd */ - ticks = rspamd_get_calendar_ticks (); - g_assert ((rrd = rspamd_rrd_create (tmpfile, 2, 4, 1, ticks, &err)) != NULL); + ticks = rspamd_get_calendar_ticks(); + g_assert((rrd = rspamd_rrd_create(tmpfile, 2, 4, 1, ticks, &err)) != NULL); /* Add RRA */ - rrd_make_default_rra ("AVERAGE", pdp_per_cdp, rows_cnt, &rra[0]); - rrd_make_default_rra ("AVERAGE", pdp_per_cdp / 2, rows_cnt, &rra[1]); - rrd_make_default_rra ("AVERAGE", pdp_per_cdp / 4, rows_cnt, &rra[2]); - rrd_make_default_rra ("AVERAGE", pdp_per_cdp / 10, rows_cnt, &rra[3]); + rrd_make_default_rra("AVERAGE", pdp_per_cdp, rows_cnt, &rra[0]); + rrd_make_default_rra("AVERAGE", pdp_per_cdp / 2, rows_cnt, &rra[1]); + rrd_make_default_rra("AVERAGE", pdp_per_cdp / 4, rows_cnt, &rra[2]); + rrd_make_default_rra("AVERAGE", pdp_per_cdp / 10, rows_cnt, &rra[3]); ar.data = rra; - ar.len = sizeof (rra); - g_assert (rspamd_rrd_add_rra (rrd, &ar, &err)); + ar.len = sizeof(rra); + g_assert(rspamd_rrd_add_rra(rrd, &ar, &err)); /* Add DS */ - rrd_make_default_ds ("test", "COUNTER", 1, &ds[0]); - rrd_make_default_ds ("test1", "COUNTER", 1, &ds[1]); + rrd_make_default_ds("test", "COUNTER", 1, &ds[0]); + rrd_make_default_ds("test1", "COUNTER", 1, &ds[1]); ar.data = ds; - ar.len = sizeof (ds); - g_assert (rspamd_rrd_add_ds (rrd, &ar, &err)); + ar.len = sizeof(ds); + g_assert(rspamd_rrd_add_ds(rrd, &ar, &err)); /* Finalize */ - g_assert (rspamd_rrd_finalize (rrd, &err)); + g_assert(rspamd_rrd_finalize(rrd, &err)); /* Close */ - rspamd_rrd_close (rrd); + rspamd_rrd_close(rrd); /* Reopen */ - g_assert ((rrd = rspamd_rrd_open (tmpfile, &err)) != NULL); + g_assert((rrd = rspamd_rrd_open(tmpfile, &err)) != NULL); /* Add some points */ - for (i = 0; i < pdp_per_cdp * rows_cnt / 2; i ++) { + for (i = 0; i < pdp_per_cdp * rows_cnt / 2; i++) { t[0] = i; - t[1] = cnt ++; + t[1] = cnt++; ar.data = t; - ar.len = sizeof (t); + ar.len = sizeof(t); ticks += 1.0; - g_assert (rspamd_rrd_add_record (rrd, &ar, ticks, &err)); - + g_assert(rspamd_rrd_add_record(rrd, &ar, ticks, &err)); } /* Add some more points */ - for (i = 0; i < pdp_per_cdp * rows_cnt / 4; i ++) { - t[0] = i + rspamd_time_jitter (1.0, 0.0); - t[1] = cnt ++; + for (i = 0; i < pdp_per_cdp * rows_cnt / 4; i++) { + t[0] = i + rspamd_time_jitter(1.0, 0.0); + t[1] = cnt++; ar.data = t; - ar.len = sizeof (t); + ar.len = sizeof(t); ticks += 1.0; - g_assert (rspamd_rrd_add_record (rrd, &ar, ticks, &err)); - + g_assert(rspamd_rrd_add_record(rrd, &ar, ticks, &err)); } /* Add undefined interval */ ticks += 200; /* Add some more points */ - for (i = 0; i < pdp_per_cdp * rows_cnt / 8; i ++) { + for (i = 0; i < pdp_per_cdp * rows_cnt / 8; i++) { t[0] = i; - t[1] = cnt ++; + t[1] = cnt++; ar.data = t; - ar.len = sizeof (t); + ar.len = sizeof(t); ticks += 1.0; - g_assert (rspamd_rrd_add_record (rrd, &ar, ticks, &err)); - + g_assert(rspamd_rrd_add_record(rrd, &ar, ticks, &err)); } /* Finish */ - rspamd_rrd_close (rrd); + rspamd_rrd_close(rrd); /* unlink (tmpfile); */ } diff --git a/test/rspamd_shingles_test.c b/test/rspamd_shingles_test.c index e1367cca4..307634e30 100644 --- a/test/rspamd_shingles_test.c +++ b/test/rspamd_shingles_test.c @@ -20,7 +20,7 @@ #include <math.h> static const gchar * -algorithm_to_string (enum rspamd_shingle_alg alg) +algorithm_to_string(enum rspamd_shingle_alg alg) { const gchar *ret = "unknown"; @@ -43,70 +43,70 @@ algorithm_to_string (enum rspamd_shingle_alg alg) } static void -generate_random_string (char *begin, size_t len) +generate_random_string(char *begin, size_t len) { gsize i; - for (i = 0; i < len; i ++) { - begin[i] = ottery_rand_range ('z' - 'a') + 'a'; + for (i = 0; i < len; i++) { + begin[i] = ottery_rand_range('z' - 'a') + 'a'; } } static GArray * -generate_fuzzy_words (gsize cnt, gsize max_len) +generate_fuzzy_words(gsize cnt, gsize max_len) { GArray *res; gsize i, wlen; rspamd_ftok_t w; char *t; - res = g_array_sized_new (FALSE, FALSE, sizeof (rspamd_ftok_t), cnt); + res = g_array_sized_new(FALSE, FALSE, sizeof(rspamd_ftok_t), cnt); - for (i = 0; i < cnt; i ++) { - wlen = ottery_rand_range (max_len) + 1; + for (i = 0; i < cnt; i++) { + wlen = ottery_rand_range(max_len) + 1; /* wlen = max_len; */ w.len = wlen; - t = g_malloc (wlen); - generate_random_string (t, wlen); + t = g_malloc(wlen); + generate_random_string(t, wlen); w.begin = t; - g_array_append_val (res, w); + g_array_append_val(res, w); } return res; } static void -permute_vector (GArray *in, gdouble prob) +permute_vector(GArray *in, gdouble prob) { gsize i, total = 0; rspamd_ftok_t *w; - for (i = 0; i < in->len; i ++) { - if (ottery_rand_unsigned () <= G_MAXUINT * prob) { - w = &g_array_index (in, rspamd_ftok_t, i); - generate_random_string ((gchar *)w->begin, w->len); - total ++; + for (i = 0; i < in->len; i++) { + if (ottery_rand_unsigned() <= G_MAXUINT * prob) { + w = &g_array_index(in, rspamd_ftok_t, i); + generate_random_string((gchar *) w->begin, w->len); + total++; } } - msg_debug ("generated %z permutations of %ud words", total, in->len); + msg_debug("generated %z permutations of %ud words", total, in->len); } static void -free_fuzzy_words (GArray *ar) +free_fuzzy_words(GArray *ar) { gsize i; rspamd_ftok_t *w; - for (i = 0; i < ar->len; i ++) { - w = &g_array_index (ar, rspamd_ftok_t, i); - g_free ((gpointer)w->begin); + for (i = 0; i < ar->len; i++) { + w = &g_array_index(ar, rspamd_ftok_t, i); + g_free((gpointer) w->begin); } } static void -test_case (gsize cnt, gsize max_len, gdouble perm_factor, - enum rspamd_shingle_alg alg) +test_case(gsize cnt, gsize max_len, gdouble perm_factor, + enum rspamd_shingle_alg alg) { GArray *input; struct rspamd_shingle *sgl, *sgl_permuted; @@ -114,74 +114,169 @@ test_case (gsize cnt, gsize max_len, gdouble perm_factor, guchar key[16]; gdouble ts1, ts2; - ottery_rand_bytes (key, sizeof (key)); - input = generate_fuzzy_words (cnt, max_len); - ts1 = rspamd_get_virtual_ticks (); - sgl = rspamd_shingles_from_text (input, key, NULL, - rspamd_shingles_default_filter, NULL, alg); - ts2 = rspamd_get_virtual_ticks (); - permute_vector (input, perm_factor); - sgl_permuted = rspamd_shingles_from_text (input, key, NULL, - rspamd_shingles_default_filter, NULL, alg); - - res = rspamd_shingles_compare (sgl, sgl_permuted); - - msg_info ("%s (%z words of %z max len, %.2f perm factor):" - " percentage of common shingles: %.3f, generate time: %.4f sec", - algorithm_to_string (alg), cnt, max_len, perm_factor, res, ts2 - ts1); + ottery_rand_bytes(key, sizeof(key)); + input = generate_fuzzy_words(cnt, max_len); + ts1 = rspamd_get_virtual_ticks(); + sgl = rspamd_shingles_from_text(input, key, NULL, + rspamd_shingles_default_filter, NULL, alg); + ts2 = rspamd_get_virtual_ticks(); + permute_vector(input, perm_factor); + sgl_permuted = rspamd_shingles_from_text(input, key, NULL, + rspamd_shingles_default_filter, NULL, alg); + + res = rspamd_shingles_compare(sgl, sgl_permuted); + + msg_info("%s (%z words of %z max len, %.2f perm factor):" + " percentage of common shingles: %.3f, generate time: %.4f sec", + algorithm_to_string(alg), cnt, max_len, perm_factor, res, ts2 - ts1); //g_assert_cmpfloat (fabs ((1.0 - res) - sqrt (perm_factor)), <=, 0.25); - free_fuzzy_words (input); - g_free (sgl); - g_free (sgl_permuted); + free_fuzzy_words(input); + g_free(sgl); + g_free(sgl_permuted); } static const guint64 expected_old[RSPAMD_SHINGLE_SIZE] = { - 0x2a97e024235cedc5, 0x46238acbcc55e9e0, 0x2378ff151af075b3, 0xde1f29a95cad109, - 0x5d3bbbdb5db5d19f, 0x4d75a0ec52af10a6, 0x215ecd6372e755b5, 0x7b52295758295350, - 0x17387d1beddc7f62, 0x26264ca879ffcada, 0x49d4a65ec0ab9914, 0xa2763e6995350cf, - 0x3f4570231449c13f, 0x3309f857a0e54ee5, 0x24e4c5b561b0fce3, 0x1f153e3b275bfd1b, - 0x4d067dbc97c3fd78, 0x9ffa2d076fa4f8bc, 0x3d8907f84b9ffc6c, 0x1cfd664c5262d256, - 0xcdd7e744b699c15, 0x5544a2bbe05124f7, 0x5a4029b5d6a06f7, 0xd5adfbdc756c0e4, - 0xa504b23d9689a67e, 0x15d945f7007de115, 0xbf676c0522a2c51d, 0x1c8d8163ad4b0f93, - 0xa2c4ba20799344d7, 0x27c6f13c02134388, 0xa1d443d31fd5a3, 0x99fbca9f8563080, + 0x2a97e024235cedc5, + 0x46238acbcc55e9e0, + 0x2378ff151af075b3, + 0xde1f29a95cad109, + 0x5d3bbbdb5db5d19f, + 0x4d75a0ec52af10a6, + 0x215ecd6372e755b5, + 0x7b52295758295350, + 0x17387d1beddc7f62, + 0x26264ca879ffcada, + 0x49d4a65ec0ab9914, + 0xa2763e6995350cf, + 0x3f4570231449c13f, + 0x3309f857a0e54ee5, + 0x24e4c5b561b0fce3, + 0x1f153e3b275bfd1b, + 0x4d067dbc97c3fd78, + 0x9ffa2d076fa4f8bc, + 0x3d8907f84b9ffc6c, + 0x1cfd664c5262d256, + 0xcdd7e744b699c15, + 0x5544a2bbe05124f7, + 0x5a4029b5d6a06f7, + 0xd5adfbdc756c0e4, + 0xa504b23d9689a67e, + 0x15d945f7007de115, + 0xbf676c0522a2c51d, + 0x1c8d8163ad4b0f93, + 0xa2c4ba20799344d7, + 0x27c6f13c02134388, + 0xa1d443d31fd5a3, + 0x99fbca9f8563080, }; static const guint64 expected_xxhash[RSPAMD_SHINGLE_SIZE] = { - 0x33b134be11a705a, 0x36e2ea657aa36903, 0x6547b57f7470ce9d, 0x8253eb6d2f8f158e, - 0x1cc99e3cf22388f, 0x2396da27ea36ffe8, 0x1b457d208ad3d96c, 0x2d6ac733d7a2c107, - 0x17849cbed75cc4d1, 0x4dd94e772330e804, 0x39f592fa32014ed4, 0xa2f6229ad356461, - 0x6dc825879a057b37, 0x886b12cef4338b05, 0x8b23af68c186518a, 0x16932b40339aaf02, - 0x412090c6bb0b719c, 0x4d4a88cbdf1935f3, 0x233bcbddb5f67a7, 0x474719442a33dcca, - 0x2da7ec30563e622, 0x7ab90086960e1ad2, 0x3ea2b45582539f75, 0x108cd9287d95a6c5, - 0x69ba7c67c115597, 0x10880860eb75e982, 0x16f3d90e6ab995a6, 0x5f24ea09379b9f5c, - 0x3c2dc04088e8fe54, 0x340b8cf1c6f1227, 0x193bc348ed2e9ce7, 0x68454ef43da9c748, + 0x33b134be11a705a, + 0x36e2ea657aa36903, + 0x6547b57f7470ce9d, + 0x8253eb6d2f8f158e, + 0x1cc99e3cf22388f, + 0x2396da27ea36ffe8, + 0x1b457d208ad3d96c, + 0x2d6ac733d7a2c107, + 0x17849cbed75cc4d1, + 0x4dd94e772330e804, + 0x39f592fa32014ed4, + 0xa2f6229ad356461, + 0x6dc825879a057b37, + 0x886b12cef4338b05, + 0x8b23af68c186518a, + 0x16932b40339aaf02, + 0x412090c6bb0b719c, + 0x4d4a88cbdf1935f3, + 0x233bcbddb5f67a7, + 0x474719442a33dcca, + 0x2da7ec30563e622, + 0x7ab90086960e1ad2, + 0x3ea2b45582539f75, + 0x108cd9287d95a6c5, + 0x69ba7c67c115597, + 0x10880860eb75e982, + 0x16f3d90e6ab995a6, + 0x5f24ea09379b9f5c, + 0x3c2dc04088e8fe54, + 0x340b8cf1c6f1227, + 0x193bc348ed2e9ce7, + 0x68454ef43da9c748, }; static const guint64 expected_mumhash[RSPAMD_SHINGLE_SIZE] = { - 0x38d35473b80a7fc3, 0x1300531adc2d16a1, 0x26883bc89f78f4bd, 0x57de365ef6d1a62, - 0x773603185fcbb20a, 0x39c6cbd7ebbeaa88, 0x676c7445ad167e70, 0x432315d1ecc4c0b1, - 0x1380b95756dbb078, 0x9ee12832fa53b90e, 0x72970be210f0dd0b, 0x62909bd520f5956, - 0x66196965a45eb32a, 0x2466a9ca5436620e, 0x157b828b10e10f6e, 0x429bb673a523a7e5, - 0x51a6ace94f320f88, 0x23f53a30bd7d7147, 0xbee557664d3bc34c, 0x65730c88cd212a9, - 0x87e72c0cd05fd0e, 0x417a744669baeb3d, 0x78e26f7917829324, 0x439777dcfc25fdf4, - 0x582eac6ff013f00b, 0x1e40aa90e367f4af, 0x301d14a28d6c23a2, 0x34140ecb21b6c69, - 0x390a091c8b4c31b9, 0x2e35fecf9fff0ae7, 0x94322e1a5cf31f1b, 0x33cb9190905e049a, + 0x38d35473b80a7fc3, + 0x1300531adc2d16a1, + 0x26883bc89f78f4bd, + 0x57de365ef6d1a62, + 0x773603185fcbb20a, + 0x39c6cbd7ebbeaa88, + 0x676c7445ad167e70, + 0x432315d1ecc4c0b1, + 0x1380b95756dbb078, + 0x9ee12832fa53b90e, + 0x72970be210f0dd0b, + 0x62909bd520f5956, + 0x66196965a45eb32a, + 0x2466a9ca5436620e, + 0x157b828b10e10f6e, + 0x429bb673a523a7e5, + 0x51a6ace94f320f88, + 0x23f53a30bd7d7147, + 0xbee557664d3bc34c, + 0x65730c88cd212a9, + 0x87e72c0cd05fd0e, + 0x417a744669baeb3d, + 0x78e26f7917829324, + 0x439777dcfc25fdf4, + 0x582eac6ff013f00b, + 0x1e40aa90e367f4af, + 0x301d14a28d6c23a2, + 0x34140ecb21b6c69, + 0x390a091c8b4c31b9, + 0x2e35fecf9fff0ae7, + 0x94322e1a5cf31f1b, + 0x33cb9190905e049a, }; static const guint64 expected_fasthash[RSPAMD_SHINGLE_SIZE] = { - 0x3843a716f94828a6, 0x13fd5386dda3b28d, 0x71cb09de527c40a, 0x5d6f59ffd839c62, - 0x7ce3633acd568476, 0x9014298cbd00167, 0x6708ec29eedb5350, 0x2882931ff2c5c410, - 0x1839d8b947b12571, 0x58f7bc3829173302, 0x4dac8103da51abc4, 0x6c5cbcc6fb1de28, - 0x31fefcef9bafb755, 0x6f2d1a0b1feca401, 0x3e71f3718e520b06, 0x42f6ba11164ab231, - 0x21164d010bd76f4a, 0x4c597ccc7b60f620, 0x2cf1ca3383b77574, 0x54ff9c01660b8add, - 0x2ca344758f40380d, 0x1b962321bd37d0f2, 0x9323bb99c32bc418, 0x375659d0eef2b8f2, - 0x1dbd23a1030084b7, 0x83cb978dee06aa0a, 0x42c97be5b27a7763, 0x3b6d6b7270ed765, - 0x125c12fdba584aed, 0x1c826397afe58763, 0x8bdbe2d43f3eda96, 0x954cda70edf6591f, + 0x3843a716f94828a6, + 0x13fd5386dda3b28d, + 0x71cb09de527c40a, + 0x5d6f59ffd839c62, + 0x7ce3633acd568476, + 0x9014298cbd00167, + 0x6708ec29eedb5350, + 0x2882931ff2c5c410, + 0x1839d8b947b12571, + 0x58f7bc3829173302, + 0x4dac8103da51abc4, + 0x6c5cbcc6fb1de28, + 0x31fefcef9bafb755, + 0x6f2d1a0b1feca401, + 0x3e71f3718e520b06, + 0x42f6ba11164ab231, + 0x21164d010bd76f4a, + 0x4c597ccc7b60f620, + 0x2cf1ca3383b77574, + 0x54ff9c01660b8add, + 0x2ca344758f40380d, + 0x1b962321bd37d0f2, + 0x9323bb99c32bc418, + 0x375659d0eef2b8f2, + 0x1dbd23a1030084b7, + 0x83cb978dee06aa0a, + 0x42c97be5b27a7763, + 0x3b6d6b7270ed765, + 0x125c12fdba584aed, + 0x1c826397afe58763, + 0x8bdbe2d43f3eda96, + 0x954cda70edf6591f, }; -void -rspamd_shingles_test_func (void) +void rspamd_shingles_test_func(void) { enum rspamd_shingle_alg alg = RSPAMD_SHINGLES_OLD; struct rspamd_shingle *sgl; @@ -190,55 +285,55 @@ rspamd_shingles_test_func (void) rspamd_ftok_t tok; int i; - memset (key, 0, sizeof (key)); - input = g_array_sized_new (FALSE, FALSE, sizeof (rspamd_ftok_t), 5); + memset(key, 0, sizeof(key)); + input = g_array_sized_new(FALSE, FALSE, sizeof(rspamd_ftok_t), 5); - for (i = 0; i < 5; i ++) { - gchar *b = g_alloca (8); - memset (b, 0, 8); - memcpy (b + 1, "test", 4); + for (i = 0; i < 5; i++) { + gchar *b = g_alloca(8); + memset(b, 0, 8); + memcpy(b + 1, "test", 4); b[0] = 'a' + i; tok.begin = b; tok.len = 5 + ((i + 1) % 4); - g_array_append_val (input, tok); + g_array_append_val(input, tok); } - sgl = rspamd_shingles_from_text (input, key, NULL, - rspamd_shingles_default_filter, NULL, RSPAMD_SHINGLES_OLD); - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { - g_assert (sgl->hashes[i] == expected_old[i]); + sgl = rspamd_shingles_from_text(input, key, NULL, + rspamd_shingles_default_filter, NULL, RSPAMD_SHINGLES_OLD); + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { + g_assert(sgl->hashes[i] == expected_old[i]); } - g_free (sgl); + g_free(sgl); - sgl = rspamd_shingles_from_text (input, key, NULL, - rspamd_shingles_default_filter, NULL, RSPAMD_SHINGLES_XXHASH); - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { - g_assert (sgl->hashes[i] == expected_xxhash[i]); + sgl = rspamd_shingles_from_text(input, key, NULL, + rspamd_shingles_default_filter, NULL, RSPAMD_SHINGLES_XXHASH); + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { + g_assert(sgl->hashes[i] == expected_xxhash[i]); } - g_free (sgl); + g_free(sgl); - sgl = rspamd_shingles_from_text (input, key, NULL, - rspamd_shingles_default_filter, NULL, RSPAMD_SHINGLES_MUMHASH); - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { - g_assert (sgl->hashes[i] == expected_mumhash[i]); + sgl = rspamd_shingles_from_text(input, key, NULL, + rspamd_shingles_default_filter, NULL, RSPAMD_SHINGLES_MUMHASH); + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { + g_assert(sgl->hashes[i] == expected_mumhash[i]); } - g_free (sgl); + g_free(sgl); - sgl = rspamd_shingles_from_text (input, key, NULL, - rspamd_shingles_default_filter, NULL, RSPAMD_SHINGLES_FAST); - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { - g_assert (sgl->hashes[i] == expected_fasthash[i]); + sgl = rspamd_shingles_from_text(input, key, NULL, + rspamd_shingles_default_filter, NULL, RSPAMD_SHINGLES_FAST); + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { + g_assert(sgl->hashes[i] == expected_fasthash[i]); } - g_free (sgl); - - for (alg = RSPAMD_SHINGLES_OLD; alg <= RSPAMD_SHINGLES_FAST; alg ++) { - test_case (200, 10, 0.1, alg); - test_case (500, 20, 0.01, alg); - test_case (5000, 20, 0.01, alg); - test_case (5000, 15, 0, alg); - test_case (5000, 30, 1.0, alg); - test_case (50000, 30, 0.02, alg); - test_case (50000, 5, 0.02, alg); - test_case (50000, 16, 0.02, alg); + g_free(sgl); + + for (alg = RSPAMD_SHINGLES_OLD; alg <= RSPAMD_SHINGLES_FAST; alg++) { + test_case(200, 10, 0.1, alg); + test_case(500, 20, 0.01, alg); + test_case(5000, 20, 0.01, alg); + test_case(5000, 15, 0, alg); + test_case(5000, 30, 1.0, alg); + test_case(50000, 30, 0.02, alg); + test_case(50000, 5, 0.02, alg); + test_case(50000, 16, 0.02, alg); } } diff --git a/test/rspamd_statfile_test.c b/test/rspamd_statfile_test.c index 155e4ad68..0a3837d2a 100644 --- a/test/rspamd_statfile_test.c +++ b/test/rspamd_statfile_test.c @@ -6,8 +6,7 @@ #define TEST_FILENAME "/tmp/rspamd_test.stat" #define HASHES_NUM 256 -void -rspamd_statfile_test_func (void) +void rspamd_statfile_test_func(void) { /* * XXX: broken, old, need to be rewritten diff --git a/test/rspamd_test_suite.c b/test/rspamd_test_suite.c index 71dc57d33..b3d03915f 100644 --- a/test/rspamd_test_suite.c +++ b/test/rspamd_test_suite.c @@ -5,9 +5,9 @@ #include "tests.h" #include "contrib/libev/ev.h" -struct rspamd_main *rspamd_main = NULL; -struct ev_loop *event_loop = NULL; -worker_t *workers[] = { NULL }; +struct rspamd_main *rspamd_main = NULL; +struct ev_loop *event_loop = NULL; +worker_t *workers[] = {NULL}; gchar *lua_test = NULL; gchar *lua_test_case = NULL; @@ -15,75 +15,73 @@ gboolean verbose = FALSE; gchar *argv0_dirname = NULL; static GOptionEntry entries[] = -{ - { "test", 't', 0, G_OPTION_ARG_STRING, &lua_test, - "Lua test to run (i.e. selectors.lua)", NULL }, - { "test-case", 'c', 0, G_OPTION_ARG_STRING, &lua_test_case, - "Lua test to run, lua pattern i.e. \"case .* rcpts\"", NULL }, - { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL } -}; - -int -main (int argc, char **argv) + { + {"test", 't', 0, G_OPTION_ARG_STRING, &lua_test, + "Lua test to run (i.e. selectors.lua)", NULL}, + {"test-case", 'c', 0, G_OPTION_ARG_STRING, &lua_test_case, + "Lua test to run, lua pattern i.e. \"case .* rcpts\"", NULL}, + {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}}; + +int main(int argc, char **argv) { struct rspamd_config *cfg; GOptionContext *context; GError *error = NULL; - rspamd_main = (struct rspamd_main *)g_malloc (sizeof (struct rspamd_main)); - memset (rspamd_main, 0, sizeof (struct rspamd_main)); - rspamd_main->server_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL, 0); - cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_DEFAULT); - cfg->libs_ctx = rspamd_init_libs (); + rspamd_main = (struct rspamd_main *) g_malloc(sizeof(struct rspamd_main)); + memset(rspamd_main, 0, sizeof(struct rspamd_main)); + rspamd_main->server_pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), NULL, 0); + cfg = rspamd_config_new(RSPAMD_CONFIG_INIT_DEFAULT); + cfg->libs_ctx = rspamd_init_libs(); /* More aggressive GC, workaround for 'not enough memory' test failures */ cfg->lua_gc_step *= 2; rspamd_main->cfg = cfg; - cfg->cfg_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL, 0); + cfg->cfg_pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), NULL, 0); - g_test_init (&argc, &argv, NULL); + g_test_init(&argc, &argv, NULL); - argv0_dirname = g_path_get_dirname (argv[0]); + argv0_dirname = g_path_get_dirname(argv[0]); - context = g_option_context_new ("- run rspamd test"); - g_option_context_add_main_entries (context, entries, NULL); + context = g_option_context_new("- run rspamd test"); + g_option_context_add_main_entries(context, entries, NULL); - if (!g_option_context_parse (context, &argc, &argv, &error)) { - fprintf (stderr, "option parsing failed: %s\n", error->message); - g_option_context_free (context); - exit (EXIT_FAILURE); + if (!g_option_context_parse(context, &argc, &argv, &error)) { + fprintf(stderr, "option parsing failed: %s\n", error->message); + g_option_context_free(context); + exit(EXIT_FAILURE); } /* Setup logger */ - if (verbose || g_test_verbose ()) { - rspamd_main->logger = rspamd_log_open_emergency (rspamd_main->server_pool, - RSPAMD_LOG_FLAG_USEC|RSPAMD_LOG_FLAG_ENFORCED|RSPAMD_LOG_FLAG_RSPAMADM); + if (verbose || g_test_verbose()) { + rspamd_main->logger = rspamd_log_open_emergency(rspamd_main->server_pool, + RSPAMD_LOG_FLAG_USEC | RSPAMD_LOG_FLAG_ENFORCED | RSPAMD_LOG_FLAG_RSPAMADM); - rspamd_log_set_log_level (rspamd_main->logger, G_LOG_LEVEL_DEBUG); + rspamd_log_set_log_level(rspamd_main->logger, G_LOG_LEVEL_DEBUG); } else { - rspamd_main->logger = rspamd_log_open_emergency (rspamd_main->server_pool, - RSPAMD_LOG_FLAG_RSPAMADM); - rspamd_log_set_log_level (rspamd_main->logger, G_LOG_LEVEL_MESSAGE); + rspamd_main->logger = rspamd_log_open_emergency(rspamd_main->server_pool, + RSPAMD_LOG_FLAG_RSPAMADM); + rspamd_log_set_log_level(rspamd_main->logger, G_LOG_LEVEL_MESSAGE); } - rspamd_lua_set_path ((lua_State *)cfg->lua_state, NULL, NULL); - event_loop = ev_default_loop (EVFLAG_SIGNALFD|EVBACKEND_ALL); - rspamd_stat_init (cfg, event_loop); - rspamd_url_init (NULL); - - g_log_set_default_handler (rspamd_glib_log_function, rspamd_main->logger); - - g_test_add_func ("/rspamd/mem_pool", rspamd_mem_pool_test_func); - g_test_add_func ("/rspamd/radix", rspamd_radix_test_func); - g_test_add_func ("/rspamd/dns", rspamd_dns_test_func); - g_test_add_func ("/rspamd/dkim", rspamd_dkim_test_func); - g_test_add_func ("/rspamd/rrd", rspamd_rrd_test_func); - g_test_add_func ("/rspamd/upstream", rspamd_upstream_test_func); - g_test_add_func ("/rspamd/shingles", rspamd_shingles_test_func); - g_test_add_func ("/rspamd/lua", rspamd_lua_test_func); - g_test_add_func ("/rspamd/cryptobox", rspamd_cryptobox_test_func); - g_test_add_func ("/rspamd/heap", rspamd_heap_test_func); - g_test_add_func ("/rspamd/lua_pcall", rspamd_lua_lua_pcall_vs_resume_test_func); + rspamd_lua_set_path((lua_State *) cfg->lua_state, NULL, NULL); + event_loop = ev_default_loop(EVFLAG_SIGNALFD | EVBACKEND_ALL); + rspamd_stat_init(cfg, event_loop); + rspamd_url_init(NULL); + + g_log_set_default_handler(rspamd_glib_log_function, rspamd_main->logger); + + g_test_add_func("/rspamd/mem_pool", rspamd_mem_pool_test_func); + g_test_add_func("/rspamd/radix", rspamd_radix_test_func); + g_test_add_func("/rspamd/dns", rspamd_dns_test_func); + g_test_add_func("/rspamd/dkim", rspamd_dkim_test_func); + g_test_add_func("/rspamd/rrd", rspamd_rrd_test_func); + g_test_add_func("/rspamd/upstream", rspamd_upstream_test_func); + g_test_add_func("/rspamd/shingles", rspamd_shingles_test_func); + g_test_add_func("/rspamd/lua", rspamd_lua_test_func); + g_test_add_func("/rspamd/cryptobox", rspamd_cryptobox_test_func); + g_test_add_func("/rspamd/heap", rspamd_heap_test_func); + g_test_add_func("/rspamd/lua_pcall", rspamd_lua_lua_pcall_vs_resume_test_func); #if 0 g_test_add_func ("/rspamd/http", rspamd_http_test_func); @@ -91,7 +89,7 @@ main (int argc, char **argv) g_test_add_func ("/rspamd/statfile", rspamd_statfile_test_func); g_test_add_func ("/rspamd/aio", rspamd_async_test_func); #endif - g_test_run (); + g_test_run(); return 0; } diff --git a/test/rspamd_upstream_test.c b/test/rspamd_upstream_test.c index 263a28e75..1f4e27daa 100644 --- a/test/rspamd_upstream_test.c +++ b/test/rspamd_upstream_test.c @@ -26,34 +26,33 @@ char test_key[32]; extern struct ev_loop *event_loop; static void -rspamd_upstream_test_method (struct upstream_list *ls, - enum rspamd_upstream_rotation rot, const gchar *expected) +rspamd_upstream_test_method(struct upstream_list *ls, + enum rspamd_upstream_rotation rot, const gchar *expected) { struct upstream *up; if (rot != RSPAMD_UPSTREAM_HASHED) { - up = rspamd_upstream_get (ls, rot, NULL, 0); - g_assert (up != NULL); - g_assert (strcmp (rspamd_upstream_name (up), expected) == 0); + up = rspamd_upstream_get(ls, rot, NULL, 0); + g_assert(up != NULL); + g_assert(strcmp(rspamd_upstream_name(up), expected) == 0); } else { - up = rspamd_upstream_get (ls, RSPAMD_UPSTREAM_HASHED, test_key, - sizeof (test_key)); - g_assert (up != NULL); - g_assert (strcmp (rspamd_upstream_name (up), expected) == 0); + up = rspamd_upstream_get(ls, RSPAMD_UPSTREAM_HASHED, test_key, + sizeof(test_key)); + g_assert(up != NULL); + g_assert(strcmp(rspamd_upstream_name(up), expected) == 0); } } static void -rspamd_upstream_timeout_handler (EV_P_ ev_timer *w, int revents) +rspamd_upstream_timeout_handler(EV_P_ ev_timer *w, int revents) { - struct rspamd_dns_resolver *resolver = (struct rspamd_dns_resolver *)w->data; + struct rspamd_dns_resolver *resolver = (struct rspamd_dns_resolver *) w->data; - rdns_resolver_release (resolver->r); + rdns_resolver_release(resolver->r); } -void -rspamd_upstream_test_func (void) +void rspamd_upstream_test_func(void) { struct upstream_list *ls, *nls; struct upstream *up, *upn; @@ -65,119 +64,119 @@ rspamd_upstream_test_func (void) static ev_timer ev; rspamd_inet_addr_t *addr, *next_addr, *paddr; - cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_SKIP_LUA); + cfg = rspamd_config_new(RSPAMD_CONFIG_INIT_SKIP_LUA); cfg->dns_retransmits = 2; cfg->dns_timeout = 0.5; cfg->upstream_max_errors = 1; cfg->upstream_revive_time = 0.5; cfg->upstream_error_time = 2; - resolver = rspamd_dns_resolver_init (NULL, event_loop, cfg); - rspamd_upstreams_library_config (cfg, cfg->ups_ctx, event_loop, resolver->r); + resolver = rspamd_dns_resolver_init(NULL, event_loop, cfg); + rspamd_upstreams_library_config(cfg, cfg->ups_ctx, event_loop, resolver->r); /* * Test v4/v6 priorities */ - nls = rspamd_upstreams_create (cfg->ups_ctx); - g_assert (rspamd_upstreams_add_upstream (nls, "127.0.0.1", 0, - RSPAMD_UPSTREAM_PARSE_DEFAULT, - NULL)); - up = rspamd_upstream_get (nls, RSPAMD_UPSTREAM_RANDOM, NULL, 0); - rspamd_parse_inet_address (&paddr, "127.0.0.2", strlen ("127.0.0.2"), - RSPAMD_INET_ADDRESS_PARSE_DEFAULT); - g_assert (rspamd_upstream_add_addr (up, paddr)); - rspamd_parse_inet_address (&paddr, "::1", strlen ("::1"), - RSPAMD_INET_ADDRESS_PARSE_DEFAULT); - g_assert (rspamd_upstream_add_addr (up, paddr)); + nls = rspamd_upstreams_create(cfg->ups_ctx); + g_assert(rspamd_upstreams_add_upstream(nls, "127.0.0.1", 0, + RSPAMD_UPSTREAM_PARSE_DEFAULT, + NULL)); + up = rspamd_upstream_get(nls, RSPAMD_UPSTREAM_RANDOM, NULL, 0); + rspamd_parse_inet_address(&paddr, "127.0.0.2", strlen("127.0.0.2"), + RSPAMD_INET_ADDRESS_PARSE_DEFAULT); + g_assert(rspamd_upstream_add_addr(up, paddr)); + rspamd_parse_inet_address(&paddr, "::1", strlen("::1"), + RSPAMD_INET_ADDRESS_PARSE_DEFAULT); + g_assert(rspamd_upstream_add_addr(up, paddr)); /* Rewind to start */ - addr = rspamd_upstream_addr_next (up); - addr = rspamd_upstream_addr_next (up); + addr = rspamd_upstream_addr_next(up); + addr = rspamd_upstream_addr_next(up); /* cur should be zero here */ - addr = rspamd_upstream_addr_next (up); - next_addr = rspamd_upstream_addr_next (up); - g_assert (rspamd_inet_address_get_af (addr) == AF_INET); - g_assert (rspamd_inet_address_get_af (next_addr) == AF_INET); - next_addr = rspamd_upstream_addr_next (up); - g_assert (rspamd_inet_address_get_af (next_addr) == AF_INET6); - next_addr = rspamd_upstream_addr_next (up); - g_assert (rspamd_inet_address_get_af (next_addr) == AF_INET); - next_addr = rspamd_upstream_addr_next (up); - g_assert (rspamd_inet_address_get_af (next_addr) == AF_INET); - next_addr = rspamd_upstream_addr_next (up); - g_assert (rspamd_inet_address_get_af (next_addr) == AF_INET6); + addr = rspamd_upstream_addr_next(up); + next_addr = rspamd_upstream_addr_next(up); + g_assert(rspamd_inet_address_get_af(addr) == AF_INET); + g_assert(rspamd_inet_address_get_af(next_addr) == AF_INET); + next_addr = rspamd_upstream_addr_next(up); + g_assert(rspamd_inet_address_get_af(next_addr) == AF_INET6); + next_addr = rspamd_upstream_addr_next(up); + g_assert(rspamd_inet_address_get_af(next_addr) == AF_INET); + next_addr = rspamd_upstream_addr_next(up); + g_assert(rspamd_inet_address_get_af(next_addr) == AF_INET); + next_addr = rspamd_upstream_addr_next(up); + g_assert(rspamd_inet_address_get_af(next_addr) == AF_INET6); /* Test errors with IPv6 */ - rspamd_upstream_fail (up, TRUE, NULL); + rspamd_upstream_fail(up, TRUE, NULL); /* Now we should have merely IPv4 addresses in rotation */ - addr = rspamd_upstream_addr_next (up); + addr = rspamd_upstream_addr_next(up); for (i = 0; i < 256; i++) { - next_addr = rspamd_upstream_addr_next (up); - g_assert (rspamd_inet_address_get_af (addr) == AF_INET); - g_assert (rspamd_inet_address_get_af (next_addr) == AF_INET); - g_assert (rspamd_inet_address_compare (addr, next_addr, FALSE) != 0); + next_addr = rspamd_upstream_addr_next(up); + g_assert(rspamd_inet_address_get_af(addr) == AF_INET); + g_assert(rspamd_inet_address_get_af(next_addr) == AF_INET); + g_assert(rspamd_inet_address_compare(addr, next_addr, FALSE) != 0); addr = next_addr; } - rspamd_upstreams_destroy (nls); + rspamd_upstreams_destroy(nls); - ls = rspamd_upstreams_create (cfg->ups_ctx); - g_assert (rspamd_upstreams_parse_line (ls, test_upstream_list, 443, NULL)); - g_assert (rspamd_upstreams_count (ls) == 3); + ls = rspamd_upstreams_create(cfg->ups_ctx); + g_assert(rspamd_upstreams_parse_line(ls, test_upstream_list, 443, NULL)); + g_assert(rspamd_upstreams_count(ls) == 3); /* Test master-slave rotation */ - rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_MASTER_SLAVE, "kernel.org"); - rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_MASTER_SLAVE, "kernel.org"); + rspamd_upstream_test_method(ls, RSPAMD_UPSTREAM_MASTER_SLAVE, "kernel.org"); + rspamd_upstream_test_method(ls, RSPAMD_UPSTREAM_MASTER_SLAVE, "kernel.org"); /* Test round-robin rotation */ - rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "kernel.org"); - rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "kernel.org"); - rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "google.com"); - rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "kernel.org"); - rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "google.com"); - rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "microsoft.com"); + rspamd_upstream_test_method(ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "kernel.org"); + rspamd_upstream_test_method(ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "kernel.org"); + rspamd_upstream_test_method(ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "google.com"); + rspamd_upstream_test_method(ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "kernel.org"); + rspamd_upstream_test_method(ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "google.com"); + rspamd_upstream_test_method(ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "microsoft.com"); /* Test stable hashing */ - nls = rspamd_upstreams_create (cfg->ups_ctx); - g_assert (rspamd_upstreams_parse_line (nls, test_upstream_list, 443, NULL)); - g_assert (rspamd_upstreams_parse_line (nls, new_upstream_list, 443, NULL)); - for (i = 0; i < assumptions; i ++) { - ottery_rand_bytes (test_key, sizeof (test_key)); - up = rspamd_upstream_get (ls, RSPAMD_UPSTREAM_HASHED, test_key, - sizeof (test_key)); - upn = rspamd_upstream_get (nls, RSPAMD_UPSTREAM_HASHED, test_key, - sizeof (test_key)); - - if (strcmp (rspamd_upstream_name (up), rspamd_upstream_name (upn)) == 0) { - success ++; + nls = rspamd_upstreams_create(cfg->ups_ctx); + g_assert(rspamd_upstreams_parse_line(nls, test_upstream_list, 443, NULL)); + g_assert(rspamd_upstreams_parse_line(nls, new_upstream_list, 443, NULL)); + for (i = 0; i < assumptions; i++) { + ottery_rand_bytes(test_key, sizeof(test_key)); + up = rspamd_upstream_get(ls, RSPAMD_UPSTREAM_HASHED, test_key, + sizeof(test_key)); + upn = rspamd_upstream_get(nls, RSPAMD_UPSTREAM_HASHED, test_key, + sizeof(test_key)); + + if (strcmp(rspamd_upstream_name(up), rspamd_upstream_name(upn)) == 0) { + success++; } } - p = 1.0 - fabs (3.0 / 4.0 - (gdouble)success / (gdouble)assumptions); + p = 1.0 - fabs(3.0 / 4.0 - (gdouble) success / (gdouble) assumptions); /* * P value is calculated as following: * when we add/remove M upstreams from the list, the probability of hash * miss should be close to the relation N / (N + M), where N is the size of * the previous upstreams list. */ - msg_debug ("p value for hash consistency: %.6f", p); - g_assert (p > 0.9); + msg_debug("p value for hash consistency: %.6f", p); + g_assert(p > 0.9); - rspamd_upstreams_destroy (nls); + rspamd_upstreams_destroy(nls); /* Upstream fail test */ ev.data = resolver; - ev_timer_init (&ev, rspamd_upstream_timeout_handler, 2.0, 0.0); + ev_timer_init(&ev, rspamd_upstream_timeout_handler, 2.0, 0.0); - up = rspamd_upstream_get (ls, RSPAMD_UPSTREAM_MASTER_SLAVE, NULL, 0); - for (i = 0; i < 100; i ++) { - rspamd_upstream_fail (up, TRUE, NULL); + up = rspamd_upstream_get(ls, RSPAMD_UPSTREAM_MASTER_SLAVE, NULL, 0); + for (i = 0; i < 100; i++) { + rspamd_upstream_fail(up, TRUE, NULL); } - g_assert (rspamd_upstreams_alive (ls) == 2); + g_assert(rspamd_upstreams_alive(ls) == 2); - ev_timer_start (event_loop, &ev); + ev_timer_start(event_loop, &ev); - ev_run (event_loop, 0); - g_assert (rspamd_upstreams_alive (ls) == 3); + ev_run(event_loop, 0); + g_assert(rspamd_upstreams_alive(ls) == 3); - rspamd_upstreams_destroy (ls); - REF_RELEASE (cfg); + rspamd_upstreams_destroy(ls); + REF_RELEASE(cfg); } diff --git a/test/rspamd_url_test.c b/test/rspamd_url_test.c index 092274cb1..c06f8d088 100644 --- a/test/rspamd_url_test.c +++ b/test/rspamd_url_test.c @@ -5,57 +5,56 @@ #include "tests.h" const char *test_text = -"www.schemeless.ru\n" -"www.schemeless.rus\n" -" as ftp.schemeless.ru dasd \n" -"ftp12.schemeless.ru\n" -"ftpsearch.schemeless.ru\n" -"schemeless.ru\n" -"www.schemeless.microsoft\n" -"1.2.3.4\n" -"1.2.3.4/a\n" -"1.2.3\n" -"1.2.3.4.5\n" -"www.schemeless.ru,\n" -"www.schemeless.ru.\n" -"http://www.schemed.ru.\n" -"http://www.schemed.ru.\n" -"http://www.bolinfest.com/targetalert/'\n" -"http://www.bolinfest.com/targetalert/'';\n" -"https://www.schemed.ru.\n" -"ufps://www.schemed.ru.\n" -"http://ported.ru:8080\n" -"http://ported.ru:8080\n" -"http://1.2.3.4\n" -"http://1.2.3.4:80\n" -"1.2.3.4:80\n" -"www.a9.com\n" -"www.a-9.com\n" -"http://www.schemed.ru/a.txt:\n" -"http://www.schemed.ru/a.txt'\n" -"http://www.schemed.ru/a.txt\"\n" -"http://www.schemed.ru/a.txt>\n" -"http://www.schemed.ru/a=3&b=4\n" -"http://spam.ru/bad=user@domain.com\n" -"http://spam.ru/bad=user@domain.com\n" -"http://spam.ru user@domain.com\n" -"http://a.foto.radikal.ru/0604/de7793c6ca62.jpg\n" -"http://a.foto.radikal.ru/0604/de7793c6ca62.jpg\n" -"schemeless.gz\n" -"schemeless.jp\n" -"schemeless.ua\n" -"schemeless.gz/a\n" -"mysql.so\n" -"http://mysql.so\n" -"3com.com\n" -"lj-user.livejournal.com\n" -"http://lj-user.livejournal.com\n" -"http://vsem.ru?action;\n"; + "www.schemeless.ru\n" + "www.schemeless.rus\n" + " as ftp.schemeless.ru dasd \n" + "ftp12.schemeless.ru\n" + "ftpsearch.schemeless.ru\n" + "schemeless.ru\n" + "www.schemeless.microsoft\n" + "1.2.3.4\n" + "1.2.3.4/a\n" + "1.2.3\n" + "1.2.3.4.5\n" + "www.schemeless.ru,\n" + "www.schemeless.ru.\n" + "http://www.schemed.ru.\n" + "http://www.schemed.ru.\n" + "http://www.bolinfest.com/targetalert/'\n" + "http://www.bolinfest.com/targetalert/'';\n" + "https://www.schemed.ru.\n" + "ufps://www.schemed.ru.\n" + "http://ported.ru:8080\n" + "http://ported.ru:8080\n" + "http://1.2.3.4\n" + "http://1.2.3.4:80\n" + "1.2.3.4:80\n" + "www.a9.com\n" + "www.a-9.com\n" + "http://www.schemed.ru/a.txt:\n" + "http://www.schemed.ru/a.txt'\n" + "http://www.schemed.ru/a.txt\"\n" + "http://www.schemed.ru/a.txt>\n" + "http://www.schemed.ru/a=3&b=4\n" + "http://spam.ru/bad=user@domain.com\n" + "http://spam.ru/bad=user@domain.com\n" + "http://spam.ru user@domain.com\n" + "http://a.foto.radikal.ru/0604/de7793c6ca62.jpg\n" + "http://a.foto.radikal.ru/0604/de7793c6ca62.jpg\n" + "schemeless.gz\n" + "schemeless.jp\n" + "schemeless.ua\n" + "schemeless.gz/a\n" + "mysql.so\n" + "http://mysql.so\n" + "3com.com\n" + "lj-user.livejournal.com\n" + "http://lj-user.livejournal.com\n" + "http://vsem.ru?action;\n"; const char *test_html = "<some_tag>This is test file with <a href=\"http://microsoft.com\">http://TesT.com/././?%45%46%20 url</a></some_tag>"; /* Function for using in glib test suite */ -void -rspamd_url_test_func (void) +void rspamd_url_test_func(void) { /* XXX: maybe write test for this */ } diff --git a/test/tests.h b/test/tests.h index 95eef354d..be37bbc84 100644 --- a/test/tests.h +++ b/test/tests.h @@ -5,46 +5,46 @@ * Here are described test functions for rspamd test suite */ -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif /* URL parser test */ -void rspamd_url_test_func (void); +void rspamd_url_test_func(void); /* Memory pools */ -void rspamd_mem_pool_test_func (void); +void rspamd_mem_pool_test_func(void); /* Stat file */ -void rspamd_statfile_test_func (void); +void rspamd_statfile_test_func(void); /* Radix test */ -void rspamd_radix_test_func (void); +void rspamd_radix_test_func(void); /* DNS resolving */ -void rspamd_dns_test_func (void); +void rspamd_dns_test_func(void); /* DKIM test */ -void rspamd_dkim_test_func (void); +void rspamd_dkim_test_func(void); /* RRD test */ -void rspamd_rrd_test_func (void); +void rspamd_rrd_test_func(void); -void rspamd_upstream_test_func (void); +void rspamd_upstream_test_func(void); -void rspamd_shingles_test_func (void); +void rspamd_shingles_test_func(void); -void rspamd_http_test_func (void); +void rspamd_http_test_func(void); -void rspamd_lua_test_func (void); +void rspamd_lua_test_func(void); -void rspamd_cryptobox_test_func (void); +void rspamd_cryptobox_test_func(void); -void rspamd_heap_test_func (void); +void rspamd_heap_test_func(void); -void rspamd_lua_lua_pcall_vs_resume_test_func (void); +void rspamd_lua_lua_pcall_vs_resume_test_func(void); -#ifdef __cplusplus +#ifdef __cplusplus } #endif |