Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asn1: Add compare methods to asn1c wrapper #218

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 26 additions & 0 deletions vanetza/asn1/tests/asn1c_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,32 @@ 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_TRUE(!wrapper1.compare(wrapper1));
v0-e marked this conversation as resolved.
Show resolved Hide resolved
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) {
test_wrapper wrapper(asn_DEF_VanetzaTest);
OCTET_STRING_fromString(&wrapper->string, "1234");
Expand Down
Loading