Skip to content

Commit

Permalink
Add more message signing tests to increase code coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
stuart-auterion committed Jan 11, 2024
1 parent 0cfcd13 commit 527ff25
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion gcovr.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
exclude-throw-branches = yes
filter = include/mav
exclude = include/mav/rapidxml/*
exclude = include/mav/rapidxml/*
exclude = include/mav/picosha2/*
52 changes: 52 additions & 0 deletions tests/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ class DummyInterface : public NetworkInterface {
}
};

uint64_t getTimestamp() {
return 770479200;
}

TEST_CASE("Create network runtime") {

Expand Down Expand Up @@ -122,6 +125,9 @@ TEST_CASE("Create network runtime") {
DummyInterface interface;
NetworkRuntime network({253, 1}, message_set, interface);

std::array<uint8_t, 32> 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();
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 527ff25

Please sign in to comment.