aboutsummaryrefslogtreecommitdiffstats
path: root/test/rspamd_cryptobox_test.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@rspamd.com>2023-07-26 10:49:23 +0100
committerVsevolod Stakhov <vsevolod@rspamd.com>2023-07-26 10:49:23 +0100
commit537a7180a0d5132c11636c4fd8b1450cd99d352c (patch)
treefb9f8c84955a411bdffbd6371ea32f2716fb3687 /test/rspamd_cryptobox_test.c
parent5fd7a90fdaa33f52c59bdb0ca84451e5c1e22365 (diff)
downloadrspamd-537a7180a0d5132c11636c4fd8b1450cd99d352c.tar.gz
rspamd-537a7180a0d5132c11636c4fd8b1450cd99d352c.zip
[Rework] Use clang-format to unify formatting in all sources
No meaningful changes.
Diffstat (limited to 'test/rspamd_cryptobox_test.c')
-rw-r--r--test/rspamd_cryptobox_test.c269
1 files changed, 134 insertions, 135 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);
}
}