From a275baf202463849b284764572fbe6d528ed5315 Mon Sep 17 00:00:00 2001 From: v0-e Date: Mon, 29 Jan 2024 10:56:36 +0000 Subject: [PATCH 1/3] asn1: Add compare() method to asn1c wrapper --- vanetza/asn1/asn1c_wrapper.cpp | 5 +++++ vanetza/asn1/asn1c_wrapper.hpp | 12 +++++++++++- vanetza/asn1/tests/asn1c_wrapper.cpp | 20 ++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/vanetza/asn1/asn1c_wrapper.cpp b/vanetza/asn1/asn1c_wrapper.cpp index 7dfaae79a..1720ac3a2 100644 --- a/vanetza/asn1/asn1c_wrapper.cpp +++ b/vanetza/asn1/asn1c_wrapper.cpp @@ -83,6 +83,11 @@ bool validate(asn_TYPE_descriptor_t& td, const void* t, std::string& error) return ok; } +int compare(asn_TYPE_descriptor_t& td, const void* a, const void* b) +{ + return td.op->compare_struct(&td, a, b); +} + int print(FILE* stream, asn_TYPE_descriptor_t& td, const void* t) { return asn_fprint(stream, &td, t); diff --git a/vanetza/asn1/asn1c_wrapper.hpp b/vanetza/asn1/asn1c_wrapper.hpp index 6bf0ed387..8ae529853 100644 --- a/vanetza/asn1/asn1c_wrapper.hpp +++ b/vanetza/asn1/asn1c_wrapper.hpp @@ -18,6 +18,7 @@ void free(asn_TYPE_descriptor_t&, void*); void* copy(asn_TYPE_descriptor_t&, const void*); bool validate(asn_TYPE_descriptor_t&, const void*); bool validate(asn_TYPE_descriptor_t&, const void*, std::string&); +int compare(asn_TYPE_descriptor_t&, const void*, const void*); int print(FILE* stream, asn_TYPE_descriptor_t&, const void*); std::size_t size_per(asn_TYPE_descriptor_t&, const void*); std::size_t size_oer(asn_TYPE_descriptor_t&, const void*); @@ -94,9 +95,18 @@ class asn1c_wrapper_common return vanetza::asn1::validate(m_type, m_struct, error); } + /** + * Compare ASN.1 types + * \param other Other ASN.1 type to compare with + * \return 0 if equal, <0 if other is "greater", >0 if other is "smaller" + */ + int compare(const asn1c_wrapper_common& other) const + { + return vanetza::asn1::compare(m_type, m_struct, other.m_struct); + } + /** * Print ASN.1 type to standard output - * \param stream Output stream * \return 0 on success, -1 on error */ int print() const diff --git a/vanetza/asn1/tests/asn1c_wrapper.cpp b/vanetza/asn1/tests/asn1c_wrapper.cpp index 747ce2102..e3d89f061 100644 --- a/vanetza/asn1/tests/asn1c_wrapper.cpp +++ b/vanetza/asn1/tests/asn1c_wrapper.cpp @@ -56,6 +56,26 @@ TEST(asn1c_wrapper, validate) { EXPECT_FALSE(msg.empty()); } +TEST(asn1c_wrapper, compare) { + test_wrapper wrapper1(asn_DEF_VanetzaTest); + OCTET_STRING_fromString(&wrapper1->string, "1234"); + EXPECT_TRUE(!wrapper1.compare(wrapper1)); + + test_wrapper wrapper2(asn_DEF_VanetzaTest); + OCTET_STRING_fromString(&wrapper2->string, "1234"); + EXPECT_TRUE(!wrapper1.compare(wrapper2)); + + test_wrapper wrapper3(asn_DEF_VanetzaTest); + OCTET_STRING_fromString(&wrapper3->string, "0123"); + EXPECT_FALSE(!wrapper1.compare(wrapper3)); + EXPECT_TRUE(wrapper1.compare(wrapper3) > 0); + + test_wrapper wrapper4(asn_DEF_VanetzaTest); + wrapper4->field = 5; + OCTET_STRING_fromString(&wrapper4->string, "1234"); + EXPECT_FALSE(!wrapper1.compare(wrapper4)); +} + TEST(asn1c_wrapper, print) { test_wrapper wrapper(asn_DEF_VanetzaTest); OCTET_STRING_fromString(&wrapper->string, "1234"); From 48f6488a24849cc25d8fc2f324f25a8253e04057 Mon Sep 17 00:00:00 2001 From: v0-e Date: Mon, 29 Jan 2024 11:26:07 +0000 Subject: [PATCH 2/3] asn1: Add '==' and '!=' overload to asn1c wrapper --- vanetza/asn1/asn1c_wrapper.hpp | 10 ++++++++++ vanetza/asn1/tests/asn1c_wrapper.cpp | 20 +++++++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/vanetza/asn1/asn1c_wrapper.hpp b/vanetza/asn1/asn1c_wrapper.hpp index 8ae529853..7e6110789 100644 --- a/vanetza/asn1/asn1c_wrapper.hpp +++ b/vanetza/asn1/asn1c_wrapper.hpp @@ -75,6 +75,16 @@ class asn1c_wrapper_common const asn1c_type* content() const { return m_struct; } asn1c_type* content() { return m_struct; } + // compare semantics + bool operator==(const asn1c_wrapper_common& rhs) const + { + return vanetza::asn1::compare(m_type, m_struct, rhs.m_struct) == 0; + } + bool operator!=(const asn1c_wrapper_common& rhs) const + { + return vanetza::asn1::compare(m_type, m_struct, rhs.m_struct) != 0; + } + /** * Check ASN.1 constraints * \param error (optional) copy of error message diff --git a/vanetza/asn1/tests/asn1c_wrapper.cpp b/vanetza/asn1/tests/asn1c_wrapper.cpp index e3d89f061..b25677c39 100644 --- a/vanetza/asn1/tests/asn1c_wrapper.cpp +++ b/vanetza/asn1/tests/asn1c_wrapper.cpp @@ -59,21 +59,27 @@ TEST(asn1c_wrapper, validate) { TEST(asn1c_wrapper, compare) { test_wrapper wrapper1(asn_DEF_VanetzaTest); OCTET_STRING_fromString(&wrapper1->string, "1234"); - EXPECT_TRUE(!wrapper1.compare(wrapper1)); - test_wrapper wrapper2(asn_DEF_VanetzaTest); OCTET_STRING_fromString(&wrapper2->string, "1234"); - EXPECT_TRUE(!wrapper1.compare(wrapper2)); - test_wrapper wrapper3(asn_DEF_VanetzaTest); OCTET_STRING_fromString(&wrapper3->string, "0123"); - EXPECT_FALSE(!wrapper1.compare(wrapper3)); - EXPECT_TRUE(wrapper1.compare(wrapper3) > 0); - test_wrapper wrapper4(asn_DEF_VanetzaTest); wrapper4->field = 5; OCTET_STRING_fromString(&wrapper4->string, "1234"); + + // .compare() + EXPECT_TRUE(!wrapper1.compare(wrapper1)); + EXPECT_TRUE(!wrapper1.compare(wrapper2)); + EXPECT_FALSE(!wrapper1.compare(wrapper3)); + EXPECT_TRUE(wrapper1.compare(wrapper3) > 0); EXPECT_FALSE(!wrapper1.compare(wrapper4)); + + // 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) { From 43141324a40a62375b10cdf0fa0d57558d3cc06a Mon Sep 17 00:00:00 2001 From: v0-e Date: Mon, 29 Jan 2024 14:26:06 +0000 Subject: [PATCH 3/3] asn1: Prefer value over boolean checks in asn1c wrapper compare() tests --- vanetza/asn1/tests/asn1c_wrapper.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/vanetza/asn1/tests/asn1c_wrapper.cpp b/vanetza/asn1/tests/asn1c_wrapper.cpp index b25677c39..24a7c3309 100644 --- a/vanetza/asn1/tests/asn1c_wrapper.cpp +++ b/vanetza/asn1/tests/asn1c_wrapper.cpp @@ -68,11 +68,10 @@ TEST(asn1c_wrapper, compare) { OCTET_STRING_fromString(&wrapper4->string, "1234"); // .compare() - EXPECT_TRUE(!wrapper1.compare(wrapper1)); - EXPECT_TRUE(!wrapper1.compare(wrapper2)); - EXPECT_FALSE(!wrapper1.compare(wrapper3)); - EXPECT_TRUE(wrapper1.compare(wrapper3) > 0); - EXPECT_FALSE(!wrapper1.compare(wrapper4)); + 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);