53 lines
1.5 KiB
C++
53 lines
1.5 KiB
C++
#include "check.hpp"
|
|
#include "echoion/protocol/Crypto.hpp"
|
|
#include "echoion/protocol/Identity.hpp"
|
|
#include "echoion/protocol/Util.hpp"
|
|
|
|
#include <cstdio>
|
|
#include <string>
|
|
|
|
using namespace Echoion::Protocol;
|
|
|
|
TEST_CASE(identity_generate_uid_and_level)
|
|
{
|
|
auto id = Identity::generate(8);
|
|
|
|
// uid must equal base64(sha1(ascii(publicKeyString))).
|
|
auto h = Crypto::sha1(Crypto::ByteView(
|
|
reinterpret_cast<const uint8_t*>(id.publicKeyString().data()),
|
|
id.publicKeyString().size()));
|
|
CHECK_EQ(id.uid(),
|
|
Util::base64Encode(Crypto::ByteView(h.data(), h.size())));
|
|
|
|
// publicKeyString is valid base64 of a DER SEQUENCE (starts with 0x30).
|
|
auto der = Util::base64Decode(id.publicKeyString());
|
|
CHECK(!der.empty());
|
|
CHECK_EQ(der[0], uint8_t(0x30));
|
|
|
|
// Security level actually reached the requested threshold.
|
|
CHECK(id.securityLevel() >= 8);
|
|
}
|
|
|
|
TEST_CASE(identity_sign_verifiable_length)
|
|
{
|
|
auto id = Identity::generate(0);
|
|
std::string msg = "hello";
|
|
auto sig = id.sign(Crypto::ByteView(
|
|
reinterpret_cast<const uint8_t*>(msg.data()), msg.size()));
|
|
// DER ECDSA signature over P-256 is ~70-72 bytes; never empty.
|
|
CHECK(!sig.empty());
|
|
CHECK(sig[0] == 0x30); // DER SEQUENCE
|
|
}
|
|
|
|
TEST_CASE(identity_save_load_roundtrip)
|
|
{
|
|
auto id = Identity::generate(0);
|
|
std::string path = "/tmp/echoion_identity_test.txt";
|
|
id.save(path);
|
|
auto loaded = Identity::load(path);
|
|
CHECK_EQ(loaded.publicKeyString(), id.publicKeyString());
|
|
CHECK_EQ(loaded.uid(), id.uid());
|
|
CHECK_EQ(loaded.keyOffset(), id.keyOffset());
|
|
std::remove(path.c_str());
|
|
}
|