Fix tests and Add tests
This commit is contained in:
@@ -19,6 +19,7 @@ struct SignedMessage {
|
|||||||
std::string plaintext);
|
std::string plaintext);
|
||||||
|
|
||||||
std::vector<uint8_t> serialize();
|
std::vector<uint8_t> serialize();
|
||||||
|
// bool valid();
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Protocol
|
} // namespace Protocol
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#include <sodium.h>
|
#include <sodium.h>
|
||||||
|
|
||||||
TEST_CASE("SignedMessage", "[protocol]") {
|
TEST_CASE("SignedMessage", "[protocol]") {
|
||||||
SECTION("sodium init") REQUIRE(sodium_init() >= 0);
|
REQUIRE(sodium_init() >= 0);
|
||||||
|
|
||||||
unsigned char pub_key[crypto_sign_PUBLICKEYBYTES];
|
unsigned char pub_key[crypto_sign_PUBLICKEYBYTES];
|
||||||
unsigned char priv_key[crypto_sign_SECRETKEYBYTES];
|
unsigned char priv_key[crypto_sign_SECRETKEYBYTES];
|
||||||
@@ -13,9 +13,30 @@ TEST_CASE("SignedMessage", "[protocol]") {
|
|||||||
auto msg = Protocol::SignedMessage(pub_key, priv_key, "Hello, world!");
|
auto msg = Protocol::SignedMessage(pub_key, priv_key, "Hello, world!");
|
||||||
auto to_be_signed = msg.serialize();
|
auto to_be_signed = msg.serialize();
|
||||||
|
|
||||||
SECTION("signed message") {
|
SECTION("valid signed message") {
|
||||||
REQUIRE(
|
REQUIRE(
|
||||||
crypto_sign_verify_detached(msg.signature, to_be_signed.data(),
|
crypto_sign_verify_detached(msg.signature, to_be_signed.data(),
|
||||||
to_be_signed.size(), pub_key) == 0);
|
to_be_signed.size(), pub_key) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("tampered invalid signed message") {
|
||||||
|
// todo: use to msg.valid()
|
||||||
|
SECTION("plaintext") {
|
||||||
|
msg.plaintext = "Hello, world111111";
|
||||||
|
to_be_signed = msg.serialize();
|
||||||
|
REQUIRE_FALSE(crypto_sign_verify_detached(
|
||||||
|
msg.signature, to_be_signed.data(),
|
||||||
|
to_be_signed.size(), pub_key) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("timestamp") {
|
||||||
|
msg.timestamp = std::chrono::system_clock::now()
|
||||||
|
.time_since_epoch()
|
||||||
|
.count();
|
||||||
|
to_be_signed = msg.serialize();
|
||||||
|
REQUIRE_FALSE(crypto_sign_verify_detached(
|
||||||
|
msg.signature, to_be_signed.data(),
|
||||||
|
to_be_signed.size(), pub_key) == 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user