Skip to content

Commit

Permalink
Merge pull request #218 from v0-e/asn1-compare
Browse files Browse the repository at this point in the history
asn1: Add compare methods to asn1c wrapper
  • Loading branch information
riebl authored Jan 29, 2024
2 parents 332387b + 4314132 commit dc3ed99
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
5 changes: 5 additions & 0 deletions vanetza/asn1/asn1c_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 21 additions & 1 deletion vanetza/asn1/asn1c_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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*);
Expand Down Expand Up @@ -74,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
Expand All @@ -94,9 +105,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
Expand Down
25 changes: 25 additions & 0 deletions vanetza/asn1/tests/asn1c_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,31 @@ TEST(asn1c_wrapper, validate) {
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");
Expand Down

0 comments on commit dc3ed99

Please sign in to comment.