Files
MicrOBU/microbu-esp32c5/external/vanetza-idf/vanetza/security/v2/tests/int_x.cpp
T
Ashin Walpola 0e9525162d Keep the colleague's microbu-esp32c5 tree in this repository
obu-firmware builds against vanetza-idf from microbu-esp32c5/external, but
that tree was gitignored, so a clone of this repository could not build the
firmware it ships. It is now committed here as ordinary files in its own
folder, microbu-esp32c5/: the colleague's commit cf4b99f plus the V2X2MAP
bridge's signature verification (--trust) used on the bench. Nothing is
fetched from or pushed to the colleague's repository; this repository and
its remotes carry everything. The folder's own .gitignore keeps build output,
downloaded components and private key material out, as it did there; the
committed file set is identical to that repository's tracked files.

The ESP32-C5 is still flashed from obu-firmware/, which only takes
vanetza-idf from microbu-esp32c5/, so the two stay separate folders.
FLASHING.md says how to take a newer version of the colleague's tree (copy
it over the folder, rebuild, test, commit).
2026-09-23 17:46:40 +02:00

108 lines
2.1 KiB
C++

#include <vanetza/security/v2/int_x.hpp>
#include <vanetza/security/v2/length_coding.hpp>
#include <vanetza/security/tests/serialization.hpp>
#include <gtest/gtest.h>
using vanetza::ByteBuffer;
using vanetza::security::v2::IntX;
TEST(IntX, set_and_get)
{
IntX a;
EXPECT_EQ(0, a.get());
a.set(static_cast<int8_t>(89));
EXPECT_EQ(89, a.get());
a.set(static_cast<uint32_t>(0x12345678));
EXPECT_EQ(0x12345678, a.get());
}
TEST(IntX, get_size)
{
IntX a;
EXPECT_EQ(1, get_size(a));
a.set(static_cast<int8_t>(89));
EXPECT_EQ(1, get_size(a));
a.set(127);
EXPECT_EQ(1, get_size(a));
a.set(128);
EXPECT_EQ(2, get_size(a));
a.set(0x23);
EXPECT_EQ(1, get_size(a));
a.set(static_cast<uint32_t>(0x00130033));
EXPECT_EQ(3, get_size(a));
a.set(static_cast<uint32_t>(0x00230033));
EXPECT_EQ(4, get_size(a));
a.set(static_cast<uint32_t>(0x33003300));
EXPECT_EQ(5, get_size(a));
}
TEST(IntX, encode)
{
IntX a;
EXPECT_EQ((ByteBuffer { 0x00 }), a.encode());
a.set(127);
EXPECT_EQ((ByteBuffer { 127 }), a.encode());
a.set(128);
EXPECT_EQ((ByteBuffer { 0x80, 128 }), a.encode());
a.set(0x00120034);
EXPECT_EQ((ByteBuffer { 0xd2, 0x00, 0x34 }), a.encode());
a.set(0x00320034);
EXPECT_EQ((ByteBuffer { 0xe0, 0x32, 0x00, 0x34 }), a.encode());
}
TEST(IntX, decode)
{
ByteBuffer buf { 0xe0, 0x32, 0x00, 0x34 };
auto decoded = IntX::decode(buf);
ASSERT_TRUE(!!decoded);
EXPECT_EQ(0x320034, decoded->get());
}
TEST(IntX, decode_one_null_byte)
{
ByteBuffer buf { 0x00 };
auto decoded = IntX::decode(buf);
ASSERT_TRUE(!!decoded);
EXPECT_EQ(0, decoded->get());
}
TEST(IntX, decode_empty)
{
ByteBuffer buf;
auto decoded = IntX::decode(buf);
EXPECT_FALSE(!!decoded);
}
TEST(IntX, decode_broken)
{
ByteBuffer buf { 0xff, 0xff };
auto decoded = IntX::decode(buf);
EXPECT_FALSE(!!decoded);
}
TEST(IntX, serialization)
{
IntX x;
x.set(0);
EXPECT_EQ(x, serialize_roundtrip(x));
x.set(255);
EXPECT_EQ(x, serialize_roundtrip(x));
x.set(0x123456);
EXPECT_EQ(x, serialize_roundtrip(x));
}