From ceb49038d5c315b6cd36fa8cbfb51a7105800f5d Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 7 Aug 2024 11:08:12 +0100 Subject: [PATCH] [Test] Fix unit tests --- test/rspamd_cxx_unit_cryptobox.hxx | 106 ++++++++++++++--------------- 1 file changed, 52 insertions(+), 54 deletions(-) diff --git a/test/rspamd_cxx_unit_cryptobox.hxx b/test/rspamd_cxx_unit_cryptobox.hxx index 028583042..b23cd138a 100644 --- a/test/rspamd_cxx_unit_cryptobox.hxx +++ b/test/rspamd_cxx_unit_cryptobox.hxx @@ -1,24 +1,25 @@ /* -* Copyright 2024 Vsevolod Stakhov -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Copyright 2024 Vsevolod Stakhov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ /* Detached unit tests for the cryptobox */ #ifndef RSPAMD_RSPAMD_CXX_UNIT_CRYPTOBOX_HXX #define RSPAMD_RSPAMD_CXX_UNIT_CRYPTOBOX_HXX #include "libcryptobox/cryptobox.h" +#include TEST_SUITE("rspamd_cryptobox") { @@ -43,46 +44,39 @@ TEST_SUITE("rspamd_cryptobox") TEST_CASE("rspamd_cryptobox_hash") { - rspamd_cryptobox_hash_state_t p; - const unsigned char *key = reinterpret_cast("key"); - gsize keylen = sizeof(key); + rspamd_cryptobox_hash_state_t p = {0}; + std::string key{"key"}; - memset(&p, 0, rspamd_cryptobox_HASHBYTES); - - rspamd_cryptobox_hash_init(&p, key, keylen); - - const unsigned char* data = reinterpret_cast("data"); - gsize len = sizeof(data); - - rspamd_cryptobox_hash_update(&p, data, len); - - unsigned char out1[rspamd_cryptobox_HASHSTATEBYTES]; + rspamd_cryptobox_hash_init(&p, reinterpret_cast(key.data()), key.size()); + std::string data{"key"}; + rspamd_cryptobox_hash_update(&p, reinterpret_cast(data.data()), data.size()); + unsigned char out1[rspamd_cryptobox_HASHBYTES]; rspamd_cryptobox_hash_final(&p, out1); - unsigned char out2[rspamd_cryptobox_HASHSTATEBYTES]; - - rspamd_cryptobox_hash(out2, data, len, key, keylen); - CHECK(strcmp((char *)out1, (char *)out2) == 0); + unsigned char out2[rspamd_cryptobox_HASHBYTES]; + rspamd_cryptobox_hash(out2, + reinterpret_cast(data.data()), data.size(), + reinterpret_cast(key.data()), key.size()); + CHECK(memcmp(out1, out2, sizeof(out1)) == 0); } TEST_CASE("rspamd_cryptobox_fast_hash") { rspamd_cryptobox_fast_hash_state_s *st = rspamd_cryptobox_fast_hash_new(); - uint64_t seed = 10; - rspamd_cryptobox_fast_hash_init(st, seed); + std::string data{"key"}; - const unsigned char* data = reinterpret_cast("data"); - gsize len = sizeof(data); - - rspamd_cryptobox_fast_hash_update(st, data, len); + rspamd_cryptobox_fast_hash_update(st, + reinterpret_cast(data.data()), + data.size()); uint64_t out1 = rspamd_cryptobox_fast_hash_final(st); - CHECK(out1 == 7343692543952389622); + CHECK(out1 == 358126267837521635); - uint64_t out2 = rspamd_cryptobox_fast_hash(data, len, seed); + uint64_t out2 = rspamd_cryptobox_fast_hash(reinterpret_cast(data.data()), + data.size(), seed); CHECK(out1 == out2); rspamd_cryptobox_fast_hash_free(st); @@ -90,30 +84,35 @@ TEST_SUITE("rspamd_cryptobox") TEST_CASE("rspamd_cryptobox_pbkdf") { - const char *pass = "passpa"; - gsize pass_len = sizeof(pass); - - const uint8_t *salt = reinterpret_cast("salt"); - gsize salt_len = sizeof(salt); + std::string pass{"passpa"}; + std::string salt{"salt"}; - uint8_t key1[256]; + uint8_t key1[256] = {0}; gsize key_len1 = sizeof(key1); - uint8_t key2[256]; + uint8_t key2[256] = {0}; gsize key_len2 = sizeof(key2); unsigned int complexity = 10; enum rspamd_cryptobox_pbkdf_type type = RSPAMD_CRYPTOBOX_PBKDF2; - CHECK(rspamd_cryptobox_pbkdf(pass, pass_len, salt, salt_len, key1, key_len1, complexity, type)); - CHECK(rspamd_cryptobox_pbkdf(pass, pass_len, salt, salt_len, key2, key_len2, complexity, type)); - CHECK(strcmp((char *)key1, (char *)key2) == 0); + CHECK(rspamd_cryptobox_pbkdf(pass.data(), pass.size(), + reinterpret_cast(salt.data()), salt.size(), + key1, key_len1, complexity, type)); + CHECK(rspamd_cryptobox_pbkdf(pass.data(), pass.size(), + reinterpret_cast(salt.data()), salt.size(), + key2, key_len2, complexity, type)); + CHECK(memcmp(key1, key2, key_len1) == 0); type = RSPAMD_CRYPTOBOX_CATENA; - CHECK(rspamd_cryptobox_pbkdf(pass, pass_len, salt, salt_len, key1, key_len1, complexity, type)); - CHECK(rspamd_cryptobox_pbkdf(pass, pass_len, salt, salt_len, key2, key_len2, complexity, type)); - CHECK(strcmp((char *)key1, (char *)key2) == 0); + CHECK(rspamd_cryptobox_pbkdf(pass.data(), pass.size(), + reinterpret_cast(salt.data()), salt.size(), + key1, key_len1, complexity, type)); + CHECK(rspamd_cryptobox_pbkdf(pass.data(), pass.size(), + reinterpret_cast(salt.data()), salt.size(), + key2, key_len2, complexity, type)); + CHECK(memcmp(key1, key2, key_len1) == 0); } @@ -147,7 +146,7 @@ TEST_SUITE("rspamd_cryptobox") unsigned char sig[256]; unsigned long long siglen; const unsigned char m[] = "data to be signed"; - size_t mlen = strlen((const char*)m); + size_t mlen = strlen((const char *) m); rspamd_cryptobox_keypair(pk, sk, mode); @@ -155,7 +154,6 @@ TEST_SUITE("rspamd_cryptobox") bool check_result = rspamd_cryptobox_verify(sig, siglen, m, mlen, pk, mode); CHECK(check_result == true); } - } #endif -- 2.39.5