From 408ebf516d8d4c6c945b2126173d7de08ab29d3f Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 11 Feb 2016 09:07:05 +0000 Subject: [PATCH] Fix tests --- test/rspamd_cryptobox_test.c | 57 ++++++----- test/rspamd_http_test.c | 193 ++++++++++++++++++----------------- 2 files changed, 131 insertions(+), 119 deletions(-) diff --git a/test/rspamd_cryptobox_test.c b/test/rspamd_cryptobox_test.c index 3d6d2b2be..b7cbe2a16 100644 --- a/test/rspamd_cryptobox_test.c +++ b/test/rspamd_cryptobox_test.c @@ -24,6 +24,7 @@ static const int mapping_size = 64 * 8192 + 1; static const int max_seg = 32; 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) @@ -51,7 +52,7 @@ check_result (const rspamd_nm_t key, const rspamd_nonce_t nonce, guint64 *t = (guint64 *)begin; g_assert (rspamd_cryptobox_decrypt_nm_inplace (begin, end - begin, nonce, key, - mac)); + mac, mode)); while (t < (guint64 *)end) { g_assert (*t == 0); @@ -174,32 +175,33 @@ rspamd_cryptobox_test_func (void) /* Test baseline */ t1 = rspamd_get_ticks (); - rspamd_cryptobox_encrypt_nm_inplace (begin, end - begin, nonce, key, mac); + rspamd_cryptobox_encrypt_nm_inplace (begin, end - begin, nonce, key, mac, + mode); t2 = rspamd_get_ticks (); check_result (key, nonce, mac, begin, end); msg_info ("baseline encryption: %.6f", t2 - t1); - if (rspamd_cryptobox_openssl_mode (TRUE)) { - t1 = rspamd_get_ticks (); - rspamd_cryptobox_encrypt_nm_inplace (begin, - end - begin, - nonce, - key, - mac); - t2 = rspamd_get_ticks (); - check_result (key, nonce, mac, begin, end); + mode = RSPAMD_CRYPTOBOX_MODE_NIST; + t1 = rspamd_get_ticks (); + rspamd_cryptobox_encrypt_nm_inplace (begin, + end - begin, + nonce, + key, + mac, + mode); + t2 = rspamd_get_ticks (); + check_result (key, nonce, mac, begin, end); - msg_info ("openssl baseline encryption: %.6f", t2 - t1); - rspamd_cryptobox_openssl_mode (FALSE); - } + msg_info ("openssl baseline encryption: %.6f", 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 (); - rspamd_cryptobox_encryptv_nm_inplace (seg, 1, nonce, key, mac); + rspamd_cryptobox_encryptv_nm_inplace (seg, 1, nonce, key, mac, mode); t2 = rspamd_get_ticks (); check_result (key, nonce, mac, begin, end); @@ -212,7 +214,7 @@ start: seg[1].data = begin + seg[0].len; seg[1].len = (end - begin) - seg[0].len; t1 = rspamd_get_ticks (); - rspamd_cryptobox_encryptv_nm_inplace (seg, 2, nonce, key, mac); + rspamd_cryptobox_encryptv_nm_inplace (seg, 2, nonce, key, mac, mode); t2 = rspamd_get_ticks (); check_result (key, nonce, mac, begin, end); @@ -224,7 +226,7 @@ start: seg[1].data = begin + seg[0].len; seg[1].len = (end - begin) - seg[0].len; t1 = rspamd_get_ticks (); - rspamd_cryptobox_encryptv_nm_inplace (seg, 2, nonce, key, mac); + rspamd_cryptobox_encryptv_nm_inplace (seg, 2, nonce, key, mac, mode); t2 = rspamd_get_ticks (); check_result (key, nonce, mac, begin, end); @@ -236,7 +238,7 @@ start: seg[1].data = begin + seg[0].len; seg[1].len = (end - begin) - seg[0].len; t1 = rspamd_get_ticks (); - rspamd_cryptobox_encryptv_nm_inplace (seg, 2, nonce, key, mac); + rspamd_cryptobox_encryptv_nm_inplace (seg, 2, nonce, key, mac, mode); t2 = rspamd_get_ticks (); check_result (key, nonce, mac, begin, end); @@ -249,7 +251,7 @@ start: seg[1].data = begin + seg[0].len; seg[1].len = (end - begin) - seg[0].len; t1 = rspamd_get_ticks (); - rspamd_cryptobox_encryptv_nm_inplace (seg, 2, nonce, key, mac); + rspamd_cryptobox_encryptv_nm_inplace (seg, 2, nonce, key, mac, mode); t2 = rspamd_get_ticks (); check_result (key, nonce, mac, begin, end); @@ -264,7 +266,7 @@ start: 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 (); - rspamd_cryptobox_encryptv_nm_inplace (seg, 3, nonce, key, mac); + rspamd_cryptobox_encryptv_nm_inplace (seg, 3, nonce, key, mac, mode); t2 = rspamd_get_ticks (); check_result (key, nonce, mac, begin, end); @@ -273,7 +275,7 @@ start: cnt = create_random_split (seg, max_seg, begin, end); t1 = rspamd_get_ticks (); - rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac); + rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac, mode); t2 = rspamd_get_ticks (); check_result (key, nonce, mac, begin, end); @@ -282,7 +284,7 @@ start: cnt = create_realistic_split (seg, max_seg, begin, end); t1 = rspamd_get_ticks (); - rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac); + rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac, mode); t2 = rspamd_get_ticks (); check_result (key, nonce, mac, begin, end); @@ -291,7 +293,7 @@ start: cnt = create_constrainted_split (seg, max_seg + 1, 32, begin, end); t1 = rspamd_get_ticks (); - rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac); + rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac, mode); t2 = rspamd_get_ticks (); check_result (key, nonce, mac, begin, end); @@ -302,7 +304,7 @@ start: ms = ottery_rand_range (i % max_seg * 2) + 1; cnt = create_random_split (seg, ms, begin, end); t1 = rspamd_get_ticks (); - rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac); + rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac, mode); t2 = rspamd_get_ticks (); check_result (key, nonce, mac, begin, end); @@ -315,7 +317,7 @@ start: ms = ottery_rand_range (i % max_seg * 2) + 1; cnt = create_realistic_split (seg, ms, begin, end); t1 = rspamd_get_ticks (); - rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac); + rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac, mode); t2 = rspamd_get_ticks (); check_result (key, nonce, mac, begin, end); @@ -328,7 +330,7 @@ start: ms = ottery_rand_range (i % max_seg * 10) + 1; cnt = create_constrainted_split (seg, ms, i, begin, end); t1 = rspamd_get_ticks (); - rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac); + rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac, mode); t2 = rspamd_get_ticks (); check_result (key, nonce, mac, begin, end); @@ -338,8 +340,9 @@ start: } } - if (!checked_openssl && rspamd_cryptobox_openssl_mode (TRUE)) { + if (!checked_openssl) { checked_openssl = TRUE; + mode = RSPAMD_CRYPTOBOX_MODE_NIST; goto start; } } diff --git a/test/rspamd_http_test.c b/test/rspamd_http_test.c index ce7735379..d14b80894 100644 --- a/test/rspamd_http_test.c +++ b/test/rspamd_http_test.c @@ -20,6 +20,7 @@ #include "tests.h" #include "ottery.h" #include "cryptobox.h" +#include "keypair.h" #include "unix-std.h" #include @@ -78,7 +79,7 @@ rspamd_http_term_handler (gint fd, short what, void *arg) static void rspamd_http_server_func (gint fd, const gchar *path, rspamd_inet_addr_t *addr, - gpointer kp, struct rspamd_keypair_cache *c) + struct rspamd_cryptobox_keypair *kp, struct rspamd_keypair_cache *c) { struct rspamd_http_connection_router *rt; struct event_base *ev_base = event_init (); @@ -142,7 +143,9 @@ rspamd_client_finish (struct rspamd_http_connection *conn, static void rspamd_http_client_func (const gchar *path, rspamd_inet_addr_t *addr, - gpointer kp, gpointer peer_kp, struct rspamd_keypair_cache *c, + struct rspamd_cryptobox_keypair *kp, + struct rspamd_cryptobox_pubkey *peer_kp, + struct rspamd_keypair_cache *c, struct event_base *ev_base, double *latency) { struct rspamd_http_message *msg; @@ -163,7 +166,7 @@ rspamd_http_client_func (const gchar *path, rspamd_inet_addr_t *addr, if (kp != NULL) { g_assert (peer_kp != NULL); rspamd_http_connection_set_key (conn, kp); - msg->peer_key = rspamd_http_connection_key_ref (peer_kp); + msg->peer_key = rspamd_pubkey_ref (peer_kp); } cb = g_malloc (sizeof (*cb)); @@ -207,7 +210,8 @@ rspamd_http_calculate_mean (double *lats, double *std) static void rspamd_http_start_servers (pid_t *sfd, rspamd_inet_addr_t *addr, - gpointer serv_key, struct rspamd_keypair_cache *c) + struct rspamd_cryptobox_keypair *serv_key, + struct rspamd_keypair_cache *c) { guint i; gint fd; @@ -246,7 +250,8 @@ rspamd_http_test_func (void) { struct event_base *ev_base = event_init (); rspamd_mempool_t *pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL); - gpointer serv_key, client_key, peer_key; + struct rspamd_cryptobox_keypair *serv_key, *client_key; + struct rspamd_cryptobox_pubkey *peer_key; struct rspamd_keypair_cache *c; rspamd_mempool_mutex_t *mtx; rspamd_inet_addr_t *addr; @@ -289,8 +294,10 @@ rspamd_http_test_func (void) rspamd_parse_inet_address (&addr, "127.0.0.1", 0); rspamd_inet_address_set_port (addr, 43898); - serv_key = rspamd_http_connection_gen_key (); - client_key = rspamd_http_connection_gen_key (); + 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); @@ -325,10 +332,11 @@ rspamd_http_test_func (void) //rspamd_mempool_lock_mutex (mtx); usleep (100000); - b32_key = rspamd_http_connection_print_key (serv_key, + b32_key = rspamd_keypair_print (serv_key, RSPAMD_KEYPAIR_PUBKEY|RSPAMD_KEYPAIR_BASE32); g_assert (b32_key != NULL); - peer_key = rspamd_http_connection_make_peer_key (b32_key->str); + 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; @@ -385,92 +393,93 @@ rspamd_http_test_func (void) mean, std); /* AES mode */ - if (rspamd_cryptobox_openssl_mode (TRUE)) { - serv_key = rspamd_http_connection_gen_key (); - client_key = rspamd_http_connection_gen_key (); - c = rspamd_keypair_cache_new (16); - - /* Restart server */ - rspamd_http_stop_servers (sfd); - /* No keypairs cache */ - rspamd_http_start_servers (sfd, addr, serv_key, c); - - //rspamd_mempool_lock_mutex (mtx); - usleep (100000); - b32_key = rspamd_http_connection_print_key (serv_key, - RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32); - g_assert (b32_key != NULL); - peer_key = rspamd_http_connection_make_peer_key (b32_key->str); - g_assert (peer_key != NULL); - total_diff = 0.0; - - 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]); - } - ts1 = rspamd_get_ticks (); - event_base_loop (ev_base, 0); - ts2 = rspamd_get_ticks (); - diff = (ts2 - ts1) * 1000.0; - total_diff += diff; + 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); + /* No keypairs cache */ + 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); + total_diff = 0.0; + + 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]); } - 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); - /* No keypairs cache */ - rspamd_http_start_servers (sfd, addr, serv_key, NULL); - - //rspamd_mempool_lock_mutex (mtx); - usleep (100000); - total_diff = 0.0; - - 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]); - } - ts1 = rspamd_get_ticks (); - event_base_loop (ev_base, 0); - ts2 = rspamd_get_ticks (); - diff = (ts2 - ts1) * 1000.0; - total_diff += diff; + ts1 = rspamd_get_ticks (); + event_base_loop (ev_base, 0); + ts2 = rspamd_get_ticks (); + 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); + + /* Restart server */ + rspamd_http_stop_servers (sfd); + /* No keypairs cache */ + rspamd_http_start_servers (sfd, addr, serv_key, NULL); + + //rspamd_mempool_lock_mutex (mtx); + usleep (100000); + total_diff = 0.0; + + 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]); } - 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); + ts1 = rspamd_get_ticks (); + event_base_loop (ev_base, 0); + ts2 = rspamd_get_ticks (); + 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); -- 2.39.5