54 lines
1.9 KiB
C++
54 lines
1.9 KiB
C++
#include "sodium/crypto_sign.h"
|
|
#include <catch2/catch_test_macros.hpp>
|
|
#include <protocol/protocol.hxx>
|
|
#include <sodium.h>
|
|
|
|
TEST_CASE("Protocol::StunReplay", "[protocol]") {
|
|
SECTION("str to ipv4") {
|
|
auto replay = Protocol::StunReplay("1.1.255.255", 1337);
|
|
REQUIRE(replay.addr == 16908287);
|
|
}
|
|
SECTION("invalid ipv4 str (range)") {
|
|
REQUIRE_THROWS_AS(Protocol::StunReplay("1.1.255.256", 1337),
|
|
std::runtime_error);
|
|
REQUIRE_THROWS_AS(Protocol::StunReplay("0.1.255.255", 1337),
|
|
std::runtime_error);
|
|
}
|
|
SECTION("invalid ipv4 str (len)") {
|
|
REQUIRE_THROWS_AS(Protocol::StunReplay("1.1.1", 1337),
|
|
std::runtime_error);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("Protocol::SignedMessage", "[protocol]") {
|
|
REQUIRE(sodium_init() >= 0);
|
|
|
|
unsigned char pub_key[crypto_sign_PUBLICKEYBYTES];
|
|
unsigned char priv_key[crypto_sign_SECRETKEYBYTES];
|
|
crypto_sign_keypair(pub_key, priv_key);
|
|
|
|
auto msg = Protocol::SignedMessage(pub_key, priv_key, "Hello, world!");
|
|
|
|
SECTION("valid signed message") { REQUIRE(msg.valid(pub_key)); }
|
|
|
|
SECTION("tampered invalid signed message") {
|
|
SECTION("plaintext") {
|
|
msg.plaintext = "Hello, world111111";
|
|
REQUIRE_FALSE(msg.valid(pub_key));
|
|
}
|
|
|
|
SECTION("timestamp") {
|
|
msg.timestamp = std::chrono::system_clock::now()
|
|
.time_since_epoch()
|
|
.count();
|
|
REQUIRE_FALSE(msg.valid(pub_key));
|
|
}
|
|
|
|
SECTION("fingerprint") {
|
|
std::memset(msg.sender_key_fingerprint, 'H',
|
|
crypto_hash_sha256_BYTES);
|
|
REQUIRE_FALSE(msg.valid(pub_key));
|
|
}
|
|
}
|
|
}
|