From 527ff25c318d14ca5add9d38379f065e5cdf529b Mon Sep 17 00:00:00 2001 From: Stuart Miller Date: Thu, 11 Jan 2024 10:02:12 -0600 Subject: [PATCH] Add more message signing tests to increase code coverage. --- README.md | 7 ++++++- gcovr.cfg | 3 ++- tests/Network.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b38477a..916495b 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ You can also include the library as a submodule in your project. ### Running the tests -Libmav uses [doctest](https://github.com/doctest/doctest/). +Libmav uses [doctest](https://github.com/doctest/doctest/) and [gcovr](https://github.com/gcovr/gcovr/). To run the tests, build the library, then run the test executable. Test results will be output to console. @@ -40,6 +40,11 @@ mkdir build && cd build && cmake .. && make tests ./tests/tests ``` +To test coverage, simple invoke the coverage tool from the root directory. +```bash +gcovr +``` + ## Getting started ### Loading a message set diff --git a/gcovr.cfg b/gcovr.cfg index 1440025..4e242a4 100644 --- a/gcovr.cfg +++ b/gcovr.cfg @@ -1,3 +1,4 @@ exclude-throw-branches = yes filter = include/mav -exclude = include/mav/rapidxml/* \ No newline at end of file +exclude = include/mav/rapidxml/* +exclude = include/mav/picosha2/* \ No newline at end of file diff --git a/tests/Network.cpp b/tests/Network.cpp index 744b309..0ebfcd2 100644 --- a/tests/Network.cpp +++ b/tests/Network.cpp @@ -90,6 +90,9 @@ class DummyInterface : public NetworkInterface { } }; +uint64_t getTimestamp() { + return 770479200; +} TEST_CASE("Create network runtime") { @@ -122,6 +125,9 @@ TEST_CASE("Create network runtime") { DummyInterface interface; NetworkRuntime network({253, 1}, message_set, interface); + std::array key; + for (int i = 0 ; i < 32; i++) key[i] = i; + // send a heartbeat message, to establish a connection interface.addToReceiveQueue("\xfd\x09\x00\x00\x00\xfd\x01\x00\x00\x00\x04\x00\x00\x00\x01\x02\x03\x05\x06\x77\x53"s, interface_partner); auto connection = network.awaitConnection(); @@ -200,4 +206,50 @@ TEST_CASE("Create network runtime") { CHECK_EQ(message.name(), "HEARTBEAT"); CHECK(connection->alive()); } + + SUBCASE("Enable message signing") { + auto message = message_set.create("TEST_MESSAGE")({ + {"value", 42}, + {"text", "Hello World!"} + }); + interface.reset(); + network.enableMessageSigning(key); + connection->send(message); + CHECK(message.header().isSigned()); + // don't check anything after link_id in signature as the timestamp is dependent on current time + bool found = (interface.sendSpongeContains( + "\xfd\x10\x01\x00\x00\xfd\x01\xbc\x26\x00\x2a\x00\x00\x00\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21\xfd\x33\x00"s, + interface_partner)); + CHECK(found); + } + + SUBCASE("Enable message signing with custom timestamp function") { + auto message = message_set.create("TEST_MESSAGE")({ + {"value", 42}, + {"text", "Hello World!"} + }); + interface.reset(); + network.enableMessageSigning(key, getTimestamp); + connection->send(message); + CHECK(message.header().isSigned()); + bool found = (interface.sendSpongeContains( + "\xfd\x10\x01\x00\x00\xfd\x01\xbc\x26\x00\x2a\x00\x00\x00\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21\xfd\x33\x00\x60\x94\xec\x2d\x00\x00\x7b\xab\xfa\x1a\xed\xf9"s, + interface_partner)); + CHECK(found); + } + + SUBCASE("Disable message signing") { + auto message = message_set.create("TEST_MESSAGE")({ + {"value", 42}, + {"text", "Hello World!"} + }); + interface.reset(); + network.disableMessageSigning(); + connection->send(message); + CHECK(!message.header().isSigned()); + bool found = (interface.sendSpongeContains( + "\xfd\x10\x00\x00\x00\xfd\x01\xbc\x26\x00\x2a\x00\x00\x00\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21\x86\x37"s, + interface_partner)); + CHECK(found); + } }