Files
MicrOBU/microbu-esp32c5/external/vanetza-idf/vanetza/asn1/tests/asn1c_wrapper.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

139 lines
4.4 KiB
C++

#include <gtest/gtest.h>
#include <vanetza/asn1/asn1c_wrapper.hpp>
#include <vanetza/asn1/its/VanetzaTest.h>
using namespace vanetza::asn1;
typedef vanetza::asn1::asn1c_wrapper<VanetzaTest_t> test_wrapper;
typedef vanetza::asn1::asn1c_oer_wrapper<VanetzaTest_t> test_oer_wrapper;
TEST(asn1c_wrapper, create) {
EXPECT_NO_THROW({
test_wrapper wrapper(asn_DEF_VanetzaTest);
});
}
TEST(asn1c_wrapper, constants) {
EXPECT_EQ(42, VanetzaTest__field_magicValue);
}
TEST(asn1c_wrapper, size) {
test_wrapper wrapper(asn_DEF_VanetzaTest);
OCTET_STRING_fromString(&wrapper->string, "1234");
EXPECT_EQ(wrapper.size(), 5);
}
TEST(asn1c_wrapper, oer_size) {
test_oer_wrapper wrapper(asn_DEF_VanetzaTest);
OCTET_STRING_fromString(&wrapper->string, "1234");
EXPECT_EQ(wrapper.size(), 7);
OCTET_STRING_fromString(&wrapper->string, "12345");
EXPECT_EQ(wrapper.size(), 8);
}
TEST(asn1c_wrapper, dereferencing) {
test_wrapper wrapper(asn_DEF_VanetzaTest);
ASSERT_NE(wrapper->field, 3);
wrapper->field = 3;
EXPECT_EQ((*wrapper).field, 3);
}
TEST(asn1c_wrapper, copy) {
test_wrapper wrapper_orig(asn_DEF_VanetzaTest);
wrapper_orig->field = 5;
test_wrapper wrapper_copy = wrapper_orig;
EXPECT_EQ(wrapper_copy->field, 5);
wrapper_copy->field = 6;
EXPECT_EQ(wrapper_orig->field, 5);
}
TEST(asn1c_wrapper, self_assignment) {
test_wrapper wrapper(asn_DEF_VanetzaTest);
OCTET_STRING_fromString(&wrapper->string, "1234");
ASSERT_EQ(wrapper.size(), 5);
wrapper = wrapper;
EXPECT_EQ(wrapper.size(), 5);
}
TEST(asn1c_wrapper, validate) {
test_wrapper wrapper(asn_DEF_VanetzaTest);
OCTET_STRING_fromString(&wrapper->string, "1234");
EXPECT_TRUE(wrapper.validate());
OCTET_STRING_fromString(&wrapper->string, "ABCD"); // non-numerics
EXPECT_FALSE(wrapper.validate());
std::string msg;
EXPECT_FALSE(wrapper.validate(msg));
EXPECT_FALSE(msg.empty());
}
TEST(asn1c_wrapper, compare) {
test_wrapper wrapper1(asn_DEF_VanetzaTest);
OCTET_STRING_fromString(&wrapper1->string, "1234");
test_wrapper wrapper2(asn_DEF_VanetzaTest);
OCTET_STRING_fromString(&wrapper2->string, "1234");
test_wrapper wrapper3(asn_DEF_VanetzaTest);
OCTET_STRING_fromString(&wrapper3->string, "0123");
test_wrapper wrapper4(asn_DEF_VanetzaTest);
wrapper4->field = 5;
OCTET_STRING_fromString(&wrapper4->string, "1234");
// .compare()
EXPECT_EQ(wrapper1.compare(wrapper1), 0);
EXPECT_EQ(wrapper1.compare(wrapper2), 0);
EXPECT_EQ(wrapper1.compare(wrapper3), 1);
EXPECT_EQ(wrapper1.compare(wrapper4), -1);
// operators == and !=
EXPECT_TRUE(wrapper1 == wrapper1);
EXPECT_TRUE(wrapper1 == wrapper2);
EXPECT_FALSE(wrapper1 == wrapper3);
EXPECT_TRUE(wrapper1 != wrapper3);
EXPECT_TRUE(wrapper1 != wrapper4);
}
TEST(asn1c_wrapper, print) {
test_wrapper wrapper(asn_DEF_VanetzaTest);
OCTET_STRING_fromString(&wrapper->string, "1234");
EXPECT_EQ(wrapper.print(), 0);
}
TEST(asn1c_wrapper, encode) {
test_wrapper wrapper(asn_DEF_VanetzaTest);
OCTET_STRING_fromString(&wrapper->string, "1234");
wrapper->field = 8;
vanetza::ByteBuffer buf = wrapper.encode();
EXPECT_EQ(wrapper.size(), buf.size());
// No idea if this is the correct ASN.1 PER equivalent
EXPECT_EQ(vanetza::ByteBuffer({ 0x04, 0x02, 0x11, 0xA2, 0x80 }), buf);
}
TEST(asn1c_wrapper, decode_valid) {
test_wrapper wrapper(asn_DEF_VanetzaTest);
const vanetza::ByteBuffer buffer { 0x04, 0x02, 0x11, 0xA2, 0x80 };
bool result = wrapper.decode(buffer);
ASSERT_TRUE(result);
EXPECT_EQ(8, wrapper->field);
ASSERT_EQ(4, wrapper->string.size);
EXPECT_STREQ("1234", (const char*)(wrapper->string.buf));
}
TEST(asn1c_wrapper, decode_invalid) {
test_wrapper wrapper(asn_DEF_VanetzaTest);
const vanetza::ByteBuffer buffer { 0x12 };
bool result = wrapper.decode(buffer);
// should have failed because of short buffer
ASSERT_FALSE(result);
}
TEST(asn1c_wrapper, decode_range) {
test_wrapper wrapper(asn_DEF_VanetzaTest);
const vanetza::ByteBuffer buffer { 0xC0, 0xFF, 0xEE, 0x04, 0x02, 0x11, 0xA2, 0x80, 0x08, 0x15 };
auto begin = std::next(buffer.begin(), 3);
auto end = std::prev(buffer.end(), 2);
ASSERT_TRUE(wrapper.decode(begin, end));
EXPECT_EQ(8, wrapper->field);
EXPECT_EQ(4, wrapper->string.size);
EXPECT_STREQ("1234", (const char*)(wrapper->string.buf));
}