fix: keep feature-complete release builds within flash limits
This commit is contained in:
1 parent
bc1176c7f4
commit
805dd2cbdf
17 files changed
+735
-300
No files matched your search
Vendored
+7
@@ -8,3 +8,10 @@
|
||||
namespace mesh { namespace power {
|
||||
inline bool handleRegulatorCommand(const char*, char*, size_t) { return false; }
|
||||
}}
|
||||
|
||||
// LED persistence has its own executable coverage in test_led_settings.py.
|
||||
// These command-dispatch fixtures likewise leave unrelated LED commands to
|
||||
// that subsystem instead of requiring a board/filesystem implementation.
|
||||
namespace mesh { namespace leds {
|
||||
inline bool handleCommand(const char*, char*, size_t) { return false; }
|
||||
}}
|
||||
@@ -0,0 +1,149 @@
|
||||
"""Keep canonical retry preference bytes while batching their atomic write."""
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
PREFIX = r'''
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#define DEFAULT_POWERSAVING_ENABLED 0
|
||||
#define DEFAULT_ADVERT_LOC_POLICY 0
|
||||
#ifndef FLOOD_RETRY_PREFIX_SLOTS
|
||||
#define FLOOD_RETRY_PREFIX_SLOTS 8
|
||||
#define FLOOD_RETRY_PREFIX_LEN 3
|
||||
#define FLOOD_RETRY_BRIDGE_BUCKETS 6
|
||||
#define FLOOD_RETRY_BUCKET_PREFIXES 17
|
||||
#define FLOOD_RETRY_IGNORE_PREFIXES 8
|
||||
#endif
|
||||
namespace mesh { namespace bridge {constexpr uint8_t ESPNOW_FORMAT_WRAPPED=0;} struct RepeaterRadioTiming {static constexpr uint8_t DEFAULT_RX_WATCHDOG_ENABLED=1;}; }
|
||||
class ConfigSerializer {public:virtual void structure() {} uint8_t base_padding[3] = {};};
|
||||
'''
|
||||
|
||||
WRITER = r'''
|
||||
struct Writer {
|
||||
std::vector<uint8_t> bytes;size_t budget=std::numeric_limits<size_t>::max();bool okay=true;
|
||||
__attribute__((noinline)) size_t write(const uint8_t* p,size_t n) {
|
||||
if(!okay)return 0;const size_t used=std::min(n,budget);bytes.insert(bytes.end(),p,p+used);budget-=used;
|
||||
if(used!=n)okay=false;return used;
|
||||
}
|
||||
bool commit()const{return okay;}
|
||||
};
|
||||
'''
|
||||
|
||||
REFERENCE_WRITES = r'''
|
||||
writer.write((uint8_t *)&prefs->retry_preset, sizeof(prefs->retry_preset)); // 295
|
||||
writer.write((uint8_t *)&prefs->direct_retry_attempts, sizeof(prefs->direct_retry_attempts)); // 296
|
||||
writer.write((uint8_t *)&prefs->direct_retry_base_ms, sizeof(prefs->direct_retry_base_ms)); // 297
|
||||
writer.write((uint8_t *)&prefs->direct_retry_step_ms, sizeof(prefs->direct_retry_step_ms)); // 299
|
||||
writer.write((uint8_t *)&prefs->direct_retry_snr_margin_x4, sizeof(prefs->direct_retry_snr_margin_x4)); // 301
|
||||
writer.write((uint8_t *)&prefs->direct_retry_cr4_snr_x4, sizeof(prefs->direct_retry_cr4_snr_x4)); // 303
|
||||
writer.write((uint8_t *)&prefs->direct_retry_cr5_snr_x4, sizeof(prefs->direct_retry_cr5_snr_x4)); // 304
|
||||
writer.write((uint8_t *)&prefs->direct_retry_cr7_snr_x4, sizeof(prefs->direct_retry_cr7_snr_x4)); // 305
|
||||
writer.write((uint8_t *)&prefs->direct_retry_cr8_snr_x4, sizeof(prefs->direct_retry_cr8_snr_x4)); // 306
|
||||
writer.write((uint8_t *)&prefs->direct_retry_enabled, sizeof(prefs->direct_retry_enabled)); // 307
|
||||
writer.write((uint8_t *)&prefs->direct_retry_cr_enabled, sizeof(prefs->direct_retry_cr_enabled)); // 308
|
||||
writer.write((uint8_t *)&prefs->direct_retry_prefs_magic, sizeof(prefs->direct_retry_prefs_magic)); // 309
|
||||
writer.write((uint8_t *)&prefs->flood_retry_attempts, sizeof(prefs->flood_retry_attempts)); // 311
|
||||
writer.write((uint8_t *)&prefs->flood_retry_max_path, sizeof(prefs->flood_retry_max_path)); // 312
|
||||
writer.write((uint8_t *)&prefs->flood_retry_prefixes[0][0], sizeof(prefs->flood_retry_prefixes)); // 313
|
||||
writer.write((uint8_t *)&prefs->flood_retry_bridge_enabled, sizeof(prefs->flood_retry_bridge_enabled));
|
||||
writer.write((uint8_t *)&prefs->flood_retry_bridge_buckets[0][0][0], sizeof(prefs->flood_retry_bridge_buckets));
|
||||
writer.write((uint8_t *)&prefs->flood_retry_ignore_prefixes[0][0], sizeof(prefs->flood_retry_ignore_prefixes));
|
||||
writer.write((uint8_t *)&prefs->flood_retry_advert_enabled, sizeof(prefs->flood_retry_advert_enabled));
|
||||
writer.write((uint8_t *)&prefs->battery_alert_enabled, sizeof(prefs->battery_alert_enabled));
|
||||
writer.write((uint8_t *)&prefs->battery_alert_low_percent, sizeof(prefs->battery_alert_low_percent));
|
||||
writer.write((uint8_t *)&prefs->battery_alert_critical_percent, sizeof(prefs->battery_alert_critical_percent));
|
||||
writer.write((uint8_t *)&prefs->direct_retry_recent_enabled, sizeof(prefs->direct_retry_recent_enabled));
|
||||
writer.write((uint8_t *)&prefs->flood_channel_data_enabled, sizeof(prefs->flood_channel_data_enabled));
|
||||
writer.write((uint8_t *)&prefs->legacy_flood_channel_block_max_hops,
|
||||
sizeof(prefs->legacy_flood_channel_block_max_hops));
|
||||
writer.write((uint8_t *)&prefs->flood_channel_data_max_hops, sizeof(prefs->flood_channel_data_max_hops));
|
||||
writer.write((uint8_t *)&prefs->telemetry_access, sizeof(prefs->telemetry_access)); // 674
|
||||
'''
|
||||
|
||||
CHECKS = r'''
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
|
||||
int main(){
|
||||
NodePrefs prefs;uint32_t state=1;size_t length=0;
|
||||
for(unsigned seed=0;seed<32;seed++) {
|
||||
Writer expected;
|
||||
auto* first=reinterpret_cast<uint8_t*>(&prefs)+__builtin_offsetof(NodePrefs,retry_preset);
|
||||
const size_t size=__builtin_offsetof(NodePrefs,telemetry_access)+sizeof(prefs.telemetry_access)-__builtin_offsetof(NodePrefs,retry_preset);
|
||||
for(size_t i=0;i<size;i++){state=state*1664525u+1013904223u;first[i]=uint8_t(state>>24);}
|
||||
reference(expected,&prefs);length=expected.bytes.size();assert(expected.commit());
|
||||
Writer actual;writeCommonRetryPrefs(actual,&prefs);assert(actual.commit()&&actual.bytes==expected.bytes);
|
||||
for(size_t limit=0;limit<=length;limit++){
|
||||
Writer a,b;a.budget=b.budget=limit;reference(a,&prefs);writeCommonRetryPrefs(b,&prefs);
|
||||
assert(a.bytes==b.bytes&&a.commit()==b.commit());assert(a.commit()==(limit==length));
|
||||
}
|
||||
}
|
||||
printf("PASS: exact canonical %zu-byte tail, 32 arbitrary states, every cumulative short-write boundary\n",length);
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
'''
|
||||
|
||||
def fixture(padded=False):
|
||||
header = (ROOT / "src/helpers/CommonCLI.h").read_text()
|
||||
begin = header.index("class NodePrefs : public ConfigSerializer")
|
||||
begin = header.index("public:", begin)
|
||||
end = header.index("\nprivate:", begin)
|
||||
declarations = "class NodePrefs : public ConfigSerializer {\n" + header[begin:end] + "\n};\n"
|
||||
if padded:
|
||||
original = " uint8_t direct_retry_attempts = 0;"
|
||||
assert original in declarations
|
||||
declarations = declarations.replace(original, original + "\n uint8_t layout_break = 0;", 1)
|
||||
source = (ROOT / "src/helpers/CommonCLI.cpp").read_text()
|
||||
begin = source.index("// This canonical retry/flood span contains no padding.")
|
||||
end = source.index("void CommonCLI::savePrefs(FILESYSTEM* fs", begin)
|
||||
helper = source[begin:end]
|
||||
return (PREFIX + declarations + helper + WRITER
|
||||
+ "void reference(Writer& writer, NodePrefs* prefs) {\n"
|
||||
+ REFERENCE_WRITES + "}\n" + CHECKS)
|
||||
|
||||
|
||||
class CommonRetryTailTests(unittest.TestCase):
|
||||
def compile(self, work, definitions=(), padded=False):
|
||||
compiler = shutil.which("g++") or shutil.which("clang++")
|
||||
self.assertIsNotNone(compiler, "native C++ compiler required")
|
||||
cpp, executable = work / "tail.cpp", work / "tail"
|
||||
cpp.write_text(fixture(padded))
|
||||
result = subprocess.run([
|
||||
compiler, "-std=c++17", "-O1", "-Wall", "-Wextra", "-Werror",
|
||||
"-Wno-misleading-indentation", "-fsanitize=address,undefined",
|
||||
"-fno-sanitize-recover=all", "-fno-pie", "-no-pie", *definitions,
|
||||
str(cpp), "-o", str(executable),
|
||||
], capture_output=True, text=True, timeout=60)
|
||||
return result, executable
|
||||
|
||||
def test_actual_members_match_old_bytes_and_every_short_write_boundary(self):
|
||||
for definitions in ((), ("-DENABLE_OTA=1", "-DRAK_4631=1"), (
|
||||
"-DFLOOD_RETRY_PREFIX_SLOTS=3", "-DFLOOD_RETRY_PREFIX_LEN=5",
|
||||
"-DFLOOD_RETRY_BRIDGE_BUCKETS=2", "-DFLOOD_RETRY_BUCKET_PREFIXES=7",
|
||||
"-DFLOOD_RETRY_IGNORE_PREFIXES=4")):
|
||||
with self.subTest(definitions=definitions), tempfile.TemporaryDirectory() as directory:
|
||||
result, executable = self.compile(Path(directory), definitions)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
checked = subprocess.run([str(executable)], capture_output=True, text=True, timeout=30)
|
||||
self.assertEqual(checked.returncode, 0, checked.stdout + checked.stderr)
|
||||
self.assertIn("exact canonical", checked.stdout)
|
||||
|
||||
def test_changed_member_layout_refuses_to_compile(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
result, _ = self.compile(Path(directory), padded=True)
|
||||
self.assertNotEqual(result.returncode, 0)
|
||||
self.assertIn("retry tail has padding or changed order", result.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -58,7 +58,7 @@ def actual_harness():
|
||||
'SERIALIZER': 'template<typename Writer>\n' + extract_braced(
|
||||
source, 'static bool writeCommonPrefsImage('),
|
||||
'STORE': extract_braced(source, 'class CommonPrefsFileStore'),
|
||||
'METHODS': methods,
|
||||
'METHODS': ("\n#include <stddef.h>\ntemplate<typename Writer>\n" + extract_braced(source, "static void writeCommonRetryPrefs(") + "\n" + methods),
|
||||
'SET_DEBUG': extract_braced(source, 'if (strncmp(config, "usb.debug", 9)'),
|
||||
'SET_MASTER': extract_braced(source, 'if (strncmp(config, "usb.logging", 11)'),
|
||||
'GET_DEBUG': extract_braced(source, 'if (strcmp(config, "usb.debug") == 0)'),
|
||||
|
||||
@@ -155,7 +155,7 @@ class CommonRadioPersistenceTest(unittest.TestCase):
|
||||
'bool CommonCLI::savePrimaryRadioParams('))
|
||||
serializer = 'template<typename Writer>\n' + extract_braced(source, 'static bool writeCommonPrefsImage(')
|
||||
tail = extract_braced(source, 'if (file.available() >= (int)sizeof(_prefs->bridge_format))')
|
||||
code = HARNESS.replace('@FIELDS@',fields).replace('@METHODS@',methods)
|
||||
code = HARNESS.replace('@FIELDS@',fields).replace('@METHODS@',("\n#include <stddef.h>\ntemplate<typename Writer>\n" + extract_braced(source, "static void writeCommonRetryPrefs(") + "\n" + methods))
|
||||
code = code.replace('@SERIALIZER@',serializer).replace('@TAIL@',tail)
|
||||
sanitizer_flags = [] if os.name == 'nt' else [
|
||||
'-fsanitize=address,undefined', '-fno-sanitize-recover=all',
|
||||
|
||||
@@ -318,7 +318,7 @@ class CommonUsbDebugTest(unittest.TestCase):
|
||||
'SERIALIZER': 'template<typename Writer>\n' + extract_braced(
|
||||
source, 'static bool writeCommonPrefsImage('),
|
||||
'STORE': extract_braced(source, 'class CommonPrefsFileStore'),
|
||||
'METHODS': methods,
|
||||
'METHODS': ("\n#include <stddef.h>\ntemplate<typename Writer>\n" + extract_braced(source, "static void writeCommonRetryPrefs(") + "\n" + methods),
|
||||
'DYNAMIC': extract_braced(header, 'class BridgePrefs :'),
|
||||
'SET_DEBUG': extract_braced(source, 'if (strncmp(config, "usb.debug", 9)'),
|
||||
'SET_MASTER': extract_braced(source, 'if (strncmp(config, "usb.logging", 11)'),
|
||||
|
||||
@@ -28,14 +28,17 @@ struct File {
|
||||
struct FakeFS {
|
||||
std::map<std::string, std::vector<uint8_t>> files;
|
||||
bool fail_write = false, fail_publish = false;
|
||||
bool exists(const char* name) { return files.count(name); }
|
||||
bool remove(const char* name) { files.erase(name); return true; }
|
||||
unsigned calls = 0;
|
||||
bool exists(const char* name) { ++calls; return files.count(name); }
|
||||
bool remove(const char* name) { ++calls; files.erase(name); return true; }
|
||||
bool rename(const char* from, const char* to) {
|
||||
++calls;
|
||||
if (fail_publish && std::string(from) == "/display_prefs.tmp") return false;
|
||||
if (!exists(from) || exists(to)) return false;
|
||||
files[to] = files[from]; files.erase(from); return true;
|
||||
}
|
||||
File open(const char* name, const char* mode, bool = false) {
|
||||
++calls;
|
||||
if (*mode == 'w') files[name].clear();
|
||||
if (!exists(name)) return {};
|
||||
return File{this, name};
|
||||
@@ -326,6 +329,61 @@ int main() {
|
||||
'''
|
||||
|
||||
|
||||
HEADLESS_TEST = r'''
|
||||
#include <cassert>
|
||||
#include <helpers/ui/DisplayPowerSettings.h>
|
||||
using namespace mesh::ui;
|
||||
int main() {
|
||||
FakeFS fs;
|
||||
fs.files["/display_prefs"] = {'D', 'P', 2, 5, 42, 0, 1, 57, 0, 2, 0, 0};
|
||||
fs.files["/display_prefs.bak"] = {'b', 'a', 'c', 'k', 'u', 'p'};
|
||||
fs.files["/display_prefs.tmp"] = {'t', 'e', 'm', 'p'};
|
||||
const auto original = fs.files;
|
||||
for (bool pairing : {false, true}) for (bool wake : {false, true}) {
|
||||
displayPowerPrefs().battery = {DisplayMode::Off, 123};
|
||||
displayPowerPrefs().usb = {DisplayMode::On, 456};
|
||||
displayPowerPrefs().inbox = DisplayInboxMode::Unread;
|
||||
assert(!loadDisplayPowerSettings(&fs, pairing, wake));
|
||||
const auto& prefs = displayPowerPrefs();
|
||||
const auto mode = pairing ? DisplayMode::ButtonPairing : DisplayMode::Button;
|
||||
assert(prefs.battery.mode == mode && prefs.usb.mode == mode);
|
||||
assert(prefs.battery.seconds == 15 && prefs.usb.seconds == 15);
|
||||
assert(prefs.inbox == DisplayInboxMode::History && prefs.wake_on_boot == wake);
|
||||
assert(displayPairingSupported() == pairing);
|
||||
for (const char* key : {"display.mode", "display.timeout", "display.usb.mode",
|
||||
"display.usb.timeout", "display.inbox"}) {
|
||||
for (const char* prefix : {"get ", "set "}) for (const char* suffix : {"", " off", " typo"}) {
|
||||
char reply[160] = {};
|
||||
const std::string text = std::string(prefix) + key + suffix;
|
||||
assert(handleDisplayPowerCommand(text.c_str(), reply, sizeof(reply), true));
|
||||
assert(std::string(reply) == "Error: display unsupported");
|
||||
}
|
||||
}
|
||||
char reply[32] = "untouched";
|
||||
assert(!handleDisplayPowerCommand("get display.model", reply, sizeof(reply)));
|
||||
assert(std::string(reply) == "untouched");
|
||||
char bounded[3] = {'x', 'y', 'z'};
|
||||
assert(handleDisplayPowerCommand("get display.mode", bounded, 1));
|
||||
assert(bounded[0] == 0 && bounded[1] == 'y' && bounded[2] == 'z');
|
||||
migrateLegacyDisplayTimeout(0);
|
||||
assert(displayPowerPrefs().battery.mode == DisplayMode::On);
|
||||
assert(displayPowerPrefs().usb.mode == DisplayMode::On);
|
||||
migrateLegacyDisplayTimeout(42);
|
||||
assert(displayPowerPrefs().battery.mode == DisplayMode::Automatic);
|
||||
assert(displayPowerPrefs().usb.mode == DisplayMode::Automatic);
|
||||
assert(displayPowerPrefs().battery.seconds == 42 && displayPowerPrefs().usb.seconds == 42);
|
||||
assert(displayPowerPrefs().wake_on_boot == wake && displayPairingSupported() == pairing);
|
||||
migrateLegacyDisplayTimeout(3601);
|
||||
assert(displayPowerPrefs().battery.seconds == 42 && displayPowerPrefs().usb.seconds == 42);
|
||||
assert(fs.calls == 0 && fs.files == original);
|
||||
assert(!loadDisplayPowerSettings(nullptr, pairing, wake));
|
||||
assert(displayPowerPrefs().battery.mode == mode && displayPowerPrefs().wake_on_boot == wake);
|
||||
assert(fs.calls == 0 && fs.files == original);
|
||||
}
|
||||
}
|
||||
'''
|
||||
|
||||
|
||||
class DisplayPowerTest(unittest.TestCase):
|
||||
def test_companion_boot_wake_is_transport_and_layout_independent(self):
|
||||
mesh = (ROOT / "examples/companion_radio/MyMesh.cpp").read_text()
|
||||
@@ -344,6 +402,27 @@ class DisplayPowerTest(unittest.TestCase):
|
||||
infrastructure = (ROOT / "src/helpers/CommonCLI.cpp").read_text()
|
||||
self.assertIn("loadDisplayPowerSettings(fs, false);", infrastructure)
|
||||
|
||||
def test_headless_defaults_commands_and_legacy_migration_do_not_touch_files(self):
|
||||
compiler = shutil.which("g++") or shutil.which("clang++")
|
||||
self.assertIsNotNone(compiler, "host C++ compiler required")
|
||||
with tempfile.TemporaryDirectory(prefix="mesh-headless-display-test-") as name:
|
||||
work = Path(name)
|
||||
(work / "helpers").mkdir()
|
||||
(work / "helpers/IdentityStore.h").write_text(FS_STUB)
|
||||
(work / "test.cpp").write_text(HEADLESS_TEST)
|
||||
for label, definitions in (("absent", ()), ("null", ("-DDISPLAY_CLASS=NullDisplayDriver",))):
|
||||
with self.subTest(display=label):
|
||||
executable = work / (label + ".exe")
|
||||
result = subprocess.run([
|
||||
compiler, "-std=c++17", "-Wall", "-Wextra", "-Werror",
|
||||
*definitions, "-I" + str(work), "-I" + str(ROOT / "src"),
|
||||
str(work / "test.cpp"), str(ROOT / "src/helpers/ui/DisplayPowerSettings.cpp"),
|
||||
"-o", str(executable),
|
||||
], capture_output=True, text=True, timeout=60)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
result = subprocess.run([str(executable)], capture_output=True, text=True, timeout=10)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
|
||||
def test_policy_and_persistent_commands(self):
|
||||
compiler = shutil.which("g++") or shutil.which("clang++")
|
||||
self.assertIsNotNone(compiler, "host C++ compiler required")
|
||||
|
||||
@@ -238,7 +238,7 @@ def persistence_harness():
|
||||
"SERIALIZER": "template<typename Writer>\n" + extract_braced(
|
||||
source, "static bool writeCommonPrefsImage("),
|
||||
"STORE": extract_braced(source, "class CommonPrefsFileStore"),
|
||||
"METHODS": methods,
|
||||
"METHODS": ("\n#include <stddef.h>\ntemplate<typename Writer>\n" + extract_braced(source, "static void writeCommonRetryPrefs(") + "\n" + methods),
|
||||
"DYNAMIC": "", "SET_DEBUG": "", "SET_MASTER": "",
|
||||
"GET_DEBUG": "", "GET_MASTER": "",
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ class OtaTargetNameTest(unittest.TestCase):
|
||||
|
||||
def test_rak_full_lookup_fails_closed_without_heap_and_keeps_every_name(self):
|
||||
flags = ["-DOTA_TARGET_NAME_FRONT_CODED=1", "-DOTA_RAK15002_SD_STORE=1", "-DCOMPANION_RADIO_FULL=1"]
|
||||
data, offsets, lengths, firsts = gen_targets.compressed_name_chunks(
|
||||
data, offsets, lengths, firsts = gen_targets.grouped_name_chunks(
|
||||
self.rows, gen_targets.RAK_FULL_NAME_DECODE_CHUNK_SIZE)
|
||||
ordinary = gen_targets.compressed_name_chunks(self.rows)
|
||||
self.assertGreater(len(ordinary[0]) - len(data), 1500)
|
||||
@@ -134,6 +134,76 @@ class OtaTargetNameTest(unittest.TestCase):
|
||||
self.assertEqual(self.compile_and_lookup(flags + ["-DOTA_TEST_MALLOC_FAIL=1"],
|
||||
[self.rows[0][0], 0]), ["?", "?"])
|
||||
|
||||
def grouped_roundtrip(self, rows, chunk_size=4096):
|
||||
data, offsets, lengths, firsts = gen_targets.grouped_name_chunks(rows, chunk_size)
|
||||
names = []
|
||||
for group, length in enumerate(lengths):
|
||||
raw = zlib.decompress(data[offsets[group]:offsets[group + 1]], -15)
|
||||
self.assertEqual(len(raw), length)
|
||||
self.assertLessEqual(length, chunk_size)
|
||||
count, dictionary_bytes = raw[0], int.from_bytes(raw[1:3], "little")
|
||||
dictionary_end = 3 + dictionary_bytes
|
||||
dictionary = raw[3:dictionary_end].split(b"\0")
|
||||
self.assertEqual(dictionary[-1], b"")
|
||||
self.assertEqual(len(dictionary) - 1, count)
|
||||
position, previous, row = dictionary_end, b"", firsts[group]
|
||||
while row < firsts[group + 1]:
|
||||
prefix = raw[position]
|
||||
self.assertLessEqual(prefix, len(previous))
|
||||
end = raw.index(0, position + 1)
|
||||
previous = previous[:prefix] + raw[position + 1:end]
|
||||
aliases = raw[end + 1]
|
||||
self.assertGreater(aliases, 0)
|
||||
position = end + 2
|
||||
for index in raw[position:position + aliases]:
|
||||
self.assertLess(index, count)
|
||||
names.append((previous + dictionary[index]).decode("utf-8"))
|
||||
position += aliases; row += aliases
|
||||
self.assertEqual(row, firsts[group + 1])
|
||||
self.assertEqual(position, len(raw))
|
||||
self.assertEqual(names, [name for _, name in rows])
|
||||
return data, lengths
|
||||
|
||||
def test_rak_full_grouped_roles_keep_all_names_in_same_scratch_and_save_flash(self):
|
||||
data, lengths = self.grouped_roundtrip(self.rows)
|
||||
previous = gen_targets.compressed_name_chunks(self.rows, 4096)
|
||||
self.assertGreater(len(previous[0]) - len(data), 1000)
|
||||
self.assertLessEqual(max(lengths), 4096)
|
||||
# Unusual/legacy names stay complete; larger future inventories restart
|
||||
# board prefixes and carry a complete suffix dictionary in each chunk.
|
||||
future = gen_targets.target_rows(
|
||||
[f"board_{i:04d}_{role}" for i in range(700)
|
||||
for role in ("repeater", "companion_radio_usb", "room_server")]
|
||||
+ ["legacy_without_role", "odd_BOARD_repeatr", "case_Companion_ble"])
|
||||
_, chunks = self.grouped_roundtrip(future)
|
||||
self.assertGreater(len(chunks), 1)
|
||||
|
||||
def test_grouped_decoder_rejects_bad_dictionary_prefix_count_and_role_index(self):
|
||||
from zopfli_compress import raw_deflate_compress
|
||||
rows = gen_targets.target_rows(["hello"])
|
||||
header = gen_targets.generate_header(rows)
|
||||
data, offsets, lengths, firsts = gen_targets.grouped_name_chunks(rows)
|
||||
raw = zlib.decompress(data, -15)
|
||||
invalid = []
|
||||
for offset, value in ((0, 0), (1, 255), (3, 88), (4, 255), (-2, 0), (-1, 255)):
|
||||
changed = bytearray(raw); changed[offset] = value; invalid.append(bytes(changed))
|
||||
invalid.append(raw[:-1])
|
||||
for changed in invalid:
|
||||
stream = raw_deflate_compress(changed, numiterations=1000)
|
||||
altered = re.sub(
|
||||
r"(static const uint8_t NAMES\[\] = \{).*?(\n \};)",
|
||||
lambda match: match[1] + "\n " + ", ".join(f"0x{byte:02x}" for byte in stream) + "," + match[2],
|
||||
header, count=1, flags=re.S)
|
||||
altered = re.sub(
|
||||
r"(static const uint16_t OFFSETS\[\] = \{).*?(\n \};)",
|
||||
lambda match: match[1] + f"\n 0, {len(stream)}," + match[2], altered, count=1, flags=re.S)
|
||||
altered = re.sub(
|
||||
r"(static const uint16_t RAW_LENGTHS\[\] = \{).*?(\n \};)",
|
||||
lambda match: match[1] + f"\n {len(changed)}," + match[2], altered, count=1, flags=re.S)
|
||||
with self.subTest(raw=changed.hex()):
|
||||
flags = ["-DOTA_TARGET_NAME_FRONT_CODED=1", "-DOTA_RAK15002_SD_STORE=1", "-DCOMPANION_RADIO_FULL=1"]
|
||||
self.assertEqual(self.compile_and_lookup(flags, [rows[0][0]], altered), ["?"])
|
||||
|
||||
def test_rak_full_fingerprint_collisions_verify_the_entire_target_id(self):
|
||||
flags = ["-DOTA_TARGET_NAME_FRONT_CODED=1", "-DOTA_RAK15002_SD_STORE=1", "-DCOMPANION_RADIO_FULL=1"]
|
||||
known = {target for target, _ in self.rows}
|
||||
@@ -198,6 +268,8 @@ class OtaTargetNameTest(unittest.TestCase):
|
||||
with self.subTest(name=name):
|
||||
with self.assertRaises(ValueError):
|
||||
gen_targets.compressed_name_chunks([(1, name)])
|
||||
with self.assertRaises(ValueError):
|
||||
gen_targets.grouped_name_chunks([(1, name)])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -80,7 +80,7 @@ def common_code():
|
||||
"FIELDS": fields, "CONSTANTS": constants, "HELPERS": helpers,
|
||||
"LEGACY": extract_braced(header, "struct LegacyObserverTail") + ";",
|
||||
"SERIALIZER": "template<typename Writer>\n" + extract_braced(source, "static bool writeCommonPrefsImage("),
|
||||
"STORE": extract_braced(source, "class CommonPrefsFileStore"), "METHODS": methods,
|
||||
"STORE": extract_braced(source, "class CommonPrefsFileStore"), "METHODS": ("\n#include <stddef.h>\ntemplate<typename Writer>\n" + extract_braced(source, "static void writeCommonRetryPrefs(") + "\n" + methods),
|
||||
"DYNAMIC": extract_braced(header, "class BridgePrefs :"),
|
||||
"SET_DEBUG": "", "SET_MASTER": "", "GET_DEBUG": "", "GET_MASTER": "",
|
||||
}.items():
|
||||
|
||||
@@ -216,7 +216,7 @@ def production_harness(source, header):
|
||||
'SERIALIZER': 'template<typename Writer>\n' + extract_braced(
|
||||
source, 'static bool writeCommonPrefsImage('),
|
||||
'STORE': extract_braced(source, 'class CommonPrefsFileStore'),
|
||||
'METHODS': methods,
|
||||
'METHODS': ("\n#include <stddef.h>\ntemplate<typename Writer>\n" + extract_braced(source, "static void writeCommonRetryPrefs(") + "\n" + methods),
|
||||
'DYNAMIC': extract_braced(header, 'class RepeatPrefs :'),
|
||||
'SET_DEBUG': extract_braced(setter, branch), 'SET_MASTER': '',
|
||||
'GET_DEBUG': extract_braced(getter, branch), 'GET_MASTER': '',
|
||||
|
||||
Reference in new issue
Block a user