Files
MicrOBU/obu-firmware/external/vanetza-idf/vanetza/security/v2/tests/persistence.cpp
T
Ashin Walpola d107534eb2 Keep vanetza-idf in obu-firmware, so a plain clone builds the firmware
obu-firmware builds against the vanetza-idf C-ITS library, which until now
came from the colleague's microbu-esp32c5 tree beside the repository and was
not tracked here, so a clone of this repository could not build the firmware
it ships. The library alone is now part of obu-firmware, as
obu-firmware/external/vanetza-idf: their external/vanetza-idf at commit
cf4b99f, unchanged (9775 files; see its PROVENANCE.md). CMake takes it from
there by default; -DVANETZA_IDF_DIR still points the build elsewhere.

The rest of the colleague's tree (their own VAM firmware, PKI tooling,
station-link Python tools, the V2X2MAP bridge) stays out of this repository
and gitignored; nothing is pushed to their repository. NOTES.md, docs/06,
TODO.md and the pcap verifier's usage line point at the new location.
2026-09-24 10:56:05 +02:00

91 lines
2.6 KiB
C++

#include <vanetza/security/v2/persistence.hpp>
#include <gtest/gtest.h>
#include <array>
#include <cstdint>
#include <cstdio>
#include <fstream>
#include <iterator>
#include <sstream>
using namespace vanetza::security;
#define WRITEABLE(path) WORK_DIR "/" path
#define ASSET(path) ASSET_DIR "/" path
namespace
{
const std::array<uint8_t, 32> expected_private_key = {
0x53, 0xb7, 0x7e, 0xb8, 0x48, 0x2e, 0x3c, 0x1a,
0xd3, 0x36, 0x70, 0xc0, 0xc6, 0xc4, 0x6b, 0xc0,
0x36, 0x90, 0xd4, 0x00, 0x59, 0xd3, 0xcb, 0xb5,
0x81, 0xb3, 0x36, 0xaf, 0x8a, 0x98, 0x93, 0xf4
};
const std::array<uint8_t, 32> expected_public_x = {
0x1c, 0x85, 0x0d, 0xc7, 0x45, 0x63, 0x29, 0x3c,
0xb0, 0xf3, 0xe5, 0x5e, 0xda, 0x7b, 0x10, 0xec,
0xb4, 0xe9, 0x74, 0x6f, 0x83, 0x6f, 0x84, 0x76,
0x96, 0xc3, 0x1e, 0xe8, 0x68, 0x4e, 0x37, 0x76
};
const std::array<uint8_t, 32> expected_public_y = {
0x28, 0xf1, 0x67, 0xfb, 0x64, 0xce, 0x7b, 0x79,
0xa6, 0x02, 0x06, 0x2a, 0xac, 0x11, 0x7f, 0x59,
0x6b, 0xac, 0x77, 0xc7, 0x1c, 0xd6, 0xf4, 0xca,
0xa7, 0x08, 0x7a, 0xcc, 0xcd, 0xab, 0x91, 0xab
};
void check_key_pair(const ecdsa256::KeyPair& kp)
{
EXPECT_EQ(kp.private_key.key, expected_private_key);
EXPECT_EQ(kp.public_key.x, expected_public_x);
EXPECT_EQ(kp.public_key.y, expected_public_y);
}
ecdsa256::KeyPair make_test_key_pair()
{
ecdsa256::KeyPair kp;
kp.private_key.key = expected_private_key;
kp.public_key.x = expected_public_x;
kp.public_key.y = expected_public_y;
return kp;
}
std::string read_file_bytes(const std::string& path)
{
std::ifstream file(path, std::ios::binary);
return {std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()};
}
} // namespace
#if defined VANETZA_WITH_OPENSSL || defined VANETZA_WITH_CRYPTOPP
TEST(Persistence, load_private_key_from_file)
{
check_key_pair(v2::load_private_key_from_file(ASSET("test_key.der")));
}
TEST(Persistence, save_and_load_pkcs8_der)
{
auto kp = make_test_key_pair();
const std::string path = WRITEABLE("test_save.der");
std::ofstream ofs(path, std::ios::binary);
EXPECT_TRUE(v2::save_private_key_pkcs8_der(ofs, kp));
ofs.flush();
check_key_pair(v2::load_private_key_from_file(path));
std::remove(path.c_str());
}
TEST(Persistence, save_der_matches_reference_file)
{
auto kp = make_test_key_pair();
std::ostringstream oss(std::ios::binary);
EXPECT_TRUE(v2::save_private_key_pkcs8_der(oss, kp));
auto reference = read_file_bytes(ASSET("test_key.der"));
EXPECT_EQ(oss.str(), reference);
}
#endif /* VANETZA_WITH_OPENSSL || VANETZA_WITH_CRYPTOPP */