Keep compact contacts within the STM32 flash boundary
This commit is contained in:
1 parent
036661e7e4
commit
025eadb39f
4 files changed
+66
-13
No files matched your search
@@ -11,6 +11,37 @@ ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
class MorseCLITests(unittest.TestCase):
|
||||
def test_settings_codec_matches_independent_images_and_unaligned_input(self):
|
||||
cases = []
|
||||
for frequency in (100000, 0x020304, 902321, 1000000):
|
||||
for speed in (1, 25, 60):
|
||||
prefix = b"MC\x01" + bytes([speed]) + frequency.to_bytes(4, "little")
|
||||
golden = prefix + (zlib.crc32(prefix) ^ 0xffffffff).to_bytes(4, "little")
|
||||
cases.append("{%d,%d,{%s}}" % (
|
||||
frequency, speed, ",".join(str(byte) for byte in golden)))
|
||||
code = r'''
|
||||
#include <helpers/MorseSettings.h>
|
||||
#include <cassert>
|
||||
struct Case { uint32_t frequency; uint8_t speed; uint8_t expected[12]; };
|
||||
int main() {
|
||||
const Case cases[] = {@CASES@};
|
||||
for (const auto& item : cases) {
|
||||
mesh::MorseSettings prefs;
|
||||
prefs.frequency_khz = item.frequency; prefs.wpm = item.speed;
|
||||
uint8_t encoded[mesh::MorseSettingsImageSize];
|
||||
mesh::encodeMorseSettings(prefs, encoded);
|
||||
assert(memcmp(encoded, item.expected, sizeof(encoded)) == 0);
|
||||
alignas(4) uint8_t unaligned[13];
|
||||
memcpy(unaligned + 1, item.expected, sizeof(item.expected));
|
||||
mesh::MorseSettings decoded;
|
||||
assert(mesh::decodeMorseSettings(unaligned + 1, sizeof(item.expected), decoded));
|
||||
assert(decoded.frequency_khz == item.frequency && decoded.wpm == item.speed);
|
||||
}
|
||||
}
|
||||
'''.replace("@CASES@", ",".join(cases))
|
||||
fs_mock = (ROOT / "test/fixtures/radio_profiles/mocks/helpers/IdentityStore.h").read_text(encoding="ascii")
|
||||
compile_run(self, code, headers={"helpers/IdentityStore.h": fs_mock})
|
||||
|
||||
def test_exact_shared_handler_strict_arguments_bounded_replies_and_rollback(self):
|
||||
code = (ROOT / "test/fixtures/morse_cli.cpp").read_text(encoding="ascii")
|
||||
fs_mock = (ROOT / "test/fixtures/radio_profiles/mocks/helpers/IdentityStore.h").read_text(encoding="ascii")
|
||||
|
||||
@@ -36,6 +36,33 @@ TEST(PersistentStoreFormat, CRCDetectsPayloadChanges) {
|
||||
EXPECT_EQ(split, expected);
|
||||
}
|
||||
|
||||
TEST(PersistentStoreFormat, CRCMatchesIndependentVectorsAndEverySplit) {
|
||||
EXPECT_EQ(updateCRC32(0xFFFFFFFFUL, nullptr, 0), 0xFFFFFFFFUL);
|
||||
EXPECT_EQ(updateCRC32(0x12345678UL, nullptr, 0), 0x12345678UL);
|
||||
const uint8_t check[] = "123456789";
|
||||
EXPECT_EQ(updateCRC32(0xFFFFFFFFUL, check, sizeof(check) - 1),
|
||||
0x340BC6D9UL);
|
||||
|
||||
uint8_t payload[256];
|
||||
for (unsigned i = 0; i < sizeof(payload); ++i) payload[i] = uint8_t(i);
|
||||
// Independent Python zlib vectors, converted to the store's no-final-xor
|
||||
// convention: zlib.crc32(payload, seed ^ 0xffffffff) ^ 0xffffffff.
|
||||
const struct { uint32_t seed, expected; } vectors[] = {
|
||||
{0xFFFFFFFFUL, 0xD6FA738CUL},
|
||||
{0x00000000UL, 0x2493092BUL},
|
||||
{0x12345678UL, 0x8906DCD5UL},
|
||||
};
|
||||
for (const auto& vector : vectors) {
|
||||
EXPECT_EQ(updateCRC32(vector.seed, payload, sizeof(payload)),
|
||||
vector.expected);
|
||||
for (size_t split = 0; split <= sizeof(payload); ++split) {
|
||||
uint32_t crc = updateCRC32(vector.seed, payload, split);
|
||||
crc = updateCRC32(crc, payload + split, sizeof(payload) - split);
|
||||
EXPECT_EQ(crc, vector.expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(PersistentStoreFormat, OccupancyCanBeDerivedFromCrcProtectedRecords) {
|
||||
uint8_t empty[CONTACT_RECORD_SIZE] = {};
|
||||
EXPECT_FALSE(contactRecordHasData(empty));
|
||||
|
||||
Reference in new issue
Block a user