2014-11-02 19:38:43 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014, Vsevolod Stakhov
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY AUTHOR ''AS IS'' AND ANY
|
|
|
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2015-09-22 19:17:24 +02:00
|
|
|
#include "rspamd.h"
|
2014-11-02 22:42:53 +01:00
|
|
|
#include "ottery.h"
|
2015-10-08 14:15:15 +02:00
|
|
|
#include <math.h>
|
2014-11-02 19:38:43 +01:00
|
|
|
|
2014-11-02 20:03:15 +01:00
|
|
|
const char *test_upstream_list = "microsoft.com:443:1,google.com:80:2,kernel.org:443:3";
|
2014-11-02 22:42:53 +01:00
|
|
|
const char *new_upstream_list = "freebsd.org:80";
|
|
|
|
char test_key[32];
|
2014-11-02 19:38:43 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
rspamd_upstream_test_method (struct upstream_list *ls,
|
|
|
|
enum rspamd_upstream_rotation rot, const gchar *expected)
|
|
|
|
{
|
|
|
|
struct upstream *up;
|
|
|
|
|
2014-11-02 22:42:53 +01:00
|
|
|
if (rot != RSPAMD_UPSTREAM_HASHED) {
|
2015-11-21 14:31:26 +01:00
|
|
|
up = rspamd_upstream_get (ls, rot, NULL, 0);
|
2014-11-02 22:42:53 +01:00
|
|
|
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);
|
|
|
|
}
|
2014-11-02 19:38:43 +01:00
|
|
|
}
|
|
|
|
|
2014-11-03 12:41:07 +01:00
|
|
|
static void
|
|
|
|
rspamd_upstream_timeout_handler (int fd, short what, void *arg)
|
|
|
|
{
|
|
|
|
struct rspamd_dns_resolver *resolver = (struct rspamd_dns_resolver *)arg;
|
|
|
|
|
|
|
|
rdns_resolver_release (resolver->r);
|
|
|
|
}
|
|
|
|
|
2014-11-02 19:38:43 +01:00
|
|
|
void
|
|
|
|
rspamd_upstream_test_func (void)
|
|
|
|
{
|
2014-11-02 22:42:53 +01:00
|
|
|
struct upstream_list *ls, *nls;
|
|
|
|
struct upstream *up, *upn;
|
2014-11-02 19:38:43 +01:00
|
|
|
struct event_base *ev_base = event_init ();
|
|
|
|
struct rspamd_dns_resolver *resolver;
|
|
|
|
struct rspamd_config *cfg;
|
2014-11-02 22:42:53 +01:00
|
|
|
gint i, success = 0;
|
|
|
|
const gint assumptions = 100500;
|
2014-11-03 11:23:32 +01:00
|
|
|
gdouble p;
|
2014-11-03 12:41:07 +01:00
|
|
|
struct event ev;
|
|
|
|
struct timeval tv;
|
2015-03-12 17:48:02 +01:00
|
|
|
rspamd_inet_addr_t *addr, *next_addr, *paddr;
|
2014-11-02 19:38:43 +01:00
|
|
|
|
2015-11-20 18:03:51 +01:00
|
|
|
cfg = rspamd_config_new ();
|
2014-11-02 19:38:43 +01:00
|
|
|
cfg->dns_retransmits = 2;
|
|
|
|
cfg->dns_timeout = 0.5;
|
2014-11-03 12:41:07 +01:00
|
|
|
cfg->upstream_max_errors = 1;
|
|
|
|
cfg->upstream_revive_time = 0.5;
|
|
|
|
cfg->upstream_error_time = 2;
|
2014-11-02 19:38:43 +01:00
|
|
|
|
|
|
|
resolver = dns_resolver_init (NULL, ev_base, cfg);
|
2015-11-20 18:03:51 +01:00
|
|
|
rspamd_upstreams_library_config (cfg, cfg->ups_ctx, ev_base, resolver->r);
|
2014-11-02 19:38:43 +01:00
|
|
|
|
2015-12-03 19:51:21 +01:00
|
|
|
/*
|
|
|
|
* Test v4/v6 priorities
|
|
|
|
*/
|
|
|
|
nls = rspamd_upstreams_create (cfg->ups_ctx);
|
|
|
|
g_assert (rspamd_upstreams_add_upstream (nls, "127.0.0.1", 0, NULL));
|
|
|
|
up = rspamd_upstream_get (nls, RSPAMD_UPSTREAM_RANDOM, NULL, 0);
|
|
|
|
rspamd_parse_inet_address (&paddr, "127.0.0.2", 0);
|
|
|
|
g_assert (rspamd_upstream_add_addr (up, paddr));
|
|
|
|
rspamd_parse_inet_address (&paddr, "::1", 0);
|
|
|
|
g_assert (rspamd_upstream_add_addr (up, paddr));
|
|
|
|
/* Rewind to start */
|
|
|
|
addr = rspamd_upstream_addr (up);
|
|
|
|
addr = rspamd_upstream_addr (up);
|
|
|
|
/* cur should be zero here */
|
|
|
|
addr = rspamd_upstream_addr (up);
|
|
|
|
next_addr = rspamd_upstream_addr (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 (up);
|
|
|
|
g_assert (rspamd_inet_address_get_af (next_addr) == AF_INET6);
|
|
|
|
next_addr = rspamd_upstream_addr (up);
|
|
|
|
g_assert (rspamd_inet_address_get_af (next_addr) == AF_INET);
|
|
|
|
next_addr = rspamd_upstream_addr (up);
|
|
|
|
g_assert (rspamd_inet_address_get_af (next_addr) == AF_INET);
|
|
|
|
next_addr = rspamd_upstream_addr (up);
|
|
|
|
g_assert (rspamd_inet_address_get_af (next_addr) == AF_INET6);
|
|
|
|
/* Test errors with IPv6 */
|
|
|
|
rspamd_upstream_fail (up);
|
|
|
|
/* Now we should have merely IPv4 addresses in rotation */
|
|
|
|
addr = rspamd_upstream_addr (up);
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
next_addr = rspamd_upstream_addr (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) != 0);
|
|
|
|
addr = next_addr;
|
|
|
|
}
|
|
|
|
rspamd_upstreams_destroy (nls);
|
|
|
|
|
2015-11-20 17:33:34 +01:00
|
|
|
ls = rspamd_upstreams_create (cfg->ups_ctx);
|
2014-11-02 19:38:43 +01:00
|
|
|
g_assert (rspamd_upstreams_parse_line (ls, test_upstream_list, 443, NULL));
|
2014-11-03 12:41:07 +01:00
|
|
|
g_assert (rspamd_upstreams_count (ls) == 3);
|
2014-11-02 19:38:43 +01:00
|
|
|
|
|
|
|
/* 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");
|
|
|
|
|
|
|
|
/* Test round-robin rotation */
|
|
|
|
rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "kernel.org");
|
2014-11-02 20:03:15 +01:00
|
|
|
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");
|
2015-03-12 19:32:59 +01:00
|
|
|
rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "google.com");
|
|
|
|
rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "microsoft.com");
|
2014-11-02 22:42:53 +01:00
|
|
|
|
|
|
|
/* Test stable hashing */
|
2015-11-20 17:33:34 +01:00
|
|
|
nls = rspamd_upstreams_create (cfg->ups_ctx);
|
2014-11-02 22:42:53 +01:00
|
|
|
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 ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-03 11:23:32 +01:00
|
|
|
p = 1.0 - fabs (3.0 / 4.0 - (gdouble)success / (gdouble)assumptions);
|
2014-11-02 22:42:53 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-11-03 12:41:07 +01:00
|
|
|
msg_debug ("p value for hash consistency: %.6f", p);
|
2014-11-03 11:23:32 +01:00
|
|
|
g_assert (p > 0.9);
|
2014-11-03 12:41:07 +01:00
|
|
|
|
2014-11-07 14:33:45 +01:00
|
|
|
rspamd_upstreams_destroy (nls);
|
|
|
|
|
|
|
|
|
2014-11-03 12:41:07 +01:00
|
|
|
/* Upstream fail test */
|
|
|
|
evtimer_set (&ev, rspamd_upstream_timeout_handler, resolver);
|
|
|
|
event_base_set (ev_base, &ev);
|
|
|
|
|
2015-11-21 14:31:26 +01:00
|
|
|
up = rspamd_upstream_get (ls, RSPAMD_UPSTREAM_MASTER_SLAVE, NULL, 0);
|
2015-03-14 22:51:53 +01:00
|
|
|
for (i = 0; i < 100; i ++) {
|
|
|
|
rspamd_upstream_fail (up);
|
|
|
|
}
|
2014-11-03 12:41:07 +01:00
|
|
|
g_assert (rspamd_upstreams_alive (ls) == 2);
|
|
|
|
|
|
|
|
tv.tv_sec = 2;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
event_add (&ev, &tv);
|
|
|
|
|
|
|
|
event_base_loop (ev_base, 0);
|
|
|
|
g_assert (rspamd_upstreams_alive (ls) == 3);
|
2014-11-07 14:33:45 +01:00
|
|
|
|
|
|
|
rspamd_upstreams_destroy (ls);
|
2015-11-20 18:03:51 +01:00
|
|
|
REF_RELEASE (cfg);
|
2014-11-02 19:38:43 +01:00
|
|
|
}
|