Files

132 lines
4.6 KiB
C++

#include <Void/Security/Security.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Suite.hpp>
#include <string>
#include <vector>
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);
}
Test sealed_secrets_round_trip_and_use_random_nonces()
{
auto key = Void::Security::random_secret_key();
Void::Testing::require(key.has_value());
const std::vector<unsigned char> plaintext{'s', 'e', 'c',
'r', 'e', 't'};
auto first = Void::Security::seal_secret(plaintext, *key);
auto second = Void::Security::seal_secret(plaintext, *key);
Void::Testing::require(first.has_value());
Void::Testing::require(second.has_value());
Void::Testing::expect_ne(*first, *second);
auto opened = Void::Security::open_secret(*first, *key);
Void::Testing::require(opened.has_value());
Void::Testing::expect_eq(*opened, plaintext);
}
Test sealed_secrets_reject_tampering_and_wrong_keys()
{
auto key = Void::Security::random_secret_key();
auto wrong = Void::Security::random_secret_key();
Void::Testing::require(key.has_value());
Void::Testing::require(wrong.has_value());
const std::vector<unsigned char> plaintext{'t', 'o', 'k', 'e',
'n'};
auto envelope = Void::Security::seal_secret(plaintext, *key);
Void::Testing::require(envelope.has_value());
auto wrong_key = Void::Security::open_secret(*envelope, *wrong);
Void::Testing::expect(!wrong_key.has_value());
envelope->back() ^= 0x01U;
auto tampered = Void::Security::open_secret(*envelope, *key);
Void::Testing::expect(!tampered.has_value());
}
};
} // namespace