Files
Library/tests/standalone/Security/Security.cpp
T
faumaray 8583a479dd
CI / GCC 16 / C++26 / default (push) Has been cancelled
CI / GCC 16 / C++26 / sanitizers (push) Has been cancelled
CI / GCC 16 / C++26 / werror (push) Has been cancelled
Update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-15 19:49:06 +03:00

96 lines
3.1 KiB
C++

#include <Void/Security/Security.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Suite.hpp>
#include <string>
namespace
{
using Void::Security::PasswordHashStrength;
using Void::Testing::Test;
struct SecurityTests : Void::Testing::Suite<SecurityTests> {
Test random_tokens_are_url_safe_and_unique()
{
auto first = Void::Security::random_token();
auto second = Void::Security::random_token();
Void::Testing::require(first.has_value());
Void::Testing::require(second.has_value());
Void::Testing::expect_eq(first->size(),
static_cast<std::size_t>(43));
Void::Testing::expect(first->find_first_of("+/=") ==
std::string::npos);
Void::Testing::expect_ne(*first, *second);
}
Test random_token_rejects_zero_entropy()
{
Void::Testing::expect(
!Void::Security::random_token(0).has_value());
}
Test token_hash_is_deterministic_and_verifies_in_constant_time()
{
auto first = Void::Security::hash_token("session-secret");
auto second = Void::Security::hash_token("session-secret");
Void::Testing::require(first.has_value());
Void::Testing::require(second.has_value());
Void::Testing::expect_eq(*first, *second);
Void::Testing::expect_eq(first->size(),
static_cast<std::size_t>(64));
auto valid =
Void::Security::verify_token("session-secret", *first);
auto invalid =
Void::Security::verify_token("other-secret", *first);
Void::Testing::require(valid.has_value());
Void::Testing::require(invalid.has_value());
Void::Testing::expect(*valid);
Void::Testing::expect(!*invalid);
}
Test token_verification_rejects_malformed_digest()
{
auto malformed =
Void::Security::verify_token("token", "not-hex");
Void::Testing::require(malformed.has_value());
Void::Testing::expect(!*malformed);
}
Test password_hash_uses_argon2id_and_random_salts()
{
auto first = Void::Security::hash_password(
"correct horse battery staple",
PasswordHashStrength::Interactive);
auto second = Void::Security::hash_password(
"correct horse battery staple",
PasswordHashStrength::Interactive);
Void::Testing::require(first.has_value());
Void::Testing::require(second.has_value());
Void::Testing::expect(first->starts_with("$argon2id$"));
Void::Testing::expect_ne(*first, *second);
}
Test password_verification_accepts_only_the_original_password()
{
auto encoded = Void::Security::hash_password(
"correct horse battery staple",
PasswordHashStrength::Interactive);
Void::Testing::require(encoded.has_value());
auto valid = Void::Security::verify_password(
"correct horse battery staple", *encoded);
auto invalid =
Void::Security::verify_password("incorrect", *encoded);
auto malformed = Void::Security::verify_password(
"correct horse battery staple", "$argon2id$broken");
Void::Testing::require(valid.has_value());
Void::Testing::require(invalid.has_value());
Void::Testing::require(malformed.has_value());
Void::Testing::expect(*valid);
Void::Testing::expect(!*invalid);
Void::Testing::expect(!*malformed);
}
};
} // namespace