From 045971cbb8d2544d3b077e3ed2c89d48aedd75a4 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 7 May 2015 12:25:11 +0100 Subject: [PATCH] Add unit test for cryptobox. --- test/CMakeLists.txt | 1 + test/rspamd_cryptobox_test.c | 106 +++++++++++++++++++++++++++++++++++ test/rspamd_test_suite.c | 2 + test/tests.h | 2 + 4 files changed, 111 insertions(+) create mode 100644 test/rspamd_cryptobox_test.c diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 89f70cc3d..839fc8798 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -11,6 +11,7 @@ SET(TESTSRC rspamd_mem_pool_test.c rspamd_upstream_test.c rspamd_http_test.c rspamd_lua_test.c + rspamd_cryptobox_test.c rspamd_test_suite.c) ADD_EXECUTABLE(rspamd-test EXCLUDE_FROM_ALL ${TESTSRC}) diff --git a/test/rspamd_cryptobox_test.c b/test/rspamd_cryptobox_test.c new file mode 100644 index 000000000..171ee7092 --- /dev/null +++ b/test/rspamd_cryptobox_test.c @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2015, 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" +#include "main.h" +#include "shingles.h" +#include "fstring.h" +#include "ottery.h" +#include "cryptobox.h" + +static const int mapping_size = 64 * 8192; +static const int max_seg = 1024; + +static void * +create_mapping (int mapping_len, guchar **beg, guchar **end) +{ + void *map; + int psize = getpagesize (); + + map = mmap (NULL, mapping_len + psize * 3, PROT_READ|PROT_WRITE, + MAP_ANON|MAP_SHARED, -1, 0); + g_assert (map != 0); + mprotect (map, psize, PROT_NONE); + /* Misalign pointer */ + *beg = ((guchar *)map) + psize + 1; + *end = *beg + mapping_len; + 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_sig_t mac, guchar *begin, guchar *end) +{ + guint64 *t = (guint64 *)begin; + + g_assert (rspamd_cryptobox_decrypt_nm_inplace (begin, end - begin, nonce, key, + mac)); + + while (t < (guint64 *)end) { + g_assert (*t == 0); + t ++; + } +} + +void +rspamd_cryptobox_test_func (void) +{ + void *map; + guchar *begin, *end; + rspamd_nm_t key; + rspamd_nonce_t nonce; + rspamd_sig_t mac; + struct rspamd_cryptobox_segment *seg; + double t1, t2; + + map = create_mapping (mapping_size, &begin, &end); + + ottery_rand_bytes (key, sizeof (key)); + ottery_rand_bytes (nonce, sizeof (nonce)); + + memset (mac, 0, sizeof (mac)); + memset (begin, 0, end - begin); + seg = g_slice_alloc0 (sizeof (*seg) * max_seg); + + /* Test baseline */ + 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); + + msg_info ("baseline encryption: %.6f", t2 - t1); + /* 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); + t2 = rspamd_get_ticks (); + + check_result (key, nonce, mac, begin, end); + + msg_info ("bulk encryption: %.6f", t2 - t1); +} diff --git a/test/rspamd_test_suite.c b/test/rspamd_test_suite.c index 004b1eb92..72c2b5db6 100644 --- a/test/rspamd_test_suite.c +++ b/test/rspamd_test_suite.c @@ -55,6 +55,8 @@ main (int argc, char **argv) g_test_add_func ("/rspamd/shingles", rspamd_shingles_test_func); g_test_add_func ("/rspamd/http", rspamd_http_test_func); g_test_add_func ("/rspamd/lua", rspamd_lua_test_func); + g_test_add_func ("/rspamd/crypto", rspamd_cryptobox_test_func); + g_test_add_func ("/rspamd/cryptobox", rspamd_cryptobox_test_func); g_test_run (); diff --git a/test/tests.h b/test/tests.h index 5f304d42c..f0dab9c02 100644 --- a/test/tests.h +++ b/test/tests.h @@ -40,4 +40,6 @@ void rspamd_http_test_func (void); void rspamd_lua_test_func (void); +void rspamd_cryptobox_test_func (void); + #endif -- 2.39.5