Repository navigation
Make advert parsing robust to malformed advert packets #2459
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
Draft
mtlynch
wants to merge
17
commits into
meshcore-dev:dev
Choose a base branch
from
mtlynch:test-gps
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a5b8c65
Add native advert data round-trip tests
mtlynch 987f345
Add invalid GPS advert data tests
mtlynch a3600d9
Rewrite advert data tests with raw payloads
mtlynch c736435
Test advert parsing via mesh receive path
mtlynch 44bcaf7
Move advert data test to helpers
mtlynch 84983ed
Fix native helper test discovery
mtlynch 2dac574
Inline advert test sender identity
mtlynch 390a74d
Simplify advert test packet helper
mtlynch 1ac857a
Move advert test timestamp declarations
mtlynch ae0312e
Refactor advert data test mesh setup
mtlynch 339754c
Clean up advert data test names
mtlynch ac10c13
Add sanitizer coverage for advert GPS parsing
mtlynch 5b2b577
Clarify native mocks and advert truncation tests
mtlynch 5a9c0a2
Split test helpers to mocks.h
mtlynch 9bb9d31
Split up mocks.h
mtlynch 837fcf9
Simplify to a more obviously fake SHA256 algorithm
mtlynch 5375246
Simplify to just test AdvertDataParser
mtlynch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "build": { | ||
| "flags": [ | ||
| "-fno-sanitize=undefined" | ||
| ] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #pragma once | ||
|
|
||
| #include <stdlib.h> | ||
| #include <stddef.h> | ||
| #include <stdint.h> | ||
| #include <string.h> | ||
| #include <stdio.h> | ||
|
|
||
| // Mock Arduino compatibility header for native testing. | ||
| // Provides the small libc-backed helpers needed by Arduino-oriented code. | ||
| inline char* ltoa(long value, char* buffer, int base) { | ||
| if (base == 10) { | ||
| snprintf(buffer, 32, "%ld", value); | ||
| } else { | ||
| buffer[0] = 0; | ||
| } | ||
| return buffer; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #pragma once | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
| #include <ed_25519.h> | ||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #include <stdint.h> | ||
|
|
||
| // Mock Ed25519 wrapper for native testing. | ||
| // Adapts the test-only C ed25519 implementation to the Arduino-style class API. | ||
| class Ed25519 { | ||
| public: | ||
| static bool verify(const uint8_t* sig, const uint8_t* pub_key, const uint8_t* message, int msg_len) { | ||
| return ed25519_verify(sig, message, msg_len, pub_key) != 0; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,15 @@ | ||
| #pragma once | ||
|
|
||
| // Mock Stream class for native testing | ||
| // Provides minimal interface needed by Utils.h | ||
| #include <stddef.h> | ||
| #include <stdint.h> | ||
|
|
||
| // Mock Stream class for native testing. | ||
| // Provides the minimal interface needed by code that depends on Arduino Stream. | ||
| class Stream { | ||
| public: | ||
| virtual void print(char c) {} | ||
| virtual void print(const char* str) {} | ||
| virtual size_t readBytes(uint8_t*, size_t) { return 0; } | ||
| virtual size_t write(const uint8_t*, size_t len) { return len; } | ||
| virtual void print(char) {} | ||
| virtual void print(const char*) {} | ||
| virtual void println() {} | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <optional> | ||
|
|
||
| #include "helpers/BaseChatMesh.h" | ||
| #include "helpers/SimpleMeshTables.h" | ||
| #include "radio.h" | ||
| #include "random.h" | ||
| #include "time.h" | ||
|
|
||
| // No-op packet manager for native tests. | ||
| // Satisfies Mesh dependencies while preventing packet allocation or queuing. | ||
| class NoopPacketManager final : public mesh::PacketManager { | ||
| public: | ||
| mesh::Packet* allocNew() override { | ||
| return nullptr; | ||
| } | ||
|
|
||
| void free(mesh::Packet*) override {} | ||
|
|
||
| void queueOutbound(mesh::Packet*, uint8_t, uint32_t) override {} | ||
|
|
||
| mesh::Packet* getNextOutbound(uint32_t) override { | ||
| return nullptr; | ||
| } | ||
|
|
||
| int getOutboundCount(uint32_t) const override { | ||
| return 0; | ||
| } | ||
|
|
||
| int getOutboundTotal() const override { | ||
| return 0; | ||
| } | ||
|
|
||
| int getFreeCount() const override { | ||
| return 0; | ||
| } | ||
|
|
||
| mesh::Packet* getOutboundByIdx(int) override { | ||
| return nullptr; | ||
| } | ||
|
|
||
| mesh::Packet* removeOutboundByIdx(int) override { | ||
| return nullptr; | ||
| } | ||
|
|
||
| void queueInbound(mesh::Packet*, uint32_t) override {} | ||
|
|
||
| mesh::Packet* getNextInbound(uint32_t) override { | ||
| return nullptr; | ||
| } | ||
| }; | ||
|
|
||
| // Test chat mesh for native tests. | ||
| // Exposes packet receive handling and captures discovered contacts for assertions. | ||
| class TestChatMesh final : public BaseChatMesh { | ||
| public: | ||
| TestChatMesh(mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, | ||
| mesh::PacketManager& mgr, mesh::MeshTables& tables) | ||
| : BaseChatMesh(radio, ms, rng, rtc, mgr, tables) {} | ||
|
|
||
| mesh::DispatcherAction recv(mesh::Packet* pkt) { | ||
| return onRecvPacket(pkt); | ||
| } | ||
|
|
||
| std::optional<ContactInfo> discovered_contact; | ||
|
|
||
| protected: | ||
| void onDiscoveredContact(ContactInfo& contact, bool, uint8_t, const uint8_t*) override { | ||
| discovered_contact = contact; | ||
| } | ||
|
|
||
| ContactInfo* processAck(const uint8_t*) override { | ||
| return nullptr; | ||
| } | ||
|
|
||
| void onContactPathUpdated(const ContactInfo&) override {} | ||
|
|
||
| void onMessageRecv(const ContactInfo&, mesh::Packet*, uint32_t, const char*) override {} | ||
|
|
||
| void onCommandDataRecv(const ContactInfo&, mesh::Packet*, uint32_t, const char*) override {} | ||
|
|
||
| void onSignedMessageRecv(const ContactInfo&, mesh::Packet*, uint32_t, const uint8_t*, const char*) override {} | ||
|
|
||
| uint32_t calcFloodTimeoutMillisFor(uint32_t) const override { | ||
| return 0; | ||
| } | ||
|
|
||
| uint32_t calcDirectTimeoutMillisFor(uint32_t, uint8_t) const override { | ||
| return 0; | ||
| } | ||
|
|
||
| void onSendTimeout() override {} | ||
|
|
||
| void onChannelMessageRecv(const mesh::GroupChannel&, mesh::Packet*, uint32_t, const char*) override {} | ||
|
|
||
| uint8_t onContactRequest(const ContactInfo&, uint32_t, const uint8_t*, uint8_t, uint8_t*) override { | ||
| return 0; | ||
| } | ||
|
|
||
| void onContactResponse(const ContactInfo&, const uint8_t*, uint8_t) override {} | ||
| }; | ||
|
|
||
| // Test mesh context for native tests. | ||
| // Owns mock dependencies in construction order and exposes the mesh via operator->. | ||
| struct TestMeshContext { | ||
| explicit TestMeshContext(uint32_t current_timestamp) | ||
| : rtc(current_timestamp), mesh(radio, ms, rng, rtc, packet_manager, tables) {} | ||
|
|
||
| TestChatMesh* operator->() { | ||
| return &mesh; | ||
| } | ||
|
|
||
| const TestChatMesh* operator->() const { | ||
| return &mesh; | ||
| } | ||
|
|
||
| FakeRadio radio; | ||
| FakeMillis ms; | ||
| FakeRng rng; | ||
| FakeRtc rtc; | ||
| NoopPacketManager packet_manager; | ||
| SimpleMeshTables tables; | ||
| TestChatMesh mesh; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
|
|
||
| #include "Dispatcher.h" | ||
|
|
||
| // Fake radio for native tests. | ||
| // Provides successful no-op send/receive behavior without hardware access. | ||
| class FakeRadio final : public mesh::Radio { | ||
| public: | ||
| int recvRaw(uint8_t*, int) override { | ||
| return 0; | ||
| } | ||
|
|
||
| uint32_t getEstAirtimeFor(int) override { | ||
| return 1; | ||
| } | ||
|
|
||
| float packetScore(float, int) override { | ||
| return 1.0f; | ||
| } | ||
|
|
||
| bool startSendRaw(const uint8_t*, int) override { | ||
| return true; | ||
| } | ||
|
|
||
| bool isSendComplete() override { | ||
| return true; | ||
| } | ||
|
|
||
| void onSendFinished() override {} | ||
|
|
||
| bool isInRecvMode() const override { | ||
| return true; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #pragma once | ||
|
|
||
| #if defined(__has_include_next) | ||
| #if __has_include_next(<random.h>) | ||
| #include_next <random.h> | ||
| #endif | ||
| #endif | ||
|
|
||
| #ifdef __cplusplus | ||
|
|
||
| #include <cstddef> | ||
| #include <cstring> | ||
|
|
||
| #include "Utils.h" | ||
|
|
||
| // Fake random generator for native tests. | ||
| // Fills buffers with deterministic bytes so generated packets are repeatable. | ||
| class FakeRng final : public mesh::RNG { | ||
| public: | ||
| void random(uint8_t* dest, size_t sz) override { | ||
| memset(dest, 0x5A, sz); | ||
| } | ||
| }; | ||
|
|
||
| #endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's evil.
We shouldn't depend on a mock implementation that is not doing what it says on the tin!
How do you propose we do crypto tests (regression tests in particular)? We need a way to test interfaces including those that have arduino deps (like rweather's Crypto lib).
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still a WIP, and I haven't decided for sure on this implementation yet.
That said, it's pretty normal for mocks to not literally do what they say for the sake of testing. I imagine it's uncontroversial to mock out a clock and have it always claim to be a fixed time like 2000-01-01T00:00:00Z.
Another possibility is to let the client hardcode the inputs and outputs, so the test can say, "if you get the input
"hello meshcore", return17f4227296ecf3043173ab392f5496ad8ebe86a061a4c7ac015ec38831a3a0a2."Another option is to re-route it to a real SHA256 implementation.